1 /* $NetBSD: parse.c,v 1.194 2014/02/15 00:17:17 christos Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 static char rcsid[] = "$NetBSD: parse.c,v 1.194 2014/02/15 00:17:17 christos Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94";
79 __RCSID("$NetBSD: parse.c,v 1.194 2014/02/15 00:17:17 christos Exp $");
86 * Functions to parse a makefile.
88 * One function, Parse_Init, must be called before any functions
89 * in this module are used. After that, the function Parse_File is the
90 * main entry point and controls most of the other functions in this
93 * Most important structures are kept in Lsts. Directories for
94 * the .include "..." function are kept in the 'parseIncPath' Lst, while
95 * those for the .include <...> are kept in the 'sysIncPath' Lst. The
96 * targets currently being defined are kept in the 'targets' Lst.
98 * The variables 'fname' and 'lineno' are used to track the name
99 * of the current file and the line number in that file so that error
100 * messages can be more meaningful.
103 * Parse_Init Initialization function which must be
104 * called before anything else in this module
107 * Parse_End Cleanup the module
109 * Parse_File Function used to parse a makefile. It must
110 * be given the name of the file, which should
111 * already have been opened, and a function
112 * to call to read a character from the file.
114 * Parse_IsVar Returns TRUE if the given line is a
115 * variable assignment. Used by MainParseArgs
116 * to determine if an argument is a target
117 * or a variable assignment. Used internally
118 * for pretty much the same thing...
120 * Parse_Error Function called when an error occurs in
121 * parsing. Used by the variable and
122 * conditional modules.
123 * Parse_MainName Returns a Lst of the main target to create.
126 #include <sys/types.h>
127 #include <sys/stat.h>
140 #include "pathnames.h"
143 #include <sys/mman.h>
146 #define MAP_COPY MAP_PRIVATE
153 ////////////////////////////////////////////////////////////
154 // types and constants
157 * Structure for a file being read ("included file")
159 typedef struct IFile {
160 char *fname; /* name of file */
161 int lineno; /* current line number in file */
162 int first_lineno; /* line number of start of text */
163 int cond_depth; /* 'if' nesting when file opened */
164 char *P_str; /* point to base of string buffer */
165 char *P_ptr; /* point to next char of string buffer */
166 char *P_end; /* point to the end of string buffer */
167 char *(*nextbuf)(void *, size_t *); /* Function to get more data */
168 void *nextbuf_arg; /* Opaque arg for nextbuf() */
169 struct loadedfile *lf; /* loadedfile object, if any */
174 * These values are returned by ParseEOF to tell Parse_File whether to
175 * CONTINUE parsing, i.e. it had only reached the end of an include file,
182 * Tokens for target attributes
186 Default, /* .DEFAULT */
188 dotError, /* .ERROR */
189 Ignore, /* .IGNORE */
190 Includes, /* .INCLUDES */
191 Interrupt, /* .INTERRUPT */
195 MFlags, /* .MFLAGS or .MAKEFLAGS */
196 Main, /* .MAIN and we don't have anything user-specified to
198 NoExport, /* .NOEXPORT */
199 NoMeta, /* .NOMETA */
200 NoMetaCmp, /* .NOMETA_CMP */
201 NoPath, /* .NOPATH */
202 Not, /* Not special */
203 NotParallel, /* .NOTPARALLEL */
205 ExObjdir, /* .OBJDIR */
207 Parallel, /* .PARALLEL */
213 Precious, /* .PRECIOUS */
214 ExShell, /* .SHELL */
215 Silent, /* .SILENT */
216 SingleShell, /* .SINGLESHELL */
218 Suffixes, /* .SUFFIXES */
220 Attribute /* Generic attribute */
230 ////////////////////////////////////////////////////////////
234 * The main target to create. This is the first target on the first
235 * dependency line in the first makefile.
237 static GNode *mainNode;
239 ////////////////////////////////////////////////////////////
242 /* targets we're working on */
246 /* command lines for targets */
251 * specType contains the SPECial TYPE of the current target. It is
252 * Not if the target is unspecial. If it *is* special, however, the children
253 * are linked as children of the parent but not vice versa. This variable is
254 * set in ParseDoDependency
256 static ParseSpecial specType;
259 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
260 * seen, then set to each successive source on the line.
262 static GNode *predecessor;
264 ////////////////////////////////////////////////////////////
267 /* true if currently in a dependency line or its commands */
268 static Boolean inLine;
270 /* number of fatal errors */
271 static int fatals = 0;
274 * Variables for doing includes
277 /* current file being read */
278 static IFile *curFile;
280 /* stack of IFiles generated by .includes */
283 /* include paths (lists of directories) */
284 Lst parseIncPath; /* dirs for "..." includes */
285 Lst sysIncPath; /* dirs for <...> includes */
286 Lst defIncPath; /* default for sysIncPath */
288 ////////////////////////////////////////////////////////////
292 * The parseKeywords table is searched using binary search when deciding
293 * if a target or source is special. The 'spec' field is the ParseSpecial
294 * type of the keyword ("Not" if the keyword isn't special as a target) while
295 * the 'op' field is the operator to apply to the list of targets if the
296 * keyword is used as a source ("0" if the keyword isn't special as a source)
298 static const struct {
299 const char *name; /* Name of keyword */
300 ParseSpecial spec; /* Type when used as a target */
301 int op; /* Operator when used as a source */
302 } parseKeywords[] = {
303 { ".BEGIN", Begin, 0 },
304 { ".DEFAULT", Default, 0 },
306 { ".ERROR", dotError, 0 },
307 { ".EXEC", Attribute, OP_EXEC },
308 { ".IGNORE", Ignore, OP_IGNORE },
309 { ".INCLUDES", Includes, 0 },
310 { ".INTERRUPT", Interrupt, 0 },
311 { ".INVISIBLE", Attribute, OP_INVISIBLE },
312 { ".JOIN", Attribute, OP_JOIN },
313 { ".LIBS", Libs, 0 },
314 { ".LSTAT", Lstat, OP_LSTAT },
315 { ".MADE", Attribute, OP_MADE },
316 { ".MAIN", Main, 0 },
317 { ".MAKE", Attribute, OP_MAKE },
318 { ".MAKEFLAGS", MFlags, 0 },
319 { ".META", Meta, OP_META },
320 { ".MFLAGS", MFlags, 0 },
321 { ".NOMETA", NoMeta, OP_NOMETA },
322 { ".NOMETA_CMP", NoMetaCmp, OP_NOMETA_CMP },
323 { ".NOPATH", NoPath, OP_NOPATH },
324 { ".NOTMAIN", Attribute, OP_NOTMAIN },
325 { ".NOTPARALLEL", NotParallel, 0 },
326 { ".NO_PARALLEL", NotParallel, 0 },
327 { ".NULL", Null, 0 },
328 { ".OBJDIR", ExObjdir, 0 },
329 { ".OPTIONAL", Attribute, OP_OPTIONAL },
330 { ".ORDER", Order, 0 },
331 { ".PARALLEL", Parallel, 0 },
332 { ".PATH", ExPath, 0 },
333 { ".PHONY", Phony, OP_PHONY },
335 { ".POSIX", Posix, 0 },
337 { ".PRECIOUS", Precious, OP_PRECIOUS },
338 { ".RECURSIVE", Attribute, OP_MAKE },
339 { ".SHELL", ExShell, 0 },
340 { ".SILENT", Silent, OP_SILENT },
341 { ".SINGLESHELL", SingleShell, 0 },
342 { ".STALE", Stale, 0 },
343 { ".SUFFIXES", Suffixes, 0 },
344 { ".USE", Attribute, OP_USE },
345 { ".USEBEFORE", Attribute, OP_USEBEFORE },
346 { ".WAIT", Wait, 0 },
349 ////////////////////////////////////////////////////////////
352 static int ParseIsEscaped(const char *, const char *);
353 static void ParseErrorInternal(const char *, size_t, int, const char *, ...)
354 MAKE_ATTR_PRINTFLIKE(4,5);
355 static void ParseVErrorInternal(FILE *, const char *, size_t, int, const char *, va_list)
356 MAKE_ATTR_PRINTFLIKE(5, 0);
357 static int ParseFindKeyword(const char *);
358 static int ParseLinkSrc(void *, void *);
359 static int ParseDoOp(void *, void *);
360 static void ParseDoSrc(int, const char *);
361 static int ParseFindMain(void *, void *);
362 static int ParseAddDir(void *, void *);
363 static int ParseClearPath(void *, void *);
364 static void ParseDoDependency(char *);
365 static int ParseAddCmd(void *, void *);
366 static void ParseHasCommands(void *);
367 static void ParseDoInclude(char *);
368 static void ParseSetParseFile(const char *);
369 static void ParseSetIncludedFile(void);
371 static void ParseTraditionalInclude(char *);
374 static void ParseGmakeExport(char *);
376 static int ParseEOF(void);
377 static char *ParseReadLine(void);
378 static void ParseFinishLine(void);
379 static void ParseMark(GNode *);
381 ////////////////////////////////////////////////////////////
385 const char *path; /* name, for error reports */
386 char *buf; /* contents buffer */
387 size_t len; /* length of contents */
388 size_t maplen; /* length of mmap area, or 0 */
389 Boolean used; /* XXX: have we used the data yet */
393 * Constructor/destructor for loadedfile
395 static struct loadedfile *
396 loadedfile_create(const char *path)
398 struct loadedfile *lf;
400 lf = bmake_malloc(sizeof(*lf));
401 lf->path = (path == NULL ? "(stdin)" : path);
410 loadedfile_destroy(struct loadedfile *lf)
412 if (lf->buf != NULL) {
413 if (lf->maplen > 0) {
415 munmap(lf->buf, lf->maplen);
425 * nextbuf() operation for loadedfile, as needed by the weird and twisted
426 * logic below. Once that's cleaned up, we can get rid of lf->used...
429 loadedfile_nextbuf(void *x, size_t *len)
431 struct loadedfile *lf = x;
442 * Try to get the size of a file.
445 load_getsize(int fd, size_t *ret)
449 if (fstat(fd, &st) < 0) {
453 if (!S_ISREG(st.st_mode)) {
458 * st_size is an off_t, which is 64 bits signed; *ret is
459 * size_t, which might be 32 bits unsigned or 64 bits
460 * unsigned. Rather than being elaborate, just punt on
461 * files that are more than 2^31 bytes. We should never
462 * see a makefile that size in practice...
464 * While we're at it reject negative sizes too, just in case.
466 if (st.st_size < 0 || st.st_size > 0x7fffffff) {
470 *ret = (size_t) st.st_size;
477 * Until the path search logic can be moved under here instead of
478 * being in the caller in another source file, we need to have the fd
479 * passed in already open. Bleh.
481 * If the path is NULL use stdin and (to insure against fd leaks)
482 * assert that the caller passed in -1.
484 static struct loadedfile *
485 loadfile(const char *path, int fd)
487 struct loadedfile *lf;
494 lf = loadedfile_create(path);
501 fd = open(path, O_RDONLY);
504 Error("%s: %s", path, strerror(errno));
511 if (load_getsize(fd, &lf->len) == SUCCESS) {
512 /* found a size, try mmap */
513 pagesize = sysconf(_SC_PAGESIZE);
517 /* round size up to a page */
518 lf->maplen = pagesize * ((lf->len + pagesize - 1)/pagesize);
521 * XXX hack for dealing with empty files; remove when
522 * we're no longer limited by interfacing to the old
523 * logic elsewhere in this file.
525 if (lf->maplen == 0) {
526 lf->maplen = pagesize;
530 * FUTURE: remove PROT_WRITE when the parser no longer
531 * needs to scribble on the input.
533 lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE,
534 MAP_FILE|MAP_COPY, fd, 0);
535 if (lf->buf != MAP_FAILED) {
537 if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
538 char *b = malloc(lf->len + 1);
540 memcpy(b, lf->buf, lf->len++);
541 munmap(lf->buf, lf->maplen);
549 /* cannot mmap; load the traditional way */
553 lf->buf = bmake_malloc(lf->len);
557 assert(bufpos <= lf->len);
558 if (bufpos == lf->len) {
560 lf->buf = bmake_realloc(lf->buf, lf->len);
562 result = read(fd, lf->buf + bufpos, lf->len - bufpos);
564 Error("%s: read error: %s", path, strerror(errno));
572 assert(bufpos <= lf->len);
575 /* truncate malloc region to actual length (maybe not useful) */
577 lf->buf = bmake_realloc(lf->buf, lf->len);
589 ////////////////////////////////////////////////////////////
593 *----------------------------------------------------------------------
595 * Check if the current character is escaped on the current line
598 * 0 if the character is not backslash escaped, 1 otherwise
602 *----------------------------------------------------------------------
605 ParseIsEscaped(const char *line, const char *c)
618 *----------------------------------------------------------------------
619 * ParseFindKeyword --
620 * Look in the table of keywords for one matching the given string.
626 * The index of the keyword, or -1 if it isn't there.
630 *----------------------------------------------------------------------
633 ParseFindKeyword(const char *str)
639 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
642 cur = start + ((end - start) / 2);
643 diff = strcmp(str, parseKeywords[cur].name);
647 } else if (diff < 0) {
652 } while (start <= end);
657 * ParseVErrorInternal --
658 * Error message abort function for parsing. Prints out the context
659 * of the error (line number and file) as well as the message with
660 * two optional arguments.
666 * "fatals" is incremented if the level is PARSE_FATAL.
670 ParseVErrorInternal(FILE *f, const char *cfname, size_t clineno, int type,
671 const char *fmt, va_list ap)
673 static Boolean fatal_warning_error_printed = FALSE;
675 (void)fprintf(f, "%s: ", progname);
677 if (cfname != NULL) {
678 (void)fprintf(f, "\"");
679 if (*cfname != '/' && strcmp(cfname, "(stdin)") != 0) {
684 * Nothing is more annoying than not knowing
685 * which Makefile is the culprit.
687 dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &cp);
688 if (dir == NULL || *dir == '\0' ||
689 (*dir == '.' && dir[1] == '\0'))
690 dir = Var_Value(".CURDIR", VAR_GLOBAL, &cp);
694 (void)fprintf(f, "%s/%s", dir, cfname);
696 (void)fprintf(f, "%s", cfname);
698 (void)fprintf(f, "\" line %d: ", (int)clineno);
700 if (type == PARSE_WARNING)
701 (void)fprintf(f, "warning: ");
702 (void)vfprintf(f, fmt, ap);
703 (void)fprintf(f, "\n");
705 if (type == PARSE_FATAL || parseWarnFatal)
707 if (parseWarnFatal && !fatal_warning_error_printed) {
708 Error("parsing warnings being treated as errors");
709 fatal_warning_error_printed = TRUE;
714 * ParseErrorInternal --
725 ParseErrorInternal(const char *cfname, size_t clineno, int type,
726 const char *fmt, ...)
731 (void)fflush(stdout);
732 ParseVErrorInternal(stderr, cfname, clineno, type, fmt, ap);
735 if (debug_file != stderr && debug_file != stdout) {
737 ParseVErrorInternal(debug_file, cfname, clineno, type, fmt, ap);
744 * External interface to ParseErrorInternal; uses the default filename
755 Parse_Error(int type, const char *fmt, ...)
761 if (curFile == NULL) {
765 fname = curFile->fname;
766 lineno = curFile->lineno;
770 (void)fflush(stdout);
771 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
774 if (debug_file != stderr && debug_file != stdout) {
776 ParseVErrorInternal(debug_file, fname, lineno, type, fmt, ap);
784 * Parse a .info .warning or .error directive
786 * The input is the line minus the ".". We substitute
787 * variables, print the message and exit(1) (for .error) or just print
788 * a warning if the directive is malformed.
791 ParseMessage(char *line)
800 mtype = PARSE_WARNING;
806 Parse_Error(PARSE_WARNING, "invalid syntax: \".%s\"", line);
810 while (isalpha((u_char)*line))
812 if (!isspace((u_char)*line))
813 return FALSE; /* not for us */
814 while (isspace((u_char)*line))
817 line = Var_Subst(NULL, line, VAR_CMD, 0);
818 Parse_Error(mtype, "%s", line);
821 if (mtype == PARSE_FATAL) {
822 /* Terminate immediately. */
829 *---------------------------------------------------------------------
831 * Link the parent node to its new child. Used in a Lst_ForEach by
832 * ParseDoDependency. If the specType isn't 'Not', the parent
833 * isn't linked as a parent of the child.
836 * pgnp The parent node
837 * cgpn The child node
843 * New elements are added to the parents list of cgn and the
844 * children list of cgn. the unmade field of pgn is updated
845 * to reflect the additional child.
846 *---------------------------------------------------------------------
849 ParseLinkSrc(void *pgnp, void *cgnp)
851 GNode *pgn = (GNode *)pgnp;
852 GNode *cgn = (GNode *)cgnp;
854 if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts))
855 pgn = (GNode *)Lst_Datum(Lst_Last(pgn->cohorts));
856 (void)Lst_AtEnd(pgn->children, cgn);
858 (void)Lst_AtEnd(cgn->parents, pgn);
861 fprintf(debug_file, "# %s: added child %s - %s\n", __func__,
862 pgn->name, cgn->name);
863 Targ_PrintNode(pgn, 0);
864 Targ_PrintNode(cgn, 0);
870 *---------------------------------------------------------------------
872 * Apply the parsed operator to the given target node. Used in a
873 * Lst_ForEach call by ParseDoDependency once all targets have
874 * been found and their operator parsed. If the previous and new
875 * operators are incompatible, a major error is taken.
878 * gnp The node to which the operator is to be applied
879 * opp The operator to apply
885 * The type field of the node is altered to reflect any new bits in
887 *---------------------------------------------------------------------
890 ParseDoOp(void *gnp, void *opp)
892 GNode *gn = (GNode *)gnp;
893 int op = *(int *)opp;
895 * If the dependency mask of the operator and the node don't match and
896 * the node has actually had an operator applied to it before, and
897 * the operator actually has some dependency information in it, complain.
899 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
900 !OP_NOP(gn->type) && !OP_NOP(op))
902 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
906 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
908 * If the node was the object of a :: operator, we need to create a
909 * new instance of it for the children and commands on this dependency
910 * line. The new instance is placed on the 'cohorts' list of the
911 * initial one (note the initial one is not on its own cohorts list)
912 * and the new instance is linked to all parents of the initial
918 * Propagate copied bits to the initial node. They'll be propagated
919 * back to the rest of the cohorts later.
921 gn->type |= op & ~OP_OPMASK;
923 cohort = Targ_FindNode(gn->name, TARG_NOHASH);
927 * Make the cohort invisible as well to avoid duplicating it into
928 * other variables. True, parents of this target won't tend to do
929 * anything with their local variables, but better safe than
930 * sorry. (I think this is pointless now, since the relevant list
931 * traversals will no longer see this node anyway. -mycroft)
933 cohort->type = op | OP_INVISIBLE;
934 (void)Lst_AtEnd(gn->cohorts, cohort);
935 cohort->centurion = gn;
936 gn->unmade_cohorts += 1;
937 snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
941 * We don't want to nuke any previous flags (whatever they were) so we
942 * just OR the new operator into the old
951 *---------------------------------------------------------------------
953 * Given the name of a source, figure out if it is an attribute
954 * and apply it to the targets if it is. Else decide if there is
955 * some attribute which should be applied *to* the source because
956 * of some special target and apply it if so. Otherwise, make the
957 * source be a child of the targets in the list 'targets'
960 * tOp operator (if any) from special targets
961 * src name of the source to handle
967 * Operator bits may be added to the list of targets or to the source.
968 * The targets may have a new source added to their lists of children.
969 *---------------------------------------------------------------------
972 ParseDoSrc(int tOp, const char *src)
975 static int wait_number = 0;
978 if (*src == '.' && isupper ((unsigned char)src[1])) {
979 int keywd = ParseFindKeyword(src);
981 int op = parseKeywords[keywd].op;
983 Lst_ForEach(targets, ParseDoOp, &op);
986 if (parseKeywords[keywd].spec == Wait) {
988 * We add a .WAIT node in the dependency list.
989 * After any dynamic dependencies (and filename globbing)
990 * have happened, it is given a dependency on the each
991 * previous child back to and previous .WAIT node.
992 * The next child won't be scheduled until the .WAIT node
994 * We give each .WAIT node a unique name (mainly for diag).
996 snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
997 gn = Targ_FindNode(wait_src, TARG_NOHASH);
1000 gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
1001 Lst_ForEach(targets, ParseLinkSrc, gn);
1010 * If we have noted the existence of a .MAIN, it means we need
1011 * to add the sources of said target to the list of things
1012 * to create. The string 'src' is likely to be free, so we
1013 * must make a new copy of it. Note that this will only be
1014 * invoked if the user didn't specify a target on the command
1015 * line. This is to allow #ifmake's to succeed, or something...
1017 (void)Lst_AtEnd(create, bmake_strdup(src));
1019 * Add the name to the .TARGETS variable as well, so the user can
1020 * employ that, if desired.
1022 Var_Append(".TARGETS", src, VAR_GLOBAL);
1027 * Create proper predecessor/successor links between the previous
1028 * source and the current one.
1030 gn = Targ_FindNode(src, TARG_CREATE);
1033 if (predecessor != NULL) {
1034 (void)Lst_AtEnd(predecessor->order_succ, gn);
1035 (void)Lst_AtEnd(gn->order_pred, predecessor);
1037 fprintf(debug_file, "# %s: added Order dependency %s - %s\n",
1038 __func__, predecessor->name, gn->name);
1039 Targ_PrintNode(predecessor, 0);
1040 Targ_PrintNode(gn, 0);
1044 * The current source now becomes the predecessor for the next one.
1051 * If the source is not an attribute, we need to find/create
1052 * a node for it. After that we can apply any operator to it
1053 * from a special target or link it to its parents, as
1056 * In the case of a source that was the object of a :: operator,
1057 * the attribute is applied to all of its instances (as kept in
1058 * the 'cohorts' list of the node) or all the cohorts are linked
1059 * to all the targets.
1062 /* Find/create the 'src' node and attach to all targets */
1063 gn = Targ_FindNode(src, TARG_CREATE);
1069 Lst_ForEach(targets, ParseLinkSrc, gn);
1076 *-----------------------------------------------------------------------
1078 * Find a real target in the list and set it to be the main one.
1079 * Called by ParseDoDependency when a main target hasn't been found
1083 * gnp Node to examine
1086 * 0 if main not found yet, 1 if it is.
1089 * mainNode is changed and Targ_SetMain is called.
1091 *-----------------------------------------------------------------------
1094 ParseFindMain(void *gnp, void *dummy)
1096 GNode *gn = (GNode *)gnp;
1097 if ((gn->type & OP_NOTARGET) == 0) {
1100 return (dummy ? 1 : 1);
1102 return (dummy ? 0 : 0);
1107 *-----------------------------------------------------------------------
1109 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
1117 *-----------------------------------------------------------------------
1120 ParseAddDir(void *path, void *name)
1122 (void)Dir_AddDir((Lst) path, (char *)name);
1127 *-----------------------------------------------------------------------
1129 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
1137 *-----------------------------------------------------------------------
1140 ParseClearPath(void *path, void *dummy)
1142 Dir_ClearPath((Lst) path);
1143 return(dummy ? 0 : 0);
1147 *---------------------------------------------------------------------
1148 * ParseDoDependency --
1149 * Parse the dependency line in line.
1152 * line the line to parse
1158 * The nodes of the sources are linked as children to the nodes of the
1159 * targets. Some nodes may be created.
1161 * We parse a dependency line by first extracting words from the line and
1162 * finding nodes in the list of all targets with that name. This is done
1163 * until a character is encountered which is an operator character. Currently
1164 * these are only ! and :. At this point the operator is parsed and the
1165 * pointer into the line advanced until the first source is encountered.
1166 * The parsed operator is applied to each node in the 'targets' list,
1167 * which is where the nodes found for the targets are kept, by means of
1168 * the ParseDoOp function.
1169 * The sources are read in much the same way as the targets were except
1170 * that now they are expanded using the wildcarding scheme of the C-Shell
1171 * and all instances of the resulting words in the list of all targets
1172 * are found. Each of the resulting nodes is then linked to each of the
1173 * targets as one of its children.
1174 * Certain targets are handled specially. These are the ones detailed
1175 * by the specType variable.
1176 * The storing of transformation rules is also taken care of here.
1177 * A target is recognized as a transformation rule by calling
1178 * Suff_IsTransform. If it is a transformation rule, its node is gotten
1179 * from the suffix module via Suff_AddTransform rather than the standard
1180 * Targ_FindNode in the target module.
1181 *---------------------------------------------------------------------
1184 ParseDoDependency(char *line)
1186 char *cp; /* our current position */
1187 GNode *gn = NULL; /* a general purpose temporary node */
1188 int op; /* the operator on the line */
1189 char savec; /* a place to save a character */
1190 Lst paths; /* List of search paths to alter when parsing
1191 * a list of .PATH targets */
1192 int tOp; /* operator from special target */
1193 Lst sources; /* list of archive source names after
1195 Lst curTargs; /* list of target names to be found and added
1196 * to the targets list */
1197 char *lstart = line;
1200 fprintf(debug_file, "ParseDoDependency(%s)\n", line);
1206 curTargs = Lst_Init(FALSE);
1209 for (cp = line; *cp && (ParseIsEscaped(lstart, cp) ||
1210 !(isspace((unsigned char)*cp) ||
1211 *cp == '!' || *cp == ':' || *cp == LPAREN));
1215 * Must be a dynamic source (would have been expanded
1216 * otherwise), so call the Var module to parse the puppy
1217 * so we can safely advance beyond it...There should be
1218 * no errors in this, as they would have been discovered
1219 * in the initial Var_Subst and we wouldn't be here.
1224 (void)Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
1231 if (!ParseIsEscaped(lstart, cp) && *cp == LPAREN) {
1233 * Archives must be handled specially to make sure the OP_ARCHV
1234 * flag is set in their 'type' field, for one thing, and because
1235 * things like "archive(file1.o file2.o file3.o)" are permissible.
1236 * Arch_ParseArchive will set 'line' to be the first non-blank
1237 * after the archive-spec. It creates/finds nodes for the members
1238 * and places them on the given list, returning SUCCESS if all
1239 * went well and FAILURE if there was an error in the
1240 * specification. On error, line should remain untouched.
1242 if (Arch_ParseArchive(&line, targets, VAR_CMD) != SUCCESS) {
1243 Parse_Error(PARSE_FATAL,
1244 "Error in archive specification: \"%s\"", line);
1254 * Ending a dependency line without an operator is a Bozo
1255 * no-no. As a heuristic, this is also often triggered by
1256 * undetected conflicts from cvs/rcs merges.
1258 if ((strncmp(line, "<<<<<<", 6) == 0) ||
1259 (strncmp(line, "======", 6) == 0) ||
1260 (strncmp(line, ">>>>>>", 6) == 0))
1261 Parse_Error(PARSE_FATAL,
1262 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
1264 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive"
1265 : "Need an operator");
1271 * Have a word in line. See if it's a special target and set
1272 * specType to match it.
1274 if (*line == '.' && isupper ((unsigned char)line[1])) {
1276 * See if the target is a special target that must have it
1277 * or its sources handled specially.
1279 int keywd = ParseFindKeyword(line);
1281 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
1282 Parse_Error(PARSE_FATAL, "Mismatched special targets");
1286 specType = parseKeywords[keywd].spec;
1287 tOp = parseKeywords[keywd].op;
1290 * Certain special targets have special semantics:
1291 * .PATH Have to set the dirSearchPath
1293 * .MAIN Its sources are only used if
1294 * nothing has been specified to
1296 * .DEFAULT Need to create a node to hang
1297 * commands on, but we don't want
1298 * it in the graph, nor do we want
1299 * it to be the Main Target, so we
1300 * create it, set OP_NOTMAIN and
1301 * add it to the list, setting
1302 * DEFAULT to the new node for
1303 * later use. We claim the node is
1304 * A transformation rule to make
1305 * life easier later, when we'll
1306 * use Make_HandleUse to actually
1307 * apply the .DEFAULT commands.
1308 * .PHONY The list of targets
1309 * .NOPATH Don't search for file in the path
1314 * .INTERRUPT Are not to be considered the
1316 * .NOTPARALLEL Make only one target at a time.
1317 * .SINGLESHELL Create a shell for each command.
1318 * .ORDER Must set initial predecessor to NULL
1322 if (paths == NULL) {
1323 paths = Lst_Init(FALSE);
1325 (void)Lst_AtEnd(paths, dirSearchPath);
1328 if (!Lst_IsEmpty(create)) {
1337 gn = Targ_FindNode(line, TARG_CREATE);
1340 gn->type |= OP_NOTMAIN|OP_SPECIAL;
1341 (void)Lst_AtEnd(targets, gn);
1344 gn = Targ_NewGN(".DEFAULT");
1345 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
1346 (void)Lst_AtEnd(targets, gn);
1361 } else if (strncmp(line, ".PATH", 5) == 0) {
1363 * .PATH<suffix> has to be handled specially.
1364 * Call on the suffix module to give us a path to
1370 path = Suff_GetPath(&line[5]);
1372 Parse_Error(PARSE_FATAL,
1373 "Suffix '%s' not defined (yet)",
1377 if (paths == NULL) {
1378 paths = Lst_Init(FALSE);
1380 (void)Lst_AtEnd(paths, path);
1386 * Have word in line. Get or create its node and stick it at
1387 * the end of the targets list
1389 if ((specType == Not) && (*line != '\0')) {
1390 if (Dir_HasWildcards(line)) {
1392 * Targets are to be sought only in the current directory,
1393 * so create an empty path for the thing. Note we need to
1394 * use Dir_Destroy in the destruction of the path as the
1395 * Dir module could have added a directory to the path...
1397 Lst emptyPath = Lst_Init(FALSE);
1399 Dir_Expand(line, emptyPath, curTargs);
1401 Lst_Destroy(emptyPath, Dir_Destroy);
1404 * No wildcards, but we want to avoid code duplication,
1405 * so create a list with the word on it.
1407 (void)Lst_AtEnd(curTargs, line);
1410 while(!Lst_IsEmpty(curTargs)) {
1411 char *targName = (char *)Lst_DeQueue(curTargs);
1413 if (!Suff_IsTransform (targName)) {
1414 gn = Targ_FindNode(targName, TARG_CREATE);
1416 gn = Suff_AddTransform(targName);
1421 (void)Lst_AtEnd(targets, gn);
1423 } else if (specType == ExPath && *line != '.' && *line != '\0') {
1424 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
1429 * If it is a special type and not .PATH, it's the only target we
1430 * allow on this line...
1432 if (specType != Not && specType != ExPath) {
1433 Boolean warning = FALSE;
1435 while (*cp && (ParseIsEscaped(lstart, cp) ||
1436 ((*cp != '!') && (*cp != ':')))) {
1437 if (ParseIsEscaped(lstart, cp) ||
1438 (*cp != ' ' && *cp != '\t')) {
1444 Parse_Error(PARSE_WARNING, "Extra target ignored");
1447 while (*cp && isspace ((unsigned char)*cp)) {
1452 } while (*line && (ParseIsEscaped(lstart, line) ||
1453 ((*line != '!') && (*line != ':'))));
1456 * Don't need the list of target names anymore...
1458 Lst_Destroy(curTargs, NULL);
1461 if (!Lst_IsEmpty(targets)) {
1464 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
1473 * These four create nodes on which to hang commands, so
1474 * targets shouldn't be empty...
1478 * Nothing special here -- targets can be empty if it wants.
1485 * Have now parsed all the target names. Must parse the operator next. The
1486 * result is left in op .
1490 } else if (*cp == ':') {
1498 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive"
1499 : "Missing dependency operator");
1503 cp++; /* Advance beyond operator */
1505 Lst_ForEach(targets, ParseDoOp, &op);
1508 * Get to the first source
1510 while (*cp && isspace ((unsigned char)*cp)) {
1516 * Several special targets take different actions if present with no
1518 * a .SUFFIXES line with no sources clears out all old suffixes
1519 * a .PRECIOUS line makes all targets precious
1520 * a .IGNORE line ignores errors for all targets
1521 * a .SILENT line creates silence when making all targets
1522 * a .PATH removes all directories from the search path(s).
1527 Suff_ClearSuffixes();
1533 ignoreErrors = TRUE;
1539 Lst_ForEach(paths, ParseClearPath, NULL);
1544 Var_Set("%POSIX", "1003.2", VAR_GLOBAL, 0);
1550 } else if (specType == MFlags) {
1552 * Call on functions in main.c to deal with these arguments and
1553 * set the initial character to a null-character so the loop to
1554 * get sources won't get anything
1556 Main_ParseArgLine(line);
1558 } else if (specType == ExShell) {
1559 if (Job_ParseShell(line) != SUCCESS) {
1560 Parse_Error(PARSE_FATAL, "improper shell specification");
1564 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1569 * NOW GO FOR THE SOURCES
1571 if ((specType == Suffixes) || (specType == ExPath) ||
1572 (specType == Includes) || (specType == Libs) ||
1573 (specType == Null) || (specType == ExObjdir))
1577 * If the target was one that doesn't take files as its sources
1578 * but takes something like suffixes, we take each
1579 * space-separated word on the line as a something and deal
1580 * with it accordingly.
1582 * If the target was .SUFFIXES, we take each source as a
1583 * suffix and add it to the list of suffixes maintained by the
1586 * If the target was a .PATH, we add the source as a directory
1587 * to search on the search path.
1589 * If it was .INCLUDES, the source is taken to be the suffix of
1590 * files which will be #included and whose search path should
1591 * be present in the .INCLUDES variable.
1593 * If it was .LIBS, the source is taken to be the suffix of
1594 * files which are considered libraries and whose search path
1595 * should be present in the .LIBS variable.
1597 * If it was .NULL, the source is the suffix to use when a file
1598 * has no valid suffix.
1600 * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1601 * and will cause make to do a new chdir to that path.
1603 while (*cp && !isspace ((unsigned char)*cp)) {
1610 Suff_AddSuffix(line, &mainNode);
1613 Lst_ForEach(paths, ParseAddDir, line);
1616 Suff_AddInclude(line);
1625 Main_SetObjdir(line);
1631 if (savec != '\0') {
1634 while (*cp && isspace ((unsigned char)*cp)) {
1640 Lst_Destroy(paths, NULL);
1642 if (specType == ExPath)
1647 * The targets take real sources, so we must beware of archive
1648 * specifications (i.e. things with left parentheses in them)
1649 * and handle them accordingly.
1651 for (; *cp && !isspace ((unsigned char)*cp); cp++) {
1652 if ((*cp == LPAREN) && (cp > line) && (cp[-1] != '$')) {
1654 * Only stop for a left parenthesis if it isn't at the
1655 * start of a word (that'll be for variable changes
1656 * later) and isn't preceded by a dollar sign (a dynamic
1663 if (*cp == LPAREN) {
1664 sources = Lst_Init(FALSE);
1665 if (Arch_ParseArchive(&line, sources, VAR_CMD) != SUCCESS) {
1666 Parse_Error(PARSE_FATAL,
1667 "Error in source archive spec \"%s\"", line);
1671 while (!Lst_IsEmpty (sources)) {
1672 gn = (GNode *)Lst_DeQueue(sources);
1673 ParseDoSrc(tOp, gn->name);
1675 Lst_Destroy(sources, NULL);
1683 ParseDoSrc(tOp, line);
1685 while (*cp && isspace ((unsigned char)*cp)) {
1692 if (mainNode == NULL) {
1694 * If we have yet to decide on a main target to make, in the
1695 * absence of any user input, we want the first target on
1696 * the first dependency line that is actually a real target
1697 * (i.e. isn't a .USE or .EXEC rule) to be made.
1699 Lst_ForEach(targets, ParseFindMain, NULL);
1704 Lst_Destroy(curTargs, NULL);
1708 *---------------------------------------------------------------------
1710 * Return TRUE if the passed line is a variable assignment. A variable
1711 * assignment consists of a single word followed by optional whitespace
1712 * followed by either a += or an = operator.
1713 * This function is used both by the Parse_File function and main when
1714 * parsing the command-line arguments.
1717 * line the line to check
1720 * TRUE if it is. FALSE if it ain't
1724 *---------------------------------------------------------------------
1727 Parse_IsVar(char *line)
1729 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1732 #define ISEQOPERATOR(c) \
1733 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1736 * Skip to variable name
1738 for (;(*line == ' ') || (*line == '\t'); line++)
1741 /* Scan for one of the assignment operators outside a variable expansion */
1742 while ((ch = *line++) != 0) {
1743 if (ch == '(' || ch == '{') {
1747 if (ch == ')' || ch == '}') {
1753 while (ch == ' ' || ch == '\t') {
1758 if (ch == ':' && strncmp(line, "sh", 2) == 0) {
1765 if (*line == '=' && ISEQOPERATOR(ch))
1775 *---------------------------------------------------------------------
1777 * Take the variable assignment in the passed line and do it in the
1780 * Note: There is a lexical ambiguity with assignment modifier characters
1781 * in variable names. This routine interprets the character before the =
1782 * as a modifier. Therefore, an assignment like
1784 * is interpreted as "C+ +=" instead of "C++ =".
1787 * line a line guaranteed to be a variable assignment.
1788 * This reduces error checks
1789 * ctxt Context in which to do the assignment
1795 * the variable structure of the given variable name is altered in the
1797 *---------------------------------------------------------------------
1800 Parse_DoVar(char *line, GNode *ctxt)
1802 char *cp; /* pointer into line */
1804 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1805 } type; /* Type of assignment */
1806 char *opc; /* ptr to operator character to
1807 * null-terminate the variable name */
1808 Boolean freeCp = FALSE; /* TRUE if cp needs to be freed,
1809 * i.e. if any variable expansion was
1814 * Skip to variable name
1816 while ((*line == ' ') || (*line == '\t')) {
1821 * Skip to operator character, nulling out whitespace as we go
1822 * XXX Rather than counting () and {} we should look for $ and
1823 * then expand the variable.
1825 for (depth = 0, cp = line + 1; depth != 0 || *cp != '='; cp++) {
1826 if (*cp == '(' || *cp == '{') {
1830 if (*cp == ')' || *cp == '}') {
1834 if (depth == 0 && isspace ((unsigned char)*cp)) {
1838 opc = cp-1; /* operator is the previous character */
1839 *cp++ = '\0'; /* nuke the = */
1842 * Check operator type
1852 * If the variable already has a value, we don't do anything.
1855 if (Var_Exists(line, ctxt)) {
1874 while (opc > line && *opc != ':')
1877 if (strncmp(opc, ":sh", 3) == 0) {
1887 while (isspace ((unsigned char)*cp)) {
1891 if (type == VAR_APPEND) {
1892 Var_Append(line, cp, ctxt);
1893 } else if (type == VAR_SUBST) {
1895 * Allow variables in the old value to be undefined, but leave their
1896 * invocation alone -- this is done by forcing oldVars to be false.
1897 * XXX: This can cause recursive variables, but that's not hard to do,
1898 * and this allows someone to do something like
1900 * CFLAGS = $(.INCLUDES)
1901 * CFLAGS := -I.. $(CFLAGS)
1903 * And not get an error.
1905 Boolean oldOldVars = oldVars;
1910 * make sure that we set the variable the first time to nothing
1911 * so that it gets substituted!
1913 if (!Var_Exists(line, ctxt))
1914 Var_Set(line, "", ctxt, 0);
1916 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1917 oldVars = oldOldVars;
1920 Var_Set(line, cp, ctxt, 0);
1921 } else if (type == VAR_SHELL) {
1925 if (strchr(cp, '$') != NULL) {
1927 * There's a dollar sign in the command, so perform variable
1928 * expansion on the whole thing. The resulting string will need
1929 * freeing when we're done, so set freeCmd to TRUE.
1931 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1935 res = Cmd_Exec(cp, &error);
1936 Var_Set(line, res, ctxt, 0);
1940 Parse_Error(PARSE_WARNING, error, cp);
1943 * Normal assignment -- just do it.
1945 Var_Set(line, cp, ctxt, 0);
1947 if (strcmp(line, MAKEOVERRIDES) == 0)
1948 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */
1949 else if (strcmp(line, ".CURDIR") == 0) {
1951 * Somone is being (too?) clever...
1952 * Let's pretend they know what they are doing and
1953 * re-initialize the 'cur' Path.
1957 } else if (strcmp(line, MAKE_JOB_PREFIX) == 0) {
1959 } else if (strcmp(line, MAKE_EXPORTED) == 0) {
1969 * Lst_ForEach function to add a command line to all targets
1972 * gnp the node to which the command is to be added
1973 * cmd the command to add
1979 * A new element is added to the commands list of the node.
1982 ParseAddCmd(void *gnp, void *cmd)
1984 GNode *gn = (GNode *)gnp;
1986 /* Add to last (ie current) cohort for :: targets */
1987 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
1988 gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts));
1990 /* if target already supplied, ignore commands */
1991 if (!(gn->type & OP_HAS_COMMANDS)) {
1992 (void)Lst_AtEnd(gn->commands, cmd);
1996 /* XXX: We cannot do this until we fix the tree */
1997 (void)Lst_AtEnd(gn->commands, cmd);
1998 Parse_Error(PARSE_WARNING,
1999 "overriding commands for target \"%s\"; "
2000 "previous commands defined at %s: %d ignored",
2001 gn->name, gn->fname, gn->lineno);
2003 Parse_Error(PARSE_WARNING,
2004 "duplicate script for target \"%s\" ignored",
2006 ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING,
2007 "using previous script for \"%s\" defined here",
2015 *-----------------------------------------------------------------------
2016 * ParseHasCommands --
2017 * Callback procedure for Parse_File when destroying the list of
2018 * targets on the last dependency line. Marks a target as already
2019 * having commands if it does, to keep from having shell commands
2020 * on multiple dependency lines.
2023 * gnp Node to examine
2029 * OP_HAS_COMMANDS may be set for the target.
2031 *-----------------------------------------------------------------------
2034 ParseHasCommands(void *gnp)
2036 GNode *gn = (GNode *)gnp;
2037 if (!Lst_IsEmpty(gn->commands)) {
2038 gn->type |= OP_HAS_COMMANDS;
2043 *-----------------------------------------------------------------------
2044 * Parse_AddIncludeDir --
2045 * Add a directory to the path searched for included makefiles
2046 * bracketed by double-quotes. Used by functions in main.c
2049 * dir The name of the directory to add
2055 * The directory is appended to the list.
2057 *-----------------------------------------------------------------------
2060 Parse_AddIncludeDir(char *dir)
2062 (void)Dir_AddDir(parseIncPath, dir);
2066 *---------------------------------------------------------------------
2068 * Push to another file.
2070 * The input is the line minus the `.'. A file spec is a string
2071 * enclosed in <> or "". The former is looked for only in sysIncPath.
2072 * The latter in . and the directories specified by -I command line
2079 * A structure is added to the includes Lst and readProc, lineno,
2080 * fname and curFILE are altered for the new file
2081 *---------------------------------------------------------------------
2085 Parse_include_file(char *file, Boolean isSystem, int silent)
2087 struct loadedfile *lf;
2088 char *fullname; /* full pathname of file */
2090 char *prefEnd, *incdir;
2095 * Now we know the file's name and its search path, we attempt to
2096 * find the durn thing. A return of NULL indicates the file don't
2099 fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
2101 if (fullname == NULL && !isSystem) {
2103 * Include files contained in double-quotes are first searched for
2104 * relative to the including file's location. We don't want to
2105 * cd there, of course, so we just tack on the old file's
2106 * leading path components and call Dir_FindFile to see if
2107 * we can locate the beast.
2110 incdir = bmake_strdup(curFile->fname);
2111 prefEnd = strrchr(incdir, '/');
2112 if (prefEnd != NULL) {
2114 /* Now do lexical processing of leading "../" on the filename */
2115 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
2116 prefEnd = strrchr(incdir + 1, '/');
2117 if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0)
2121 newName = str_concat(incdir, file + i, STR_ADDSLASH);
2122 fullname = Dir_FindFile(newName, parseIncPath);
2123 if (fullname == NULL)
2124 fullname = Dir_FindFile(newName, dirSearchPath);
2129 if (fullname == NULL) {
2131 * Makefile wasn't found in same directory as included makefile.
2132 * Search for it first on the -I search path,
2133 * then on the .PATH search path, if not found in a -I directory.
2134 * If we have a suffix specific path we should use that.
2137 Lst suffPath = NULL;
2139 if ((suff = strrchr(file, '.'))) {
2140 suffPath = Suff_GetPath(suff);
2141 if (suffPath != NULL) {
2142 fullname = Dir_FindFile(file, suffPath);
2145 if (fullname == NULL) {
2146 fullname = Dir_FindFile(file, parseIncPath);
2147 if (fullname == NULL) {
2148 fullname = Dir_FindFile(file, dirSearchPath);
2154 /* Looking for a system file or file still not found */
2155 if (fullname == NULL) {
2157 * Look for it on the system path
2159 fullname = Dir_FindFile(file,
2160 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
2163 if (fullname == NULL) {
2165 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2169 /* Actually open the file... */
2170 fd = open(fullname, O_RDONLY);
2173 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
2179 lf = loadfile(fullname, fd);
2181 ParseSetIncludedFile();
2182 /* Start reading from this file next */
2183 Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf);
2188 ParseDoInclude(char *line)
2190 char endc; /* the character which ends the file spec */
2191 char *cp; /* current position in file spec */
2192 int silent = (*line != 'i') ? 1 : 0;
2193 char *file = &line[7 + silent];
2195 /* Skip to delimiter character so we know where to look */
2196 while (*file == ' ' || *file == '\t')
2199 if (*file != '"' && *file != '<') {
2200 Parse_Error(PARSE_FATAL,
2201 ".include filename must be delimited by '\"' or '<'");
2206 * Set the search path on which to find the include file based on the
2207 * characters which bracket its name. Angle-brackets imply it's
2208 * a system Makefile while double-quotes imply it's a user makefile
2216 /* Skip to matching delimiter */
2217 for (cp = ++file; *cp && *cp != endc; cp++)
2221 Parse_Error(PARSE_FATAL,
2222 "Unclosed %cinclude filename. '%c' expected",
2229 * Substitute for any variables in the file name before trying to
2232 file = Var_Subst(NULL, file, VAR_CMD, FALSE);
2234 Parse_include_file(file, endc == '>', silent);
2240 *---------------------------------------------------------------------
2241 * ParseSetIncludedFile --
2242 * Set the .INCLUDEDFROMFILE variable to the contents of .PARSEFILE
2243 * and the .INCLUDEDFROMDIR variable to the contents of .PARSEDIR
2249 * The .INCLUDEDFROMFILE variable is overwritten by the contents
2250 * of .PARSEFILE and the .INCLUDEDFROMDIR variable is overwriten
2251 * by the contents of .PARSEDIR
2252 *---------------------------------------------------------------------
2255 ParseSetIncludedFile(void)
2257 char *pf, *fp = NULL;
2258 char *pd, *dp = NULL;
2260 pf = Var_Value(".PARSEFILE", VAR_GLOBAL, &fp);
2261 Var_Set(".INCLUDEDFROMFILE", pf, VAR_GLOBAL, 0);
2262 pd = Var_Value(".PARSEDIR", VAR_GLOBAL, &dp);
2263 Var_Set(".INCLUDEDFROMDIR", pd, VAR_GLOBAL, 0);
2266 fprintf(debug_file, "%s: ${.INCLUDEDFROMDIR} = `%s' "
2267 "${.INCLUDEDFROMFILE} = `%s'\n", __func__, pd, pf);
2275 *---------------------------------------------------------------------
2276 * ParseSetParseFile --
2277 * Set the .PARSEDIR and .PARSEFILE variables to the dirname and
2278 * basename of the given filename
2284 * The .PARSEDIR and .PARSEFILE variables are overwritten by the
2285 * dirname and basename of the given filename.
2286 *---------------------------------------------------------------------
2289 ParseSetParseFile(const char *filename)
2291 char *slash, *dirname;
2292 const char *pd, *pf;
2295 slash = strrchr(filename, '/');
2296 if (slash == NULL) {
2297 Var_Set(".PARSEDIR", pd = curdir, VAR_GLOBAL, 0);
2298 Var_Set(".PARSEFILE", pf = filename, VAR_GLOBAL, 0);
2301 len = slash - filename;
2302 dirname = bmake_malloc(len + 1);
2303 memcpy(dirname, filename, len);
2304 dirname[len] = '\0';
2305 Var_Set(".PARSEDIR", pd = dirname, VAR_GLOBAL, 0);
2306 Var_Set(".PARSEFILE", pf = slash + 1, VAR_GLOBAL, 0);
2309 fprintf(debug_file, "%s: ${.PARSEDIR} = `%s' ${.PARSEFILE} = `%s'\n",
2315 * Track the makefiles we read - so makefiles can
2316 * set dependencies on them.
2317 * Avoid adding anything more than once.
2321 ParseTrackInput(const char *name)
2325 size_t name_len = strlen(name);
2327 old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp);
2329 /* does it contain name? */
2330 for (; old != NULL; old = strchr(old, ' ')) {
2333 if (memcmp(old, name, name_len) == 0
2334 && (old[name_len] == 0 || old[name_len] == ' '))
2338 Var_Append (MAKE_MAKEFILES, name, VAR_GLOBAL);
2347 *---------------------------------------------------------------------
2349 * Start Parsing from the given source
2355 * A structure is added to the includes Lst and readProc, lineno,
2356 * fname and curFile are altered for the new file
2357 *---------------------------------------------------------------------
2360 Parse_SetInput(const char *name, int line, int fd,
2361 char *(*nextbuf)(void *, size_t *), void *arg)
2367 name = curFile->fname;
2369 ParseTrackInput(name);
2372 fprintf(debug_file, "%s: file %s, line %d, fd %d, nextbuf %p, arg %p\n",
2373 __func__, name, line, fd, nextbuf, arg);
2375 if (fd == -1 && nextbuf == NULL)
2379 if (curFile != NULL)
2380 /* Save exiting file info */
2381 Lst_AtFront(includes, curFile);
2383 /* Allocate and fill in new structure */
2384 curFile = bmake_malloc(sizeof *curFile);
2387 * Once the previous state has been saved, we can get down to reading
2388 * the new file. We set up the name of the file to be the absolute
2389 * name of the include file so error messages refer to the right
2392 curFile->fname = bmake_strdup(name);
2393 curFile->lineno = line;
2394 curFile->first_lineno = line;
2395 curFile->nextbuf = nextbuf;
2396 curFile->nextbuf_arg = arg;
2399 assert(nextbuf != NULL);
2401 /* Get first block of input data */
2402 buf = curFile->nextbuf(curFile->nextbuf_arg, &len);
2404 /* Was all a waste of time ... */
2406 free(curFile->fname);
2410 curFile->P_str = buf;
2411 curFile->P_ptr = buf;
2412 curFile->P_end = buf+len;
2414 curFile->cond_depth = Cond_save_depth();
2415 ParseSetParseFile(name);
2420 *---------------------------------------------------------------------
2421 * ParseTraditionalInclude --
2422 * Push to another file.
2424 * The input is the current line. The file name(s) are
2425 * following the "include".
2431 * A structure is added to the includes Lst and readProc, lineno,
2432 * fname and curFILE are altered for the new file
2433 *---------------------------------------------------------------------
2436 ParseTraditionalInclude(char *line)
2438 char *cp; /* current position in file spec */
2440 int silent = (line[0] != 'i') ? 1 : 0;
2441 char *file = &line[silent + 7];
2445 fprintf(debug_file, "%s: %s\n", __func__, file);
2449 * Skip over whitespace
2451 while (isspace((unsigned char)*file))
2455 * Substitute for any variables in the file name before trying to
2458 all_files = Var_Subst(NULL, file, VAR_CMD, FALSE);
2460 if (*file == '\0') {
2461 Parse_Error(PARSE_FATAL,
2462 "Filename missing from \"include\"");
2466 for (file = all_files; !done; file = cp + 1) {
2467 /* Skip to end of line or next whitespace */
2468 for (cp = file; *cp && !isspace((unsigned char) *cp); cp++)
2476 Parse_include_file(file, FALSE, silent);
2484 *---------------------------------------------------------------------
2485 * ParseGmakeExport --
2486 * Parse export <variable>=<value>
2488 * And set the environment with it.
2495 *---------------------------------------------------------------------
2498 ParseGmakeExport(char *line)
2500 char *variable = &line[6];
2504 fprintf(debug_file, "%s: %s\n", __func__, variable);
2508 * Skip over whitespace
2510 while (isspace((unsigned char)*variable))
2513 for (value = variable; *value && *value != '='; value++)
2516 if (*value != '=') {
2517 Parse_Error(PARSE_FATAL,
2518 "Variable/Value missing from \"export\"");
2521 *value++ = '\0'; /* terminate variable */
2524 * Expand the value before putting it in the environment.
2526 value = Var_Subst(NULL, value, VAR_CMD, FALSE);
2527 setenv(variable, value, 1);
2532 *---------------------------------------------------------------------
2534 * Called when EOF is reached in the current file. If we were reading
2535 * an include file, the includes stack is popped and things set up
2536 * to go back to reading the previous file at the previous location.
2539 * CONTINUE if there's more to do. DONE if not.
2542 * The old curFILE, is closed. The includes list is shortened.
2543 * lineno, curFILE, and fname are changed if CONTINUE is returned.
2544 *---------------------------------------------------------------------
2552 assert(curFile->nextbuf != NULL);
2554 /* get next input buffer, if any */
2555 ptr = curFile->nextbuf(curFile->nextbuf_arg, &len);
2556 curFile->P_ptr = ptr;
2557 curFile->P_str = ptr;
2558 curFile->P_end = ptr + len;
2559 curFile->lineno = curFile->first_lineno;
2565 /* Ensure the makefile (or loop) didn't have mismatched conditionals */
2566 Cond_restore_depth(curFile->cond_depth);
2568 if (curFile->lf != NULL) {
2569 loadedfile_destroy(curFile->lf);
2573 /* Dispose of curFile info */
2574 /* Leak curFile->fname because all the gnodes have pointers to it */
2575 free(curFile->P_str);
2578 curFile = Lst_DeQueue(includes);
2580 if (curFile == NULL) {
2581 /* We've run out of input */
2582 Var_Delete(".PARSEDIR", VAR_GLOBAL);
2583 Var_Delete(".PARSEFILE", VAR_GLOBAL);
2584 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
2585 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
2590 fprintf(debug_file, "ParseEOF: returning to file %s, line %d\n",
2591 curFile->fname, curFile->lineno);
2593 /* Restore the PARSEDIR/PARSEFILE variables */
2594 ParseSetParseFile(curFile->fname);
2599 #define PARSE_SKIP 2
2602 ParseGetLine(int flags, int *length)
2604 IFile *cf = curFile;
2613 /* Loop through blank lines and comment lines */
2622 if (cf->P_end != NULL && ptr == cf->P_end) {
2628 if (ch == 0 || (ch == '\\' && ptr[1] == 0)) {
2629 if (cf->P_end == NULL)
2630 /* End of string (aka for loop) data */
2632 /* see if there is more we can parse */
2633 while (ptr++ < cf->P_end) {
2634 if ((ch = *ptr) == '\n') {
2635 if (ptr > line && ptr[-1] == '\\')
2637 Parse_Error(PARSE_WARNING,
2638 "Zero byte read from file, skipping rest of line.");
2642 if (cf->nextbuf != NULL) {
2644 * End of this buffer; return EOF and outer logic
2645 * will get the next one. (eww)
2649 Parse_Error(PARSE_FATAL, "Zero byte read from file");
2654 /* Don't treat next character as special, remember first one */
2655 if (escaped == NULL)
2663 if (ch == '#' && comment == NULL) {
2664 /* Remember first '#' for comment stripping */
2665 /* Unless previous char was '[', as in modifier :[#] */
2666 if (!(ptr > line && ptr[-1] == '['))
2672 if (!isspace((unsigned char)ch))
2673 /* We are not interested in trailing whitespace */
2677 /* Save next 'to be processed' location */
2680 /* Check we have a non-comment, non-blank line */
2681 if (line_end == line || comment == line) {
2683 /* At end of file */
2685 /* Parse another line */
2689 /* We now have a line of data */
2692 if (flags & PARSE_RAW) {
2693 /* Leave '\' (etc) in line buffer (eg 'for' lines) */
2694 *length = line_end - line;
2698 if (flags & PARSE_SKIP) {
2699 /* Completely ignore non-directives */
2702 /* We could do more of the .else/.elif/.endif checks here */
2707 /* Brutally ignore anything after a non-escaped '#' in non-commands */
2708 if (comment != NULL && line[0] != '\t') {
2713 /* If we didn't see a '\\' then the in-situ data is fine */
2714 if (escaped == NULL) {
2715 *length = line_end - line;
2719 /* Remove escapes from '\n' and '#' */
2722 for (; ; *tp++ = ch) {
2732 /* Delete '\\' at end of buffer */
2737 if (ch == '#' && line[0] != '\t')
2738 /* Delete '\\' from before '#' on non-command lines */
2742 /* Leave '\\' in buffer for later */
2744 /* Make sure we don't delete an escaped ' ' from the line end */
2749 /* Escaped '\n' replace following whitespace with a single ' ' */
2750 while (ptr[0] == ' ' || ptr[0] == '\t')
2755 /* Delete any trailing spaces - eg from empty continuations */
2756 while (tp > escaped && isspace((unsigned char)tp[-1]))
2760 *length = tp - line;
2765 *---------------------------------------------------------------------
2767 * Read an entire line from the input file. Called only by Parse_File.
2770 * A line w/o its newline
2773 * Only those associated with reading a character
2774 *---------------------------------------------------------------------
2779 char *line; /* Result */
2780 int lineLength; /* Length of result */
2781 int lineno; /* Saved line # */
2785 line = ParseGetLine(0, &lineLength);
2793 * The line might be a conditional. Ask the conditional module
2794 * about it and act accordingly
2796 switch (Cond_Eval(line)) {
2798 /* Skip to next conditional that evaluates to COND_PARSE. */
2800 line = ParseGetLine(PARSE_SKIP, &lineLength);
2801 } while (line && Cond_Eval(line) != COND_PARSE);
2807 case COND_INVALID: /* Not a conditional line */
2808 /* Check for .for loops */
2809 rval = For_Eval(line);
2811 /* Not a .for line */
2814 /* Syntax error - error printed, ignore line */
2816 /* Start of a .for loop */
2817 lineno = curFile->lineno;
2818 /* Accumulate loop lines until matching .endfor */
2820 line = ParseGetLine(PARSE_RAW, &lineLength);
2822 Parse_Error(PARSE_FATAL,
2823 "Unexpected end of file in for loop.");
2826 } while (For_Accum(line));
2827 /* Stash each iteration as a new 'input file' */
2829 /* Read next line from for-loop buffer */
2837 *-----------------------------------------------------------------------
2838 * ParseFinishLine --
2839 * Handle the end of a dependency group.
2845 * inLine set FALSE. 'targets' list destroyed.
2847 *-----------------------------------------------------------------------
2850 ParseFinishLine(void)
2853 Lst_ForEach(targets, Suff_EndTransform, NULL);
2854 Lst_Destroy(targets, ParseHasCommands);
2862 *---------------------------------------------------------------------
2864 * Parse a file into its component parts, incorporating it into the
2865 * current dependency graph. This is the main function and controls
2866 * almost every other function in this module
2869 * name the name of the file being read
2870 * fd Open file to makefile to parse
2877 * Loads. Nodes are added to the list of all targets, nodes and links
2878 * are added to the dependency graph. etc. etc. etc.
2879 *---------------------------------------------------------------------
2882 Parse_File(const char *name, int fd)
2884 char *cp; /* pointer into the line */
2885 char *line; /* the line we're working on */
2886 struct loadedfile *lf;
2888 lf = loadfile(name, fd);
2897 Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf);
2901 for (; (line = ParseReadLine()) != NULL; ) {
2903 fprintf(debug_file, "ParseReadLine (%d): '%s'\n",
2904 curFile->lineno, line);
2907 * Lines that begin with the special character may be
2908 * include or undef directives.
2909 * On the other hand they can be suffix rules (.c.o: ...)
2910 * or just dependencies for filenames that start '.'.
2912 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2915 if (strncmp(cp, "include", 7) == 0 ||
2916 ((cp[0] == 's' || cp[0] == '-') &&
2917 strncmp(&cp[1], "include", 7) == 0)) {
2921 if (strncmp(cp, "undef", 5) == 0) {
2923 for (cp += 5; isspace((unsigned char) *cp); cp++)
2925 for (cp2 = cp; !isspace((unsigned char) *cp2) &&
2926 (*cp2 != '\0'); cp2++)
2929 Var_Delete(cp, VAR_GLOBAL);
2931 } else if (strncmp(cp, "export", 6) == 0) {
2932 for (cp += 6; isspace((unsigned char) *cp); cp++)
2936 } else if (strncmp(cp, "unexport", 8) == 0) {
2939 } else if (strncmp(cp, "info", 4) == 0 ||
2940 strncmp(cp, "error", 5) == 0 ||
2941 strncmp(cp, "warning", 7) == 0) {
2942 if (ParseMessage(cp))
2947 if (*line == '\t') {
2949 * If a line starts with a tab, it can only hope to be
2950 * a creation command.
2954 for (; isspace ((unsigned char)*cp); cp++) {
2959 Parse_Error(PARSE_FATAL,
2960 "Unassociated shell command \"%s\"",
2963 * So long as it's not a blank line and we're actually
2964 * in a dependency spec, add the command to the list of
2965 * commands of all targets in the dependency spec
2968 cp = bmake_strdup(cp);
2969 Lst_ForEach(targets, ParseAddCmd, cp);
2971 Lst_AtEnd(targCmds, cp);
2979 if (((strncmp(line, "include", 7) == 0 &&
2980 isspace((unsigned char) line[7])) ||
2981 ((line[0] == 's' || line[0] == '-') &&
2982 strncmp(&line[1], "include", 7) == 0 &&
2983 isspace((unsigned char) line[8]))) &&
2984 strchr(line, ':') == NULL) {
2986 * It's an S3/S5-style "include".
2988 ParseTraditionalInclude(line);
2993 if (strncmp(line, "export", 6) == 0 &&
2994 isspace((unsigned char) line[6]) &&
2995 strchr(line, ':') == NULL) {
2997 * It's a Gmake "export".
2999 ParseGmakeExport(line);
3003 if (Parse_IsVar(line)) {
3005 Parse_DoVar(line, VAR_GLOBAL);
3011 * To make life easier on novices, if the line is indented we
3012 * first make sure the line has a dependency operator in it.
3013 * If it doesn't have an operator and we're in a dependency
3014 * line's script, we assume it's actually a shell command
3015 * and add it to the current list of targets.
3018 if (isspace((unsigned char) line[0])) {
3019 while ((*cp != '\0') && isspace((unsigned char) *cp))
3021 while (*cp && (ParseIsEscaped(line, cp) ||
3022 (*cp != ':') && (*cp != '!'))) {
3027 Parse_Error(PARSE_WARNING,
3028 "Shell command needs a leading tab");
3037 * For some reason - probably to make the parser impossible -
3038 * a ';' can be used to separate commands from dependencies.
3039 * Attempt to avoid ';' inside substitution patterns.
3044 for (cp = line; *cp != 0; cp++) {
3045 if (*cp == '\\' && cp[1] != 0) {
3050 (cp[1] == '(' || cp[1] == '{')) {
3055 if (*cp == ')' || *cp == '}') {
3059 } else if (*cp == ';') {
3065 /* Terminate the dependency list at the ';' */
3071 * We now know it's a dependency line so it needs to have all
3072 * variables expanded before being parsed. Tell the variable
3073 * module to complain if some variable is undefined...
3075 line = Var_Subst(NULL, line, VAR_CMD, TRUE);
3078 * Need a non-circular list for the target nodes
3081 Lst_Destroy(targets, NULL);
3083 targets = Lst_Init(FALSE);
3086 ParseDoDependency(line);
3089 /* If there were commands after a ';', add them now */
3095 * Reached EOF, but it may be just EOF of an include file...
3097 } while (ParseEOF() == CONTINUE);
3100 (void)fflush(stdout);
3101 (void)fprintf(stderr,
3102 "%s: Fatal errors encountered -- cannot continue",
3104 PrintOnError(NULL, NULL);
3110 *---------------------------------------------------------------------
3112 * initialize the parsing module
3118 * the parseIncPath list is initialized...
3119 *---------------------------------------------------------------------
3125 parseIncPath = Lst_Init(FALSE);
3126 sysIncPath = Lst_Init(FALSE);
3127 defIncPath = Lst_Init(FALSE);
3128 includes = Lst_Init(FALSE);
3130 targCmds = Lst_Init(FALSE);
3138 Lst_Destroy(targCmds, (FreeProc *)free);
3140 Lst_Destroy(targets, NULL);
3141 Lst_Destroy(defIncPath, Dir_Destroy);
3142 Lst_Destroy(sysIncPath, Dir_Destroy);
3143 Lst_Destroy(parseIncPath, Dir_Destroy);
3144 Lst_Destroy(includes, NULL); /* Should be empty now */
3150 *-----------------------------------------------------------------------
3152 * Return a Lst of the main target to create for main()'s sake. If
3153 * no such target exists, we Punt with an obnoxious error message.
3156 * A Lst of the single node to create.
3161 *-----------------------------------------------------------------------
3164 Parse_MainName(void)
3166 Lst mainList; /* result list */
3168 mainList = Lst_Init(FALSE);
3170 if (mainNode == NULL) {
3171 Punt("no target to make.");
3173 } else if (mainNode->type & OP_DOUBLEDEP) {
3174 (void)Lst_AtEnd(mainList, mainNode);
3175 Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW);
3178 (void)Lst_AtEnd(mainList, mainNode);
3179 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
3184 *-----------------------------------------------------------------------
3186 * Add the filename and lineno to the GNode so that we remember
3187 * where it was first defined.
3192 *-----------------------------------------------------------------------
3195 ParseMark(GNode *gn)
3197 gn->fname = curFile->fname;
3198 gn->lineno = curFile->lineno;