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, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* This is POSIX.2, but most systems using -DPOSIX probably don't have it. */
33 #include "glob/glob.h"
41 struct passwd
*getpwnam
PARAMS ((char *name
));
44 #endif /* !WINDOWS32 */
46 /* A `struct linebuffer' is a structure which holds a line of text.
47 `readline' reads a line from a stream into a linebuffer
48 and works regardless of the length of the line. */
52 /* Note: This is the number of bytes malloc'ed for `buffer'
53 It does not indicate `buffer's real length.
54 Instead, a null char indicates end-of-string. */
59 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
60 #define freebuffer(lb) free ((lb)->buffer)
63 /* Types of "words" that can be read in a makefile. */
66 w_bogus
, w_eol
, w_static
, w_variable
, w_colon
, w_dcolon
, w_semicolon
,
67 w_comment
, w_varassign
71 /* A `struct conditionals' contains the information describing
72 all the active conditionals in a makefile.
74 The global variable `conditionals' contains the conditionals
75 information for the current makefile. It is initialized from
76 the static structure `toplevel_conditionals' and is later changed
77 to new structures for included makefiles. */
81 unsigned int if_cmds
; /* Depth of conditional nesting. */
82 unsigned int allocated
; /* Elts allocated in following arrays. */
83 char *ignoring
; /* Are we ignoring or interepreting? */
84 char *seen_else
; /* Have we already seen an `else'? */
87 static struct conditionals toplevel_conditionals
;
88 static struct conditionals
*conditionals
= &toplevel_conditionals
;
91 /* Default directories to search for include files in */
93 static char *default_include_directories
[] =
95 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
97 * This completly up to the user when they install MSVC or other packages.
98 * This is defined as a placeholder.
100 #define INCLUDEDIR "."
105 "/usr/local/include",
111 /* List of directories to search for include files in */
113 static char **include_directories
;
115 /* Maximum length of an element of the above. */
117 static unsigned int max_incl_len
;
119 /* The filename and pointer to line number of the
120 makefile currently being read in. */
122 char *reading_filename
;
123 unsigned int *reading_lineno_ptr
;
125 /* The chain of makefiles read by read_makefile. */
127 static struct dep
*read_makefiles
= 0;
129 static int read_makefile
PARAMS ((char *filename
, int flags
));
130 static unsigned int readline
PARAMS ((struct linebuffer
*linebuffer
, FILE *stream
,
131 char *filename
, unsigned int lineno
));
132 static unsigned int do_define
PARAMS ((char *name
, unsigned int namelen
, enum variable_origin origin
,
133 unsigned int lineno
, FILE *infile
, char *filename
));
134 static int conditional_line
PARAMS ((char *line
, char *filename
, unsigned int lineno
));
135 static void record_files
PARAMS ((struct nameseq
*filenames
, char *pattern
, char *pattern_percent
,
136 struct dep
*deps
, unsigned int cmds_started
, char *commands
,
137 unsigned int commands_idx
, int two_colon
, char *filename
,
138 unsigned int lineno
, int set_default
));
139 static void record_target_var
PARAMS ((struct nameseq
*filenames
, char *defn
,
140 int two_colon
, enum variable_origin origin
,
141 char *filename
, unsigned int lineno
));
142 static enum make_word_type get_next_mword
PARAMS ((char *buffer
, char *delim
,
143 char **startp
, unsigned int *length
));
145 /* Read in all the makefiles and return the chain of their names. */
148 read_all_makefiles (makefiles
)
151 unsigned int num_makefiles
= 0;
154 puts ("Reading makefiles...");
156 /* If there's a non-null variable MAKEFILES, its value is a list of
157 files to read first thing. But don't let it prevent reading the
158 default makefiles and don't let the default goal come from there. */
166 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
167 int save
= warn_undefined_variables_flag
;
168 warn_undefined_variables_flag
= 0;
170 value
= allocated_variable_expand ("$(MAKEFILES)");
172 warn_undefined_variables_flag
= save
;
175 /* Set NAME to the start of next token and LENGTH to its length.
176 MAKEFILES is updated for finding remaining tokens. */
179 while ((name
= find_next_token (&p
, &length
)) != 0)
183 (void) read_makefile (name
,
184 RM_NO_DEFAULT_GOAL
| RM_INCLUDED
| RM_DONTCARE
);
190 /* Read makefiles specified with -f switches. */
193 while (*makefiles
!= 0)
195 struct dep
*tail
= read_makefiles
;
196 register struct dep
*d
;
198 if (! read_makefile (*makefiles
, 0))
199 perror_with_name ("", *makefiles
);
201 /* Find the right element of read_makefiles. */
203 while (d
->next
!= tail
)
206 /* Use the storage read_makefile allocates. */
207 *makefiles
= dep_name (d
);
212 /* If there were no -f switches, try the default names. */
214 if (num_makefiles
== 0)
216 static char *default_makefiles
[] =
218 /* all lower case since readdir() (the vms version) 'lowercasifies' */
219 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
222 { "GNUmakefile", "Makefile", "SMakefile", 0 };
223 #else /* !Amiga && !VMS */
224 { "GNUmakefile", "makefile", "Makefile", 0 };
227 register char **p
= default_makefiles
;
228 while (*p
!= 0 && !file_exists_p (*p
))
233 if (! read_makefile (*p
, 0))
234 perror_with_name ("", *p
);
238 /* No default makefile was found. Add the default makefiles to the
239 `read_makefiles' chain so they will be updated if possible. */
240 struct dep
*tail
= read_makefiles
;
241 /* Add them to the tail, after any MAKEFILES variable makefiles. */
242 while (tail
!= 0 && tail
->next
!= 0)
244 for (p
= default_makefiles
; *p
!= 0; ++p
)
246 struct dep
*d
= (struct dep
*) xmalloc (sizeof (struct dep
));
248 d
->file
= enter_file (*p
);
249 d
->file
->dontcare
= 1;
250 /* Tell update_goal_chain to bail out as soon as this file is
251 made, and main not to die if we can't make this file. */
252 d
->changed
= RM_DONTCARE
;
264 return read_makefiles
;
267 /* Read file FILENAME as a makefile and add its contents to the data base.
269 FLAGS contains bits as above.
271 FILENAME is added to the `read_makefiles' chain.
273 Returns 1 if a file was found and read, 0 if not. */
276 read_makefile (filename
, flags
)
280 static char *collapsed
= 0;
281 static unsigned int collapsed_length
= 0;
282 register FILE *infile
;
283 struct linebuffer lb
;
284 unsigned int commands_len
= 200;
285 char *commands
= (char *) xmalloc (200);
286 unsigned int commands_idx
= 0;
287 unsigned int cmds_started
;
290 int len
, reading_target
;
291 int ignoring
= 0, in_ignored_define
= 0;
292 int no_targets
= 0; /* Set when reading a rule without targets. */
293 char *passed_filename
= filename
;
295 struct nameseq
*filenames
= 0;
297 unsigned int lineno
= 1;
298 unsigned int nlines
= 0;
300 char *pattern
= 0, *pattern_percent
;
303 #if defined (WINDOWS32) || defined (__MSDOS__)
307 #define record_waiting_files() \
310 if (filenames != 0) \
311 record_files (filenames, pattern, pattern_percent, deps, \
312 cmds_started, commands, commands_idx, \
313 two_colon, filename, lineno, \
314 !(flags & RM_NO_DEFAULT_GOAL)); \
317 if (pattern) { free(pattern); pattern = 0; } \
321 cmds_started
= lineno
;
325 printf ("Reading makefile `%s'", filename
);
326 if (flags
& RM_NO_DEFAULT_GOAL
)
327 printf (" (no default goal)");
328 if (flags
& RM_INCLUDED
)
329 printf (" (search path)");
330 if (flags
& RM_DONTCARE
)
331 printf (" (don't care)");
332 if (flags
& RM_NO_TILDE
)
333 printf (" (no ~ expansion)");
337 /* First, get a stream to read. */
339 /* Expand ~ in FILENAME unless it came from `include',
340 in which case it was already done. */
341 if (!(flags
& RM_NO_TILDE
) && filename
[0] == '~')
343 char *expanded
= tilde_expand (filename
);
348 infile
= fopen (filename
, "r");
349 /* Save the error code so we print the right message later. */
350 makefile_errno
= errno
;
352 /* If the makefile wasn't found and it's either a makefile from
353 the `MAKEFILES' variable or an included makefile,
354 search the included makefile search path for this makefile. */
355 if (infile
== 0 && (flags
& RM_INCLUDED
) && *filename
!= '/')
357 register unsigned int i
;
358 for (i
= 0; include_directories
[i
] != 0; ++i
)
360 char *name
= concat (include_directories
[i
], "/", filename
);
361 infile
= fopen (name
, "r");
372 /* Add FILENAME to the chain of read makefiles. */
373 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
374 deps
->next
= read_makefiles
;
375 read_makefiles
= deps
;
377 deps
->file
= lookup_file (filename
);
380 deps
->file
= enter_file (savestring (filename
, strlen (filename
)));
381 if (flags
& RM_DONTCARE
)
382 deps
->file
->dontcare
= 1;
384 if (filename
!= passed_filename
)
386 filename
= deps
->file
->name
;
387 deps
->changed
= flags
;
390 /* If the makefile can't be found at all, give up entirely. */
394 /* If we did some searching, errno has the error from the last
395 attempt, rather from FILENAME itself. Restore it in case the
396 caller wants to use it in a message. */
397 errno
= makefile_errno
;
401 reading_filename
= filename
;
402 reading_lineno_ptr
= &lineno
;
404 /* Loop over lines in the file.
405 The strategy is to accumulate target names in FILENAMES, dependencies
406 in DEPS and commands in COMMANDS. These are used to define a rule
407 when the start of the next rule (or eof) is encountered. */
411 while (!feof (infile
))
414 nlines
= readline (&lb
, infile
, filename
, lineno
);
416 /* Check for a shell command line first.
417 If it is not one, we can stop treating tab specially. */
418 if (lb
.buffer
[0] == '\t')
420 /* This line is a probably shell command. */
424 /* Ignore the commands in a rule with no targets. */
427 /* If there is no preceding rule line, don't treat this line
428 as a command, even though it begins with a tab character.
429 SunOS 4 make appears to behave this way. */
434 /* Yep, this is a shell command, and we don't care. */
437 /* Append this command line to the line being accumulated. */
439 if (commands_idx
== 0)
440 cmds_started
= lineno
;
442 if (len
+ 1 + commands_idx
> commands_len
)
444 commands_len
= (len
+ 1 + commands_idx
) * 2;
445 commands
= (char *) xrealloc (commands
, commands_len
);
447 bcopy (p
, &commands
[commands_idx
], len
);
449 commands
[commands_idx
++] = '\n';
455 /* This line is not a shell command line. Don't worry about tabs. */
457 if (collapsed_length
< lb
.size
)
459 collapsed_length
= lb
.size
;
462 collapsed
= (char *) xmalloc (collapsed_length
);
464 strcpy (collapsed
, lb
.buffer
);
465 /* Collapse continuation lines. */
466 collapse_continuations (collapsed
);
467 remove_comments (collapsed
);
469 /* Compare a word, both length and contents. */
470 #define word1eq(s, l) (len == l && !strncmp (s, p, l))
475 /* This line is completely empty. */
478 /* Find the end of the first token. Note we don't need to worry about
479 * ":" here since we compare tokens by length (so "export" will never
480 * be equal to "export:").
482 for (p2
= p
+1; *p2
!= '\0' && !isspace(*p2
); ++p2
)
486 /* Find the start of the second token. If it's a `:' remember it,
487 since it can't be a preprocessor token--this allows targets named
488 `ifdef', `export', etc. */
490 while (isspace (*p2
))
494 else if (p2
[0] == ':' && p2
[1] == '\0')
497 goto skip_conditionals
;
500 /* We must first check for conditional and `define' directives before
501 ignoring anything, since they control what we will do with
504 if (!in_ignored_define
505 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
506 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
507 || word1eq ("else", 4) || word1eq ("endif", 5)))
509 int i
= conditional_line (p
, filename
, lineno
);
513 makefile_fatal (filename
, lineno
,
514 "invalid syntax in conditional");
518 if (word1eq ("endef", 5))
520 if (in_ignored_define
)
521 in_ignored_define
= 0;
523 makefile_fatal (filename
, lineno
, "extraneous `endef'");
527 if (word1eq ("define", 6))
530 in_ignored_define
= 1;
533 p2
= next_token (p
+ 6);
534 /* Let the variable name be the whole rest of the line,
535 with trailing blanks stripped (comments have already been
536 removed), so it could be a complex variable/function
537 reference that might contain blanks. */
538 p
= index (p2
, '\0');
539 while (isblank (p
[-1]))
541 lineno
= do_define (p2
, p
- p2
, o_file
,
542 lineno
, infile
, filename
);
547 if (word1eq ("override", 8))
549 p2
= next_token (p
+ 8);
551 makefile_error (filename
, lineno
, "empty `override' directive");
552 if (!strncmp (p2
, "define", 6) && (isblank (p2
[6]) || p2
[6] == '\0'))
555 in_ignored_define
= 1;
558 p2
= next_token (p2
+ 6);
559 /* Let the variable name be the whole rest of the line,
560 with trailing blanks stripped (comments have already been
561 removed), so it could be a complex variable/function
562 reference that might contain blanks. */
563 p
= index (p2
, '\0');
564 while (isblank (p
[-1]))
566 lineno
= do_define (p2
, p
- p2
, o_override
,
567 lineno
, infile
, filename
);
571 && !try_variable_definition (filename
, lineno
,
573 makefile_error (filename
, lineno
, "empty `override' directive");
580 /* Ignore the line. We continue here so conditionals
581 can appear in the middle of a rule. */
584 if (!reading_target
&& word1eq ("export", 6))
587 p2
= next_token (p
+ 6);
589 export_all_variables
= 1;
590 v
= try_variable_definition (filename
, lineno
, p2
, o_file
);
592 v
->export
= v_export
;
596 for (p
= find_next_token (&p2
, &len
); p
!= 0;
597 p
= find_next_token (&p2
, &len
))
599 v
= lookup_variable (p
, len
);
601 v
= define_variable (p
, len
, "", o_file
, 0);
602 v
->export
= v_export
;
606 else if (!reading_target
&& word1eq ("unexport", 8))
610 p2
= next_token (p
+ 8);
612 export_all_variables
= 0;
613 for (p
= find_next_token (&p2
, &len
); p
!= 0;
614 p
= find_next_token (&p2
, &len
))
616 v
= lookup_variable (p
, len
);
618 v
= define_variable (p
, len
, "", o_file
, 0);
619 v
->export
= v_noexport
;
622 else if (word1eq ("vpath", 5))
626 p2
= variable_expand (p
+ 5);
627 p
= find_next_token (&p2
, &len
);
630 pattern
= savestring (p
, len
);
631 p
= find_next_token (&p2
, &len
);
632 /* No searchpath means remove all previous
633 selective VPATH's with the same pattern. */
636 /* No pattern means remove all previous selective VPATH's. */
638 construct_vpath_list (pattern
, p
);
642 else if (word1eq ("include", 7) || word1eq ("-include", 8)
643 || word1eq ("sinclude", 8))
645 /* We have found an `include' line specifying a nested
646 makefile to be read at this point. */
647 struct conditionals
*save
, new_conditionals
;
648 struct nameseq
*files
;
649 /* "-include" (vs "include") says no error if the file does not
650 exist. "sinclude" is an alias for this from SGI. */
651 int noerror
= p
[0] != 'i';
653 p
= allocated_variable_expand (next_token (p
+ (noerror
? 8 : 7)));
656 makefile_error (filename
, lineno
,
657 "no file name for `%sinclude'",
662 /* Parse the list of file names. */
664 files
= multi_glob (parse_file_seq (&p2
, '\0',
665 sizeof (struct nameseq
),
667 sizeof (struct nameseq
));
670 /* Save the state of conditionals and start
671 the included makefile with a clean slate. */
673 bzero ((char *) &new_conditionals
, sizeof new_conditionals
);
674 conditionals
= &new_conditionals
;
676 /* Record the rules that are waiting so they will determine
677 the default goal before those in the included makefile. */
678 record_waiting_files ();
680 /* Read each included makefile. */
683 struct nameseq
*next
= files
->next
;
684 char *name
= files
->name
;
685 free ((char *)files
);
688 if (! read_makefile (name
, (RM_INCLUDED
| RM_NO_TILDE
689 | (noerror
? RM_DONTCARE
: 0)))
691 makefile_error (filename
, lineno
,
692 "%s: %s", name
, strerror (errno
));
696 /* Free any space allocated by conditional_line. */
697 if (conditionals
->ignoring
)
698 free (conditionals
->ignoring
);
699 if (conditionals
->seen_else
)
700 free (conditionals
->seen_else
);
704 reading_filename
= filename
;
705 reading_lineno_ptr
= &lineno
;
708 else if (try_variable_definition (filename
, lineno
, p
, o_file
))
709 /* This line has been dealt with. */
711 else if (lb
.buffer
[0] == '\t')
713 p
= collapsed
; /* Ignore comments. */
717 /* The line is completely blank; that is harmless. */
719 /* This line starts with a tab but was not caught above
720 because there was no preceding target, and the line
721 might have been usable as a variable definition.
722 But now it is definitely lossage. */
723 makefile_fatal (filename
, lineno
,
724 "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;
743 /* Record the previous rule. */
745 record_waiting_files ();
747 /* Search the line for an unquoted ; that is not after an
749 cmdleft
= find_char_unquote (lb
.buffer
, ";#", 0);
750 if (cmdleft
!= 0 && *cmdleft
== '#')
752 /* We found a comment before a semicolon. */
756 else if (cmdleft
!= 0)
757 /* Found one. Cut the line short there before expanding it. */
760 collapse_continuations (lb
.buffer
);
762 /* We can't expand the entire line, since if it's a per-target
763 variable we don't want to expand it. So, walk from the
764 beginning, expanding as we go, and looking for "interesting"
765 chars. The first word is always expandable. */
766 wtype
= get_next_mword(lb
.buffer
, NULL
, &lb_next
, &len
);
771 makefile_fatal (filename
, lineno
,
772 "missing rule before commands");
774 /* This line contained a variable reference that
775 expanded to nothing but whitespace. */
780 /* We accept and ignore rules without targets for
781 compatibility with SunOS 4 make. */
789 p2
= variable_expand_string(NULL
, lb_next
, len
);
797 /* Look for a semicolon in the expanded line. */
798 cmdleft
= find_char_unquote (p2
, ";", 0);
802 unsigned long p2_off
= p2
- variable_buffer
;
803 unsigned long cmd_off
= cmdleft
- variable_buffer
;
804 char *pend
= p2
+ strlen(p2
);
806 /* Append any remnants of lb, then cut the line short
810 /* One school of thought says that you shouldn't expand
811 here, but merely copy, since now you're beyond a ";"
812 and into a command script. However, the old parser
813 expanded the whole line, so we continue that for
814 backwards-compatiblity. Also, it wouldn't be
815 entirely consistent, since we do an unconditional
816 expand below once we know we don't have a
817 target-specific variable. */
818 (void)variable_expand_string(pend
, lb_next
, -1);
819 lb_next
+= strlen(lb_next
);
820 p2
= variable_buffer
+ p2_off
;
821 cmdleft
= variable_buffer
+ cmd_off
+ 1;
825 colonp
= find_char_unquote(p2
, ":", 0);
826 #if defined(__MSDOS__) || defined(WINDOWS32)
827 /* The drive spec brain-damage strikes again... */
828 /* FIXME: is whitespace the only possible separator of words
829 in this context? If not, the `isspace' test below will
830 need to be changed into a call to `index'. */
831 while (colonp
&& (colonp
[1] == '/' || colonp
[1] == '\\') &&
832 colonp
> p2
&& isalpha(colonp
[-1]) &&
833 (colonp
== p2
+ 1 || isspace(colonp
[-2])))
834 colonp
= find_char_unquote(colonp
+ 1, ":", 0);
839 wtype
= get_next_mword(lb_next
, NULL
, &lb_next
, &len
);
841 makefile_fatal (filename
, lineno
, "missing separator");
845 p2
= variable_expand_string(p2
, lb_next
, len
);
846 /* We don't need to worry about cmdleft here, because if it was
847 found in the variable_buffer the entire buffer has already
848 been expanded... we'll never get here. */
851 p2
= next_token (variable_buffer
);
853 filenames
= multi_glob (parse_file_seq (&p2
, ':',
854 sizeof (struct nameseq
),
856 sizeof (struct nameseq
));
860 /* We accept and ignore rules without targets for
861 compatibility with SunOS 4 make. */
865 /* This should never be possible; we handled it above. */
869 /* Is this a one-colon or two-colon entry? */
870 two_colon
= *p2
== ':';
874 /* Test to see if it's a target-specific variable. Copy the rest
875 of the buffer over, possibly temporarily (we'll expand it later
876 if it's not a target-specific variable). PLEN saves the length
877 of the unparsed section of p2, for later. */
878 if (*lb_next
!= '\0')
880 unsigned int l
= p2
- variable_buffer
;
882 (void)variable_buffer_output(p2
+plen
,
883 lb_next
, strlen(lb_next
)+1);
884 p2
= variable_buffer
+ l
;
886 wtype
= get_next_mword(p2
, NULL
, &p
, &len
);
888 if (wtype
== w_static
&& (len
== (sizeof("override")-1)
889 && !strncmp(p
, "override", len
)))
891 v_origin
= o_override
;
892 (void)get_next_mword(p
+len
, NULL
, &p
, &len
);
894 else if (wtype
!= w_eol
)
895 wtype
= get_next_mword(p
+len
, NULL
, NULL
, NULL
);
897 if (wtype
== w_varassign
|| v_origin
== o_override
)
899 record_target_var(filenames
, p
, two_colon
, v_origin
,
905 /* This is a normal target, _not_ a target-specific variable.
906 Unquote any = in the dependency list. */
907 find_char_unquote (lb_next
, "=", 0);
909 /* We have some targets, so don't ignore the following commands. */
912 /* Expand the dependencies, etc. */
913 if (*lb_next
!= '\0')
915 unsigned int l
= p2
- variable_buffer
;
916 (void)variable_expand_string(p2
+ plen
, lb_next
, -1);
917 p2
= variable_buffer
+ l
;
919 /* Look for a semicolon in the expanded line. */
922 cmdleft
= find_char_unquote (p2
, ";", 0);
928 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
930 while (p
!= 0 && p
[-1] == '\\')
932 register char *q
= &p
[-1];
933 register int backslash
= 0;
935 backslash
= !backslash
;
937 p
= index (p
+ 1, ':');
942 /* Here, the situation is quite complicated. Let's have a look
943 at a couple of targets:
951 The rule is that it's only a target, if there are TWO :'s
952 OR a space around the :.
954 if (p
&& !(isspace(p
[1]) || !p
[1] || isspace(p
[-1])))
957 #if defined (WINDOWS32) || defined (__MSDOS__)
960 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
961 if (p
!= 0 && (p
[1] == '\\' || p
[1] == '/') &&
962 isalpha(p
[-1]) && (p
== p2
+ 1 || index(" \t:", p
[-2]) != 0)) {
963 p
= index(p
+ 1, ':');
966 } while (check_again
);
970 struct nameseq
*target
;
971 target
= parse_file_seq (&p2
, ':', sizeof (struct nameseq
), 1);
974 makefile_fatal (filename
, lineno
, "missing target pattern");
975 else if (target
->next
!= 0)
976 makefile_fatal (filename
, lineno
, "multiple target patterns");
977 pattern
= target
->name
;
978 pattern_percent
= find_percent (pattern
);
979 if (pattern_percent
== 0)
980 makefile_fatal (filename
, lineno
,
981 "target pattern contains no `%%'");
982 free((char *)target
);
987 /* Parse the dependencies. */
988 deps
= (struct dep
*)
989 multi_glob (parse_file_seq (&p2
, '\0', sizeof (struct dep
), 1),
990 sizeof (struct dep
));
995 /* Semicolon means rest of line is a command. */
996 unsigned int len
= strlen (cmdleft
);
998 cmds_started
= lineno
;
1000 /* Add this command line to the buffer. */
1001 if (len
+ 2 > commands_len
)
1003 commands_len
= (len
+ 2) * 2;
1004 commands
= (char *) xrealloc (commands
, commands_len
);
1006 bcopy (cmdleft
, commands
, len
);
1007 commands_idx
+= len
;
1008 commands
[commands_idx
++] = '\n';
1014 /* We get here except in the case that we just read a rule line.
1015 Record now the last rule we read, so following spurious
1016 commands are properly diagnosed. */
1017 record_waiting_files ();
1021 if (conditionals
->if_cmds
)
1022 makefile_fatal (filename
, lineno
, "missing `endif'");
1024 /* At eof, record the last rule. */
1025 record_waiting_files ();
1028 free ((char *) commands
);
1031 reading_filename
= 0;
1032 reading_lineno_ptr
= 0;
1037 /* Execute a `define' directive.
1038 The first line has already been read, and NAME is the name of
1039 the variable to be defined. The following lines remain to be read.
1040 LINENO, INFILE and FILENAME refer to the makefile being read.
1041 The value returned is LINENO, updated for lines read here. */
1044 do_define (name
, namelen
, origin
, lineno
, infile
, filename
)
1046 unsigned int namelen
;
1047 enum variable_origin origin
;
1048 unsigned int lineno
;
1052 struct linebuffer lb
;
1053 unsigned int nlines
= 0;
1054 unsigned int length
= 100;
1055 char *definition
= (char *) xmalloc (100);
1056 register unsigned int idx
= 0;
1059 /* Expand the variable name. */
1060 char *var
= (char *) alloca (namelen
+ 1);
1061 bcopy (name
, var
, namelen
);
1062 var
[namelen
] = '\0';
1063 var
= variable_expand (var
);
1066 while (!feof (infile
))
1071 nlines
= readline (&lb
, infile
, filename
, lineno
);
1073 collapse_continuations (lb
.buffer
);
1075 p
= next_token (lb
.buffer
);
1077 if ((len
== 5 || (len
> 5 && isblank (p
[5])))
1078 && !strncmp (p
, "endef", 5))
1081 remove_comments (p
);
1082 if (*next_token (p
) != '\0')
1083 makefile_error (filename
, lineno
,
1084 "Extraneous text after `endef' directive");
1085 /* Define the variable. */
1087 definition
[0] = '\0';
1089 definition
[idx
- 1] = '\0';
1090 (void) define_variable (var
, strlen (var
), definition
, origin
, 1);
1093 return (lineno
+ nlines
);
1097 len
= strlen (lb
.buffer
);
1098 /* Increase the buffer size if necessary. */
1099 if (idx
+ len
+ 1 > length
)
1101 length
= (idx
+ len
) * 2;
1102 definition
= (char *) xrealloc (definition
, length
+ 1);
1105 bcopy (lb
.buffer
, &definition
[idx
], len
);
1107 /* Separate lines with a newline. */
1108 definition
[idx
++] = '\n';
1113 makefile_fatal (filename
, lineno
, "missing `endef', unterminated `define'");
1119 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1120 "ifneq", "else" and "endif".
1121 LINE is the input line, with the command as its first word.
1123 FILENAME and LINENO are the filename and line number in the
1124 current makefile. They are used for error messages.
1126 Value is -1 if the line is invalid,
1127 0 if following text should be interpreted,
1128 1 if following text should be ignored. */
1131 conditional_line (line
, filename
, lineno
)
1134 unsigned int lineno
;
1138 register unsigned int i
;
1142 /* It's an "if..." command. */
1143 notdef
= line
[2] == 'n';
1146 cmdname
= line
[3] == 'd' ? "ifndef" : "ifneq";
1147 line
+= cmdname
[3] == 'd' ? 7 : 6;
1151 cmdname
= line
[2] == 'd' ? "ifdef" : "ifeq";
1152 line
+= cmdname
[2] == 'd' ? 6 : 5;
1157 /* It's an "else" or "endif" command. */
1158 notdef
= line
[1] == 'n';
1159 cmdname
= notdef
? "endif" : "else";
1160 line
+= notdef
? 5 : 4;
1163 line
= next_token (line
);
1165 if (*cmdname
== 'e')
1168 makefile_error (filename
, lineno
,
1169 "Extraneous text after `%s' directive",
1171 /* "Else" or "endif". */
1172 if (conditionals
->if_cmds
== 0)
1173 makefile_fatal (filename
, lineno
, "extraneous `%s'", cmdname
);
1174 /* NOTDEF indicates an `endif' command. */
1176 --conditionals
->if_cmds
;
1177 else if (conditionals
->seen_else
[conditionals
->if_cmds
- 1])
1178 makefile_fatal (filename
, lineno
, "only one `else' per conditional");
1181 /* Toggle the state of ignorance. */
1182 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1183 = !conditionals
->ignoring
[conditionals
->if_cmds
- 1];
1184 /* Record that we have seen an `else' in this conditional.
1185 A second `else' will be erroneous. */
1186 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 1;
1188 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1189 if (conditionals
->ignoring
[i
])
1194 if (conditionals
->allocated
== 0)
1196 conditionals
->allocated
= 5;
1197 conditionals
->ignoring
= (char *) xmalloc (conditionals
->allocated
);
1198 conditionals
->seen_else
= (char *) xmalloc (conditionals
->allocated
);
1201 ++conditionals
->if_cmds
;
1202 if (conditionals
->if_cmds
> conditionals
->allocated
)
1204 conditionals
->allocated
+= 5;
1205 conditionals
->ignoring
= (char *)
1206 xrealloc (conditionals
->ignoring
, conditionals
->allocated
);
1207 conditionals
->seen_else
= (char *)
1208 xrealloc (conditionals
->seen_else
, conditionals
->allocated
);
1211 /* Record that we have seen an `if...' but no `else' so far. */
1212 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 0;
1214 /* Search through the stack to see if we're already ignoring. */
1215 for (i
= 0; i
< conditionals
->if_cmds
- 1; ++i
)
1216 if (conditionals
->ignoring
[i
])
1218 /* We are already ignoring, so just push a level
1219 to match the next "else" or "endif", and keep ignoring.
1220 We don't want to expand variables in the condition. */
1221 conditionals
->ignoring
[conditionals
->if_cmds
- 1] = 1;
1225 if (cmdname
[notdef
? 3 : 2] == 'd')
1227 /* "Ifdef" or "ifndef". */
1229 register char *p
= end_of_token (line
);
1234 v
= lookup_variable (line
, i
);
1235 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1236 = (v
!= 0 && *v
->value
!= '\0') == notdef
;
1240 /* "Ifeq" or "ifneq". */
1243 char termin
= *line
== '(' ? ',' : *line
;
1245 if (termin
!= ',' && termin
!= '"' && termin
!= '\'')
1249 /* Find the end of the first string. */
1252 register int count
= 0;
1253 for (; *line
!= '\0'; ++line
)
1256 else if (*line
== ')')
1258 else if (*line
== ',' && count
<= 0)
1262 while (*line
!= '\0' && *line
!= termin
)
1270 /* Strip blanks after the first string. */
1272 while (isblank (p
[-1]))
1279 s2
= variable_expand (s1
);
1280 /* We must allocate a new copy of the expanded string because
1281 variable_expand re-uses the same buffer. */
1283 s1
= (char *) alloca (len
+ 1);
1284 bcopy (s2
, s1
, len
+ 1);
1287 /* Find the start of the second string. */
1288 line
= next_token (line
);
1290 termin
= termin
== ',' ? ')' : *line
;
1291 if (termin
!= ')' && termin
!= '"' && termin
!= '\'')
1294 /* Find the end of the second string. */
1297 register int count
= 0;
1298 s2
= next_token (line
);
1299 for (line
= s2
; *line
!= '\0'; ++line
)
1303 else if (*line
== ')')
1314 while (*line
!= '\0' && *line
!= termin
)
1322 line
= next_token (++line
);
1324 makefile_error (filename
, lineno
,
1325 "Extraneous text after `%s' directive",
1328 s2
= variable_expand (s2
);
1329 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1330 = streq (s1
, s2
) == notdef
;
1333 /* Search through the stack to see if we're ignoring. */
1334 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1335 if (conditionals
->ignoring
[i
])
1340 /* Remove duplicate dependencies in CHAIN. */
1343 uniquize_deps (chain
)
1346 register struct dep
*d
;
1348 /* Make sure that no dependencies are repeated. This does not
1349 really matter for the purpose of updating targets, but it
1350 might make some names be listed twice for $^ and $?. */
1352 for (d
= chain
; d
!= 0; d
= d
->next
)
1354 struct dep
*last
, *next
;
1359 if (streq (dep_name (d
), dep_name (next
)))
1361 struct dep
*n
= next
->next
;
1363 if (next
->name
!= 0 && next
->name
!= d
->name
)
1366 free ((char *) next
);
1377 /* Record target-specific variable values for files FILENAMES.
1378 TWO_COLON is nonzero if a double colon was used.
1380 The links of FILENAMES are freed, and so are any names in it
1381 that are not incorporated into other data structures.
1383 If the target is a pattern, add the variable to the pattern-specific
1384 variable value list. */
1387 record_target_var (filenames
, defn
, two_colon
, origin
, filename
, lineno
)
1388 struct nameseq
*filenames
;
1391 enum variable_origin origin
;
1393 unsigned int lineno
;
1395 struct nameseq
*nextf
;
1396 struct variable_set_list
*global
;
1398 global
= current_variable_set_list
;
1400 for (; filenames
!= 0; filenames
= nextf
)
1403 register char *name
= filenames
->name
;
1404 struct variable_set_list
*vlist
;
1408 nextf
= filenames
->next
;
1409 free ((char *) filenames
);
1411 /* If it's a pattern target, then add it to the pattern-specific
1413 percent
= find_percent (name
);
1416 struct pattern_var
*p
;
1418 /* Get a reference for this pattern-specific variable struct. */
1419 p
= create_pattern_var(name
, percent
);
1427 /* Get a file reference for this file, and initialize it. */
1428 f
= enter_file (name
);
1429 initialize_file_variables (f
);
1430 vlist
= f
->variables
;
1434 /* Make the new variable context current and define the variable. */
1435 current_variable_set_list
= vlist
;
1436 v
= try_variable_definition(filename
, lineno
, defn
, origin
);
1438 makefile_error(filename
, lineno
,
1439 "Malformed per-target variable definition");
1442 /* If it's not an override, check to see if there was a command-line
1443 setting. If so, reset the value. */
1444 if (origin
!= o_override
)
1446 struct variable
*gv
;
1447 int len
= strlen(v
->name
);
1449 current_variable_set_list
= global
;
1450 gv
= lookup_variable(v
->name
, len
);
1451 if (gv
&& (gv
->origin
== o_env_override
|| gv
->origin
== o_command
))
1452 define_variable_in_set(v
->name
, len
, gv
->value
, gv
->origin
,
1453 gv
->recursive
, vlist
->set
);
1456 /* Free name if not needed further. */
1457 if (name
!= fname
&& (name
< fname
|| name
> fname
+ strlen (fname
)))
1461 current_variable_set_list
= global
;
1464 /* Record a description line for files FILENAMES,
1465 with dependencies DEPS, commands to execute described
1466 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1467 TWO_COLON is nonzero if a double colon was used.
1468 If not nil, PATTERN is the `%' pattern to make this
1469 a static pattern rule, and PATTERN_PERCENT is a pointer
1470 to the `%' within it.
1472 The links of FILENAMES are freed, and so are any names in it
1473 that are not incorporated into other data structures. */
1476 record_files (filenames
, pattern
, pattern_percent
, deps
, cmds_started
,
1477 commands
, commands_idx
, two_colon
, filename
, lineno
, set_default
)
1478 struct nameseq
*filenames
;
1479 char *pattern
, *pattern_percent
;
1481 unsigned int cmds_started
;
1483 unsigned int commands_idx
;
1486 unsigned int lineno
;
1489 struct nameseq
*nextf
;
1491 unsigned int max_targets
= 0, target_idx
= 0;
1492 char **targets
= 0, **target_percents
= 0;
1493 struct commands
*cmds
;
1495 if (commands_idx
> 0)
1497 cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
1498 cmds
->filename
= filename
;
1499 cmds
->lineno
= cmds_started
;
1500 cmds
->commands
= savestring (commands
, commands_idx
);
1501 cmds
->command_lines
= 0;
1506 for (; filenames
!= 0; filenames
= nextf
)
1509 register char *name
= filenames
->name
;
1510 register struct file
*f
;
1511 register struct dep
*d
;
1513 char *implicit_percent
;
1515 nextf
= filenames
->next
;
1516 free ((char *) filenames
);
1518 implicit_percent
= find_percent (name
);
1519 implicit
|= implicit_percent
!= 0;
1521 if (implicit
&& pattern
!= 0)
1522 makefile_fatal (filename
, lineno
,
1523 "mixed implicit and static pattern rules");
1525 if (implicit
&& implicit_percent
== 0)
1526 makefile_fatal (filename
, lineno
, "mixed implicit and normal rules");
1533 targets
= (char **) xmalloc (5 * sizeof (char *));
1534 target_percents
= (char **) xmalloc (5 * sizeof (char *));
1537 else if (target_idx
== max_targets
- 1)
1540 targets
= (char **) xrealloc ((char *) targets
,
1541 max_targets
* sizeof (char *));
1543 = (char **) xrealloc ((char *) target_percents
,
1544 max_targets
* sizeof (char *));
1546 targets
[target_idx
] = name
;
1547 target_percents
[target_idx
] = implicit_percent
;
1552 /* If there are multiple filenames, copy the chain DEPS
1553 for all but the last one. It is not safe for the same deps
1554 to go in more than one place in the data base. */
1555 this = nextf
!= 0 ? copy_dep_chain (deps
) : deps
;
1558 /* If this is an extended static rule:
1559 `targets: target%pattern: dep%pattern; cmds',
1560 translate each dependency pattern into a plain filename
1561 using the target pattern and this target's name. */
1562 if (!pattern_matches (pattern
, pattern_percent
, name
))
1564 /* Give a warning if the rule is meaningless. */
1565 makefile_error (filename
, lineno
,
1566 "target `%s' doesn't match the target pattern",
1572 /* We use patsubst_expand to do the work of translating
1573 the target pattern, the target's name and the dependencies'
1574 patterns into plain dependency names. */
1575 char *buffer
= variable_expand ("");
1577 for (d
= this; d
!= 0; d
= d
->next
)
1580 char *percent
= find_percent (d
->name
);
1583 o
= patsubst_expand (buffer
, name
, pattern
, d
->name
,
1584 pattern_percent
, percent
);
1586 d
->name
= savestring (buffer
, o
- buffer
);
1592 /* Single-colon. Combine these dependencies
1593 with others in file's existing record, if any. */
1594 f
= enter_file (name
);
1596 if (f
->double_colon
)
1597 makefile_fatal (filename
, lineno
,
1598 "target file `%s' has both : and :: entries",
1601 /* If CMDS == F->CMDS, this target was listed in this rule
1602 more than once. Just give a warning since this is harmless. */
1603 if (cmds
!= 0 && cmds
== f
->cmds
)
1606 "target `%s' given more than once in the same rule.",
1609 /* Check for two single-colon entries both with commands.
1610 Check is_target so that we don't lose on files such as .c.o
1611 whose commands were preinitialized. */
1612 else if (cmds
!= 0 && f
->cmds
!= 0 && f
->is_target
)
1614 makefile_error (cmds
->filename
, cmds
->lineno
,
1615 "warning: overriding commands for target `%s'",
1617 makefile_error (f
->cmds
->filename
, f
->cmds
->lineno
,
1618 "warning: ignoring old commands for target `%s'",
1624 /* Defining .DEFAULT with no deps or cmds clears it. */
1625 if (f
== default_file
&& this == 0 && cmds
== 0)
1629 /* Defining .SUFFIXES with no dependencies
1630 clears out the list of suffixes. */
1631 if (f
== suffix_file
&& this == 0)
1636 struct dep
*nextd
= d
->next
;
1643 else if (f
->deps
!= 0)
1645 /* Add the file's old deps and the new ones in THIS together. */
1647 struct dep
*firstdeps
, *moredeps
;
1650 /* This is the rule with commands, so put its deps first.
1651 The rationale behind this is that $< expands to the
1652 first dep in the chain, and commands use $< expecting
1653 to get the dep that rule specifies. */
1659 /* Append the new deps to the old ones. */
1660 firstdeps
= f
->deps
;
1665 firstdeps
= moredeps
;
1669 while (d
->next
!= 0)
1674 f
->deps
= firstdeps
;
1679 /* If this is a static pattern rule, set the file's stem to
1680 the part of its name that matched the `%' in the pattern,
1681 so you can use $* in the commands. */
1684 static char *percent
= "%";
1685 char *buffer
= variable_expand ("");
1686 char *o
= patsubst_expand (buffer
, name
, pattern
, percent
,
1687 pattern_percent
, percent
);
1688 f
->stem
= savestring (buffer
, o
- buffer
);
1693 /* Double-colon. Make a new record
1694 even if the file already has one. */
1695 f
= lookup_file (name
);
1696 /* Check for both : and :: rules. Check is_target so
1697 we don't lose on default suffix rules or makefiles. */
1698 if (f
!= 0 && f
->is_target
&& !f
->double_colon
)
1699 makefile_fatal (filename
, lineno
,
1700 "target file `%s' has both : and :: entries",
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 (strncmp (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
= savestring(q
, strlen(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).
2093 readline (linebuffer
, stream
, filename
, lineno
)
2094 struct linebuffer
*linebuffer
;
2097 unsigned int lineno
;
2099 char *buffer
= linebuffer
->buffer
;
2100 register char *p
= linebuffer
->buffer
;
2101 register char *end
= p
+ linebuffer
->size
;
2102 register int len
, lastlen
= 0;
2104 register unsigned int nlines
= 0;
2105 register int backslash
;
2109 while (fgets (p
, end
- p
, stream
) != 0)
2114 /* This only happens when the first thing on the line is a '\0'.
2115 It is a pretty hopeless case, but (wonder of wonders) Athena
2116 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2117 There is nothing really to be done; we synthesize a newline so
2118 the following line doesn't appear to be part of this line. */
2119 makefile_error (filename
, lineno
,
2120 "warning: NUL character seen; rest of line ignored");
2128 /* Probably ran out of buffer space. */
2129 register unsigned int p_off
= p
- buffer
;
2130 linebuffer
->size
*= 2;
2131 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
2133 end
= buffer
+ linebuffer
->size
;
2134 linebuffer
->buffer
= buffer
;
2142 if (len
== 1 && p
> buffer
)
2143 /* P is pointing at a newline and it's the beginning of
2144 the buffer returned by the last fgets call. However,
2145 it is not necessarily the beginning of a line if P is
2146 pointing past the beginning of the holding buffer.
2147 If the buffer was just enlarged (right before the newline),
2148 we must account for that, so we pretend that the two lines
2153 for (p2
= p
- 2; --len
> 0; --p2
)
2156 backslash
= !backslash
;
2169 /* Enlarge the buffer. */
2170 register unsigned int p_off
= p
- buffer
;
2171 linebuffer
->size
*= 2;
2172 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
2174 end
= buffer
+ linebuffer
->size
;
2175 linebuffer
->buffer
= buffer
;
2179 if (ferror (stream
))
2180 pfatal_with_name (filename
);
2185 /* Parse the next "makefile word" from the input buffer, and return info
2188 A "makefile word" is one of:
2190 w_bogus Should never happen
2192 w_static A static word; cannot be expanded
2193 w_variable A word containing one or more variables/functions
2195 w_dcolon A double-colon
2196 w_semicolon A semicolon
2197 w_comment A comment character
2198 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2200 Note that this function is only used when reading certain parts of the
2201 makefile. Don't use it where special rules hold sway (RHS of a variable,
2202 in a command list, etc.) */
2204 static enum make_word_type
2205 get_next_mword (buffer
, delim
, startp
, length
)
2209 unsigned int *length
;
2211 enum make_word_type wtype
= w_bogus
;
2212 char *p
= buffer
, *beg
;
2215 /* Skip any leading whitespace. */
2232 wtype
= w_semicolon
;
2236 wtype
= w_varassign
;
2250 wtype
= w_varassign
;
2260 wtype
= w_varassign
;
2265 if (delim
&& index(delim
, c
))
2270 /* Did we find something? If so, return now. */
2271 if (wtype
!= w_bogus
)
2274 /* This is some non-operator word. A word consists of the longest
2275 string of characters that doesn't contain whitespace, one of [:=#],
2276 or [?+]=, or one of the chars in the DELIM string. */
2278 /* We start out assuming a static word; if we see a variable we'll
2279 adjust our assumptions then. */
2282 /* We already found the first value of "c", above. */
2298 #if defined(__MSDOS__) || defined(WINDOWS32)
2299 /* A word CAN include a colon in its drive spec. */
2300 if (!(p
- beg
== 2 && (*p
== '/' || *p
== '\\') && isalpha (*beg
)))
2309 /* This is a variable reference, so note that it's expandable.
2310 Then read it to the matching close paren. */
2318 /* This is a single-letter variable reference. */
2321 for (count
=0; *p
!= '\0'; ++p
)
2325 else if (*p
== closeparen
&& --count
< 0)
2351 if (delim
&& index(delim
, c
))
2369 /* Construct the list of include directories
2370 from the arguments and the default list. */
2373 construct_include_path (arg_dirs
)
2376 register unsigned int i
;
2377 #ifdef VAXC /* just don't ask ... */
2382 /* Table to hold the dirs. */
2384 register unsigned int defsize
= (sizeof (default_include_directories
)
2385 / sizeof (default_include_directories
[0]));
2386 register unsigned int max
= 5;
2387 register char **dirs
= (char **) xmalloc ((5 + defsize
) * sizeof (char *));
2388 register unsigned int idx
= 0;
2394 /* First consider any dirs specified with -I switches.
2395 Ignore dirs that don't exist. */
2398 while (*arg_dirs
!= 0)
2400 char *dir
= *arg_dirs
++;
2404 char *expanded
= tilde_expand (dir
);
2409 if (stat (dir
, &stbuf
) == 0 && S_ISDIR (stbuf
.st_mode
))
2415 xrealloc ((char *) dirs
, (max
+ defsize
) * sizeof (char *));
2419 else if (dir
!= arg_dirs
[-1])
2423 /* Now add at the end the standard default dirs. */
2427 /* The environment variable $DJDIR holds the root of the
2428 DJGPP directory tree; add ${DJDIR}/include. */
2429 struct variable
*djdir
= lookup_variable ("DJDIR", 5);
2433 char *defdir
= (char *) xmalloc (strlen (djdir
->value
) + 8 + 1);
2435 strcat (strcpy (defdir
, djdir
->value
), "/include");
2436 dirs
[idx
++] = defdir
;
2441 for (i
= 0; default_include_directories
[i
] != 0; ++i
)
2442 if (stat (default_include_directories
[i
], &stbuf
) == 0
2443 && S_ISDIR (stbuf
.st_mode
))
2444 dirs
[idx
++] = default_include_directories
[i
];
2448 /* Now compute the maximum length of any name in it. */
2451 for (i
= 0; i
< idx
; ++i
)
2453 unsigned int len
= strlen (dirs
[i
]);
2454 /* If dir name is written with a trailing slash, discard it. */
2455 if (dirs
[i
][len
- 1] == '/')
2456 /* We can't just clobber a null in because it may have come from
2457 a literal string and literal strings may not be writable. */
2458 dirs
[i
] = savestring (dirs
[i
], len
- 1);
2459 if (len
> max_incl_len
)
2463 include_directories
= dirs
;
2466 /* Expand ~ or ~USER at the beginning of NAME.
2467 Return a newly malloc'd string or 0. */
2474 if (name
[1] == '/' || name
[1] == '\0')
2476 extern char *getenv ();
2481 /* Turn off --warn-undefined-variables while we expand HOME. */
2482 int save
= warn_undefined_variables_flag
;
2483 warn_undefined_variables_flag
= 0;
2485 home_dir
= allocated_variable_expand ("$(HOME)");
2487 warn_undefined_variables_flag
= save
;
2490 is_variable
= home_dir
[0] != '\0';
2494 home_dir
= getenv ("HOME");
2496 #if !defined(_AMIGA) && !defined(WINDOWS32)
2497 if (home_dir
== 0 || home_dir
[0] == '\0')
2499 extern char *getlogin ();
2500 char *logname
= getlogin ();
2504 struct passwd
*p
= getpwnam (logname
);
2506 home_dir
= p
->pw_dir
;
2509 #endif /* !AMIGA && !WINDOWS32 */
2512 char *new = concat (home_dir
, "", name
+ 1);
2518 #if !defined(_AMIGA) && !defined(WINDOWS32)
2521 struct passwd
*pwent
;
2522 char *userend
= index (name
+ 1, '/');
2525 pwent
= getpwnam (name
+ 1);
2529 return savestring (pwent
->pw_dir
, strlen (pwent
->pw_dir
));
2531 return concat (pwent
->pw_dir
, "/", userend
+ 1);
2533 else if (userend
!= 0)
2536 #endif /* !AMIGA && !WINDOWS32 */
2541 /* Given a chain of struct nameseq's describing a sequence of filenames,
2542 in reverse of the intended order, return a new chain describing the
2543 result of globbing the filenames. The new chain is in forward order.
2544 The links of the old chain are freed or used in the new chain.
2545 Likewise for the names in the old chain.
2547 SIZE is how big to construct chain elements.
2548 This is useful if we want them actually to be other structures
2549 that have room for additional info. */
2552 multi_glob (chain
, size
)
2553 struct nameseq
*chain
;
2556 extern void dir_setup_glob ();
2557 register struct nameseq
*new = 0;
2558 register struct nameseq
*old
;
2559 struct nameseq
*nexto
;
2562 dir_setup_glob (&gl
);
2564 for (old
= chain
; old
!= 0; old
= nexto
)
2572 if (old
->name
[0] == '~')
2574 char *newname
= tilde_expand (old
->name
);
2578 old
->name
= newname
;
2583 if (ar_name (old
->name
))
2585 /* OLD->name is an archive member reference.
2586 Replace it with the archive file name,
2587 and save the member name in MEMNAME.
2588 We will glob on the archive name and then
2589 reattach MEMNAME later. */
2591 ar_parse_name (old
->name
, &arname
, &memname
);
2597 #endif /* !NO_ARCHIVES */
2599 switch (glob (old
->name
, GLOB_NOCHECK
|GLOB_ALTDIRFUNC
, NULL
, &gl
))
2601 case 0: /* Success. */
2603 register int i
= gl
.gl_pathc
;
2609 /* Try to glob on MEMNAME within the archive. */
2610 struct nameseq
*found
2611 = ar_glob (gl
.gl_pathv
[i
], memname
, size
);
2614 /* No matches. Use MEMNAME as-is. */
2616 = (struct nameseq
*) xmalloc (size
);
2617 unsigned int alen
= strlen (gl
.gl_pathv
[i
]);
2618 unsigned int mlen
= strlen (memname
);
2619 elt
->name
= (char *) xmalloc (alen
+ 1 + mlen
+ 2);
2620 bcopy (gl
.gl_pathv
[i
], elt
->name
, alen
);
2621 elt
->name
[alen
] = '(';
2622 bcopy (memname
, &elt
->name
[alen
+ 1], mlen
);
2623 elt
->name
[alen
+ 1 + mlen
] = ')';
2624 elt
->name
[alen
+ 1 + mlen
+ 1] = '\0';
2630 /* Find the end of the FOUND chain. */
2631 struct nameseq
*f
= found
;
2632 while (f
->next
!= 0)
2635 /* Attach the chain being built to the end of the FOUND
2636 chain, and make FOUND the new NEW chain. */
2644 #endif /* !NO_ARCHIVES */
2646 struct nameseq
*elt
= (struct nameseq
*) xmalloc (size
);
2647 elt
->name
= savestring (gl
.gl_pathv
[i
],
2648 strlen (gl
.gl_pathv
[i
]));
2660 fatal ("virtual memory exhausted");