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. */
40 struct passwd
*getpwnam
PARAMS ((char *name
));
43 #endif /* !WINDOWS32 */
45 /* A 'struct ebuffer' controls the origin of the makefile we are currently
51 char *buffer
; /* Start of the current line in the buffer. */
52 char *bufnext
; /* Start of the next line in the buffer. */
53 char *bufstart
; /* Start of the entire buffer. */
54 unsigned int size
; /* Malloc'd size of buffer. */
55 FILE *fp
; /* File, or NULL if this is an internal buffer. */
56 struct floc floc
; /* Info on the file in fp (if any). */
59 /* Types of "words" that can be read in a makefile. */
62 w_bogus
, w_eol
, w_static
, w_variable
, w_colon
, w_dcolon
, w_semicolon
,
63 w_comment
, w_varassign
67 /* A `struct conditionals' contains the information describing
68 all the active conditionals in a makefile.
70 The global variable `conditionals' contains the conditionals
71 information for the current makefile. It is initialized from
72 the static structure `toplevel_conditionals' and is later changed
73 to new structures for included makefiles. */
77 unsigned int if_cmds
; /* Depth of conditional nesting. */
78 unsigned int allocated
; /* Elts allocated in following arrays. */
79 char *ignoring
; /* Are we ignoring or interepreting? */
80 char *seen_else
; /* Have we already seen an `else'? */
83 static struct conditionals toplevel_conditionals
;
84 static struct conditionals
*conditionals
= &toplevel_conditionals
;
87 /* Default directories to search for include files in */
89 static char *default_include_directories
[] =
91 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
93 * This completely up to the user when they install MSVC or other packages.
94 * This is defined as a placeholder.
96 #define INCLUDEDIR "."
101 "/usr/local/include",
107 /* List of directories to search for include files in */
109 static char **include_directories
;
111 /* Maximum length of an element of the above. */
113 static unsigned int max_incl_len
;
115 /* The filename and pointer to line number of the
116 makefile currently being read in. */
118 const struct floc
*reading_file
= 0;
120 /* The chain of makefiles read by read_makefile. */
122 static struct dep
*read_makefiles
= 0;
124 static int eval_makefile
PARAMS ((char *filename
, int flags
));
125 static int eval
PARAMS ((struct ebuffer
*buffer
, int flags
));
127 static long readline
PARAMS ((struct ebuffer
*ebuf
));
128 static void do_define
PARAMS ((char *name
, unsigned int namelen
,
129 enum variable_origin origin
,
130 struct ebuffer
*ebuf
));
131 static int conditional_line
PARAMS ((char *line
, const struct floc
*flocp
));
132 static void record_files
PARAMS ((struct nameseq
*filenames
, char *pattern
, char *pattern_percent
,
133 struct dep
*deps
, unsigned int cmds_started
, char *commands
,
134 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;
152 /* Create *_LIST variables, to hold the makefiles, targets, and variables
153 we will be reading. */
155 define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file
, 0);
157 DB (DB_BASIC
, (_("Reading makefiles...\n")));
159 /* If there's a non-null variable MAKEFILES, its value is a list of
160 files to read first thing. But don't let it prevent reading the
161 default makefiles and don't let the default goal come from there. */
169 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
170 int save
= warn_undefined_variables_flag
;
171 warn_undefined_variables_flag
= 0;
173 value
= allocated_variable_expand ("$(MAKEFILES)");
175 warn_undefined_variables_flag
= save
;
178 /* Set NAME to the start of next token and LENGTH to its length.
179 MAKEFILES is updated for finding remaining tokens. */
182 while ((name
= find_next_token (&p
, &length
)) != 0)
186 name
= xstrdup (name
);
187 if (eval_makefile (name
,
188 RM_NO_DEFAULT_GOAL
|RM_INCLUDED
|RM_DONTCARE
) < 2)
195 /* Read makefiles specified with -f switches. */
198 while (*makefiles
!= 0)
200 struct dep
*tail
= read_makefiles
;
201 register struct dep
*d
;
203 if (! eval_makefile (*makefiles
, 0))
204 perror_with_name ("", *makefiles
);
206 /* Find the right element of read_makefiles. */
208 while (d
->next
!= tail
)
211 /* Use the storage read_makefile allocates. */
212 *makefiles
= dep_name (d
);
217 /* If there were no -f switches, try the default names. */
219 if (num_makefiles
== 0)
221 static char *default_makefiles
[] =
223 /* all lower case since readdir() (the vms version) 'lowercasifies' */
224 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
227 { "GNUmakefile", "Makefile", "SMakefile", 0 };
228 #else /* !Amiga && !VMS */
229 { "GNUmakefile", "makefile", "Makefile", 0 };
232 register char **p
= default_makefiles
;
233 while (*p
!= 0 && !file_exists_p (*p
))
238 if (! eval_makefile (*p
, 0))
239 perror_with_name ("", *p
);
243 /* No default makefile was found. Add the default makefiles to the
244 `read_makefiles' chain so they will be updated if possible. */
245 struct dep
*tail
= read_makefiles
;
246 /* Add them to the tail, after any MAKEFILES variable makefiles. */
247 while (tail
!= 0 && tail
->next
!= 0)
249 for (p
= default_makefiles
; *p
!= 0; ++p
)
251 struct dep
*d
= (struct dep
*) xmalloc (sizeof (struct dep
));
253 d
->file
= enter_file (*p
);
254 d
->file
->dontcare
= 1;
256 /* Tell update_goal_chain to bail out as soon as this file is
257 made, and main not to die if we can't make this file. */
258 d
->changed
= RM_DONTCARE
;
270 return read_makefiles
;
274 eval_makefile (filename
, flags
)
280 const struct floc
*curfile
;
284 ebuf
.floc
.filenm
= filename
;
285 ebuf
.floc
.lineno
= 1;
287 if (ISDB (DB_VERBOSE
))
289 printf (_("Reading makefile `%s'"), filename
);
290 if (flags
& RM_NO_DEFAULT_GOAL
)
291 printf (_(" (no default goal)"));
292 if (flags
& RM_INCLUDED
)
293 printf (_(" (search path)"));
294 if (flags
& RM_DONTCARE
)
295 printf (_(" (don't care)"));
296 if (flags
& RM_NO_TILDE
)
297 printf (_(" (no ~ expansion)"));
301 /* First, get a stream to read. */
303 /* Expand ~ in FILENAME unless it came from `include',
304 in which case it was already done. */
305 if (!(flags
& RM_NO_TILDE
) && filename
[0] == '~')
307 char *expanded
= tilde_expand (filename
);
312 ebuf
.fp
= fopen (filename
, "r");
313 /* Save the error code so we print the right message later. */
314 makefile_errno
= errno
;
316 /* If the makefile wasn't found and it's either a makefile from
317 the `MAKEFILES' variable or an included makefile,
318 search the included makefile search path for this makefile. */
319 if (ebuf
.fp
== 0 && (flags
& RM_INCLUDED
) && *filename
!= '/')
321 register unsigned int i
;
322 for (i
= 0; include_directories
[i
] != 0; ++i
)
324 char *name
= concat (include_directories
[i
], "/", filename
);
325 ebuf
.fp
= fopen (name
, "r");
336 /* Add FILENAME to the chain of read makefiles. */
337 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
338 deps
->next
= read_makefiles
;
339 read_makefiles
= deps
;
341 deps
->file
= lookup_file (filename
);
344 deps
->file
= enter_file (xstrdup (filename
));
345 if (flags
& RM_DONTCARE
)
346 deps
->file
->dontcare
= 1;
348 if (filename
!= ebuf
.floc
.filenm
)
350 filename
= deps
->file
->name
;
351 deps
->changed
= flags
;
352 deps
->ignore_mtime
= 0;
354 /* If the makefile can't be found at all, give up entirely. */
358 /* If we did some searching, errno has the error from the last
359 attempt, rather from FILENAME itself. Restore it in case the
360 caller wants to use it in a message. */
361 errno
= makefile_errno
;
365 /* Add this makefile to the list. */
366 do_variable_definition (&ebuf
.floc
, "MAKEFILE_LIST", filename
, o_file
,
369 /* Evaluate the makefile */
372 ebuf
.buffer
= ebuf
.bufnext
= ebuf
.bufstart
= xmalloc (ebuf
.size
);
374 curfile
= reading_file
;
375 reading_file
= &ebuf
.floc
;
377 r
= eval (&ebuf
, !(flags
& RM_NO_DEFAULT_GOAL
));
379 reading_file
= curfile
;
389 const struct floc
*curfile
;
392 /* Evaluate the buffer */
394 ebuf
.size
= strlen (buffer
);
395 ebuf
.buffer
= ebuf
.bufnext
= ebuf
.bufstart
= buffer
;
398 ebuf
.floc
= *reading_file
;
400 curfile
= reading_file
;
401 reading_file
= &ebuf
.floc
;
405 reading_file
= curfile
;
411 /* Read file FILENAME as a makefile and add its contents to the data base.
413 SET_DEFAULT is true if we are allowed to set the default goal.
415 FILENAME is added to the `read_makefiles' chain.
417 Returns 0 if a file was not found or not read.
418 Returns 1 if FILENAME was found and read.
419 Returns 2 if FILENAME was read, and we kept a reference (don't free it). */
422 eval (ebuf
, set_default
)
423 struct ebuffer
*ebuf
;
426 static char *collapsed
= 0;
427 static unsigned int collapsed_length
= 0;
428 unsigned int commands_len
= 200;
430 unsigned int commands_idx
= 0;
431 unsigned int cmds_started
, tgts_started
;
432 int ignoring
= 0, in_ignored_define
= 0;
433 int no_targets
= 0; /* Set when reading a rule without targets. */
434 int have_sysv_atvar
= 0;
435 struct nameseq
*filenames
= 0;
436 struct dep
*deps
= 0;
439 char *pattern
= 0, *pattern_percent
;
443 #if defined (WINDOWS32) || defined (__MSDOS__)
447 #define record_waiting_files() \
450 if (filenames != 0) \
452 fi.lineno = tgts_started; \
453 record_files (filenames, pattern, pattern_percent, deps, \
454 cmds_started, commands, commands_idx, two_colon, \
455 have_sysv_atvar, &fi, set_default); \
460 if (pattern) { free(pattern); pattern = 0; } \
464 cmds_started
= tgts_started
= 1;
466 fstart
= &ebuf
->floc
;
467 fi
.filenm
= ebuf
->floc
.filenm
;
469 /* Loop over lines in the file.
470 The strategy is to accumulate target names in FILENAMES, dependencies
471 in DEPS and commands in COMMANDS. These are used to define a rule
472 when the start of the next rule (or eof) is encountered.
474 When you see a "continue" in the loop below, that means we are moving on
475 to the next line _without_ ending any rule that we happen to be working
476 with at the moment. If you see a "goto rule_complete", then the
477 statement we just parsed also finishes the previous rule. */
479 commands
= xmalloc (200);
489 /* Grab the next line to be evaluated */
490 ebuf
->floc
.lineno
+= nlines
;
491 nlines
= readline (ebuf
);
493 /* If there is nothing left to eval, we're done. */
497 /* If this line is empty, skip it. */
502 linelen
= strlen (line
);
504 /* Check for a shell command line first.
505 If it is not one, we can stop treating tab specially. */
509 /* Ignore the commands in a rule with no targets. */
512 /* If there is no preceding rule line, don't treat this line
513 as a command, even though it begins with a tab character.
514 SunOS 4 make appears to behave this way. */
519 /* Yep, this is a shell command, and we don't care. */
522 /* Append this command line to the line being accumulated. */
523 if (commands_idx
== 0)
524 cmds_started
= ebuf
->floc
.lineno
;
526 if (linelen
+ 1 + commands_idx
> commands_len
)
528 commands_len
= (linelen
+ 1 + commands_idx
) * 2;
529 commands
= xrealloc (commands
, commands_len
);
531 bcopy (line
, &commands
[commands_idx
], linelen
);
532 commands_idx
+= linelen
;
533 commands
[commands_idx
++] = '\n';
539 /* This line is not a shell command line. Don't worry about tabs. */
541 if (collapsed_length
< linelen
+1)
543 collapsed_length
= linelen
+1;
546 collapsed
= (char *) xmalloc (collapsed_length
);
548 strcpy (collapsed
, line
);
549 /* Collapse continuation lines. */
550 collapse_continuations (collapsed
);
551 remove_comments (collapsed
);
553 /* Compare a word, both length and contents. */
554 #define word1eq(s) (len == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
556 while (isspace ((unsigned char)*p
))
560 /* This line is completely empty--ignore it. */
563 /* Find the end of the first token. Note we don't need to worry about
564 * ":" here since we compare tokens by length (so "export" will never
565 * be equal to "export:").
567 for (p2
= p
+1; *p2
!= '\0' && !isspace ((unsigned char)*p2
); ++p2
)
571 /* Find the start of the second token. If it looks like a target or
572 variable definition it can't be a preprocessor token so skip
573 them--this allows variables/targets named `ifdef', `export', etc. */
574 while (isspace ((unsigned char)*p2
))
577 if ((p2
[0] == ':' || p2
[0] == '+' || p2
[0] == '=') && p2
[1] == '\0')
579 /* It can't be a preprocessor token so skip it if we're ignoring */
583 goto skip_conditionals
;
586 /* We must first check for conditional and `define' directives before
587 ignoring anything, since they control what we will do with
590 if (!in_ignored_define
591 && (word1eq ("ifdef") || word1eq ("ifndef")
592 || word1eq ("ifeq") || word1eq ("ifneq")
593 || word1eq ("else") || word1eq ("endif")))
595 int i
= conditional_line (p
, fstart
);
597 fatal (fstart
, _("invalid syntax in conditional"));
603 if (word1eq ("endef"))
605 if (!in_ignored_define
)
606 fatal (fstart
, _("extraneous `endef'"));
607 in_ignored_define
= 0;
611 if (word1eq ("define"))
614 in_ignored_define
= 1;
618 fatal (fstart
, _("empty variable name"));
620 /* Let the variable name be the whole rest of the line,
621 with trailing blanks stripped (comments have already been
622 removed), so it could be a complex variable/function
623 reference that might contain blanks. */
624 p
= strchr (p2
, '\0');
625 while (isblank ((unsigned char)p
[-1]))
627 do_define (p2
, p
- p2
, o_file
, ebuf
);
632 if (word1eq ("override"))
635 error (fstart
, _("empty `override' directive"));
637 if (strneq (p2
, "define", 6)
638 && (isblank ((unsigned char)p2
[6]) || p2
[6] == '\0'))
641 in_ignored_define
= 1;
644 p2
= next_token (p2
+ 6);
646 fatal (fstart
, _("empty variable name"));
648 /* Let the variable name be the whole rest of the line,
649 with trailing blanks stripped (comments have already been
650 removed), so it could be a complex variable/function
651 reference that might contain blanks. */
652 p
= strchr (p2
, '\0');
653 while (isblank ((unsigned char)p
[-1]))
655 do_define (p2
, p
- p2
, o_override
, ebuf
);
659 && !try_variable_definition (fstart
, p2
, o_override
, 0))
660 error (fstart
, _("invalid `override' directive"));
666 /* Ignore the line. We continue here so conditionals
667 can appear in the middle of a rule. */
670 if (word1eq ("export"))
672 /* 'export' by itself causes everything to be exported. */
674 export_all_variables
= 1;
679 v
= try_variable_definition (fstart
, p2
, o_file
, 0);
681 v
->export
= v_export
;
685 for (p
= find_next_token (&p2
, &len
); p
!= 0;
686 p
= find_next_token (&p2
, &len
))
688 v
= lookup_variable (p
, len
);
690 v
= define_variable_loc (p
, len
, "", o_file
, 0,
692 v
->export
= v_export
;
699 if (word1eq ("unexport"))
702 export_all_variables
= 0;
707 for (p
= find_next_token (&p2
, &len
); p
!= 0;
708 p
= find_next_token (&p2
, &len
))
710 v
= lookup_variable (p
, len
);
712 v
= define_variable_loc (p
, len
, "", o_file
, 0, fstart
);
713 v
->export
= v_noexport
;
720 if (word1eq ("vpath"))
724 p2
= variable_expand (p2
);
725 p
= find_next_token (&p2
, &len
);
728 pattern
= savestring (p
, len
);
729 p
= find_next_token (&p2
, &len
);
730 /* No searchpath means remove all previous
731 selective VPATH's with the same pattern. */
734 /* No pattern means remove all previous selective VPATH's. */
736 construct_vpath_list (pattern
, p
);
743 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
745 /* We have found an `include' line specifying a nested
746 makefile to be read at this point. */
747 struct conditionals
*save
;
748 struct conditionals new_conditionals
;
749 struct nameseq
*files
;
750 /* "-include" (vs "include") says no error if the file does not
751 exist. "sinclude" is an alias for this from SGI. */
752 int noerror
= (p
[0] != 'i');
754 p
= allocated_variable_expand (p2
);
758 _("no file name for `%sinclude'"), noerror
? "-" : "");
762 /* Parse the list of file names. */
764 files
= multi_glob (parse_file_seq (&p2
, '\0',
765 sizeof (struct nameseq
),
767 sizeof (struct nameseq
));
770 /* Save the state of conditionals and start
771 the included makefile with a clean slate. */
773 bzero ((char *) &new_conditionals
, sizeof new_conditionals
);
774 conditionals
= &new_conditionals
;
776 /* Record the rules that are waiting so they will determine
777 the default goal before those in the included makefile. */
778 record_waiting_files ();
780 /* Read each included makefile. */
783 struct nameseq
*next
= files
->next
;
784 char *name
= files
->name
;
787 free ((char *)files
);
790 r
= eval_makefile (name
, (RM_INCLUDED
| RM_NO_TILDE
791 | (noerror
? RM_DONTCARE
: 0)));
795 error (fstart
, "%s: %s", name
, strerror (errno
));
800 /* Free any space allocated by conditional_line. */
801 if (conditionals
->ignoring
)
802 free (conditionals
->ignoring
);
803 if (conditionals
->seen_else
)
804 free (conditionals
->seen_else
);
812 if (try_variable_definition (fstart
, p
, o_file
, 0))
813 /* This line has been dealt with. */
818 p
= collapsed
; /* Ignore comments, etc. */
819 while (isblank ((unsigned char)*p
))
822 /* The line is completely blank; that is harmless. */
825 /* This line starts with a tab but was not caught above
826 because there was no preceding target, and the line
827 might have been usable as a variable definition.
828 But now we know it is definitely lossage. */
829 fatal(fstart
, _("commands commence before first target"));
832 /* This line describes some target files. This is complicated by
833 the existence of target-specific variables, because we can't
834 expand the entire line until we know if we have one or not. So
835 we expand the line word by word until we find the first `:',
836 then check to see if it's a target-specific variable.
838 In this algorithm, `lb_next' will point to the beginning of the
839 unexpanded parts of the input buffer, while `p2' points to the
840 parts of the expanded buffer we haven't searched yet. */
843 enum make_word_type wtype
;
844 enum variable_origin v_origin
;
845 char *cmdleft
, *semip
, *lb_next
;
846 unsigned int len
, plen
= 0;
849 /* Record the previous rule. */
851 record_waiting_files ();
852 tgts_started
= fstart
->lineno
;
854 /* Search the line for an unquoted ; that is not after an
856 cmdleft
= find_char_unquote (line
, ";#", 0);
857 if (cmdleft
!= 0 && *cmdleft
== '#')
859 /* We found a comment before a semicolon. */
863 else if (cmdleft
!= 0)
864 /* Found one. Cut the line short there before expanding it. */
868 collapse_continuations (line
);
870 /* We can't expand the entire line, since if it's a per-target
871 variable we don't want to expand it. So, walk from the
872 beginning, expanding as we go, and looking for "interesting"
873 chars. The first word is always expandable. */
874 wtype
= get_next_mword(line
, NULL
, &lb_next
, &len
);
879 fatal(fstart
, _("missing rule before commands"));
880 /* This line contained something but turned out to be nothing
881 but whitespace (a comment?). */
886 /* We accept and ignore rules without targets for
887 compatibility with SunOS 4 make. */
895 p2
= variable_expand_string(NULL
, lb_next
, len
);
901 /* Look for a semicolon in the expanded line. */
902 cmdleft
= find_char_unquote (p2
, ";", 0);
906 unsigned long p2_off
= p2
- variable_buffer
;
907 unsigned long cmd_off
= cmdleft
- variable_buffer
;
908 char *pend
= p2
+ strlen(p2
);
910 /* Append any remnants of lb, then cut the line short
914 /* One school of thought says that you shouldn't expand
915 here, but merely copy, since now you're beyond a ";"
916 and into a command script. However, the old parser
917 expanded the whole line, so we continue that for
918 backwards-compatiblity. Also, it wouldn't be
919 entirely consistent, since we do an unconditional
920 expand below once we know we don't have a
921 target-specific variable. */
922 (void)variable_expand_string(pend
, lb_next
, (long)-1);
923 lb_next
+= strlen(lb_next
);
924 p2
= variable_buffer
+ p2_off
;
925 cmdleft
= variable_buffer
+ cmd_off
+ 1;
929 colonp
= find_char_unquote(p2
, ":", 0);
930 #if defined(__MSDOS__) || defined(WINDOWS32)
931 /* The drive spec brain-damage strikes again... */
932 /* Note that the only separators of targets in this context
933 are whitespace and a left paren. If others are possible,
934 they should be added to the string in the call to index. */
935 while (colonp
&& (colonp
[1] == '/' || colonp
[1] == '\\') &&
936 colonp
> p2
&& isalpha ((unsigned char)colonp
[-1]) &&
937 (colonp
== p2
+ 1 || strchr (" \t(", colonp
[-2]) != 0))
938 colonp
= find_char_unquote(colonp
+ 1, ":", 0);
943 wtype
= get_next_mword(lb_next
, NULL
, &lb_next
, &len
);
949 p2
= variable_expand_string(p2
, lb_next
, len
);
950 /* We don't need to worry about cmdleft here, because if it was
951 found in the variable_buffer the entire buffer has already
952 been expanded... we'll never get here. */
955 p2
= next_token (variable_buffer
);
957 /* If the word we're looking at is EOL, see if there's _anything_
958 on the line. If not, a variable expanded to nothing, so ignore
959 it. If so, we can't parse this line so punt. */
963 /* There's no need to be ivory-tower about this: check for
964 one of the most common bugs found in makefiles... */
965 fatal (fstart
, _("missing separator%s"),
966 !strneq(line
, " ", 8) ? ""
967 : _(" (did you mean TAB instead of 8 spaces?)"));
971 /* Make the colon the end-of-string so we know where to stop
972 looking for targets. */
974 filenames
= multi_glob (parse_file_seq (&p2
, '\0',
975 sizeof (struct nameseq
),
977 sizeof (struct nameseq
));
982 /* We accept and ignore rules without targets for
983 compatibility with SunOS 4 make. */
987 /* This should never be possible; we handled it above. */
988 assert (*p2
!= '\0');
991 /* Is this a one-colon or two-colon entry? */
992 two_colon
= *p2
== ':';
996 /* Test to see if it's a target-specific variable. Copy the rest
997 of the buffer over, possibly temporarily (we'll expand it later
998 if it's not a target-specific variable). PLEN saves the length
999 of the unparsed section of p2, for later. */
1000 if (*lb_next
!= '\0')
1002 unsigned int l
= p2
- variable_buffer
;
1004 (void) variable_buffer_output (p2
+plen
,
1005 lb_next
, strlen (lb_next
)+1);
1006 p2
= variable_buffer
+ l
;
1009 /* See if it's an "override" keyword; if so see if what comes after
1010 it looks like a variable definition. */
1012 wtype
= get_next_mword (p2
, NULL
, &p
, &len
);
1015 if (wtype
== w_static
&& word1eq ("override"))
1017 v_origin
= o_override
;
1018 wtype
= get_next_mword (p
+len
, NULL
, &p
, &len
);
1022 wtype
= get_next_mword (p
+len
, NULL
, NULL
, NULL
);
1024 if (wtype
== w_varassign
)
1026 /* If there was a semicolon found, add it back, plus anything
1031 variable_buffer_output (p2
+ strlen (p2
),
1032 semip
, strlen (semip
)+1);
1034 record_target_var (filenames
, p
, two_colon
, v_origin
, fstart
);
1039 /* This is a normal target, _not_ a target-specific variable.
1040 Unquote any = in the dependency list. */
1041 find_char_unquote (lb_next
, "=", 0);
1043 /* We have some targets, so don't ignore the following commands. */
1046 /* Expand the dependencies, etc. */
1047 if (*lb_next
!= '\0')
1049 unsigned int l
= p2
- variable_buffer
;
1050 (void) variable_expand_string (p2
+ plen
, lb_next
, (long)-1);
1051 p2
= variable_buffer
+ l
;
1053 /* Look for a semicolon in the expanded line. */
1056 cmdleft
= find_char_unquote (p2
, ";", 0);
1058 *(cmdleft
++) = '\0';
1062 /* Do any of the prerequisites appear to have $@ etc.? */
1063 have_sysv_atvar
= 0;
1064 if (!posix_pedantic
)
1065 for (p
= strchr (p2
, '$'); p
!= 0; p
= strchr (p
+1, '$'))
1066 if (p
[1] == '@' || (p
[1] == '(' && p
[2] == '@'))
1068 have_sysv_atvar
= 1;
1072 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1073 p
= strchr (p2
, ':');
1074 while (p
!= 0 && p
[-1] == '\\')
1076 register char *q
= &p
[-1];
1077 register int backslash
= 0;
1078 while (*q
-- == '\\')
1079 backslash
= !backslash
;
1081 p
= strchr (p
+ 1, ':');
1086 /* Here, the situation is quite complicated. Let's have a look
1087 at a couple of targets:
1095 The rule is that it's only a target, if there are TWO :'s
1096 OR a space around the :.
1098 if (p
&& !(isspace ((unsigned char)p
[1]) || !p
[1]
1099 || isspace ((unsigned char)p
[-1])))
1102 #if defined (WINDOWS32) || defined (__MSDOS__)
1105 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
1106 if (p
!= 0 && (p
[1] == '\\' || p
[1] == '/') &&
1107 isalpha ((unsigned char)p
[-1]) &&
1108 (p
== p2
+ 1 || strchr (" \t:(", p
[-2]) != 0)) {
1109 p
= strchr (p
+ 1, ':');
1112 } while (check_again
);
1116 struct nameseq
*target
;
1117 target
= parse_file_seq (&p2
, ':', sizeof (struct nameseq
), 1);
1120 fatal (fstart
, _("missing target pattern"));
1121 else if (target
->next
!= 0)
1122 fatal (fstart
, _("multiple target patterns"));
1123 pattern
= target
->name
;
1124 pattern_percent
= find_percent (pattern
);
1125 if (pattern_percent
== 0)
1126 fatal (fstart
, _("target pattern contains no `%%'"));
1127 free((char *)target
);
1132 /* Parse the dependencies. */
1133 deps
= (struct dep
*)
1134 multi_glob (parse_file_seq (&p2
, '|', sizeof (struct dep
), 1),
1135 sizeof (struct dep
));
1138 /* Files that follow '|' are special prerequisites that
1139 need only exist in order to satisfy the dependency.
1140 Their modification times are irrelevant. */
1141 struct dep
**deps_ptr
= &deps
;
1143 for (deps_ptr
= &deps
; *deps_ptr
; deps_ptr
= &(*deps_ptr
)->next
)
1146 *deps_ptr
= (struct dep
*)
1147 multi_glob (parse_file_seq (&p2
, '\0', sizeof (struct dep
), 1),
1148 sizeof (struct dep
));
1149 for (d
= *deps_ptr
; d
!= 0; d
= d
->next
)
1150 d
->ignore_mtime
= 1;
1156 /* Semicolon means rest of line is a command. */
1157 unsigned int len
= strlen (cmdleft
);
1159 cmds_started
= fstart
->lineno
;
1161 /* Add this command line to the buffer. */
1162 if (len
+ 2 > commands_len
)
1164 commands_len
= (len
+ 2) * 2;
1165 commands
= (char *) xrealloc (commands
, commands_len
);
1167 bcopy (cmdleft
, commands
, len
);
1168 commands_idx
+= len
;
1169 commands
[commands_idx
++] = '\n';
1175 /* We get here except in the case that we just read a rule line.
1176 Record now the last rule we read, so following spurious
1177 commands are properly diagnosed. */
1179 record_waiting_files ();
1184 if (conditionals
->if_cmds
)
1185 fatal (fstart
, _("missing `endif'"));
1187 /* At eof, record the last rule. */
1188 record_waiting_files ();
1190 free ((char *) commands
);
1196 /* Execute a `define' directive.
1197 The first line has already been read, and NAME is the name of
1198 the variable to be defined. The following lines remain to be read. */
1201 do_define (name
, namelen
, origin
, ebuf
)
1203 unsigned int namelen
;
1204 enum variable_origin origin
;
1205 struct ebuffer
*ebuf
;
1207 struct floc defstart
;
1210 unsigned int length
= 100;
1211 char *definition
= (char *) xmalloc (length
);
1212 unsigned int idx
= 0;
1215 /* Expand the variable name. */
1216 char *var
= (char *) alloca (namelen
+ 1);
1217 bcopy (name
, var
, namelen
);
1218 var
[namelen
] = '\0';
1219 var
= variable_expand (var
);
1221 defstart
= ebuf
->floc
;
1228 ebuf
->floc
.lineno
+= nlines
;
1229 nlines
= readline (ebuf
);
1231 /* If there is nothing left to eval, we're done. */
1235 line
= ebuf
->buffer
;
1237 collapse_continuations (line
);
1239 /* If the line doesn't begin with a tab, test to see if it introduces
1240 another define, or ends one. */
1242 /* Stop if we find an 'endef' */
1243 if (line
[0] != '\t')
1245 p
= next_token (line
);
1248 /* If this is another 'define', increment the level count. */
1249 if ((len
== 6 || (len
> 6 && isblank ((unsigned char)p
[6])))
1250 && strneq (p
, "define", 6))
1253 /* If this is an 'endef', decrement the count. If it's now 0,
1254 we've found the last one. */
1255 else if ((len
== 5 || (len
> 5 && isblank ((unsigned char)p
[5])))
1256 && strneq (p
, "endef", 5))
1259 remove_comments (p
);
1260 if (*next_token (p
) != '\0')
1262 _("Extraneous text after `endef' directive"));
1266 /* Define the variable. */
1268 definition
[0] = '\0';
1270 definition
[idx
- 1] = '\0';
1272 /* Always define these variables in the global set. */
1273 define_variable_global (var
, strlen (var
), definition
,
1274 origin
, 1, &defstart
);
1281 /* Otherwise add this line to the variable definition. */
1282 len
= strlen (line
);
1283 if (idx
+ len
+ 1 > length
)
1285 length
= (idx
+ len
) * 2;
1286 definition
= (char *) xrealloc (definition
, length
+ 1);
1289 bcopy (line
, &definition
[idx
], len
);
1291 /* Separate lines with a newline. */
1292 definition
[idx
++] = '\n';
1296 fatal (&defstart
, _("missing `endef', unterminated `define'"));
1302 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1303 "ifneq", "else" and "endif".
1304 LINE is the input line, with the command as its first word.
1306 FILENAME and LINENO are the filename and line number in the
1307 current makefile. They are used for error messages.
1309 Value is -1 if the line is invalid,
1310 0 if following text should be interpreted,
1311 1 if following text should be ignored. */
1314 conditional_line (line
, flocp
)
1316 const struct floc
*flocp
;
1320 register unsigned int i
;
1324 /* It's an "if..." command. */
1325 notdef
= line
[2] == 'n';
1328 cmdname
= line
[3] == 'd' ? "ifndef" : "ifneq";
1329 line
+= cmdname
[3] == 'd' ? 7 : 6;
1333 cmdname
= line
[2] == 'd' ? "ifdef" : "ifeq";
1334 line
+= cmdname
[2] == 'd' ? 6 : 5;
1339 /* It's an "else" or "endif" command. */
1340 notdef
= line
[1] == 'n';
1341 cmdname
= notdef
? "endif" : "else";
1342 line
+= notdef
? 5 : 4;
1345 line
= next_token (line
);
1347 if (*cmdname
== 'e')
1350 error (flocp
, _("Extraneous text after `%s' directive"), cmdname
);
1351 /* "Else" or "endif". */
1352 if (conditionals
->if_cmds
== 0)
1353 fatal (flocp
, _("extraneous `%s'"), cmdname
);
1354 /* NOTDEF indicates an `endif' command. */
1356 --conditionals
->if_cmds
;
1357 else if (conditionals
->seen_else
[conditionals
->if_cmds
- 1])
1358 fatal (flocp
, _("only one `else' per conditional"));
1361 /* Toggle the state of ignorance. */
1362 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1363 = !conditionals
->ignoring
[conditionals
->if_cmds
- 1];
1364 /* Record that we have seen an `else' in this conditional.
1365 A second `else' will be erroneous. */
1366 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 1;
1368 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1369 if (conditionals
->ignoring
[i
])
1374 if (conditionals
->allocated
== 0)
1376 conditionals
->allocated
= 5;
1377 conditionals
->ignoring
= (char *) xmalloc (conditionals
->allocated
);
1378 conditionals
->seen_else
= (char *) xmalloc (conditionals
->allocated
);
1381 ++conditionals
->if_cmds
;
1382 if (conditionals
->if_cmds
> conditionals
->allocated
)
1384 conditionals
->allocated
+= 5;
1385 conditionals
->ignoring
= (char *)
1386 xrealloc (conditionals
->ignoring
, conditionals
->allocated
);
1387 conditionals
->seen_else
= (char *)
1388 xrealloc (conditionals
->seen_else
, conditionals
->allocated
);
1391 /* Record that we have seen an `if...' but no `else' so far. */
1392 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 0;
1394 /* Search through the stack to see if we're already ignoring. */
1395 for (i
= 0; i
< conditionals
->if_cmds
- 1; ++i
)
1396 if (conditionals
->ignoring
[i
])
1398 /* We are already ignoring, so just push a level
1399 to match the next "else" or "endif", and keep ignoring.
1400 We don't want to expand variables in the condition. */
1401 conditionals
->ignoring
[conditionals
->if_cmds
- 1] = 1;
1405 if (cmdname
[notdef
? 3 : 2] == 'd')
1407 /* "Ifdef" or "ifndef". */
1409 register char *p
= end_of_token (line
);
1414 v
= lookup_variable (line
, i
);
1415 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1416 = (v
!= 0 && *v
->value
!= '\0') == notdef
;
1420 /* "Ifeq" or "ifneq". */
1423 char termin
= *line
== '(' ? ',' : *line
;
1425 if (termin
!= ',' && termin
!= '"' && termin
!= '\'')
1429 /* Find the end of the first string. */
1432 register int count
= 0;
1433 for (; *line
!= '\0'; ++line
)
1436 else if (*line
== ')')
1438 else if (*line
== ',' && count
<= 0)
1442 while (*line
!= '\0' && *line
!= termin
)
1450 /* Strip blanks after the first string. */
1452 while (isblank ((unsigned char)p
[-1]))
1459 s2
= variable_expand (s1
);
1460 /* We must allocate a new copy of the expanded string because
1461 variable_expand re-uses the same buffer. */
1463 s1
= (char *) alloca (len
+ 1);
1464 bcopy (s2
, s1
, len
+ 1);
1467 /* Find the start of the second string. */
1468 line
= next_token (line
);
1470 termin
= termin
== ',' ? ')' : *line
;
1471 if (termin
!= ')' && termin
!= '"' && termin
!= '\'')
1474 /* Find the end of the second string. */
1477 register int count
= 0;
1478 s2
= next_token (line
);
1479 for (line
= s2
; *line
!= '\0'; ++line
)
1483 else if (*line
== ')')
1496 while (*line
!= '\0' && *line
!= termin
)
1504 line
= next_token (++line
);
1506 error (flocp
, _("Extraneous text after `%s' directive"), cmdname
);
1508 s2
= variable_expand (s2
);
1509 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1510 = streq (s1
, s2
) == notdef
;
1513 /* Search through the stack to see if we're ignoring. */
1514 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1515 if (conditionals
->ignoring
[i
])
1520 /* Remove duplicate dependencies in CHAIN. */
1523 uniquize_deps (chain
)
1526 register struct dep
*d
;
1528 /* Make sure that no dependencies are repeated. This does not
1529 really matter for the purpose of updating targets, but it
1530 might make some names be listed twice for $^ and $?. */
1532 for (d
= chain
; d
!= 0; d
= d
->next
)
1534 struct dep
*last
, *next
;
1539 if (streq (dep_name (d
), dep_name (next
)))
1541 struct dep
*n
= next
->next
;
1542 /* If ignore_mtimes are not equal, one of these is an order-only
1543 prerequisite and one isn't. That means that we should remove
1544 the one that isn't and keep the one that is. Ideally we'd
1545 like to keep the normal one always but that's hard, and
1546 probably not very important, so just remove the second one and
1547 force the first one to be normal. */
1548 if (d
->ignore_mtime
!= next
->ignore_mtime
)
1549 d
->ignore_mtime
= 0;
1551 if (next
->name
!= 0 && next
->name
!= d
->name
)
1554 free ((char *) next
);
1565 /* Record target-specific variable values for files FILENAMES.
1566 TWO_COLON is nonzero if a double colon was used.
1568 The links of FILENAMES are freed, and so are any names in it
1569 that are not incorporated into other data structures.
1571 If the target is a pattern, add the variable to the pattern-specific
1572 variable value list. */
1575 record_target_var (filenames
, defn
, two_colon
, origin
, flocp
)
1576 struct nameseq
*filenames
;
1579 enum variable_origin origin
;
1580 const struct floc
*flocp
;
1582 struct nameseq
*nextf
;
1583 struct variable_set_list
*global
;
1585 global
= current_variable_set_list
;
1587 /* If the variable is an append version, store that but treat it as a
1588 normal recursive variable. */
1590 for (; filenames
!= 0; filenames
= nextf
)
1593 register char *name
= filenames
->name
;
1594 struct variable_set_list
*vlist
;
1598 nextf
= filenames
->next
;
1599 free ((char *) filenames
);
1601 /* If it's a pattern target, then add it to the pattern-specific
1603 percent
= find_percent (name
);
1606 struct pattern_var
*p
;
1608 /* Get a reference for this pattern-specific variable struct. */
1609 p
= create_pattern_var(name
, percent
);
1617 /* Get a file reference for this file, and initialize it. */
1618 f
= enter_file (name
);
1619 initialize_file_variables (f
, 1);
1620 vlist
= f
->variables
;
1624 /* Make the new variable context current and define the variable. */
1625 current_variable_set_list
= vlist
;
1626 v
= try_variable_definition (flocp
, defn
, origin
, 1);
1628 error (flocp
, _("Malformed per-target variable definition"));
1631 /* If it's not an override, check to see if there was a command-line
1632 setting. If so, reset the value. */
1633 if (origin
!= o_override
)
1635 struct variable
*gv
;
1636 int len
= strlen(v
->name
);
1638 current_variable_set_list
= global
;
1639 gv
= lookup_variable (v
->name
, len
);
1640 if (gv
&& (gv
->origin
== o_env_override
|| gv
->origin
== o_command
))
1642 v
= define_variable_in_set (v
->name
, len
, gv
->value
, gv
->origin
,
1643 gv
->recursive
, vlist
->set
, flocp
);
1648 /* Free name if not needed further. */
1649 if (name
!= fname
&& (name
< fname
|| name
> fname
+ strlen (fname
)))
1653 current_variable_set_list
= global
;
1656 /* Record a description line for files FILENAMES,
1657 with dependencies DEPS, commands to execute described
1658 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1659 TWO_COLON is nonzero if a double colon was used.
1660 If not nil, PATTERN is the `%' pattern to make this
1661 a static pattern rule, and PATTERN_PERCENT is a pointer
1662 to the `%' within it.
1664 The links of FILENAMES are freed, and so are any names in it
1665 that are not incorporated into other data structures. */
1668 record_files (filenames
, pattern
, pattern_percent
, deps
, cmds_started
,
1669 commands
, commands_idx
, two_colon
, have_sysv_atvar
,
1671 struct nameseq
*filenames
;
1672 char *pattern
, *pattern_percent
;
1674 unsigned int cmds_started
;
1676 unsigned int commands_idx
;
1678 int have_sysv_atvar
;
1679 const struct floc
*flocp
;
1682 struct nameseq
*nextf
;
1684 unsigned int max_targets
= 0, target_idx
= 0;
1685 char **targets
= 0, **target_percents
= 0;
1686 struct commands
*cmds
;
1688 if (commands_idx
> 0)
1690 cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
1691 cmds
->fileinfo
.filenm
= flocp
->filenm
;
1692 cmds
->fileinfo
.lineno
= cmds_started
;
1693 cmds
->commands
= savestring (commands
, commands_idx
);
1694 cmds
->command_lines
= 0;
1699 for (; filenames
!= 0; filenames
= nextf
)
1701 char *name
= filenames
->name
;
1705 char *implicit_percent
;
1707 nextf
= filenames
->next
;
1710 /* Check for .POSIX. We used to do this in snap_deps() but that's not
1711 good enough: it doesn't happen until after the makefile is read,
1712 which means we cannot use its value during parsing. */
1714 if (streq (name
, ".POSIX"))
1717 implicit_percent
= find_percent (name
);
1718 implicit
|= implicit_percent
!= 0;
1720 if (implicit
&& pattern
!= 0)
1721 fatal (flocp
, _("mixed implicit and static pattern rules"));
1723 if (implicit
&& implicit_percent
== 0)
1724 fatal (flocp
, _("mixed implicit and normal rules"));
1731 targets
= (char **) xmalloc (5 * sizeof (char *));
1732 target_percents
= (char **) xmalloc (5 * sizeof (char *));
1735 else if (target_idx
== max_targets
- 1)
1738 targets
= (char **) xrealloc ((char *) targets
,
1739 max_targets
* sizeof (char *));
1741 = (char **) xrealloc ((char *) target_percents
,
1742 max_targets
* sizeof (char *));
1744 targets
[target_idx
] = name
;
1745 target_percents
[target_idx
] = implicit_percent
;
1750 /* If there are multiple filenames, copy the chain DEPS
1751 for all but the last one. It is not safe for the same deps
1752 to go in more than one place in the data base. */
1753 this = nextf
!= 0 ? copy_dep_chain (deps
) : deps
;
1757 /* If this is an extended static rule:
1758 `targets: target%pattern: dep%pattern; cmds',
1759 translate each dependency pattern into a plain filename
1760 using the target pattern and this target's name. */
1761 if (!pattern_matches (pattern
, pattern_percent
, name
))
1763 /* Give a warning if the rule is meaningless. */
1765 _("target `%s' doesn't match the target pattern"), name
);
1770 /* We use patsubst_expand to do the work of translating
1771 the target pattern, the target's name and the dependencies'
1772 patterns into plain dependency names. */
1773 char *buffer
= variable_expand ("");
1775 for (d
= this; d
!= 0; d
= d
->next
)
1778 char *percent
= find_percent (d
->name
);
1781 o
= patsubst_expand (buffer
, name
, pattern
, d
->name
,
1782 pattern_percent
, percent
);
1783 /* If the name expanded to the empty string, that's
1787 _("target `%s' leaves prerequisite pattern empty"),
1790 d
->name
= savestring (buffer
, o
- buffer
);
1795 /* If at least one of the dependencies uses $$@ etc. deal with that.
1796 It would be very nice and very simple to just expand everything, but
1797 it would break a lot of backward compatibility. Maybe that's OK
1798 since we're just emulating a SysV function, and if we do that then
1799 why not emulate it completely (that's what SysV make does: it
1800 re-expands the entire prerequisite list, all the time, with $@
1801 etc. in scope. But, it would be a pain indeed to document this
1802 ("iff you use $$@, your prerequisite lists is expanded twice...")
1803 Ouch. Maybe better to make the code more complex. */
1805 if (have_sysv_atvar
)
1808 int tlen
= strlen (name
);
1809 char *fnp
= strrchr (name
, '/');
1817 flen
= strlen (fnp
);
1827 for (d
= this; d
!= 0; d
= d
->next
)
1828 for (p
= strchr (d
->name
, '$'); p
!= 0; p
= strchr (p
+1, '$'))
1834 /* If it's a '$@' or '$(@', it's escaped */
1836 && (p
[1] == '@' || (p
[1] == '(' && p
[2] == '@')))
1838 bcopy (p
, s
, strlen (p
)+1);
1842 /* Maybe found one. Check. p will point to '@' [for $@] or
1843 ')' [for $(@)] or 'D' [for $(@D)] or 'F' [for $(@F)]. */
1845 && (p
[0] != '(' || (++p
)[0] != '@'
1847 && (p
[1] != ')' || (p
[0] != 'D' && p
[0] != 'F')))))
1850 /* Found one. Compute the length and string ptr. Move p
1851 past the variable reference. */
1873 /* Get more space. */
1875 int soff
= s
- d
->name
;
1876 int poff
= p
- d
->name
;
1877 d
->name
= (char *) xrealloc (d
->name
,
1878 strlen (d
->name
) + atlen
+ 1);
1883 /* Copy the string over. */
1884 bcopy(p
, s
+atlen
, strlen (p
)+1);
1885 bcopy(at
, s
, atlen
);
1892 /* Single-colon. Combine these dependencies
1893 with others in file's existing record, if any. */
1894 f
= enter_file (name
);
1896 if (f
->double_colon
)
1898 _("target file `%s' has both : and :: entries"), f
->name
);
1900 /* If CMDS == F->CMDS, this target was listed in this rule
1901 more than once. Just give a warning since this is harmless. */
1902 if (cmds
!= 0 && cmds
== f
->cmds
)
1904 _("target `%s' given more than once in the same rule."),
1907 /* Check for two single-colon entries both with commands.
1908 Check is_target so that we don't lose on files such as .c.o
1909 whose commands were preinitialized. */
1910 else if (cmds
!= 0 && f
->cmds
!= 0 && f
->is_target
)
1912 error (&cmds
->fileinfo
,
1913 _("warning: overriding commands for target `%s'"),
1915 error (&f
->cmds
->fileinfo
,
1916 _("warning: ignoring old commands for target `%s'"),
1922 /* Defining .DEFAULT with no deps or cmds clears it. */
1923 if (f
== default_file
&& this == 0 && cmds
== 0)
1927 /* Defining .SUFFIXES with no dependencies
1928 clears out the list of suffixes. */
1929 if (f
== suffix_file
&& this == 0)
1934 struct dep
*nextd
= d
->next
;
1941 else if (f
->deps
!= 0)
1943 /* Add the file's old deps and the new ones in THIS together. */
1945 struct dep
*firstdeps
, *moredeps
;
1948 /* This is the rule with commands, so put its deps first.
1949 The rationale behind this is that $< expands to the
1950 first dep in the chain, and commands use $< expecting
1951 to get the dep that rule specifies. */
1957 /* Append the new deps to the old ones. */
1958 firstdeps
= f
->deps
;
1963 firstdeps
= moredeps
;
1967 while (d
->next
!= 0)
1972 f
->deps
= firstdeps
;
1977 /* If this is a static pattern rule, set the file's stem to
1978 the part of its name that matched the `%' in the pattern,
1979 so you can use $* in the commands. */
1982 static char *percent
= "%";
1983 char *buffer
= variable_expand ("");
1984 char *o
= patsubst_expand (buffer
, name
, pattern
, percent
,
1985 pattern_percent
, percent
);
1986 f
->stem
= savestring (buffer
, o
- buffer
);
1991 /* Double-colon. Make a new record
1992 even if the file already has one. */
1993 f
= lookup_file (name
);
1994 /* Check for both : and :: rules. Check is_target so
1995 we don't lose on default suffix rules or makefiles. */
1996 if (f
!= 0 && f
->is_target
&& !f
->double_colon
)
1998 _("target file `%s' has both : and :: entries"), f
->name
);
1999 f
= enter_file (name
);
2000 /* If there was an existing entry and it was a double-colon
2001 entry, enter_file will have returned a new one, making it the
2002 prev pointer of the old one, and setting its double_colon
2003 pointer to the first one. */
2004 if (f
->double_colon
== 0)
2005 /* This is the first entry for this name, so we must
2006 set its double_colon pointer to itself. */
2007 f
->double_colon
= f
;
2013 /* Free name if not needed further. */
2014 if (f
!= 0 && name
!= f
->name
2015 && (name
< f
->name
|| name
> f
->name
+ strlen (f
->name
)))
2021 /* See if this is first target seen whose name does
2022 not start with a `.', unless it contains a slash. */
2023 if (default_goal_file
== 0 && set_default
2024 && (*name
!= '.' || strchr (name
, '/') != 0
2025 #if defined(__MSDOS__) || defined(WINDOWS32)
2026 || strchr (name
, '\\') != 0
2032 /* If this file is a suffix, don't
2033 let it be the default goal file. */
2035 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
2037 register struct dep
*d2
;
2038 if (*dep_name (d
) != '.' && streq (name
, dep_name (d
)))
2043 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
2045 register unsigned int len
= strlen (dep_name (d2
));
2046 if (!strneq (name
, dep_name (d2
), len
))
2048 if (streq (name
+ len
, dep_name (d
)))
2059 default_goal_file
= f
;
2065 targets
[target_idx
] = 0;
2066 target_percents
[target_idx
] = 0;
2067 create_pattern_rule (targets
, target_percents
, two_colon
, deps
, cmds
, 1);
2068 free ((char *) target_percents
);
2072 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2073 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2074 Quoting backslashes are removed from STRING by compacting it into
2075 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2076 one, or nil if there are none. */
2079 find_char_unquote (string
, stopchars
, blank
)
2084 unsigned int string_len
= 0;
2085 register char *p
= string
;
2089 while (*p
!= '\0' && strchr (stopchars
, *p
) == 0
2090 && (!blank
|| !isblank ((unsigned char)*p
)))
2095 if (p
> string
&& p
[-1] == '\\')
2097 /* Search for more backslashes. */
2098 register int i
= -2;
2099 while (&p
[i
] >= string
&& p
[i
] == '\\')
2102 /* Only compute the length if really needed. */
2103 if (string_len
== 0)
2104 string_len
= strlen (string
);
2105 /* The number of backslashes is now -I.
2106 Copy P over itself to swallow half of them. */
2107 bcopy (&p
[i
/ 2], &p
[i
], (string_len
- (p
- string
)) - (i
/ 2) + 1);
2110 /* All the backslashes quoted each other; the STOPCHAR was
2114 /* The STOPCHAR was quoted by a backslash. Look for another. */
2117 /* No backslash in sight. */
2121 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2125 /* Search PATTERN for an unquoted %. */
2128 find_percent (pattern
)
2131 return find_char_unquote (pattern
, "%", 0);
2134 /* Parse a string into a sequence of filenames represented as a
2135 chain of struct nameseq's in reverse order and return that chain.
2137 The string is passed as STRINGP, the address of a string pointer.
2138 The string pointer is updated to point at the first character
2139 not parsed, which either is a null char or equals STOPCHAR.
2141 SIZE is how big to construct chain elements.
2142 This is useful if we want them actually to be other structures
2143 that have room for additional info.
2145 If STRIP is nonzero, strip `./'s off the beginning. */
2148 parse_file_seq (stringp
, stopchar
, size
, strip
)
2154 register struct nameseq
*new = 0;
2155 register struct nameseq
*new1
, *lastnew1
;
2156 register char *p
= *stringp
;
2163 stopchars
[1] = stopchar
;
2164 stopchars
[2] = '\0';
2166 stopchars
[0] = stopchar
;
2167 stopchars
[1] = '\0';
2172 /* Skip whitespace; see if any more names are left. */
2179 /* Yes, find end of next name. */
2181 p
= find_char_unquote (q
, stopchars
, 1);
2183 /* convert comma separated list to space separated */
2188 if (stopchar
== ':' && p
&& *p
== ':'
2189 && !(isspace ((unsigned char)p
[1]) || !p
[1]
2190 || isspace ((unsigned char)p
[-1])))
2192 p
= find_char_unquote (p
+1, stopchars
, 1);
2195 #if defined(WINDOWS32) || defined(__MSDOS__)
2196 /* For WINDOWS32, skip a "C:\..." or a "C:/..." until we find the
2197 first colon which isn't followed by a slash or a backslash.
2198 Note that tokens separated by spaces should be treated as separate
2199 tokens since make doesn't allow path names with spaces */
2200 if (stopchar
== ':')
2201 while (p
!= 0 && !isspace ((unsigned char)*p
) &&
2202 (p
[1] == '\\' || p
[1] == '/') && isalpha ((unsigned char)p
[-1]))
2203 p
= find_char_unquote (p
+ 1, stopchars
, 1);
2210 /* Skip leading `[]'s. */
2211 while (p
- q
> 2 && q
[0] == '[' && q
[1] == ']')
2213 /* Skip leading `./'s. */
2214 while (p
- q
> 2 && q
[0] == '.' && q
[1] == '/')
2217 q
+= 2; /* Skip "./". */
2218 while (q
< p
&& *q
== '/')
2219 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2223 /* Extract the filename just found, and skip it. */
2226 /* ".///" was stripped to "". */
2231 name
= savestring ("", 0);
2233 name
= savestring ("./", 2);
2238 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2239 * to remove this '\' before we can use the filename.
2240 * Savestring called because q may be read-only string constant.
2243 char *qbase
= xstrdup (q
);
2244 char *pbase
= qbase
+ (p
-q
);
2251 if (*q1
== '\\' && *(q1
+1) == ':')
2258 name
= savestring (qbase
, p1
- qbase
);
2262 name
= savestring (q
, p
- q
);
2265 /* Add it to the front of the chain. */
2266 new1
= (struct nameseq
*) xmalloc (size
);
2274 /* Look for multi-word archive references.
2275 They are indicated by a elt ending with an unmatched `)' and
2276 an elt further down the chain (i.e., previous in the file list)
2277 with an unmatched `(' (e.g., "lib(mem"). */
2282 if (new1
->name
[0] != '(' /* Don't catch "(%)" and suchlike. */
2283 && new1
->name
[strlen (new1
->name
) - 1] == ')'
2284 && strchr (new1
->name
, '(') == 0)
2286 /* NEW1 ends with a `)' but does not contain a `('.
2287 Look back for an elt with an opening `(' but no closing `)'. */
2289 struct nameseq
*n
= new1
->next
, *lastn
= new1
;
2291 while (n
!= 0 && (paren
= strchr (n
->name
, '(')) == 0)
2297 /* Ignore something starting with `(', as that cannot actually
2298 be an archive-member reference (and treating it as such
2299 results in an empty file name, which causes much lossage). */
2300 && n
->name
[0] != '(')
2302 /* N is the first element in the archive group.
2303 Its name looks like "lib(mem" (with no closing `)'). */
2307 /* Copy "lib(" into LIBNAME. */
2309 libname
= (char *) alloca (paren
- n
->name
+ 1);
2310 bcopy (n
->name
, libname
, paren
- n
->name
);
2311 libname
[paren
- n
->name
] = '\0';
2315 /* N was just "lib(", part of something like "lib( a b)".
2316 Edit it out of the chain and free its storage. */
2317 lastn
->next
= n
->next
;
2320 /* LASTN->next is the new stopping elt for the loop below. */
2325 /* Replace N's name with the full archive reference. */
2326 name
= concat (libname
, paren
, ")");
2331 if (new1
->name
[1] == '\0')
2333 /* NEW1 is just ")", part of something like "lib(a b )".
2334 Omit it from the chain and free its storage. */
2338 lastnew1
->next
= new1
->next
;
2342 free ((char *) lastn
);
2346 /* Replace also NEW1->name, which already has closing `)'. */
2347 name
= concat (libname
, new1
->name
, "");
2353 /* Trace back from NEW1 (the end of the list) until N
2354 (the beginning of the list), rewriting each name
2355 with the full archive reference. */
2359 name
= concat (libname
, new1
->name
, ")");
2368 /* No frobnication happening. Just step down the list. */
2385 /* Find the next line of text in an eval buffer, combining continuation lines
2387 Return the number of actual lines read (> 1 if continuation lines).
2388 Returns -1 if there's nothing left in the buffer.
2390 After this function, ebuf->buffer points to the first character of the
2394 /* Read a line of text from a STRING.
2395 Since we aren't really reading from a file, don't bother with linenumbers.
2398 static unsigned long
2400 struct ebuffer
*ebuf
;
2404 /* If there is nothing left in this buffer, return 0. */
2405 if (ebuf
->bufnext
> ebuf
->bufstart
+ ebuf
->size
)
2408 /* Set up a new starting point for the buffer, and find the end of the
2409 next logical line (taking into account backslash/newline pairs). */
2411 p
= ebuf
->buffer
= ebuf
->bufnext
;
2417 /* Find the next newline. Keep track of backslashes as we look. */
2418 for (; *p
!= '\n' && *p
!= '\0'; ++p
)
2420 backslash
= !backslash
;
2422 /* If we got to the end of the string or a newline with no backslash,
2424 if (*p
== '\0' || !backslash
)
2428 /* Overwrite the newline char. */
2430 ebuf
->bufnext
= p
+1;
2437 struct ebuffer
*ebuf
;
2439 char *buffer
= ebuf
->buffer
;
2440 char *p
= ebuf
->buffer
;
2441 char *end
= p
+ ebuf
->size
;
2444 /* The behaviors between string and stream buffers are different enough to
2445 warrant different functions. Do the Right Thing. */
2448 return readstring (ebuf
);
2452 while (fgets (p
, end
- p
, ebuf
->fp
) != 0)
2461 /* This only happens when the first thing on the line is a '\0'.
2462 It is a pretty hopeless case, but (wonder of wonders) Athena
2463 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2464 There is nothing really to be done; we synthesize a newline so
2465 the following line doesn't appear to be part of this line. */
2467 _("warning: NUL character seen; rest of line ignored"));
2472 /* Jump past the text we just read. */
2475 /* If the last char isn't a newline, the whole line didn't fit into the
2476 buffer. Get some more buffer and try again. */
2479 unsigned long p_off
= p
- buffer
;
2481 buffer
= (char *) xrealloc (buffer
, ebuf
->size
);
2483 end
= buffer
+ ebuf
->size
;
2484 ebuf
->buffer
= buffer
;
2489 /* We got a newline, so add one to the count of lines. */
2492 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2493 /* Check to see if the line was really ended with CRLF; if so ignore
2495 if ((p
- buffer
) > 1 && p
[-2] == '\r')
2503 for (p2
= p
- 2; p2
>= buffer
; --p2
)
2507 backslash
= !backslash
;
2518 /* Enlarge the buffer. */
2519 unsigned int p_off
= p
- buffer
;
2521 buffer
= (char *) xrealloc (buffer
, ebuf
->size
);
2523 end
= buffer
+ ebuf
->size
;
2524 ebuf
->buffer
= buffer
;
2528 if (ferror (ebuf
->fp
))
2529 pfatal_with_name (ebuf
->floc
.filenm
);
2531 return nlines
? nlines
: -1;
2534 /* Parse the next "makefile word" from the input buffer, and return info
2537 A "makefile word" is one of:
2539 w_bogus Should never happen
2541 w_static A static word; cannot be expanded
2542 w_variable A word containing one or more variables/functions
2544 w_dcolon A double-colon
2545 w_semicolon A semicolon
2546 w_comment A comment character
2547 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2549 Note that this function is only used when reading certain parts of the
2550 makefile. Don't use it where special rules hold sway (RHS of a variable,
2551 in a command list, etc.) */
2553 static enum make_word_type
2554 get_next_mword (buffer
, delim
, startp
, length
)
2558 unsigned int *length
;
2560 enum make_word_type wtype
= w_bogus
;
2561 char *p
= buffer
, *beg
;
2564 /* Skip any leading whitespace. */
2565 while (isblank ((unsigned char)*p
))
2581 wtype
= w_semicolon
;
2585 wtype
= w_varassign
;
2599 wtype
= w_varassign
;
2609 wtype
= w_varassign
;
2614 if (delim
&& strchr (delim
, c
))
2619 /* Did we find something? If so, return now. */
2620 if (wtype
!= w_bogus
)
2623 /* This is some non-operator word. A word consists of the longest
2624 string of characters that doesn't contain whitespace, one of [:=#],
2625 or [?+]=, or one of the chars in the DELIM string. */
2627 /* We start out assuming a static word; if we see a variable we'll
2628 adjust our assumptions then. */
2631 /* We already found the first value of "c", above. */
2647 #if defined(__MSDOS__) || defined(WINDOWS32)
2648 /* A word CAN include a colon in its drive spec. The drive
2649 spec is allowed either at the beginning of a word, or as part
2650 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2652 && (*p
== '/' || *p
== '\\') && isalpha ((unsigned char)p
[-2])
2653 && (p
- beg
== 2 || p
[-3] == '(')))
2662 /* This is a variable reference, so note that it's expandable.
2663 Then read it to the matching close paren. */
2671 /* This is a single-letter variable reference. */
2674 for (count
=0; *p
!= '\0'; ++p
)
2678 else if (*p
== closeparen
&& --count
< 0)
2705 if (delim
&& strchr (delim
, c
))
2723 /* Construct the list of include directories
2724 from the arguments and the default list. */
2727 construct_include_path (arg_dirs
)
2730 register unsigned int i
;
2731 #ifdef VAXC /* just don't ask ... */
2736 /* Table to hold the dirs. */
2738 register unsigned int defsize
= (sizeof (default_include_directories
)
2739 / sizeof (default_include_directories
[0]));
2740 register unsigned int max
= 5;
2741 register char **dirs
= (char **) xmalloc ((5 + defsize
) * sizeof (char *));
2742 register unsigned int idx
= 0;
2748 /* First consider any dirs specified with -I switches.
2749 Ignore dirs that don't exist. */
2752 while (*arg_dirs
!= 0)
2754 char *dir
= *arg_dirs
++;
2758 char *expanded
= tilde_expand (dir
);
2763 if (stat (dir
, &stbuf
) == 0 && S_ISDIR (stbuf
.st_mode
))
2769 xrealloc ((char *) dirs
, (max
+ defsize
) * sizeof (char *));
2773 else if (dir
!= arg_dirs
[-1])
2777 /* Now add at the end the standard default dirs. */
2781 /* The environment variable $DJDIR holds the root of the
2782 DJGPP directory tree; add ${DJDIR}/include. */
2783 struct variable
*djdir
= lookup_variable ("DJDIR", 5);
2787 char *defdir
= (char *) xmalloc (strlen (djdir
->value
) + 8 + 1);
2789 strcat (strcpy (defdir
, djdir
->value
), "/include");
2790 dirs
[idx
++] = defdir
;
2795 for (i
= 0; default_include_directories
[i
] != 0; ++i
)
2796 if (stat (default_include_directories
[i
], &stbuf
) == 0
2797 && S_ISDIR (stbuf
.st_mode
))
2798 dirs
[idx
++] = default_include_directories
[i
];
2802 /* Now compute the maximum length of any name in it. */
2805 for (i
= 0; i
< idx
; ++i
)
2807 unsigned int len
= strlen (dirs
[i
]);
2808 /* If dir name is written with a trailing slash, discard it. */
2809 if (dirs
[i
][len
- 1] == '/')
2810 /* We can't just clobber a null in because it may have come from
2811 a literal string and literal strings may not be writable. */
2812 dirs
[i
] = savestring (dirs
[i
], len
- 1);
2813 if (len
> max_incl_len
)
2817 include_directories
= dirs
;
2820 /* Expand ~ or ~USER at the beginning of NAME.
2821 Return a newly malloc'd string or 0. */
2828 if (name
[1] == '/' || name
[1] == '\0')
2830 extern char *getenv ();
2835 /* Turn off --warn-undefined-variables while we expand HOME. */
2836 int save
= warn_undefined_variables_flag
;
2837 warn_undefined_variables_flag
= 0;
2839 home_dir
= allocated_variable_expand ("$(HOME)");
2841 warn_undefined_variables_flag
= save
;
2844 is_variable
= home_dir
[0] != '\0';
2848 home_dir
= getenv ("HOME");
2850 #if !defined(_AMIGA) && !defined(WINDOWS32)
2851 if (home_dir
== 0 || home_dir
[0] == '\0')
2853 extern char *getlogin ();
2854 char *logname
= getlogin ();
2858 struct passwd
*p
= getpwnam (logname
);
2860 home_dir
= p
->pw_dir
;
2863 #endif /* !AMIGA && !WINDOWS32 */
2866 char *new = concat (home_dir
, "", name
+ 1);
2872 #if !defined(_AMIGA) && !defined(WINDOWS32)
2875 struct passwd
*pwent
;
2876 char *userend
= strchr (name
+ 1, '/');
2879 pwent
= getpwnam (name
+ 1);
2883 return xstrdup (pwent
->pw_dir
);
2885 return concat (pwent
->pw_dir
, "/", userend
+ 1);
2887 else if (userend
!= 0)
2890 #endif /* !AMIGA && !WINDOWS32 */
2895 /* Given a chain of struct nameseq's describing a sequence of filenames,
2896 in reverse of the intended order, return a new chain describing the
2897 result of globbing the filenames. The new chain is in forward order.
2898 The links of the old chain are freed or used in the new chain.
2899 Likewise for the names in the old chain.
2901 SIZE is how big to construct chain elements.
2902 This is useful if we want them actually to be other structures
2903 that have room for additional info. */
2906 multi_glob (chain
, size
)
2907 struct nameseq
*chain
;
2910 extern void dir_setup_glob ();
2911 register struct nameseq
*new = 0;
2912 register struct nameseq
*old
;
2913 struct nameseq
*nexto
;
2916 dir_setup_glob (&gl
);
2918 for (old
= chain
; old
!= 0; old
= nexto
)
2926 if (old
->name
[0] == '~')
2928 char *newname
= tilde_expand (old
->name
);
2932 old
->name
= newname
;
2937 if (ar_name (old
->name
))
2939 /* OLD->name is an archive member reference.
2940 Replace it with the archive file name,
2941 and save the member name in MEMNAME.
2942 We will glob on the archive name and then
2943 reattach MEMNAME later. */
2945 ar_parse_name (old
->name
, &arname
, &memname
);
2951 #endif /* !NO_ARCHIVES */
2953 switch (glob (old
->name
, GLOB_NOCHECK
|GLOB_ALTDIRFUNC
, NULL
, &gl
))
2955 case 0: /* Success. */
2957 register int i
= gl
.gl_pathc
;
2963 /* Try to glob on MEMNAME within the archive. */
2964 struct nameseq
*found
2965 = ar_glob (gl
.gl_pathv
[i
], memname
, size
);
2968 /* No matches. Use MEMNAME as-is. */
2969 unsigned int alen
= strlen (gl
.gl_pathv
[i
]);
2970 unsigned int mlen
= strlen (memname
);
2972 = (struct nameseq
*) xmalloc (size
);
2973 if (size
> sizeof (struct nameseq
))
2974 bzero (((char *) elt
) + sizeof (struct nameseq
),
2975 size
- sizeof (struct nameseq
));
2976 elt
->name
= (char *) xmalloc (alen
+ 1 + mlen
+ 2);
2977 bcopy (gl
.gl_pathv
[i
], elt
->name
, alen
);
2978 elt
->name
[alen
] = '(';
2979 bcopy (memname
, &elt
->name
[alen
+ 1], mlen
);
2980 elt
->name
[alen
+ 1 + mlen
] = ')';
2981 elt
->name
[alen
+ 1 + mlen
+ 1] = '\0';
2987 /* Find the end of the FOUND chain. */
2988 struct nameseq
*f
= found
;
2989 while (f
->next
!= 0)
2992 /* Attach the chain being built to the end of the FOUND
2993 chain, and make FOUND the new NEW chain. */
3001 #endif /* !NO_ARCHIVES */
3003 struct nameseq
*elt
= (struct nameseq
*) xmalloc (size
);
3004 if (size
> sizeof (struct nameseq
))
3005 bzero (((char *) elt
) + sizeof (struct nameseq
),
3006 size
- sizeof (struct nameseq
));
3007 elt
->name
= xstrdup (gl
.gl_pathv
[i
]);
3019 fatal (NILF
, _("virtual memory exhausted"));