1 /* $NetBSD: parse.c,v 1.420 2020/11/01 00:24:57 rillig 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 * Parsing of makefiles.
74 * Parse_File is the main entry point and controls most of the other
75 * functions in this module.
77 * The directories for the .include "..." directive are kept in
78 * 'parseIncPath', while those for .include <...> are kept in 'sysIncPath'.
79 * The targets currently being defined are kept in 'targets'.
82 * Parse_Init Initialize the module
84 * Parse_End Clean up the module
86 * Parse_File Parse a top-level makefile. Included files are
87 * handled by Parse_include_file though.
89 * Parse_IsVar Return TRUE if the given line is a variable
90 * assignment. Used by MainParseArgs to determine if
91 * an argument is a target or a variable assignment.
92 * Used internally for pretty much the same thing.
94 * Parse_Error Report a parse error, a warning or an informational
97 * Parse_MainName Returns a list of the main target to create.
100 #include <sys/types.h>
101 #include <sys/stat.h>
112 #include <sys/mman.h>
115 #define MAP_COPY MAP_PRIVATE
124 #include "pathnames.h"
126 /* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
127 MAKE_RCSID("$NetBSD: parse.c,v 1.420 2020/11/01 00:24:57 rillig Exp $");
129 /* types and constants */
132 * Structure for a file being read ("included file")
134 typedef struct IFile {
135 char *fname; /* name of file */
136 Boolean fromForLoop; /* simulated .include by the .for loop */
137 int lineno; /* current line number in file */
138 int first_lineno; /* line number of start of text */
139 unsigned int cond_depth; /* 'if' nesting when file opened */
140 Boolean depending; /* state of doing_depend on EOF */
142 /* The buffer from which the file's content is read. */
144 char *buf_ptr; /* next char to be read */
147 char *(*nextbuf)(void *, size_t *); /* Function to get more data */
148 void *nextbuf_arg; /* Opaque arg for nextbuf() */
149 struct loadedfile *lf; /* loadedfile object, if any */
153 * Tokens for target attributes
155 typedef enum ParseSpecial {
156 SP_ATTRIBUTE, /* Generic attribute */
157 SP_BEGIN, /* .BEGIN */
158 SP_DEFAULT, /* .DEFAULT */
159 SP_DELETE_ON_ERROR, /* .DELETE_ON_ERROR */
161 SP_ERROR, /* .ERROR */
162 SP_IGNORE, /* .IGNORE */
163 SP_INCLUDES, /* .INCLUDES; not mentioned in the manual page */
164 SP_INTERRUPT, /* .INTERRUPT */
165 SP_LIBS, /* .LIBS; not mentioned in the manual page */
166 SP_MAIN, /* .MAIN and we don't have anything user-specified to
169 SP_MFLAGS, /* .MFLAGS or .MAKEFLAGS */
170 SP_NOMETA, /* .NOMETA */
171 SP_NOMETA_CMP, /* .NOMETA_CMP */
172 SP_NOPATH, /* .NOPATH */
173 SP_NOT, /* Not special */
174 SP_NOTPARALLEL, /* .NOTPARALLEL or .NO_PARALLEL */
175 SP_NULL, /* .NULL; not mentioned in the manual page */
176 SP_OBJDIR, /* .OBJDIR */
177 SP_ORDER, /* .ORDER */
178 SP_PARALLEL, /* .PARALLEL; not mentioned in the manual page */
179 SP_PATH, /* .PATH or .PATH.suffix */
180 SP_PHONY, /* .PHONY */
182 SP_POSIX, /* .POSIX; not mentioned in the manual page */
184 SP_PRECIOUS, /* .PRECIOUS */
185 SP_SHELL, /* .SHELL */
186 SP_SILENT, /* .SILENT */
187 SP_SINGLESHELL, /* .SINGLESHELL; not mentioned in the manual page */
188 SP_STALE, /* .STALE */
189 SP_SUFFIXES, /* .SUFFIXES */
193 typedef List SearchPathList;
194 typedef ListNode SearchPathListNode;
199 * The main target to create. This is the first target on the first
200 * dependency line in the first makefile.
202 static GNode *mainNode;
206 /* During parsing, the targets from the left-hand side of the currently
207 * active dependency line, or NULL if the current line does not belong to a
208 * dependency line, for example because it is a variable assignment.
210 * See unit-tests/deptgt.mk, keyword "parse.c:targets". */
211 static GNodeList *targets;
214 /* All shell commands for all targets, in no particular order and possibly
215 * with duplicates. Kept in a separate list since the commands from .USE or
216 * .USEBEFORE nodes are shared with other GNodes, thereby giving up the
217 * easily understandable ownership over the allocated strings. */
218 static StringList *targCmds;
222 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
223 * seen, then set to each successive source on the line.
225 static GNode *order_pred;
229 /* number of fatal errors */
230 static int fatals = 0;
233 * Variables for doing includes
236 /* The include chain of makefiles. At the bottom is the top-level makefile
237 * from the command line, and on top of that, there are the included files or
238 * .for loops, up to and including the current file.
240 * This data could be used to print stack traces on parse errors. As of
241 * 2020-09-14, this is not done though. It seems quite simple to print the
242 * tuples (fname:lineno:fromForLoop), from top to bottom. This simple idea is
243 * made complicated by the fact that the .for loops also use this stack for
244 * storing information.
246 * The lineno fields of the IFiles with fromForLoop == TRUE look confusing,
247 * which is demonstrated by the test 'include-main.mk'. They seem sorted
248 * backwards since they tell the number of completely parsed lines, which for
249 * a .for loop is right after the terminating .endfor. To compensate for this
250 * confusion, there is another field first_lineno pointing at the start of the
251 * .for loop, 1-based for human consumption.
253 * To make the stack trace intuitive, the entry below the first .for loop must
254 * be ignored completely since neither its lineno nor its first_lineno is
255 * useful. Instead, the topmost of each chain of .for loop needs to be
256 * printed twice, once with its first_lineno and once with its lineno.
258 * As of 2020-10-28, using the above rules, the stack trace for the .info line
259 * in include-subsub.mk would be:
261 * includes[5]: include-subsub.mk:4
262 * (lineno, from an .include)
263 * includes[4]: include-sub.mk:32
264 * (lineno, from a .for loop below an .include)
265 * includes[4]: include-sub.mk:31
266 * (first_lineno, from a .for loop, lineno == 32)
267 * includes[3]: include-sub.mk:30
268 * (first_lineno, from a .for loop, lineno == 33)
269 * includes[2]: include-sub.mk:29
270 * (first_lineno, from a .for loop, lineno == 34)
271 * includes[1]: include-sub.mk:35
272 * (not printed since it is below a .for loop)
273 * includes[0]: include-main.mk:27
275 static Vector /* of IFile */ includes;
280 return Vector_Get(&includes, i);
283 /* The file that is currently being read. */
287 return GetInclude(includes.len - 1);
291 SearchPath *parseIncPath; /* dirs for "..." includes */
292 SearchPath *sysIncPath; /* dirs for <...> includes */
293 SearchPath *defSysIncPath; /* default for sysIncPath */
298 * The parseKeywords table is searched using binary search when deciding
299 * if a target or source is special. The 'spec' field is the ParseSpecial
300 * type of the keyword (SP_NOT if the keyword isn't special as a target) while
301 * the 'op' field is the operator to apply to the list of targets if the
302 * keyword is used as a source ("0" if the keyword isn't special as a source)
304 static const struct {
305 const char *name; /* Name of keyword */
306 ParseSpecial spec; /* Type when used as a target */
307 GNodeType op; /* Operator when used as a source */
308 } parseKeywords[] = {
309 { ".BEGIN", SP_BEGIN, 0 },
310 { ".DEFAULT", SP_DEFAULT, 0 },
311 { ".DELETE_ON_ERROR", SP_DELETE_ON_ERROR, 0 },
312 { ".END", SP_END, 0 },
313 { ".ERROR", SP_ERROR, 0 },
314 { ".EXEC", SP_ATTRIBUTE, OP_EXEC },
315 { ".IGNORE", SP_IGNORE, OP_IGNORE },
316 { ".INCLUDES", SP_INCLUDES, 0 },
317 { ".INTERRUPT", SP_INTERRUPT, 0 },
318 { ".INVISIBLE", SP_ATTRIBUTE, OP_INVISIBLE },
319 { ".JOIN", SP_ATTRIBUTE, OP_JOIN },
320 { ".LIBS", SP_LIBS, 0 },
321 { ".MADE", SP_ATTRIBUTE, OP_MADE },
322 { ".MAIN", SP_MAIN, 0 },
323 { ".MAKE", SP_ATTRIBUTE, OP_MAKE },
324 { ".MAKEFLAGS", SP_MFLAGS, 0 },
325 { ".META", SP_META, OP_META },
326 { ".MFLAGS", SP_MFLAGS, 0 },
327 { ".NOMETA", SP_NOMETA, OP_NOMETA },
328 { ".NOMETA_CMP", SP_NOMETA_CMP, OP_NOMETA_CMP },
329 { ".NOPATH", SP_NOPATH, OP_NOPATH },
330 { ".NOTMAIN", SP_ATTRIBUTE, OP_NOTMAIN },
331 { ".NOTPARALLEL", SP_NOTPARALLEL, 0 },
332 { ".NO_PARALLEL", SP_NOTPARALLEL, 0 },
333 { ".NULL", SP_NULL, 0 },
334 { ".OBJDIR", SP_OBJDIR, 0 },
335 { ".OPTIONAL", SP_ATTRIBUTE, OP_OPTIONAL },
336 { ".ORDER", SP_ORDER, 0 },
337 { ".PARALLEL", SP_PARALLEL, 0 },
338 { ".PATH", SP_PATH, 0 },
339 { ".PHONY", SP_PHONY, OP_PHONY },
341 { ".POSIX", SP_POSIX, 0 },
343 { ".PRECIOUS", SP_PRECIOUS, OP_PRECIOUS },
344 { ".RECURSIVE", SP_ATTRIBUTE, OP_MAKE },
345 { ".SHELL", SP_SHELL, 0 },
346 { ".SILENT", SP_SILENT, OP_SILENT },
347 { ".SINGLESHELL", SP_SINGLESHELL, 0 },
348 { ".STALE", SP_STALE, 0 },
349 { ".SUFFIXES", SP_SUFFIXES, 0 },
350 { ".USE", SP_ATTRIBUTE, OP_USE },
351 { ".USEBEFORE", SP_ATTRIBUTE, OP_USEBEFORE },
352 { ".WAIT", SP_WAIT, 0 },
358 const char *path; /* name, for error reports */
359 char *buf; /* contents buffer */
360 size_t len; /* length of contents */
361 size_t maplen; /* length of mmap area, or 0 */
362 Boolean used; /* XXX: have we used the data yet */
365 static struct loadedfile *
366 loadedfile_create(const char *path)
368 struct loadedfile *lf;
370 lf = bmake_malloc(sizeof(*lf));
371 lf->path = path == NULL ? "(stdin)" : path;
380 loadedfile_destroy(struct loadedfile *lf)
382 if (lf->buf != NULL) {
383 if (lf->maplen > 0) {
385 munmap(lf->buf, lf->maplen);
395 * nextbuf() operation for loadedfile, as needed by the weird and twisted
396 * logic below. Once that's cleaned up, we can get rid of lf->used...
399 loadedfile_nextbuf(void *x, size_t *len)
401 struct loadedfile *lf = x;
412 * Try to get the size of a file.
415 load_getsize(int fd, size_t *ret)
419 if (fstat(fd, &st) < 0) {
423 if (!S_ISREG(st.st_mode)) {
428 * st_size is an off_t, which is 64 bits signed; *ret is
429 * size_t, which might be 32 bits unsigned or 64 bits
430 * unsigned. Rather than being elaborate, just punt on
431 * files that are more than 2^31 bytes. We should never
432 * see a makefile that size in practice...
434 * While we're at it reject negative sizes too, just in case.
436 if (st.st_size < 0 || st.st_size > 0x7fffffff) {
440 *ret = (size_t)st.st_size;
446 loadedfile_mmap(struct loadedfile *lf, int fd)
448 static unsigned long pagesize = 0;
450 if (load_getsize(fd, &lf->len)) {
452 /* found a size, try mmap */
455 pagesize = (unsigned long)sysconf(_SC_PAGESIZE);
457 if (pagesize == 0 || pagesize == (unsigned long)-1) {
460 /* round size up to a page */
461 lf->maplen = pagesize * ((lf->len + pagesize - 1) / pagesize);
464 * XXX hack for dealing with empty files; remove when
465 * we're no longer limited by interfacing to the old
466 * logic elsewhere in this file.
468 if (lf->maplen == 0) {
469 lf->maplen = pagesize;
473 * FUTURE: remove PROT_WRITE when the parser no longer
474 * needs to scribble on the input.
476 lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE,
477 MAP_FILE|MAP_COPY, fd, 0);
478 if (lf->buf != MAP_FAILED) {
480 if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
481 char *b = bmake_malloc(lf->len + 1);
483 memcpy(b, lf->buf, lf->len++);
484 munmap(lf->buf, lf->maplen);
498 * Until the path search logic can be moved under here instead of
499 * being in the caller in another source file, we need to have the fd
500 * passed in already open. Bleh.
502 * If the path is NULL use stdin and (to insure against fd leaks)
503 * assert that the caller passed in -1.
505 static struct loadedfile *
506 loadfile(const char *path, int fd)
508 struct loadedfile *lf;
512 lf = loadedfile_create(path);
519 fd = open(path, O_RDONLY);
522 Error("%s: %s", path, strerror(errno));
529 if (loadedfile_mmap(lf, fd))
533 /* cannot mmap; load the traditional way */
537 lf->buf = bmake_malloc(lf->len);
541 assert(bufpos <= lf->len);
542 if (bufpos == lf->len) {
543 if (lf->len > SIZE_MAX/2) {
545 Error("%s: file too large", path);
549 lf->buf = bmake_realloc(lf->buf, lf->len);
551 assert(bufpos < lf->len);
552 result = read(fd, lf->buf + bufpos, lf->len - bufpos);
554 Error("%s: read error: %s", path, strerror(errno));
560 bufpos += (size_t)result;
562 assert(bufpos <= lf->len);
565 /* truncate malloc region to actual length (maybe not useful) */
567 /* as for mmap case, ensure trailing \n */
568 if (lf->buf[lf->len - 1] != '\n')
570 lf->buf = bmake_realloc(lf->buf, lf->len);
571 lf->buf[lf->len - 1] = '\n';
585 /* Check if the current character is escaped on the current line. */
587 ParseIsEscaped(const char *line, const char *c)
589 Boolean active = FALSE;
599 /* Add the filename and lineno to the GNode so that we remember where it
600 * was first defined. */
604 IFile *curFile = CurFile();
605 gn->fname = curFile->fname;
606 gn->lineno = curFile->lineno;
609 /* Look in the table of keywords for one matching the given string.
610 * Return the index of the keyword, or -1 if it isn't there. */
612 ParseFindKeyword(const char *str)
618 end = sizeof parseKeywords / sizeof parseKeywords[0] - 1;
621 cur = start + (end - start) / 2;
622 diff = strcmp(str, parseKeywords[cur].name);
626 } else if (diff < 0) {
631 } while (start <= end);
636 PrintLocation(FILE *f, const char *filename, size_t lineno)
638 char dirbuf[MAXPATHLEN+1];
639 const char *dir, *base;
640 void *dir_freeIt, *base_freeIt;
642 if (*filename == '/' || strcmp(filename, "(stdin)") == 0) {
643 (void)fprintf(f, "\"%s\" line %zu: ", filename, lineno);
647 /* Find out which makefile is the culprit.
648 * We try ${.PARSEDIR} and apply realpath(3) if not absolute. */
650 dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &dir_freeIt);
654 dir = realpath(dir, dirbuf);
656 base = Var_Value(".PARSEFILE", VAR_GLOBAL, &base_freeIt);
658 const char *slash = strrchr(filename, '/');
659 base = slash != NULL ? slash + 1 : filename;
662 (void)fprintf(f, "\"%s/%s\" line %zu: ", dir, base, lineno);
663 bmake_free(base_freeIt);
664 bmake_free(dir_freeIt);
667 /* Print a parse error message, including location information.
669 * Increment "fatals" if the level is PARSE_FATAL, and continue parsing
670 * until the end of the current top-level makefile, then exit (see
673 ParseVErrorInternal(FILE *f, const char *cfname, size_t clineno,
674 ParseErrorLevel type, const char *fmt, va_list ap)
676 static Boolean fatal_warning_error_printed = FALSE;
678 (void)fprintf(f, "%s: ", progname);
681 PrintLocation(f, cfname, clineno);
682 if (type == PARSE_WARNING)
683 (void)fprintf(f, "warning: ");
684 (void)vfprintf(f, fmt, ap);
685 (void)fprintf(f, "\n");
688 if (type == PARSE_INFO)
690 if (type == PARSE_FATAL || opts.parseWarnFatal)
692 if (opts.parseWarnFatal && !fatal_warning_error_printed) {
693 Error("parsing warnings being treated as errors");
694 fatal_warning_error_printed = TRUE;
699 ParseErrorInternal(const char *cfname, size_t clineno, ParseErrorLevel type,
700 const char *fmt, ...)
705 (void)fflush(stdout);
706 ParseVErrorInternal(stderr, cfname, clineno, type, fmt, ap);
709 if (opts.debug_file != stderr && opts.debug_file != stdout) {
711 ParseVErrorInternal(opts.debug_file, cfname, clineno, type,
717 /* External interface to ParseErrorInternal; uses the default filename and
720 * Fmt is given without a trailing newline. */
722 Parse_Error(ParseErrorLevel type, const char *fmt, ...)
728 if (includes.len == 0) {
732 IFile *curFile = CurFile();
733 fname = curFile->fname;
734 lineno = (size_t)curFile->lineno;
738 (void)fflush(stdout);
739 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
742 if (opts.debug_file != stderr && opts.debug_file != stdout) {
744 ParseVErrorInternal(opts.debug_file, fname, lineno, type,
751 /* Parse a .info .warning or .error directive.
753 * The input is the line minus the ".". We substitute variables, print the
754 * message and exit(1) (for .error) or just print a warning if the directive
758 ParseMessage(const char *directive)
760 const char *p = directive;
761 int mtype = *p == 'i' ? PARSE_INFO :
762 *p == 'w' ? PARSE_WARNING : PARSE_FATAL;
765 while (ch_isalpha(*p))
768 return FALSE; /* missing argument */
770 cpp_skip_whitespace(&p);
771 (void)Var_Subst(p, VAR_CMDLINE, VARE_WANTRES, &arg);
772 /* TODO: handle errors */
774 Parse_Error(mtype, "%s", arg);
777 if (mtype == PARSE_FATAL) {
778 PrintOnError(NULL, NULL);
784 /* Add the child to the parent's children.
786 * Additionally, add the parent to the child's parents, but only if the
787 * target is not special. An example for such a special target is .END,
788 * which does not need to be informed once the child target has been made. */
790 LinkSource(GNode *pgn, GNode *cgn, Boolean isSpecial)
792 if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(pgn->cohorts))
793 pgn = pgn->cohorts->last->datum;
795 Lst_Append(pgn->children, cgn);
798 /* Special targets like .END don't need any children. */
800 Lst_Append(cgn->parents, pgn);
803 debug_printf("# %s: added child %s - %s\n",
804 __func__, pgn->name, cgn->name);
805 Targ_PrintNode(pgn, 0);
806 Targ_PrintNode(cgn, 0);
810 /* Add the node to each target from the current dependency group. */
812 LinkToTargets(GNode *gn, Boolean isSpecial)
815 for (ln = targets->first; ln != NULL; ln = ln->next)
816 LinkSource(ln->datum, gn, isSpecial);
820 TryApplyDependencyOperator(GNode *gn, GNodeType op)
823 * If the node occurred on the left-hand side of a dependency and the
824 * operator also defines a dependency, they must match.
826 if ((op & OP_OPMASK) && (gn->type & OP_OPMASK) &&
827 ((op & OP_OPMASK) != (gn->type & OP_OPMASK)))
829 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
833 if (op == OP_DOUBLEDEP && (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
835 * If the node was the object of a :: operator, we need to create a
836 * new instance of it for the children and commands on this dependency
837 * line. The new instance is placed on the 'cohorts' list of the
838 * initial one (note the initial one is not on its own cohorts list)
839 * and the new instance is linked to all parents of the initial
845 * Propagate copied bits to the initial node. They'll be propagated
846 * back to the rest of the cohorts later.
848 gn->type |= op & ~OP_OPMASK;
850 cohort = Targ_NewInternalNode(gn->name);
854 * Make the cohort invisible as well to avoid duplicating it into
855 * other variables. True, parents of this target won't tend to do
856 * anything with their local variables, but better safe than
857 * sorry. (I think this is pointless now, since the relevant list
858 * traversals will no longer see this node anyway. -mycroft)
860 cohort->type = op | OP_INVISIBLE;
861 Lst_Append(gn->cohorts, cohort);
862 cohort->centurion = gn;
863 gn->unmade_cohorts++;
864 snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
865 (unsigned int)gn->unmade_cohorts % 1000000);
868 * We don't want to nuke any previous flags (whatever they were) so we
869 * just OR the new operator into the old
878 ApplyDependencyOperator(GNodeType op)
881 for (ln = targets->first; ln != NULL; ln = ln->next)
882 if (!TryApplyDependencyOperator(ln->datum, op))
887 ParseDoSrcKeyword(const char *src, ParseSpecial specType)
889 static int wait_number = 0;
893 if (*src == '.' && ch_isupper(src[1])) {
894 int keywd = ParseFindKeyword(src);
896 int op = parseKeywords[keywd].op;
898 ApplyDependencyOperator(op);
901 if (parseKeywords[keywd].spec == SP_WAIT) {
903 * We add a .WAIT node in the dependency list.
904 * After any dynamic dependencies (and filename globbing)
905 * have happened, it is given a dependency on the each
906 * previous child back to and previous .WAIT node.
907 * The next child won't be scheduled until the .WAIT node
909 * We give each .WAIT node a unique name (mainly for diag).
911 snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
912 gn = Targ_NewInternalNode(wait_src);
915 gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
916 LinkToTargets(gn, specType != SP_NOT);
925 ParseDoSrcMain(const char *src)
928 * If we have noted the existence of a .MAIN, it means we need
929 * to add the sources of said target to the list of things
930 * to create. The string 'src' is likely to be free, so we
931 * must make a new copy of it. Note that this will only be
932 * invoked if the user didn't specify a target on the command
933 * line. This is to allow #ifmake's to succeed, or something...
935 Lst_Append(opts.create, bmake_strdup(src));
937 * Add the name to the .TARGETS variable as well, so the user can
938 * employ that, if desired.
940 Var_Append(".TARGETS", src, VAR_GLOBAL);
944 ParseDoSrcOrder(const char *src)
948 * Create proper predecessor/successor links between the previous
949 * source and the current one.
951 gn = Targ_GetNode(src);
954 if (order_pred != NULL) {
955 Lst_Append(order_pred->order_succ, gn);
956 Lst_Append(gn->order_pred, order_pred);
958 debug_printf("# %s: added Order dependency %s - %s\n",
959 __func__, order_pred->name, gn->name);
960 Targ_PrintNode(order_pred, 0);
961 Targ_PrintNode(gn, 0);
965 * The current source now becomes the predecessor for the next one.
971 ParseDoSrcOther(const char *src, GNodeType tOp, ParseSpecial specType)
976 * If the source is not an attribute, we need to find/create
977 * a node for it. After that we can apply any operator to it
978 * from a special target or link it to its parents, as
981 * In the case of a source that was the object of a :: operator,
982 * the attribute is applied to all of its instances (as kept in
983 * the 'cohorts' list of the node) or all the cohorts are linked
984 * to all the targets.
987 /* Find/create the 'src' node and attach to all targets */
988 gn = Targ_GetNode(src);
994 LinkToTargets(gn, specType != SP_NOT);
998 /* Given the name of a source in a dependency line, figure out if it is an
999 * attribute (such as .SILENT) and apply it to the targets if it is. Else
1000 * decide if there is some attribute which should be applied *to* the source
1001 * because of some special target (such as .PHONY) and apply it if so.
1002 * Otherwise, make the source a child of the targets in the list 'targets'.
1005 * tOp operator (if any) from special targets
1006 * src name of the source to handle
1009 ParseDoSrc(GNodeType tOp, const char *src, ParseSpecial specType)
1011 if (ParseDoSrcKeyword(src, specType))
1014 if (specType == SP_MAIN)
1015 ParseDoSrcMain(src);
1016 else if (specType == SP_ORDER)
1017 ParseDoSrcOrder(src);
1019 ParseDoSrcOther(src, tOp, specType);
1022 /* If we have yet to decide on a main target to make, in the absence of any
1023 * user input, we want the first target on the first dependency line that is
1024 * actually a real target (i.e. isn't a .USE or .EXEC rule) to be made. */
1026 FindMainTarget(void)
1030 if (mainNode != NULL)
1033 for (ln = targets->first; ln != NULL; ln = ln->next) {
1034 GNode *gn = ln->datum;
1035 if (!(gn->type & OP_NOTARGET)) {
1044 * We got to the end of the line while we were still looking at targets.
1046 * Ending a dependency line without an operator is a Bozo no-no. As a
1047 * heuristic, this is also often triggered by undetected conflicts from
1051 ParseErrorNoDependency(const char *lstart)
1053 if ((strncmp(lstart, "<<<<<<", 6) == 0) ||
1054 (strncmp(lstart, "======", 6) == 0) ||
1055 (strncmp(lstart, ">>>>>>", 6) == 0))
1056 Parse_Error(PARSE_FATAL,
1057 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
1058 else if (lstart[0] == '.') {
1059 const char *dirstart = lstart + 1;
1061 cpp_skip_whitespace(&dirstart);
1063 while (ch_isalnum(*dirend) || *dirend == '-')
1065 Parse_Error(PARSE_FATAL, "Unknown directive \"%.*s\"",
1066 (int)(dirend - dirstart), dirstart);
1068 Parse_Error(PARSE_FATAL, "Need an operator");
1072 ParseDependencyTargetWord(/*const*/ char **pp, const char *lstart)
1074 /*const*/ char *cp = *pp;
1076 while (*cp != '\0') {
1077 if ((ch_isspace(*cp) || *cp == '!' || *cp == ':' || *cp == '(') &&
1078 !ParseIsEscaped(lstart, cp))
1083 * Must be a dynamic source (would have been expanded
1084 * otherwise), so call the Var module to parse the puppy
1085 * so we can safely advance beyond it...There should be
1086 * no errors in this, as they would have been discovered
1087 * in the initial Var_Subst and we wouldn't be here.
1089 const char *nested_p = cp;
1090 const char *nested_val;
1093 (void)Var_Parse(&nested_p, VAR_CMDLINE, VARE_UNDEFERR|VARE_WANTRES,
1094 &nested_val, &freeIt);
1095 /* TODO: handle errors */
1097 cp += nested_p - cp;
1106 * Certain special targets have special semantics:
1107 * .PATH Have to set the dirSearchPath
1109 * .MAIN Its sources are only used if
1110 * nothing has been specified to
1112 * .DEFAULT Need to create a node to hang
1113 * commands on, but we don't want
1114 * it in the graph, nor do we want
1115 * it to be the Main Target, so we
1116 * create it, set OP_NOTMAIN and
1117 * add it to the list, setting
1118 * DEFAULT to the new node for
1119 * later use. We claim the node is
1120 * A transformation rule to make
1121 * life easier later, when we'll
1122 * use Make_HandleUse to actually
1123 * apply the .DEFAULT commands.
1124 * .PHONY The list of targets
1125 * .NOPATH Don't search for file in the path
1131 * .INTERRUPT Are not to be considered the
1133 * .NOTPARALLEL Make only one target at a time.
1134 * .SINGLESHELL Create a shell for each command.
1135 * .ORDER Must set initial predecessor to NULL
1138 ParseDoDependencyTargetSpecial(ParseSpecial *inout_specType,
1140 SearchPathList **inout_paths)
1142 switch (*inout_specType) {
1144 if (*inout_paths == NULL) {
1145 *inout_paths = Lst_New();
1147 Lst_Append(*inout_paths, dirSearchPath);
1150 if (!Lst_IsEmpty(opts.create)) {
1151 *inout_specType = SP_NOT;
1158 case SP_INTERRUPT: {
1159 GNode *gn = Targ_GetNode(line);
1162 gn->type |= OP_NOTMAIN|OP_SPECIAL;
1163 Lst_Append(targets, gn);
1167 GNode *gn = Targ_NewGN(".DEFAULT");
1168 gn->type |= OP_NOTMAIN|OP_TRANSFORM;
1169 Lst_Append(targets, gn);
1173 case SP_DELETE_ON_ERROR:
1174 deleteOnError = TRUE;
1176 case SP_NOTPARALLEL:
1179 case SP_SINGLESHELL:
1180 opts.compatMake = TRUE;
1191 * .PATH<suffix> has to be handled specially.
1192 * Call on the suffix module to give us a path to modify.
1195 ParseDoDependencyTargetPath(const char *line, SearchPathList **inout_paths)
1199 path = Suff_GetPath(&line[5]);
1201 Parse_Error(PARSE_FATAL,
1202 "Suffix '%s' not defined (yet)",
1206 if (*inout_paths == NULL) {
1207 *inout_paths = Lst_New();
1209 Lst_Append(*inout_paths, path);
1215 * See if it's a special target and if so set specType to match it.
1218 ParseDoDependencyTarget(const char *line, ParseSpecial *inout_specType,
1219 GNodeType *out_tOp, SearchPathList **inout_paths)
1223 if (!(*line == '.' && ch_isupper(line[1])))
1227 * See if the target is a special target that must have it
1228 * or its sources handled specially.
1230 keywd = ParseFindKeyword(line);
1232 if (*inout_specType == SP_PATH && parseKeywords[keywd].spec != SP_PATH) {
1233 Parse_Error(PARSE_FATAL, "Mismatched special targets");
1237 *inout_specType = parseKeywords[keywd].spec;
1238 *out_tOp = parseKeywords[keywd].op;
1240 ParseDoDependencyTargetSpecial(inout_specType, line, inout_paths);
1242 } else if (strncmp(line, ".PATH", 5) == 0) {
1243 *inout_specType = SP_PATH;
1244 if (!ParseDoDependencyTargetPath(line, inout_paths))
1251 ParseDoDependencyTargetMundane(char *line, StringList *curTargs)
1253 if (Dir_HasWildcards(line)) {
1255 * Targets are to be sought only in the current directory,
1256 * so create an empty path for the thing. Note we need to
1257 * use Dir_Destroy in the destruction of the path as the
1258 * Dir module could have added a directory to the path...
1260 SearchPath *emptyPath = Lst_New();
1262 Dir_Expand(line, emptyPath, curTargs);
1264 Lst_Destroy(emptyPath, Dir_Destroy);
1267 * No wildcards, but we want to avoid code duplication,
1268 * so create a list with the word on it.
1270 Lst_Append(curTargs, line);
1273 /* Apply the targets. */
1275 while (!Lst_IsEmpty(curTargs)) {
1276 char *targName = Lst_Dequeue(curTargs);
1277 GNode *gn = Suff_IsTransform(targName)
1278 ? Suff_AddTransform(targName)
1279 : Targ_GetNode(targName);
1283 Lst_Append(targets, gn);
1288 ParseDoDependencyTargetExtraWarn(char **pp, const char *lstart)
1290 Boolean warning = FALSE;
1293 while (*cp && (ParseIsEscaped(lstart, cp) ||
1294 (*cp != '!' && *cp != ':'))) {
1295 if (ParseIsEscaped(lstart, cp) ||
1296 (*cp != ' ' && *cp != '\t')) {
1302 Parse_Error(PARSE_WARNING, "Extra target ignored");
1308 ParseDoDependencyCheckSpec(ParseSpecial specType)
1312 Parse_Error(PARSE_WARNING,
1313 "Special and mundane targets don't mix. Mundane ones ignored");
1322 * These four create nodes on which to hang commands, so
1323 * targets shouldn't be empty...
1327 * Nothing special here -- targets can be empty if it wants.
1334 ParseDoDependencyParseOp(char **pp, const char *lstart, GNodeType *out_op)
1336 const char *cp = *pp;
1346 *out_op = OP_DOUBLEDEP;
1349 *out_op = OP_DEPENDS;
1356 const char *msg = lstart[0] == '.' ? "Unknown directive"
1357 : "Missing dependency operator";
1358 Parse_Error(PARSE_FATAL, "%s", msg);
1364 ClearPaths(SearchPathList *paths)
1366 if (paths != NULL) {
1367 SearchPathListNode *ln;
1368 for (ln = paths->first; ln != NULL; ln = ln->next)
1369 Dir_ClearPath(ln->datum);
1376 ParseDoDependencySourcesEmpty(ParseSpecial specType, SearchPathList *paths)
1380 Suff_ClearSuffixes();
1386 opts.ignoreErrors = TRUE;
1389 opts.beSilent = TRUE;
1396 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1405 AddToPaths(const char *dir, SearchPathList *paths)
1407 if (paths != NULL) {
1408 SearchPathListNode *ln;
1409 for (ln = paths->first; ln != NULL; ln = ln->next)
1410 (void)Dir_AddDir(ln->datum, dir);
1415 * If the target was one that doesn't take files as its sources
1416 * but takes something like suffixes, we take each
1417 * space-separated word on the line as a something and deal
1418 * with it accordingly.
1420 * If the target was .SUFFIXES, we take each source as a
1421 * suffix and add it to the list of suffixes maintained by the
1424 * If the target was a .PATH, we add the source as a directory
1425 * to search on the search path.
1427 * If it was .INCLUDES, the source is taken to be the suffix of
1428 * files which will be #included and whose search path should
1429 * be present in the .INCLUDES variable.
1431 * If it was .LIBS, the source is taken to be the suffix of
1432 * files which are considered libraries and whose search path
1433 * should be present in the .LIBS variable.
1435 * If it was .NULL, the source is the suffix to use when a file
1436 * has no valid suffix.
1438 * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1439 * and will cause make to do a new chdir to that path.
1442 ParseDoDependencySourceSpecial(ParseSpecial specType, char *word,
1443 SearchPathList *paths)
1447 Suff_AddSuffix(word, &mainNode);
1450 AddToPaths(word, paths);
1453 Suff_AddInclude(word);
1462 Main_SetObjdir("%s", word);
1470 ParseDoDependencyTargets(char **inout_cp,
1473 ParseSpecial *inout_specType,
1474 GNodeType *inout_tOp,
1475 SearchPathList **inout_paths,
1476 StringList *curTargs)
1478 char *cp = *inout_cp;
1479 char *line = *inout_line;
1484 * Here LINE points to the beginning of the next word, and
1485 * LSTART points to the actual beginning of the line.
1488 /* Find the end of the next word. */
1490 ParseDependencyTargetWord(&cp, lstart);
1493 * If the word is followed by a left parenthesis, it's the
1494 * name of an object file inside an archive (ar file).
1496 if (!ParseIsEscaped(lstart, cp) && *cp == '(') {
1498 * Archives must be handled specially to make sure the OP_ARCHV
1499 * flag is set in their 'type' field, for one thing, and because
1500 * things like "archive(file1.o file2.o file3.o)" are permissible.
1501 * Arch_ParseArchive will set 'line' to be the first non-blank
1502 * after the archive-spec. It creates/finds nodes for the members
1503 * and places them on the given list, returning TRUE if all
1504 * went well and FALSE if there was an error in the
1505 * specification. On error, line should remain untouched.
1507 if (!Arch_ParseArchive(&line, targets, VAR_CMDLINE)) {
1508 Parse_Error(PARSE_FATAL,
1509 "Error in archive specification: \"%s\"", line);
1512 /* Done with this word; on to the next. */
1519 ParseErrorNoDependency(lstart);
1523 /* Insert a null terminator. */
1527 if (!ParseDoDependencyTarget(line, inout_specType, inout_tOp,
1532 * Have word in line. Get or create its node and stick it at
1533 * the end of the targets list
1535 if (*inout_specType == SP_NOT && *line != '\0') {
1536 ParseDoDependencyTargetMundane(line, curTargs);
1537 } else if (*inout_specType == SP_PATH && *line != '.' && *line != '\0') {
1538 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
1541 /* Don't need the inserted null terminator any more. */
1545 * If it is a special type and not .PATH, it's the only target we
1546 * allow on this line...
1548 if (*inout_specType != SP_NOT && *inout_specType != SP_PATH) {
1549 ParseDoDependencyTargetExtraWarn(&cp, lstart);
1551 pp_skip_whitespace(&cp);
1556 if ((*line == '!' || *line == ':') && !ParseIsEscaped(lstart, line))
1566 ParseDoDependencySourcesSpecial(char *start, char *end,
1567 ParseSpecial specType, SearchPathList *paths)
1572 while (*end && !ch_isspace(*end))
1576 ParseDoDependencySourceSpecial(specType, start, paths);
1580 pp_skip_whitespace(&end);
1586 ParseDoDependencySourcesMundane(char *start, char *end,
1587 ParseSpecial specType, GNodeType tOp)
1591 * The targets take real sources, so we must beware of archive
1592 * specifications (i.e. things with left parentheses in them)
1593 * and handle them accordingly.
1595 for (; *end && !ch_isspace(*end); end++) {
1596 if (*end == '(' && end > start && end[-1] != '$') {
1598 * Only stop for a left parenthesis if it isn't at the
1599 * start of a word (that'll be for variable changes
1600 * later) and isn't preceded by a dollar sign (a dynamic
1608 GNodeList *sources = Lst_New();
1609 if (!Arch_ParseArchive(&start, sources, VAR_CMDLINE)) {
1610 Parse_Error(PARSE_FATAL,
1611 "Error in source archive spec \"%s\"", start);
1615 while (!Lst_IsEmpty(sources)) {
1616 GNode *gn = Lst_Dequeue(sources);
1617 ParseDoSrc(tOp, gn->name, specType);
1627 ParseDoSrc(tOp, start, specType);
1629 pp_skip_whitespace(&end);
1635 /* Parse a dependency line consisting of targets, followed by a dependency
1636 * operator, optionally followed by sources.
1638 * The nodes of the sources are linked as children to the nodes of the
1639 * targets. Nodes are created as necessary.
1641 * The operator is applied to each node in the global 'targets' list,
1642 * which is where the nodes found for the targets are kept, by means of
1643 * the ParseDoOp function.
1645 * The sources are parsed in much the same way as the targets, except
1646 * that they are expanded using the wildcarding scheme of the C-Shell,
1647 * and all instances of the resulting words in the list of all targets
1648 * are found. Each of the resulting nodes is then linked to each of the
1649 * targets as one of its children.
1651 * Certain targets and sources such as .PHONY or .PRECIOUS are handled
1652 * specially. These are the ones detailed by the specType variable.
1654 * The storing of transformation rules such as '.c.o' is also taken care of
1655 * here. A target is recognized as a transformation rule by calling
1656 * Suff_IsTransform. If it is a transformation rule, its node is gotten
1657 * from the suffix module via Suff_AddTransform rather than the standard
1658 * Targ_FindNode in the target module.
1661 ParseDoDependency(char *line)
1663 char *cp; /* our current position */
1664 GNodeType op; /* the operator on the line */
1665 SearchPathList *paths; /* search paths to alter when parsing
1666 * a list of .PATH targets */
1667 int tOp; /* operator from special target */
1668 StringList *curTargs; /* target names to be found and added
1669 * to the targets list */
1670 char *lstart = line;
1673 * specType contains the SPECial TYPE of the current target. It is SP_NOT
1674 * if the target is unspecial. If it *is* special, however, the children
1675 * are linked as children of the parent but not vice versa.
1677 ParseSpecial specType = SP_NOT;
1679 DEBUG1(PARSE, "ParseDoDependency(%s)\n", line);
1684 curTargs = Lst_New();
1687 * First, grind through the targets.
1689 if (!ParseDoDependencyTargets(&cp, &line, lstart, &specType, &tOp, &paths,
1694 * Don't need the list of target names anymore...
1699 if (!Lst_IsEmpty(targets))
1700 ParseDoDependencyCheckSpec(specType);
1703 * Have now parsed all the target names. Must parse the operator next.
1705 if (!ParseDoDependencyParseOp(&cp, lstart, &op))
1709 * Apply the operator to the target. This is how we remember which
1710 * operator a target was defined with. It fails if the operator
1711 * used isn't consistent across all references.
1713 ApplyDependencyOperator(op);
1716 * Onward to the sources.
1718 * LINE will now point to the first source word, if any, or the
1719 * end of the string if not.
1721 pp_skip_whitespace(&cp);
1725 * Several special targets take different actions if present with no
1727 * a .SUFFIXES line with no sources clears out all old suffixes
1728 * a .PRECIOUS line makes all targets precious
1729 * a .IGNORE line ignores errors for all targets
1730 * a .SILENT line creates silence when making all targets
1731 * a .PATH removes all directories from the search path(s).
1734 ParseDoDependencySourcesEmpty(specType, paths);
1735 } else if (specType == SP_MFLAGS) {
1737 * Call on functions in main.c to deal with these arguments and
1738 * set the initial character to a null-character so the loop to
1739 * get sources won't get anything
1741 Main_ParseArgLine(line);
1743 } else if (specType == SP_SHELL) {
1744 if (!Job_ParseShell(line)) {
1745 Parse_Error(PARSE_FATAL, "improper shell specification");
1749 } else if (specType == SP_NOTPARALLEL || specType == SP_SINGLESHELL ||
1750 specType == SP_DELETE_ON_ERROR) {
1755 * NOW GO FOR THE SOURCES
1757 if (specType == SP_SUFFIXES || specType == SP_PATH ||
1758 specType == SP_INCLUDES || specType == SP_LIBS ||
1759 specType == SP_NULL || specType == SP_OBJDIR)
1761 ParseDoDependencySourcesSpecial(line, cp, specType, paths);
1766 if (specType == SP_PATH)
1769 assert(paths == NULL);
1770 if (!ParseDoDependencySourcesMundane(line, cp, specType, tOp))
1779 if (curTargs != NULL)
1783 typedef struct VarAssignParsed {
1784 const char *nameStart; /* unexpanded */
1785 const char *nameEnd; /* before operator adjustment */
1786 const char *eq; /* the '=' of the assignment operator */
1789 /* Determine the assignment operator and adjust the end of the variable
1790 * name accordingly. */
1792 AdjustVarassignOp(const VarAssignParsed *pvar, const char *value,
1795 const char *op = pvar->eq;
1796 const char * const name = pvar->nameStart;
1799 if (op > name && op[-1] == '+') {
1803 } else if (op > name && op[-1] == '?') {
1807 } else if (op > name && op[-1] == ':') {
1811 } else if (op > name && op[-1] == '!') {
1818 while (op > name && ch_isspace(op[-1]))
1821 if (op >= name + 3 && op[-3] == ':' && op[-2] == 's' && op[-1] == 'h') {
1829 const char *nameEnd = pvar->nameEnd < op ? pvar->nameEnd : op;
1830 out_var->varname = bmake_strsedup(pvar->nameStart, nameEnd);
1832 out_var->value = value;
1836 /* Parse a variable assignment, consisting of a single-word variable name,
1837 * optional whitespace, an assignment operator, optional whitespace and the
1840 * Note: There is a lexical ambiguity with assignment modifier characters
1841 * in variable names. This routine interprets the character before the =
1842 * as a modifier. Therefore, an assignment like
1844 * is interpreted as "C+ +=" instead of "C++ =".
1846 * Used for both lines in a file and command line arguments. */
1848 Parse_IsVar(const char *p, VarAssign *out_var)
1850 VarAssignParsed pvar;
1851 const char *firstSpace = NULL;
1855 /* Skip to variable name */
1856 while (*p == ' ' || *p == '\t')
1859 /* During parsing, the '+' of the '+=' operator is initially parsed
1860 * as part of the variable name. It is later corrected, as is the ':sh'
1861 * modifier. Of these two (nameEnd and op), the earlier one determines the
1862 * actual end of the variable name. */
1865 pvar.nameEnd = NULL;
1869 /* Scan for one of the assignment operators outside a variable expansion */
1870 while ((ch = *p++) != 0) {
1871 if (ch == '(' || ch == '{') {
1875 if (ch == ')' || ch == '}') {
1883 if (ch == ' ' || ch == '\t')
1884 if (firstSpace == NULL)
1886 while (ch == ' ' || ch == '\t')
1890 if (ch == ':' && strncmp(p, "sh", 2) == 0) {
1897 pvar.nameEnd = firstSpace != NULL ? firstSpace : p - 1;
1898 cpp_skip_whitespace(&p);
1899 AdjustVarassignOp(&pvar, p, out_var);
1902 if (*p == '=' && (ch == '+' || ch == ':' || ch == '?' || ch == '!')) {
1904 pvar.nameEnd = firstSpace != NULL ? firstSpace : p;
1906 cpp_skip_whitespace(&p);
1907 AdjustVarassignOp(&pvar, p, out_var);
1910 if (firstSpace != NULL)
1918 VarCheckSyntax(VarAssignOp type, const char *uvalue, GNode *ctxt)
1921 if (type != VAR_SUBST && strchr(uvalue, '$') != NULL) {
1922 /* Check for syntax errors such as unclosed expressions or
1923 * unknown modifiers. */
1924 char *expandedValue;
1926 (void)Var_Subst(uvalue, ctxt, VARE_NONE, &expandedValue);
1927 /* TODO: handle errors */
1928 free(expandedValue);
1934 VarAssign_EvalSubst(const char *name, const char *uvalue, GNode *ctxt,
1935 const char **out_avalue, void **out_avalue_freeIt)
1937 const char *avalue = uvalue;
1940 * Allow variables in the old value to be undefined, but leave their
1941 * expressions alone -- this is done by forcing oldVars to be false.
1942 * XXX: This can cause recursive variables, but that's not hard to do,
1943 * and this allows someone to do something like
1945 * CFLAGS = $(.INCLUDES)
1946 * CFLAGS := -I.. $(CFLAGS)
1948 * And not get an error.
1950 Boolean oldOldVars = oldVars;
1955 * make sure that we set the variable the first time to nothing
1956 * so that it gets substituted!
1958 if (!Var_Exists(name, ctxt))
1959 Var_Set(name, "", ctxt);
1961 (void)Var_Subst(uvalue, ctxt, VARE_WANTRES|VARE_ASSIGN, &evalue);
1962 /* TODO: handle errors */
1963 oldVars = oldOldVars;
1965 Var_Set(name, avalue, ctxt);
1967 *out_avalue = avalue;
1968 *out_avalue_freeIt = evalue;
1972 VarAssign_EvalShell(const char *name, const char *uvalue, GNode *ctxt,
1973 const char **out_avalue, void **out_avalue_freeIt)
1975 const char *cmd, *errfmt;
1977 void *cmd_freeIt = NULL;
1980 if (strchr(cmd, '$') != NULL) {
1982 (void)Var_Subst(cmd, VAR_CMDLINE, VARE_UNDEFERR | VARE_WANTRES, &ecmd);
1983 /* TODO: handle errors */
1984 cmd = cmd_freeIt = ecmd;
1987 cmdOut = Cmd_Exec(cmd, &errfmt);
1988 Var_Set(name, cmdOut, ctxt);
1989 *out_avalue = *out_avalue_freeIt = cmdOut;
1992 Parse_Error(PARSE_WARNING, errfmt, cmd);
1997 /* Perform a variable assignment.
1999 * The actual value of the variable is returned in *out_avalue and
2000 * *out_avalue_freeIt. Especially for VAR_SUBST and VAR_SHELL this can differ
2001 * from the literal value.
2003 * Return whether the assignment was actually done. The assignment is only
2004 * skipped if the operator is '?=' and the variable already exists. */
2006 VarAssign_Eval(const char *name, VarAssignOp op, const char *uvalue,
2007 GNode *ctxt, const char **out_avalue, void **out_avalue_freeIt)
2009 const char *avalue = uvalue;
2010 void *avalue_freeIt = NULL;
2012 if (op == VAR_APPEND) {
2013 Var_Append(name, uvalue, ctxt);
2014 } else if (op == VAR_SUBST) {
2015 VarAssign_EvalSubst(name, uvalue, ctxt, &avalue, &avalue_freeIt);
2016 } else if (op == VAR_SHELL) {
2017 VarAssign_EvalShell(name, uvalue, ctxt, &avalue, &avalue_freeIt);
2019 if (op == VAR_DEFAULT && Var_Exists(name, ctxt)) {
2020 *out_avalue_freeIt = NULL;
2024 /* Normal assignment -- just do it. */
2025 Var_Set(name, uvalue, ctxt);
2028 *out_avalue = avalue;
2029 *out_avalue_freeIt = avalue_freeIt;
2034 VarAssignSpecial(const char *name, const char *avalue)
2036 if (strcmp(name, MAKEOVERRIDES) == 0)
2037 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */
2038 else if (strcmp(name, ".CURDIR") == 0) {
2040 * Someone is being (too?) clever...
2041 * Let's pretend they know what they are doing and
2042 * re-initialize the 'cur' CachedDir.
2044 Dir_InitCur(avalue);
2046 } else if (strcmp(name, MAKE_JOB_PREFIX) == 0) {
2048 } else if (strcmp(name, MAKE_EXPORTED) == 0) {
2049 Var_Export(avalue, FALSE);
2053 /* Perform the variable variable assignment in the given context. */
2055 Parse_DoVar(VarAssign *var, GNode *ctxt)
2057 const char *avalue; /* actual value (maybe expanded) */
2058 void *avalue_freeIt;
2060 VarCheckSyntax(var->op, var->value, ctxt);
2061 if (VarAssign_Eval(var->varname, var->op, var->value, ctxt,
2062 &avalue, &avalue_freeIt))
2063 VarAssignSpecial(var->varname, avalue);
2065 free(avalue_freeIt);
2071 * ParseMaybeSubMake --
2072 * Scan the command string to see if it a possible submake node
2074 * cmd the command to scan
2076 * TRUE if the command is possibly a submake, FALSE if not.
2079 ParseMaybeSubMake(const char *cmd)
2086 #define MKV(A) { A, sizeof(A) - 1 }
2093 for (i = 0; i < sizeof vals / sizeof vals[0]; i++) {
2095 if ((ptr = strstr(cmd, vals[i].name)) == NULL)
2097 if ((ptr == cmd || !ch_isalnum(ptr[-1]))
2098 && !ch_isalnum(ptr[vals[i].len]))
2104 /* Append the command to the target node.
2106 * The node may be marked as a submake node if the command is determined to
2109 ParseAddCmd(GNode *gn, char *cmd)
2111 /* Add to last (ie current) cohort for :: targets */
2112 if ((gn->type & OP_DOUBLEDEP) && gn->cohorts->last != NULL)
2113 gn = gn->cohorts->last->datum;
2115 /* if target already supplied, ignore commands */
2116 if (!(gn->type & OP_HAS_COMMANDS)) {
2117 Lst_Append(gn->commands, cmd);
2118 if (ParseMaybeSubMake(cmd))
2119 gn->type |= OP_SUBMAKE;
2123 /* XXX: We cannot do this until we fix the tree */
2124 Lst_Append(gn->commands, cmd);
2125 Parse_Error(PARSE_WARNING,
2126 "overriding commands for target \"%s\"; "
2127 "previous commands defined at %s: %d ignored",
2128 gn->name, gn->fname, gn->lineno);
2130 Parse_Error(PARSE_WARNING,
2131 "duplicate script for target \"%s\" ignored",
2133 ParseErrorInternal(gn->fname, (size_t)gn->lineno, PARSE_WARNING,
2134 "using previous script for \"%s\" defined here",
2140 /* Add a directory to the path searched for included makefiles bracketed
2141 * by double-quotes. */
2143 Parse_AddIncludeDir(const char *dir)
2145 (void)Dir_AddDir(parseIncPath, dir);
2148 /* Push to another file.
2150 * The input is the line minus the '.'. A file spec is a string enclosed in
2151 * <> or "". The <> file is looked for only in sysIncPath. The "" file is
2152 * first searched in the parsedir and then in the directories specified by
2153 * the -I command line options.
2156 Parse_include_file(char *file, Boolean isSystem, Boolean depinc, int silent)
2158 struct loadedfile *lf;
2159 char *fullname; /* full pathname of file */
2161 char *prefEnd, *incdir;
2166 * Now we know the file's name and its search path, we attempt to
2167 * find the durn thing. A return of NULL indicates the file don't
2170 fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
2172 if (fullname == NULL && !isSystem) {
2174 * Include files contained in double-quotes are first searched for
2175 * relative to the including file's location. We don't want to
2176 * cd there, of course, so we just tack on the old file's
2177 * leading path components and call Dir_FindFile to see if
2178 * we can locate the beast.
2181 incdir = bmake_strdup(CurFile()->fname);
2182 prefEnd = strrchr(incdir, '/');
2183 if (prefEnd != NULL) {
2185 /* Now do lexical processing of leading "../" on the filename */
2186 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
2187 prefEnd = strrchr(incdir + 1, '/');
2188 if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0)
2192 newName = str_concat3(incdir, "/", file + i);
2193 fullname = Dir_FindFile(newName, parseIncPath);
2194 if (fullname == NULL)
2195 fullname = Dir_FindFile(newName, dirSearchPath);
2200 if (fullname == NULL) {
2202 * Makefile wasn't found in same directory as included makefile.
2203 * Search for it first on the -I search path,
2204 * then on the .PATH search path, if not found in a -I directory.
2205 * If we have a suffix specific path we should use that.
2208 SearchPath *suffPath = NULL;
2210 if ((suff = strrchr(file, '.'))) {
2211 suffPath = Suff_GetPath(suff);
2212 if (suffPath != NULL) {
2213 fullname = Dir_FindFile(file, suffPath);
2216 if (fullname == NULL) {
2217 fullname = Dir_FindFile(file, parseIncPath);
2218 if (fullname == NULL) {
2219 fullname = Dir_FindFile(file, dirSearchPath);
2225 /* Looking for a system file or file still not found */
2226 if (fullname == NULL) {
2228 * Look for it on the system path
2230 SearchPath *path = Lst_IsEmpty(sysIncPath) ? defSysIncPath : sysIncPath;
2231 fullname = Dir_FindFile(file, path);
2234 if (fullname == NULL) {
2236 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2240 /* Actually open the file... */
2241 fd = open(fullname, O_RDONLY);
2244 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
2250 lf = loadfile(fullname, fd);
2252 /* Start reading from this file next */
2253 Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf);
2256 doing_depend = depinc; /* only turn it on */
2260 ParseDoInclude(char *line)
2262 char endc; /* the character which ends the file spec */
2263 char *cp; /* current position in file spec */
2264 int silent = *line != 'i';
2265 char *file = line + (silent ? 8 : 7);
2267 /* Skip to delimiter character so we know where to look */
2268 while (*file == ' ' || *file == '\t')
2271 if (*file != '"' && *file != '<') {
2272 Parse_Error(PARSE_FATAL,
2273 ".include filename must be delimited by '\"' or '<'");
2278 * Set the search path on which to find the include file based on the
2279 * characters which bracket its name. Angle-brackets imply it's
2280 * a system Makefile while double-quotes imply it's a user makefile
2288 /* Skip to matching delimiter */
2289 for (cp = ++file; *cp && *cp != endc; cp++)
2293 Parse_Error(PARSE_FATAL,
2294 "Unclosed %cinclude filename. '%c' expected",
2301 * Substitute for any variables in the file name before trying to
2304 (void)Var_Subst(file, VAR_CMDLINE, VARE_WANTRES, &file);
2305 /* TODO: handle errors */
2307 Parse_include_file(file, endc == '>', *line == 'd', silent);
2311 /* Split filename into dirname + basename, then assign these to the
2312 * given variables. */
2314 SetFilenameVars(const char *filename, const char *dirvar, const char *filevar)
2316 const char *slash, *dirname, *basename;
2319 slash = strrchr(filename, '/');
2320 if (slash == NULL) {
2322 basename = filename;
2325 dirname = freeIt = bmake_strsedup(filename, slash);
2326 basename = slash + 1;
2329 Var_Set(dirvar, dirname, VAR_GLOBAL);
2330 Var_Set(filevar, basename, VAR_GLOBAL);
2332 DEBUG5(PARSE, "%s: ${%s} = `%s' ${%s} = `%s'\n",
2333 __func__, dirvar, dirname, filevar, basename);
2337 /* Return the immediately including file.
2339 * This is made complicated since the .for loop is implemented as a special
2340 * kind of .include; see For_Run. */
2342 GetActuallyIncludingFile(void)
2345 const IFile *incs = GetInclude(0);
2347 for (i = includes.len; i >= 2; i--)
2348 if (!incs[i - 1].fromForLoop)
2349 return incs[i - 2].fname;
2353 /* Set .PARSEDIR, .PARSEFILE, .INCLUDEDFROMDIR and .INCLUDEDFROMFILE. */
2355 ParseSetParseFile(const char *filename)
2357 const char *including;
2359 SetFilenameVars(filename, ".PARSEDIR", ".PARSEFILE");
2361 including = GetActuallyIncludingFile();
2362 if (including != NULL) {
2363 SetFilenameVars(including,
2364 ".INCLUDEDFROMDIR", ".INCLUDEDFROMFILE");
2366 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
2367 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
2372 StrContainsWord(const char *str, const char *word)
2374 size_t strLen = strlen(str);
2375 size_t wordLen = strlen(word);
2376 const char *p, *end;
2378 if (strLen < wordLen)
2379 return FALSE; /* str is too short to contain word */
2381 end = str + strLen - wordLen;
2382 for (p = str; p != NULL; p = strchr(p, ' ')) {
2386 return FALSE; /* cannot contain word */
2388 if (memcmp(p, word, wordLen) == 0 &&
2389 (p[wordLen] == '\0' || p[wordLen] == ' '))
2395 /* XXX: Searching through a set of words with this linear search is
2396 * inefficient for variables that contain thousands of words. */
2398 VarContainsWord(const char *varname, const char *word)
2401 const char *val = Var_Value(varname, VAR_GLOBAL, &val_freeIt);
2402 Boolean found = val != NULL && StrContainsWord(val, word);
2403 bmake_free(val_freeIt);
2407 /* Track the makefiles we read - so makefiles can set dependencies on them.
2408 * Avoid adding anything more than once. */
2410 ParseTrackInput(const char *name)
2412 if (!VarContainsWord(MAKE_MAKEFILES, name))
2413 Var_Append(MAKE_MAKEFILES, name, VAR_GLOBAL);
2417 /* Start Parsing from the given source.
2419 * The given file is added to the includes stack. */
2421 Parse_SetInput(const char *name, int line, int fd,
2422 char *(*nextbuf)(void *, size_t *), void *arg)
2427 Boolean fromForLoop = name == NULL;
2430 name = CurFile()->fname;
2432 ParseTrackInput(name);
2435 debug_printf("%s: file %s, line %d, fd %d, nextbuf %s, arg %p\n",
2436 __func__, name, line, fd,
2437 nextbuf == loadedfile_nextbuf ? "loadedfile" : "other",
2440 if (fd == -1 && nextbuf == NULL)
2444 curFile = Vector_Push(&includes);
2447 * Once the previous state has been saved, we can get down to reading
2448 * the new file. We set up the name of the file to be the absolute
2449 * name of the include file so error messages refer to the right
2452 curFile->fname = bmake_strdup(name);
2453 curFile->fromForLoop = fromForLoop;
2454 curFile->lineno = line;
2455 curFile->first_lineno = line;
2456 curFile->nextbuf = nextbuf;
2457 curFile->nextbuf_arg = arg;
2459 curFile->depending = doing_depend; /* restore this on EOF */
2461 assert(nextbuf != NULL);
2463 /* Get first block of input data */
2464 buf = curFile->nextbuf(curFile->nextbuf_arg, &len);
2466 /* Was all a waste of time ... */
2468 free(curFile->fname);
2472 curFile->buf_freeIt = buf;
2473 curFile->buf_ptr = buf;
2474 curFile->buf_end = buf + len;
2476 curFile->cond_depth = Cond_save_depth();
2477 ParseSetParseFile(name);
2480 /* Check if the directive is an include directive. */
2482 IsInclude(const char *dir, Boolean sysv)
2484 if (dir[0] == 's' || dir[0] == '-' || (dir[0] == 'd' && !sysv))
2487 if (strncmp(dir, "include", 7) != 0)
2490 /* Space is not mandatory for BSD .include */
2491 return !sysv || ch_isspace(dir[7]);
2496 /* Check if the line is a SYSV include directive. */
2498 IsSysVInclude(const char *line)
2502 if (!IsInclude(line, TRUE))
2505 /* Avoid interpreting a dependency line as an include */
2506 for (p = line; (p = strchr(p, ':')) != NULL;) {
2508 /* end of line -> dependency */
2511 if (*p == ':' || ch_isspace(*p)) {
2512 /* :: operator or ': ' -> dependency */
2519 /* Push to another file. The line points to the word "include". */
2521 ParseTraditionalInclude(char *line)
2523 char *cp; /* current position in file spec */
2525 int silent = line[0] != 'i';
2526 char *file = line + (silent ? 8 : 7);
2529 DEBUG2(PARSE, "%s: %s\n", __func__, file);
2531 pp_skip_whitespace(&file);
2534 * Substitute for any variables in the file name before trying to
2537 (void)Var_Subst(file, VAR_CMDLINE, VARE_WANTRES, &all_files);
2538 /* TODO: handle errors */
2540 if (*file == '\0') {
2541 Parse_Error(PARSE_FATAL, "Filename missing from \"include\"");
2545 for (file = all_files; !done; file = cp + 1) {
2546 /* Skip to end of line or next whitespace */
2547 for (cp = file; *cp && !ch_isspace(*cp); cp++)
2555 Parse_include_file(file, FALSE, FALSE, silent);
2563 /* Parse "export <variable>=<value>", and actually export it. */
2565 ParseGmakeExport(char *line)
2567 char *variable = line + 6;
2570 DEBUG2(PARSE, "%s: %s\n", __func__, variable);
2572 pp_skip_whitespace(&variable);
2574 for (value = variable; *value && *value != '='; value++)
2577 if (*value != '=') {
2578 Parse_Error(PARSE_FATAL,
2579 "Variable/Value missing from \"export\"");
2582 *value++ = '\0'; /* terminate variable */
2585 * Expand the value before putting it in the environment.
2587 (void)Var_Subst(value, VAR_CMDLINE, VARE_WANTRES, &value);
2588 /* TODO: handle errors */
2590 setenv(variable, value, 1);
2595 /* Called when EOF is reached in the current file. If we were reading an
2596 * include file, the includes stack is popped and things set up to go back
2597 * to reading the previous file at the previous location.
2600 * TRUE to continue parsing, i.e. it had only reached the end of an
2601 * included file, FALSE if the main file has been parsed completely.
2608 IFile *curFile = CurFile();
2610 assert(curFile->nextbuf != NULL);
2612 doing_depend = curFile->depending; /* restore this */
2613 /* get next input buffer, if any */
2614 ptr = curFile->nextbuf(curFile->nextbuf_arg, &len);
2615 curFile->buf_ptr = ptr;
2616 curFile->buf_freeIt = ptr;
2617 curFile->buf_end = ptr + len;
2618 curFile->lineno = curFile->first_lineno;
2624 /* Ensure the makefile (or loop) didn't have mismatched conditionals */
2625 Cond_restore_depth(curFile->cond_depth);
2627 if (curFile->lf != NULL) {
2628 loadedfile_destroy(curFile->lf);
2632 /* Dispose of curFile info */
2633 /* Leak curFile->fname because all the gnodes have pointers to it */
2634 free(curFile->buf_freeIt);
2635 Vector_Pop(&includes);
2637 if (includes.len == 0) {
2638 /* We've run out of input */
2639 Var_Delete(".PARSEDIR", VAR_GLOBAL);
2640 Var_Delete(".PARSEFILE", VAR_GLOBAL);
2641 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
2642 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
2646 curFile = CurFile();
2647 DEBUG2(PARSE, "ParseEOF: returning to file %s, line %d\n",
2648 curFile->fname, curFile->lineno);
2650 ParseSetParseFile(curFile->fname);
2655 #define PARSE_SKIP 2
2658 ParseGetLine(int flags)
2660 IFile *cf = CurFile();
2669 /* Loop through blank lines and comment lines */
2678 /* XXX: can buf_end ever be null? */
2679 if (cf->buf_end != NULL && ptr == cf->buf_end) {
2685 if (ch == 0 || (ch == '\\' && ptr[1] == 0)) {
2686 /* XXX: can buf_end ever be null? */
2687 if (cf->buf_end == NULL)
2688 /* End of string (aka for loop) data */
2690 /* see if there is more we can parse */
2691 while (ptr++ < cf->buf_end) {
2692 if ((ch = *ptr) == '\n') {
2693 if (ptr > line && ptr[-1] == '\\')
2695 Parse_Error(PARSE_WARNING,
2696 "Zero byte read from file, "
2697 "skipping rest of line.");
2701 if (cf->nextbuf != NULL) {
2703 * End of this buffer; return EOF and outer logic
2704 * will get the next one. (eww)
2708 Parse_Error(PARSE_FATAL, "Zero byte read from file");
2713 /* Don't treat next character as special, remember first one */
2714 if (escaped == NULL)
2722 if (ch == '#' && comment == NULL) {
2723 /* Remember first '#' for comment stripping */
2724 /* Unless previous char was '[', as in modifier :[#] */
2725 if (!(ptr > line && ptr[-1] == '['))
2731 if (!ch_isspace(ch))
2732 /* We are not interested in trailing whitespace */
2736 /* Save next 'to be processed' location */
2739 /* Check we have a non-comment, non-blank line */
2740 if (line_end == line || comment == line) {
2742 /* At end of file */
2744 /* Parse another line */
2748 /* We now have a line of data */
2751 if (flags & PARSE_RAW) {
2752 /* Leave '\' (etc) in line buffer (eg 'for' lines) */
2756 if (flags & PARSE_SKIP) {
2757 /* Completely ignore non-directives */
2760 /* We could do more of the .else/.elif/.endif checks here */
2765 /* Brutally ignore anything after a non-escaped '#' in non-commands */
2766 if (comment != NULL && line[0] != '\t') {
2771 /* If we didn't see a '\\' then the in-situ data is fine */
2772 if (escaped == NULL)
2775 /* Remove escapes from '\n' and '#' */
2778 for (; ; *tp++ = ch) {
2788 /* Delete '\\' at end of buffer */
2793 if (ch == '#' && line[0] != '\t')
2794 /* Delete '\\' from before '#' on non-command lines */
2798 /* Leave '\\' in buffer for later */
2800 /* Make sure we don't delete an escaped ' ' from the line end */
2805 /* Escaped '\n' replace following whitespace with a single ' ' */
2806 while (ptr[0] == ' ' || ptr[0] == '\t')
2811 /* Delete any trailing spaces - eg from empty continuations */
2812 while (tp > escaped && ch_isspace(tp[-1]))
2819 /* Read an entire line from the input file. Called only by Parse_File.
2822 * A line without its newline.
2825 * Only those associated with reading a character
2830 char *line; /* Result */
2831 int lineno; /* Saved line # */
2835 line = ParseGetLine(0);
2843 * The line might be a conditional. Ask the conditional module
2844 * about it and act accordingly
2846 switch (Cond_EvalLine(line)) {
2848 /* Skip to next conditional that evaluates to COND_PARSE. */
2850 line = ParseGetLine(PARSE_SKIP);
2851 } while (line && Cond_EvalLine(line) != COND_PARSE);
2857 case COND_INVALID: /* Not a conditional line */
2858 /* Check for .for loops */
2859 rval = For_Eval(line);
2861 /* Not a .for line */
2864 /* Syntax error - error printed, ignore line */
2866 /* Start of a .for loop */
2867 lineno = CurFile()->lineno;
2868 /* Accumulate loop lines until matching .endfor */
2870 line = ParseGetLine(PARSE_RAW);
2872 Parse_Error(PARSE_FATAL,
2873 "Unexpected end of file in for loop.");
2876 } while (For_Accum(line));
2877 /* Stash each iteration as a new 'input file' */
2879 /* Read next line from for-loop buffer */
2887 FinishDependencyGroup(void)
2889 if (targets != NULL) {
2891 for (ln = targets->first; ln != NULL; ln = ln->next) {
2892 GNode *gn = ln->datum;
2894 Suff_EndTransform(gn);
2896 /* Mark the target as already having commands if it does, to
2897 * keep from having shell commands on multiple dependency lines. */
2898 if (!Lst_IsEmpty(gn->commands))
2899 gn->type |= OP_HAS_COMMANDS;
2907 /* Add the command to each target from the current dependency spec. */
2909 ParseLine_ShellCommand(const char *p)
2911 cpp_skip_whitespace(&p);
2913 return; /* skip empty commands */
2915 if (targets == NULL) {
2916 Parse_Error(PARSE_FATAL, "Unassociated shell command \"%s\"", p);
2921 char *cmd = bmake_strdup(p);
2924 for (ln = targets->first; ln != NULL; ln = ln->next) {
2925 GNode *gn = ln->datum;
2926 ParseAddCmd(gn, cmd);
2929 Lst_Append(targCmds, cmd);
2935 ParseDirective(char *line)
2941 * Lines that begin with the special character may be
2942 * include or undef directives.
2943 * On the other hand they can be suffix rules (.c.o: ...)
2944 * or just dependencies for filenames that start '.'.
2947 pp_skip_whitespace(&cp);
2948 if (IsInclude(cp, FALSE)) {
2952 if (strncmp(cp, "undef", 5) == 0) {
2953 const char *varname;
2955 pp_skip_whitespace(&cp);
2957 for (; !ch_isspace(*cp) && *cp != '\0'; cp++)
2960 Var_Delete(varname, VAR_GLOBAL);
2961 /* TODO: undefine all variables, not only the first */
2962 /* TODO: use Str_Words, like everywhere else */
2964 } else if (strncmp(cp, "export", 6) == 0) {
2966 pp_skip_whitespace(&cp);
2967 Var_Export(cp, TRUE);
2969 } else if (strncmp(cp, "unexport", 8) == 0) {
2972 } else if (strncmp(cp, "info", 4) == 0 ||
2973 strncmp(cp, "error", 5) == 0 ||
2974 strncmp(cp, "warning", 7) == 0) {
2975 if (ParseMessage(cp))
2983 ParseVarassign(const char *line)
2986 if (Parse_IsVar(line, &var)) {
2987 FinishDependencyGroup();
2988 Parse_DoVar(&var, VAR_GLOBAL);
2995 FindSemicolon(char *p)
2999 for (; *p != '\0'; p++) {
3000 if (*p == '\\' && p[1] != '\0') {
3005 if (*p == '$' && (p[1] == '(' || p[1] == '{')) {
3010 if (level > 0 && (*p == ')' || *p == '}')) {
3015 if (level == 0 && *p == ';') {
3022 /* dependency -> target... op [source...]
3023 * op -> ':' | '::' | '!' */
3025 ParseDependency(char *line)
3027 VarEvalFlags eflags;
3028 char *expanded_line;
3029 const char *shellcmd = NULL;
3032 * For some reason - probably to make the parser impossible -
3033 * a ';' can be used to separate commands from dependencies.
3034 * Attempt to avoid ';' inside substitution patterns.
3037 char *semicolon = FindSemicolon(line);
3038 if (*semicolon != '\0') {
3039 /* Terminate the dependency list at the ';' */
3041 shellcmd = semicolon + 1;
3046 * We now know it's a dependency line so it needs to have all
3047 * variables expanded before being parsed.
3049 * XXX: Ideally the dependency line would first be split into
3050 * its left-hand side, dependency operator and right-hand side,
3051 * and then each side would be expanded on its own. This would
3052 * allow for the left-hand side to allow only defined variables
3053 * and to allow variables on the right-hand side to be undefined
3056 * Parsing the line first would also prevent that targets
3057 * generated from variable expressions are interpreted as the
3058 * dependency operator, such as in "target${:U:} middle: source",
3059 * in which the middle is interpreted as a source, not a target.
3062 /* In lint mode, allow undefined variables to appear in
3065 * Ideally, only the right-hand side would allow undefined
3066 * variables since it is common to have no dependencies.
3067 * Having undefined variables on the left-hand side is more
3068 * unusual though. Since both sides are expanded in a single
3069 * pass, there is not much choice what to do here.
3071 * In normal mode, it does not matter whether undefined
3072 * variables are allowed or not since as of 2020-09-14,
3073 * Var_Parse does not print any parse errors in such a case.
3074 * It simply returns the special empty string var_Error,
3075 * which cannot be detected in the result of Var_Subst. */
3076 eflags = DEBUG(LINT) ? VARE_WANTRES : VARE_UNDEFERR | VARE_WANTRES;
3077 (void)Var_Subst(line, VAR_CMDLINE, eflags, &expanded_line);
3078 /* TODO: handle errors */
3080 /* Need a fresh list for the target nodes */
3081 if (targets != NULL)
3083 targets = Lst_New();
3085 ParseDoDependency(expanded_line);
3086 free(expanded_line);
3088 if (shellcmd != NULL)
3089 ParseLine_ShellCommand(shellcmd);
3093 ParseLine(char *line)
3095 if (ParseDirective(line))
3098 if (*line == '\t') {
3099 ParseLine_ShellCommand(line + 1);
3104 if (IsSysVInclude(line)) {
3106 * It's an S3/S5-style "include".
3108 ParseTraditionalInclude(line);
3114 if (strncmp(line, "export", 6) == 0 && ch_isspace(line[6]) &&
3115 strchr(line, ':') == NULL) {
3117 * It's a Gmake "export".
3119 ParseGmakeExport(line);
3124 if (ParseVarassign(line))
3127 FinishDependencyGroup();
3129 ParseDependency(line);
3132 /* Parse a top-level makefile into its component parts, incorporating them
3133 * into the global dependency graph.
3136 * name The name of the file being read
3137 * fd The open file to parse; will be closed at the end
3140 Parse_File(const char *name, int fd)
3142 char *line; /* the line we're working on */
3143 struct loadedfile *lf;
3145 lf = loadfile(name, fd);
3147 assert(targets == NULL);
3153 Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf);
3157 while ((line = ParseReadLine()) != NULL) {
3158 DEBUG2(PARSE, "ParseReadLine (%d): '%s'\n",
3159 CurFile()->lineno, line);
3163 * Reached EOF, but it may be just EOF of an include file...
3165 } while (ParseEOF());
3167 FinishDependencyGroup();
3170 (void)fflush(stdout);
3171 (void)fprintf(stderr,
3172 "%s: Fatal errors encountered -- cannot continue",
3174 PrintOnError(NULL, NULL);
3179 /* Initialize the parsing module. */
3184 parseIncPath = Lst_New();
3185 sysIncPath = Lst_New();
3186 defSysIncPath = Lst_New();
3187 Vector_Init(&includes, sizeof(IFile));
3189 targCmds = Lst_New();
3193 /* Clean up the parsing module. */
3198 Lst_Destroy(targCmds, free);
3199 assert(targets == NULL);
3200 Lst_Destroy(defSysIncPath, Dir_Destroy);
3201 Lst_Destroy(sysIncPath, Dir_Destroy);
3202 Lst_Destroy(parseIncPath, Dir_Destroy);
3203 assert(includes.len == 0);
3204 Vector_Done(&includes);
3210 *-----------------------------------------------------------------------
3212 * Return a Lst of the main target to create for main()'s sake. If
3213 * no such target exists, we Punt with an obnoxious error message.
3216 * A Lst of the single node to create.
3221 *-----------------------------------------------------------------------
3224 Parse_MainName(void)
3226 GNodeList *mainList;
3228 mainList = Lst_New();
3230 if (mainNode == NULL) {
3231 Punt("no target to make.");
3233 } else if (mainNode->type & OP_DOUBLEDEP) {
3234 Lst_Append(mainList, mainNode);
3235 Lst_AppendAll(mainList, mainNode->cohorts);
3237 Lst_Append(mainList, mainNode);
3238 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
3243 Parse_GetFatals(void)