1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
39 struct passwd
*getpwnam
PARAMS ((char *name
));
42 #endif /* !WINDOWS32 */
44 /* A `struct linebuffer' is a structure which holds a line of text.
45 `readline' reads a line from a stream into a linebuffer
46 and works regardless of the length of the line. */
50 /* Note: This is the number of bytes malloc'ed for `buffer'
51 It does not indicate `buffer's real length.
52 Instead, a null char indicates end-of-string. */
57 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
58 #define freebuffer(lb) free ((lb)->buffer)
61 /* Types of "words" that can be read in a makefile. */
64 w_bogus
, w_eol
, w_static
, w_variable
, w_colon
, w_dcolon
, w_semicolon
,
65 w_comment
, w_varassign
69 /* A `struct conditionals' contains the information describing
70 all the active conditionals in a makefile.
72 The global variable `conditionals' contains the conditionals
73 information for the current makefile. It is initialized from
74 the static structure `toplevel_conditionals' and is later changed
75 to new structures for included makefiles. */
79 unsigned int if_cmds
; /* Depth of conditional nesting. */
80 unsigned int allocated
; /* Elts allocated in following arrays. */
81 char *ignoring
; /* Are we ignoring or interepreting? */
82 char *seen_else
; /* Have we already seen an `else'? */
85 static struct conditionals toplevel_conditionals
;
86 static struct conditionals
*conditionals
= &toplevel_conditionals
;
89 /* Default directories to search for include files in */
91 static char *default_include_directories
[] =
93 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
95 * This completly up to the user when they install MSVC or other packages.
96 * This is defined as a placeholder.
98 #define INCLUDEDIR "."
103 "/usr/local/include",
109 /* List of directories to search for include files in */
111 static char **include_directories
;
113 /* Maximum length of an element of the above. */
115 static unsigned int max_incl_len
;
117 /* The filename and pointer to line number of the
118 makefile currently being read in. */
120 const struct floc
*reading_file
;
122 /* The chain of makefiles read by read_makefile. */
124 static struct dep
*read_makefiles
= 0;
126 static int read_makefile
PARAMS ((char *filename
, int flags
));
127 static unsigned long readline
PARAMS ((struct linebuffer
*linebuffer
,
128 FILE *stream
, const struct floc
*flocp
));
129 static void do_define
PARAMS ((char *name
, unsigned int namelen
,
130 enum variable_origin origin
, FILE *infile
,
131 struct floc
*flocp
));
132 static int conditional_line
PARAMS ((char *line
, const struct floc
*flocp
));
133 static void record_files
PARAMS ((struct nameseq
*filenames
, char *pattern
, char *pattern_percent
,
134 struct dep
*deps
, unsigned int cmds_started
, char *commands
,
135 unsigned int commands_idx
, int two_colon
,
136 const struct floc
*flocp
, int set_default
));
137 static void record_target_var
PARAMS ((struct nameseq
*filenames
, char *defn
,
139 enum variable_origin origin
,
140 const struct floc
*flocp
));
141 static enum make_word_type get_next_mword
PARAMS ((char *buffer
, char *delim
,
142 char **startp
, unsigned int *length
));
144 /* Read in all the makefiles and return the chain of their names. */
147 read_all_makefiles (makefiles
)
150 unsigned int num_makefiles
= 0;
153 puts (_("Reading makefiles..."));
155 /* If there's a non-null variable MAKEFILES, its value is a list of
156 files to read first thing. But don't let it prevent reading the
157 default makefiles and don't let the default goal come from there. */
165 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
166 int save
= warn_undefined_variables_flag
;
167 warn_undefined_variables_flag
= 0;
169 value
= allocated_variable_expand ("$(MAKEFILES)");
171 warn_undefined_variables_flag
= save
;
174 /* Set NAME to the start of next token and LENGTH to its length.
175 MAKEFILES is updated for finding remaining tokens. */
178 while ((name
= find_next_token (&p
, &length
)) != 0)
182 (void) read_makefile (name
,
183 RM_NO_DEFAULT_GOAL
| RM_INCLUDED
| RM_DONTCARE
);
189 /* Read makefiles specified with -f switches. */
192 while (*makefiles
!= 0)
194 struct dep
*tail
= read_makefiles
;
195 register struct dep
*d
;
197 if (! read_makefile (*makefiles
, 0))
198 perror_with_name ("", *makefiles
);
200 /* Find the right element of read_makefiles. */
202 while (d
->next
!= tail
)
205 /* Use the storage read_makefile allocates. */
206 *makefiles
= dep_name (d
);
211 /* If there were no -f switches, try the default names. */
213 if (num_makefiles
== 0)
215 static char *default_makefiles
[] =
217 /* all lower case since readdir() (the vms version) 'lowercasifies' */
218 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
221 { "GNUmakefile", "Makefile", "SMakefile", 0 };
222 #else /* !Amiga && !VMS */
223 { "GNUmakefile", "makefile", "Makefile", 0 };
226 register char **p
= default_makefiles
;
227 while (*p
!= 0 && !file_exists_p (*p
))
232 if (! read_makefile (*p
, 0))
233 perror_with_name ("", *p
);
237 /* No default makefile was found. Add the default makefiles to the
238 `read_makefiles' chain so they will be updated if possible. */
239 struct dep
*tail
= read_makefiles
;
240 /* Add them to the tail, after any MAKEFILES variable makefiles. */
241 while (tail
!= 0 && tail
->next
!= 0)
243 for (p
= default_makefiles
; *p
!= 0; ++p
)
245 struct dep
*d
= (struct dep
*) xmalloc (sizeof (struct dep
));
247 d
->file
= enter_file (*p
);
248 d
->file
->dontcare
= 1;
249 /* Tell update_goal_chain to bail out as soon as this file is
250 made, and main not to die if we can't make this file. */
251 d
->changed
= RM_DONTCARE
;
263 return read_makefiles
;
266 /* Read file FILENAME as a makefile and add its contents to the data base.
268 FLAGS contains bits as above.
270 FILENAME is added to the `read_makefiles' chain.
272 Returns 1 if a file was found and read, 0 if not. */
275 read_makefile (filename
, flags
)
279 static char *collapsed
= 0;
280 static unsigned int collapsed_length
= 0;
281 register FILE *infile
;
282 struct linebuffer lb
;
283 unsigned int commands_len
= 200;
285 unsigned int commands_idx
= 0;
286 unsigned int cmds_started
;
289 int len
, reading_target
;
290 int ignoring
= 0, in_ignored_define
= 0;
291 int no_targets
= 0; /* Set when reading a rule without targets. */
292 struct floc fileinfo
;
293 char *passed_filename
= filename
;
295 struct nameseq
*filenames
= 0;
297 unsigned int nlines
= 0;
299 char *pattern
= 0, *pattern_percent
;
302 #if defined (WINDOWS32) || defined (__MSDOS__)
306 #define record_waiting_files() \
309 if (filenames != 0) \
310 record_files (filenames, pattern, pattern_percent, deps, \
311 cmds_started, commands, commands_idx, \
312 two_colon, &fileinfo, \
313 !(flags & RM_NO_DEFAULT_GOAL)); \
316 if (pattern) { free(pattern); pattern = 0; } \
319 fileinfo
.filenm
= filename
;
323 cmds_started
= fileinfo
.lineno
;
327 printf (_("Reading makefile `%s'"), fileinfo
.filenm
);
328 if (flags
& RM_NO_DEFAULT_GOAL
)
329 printf (_(" (no default goal)"));
330 if (flags
& RM_INCLUDED
)
331 printf (_(" (search path)"));
332 if (flags
& RM_DONTCARE
)
333 printf (_(" (don't care)"));
334 if (flags
& RM_NO_TILDE
)
335 printf (_(" (no ~ expansion)"));
339 /* First, get a stream to read. */
341 /* Expand ~ in FILENAME unless it came from `include',
342 in which case it was already done. */
343 if (!(flags
& RM_NO_TILDE
) && filename
[0] == '~')
345 char *expanded
= tilde_expand (filename
);
350 infile
= fopen (filename
, "r");
351 /* Save the error code so we print the right message later. */
352 makefile_errno
= errno
;
354 /* If the makefile wasn't found and it's either a makefile from
355 the `MAKEFILES' variable or an included makefile,
356 search the included makefile search path for this makefile. */
357 if (infile
== 0 && (flags
& RM_INCLUDED
) && *filename
!= '/')
359 register unsigned int i
;
360 for (i
= 0; include_directories
[i
] != 0; ++i
)
362 char *name
= concat (include_directories
[i
], "/", filename
);
363 infile
= fopen (name
, "r");
374 /* Add FILENAME to the chain of read makefiles. */
375 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
376 deps
->next
= read_makefiles
;
377 read_makefiles
= deps
;
379 deps
->file
= lookup_file (filename
);
382 deps
->file
= enter_file (xstrdup (filename
));
383 if (flags
& RM_DONTCARE
)
384 deps
->file
->dontcare
= 1;
386 if (filename
!= passed_filename
)
388 filename
= deps
->file
->name
;
389 deps
->changed
= flags
;
392 /* If the makefile can't be found at all, give up entirely. */
396 /* If we did some searching, errno has the error from the last
397 attempt, rather from FILENAME itself. Restore it in case the
398 caller wants to use it in a message. */
399 errno
= makefile_errno
;
403 reading_file
= &fileinfo
;
405 /* Loop over lines in the file.
406 The strategy is to accumulate target names in FILENAMES, dependencies
407 in DEPS and commands in COMMANDS. These are used to define a rule
408 when the start of the next rule (or eof) is encountered. */
411 commands
= xmalloc (200);
413 while (!feof (infile
))
415 fileinfo
.lineno
+= nlines
;
416 nlines
= readline (&lb
, infile
, &fileinfo
);
418 /* Check for a shell command line first.
419 If it is not one, we can stop treating tab specially. */
420 if (lb
.buffer
[0] == '\t')
422 /* This line is a probably shell command. */
426 /* Ignore the commands in a rule with no targets. */
429 /* If there is no preceding rule line, don't treat this line
430 as a command, even though it begins with a tab character.
431 SunOS 4 make appears to behave this way. */
436 /* Yep, this is a shell command, and we don't care. */
439 /* Append this command line to the line being accumulated. */
441 if (commands_idx
== 0)
442 cmds_started
= fileinfo
.lineno
;
444 if (len
+ 1 + commands_idx
> commands_len
)
446 commands_len
= (len
+ 1 + commands_idx
) * 2;
447 commands
= (char *) xrealloc (commands
, commands_len
);
449 bcopy (p
, &commands
[commands_idx
], len
);
451 commands
[commands_idx
++] = '\n';
457 /* This line is not a shell command line. Don't worry about tabs. */
459 if (collapsed_length
< lb
.size
)
461 collapsed_length
= lb
.size
;
464 collapsed
= (char *) xmalloc (collapsed_length
);
466 strcpy (collapsed
, lb
.buffer
);
467 /* Collapse continuation lines. */
468 collapse_continuations (collapsed
);
469 remove_comments (collapsed
);
471 /* Compare a word, both length and contents. */
472 #define word1eq(s, l) (len == l && strneq (s, p, l))
477 /* This line is completely empty. */
480 /* Find the end of the first token. Note we don't need to worry about
481 * ":" here since we compare tokens by length (so "export" will never
482 * be equal to "export:").
484 for (p2
= p
+1; *p2
!= '\0' && !isspace(*p2
); ++p2
)
488 /* Find the start of the second token. If it's a `:' remember it,
489 since it can't be a preprocessor token--this allows targets named
490 `ifdef', `export', etc. */
492 while (isspace (*p2
))
496 else if (p2
[0] == ':' && p2
[1] == '\0')
499 goto skip_conditionals
;
502 /* We must first check for conditional and `define' directives before
503 ignoring anything, since they control what we will do with
506 if (!in_ignored_define
507 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
508 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
509 || word1eq ("else", 4) || word1eq ("endif", 5)))
511 int i
= conditional_line (p
, &fileinfo
);
515 fatal (&fileinfo
, _("invalid syntax in conditional"));
519 if (word1eq ("endef", 5))
521 if (in_ignored_define
)
522 in_ignored_define
= 0;
524 fatal (&fileinfo
, _("extraneous `endef'"));
528 if (word1eq ("define", 6))
531 in_ignored_define
= 1;
534 p2
= next_token (p
+ 6);
536 fatal (&fileinfo
, _("empty variable name"));
538 /* Let the variable name be the whole rest of the line,
539 with trailing blanks stripped (comments have already been
540 removed), so it could be a complex variable/function
541 reference that might contain blanks. */
542 p
= index (p2
, '\0');
543 while (isblank (p
[-1]))
545 do_define (p2
, p
- p2
, o_file
, infile
, &fileinfo
);
550 if (word1eq ("override", 8))
552 p2
= next_token (p
+ 8);
554 error (&fileinfo
, _("empty `override' directive"));
555 if (strneq (p2
, "define", 6) && (isblank (p2
[6]) || p2
[6] == '\0'))
558 in_ignored_define
= 1;
561 p2
= next_token (p2
+ 6);
563 fatal (&fileinfo
, _("empty variable name"));
565 /* Let the variable name be the whole rest of the line,
566 with trailing blanks stripped (comments have already been
567 removed), so it could be a complex variable/function
568 reference that might contain blanks. */
569 p
= index (p2
, '\0');
570 while (isblank (p
[-1]))
572 do_define (p2
, p
- p2
, o_override
, infile
, &fileinfo
);
576 && !try_variable_definition (&fileinfo
, p2
, o_override
))
577 error (&fileinfo
, _("invalid `override' directive"));
584 /* Ignore the line. We continue here so conditionals
585 can appear in the middle of a rule. */
588 if (!reading_target
&& word1eq ("export", 6))
591 p2
= next_token (p
+ 6);
593 export_all_variables
= 1;
594 v
= try_variable_definition (&fileinfo
, p2
, o_file
);
596 v
->export
= v_export
;
600 for (p
= find_next_token (&p2
, &len
); p
!= 0;
601 p
= find_next_token (&p2
, &len
))
603 v
= lookup_variable (p
, len
);
605 v
= define_variable (p
, len
, "", o_file
, 0);
606 v
->export
= v_export
;
610 else if (!reading_target
&& word1eq ("unexport", 8))
614 p2
= next_token (p
+ 8);
616 export_all_variables
= 0;
617 for (p
= find_next_token (&p2
, &len
); p
!= 0;
618 p
= find_next_token (&p2
, &len
))
620 v
= lookup_variable (p
, len
);
622 v
= define_variable (p
, len
, "", o_file
, 0);
623 v
->export
= v_noexport
;
626 else if (word1eq ("vpath", 5))
630 p2
= variable_expand (p
+ 5);
631 p
= find_next_token (&p2
, &len
);
634 pattern
= savestring (p
, len
);
635 p
= find_next_token (&p2
, &len
);
636 /* No searchpath means remove all previous
637 selective VPATH's with the same pattern. */
640 /* No pattern means remove all previous selective VPATH's. */
642 construct_vpath_list (pattern
, p
);
646 else if (word1eq ("include", 7) || word1eq ("-include", 8)
647 || word1eq ("sinclude", 8))
649 /* We have found an `include' line specifying a nested
650 makefile to be read at this point. */
651 struct conditionals
*save
, new_conditionals
;
652 struct nameseq
*files
;
653 /* "-include" (vs "include") says no error if the file does not
654 exist. "sinclude" is an alias for this from SGI. */
655 int noerror
= p
[0] != 'i';
657 p
= allocated_variable_expand (next_token (p
+ (noerror
? 8 : 7)));
661 _("no file name for `%sinclude'"), noerror
? "-" : "");
665 /* Parse the list of file names. */
667 files
= multi_glob (parse_file_seq (&p2
, '\0',
668 sizeof (struct nameseq
),
670 sizeof (struct nameseq
));
673 /* Save the state of conditionals and start
674 the included makefile with a clean slate. */
676 bzero ((char *) &new_conditionals
, sizeof new_conditionals
);
677 conditionals
= &new_conditionals
;
679 /* Record the rules that are waiting so they will determine
680 the default goal before those in the included makefile. */
681 record_waiting_files ();
683 /* Read each included makefile. */
686 struct nameseq
*next
= files
->next
;
687 char *name
= files
->name
;
688 free ((char *)files
);
691 if (! read_makefile (name
, (RM_INCLUDED
| RM_NO_TILDE
692 | (noerror
? RM_DONTCARE
: 0)))
694 error (&fileinfo
, "%s: %s", name
, strerror (errno
));
698 /* Free any space allocated by conditional_line. */
699 if (conditionals
->ignoring
)
700 free (conditionals
->ignoring
);
701 if (conditionals
->seen_else
)
702 free (conditionals
->seen_else
);
706 reading_file
= &fileinfo
;
709 else if (try_variable_definition (&fileinfo
, p
, o_file
))
710 /* This line has been dealt with. */
712 else if (lb
.buffer
[0] == '\t')
714 p
= collapsed
; /* Ignore comments. */
718 /* The line is completely blank; that is harmless. */
720 /* This line starts with a tab but was not caught above
721 because there was no preceding target, and the line
722 might have been usable as a variable definition.
723 But now it is definitely lossage. */
724 fatal(&fileinfo
, _("commands commence before first target"));
728 /* This line describes some target files. This is complicated by
729 the existence of target-specific variables, because we can't
730 expand the entire line until we know if we have one or not. So
731 we expand the line word by word until we find the first `:',
732 then check to see if it's a target-specific variable.
734 In this algorithm, `lb_next' will point to the beginning of the
735 unexpanded parts of the input buffer, while `p2' points to the
736 parts of the expanded buffer we haven't searched yet. */
738 enum make_word_type wtype
;
739 enum variable_origin v_origin
;
740 char *cmdleft
, *lb_next
;
741 unsigned int len
, plen
= 0;
744 /* Record the previous rule. */
746 record_waiting_files ();
748 /* Search the line for an unquoted ; that is not after an
750 cmdleft
= find_char_unquote (lb
.buffer
, ";#", 0);
751 if (cmdleft
!= 0 && *cmdleft
== '#')
753 /* We found a comment before a semicolon. */
757 else if (cmdleft
!= 0)
758 /* Found one. Cut the line short there before expanding it. */
761 collapse_continuations (lb
.buffer
);
763 /* We can't expand the entire line, since if it's a per-target
764 variable we don't want to expand it. So, walk from the
765 beginning, expanding as we go, and looking for "interesting"
766 chars. The first word is always expandable. */
767 wtype
= get_next_mword(lb
.buffer
, NULL
, &lb_next
, &len
);
772 fatal(&fileinfo
, _("missing rule before commands"));
773 /* This line contained something but turned out to be nothing
774 but whitespace (a comment?). */
779 /* We accept and ignore rules without targets for
780 compatibility with SunOS 4 make. */
788 p2
= variable_expand_string(NULL
, lb_next
, len
);
794 /* Look for a semicolon in the expanded line. */
795 cmdleft
= find_char_unquote (p2
, ";", 0);
799 unsigned long p2_off
= p2
- variable_buffer
;
800 unsigned long cmd_off
= cmdleft
- variable_buffer
;
801 char *pend
= p2
+ strlen(p2
);
803 /* Append any remnants of lb, then cut the line short
807 /* One school of thought says that you shouldn't expand
808 here, but merely copy, since now you're beyond a ";"
809 and into a command script. However, the old parser
810 expanded the whole line, so we continue that for
811 backwards-compatiblity. Also, it wouldn't be
812 entirely consistent, since we do an unconditional
813 expand below once we know we don't have a
814 target-specific variable. */
815 (void)variable_expand_string(pend
, lb_next
, (long)-1);
816 lb_next
+= strlen(lb_next
);
817 p2
= variable_buffer
+ p2_off
;
818 cmdleft
= variable_buffer
+ cmd_off
+ 1;
822 colonp
= find_char_unquote(p2
, ":", 0);
823 #if defined(__MSDOS__) || defined(WINDOWS32)
824 /* The drive spec brain-damage strikes again... */
825 /* Note that the only separators of targets in this context
826 are whitespace and a left paren. If others are possible,
827 they should be added to the string in the call to index. */
828 while (colonp
&& (colonp
[1] == '/' || colonp
[1] == '\\') &&
829 colonp
> p2
&& isalpha(colonp
[-1]) &&
830 (colonp
== p2
+ 1 || index(" \t(", colonp
[-2]) != 0))
831 colonp
= find_char_unquote(colonp
+ 1, ":", 0);
836 wtype
= get_next_mword(lb_next
, NULL
, &lb_next
, &len
);
842 p2
= variable_expand_string(p2
, lb_next
, len
);
843 /* We don't need to worry about cmdleft here, because if it was
844 found in the variable_buffer the entire buffer has already
845 been expanded... we'll never get here. */
848 p2
= next_token (variable_buffer
);
850 /* If the word we're looking at is EOL, see if there's _anything_
851 on the line. If not, a variable expanded to nothing, so ignore
852 it. If so, we can't parse this line so punt. */
856 /* There's no need to be ivory-tower about this: check for
857 one of the most common bugs found in makefiles... */
858 fatal (&fileinfo
, _("missing separator%s"),
859 !strneq(lb
.buffer
, " ", 8) ? ""
860 : _(" (did you mean TAB instead of 8 spaces?)"));
864 /* Make the colon the end-of-string so we know where to stop
865 looking for targets. */
867 filenames
= multi_glob (parse_file_seq (&p2
, '\0',
868 sizeof (struct nameseq
),
870 sizeof (struct nameseq
));
875 /* We accept and ignore rules without targets for
876 compatibility with SunOS 4 make. */
880 /* This should never be possible; we handled it above. */
884 /* Is this a one-colon or two-colon entry? */
885 two_colon
= *p2
== ':';
889 /* Test to see if it's a target-specific variable. Copy the rest
890 of the buffer over, possibly temporarily (we'll expand it later
891 if it's not a target-specific variable). PLEN saves the length
892 of the unparsed section of p2, for later. */
893 if (*lb_next
!= '\0')
895 unsigned int l
= p2
- variable_buffer
;
897 (void)variable_buffer_output(p2
+plen
,
898 lb_next
, strlen(lb_next
)+1);
899 p2
= variable_buffer
+ l
;
901 wtype
= get_next_mword(p2
, NULL
, &p
, &len
);
903 if (wtype
== w_static
&& (len
== (sizeof("override")-1)
904 && strneq(p
, "override", len
)))
906 v_origin
= o_override
;
907 (void)get_next_mword(p
+len
, NULL
, &p
, &len
);
909 else if (wtype
!= w_eol
)
910 wtype
= get_next_mword(p
+len
, NULL
, NULL
, NULL
);
912 if (wtype
== w_varassign
|| v_origin
== o_override
)
914 record_target_var(filenames
, p
, two_colon
, v_origin
, &fileinfo
);
919 /* This is a normal target, _not_ a target-specific variable.
920 Unquote any = in the dependency list. */
921 find_char_unquote (lb_next
, "=", 0);
923 /* We have some targets, so don't ignore the following commands. */
926 /* Expand the dependencies, etc. */
927 if (*lb_next
!= '\0')
929 unsigned int l
= p2
- variable_buffer
;
930 (void)variable_expand_string(p2
+ plen
, lb_next
, (long)-1);
931 p2
= variable_buffer
+ l
;
933 /* Look for a semicolon in the expanded line. */
936 cmdleft
= find_char_unquote (p2
, ";", 0);
942 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
944 while (p
!= 0 && p
[-1] == '\\')
946 register char *q
= &p
[-1];
947 register int backslash
= 0;
949 backslash
= !backslash
;
951 p
= index (p
+ 1, ':');
956 /* Here, the situation is quite complicated. Let's have a look
957 at a couple of targets:
965 The rule is that it's only a target, if there are TWO :'s
966 OR a space around the :.
968 if (p
&& !(isspace(p
[1]) || !p
[1] || isspace(p
[-1])))
971 #if defined (WINDOWS32) || defined (__MSDOS__)
974 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
975 if (p
!= 0 && (p
[1] == '\\' || p
[1] == '/') &&
977 (p
== p2
+ 1 || index(" \t:(", p
[-2]) != 0)) {
978 p
= index(p
+ 1, ':');
981 } while (check_again
);
985 struct nameseq
*target
;
986 target
= parse_file_seq (&p2
, ':', sizeof (struct nameseq
), 1);
989 fatal (&fileinfo
, _("missing target pattern"));
990 else if (target
->next
!= 0)
991 fatal (&fileinfo
, _("multiple target patterns"));
992 pattern
= target
->name
;
993 pattern_percent
= find_percent (pattern
);
994 if (pattern_percent
== 0)
995 fatal (&fileinfo
, _("target pattern contains no `%%'"));
996 free((char *)target
);
1001 /* Parse the dependencies. */
1002 deps
= (struct dep
*)
1003 multi_glob (parse_file_seq (&p2
, '\0', sizeof (struct dep
), 1),
1004 sizeof (struct dep
));
1009 /* Semicolon means rest of line is a command. */
1010 unsigned int len
= strlen (cmdleft
);
1012 cmds_started
= fileinfo
.lineno
;
1014 /* Add this command line to the buffer. */
1015 if (len
+ 2 > commands_len
)
1017 commands_len
= (len
+ 2) * 2;
1018 commands
= (char *) xrealloc (commands
, commands_len
);
1020 bcopy (cmdleft
, commands
, len
);
1021 commands_idx
+= len
;
1022 commands
[commands_idx
++] = '\n';
1028 /* We get here except in the case that we just read a rule line.
1029 Record now the last rule we read, so following spurious
1030 commands are properly diagnosed. */
1031 record_waiting_files ();
1035 if (conditionals
->if_cmds
)
1036 fatal (&fileinfo
, _("missing `endif'"));
1038 /* At eof, record the last rule. */
1039 record_waiting_files ();
1042 free ((char *) commands
);
1050 /* Execute a `define' directive.
1051 The first line has already been read, and NAME is the name of
1052 the variable to be defined. The following lines remain to be read.
1053 LINENO, INFILE and FILENAME refer to the makefile being read.
1054 The value returned is LINENO, updated for lines read here. */
1057 do_define (name
, namelen
, origin
, infile
, flocp
)
1059 unsigned int namelen
;
1060 enum variable_origin origin
;
1064 struct linebuffer lb
;
1065 unsigned int nlines
= 0;
1066 unsigned int length
= 100;
1067 char *definition
= (char *) xmalloc (100);
1068 register unsigned int idx
= 0;
1071 /* Expand the variable name. */
1072 char *var
= (char *) alloca (namelen
+ 1);
1073 bcopy (name
, var
, namelen
);
1074 var
[namelen
] = '\0';
1075 var
= variable_expand (var
);
1078 while (!feof (infile
))
1082 flocp
->lineno
+= nlines
;
1083 nlines
= readline (&lb
, infile
, flocp
);
1085 collapse_continuations (lb
.buffer
);
1087 p
= next_token (lb
.buffer
);
1089 if ((len
== 5 || (len
> 5 && isblank (p
[5])))
1090 && strneq (p
, "endef", 5))
1093 remove_comments (p
);
1094 if (*next_token (p
) != '\0')
1095 error (flocp
, _("Extraneous text after `endef' directive"));
1096 /* Define the variable. */
1098 definition
[0] = '\0';
1100 definition
[idx
- 1] = '\0';
1101 (void) define_variable (var
, strlen (var
), definition
, origin
, 1);
1108 len
= strlen (lb
.buffer
);
1109 /* Increase the buffer size if necessary. */
1110 if (idx
+ len
+ 1 > length
)
1112 length
= (idx
+ len
) * 2;
1113 definition
= (char *) xrealloc (definition
, length
+ 1);
1116 bcopy (lb
.buffer
, &definition
[idx
], len
);
1118 /* Separate lines with a newline. */
1119 definition
[idx
++] = '\n';
1124 fatal (flocp
, _("missing `endef', unterminated `define'"));
1130 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1131 "ifneq", "else" and "endif".
1132 LINE is the input line, with the command as its first word.
1134 FILENAME and LINENO are the filename and line number in the
1135 current makefile. They are used for error messages.
1137 Value is -1 if the line is invalid,
1138 0 if following text should be interpreted,
1139 1 if following text should be ignored. */
1142 conditional_line (line
, flocp
)
1144 const struct floc
*flocp
;
1148 register unsigned int i
;
1152 /* It's an "if..." command. */
1153 notdef
= line
[2] == 'n';
1156 cmdname
= line
[3] == 'd' ? "ifndef" : "ifneq";
1157 line
+= cmdname
[3] == 'd' ? 7 : 6;
1161 cmdname
= line
[2] == 'd' ? "ifdef" : "ifeq";
1162 line
+= cmdname
[2] == 'd' ? 6 : 5;
1167 /* It's an "else" or "endif" command. */
1168 notdef
= line
[1] == 'n';
1169 cmdname
= notdef
? "endif" : "else";
1170 line
+= notdef
? 5 : 4;
1173 line
= next_token (line
);
1175 if (*cmdname
== 'e')
1178 error (flocp
, _("Extraneous text after `%s' directive"), cmdname
);
1179 /* "Else" or "endif". */
1180 if (conditionals
->if_cmds
== 0)
1181 fatal (flocp
, _("extraneous `%s'"), cmdname
);
1182 /* NOTDEF indicates an `endif' command. */
1184 --conditionals
->if_cmds
;
1185 else if (conditionals
->seen_else
[conditionals
->if_cmds
- 1])
1186 fatal (flocp
, _("only one `else' per conditional"));
1189 /* Toggle the state of ignorance. */
1190 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1191 = !conditionals
->ignoring
[conditionals
->if_cmds
- 1];
1192 /* Record that we have seen an `else' in this conditional.
1193 A second `else' will be erroneous. */
1194 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 1;
1196 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1197 if (conditionals
->ignoring
[i
])
1202 if (conditionals
->allocated
== 0)
1204 conditionals
->allocated
= 5;
1205 conditionals
->ignoring
= (char *) xmalloc (conditionals
->allocated
);
1206 conditionals
->seen_else
= (char *) xmalloc (conditionals
->allocated
);
1209 ++conditionals
->if_cmds
;
1210 if (conditionals
->if_cmds
> conditionals
->allocated
)
1212 conditionals
->allocated
+= 5;
1213 conditionals
->ignoring
= (char *)
1214 xrealloc (conditionals
->ignoring
, conditionals
->allocated
);
1215 conditionals
->seen_else
= (char *)
1216 xrealloc (conditionals
->seen_else
, conditionals
->allocated
);
1219 /* Record that we have seen an `if...' but no `else' so far. */
1220 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 0;
1222 /* Search through the stack to see if we're already ignoring. */
1223 for (i
= 0; i
< conditionals
->if_cmds
- 1; ++i
)
1224 if (conditionals
->ignoring
[i
])
1226 /* We are already ignoring, so just push a level
1227 to match the next "else" or "endif", and keep ignoring.
1228 We don't want to expand variables in the condition. */
1229 conditionals
->ignoring
[conditionals
->if_cmds
- 1] = 1;
1233 if (cmdname
[notdef
? 3 : 2] == 'd')
1235 /* "Ifdef" or "ifndef". */
1237 register char *p
= end_of_token (line
);
1242 v
= lookup_variable (line
, i
);
1243 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1244 = (v
!= 0 && *v
->value
!= '\0') == notdef
;
1248 /* "Ifeq" or "ifneq". */
1251 char termin
= *line
== '(' ? ',' : *line
;
1253 if (termin
!= ',' && termin
!= '"' && termin
!= '\'')
1257 /* Find the end of the first string. */
1260 register int count
= 0;
1261 for (; *line
!= '\0'; ++line
)
1264 else if (*line
== ')')
1266 else if (*line
== ',' && count
<= 0)
1270 while (*line
!= '\0' && *line
!= termin
)
1278 /* Strip blanks after the first string. */
1280 while (isblank (p
[-1]))
1287 s2
= variable_expand (s1
);
1288 /* We must allocate a new copy of the expanded string because
1289 variable_expand re-uses the same buffer. */
1291 s1
= (char *) alloca (len
+ 1);
1292 bcopy (s2
, s1
, len
+ 1);
1295 /* Find the start of the second string. */
1296 line
= next_token (line
);
1298 termin
= termin
== ',' ? ')' : *line
;
1299 if (termin
!= ')' && termin
!= '"' && termin
!= '\'')
1302 /* Find the end of the second string. */
1305 register int count
= 0;
1306 s2
= next_token (line
);
1307 for (line
= s2
; *line
!= '\0'; ++line
)
1311 else if (*line
== ')')
1324 while (*line
!= '\0' && *line
!= termin
)
1332 line
= next_token (++line
);
1334 error (flocp
, _("Extraneous text after `%s' directive"), cmdname
);
1336 s2
= variable_expand (s2
);
1337 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1338 = streq (s1
, s2
) == notdef
;
1341 /* Search through the stack to see if we're ignoring. */
1342 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1343 if (conditionals
->ignoring
[i
])
1348 /* Remove duplicate dependencies in CHAIN. */
1351 uniquize_deps (chain
)
1354 register struct dep
*d
;
1356 /* Make sure that no dependencies are repeated. This does not
1357 really matter for the purpose of updating targets, but it
1358 might make some names be listed twice for $^ and $?. */
1360 for (d
= chain
; d
!= 0; d
= d
->next
)
1362 struct dep
*last
, *next
;
1367 if (streq (dep_name (d
), dep_name (next
)))
1369 struct dep
*n
= next
->next
;
1371 if (next
->name
!= 0 && next
->name
!= d
->name
)
1374 free ((char *) next
);
1385 /* Record target-specific variable values for files FILENAMES.
1386 TWO_COLON is nonzero if a double colon was used.
1388 The links of FILENAMES are freed, and so are any names in it
1389 that are not incorporated into other data structures.
1391 If the target is a pattern, add the variable to the pattern-specific
1392 variable value list. */
1395 record_target_var (filenames
, defn
, two_colon
, origin
, flocp
)
1396 struct nameseq
*filenames
;
1399 enum variable_origin origin
;
1400 const struct floc
*flocp
;
1402 struct nameseq
*nextf
;
1403 struct variable_set_list
*global
;
1405 global
= current_variable_set_list
;
1407 for (; filenames
!= 0; filenames
= nextf
)
1410 register char *name
= filenames
->name
;
1411 struct variable_set_list
*vlist
;
1415 nextf
= filenames
->next
;
1416 free ((char *) filenames
);
1418 /* If it's a pattern target, then add it to the pattern-specific
1420 percent
= find_percent (name
);
1423 struct pattern_var
*p
;
1425 /* Get a reference for this pattern-specific variable struct. */
1426 p
= create_pattern_var(name
, percent
);
1434 /* Get a file reference for this file, and initialize it. */
1435 f
= enter_file (name
);
1436 initialize_file_variables (f
);
1437 vlist
= f
->variables
;
1441 /* Make the new variable context current and define the variable. */
1442 current_variable_set_list
= vlist
;
1443 v
= try_variable_definition (flocp
, defn
, origin
);
1445 error (flocp
, _("Malformed per-target variable definition"));
1448 /* If it's not an override, check to see if there was a command-line
1449 setting. If so, reset the value. */
1450 if (origin
!= o_override
)
1452 struct variable
*gv
;
1453 int len
= strlen(v
->name
);
1455 current_variable_set_list
= global
;
1456 gv
= lookup_variable (v
->name
, len
);
1457 if (gv
&& (gv
->origin
== o_env_override
|| gv
->origin
== o_command
))
1458 define_variable_in_set (v
->name
, len
, gv
->value
, gv
->origin
,
1459 gv
->recursive
, vlist
->set
);
1462 /* Free name if not needed further. */
1463 if (name
!= fname
&& (name
< fname
|| name
> fname
+ strlen (fname
)))
1467 current_variable_set_list
= global
;
1470 /* Record a description line for files FILENAMES,
1471 with dependencies DEPS, commands to execute described
1472 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1473 TWO_COLON is nonzero if a double colon was used.
1474 If not nil, PATTERN is the `%' pattern to make this
1475 a static pattern rule, and PATTERN_PERCENT is a pointer
1476 to the `%' within it.
1478 The links of FILENAMES are freed, and so are any names in it
1479 that are not incorporated into other data structures. */
1482 record_files (filenames
, pattern
, pattern_percent
, deps
, cmds_started
,
1483 commands
, commands_idx
, two_colon
, flocp
, set_default
)
1484 struct nameseq
*filenames
;
1485 char *pattern
, *pattern_percent
;
1487 unsigned int cmds_started
;
1489 unsigned int commands_idx
;
1491 const struct floc
*flocp
;
1494 struct nameseq
*nextf
;
1496 unsigned int max_targets
= 0, target_idx
= 0;
1497 char **targets
= 0, **target_percents
= 0;
1498 struct commands
*cmds
;
1500 if (commands_idx
> 0)
1502 cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
1503 cmds
->fileinfo
.filenm
= flocp
->filenm
;
1504 cmds
->fileinfo
.lineno
= cmds_started
;
1505 cmds
->commands
= savestring (commands
, commands_idx
);
1506 cmds
->command_lines
= 0;
1511 for (; filenames
!= 0; filenames
= nextf
)
1514 register char *name
= filenames
->name
;
1515 register struct file
*f
;
1516 register struct dep
*d
;
1518 char *implicit_percent
;
1520 nextf
= filenames
->next
;
1521 free ((char *) filenames
);
1523 implicit_percent
= find_percent (name
);
1524 implicit
|= implicit_percent
!= 0;
1526 if (implicit
&& pattern
!= 0)
1527 fatal (flocp
, _("mixed implicit and static pattern rules"));
1529 if (implicit
&& implicit_percent
== 0)
1530 fatal (flocp
, _("mixed implicit and normal rules"));
1537 targets
= (char **) xmalloc (5 * sizeof (char *));
1538 target_percents
= (char **) xmalloc (5 * sizeof (char *));
1541 else if (target_idx
== max_targets
- 1)
1544 targets
= (char **) xrealloc ((char *) targets
,
1545 max_targets
* sizeof (char *));
1547 = (char **) xrealloc ((char *) target_percents
,
1548 max_targets
* sizeof (char *));
1550 targets
[target_idx
] = name
;
1551 target_percents
[target_idx
] = implicit_percent
;
1556 /* If there are multiple filenames, copy the chain DEPS
1557 for all but the last one. It is not safe for the same deps
1558 to go in more than one place in the data base. */
1559 this = nextf
!= 0 ? copy_dep_chain (deps
) : deps
;
1563 /* If this is an extended static rule:
1564 `targets: target%pattern: dep%pattern; cmds',
1565 translate each dependency pattern into a plain filename
1566 using the target pattern and this target's name. */
1567 if (!pattern_matches (pattern
, pattern_percent
, name
))
1569 /* Give a warning if the rule is meaningless. */
1571 _("target `%s' doesn't match the target pattern"), name
);
1576 /* We use patsubst_expand to do the work of translating
1577 the target pattern, the target's name and the dependencies'
1578 patterns into plain dependency names. */
1579 char *buffer
= variable_expand ("");
1581 for (d
= this; d
!= 0; d
= d
->next
)
1584 char *percent
= find_percent (d
->name
);
1587 o
= patsubst_expand (buffer
, name
, pattern
, d
->name
,
1588 pattern_percent
, percent
);
1590 d
->name
= savestring (buffer
, o
- buffer
);
1597 /* Single-colon. Combine these dependencies
1598 with others in file's existing record, if any. */
1599 f
= enter_file (name
);
1601 if (f
->double_colon
)
1603 _("target file `%s' has both : and :: entries"), f
->name
);
1605 /* If CMDS == F->CMDS, this target was listed in this rule
1606 more than once. Just give a warning since this is harmless. */
1607 if (cmds
!= 0 && cmds
== f
->cmds
)
1608 error (flocp
, _("target `%s' given more than once in the same rule."),
1611 /* Check for two single-colon entries both with commands.
1612 Check is_target so that we don't lose on files such as .c.o
1613 whose commands were preinitialized. */
1614 else if (cmds
!= 0 && f
->cmds
!= 0 && f
->is_target
)
1616 error (&cmds
->fileinfo
,
1617 _("warning: overriding commands for target `%s'"), f
->name
);
1618 error (&f
->cmds
->fileinfo
,
1619 _("warning: ignoring old commands for target `%s'"),
1625 /* Defining .DEFAULT with no deps or cmds clears it. */
1626 if (f
== default_file
&& this == 0 && cmds
== 0)
1630 /* Defining .SUFFIXES with no dependencies
1631 clears out the list of suffixes. */
1632 if (f
== suffix_file
&& this == 0)
1637 struct dep
*nextd
= d
->next
;
1644 else if (f
->deps
!= 0)
1646 /* Add the file's old deps and the new ones in THIS together. */
1648 struct dep
*firstdeps
, *moredeps
;
1651 /* This is the rule with commands, so put its deps first.
1652 The rationale behind this is that $< expands to the
1653 first dep in the chain, and commands use $< expecting
1654 to get the dep that rule specifies. */
1660 /* Append the new deps to the old ones. */
1661 firstdeps
= f
->deps
;
1666 firstdeps
= moredeps
;
1670 while (d
->next
!= 0)
1675 f
->deps
= firstdeps
;
1680 /* If this is a static pattern rule, set the file's stem to
1681 the part of its name that matched the `%' in the pattern,
1682 so you can use $* in the commands. */
1685 static char *percent
= "%";
1686 char *buffer
= variable_expand ("");
1687 char *o
= patsubst_expand (buffer
, name
, pattern
, percent
,
1688 pattern_percent
, percent
);
1689 f
->stem
= savestring (buffer
, o
- buffer
);
1694 /* Double-colon. Make a new record
1695 even if the file already has one. */
1696 f
= lookup_file (name
);
1697 /* Check for both : and :: rules. Check is_target so
1698 we don't lose on default suffix rules or makefiles. */
1699 if (f
!= 0 && f
->is_target
&& !f
->double_colon
)
1701 _("target file `%s' has both : and :: entries"), f
->name
);
1702 f
= enter_file (name
);
1703 /* If there was an existing entry and it was a double-colon
1704 entry, enter_file will have returned a new one, making it the
1705 prev pointer of the old one, and setting its double_colon
1706 pointer to the first one. */
1707 if (f
->double_colon
== 0)
1708 /* This is the first entry for this name, so we must
1709 set its double_colon pointer to itself. */
1710 f
->double_colon
= f
;
1716 /* Free name if not needed further. */
1717 if (f
!= 0 && name
!= f
->name
1718 && (name
< f
->name
|| name
> f
->name
+ strlen (f
->name
)))
1724 /* See if this is first target seen whose name does
1725 not start with a `.', unless it contains a slash. */
1726 if (default_goal_file
== 0 && set_default
1727 && (*name
!= '.' || index (name
, '/') != 0
1729 || index (name
, '\\') != 0
1735 /* If this file is a suffix, don't
1736 let it be the default goal file. */
1738 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
1740 register struct dep
*d2
;
1741 if (*dep_name (d
) != '.' && streq (name
, dep_name (d
)))
1746 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
1748 register unsigned int len
= strlen (dep_name (d2
));
1749 if (!strneq (name
, dep_name (d2
), len
))
1751 if (streq (name
+ len
, dep_name (d
)))
1762 default_goal_file
= f
;
1768 targets
[target_idx
] = 0;
1769 target_percents
[target_idx
] = 0;
1770 create_pattern_rule (targets
, target_percents
, two_colon
, deps
, cmds
, 1);
1771 free ((char *) target_percents
);
1775 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1776 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1777 Quoting backslashes are removed from STRING by compacting it into
1778 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1779 one, or nil if there are none. */
1782 find_char_unquote (string
, stopchars
, blank
)
1787 unsigned int string_len
= 0;
1788 register char *p
= string
;
1792 while (*p
!= '\0' && index (stopchars
, *p
) == 0
1793 && (!blank
|| !isblank (*p
)))
1798 if (p
> string
&& p
[-1] == '\\')
1800 /* Search for more backslashes. */
1801 register int i
= -2;
1802 while (&p
[i
] >= string
&& p
[i
] == '\\')
1805 /* Only compute the length if really needed. */
1806 if (string_len
== 0)
1807 string_len
= strlen (string
);
1808 /* The number of backslashes is now -I.
1809 Copy P over itself to swallow half of them. */
1810 bcopy (&p
[i
/ 2], &p
[i
], (string_len
- (p
- string
)) - (i
/ 2) + 1);
1813 /* All the backslashes quoted each other; the STOPCHAR was
1817 /* The STOPCHAR was quoted by a backslash. Look for another. */
1820 /* No backslash in sight. */
1824 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1828 /* Search PATTERN for an unquoted %. */
1831 find_percent (pattern
)
1834 return find_char_unquote (pattern
, "%", 0);
1837 /* Parse a string into a sequence of filenames represented as a
1838 chain of struct nameseq's in reverse order and return that chain.
1840 The string is passed as STRINGP, the address of a string pointer.
1841 The string pointer is updated to point at the first character
1842 not parsed, which either is a null char or equals STOPCHAR.
1844 SIZE is how big to construct chain elements.
1845 This is useful if we want them actually to be other structures
1846 that have room for additional info.
1848 If STRIP is nonzero, strip `./'s off the beginning. */
1851 parse_file_seq (stringp
, stopchar
, size
, strip
)
1857 register struct nameseq
*new = 0;
1858 register struct nameseq
*new1
, *lastnew1
;
1859 register char *p
= *stringp
;
1866 stopchars
[1] = stopchar
;
1867 stopchars
[2] = '\0';
1869 stopchars
[0] = stopchar
;
1870 stopchars
[1] = '\0';
1875 /* Skip whitespace; see if any more names are left. */
1882 /* Yes, find end of next name. */
1884 p
= find_char_unquote (q
, stopchars
, 1);
1886 /* convert comma separated list to space separated */
1891 if (stopchar
== ':' && p
&& *p
== ':' &&
1892 !(isspace(p
[1]) || !p
[1] || isspace(p
[-1])))
1894 p
= find_char_unquote (p
+1, stopchars
, 1);
1897 #if defined(WINDOWS32) || defined(__MSDOS__)
1898 /* For WINDOWS32, skip a "C:\..." or a "C:/..." until we find the
1899 first colon which isn't followed by a slash or a backslash.
1900 Note that tokens separated by spaces should be treated as separate
1901 tokens since make doesn't allow path names with spaces */
1902 if (stopchar
== ':')
1903 while (p
!= 0 && !isspace(*p
) &&
1904 (p
[1] == '\\' || p
[1] == '/') && isalpha (p
[-1]))
1905 p
= find_char_unquote (p
+ 1, stopchars
, 1);
1912 /* Skip leading `[]'s. */
1913 while (p
- q
> 2 && q
[0] == '[' && q
[1] == ']')
1915 /* Skip leading `./'s. */
1916 while (p
- q
> 2 && q
[0] == '.' && q
[1] == '/')
1919 q
+= 2; /* Skip "./". */
1920 while (q
< p
&& *q
== '/')
1921 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1925 /* Extract the filename just found, and skip it. */
1928 /* ".///" was stripped to "". */
1933 name
= savestring ("", 0);
1935 name
= savestring ("./", 2);
1940 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1941 * to remove this '\' before we can use the filename.
1942 * Savestring called because q may be read-only string constant.
1945 char *qbase
= xstrdup (q
);
1946 char *pbase
= qbase
+ (p
-q
);
1953 if (*q1
== '\\' && *(q1
+1) == ':')
1960 name
= savestring (qbase
, p1
- qbase
);
1964 name
= savestring (q
, p
- q
);
1967 /* Add it to the front of the chain. */
1968 new1
= (struct nameseq
*) xmalloc (size
);
1976 /* Look for multi-word archive references.
1977 They are indicated by a elt ending with an unmatched `)' and
1978 an elt further down the chain (i.e., previous in the file list)
1979 with an unmatched `(' (e.g., "lib(mem"). */
1984 if (new1
->name
[0] != '(' /* Don't catch "(%)" and suchlike. */
1985 && new1
->name
[strlen (new1
->name
) - 1] == ')'
1986 && index (new1
->name
, '(') == 0)
1988 /* NEW1 ends with a `)' but does not contain a `('.
1989 Look back for an elt with an opening `(' but no closing `)'. */
1991 struct nameseq
*n
= new1
->next
, *lastn
= new1
;
1993 while (n
!= 0 && (paren
= index (n
->name
, '(')) == 0)
1999 /* Ignore something starting with `(', as that cannot actually
2000 be an archive-member reference (and treating it as such
2001 results in an empty file name, which causes much lossage). */
2002 && n
->name
[0] != '(')
2004 /* N is the first element in the archive group.
2005 Its name looks like "lib(mem" (with no closing `)'). */
2009 /* Copy "lib(" into LIBNAME. */
2011 libname
= (char *) alloca (paren
- n
->name
+ 1);
2012 bcopy (n
->name
, libname
, paren
- n
->name
);
2013 libname
[paren
- n
->name
] = '\0';
2017 /* N was just "lib(", part of something like "lib( a b)".
2018 Edit it out of the chain and free its storage. */
2019 lastn
->next
= n
->next
;
2022 /* LASTN->next is the new stopping elt for the loop below. */
2027 /* Replace N's name with the full archive reference. */
2028 name
= concat (libname
, paren
, ")");
2033 if (new1
->name
[1] == '\0')
2035 /* NEW1 is just ")", part of something like "lib(a b )".
2036 Omit it from the chain and free its storage. */
2040 lastnew1
->next
= new1
->next
;
2044 free ((char *) lastn
);
2048 /* Replace also NEW1->name, which already has closing `)'. */
2049 name
= concat (libname
, new1
->name
, "");
2055 /* Trace back from NEW1 (the end of the list) until N
2056 (the beginning of the list), rewriting each name
2057 with the full archive reference. */
2061 name
= concat (libname
, new1
->name
, ")");
2070 /* No frobnication happening. Just step down the list. */
2087 /* Read a line of text from STREAM into LINEBUFFER.
2088 Combine continuation lines into one line.
2089 Return the number of actual lines read (> 1 if hacked continuation lines).
2092 static unsigned long
2093 readline (linebuffer
, stream
, flocp
)
2094 struct linebuffer
*linebuffer
;
2096 const struct floc
*flocp
;
2098 char *buffer
= linebuffer
->buffer
;
2099 register char *p
= linebuffer
->buffer
;
2100 register char *end
= p
+ linebuffer
->size
;
2101 register int len
, lastlen
= 0;
2103 register unsigned int nlines
= 0;
2104 register int backslash
;
2108 while (fgets (p
, end
- p
, stream
) != 0)
2113 /* This only happens when the first thing on the line is a '\0'.
2114 It is a pretty hopeless case, but (wonder of wonders) Athena
2115 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2116 There is nothing really to be done; we synthesize a newline so
2117 the following line doesn't appear to be part of this line. */
2118 error (flocp
, _("warning: NUL character seen; rest of line ignored"));
2126 /* Probably ran out of buffer space. */
2127 register unsigned int p_off
= p
- buffer
;
2128 linebuffer
->size
*= 2;
2129 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
2131 end
= buffer
+ linebuffer
->size
;
2132 linebuffer
->buffer
= buffer
;
2140 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2141 /* Check to see if the line was really ended with CRLF; if so ignore
2143 if (len
> 1 && p
[-2] == '\r')
2151 if (len
== 1 && p
> buffer
)
2152 /* P is pointing at a newline and it's the beginning of
2153 the buffer returned by the last fgets call. However,
2154 it is not necessarily the beginning of a line if P is
2155 pointing past the beginning of the holding buffer.
2156 If the buffer was just enlarged (right before the newline),
2157 we must account for that, so we pretend that the two lines
2162 for (p2
= p
- 2; --len
> 0; --p2
)
2165 backslash
= !backslash
;
2178 /* Enlarge the buffer. */
2179 register unsigned int p_off
= p
- buffer
;
2180 linebuffer
->size
*= 2;
2181 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
2183 end
= buffer
+ linebuffer
->size
;
2184 linebuffer
->buffer
= buffer
;
2188 if (ferror (stream
))
2189 pfatal_with_name (flocp
->filenm
);
2194 /* Parse the next "makefile word" from the input buffer, and return info
2197 A "makefile word" is one of:
2199 w_bogus Should never happen
2201 w_static A static word; cannot be expanded
2202 w_variable A word containing one or more variables/functions
2204 w_dcolon A double-colon
2205 w_semicolon A semicolon
2206 w_comment A comment character
2207 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2209 Note that this function is only used when reading certain parts of the
2210 makefile. Don't use it where special rules hold sway (RHS of a variable,
2211 in a command list, etc.) */
2213 static enum make_word_type
2214 get_next_mword (buffer
, delim
, startp
, length
)
2218 unsigned int *length
;
2220 enum make_word_type wtype
= w_bogus
;
2221 char *p
= buffer
, *beg
;
2224 /* Skip any leading whitespace. */
2241 wtype
= w_semicolon
;
2245 wtype
= w_varassign
;
2259 wtype
= w_varassign
;
2269 wtype
= w_varassign
;
2274 if (delim
&& index(delim
, c
))
2279 /* Did we find something? If so, return now. */
2280 if (wtype
!= w_bogus
)
2283 /* This is some non-operator word. A word consists of the longest
2284 string of characters that doesn't contain whitespace, one of [:=#],
2285 or [?+]=, or one of the chars in the DELIM string. */
2287 /* We start out assuming a static word; if we see a variable we'll
2288 adjust our assumptions then. */
2291 /* We already found the first value of "c", above. */
2307 #if defined(__MSDOS__) || defined(WINDOWS32)
2308 /* A word CAN include a colon in its drive spec. The drive
2309 spec is allowed either at the beginning of a word, or as part
2310 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2311 if (!(p
- beg
>= 2 &&
2312 (*p
== '/' || *p
== '\\') && isalpha (p
[-2]) &&
2313 (p
- beg
== 2 || p
[-3] == '(')))
2322 /* This is a variable reference, so note that it's expandable.
2323 Then read it to the matching close paren. */
2331 /* This is a single-letter variable reference. */
2334 for (count
=0; *p
!= '\0'; ++p
)
2338 else if (*p
== closeparen
&& --count
< 0)
2365 if (delim
&& index(delim
, c
))
2383 /* Construct the list of include directories
2384 from the arguments and the default list. */
2387 construct_include_path (arg_dirs
)
2390 register unsigned int i
;
2391 #ifdef VAXC /* just don't ask ... */
2396 /* Table to hold the dirs. */
2398 register unsigned int defsize
= (sizeof (default_include_directories
)
2399 / sizeof (default_include_directories
[0]));
2400 register unsigned int max
= 5;
2401 register char **dirs
= (char **) xmalloc ((5 + defsize
) * sizeof (char *));
2402 register unsigned int idx
= 0;
2408 /* First consider any dirs specified with -I switches.
2409 Ignore dirs that don't exist. */
2412 while (*arg_dirs
!= 0)
2414 char *dir
= *arg_dirs
++;
2418 char *expanded
= tilde_expand (dir
);
2423 if (stat (dir
, &stbuf
) == 0 && S_ISDIR (stbuf
.st_mode
))
2429 xrealloc ((char *) dirs
, (max
+ defsize
) * sizeof (char *));
2433 else if (dir
!= arg_dirs
[-1])
2437 /* Now add at the end the standard default dirs. */
2441 /* The environment variable $DJDIR holds the root of the
2442 DJGPP directory tree; add ${DJDIR}/include. */
2443 struct variable
*djdir
= lookup_variable ("DJDIR", 5);
2447 char *defdir
= (char *) xmalloc (strlen (djdir
->value
) + 8 + 1);
2449 strcat (strcpy (defdir
, djdir
->value
), "/include");
2450 dirs
[idx
++] = defdir
;
2455 for (i
= 0; default_include_directories
[i
] != 0; ++i
)
2456 if (stat (default_include_directories
[i
], &stbuf
) == 0
2457 && S_ISDIR (stbuf
.st_mode
))
2458 dirs
[idx
++] = default_include_directories
[i
];
2462 /* Now compute the maximum length of any name in it. */
2465 for (i
= 0; i
< idx
; ++i
)
2467 unsigned int len
= strlen (dirs
[i
]);
2468 /* If dir name is written with a trailing slash, discard it. */
2469 if (dirs
[i
][len
- 1] == '/')
2470 /* We can't just clobber a null in because it may have come from
2471 a literal string and literal strings may not be writable. */
2472 dirs
[i
] = savestring (dirs
[i
], len
- 1);
2473 if (len
> max_incl_len
)
2477 include_directories
= dirs
;
2480 /* Expand ~ or ~USER at the beginning of NAME.
2481 Return a newly malloc'd string or 0. */
2488 if (name
[1] == '/' || name
[1] == '\0')
2490 extern char *getenv ();
2495 /* Turn off --warn-undefined-variables while we expand HOME. */
2496 int save
= warn_undefined_variables_flag
;
2497 warn_undefined_variables_flag
= 0;
2499 home_dir
= allocated_variable_expand ("$(HOME)");
2501 warn_undefined_variables_flag
= save
;
2504 is_variable
= home_dir
[0] != '\0';
2508 home_dir
= getenv ("HOME");
2510 #if !defined(_AMIGA) && !defined(WINDOWS32)
2511 if (home_dir
== 0 || home_dir
[0] == '\0')
2513 extern char *getlogin ();
2514 char *logname
= getlogin ();
2518 struct passwd
*p
= getpwnam (logname
);
2520 home_dir
= p
->pw_dir
;
2523 #endif /* !AMIGA && !WINDOWS32 */
2526 char *new = concat (home_dir
, "", name
+ 1);
2532 #if !defined(_AMIGA) && !defined(WINDOWS32)
2535 struct passwd
*pwent
;
2536 char *userend
= index (name
+ 1, '/');
2539 pwent
= getpwnam (name
+ 1);
2543 return xstrdup (pwent
->pw_dir
);
2545 return concat (pwent
->pw_dir
, "/", userend
+ 1);
2547 else if (userend
!= 0)
2550 #endif /* !AMIGA && !WINDOWS32 */
2555 /* Given a chain of struct nameseq's describing a sequence of filenames,
2556 in reverse of the intended order, return a new chain describing the
2557 result of globbing the filenames. The new chain is in forward order.
2558 The links of the old chain are freed or used in the new chain.
2559 Likewise for the names in the old chain.
2561 SIZE is how big to construct chain elements.
2562 This is useful if we want them actually to be other structures
2563 that have room for additional info. */
2566 multi_glob (chain
, size
)
2567 struct nameseq
*chain
;
2570 extern void dir_setup_glob ();
2571 register struct nameseq
*new = 0;
2572 register struct nameseq
*old
;
2573 struct nameseq
*nexto
;
2576 dir_setup_glob (&gl
);
2578 for (old
= chain
; old
!= 0; old
= nexto
)
2586 if (old
->name
[0] == '~')
2588 char *newname
= tilde_expand (old
->name
);
2592 old
->name
= newname
;
2597 if (ar_name (old
->name
))
2599 /* OLD->name is an archive member reference.
2600 Replace it with the archive file name,
2601 and save the member name in MEMNAME.
2602 We will glob on the archive name and then
2603 reattach MEMNAME later. */
2605 ar_parse_name (old
->name
, &arname
, &memname
);
2611 #endif /* !NO_ARCHIVES */
2613 switch (glob (old
->name
, GLOB_NOCHECK
|GLOB_ALTDIRFUNC
, NULL
, &gl
))
2615 case 0: /* Success. */
2617 register int i
= gl
.gl_pathc
;
2623 /* Try to glob on MEMNAME within the archive. */
2624 struct nameseq
*found
2625 = ar_glob (gl
.gl_pathv
[i
], memname
, size
);
2628 /* No matches. Use MEMNAME as-is. */
2630 = (struct nameseq
*) xmalloc (size
);
2631 unsigned int alen
= strlen (gl
.gl_pathv
[i
]);
2632 unsigned int mlen
= strlen (memname
);
2633 elt
->name
= (char *) xmalloc (alen
+ 1 + mlen
+ 2);
2634 bcopy (gl
.gl_pathv
[i
], elt
->name
, alen
);
2635 elt
->name
[alen
] = '(';
2636 bcopy (memname
, &elt
->name
[alen
+ 1], mlen
);
2637 elt
->name
[alen
+ 1 + mlen
] = ')';
2638 elt
->name
[alen
+ 1 + mlen
+ 1] = '\0';
2644 /* Find the end of the FOUND chain. */
2645 struct nameseq
*f
= found
;
2646 while (f
->next
!= 0)
2649 /* Attach the chain being built to the end of the FOUND
2650 chain, and make FOUND the new NEW chain. */
2658 #endif /* !NO_ARCHIVES */
2660 struct nameseq
*elt
= (struct nameseq
*) xmalloc (size
);
2661 elt
->name
= xstrdup (gl
.gl_pathv
[i
]);
2673 fatal (NILF
, _("virtual memory exhausted"));