1 /* $NetBSD: parse.c,v 1.560 2021/06/21 10:42:06 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 IncludeFile instead.
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.560 2021/06/21 10:42:06 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 (relative? absolute?) */
136 bool 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 bool 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 /* Function to read more data, with a single opaque argument. */
148 ReadMoreProc readMore;
151 struct loadedfile *lf; /* loadedfile object, if any */
155 * Tokens for target attributes
157 typedef enum ParseSpecial {
158 SP_ATTRIBUTE, /* Generic attribute */
159 SP_BEGIN, /* .BEGIN */
160 SP_DEFAULT, /* .DEFAULT */
161 SP_DELETE_ON_ERROR, /* .DELETE_ON_ERROR */
163 SP_ERROR, /* .ERROR */
164 SP_IGNORE, /* .IGNORE */
165 SP_INCLUDES, /* .INCLUDES; not mentioned in the manual page */
166 SP_INTERRUPT, /* .INTERRUPT */
167 SP_LIBS, /* .LIBS; not mentioned in the manual page */
168 /* .MAIN and we don't have anything user-specified to make */
171 SP_MFLAGS, /* .MFLAGS or .MAKEFLAGS */
172 SP_NOMETA, /* .NOMETA */
173 SP_NOMETA_CMP, /* .NOMETA_CMP */
174 SP_NOPATH, /* .NOPATH */
175 SP_NOT, /* Not special */
176 SP_NOTPARALLEL, /* .NOTPARALLEL or .NO_PARALLEL */
177 SP_NULL, /* .NULL; not mentioned in the manual page */
178 SP_OBJDIR, /* .OBJDIR */
179 SP_ORDER, /* .ORDER */
180 SP_PARALLEL, /* .PARALLEL; not mentioned in the manual page */
181 SP_PATH, /* .PATH or .PATH.suffix */
182 SP_PHONY, /* .PHONY */
184 SP_POSIX, /* .POSIX; not mentioned in the manual page */
186 SP_PRECIOUS, /* .PRECIOUS */
187 SP_SHELL, /* .SHELL */
188 SP_SILENT, /* .SILENT */
189 SP_SINGLESHELL, /* .SINGLESHELL; not mentioned in the manual page */
190 SP_STALE, /* .STALE */
191 SP_SUFFIXES, /* .SUFFIXES */
195 typedef List SearchPathList;
196 typedef ListNode SearchPathListNode;
201 * The main target to create. This is the first target on the first
202 * dependency line in the first makefile.
204 static GNode *mainNode;
209 * During parsing, the targets from the left-hand side of the currently
210 * active dependency line, or NULL if the current line does not belong to a
211 * dependency line, for example because it is a variable assignment.
213 * See unit-tests/deptgt.mk, keyword "parse.c:targets".
215 static GNodeList *targets;
219 * All shell commands for all targets, in no particular order and possibly
220 * with duplicates. Kept in a separate list since the commands from .USE or
221 * .USEBEFORE nodes are shared with other GNodes, thereby giving up the
222 * easily understandable ownership over the allocated strings.
224 static StringList targCmds = LST_INIT;
228 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
229 * seen, then set to each successive source on the line.
231 static GNode *order_pred;
235 /* number of fatal errors */
236 static int fatals = 0;
239 * Variables for doing includes
243 * The include chain of makefiles. At index 0 is the top-level makefile from
244 * the command line, followed by the included files or .for loops, up to and
245 * including the current file.
247 * See PrintStackTrace for how to interpret the data.
249 static Vector /* of IFile */ includes;
254 return Vector_Get(&includes, i);
257 /* The file that is currently being read. */
261 return GetInclude(includes.len - 1);
265 SearchPath *parseIncPath; /* directories for "..." includes */
266 SearchPath *sysIncPath; /* directories for <...> includes */
267 SearchPath *defSysIncPath; /* default for sysIncPath */
272 * The parseKeywords table is searched using binary search when deciding
273 * if a target or source is special. The 'spec' field is the ParseSpecial
274 * type of the keyword (SP_NOT if the keyword isn't special as a target) while
275 * the 'op' field is the operator to apply to the list of targets if the
276 * keyword is used as a source ("0" if the keyword isn't special as a source)
278 static const struct {
279 const char *name; /* Name of keyword */
280 ParseSpecial spec; /* Type when used as a target */
281 GNodeType op; /* Operator when used as a source */
282 } parseKeywords[] = {
283 { ".BEGIN", SP_BEGIN, OP_NONE },
284 { ".DEFAULT", SP_DEFAULT, OP_NONE },
285 { ".DELETE_ON_ERROR", SP_DELETE_ON_ERROR, OP_NONE },
286 { ".END", SP_END, OP_NONE },
287 { ".ERROR", SP_ERROR, OP_NONE },
288 { ".EXEC", SP_ATTRIBUTE, OP_EXEC },
289 { ".IGNORE", SP_IGNORE, OP_IGNORE },
290 { ".INCLUDES", SP_INCLUDES, OP_NONE },
291 { ".INTERRUPT", SP_INTERRUPT, OP_NONE },
292 { ".INVISIBLE", SP_ATTRIBUTE, OP_INVISIBLE },
293 { ".JOIN", SP_ATTRIBUTE, OP_JOIN },
294 { ".LIBS", SP_LIBS, OP_NONE },
295 { ".MADE", SP_ATTRIBUTE, OP_MADE },
296 { ".MAIN", SP_MAIN, OP_NONE },
297 { ".MAKE", SP_ATTRIBUTE, OP_MAKE },
298 { ".MAKEFLAGS", SP_MFLAGS, OP_NONE },
299 { ".META", SP_META, OP_META },
300 { ".MFLAGS", SP_MFLAGS, OP_NONE },
301 { ".NOMETA", SP_NOMETA, OP_NOMETA },
302 { ".NOMETA_CMP", SP_NOMETA_CMP, OP_NOMETA_CMP },
303 { ".NOPATH", SP_NOPATH, OP_NOPATH },
304 { ".NOTMAIN", SP_ATTRIBUTE, OP_NOTMAIN },
305 { ".NOTPARALLEL", SP_NOTPARALLEL, OP_NONE },
306 { ".NO_PARALLEL", SP_NOTPARALLEL, OP_NONE },
307 { ".NULL", SP_NULL, OP_NONE },
308 { ".OBJDIR", SP_OBJDIR, OP_NONE },
309 { ".OPTIONAL", SP_ATTRIBUTE, OP_OPTIONAL },
310 { ".ORDER", SP_ORDER, OP_NONE },
311 { ".PARALLEL", SP_PARALLEL, OP_NONE },
312 { ".PATH", SP_PATH, OP_NONE },
313 { ".PHONY", SP_PHONY, OP_PHONY },
315 { ".POSIX", SP_POSIX, OP_NONE },
317 { ".PRECIOUS", SP_PRECIOUS, OP_PRECIOUS },
318 { ".RECURSIVE", SP_ATTRIBUTE, OP_MAKE },
319 { ".SHELL", SP_SHELL, OP_NONE },
320 { ".SILENT", SP_SILENT, OP_SILENT },
321 { ".SINGLESHELL", SP_SINGLESHELL, OP_NONE },
322 { ".STALE", SP_STALE, OP_NONE },
323 { ".SUFFIXES", SP_SUFFIXES, OP_NONE },
324 { ".USE", SP_ATTRIBUTE, OP_USE },
325 { ".USEBEFORE", SP_ATTRIBUTE, OP_USEBEFORE },
326 { ".WAIT", SP_WAIT, OP_NONE },
332 /* XXX: What is the lifetime of this path? Who manages the memory? */
333 const char *path; /* name, for error reports */
334 char *buf; /* contents buffer */
335 size_t len; /* length of contents */
336 bool used; /* XXX: have we used the data yet */
339 /* XXX: What is the lifetime of the path? Who manages the memory? */
340 static struct loadedfile *
341 loadedfile_create(const char *path, char *buf, size_t buflen)
343 struct loadedfile *lf;
345 lf = bmake_malloc(sizeof *lf);
346 lf->path = path == NULL ? "(stdin)" : path;
354 loadedfile_destroy(struct loadedfile *lf)
361 * readMore() operation for loadedfile, as needed by the weird and twisted
362 * logic below. Once that's cleaned up, we can get rid of lf->used.
365 loadedfile_readMore(void *x, size_t *len)
367 struct loadedfile *lf = x;
378 * Try to get the size of a file.
381 load_getsize(int fd, size_t *ret)
385 if (fstat(fd, &st) < 0)
388 if (!S_ISREG(st.st_mode))
392 * st_size is an off_t, which is 64 bits signed; *ret is
393 * size_t, which might be 32 bits unsigned or 64 bits
394 * unsigned. Rather than being elaborate, just punt on
395 * files that are more than 1 GiB. We should never
396 * see a makefile that size in practice.
398 * While we're at it reject negative sizes too, just in case.
400 if (st.st_size < 0 || st.st_size > 0x3fffffff)
403 *ret = (size_t)st.st_size;
410 * Until the path search logic can be moved under here instead of
411 * being in the caller in another source file, we need to have the fd
412 * passed in already open. Bleh.
414 * If the path is NULL, use stdin.
416 static struct loadedfile *
417 loadfile(const char *path, int fd)
429 if (load_getsize(fd, &filesize)) {
431 * Avoid resizing the buffer later for no reason.
433 * At the same time leave space for adding a final '\n',
434 * just in case it is missing in the file.
439 Buf_InitSize(&buf, filesize);
442 assert(buf.len <= buf.cap);
443 if (buf.len == buf.cap) {
444 if (buf.cap > 0x1fffffff) {
446 Error("%s: file too large", path);
447 exit(2); /* Not 1 so -q can distinguish error */
451 assert(buf.len < buf.cap);
452 n = read(fd, buf.data + buf.len, buf.cap - buf.len);
454 Error("%s: read error: %s", path, strerror(errno));
455 exit(2); /* Not 1 so -q can distinguish error */
460 buf.len += (size_t)n;
462 assert(buf.len <= buf.cap);
464 if (!Buf_EndsWith(&buf, '\n'))
465 Buf_AddByte(&buf, '\n');
471 struct loadedfile *lf = loadedfile_create(path,
479 PrintStackTrace(void)
481 const IFile *entries;
487 entries = GetInclude(0);
491 n--; /* This entry is already in the diagnostic. */
494 * For the IFiles with fromForLoop, lineno seems to be sorted
495 * backwards. This is because lineno is the number of completely
496 * parsed lines, which for a .for loop is right after the
497 * corresponding .endfor. The intuitive line number comes from
498 * first_lineno instead, which points at the start of the .for loop.
500 * To make the stack trace intuitive, the entry below each chain of
501 * .for loop entries must be ignored completely since neither its
502 * lineno nor its first_lineno is useful. Instead, the topmost of
503 * each chain of .for loop entries needs to be printed twice, once
504 * with its first_lineno and once with its lineno.
507 for (i = n; i-- > 0;) {
508 const IFile *entry = entries + i;
509 const char *fname = entry->fname;
511 char dirbuf[MAXPATHLEN + 1];
513 if (fname[0] != '/' && strcmp(fname, "(stdin)") != 0)
514 fname = realpath(fname, dirbuf);
516 printLineno = !entry->fromForLoop;
517 if (i + 1 < n && entries[i + 1].fromForLoop == printLineno)
518 printLineno = entry->fromForLoop;
521 debug_printf("\tin .include from %s:%d\n",
522 fname, entry->lineno);
523 if (entry->fromForLoop)
524 debug_printf("\tin .for loop from %s:%d\n",
525 fname, entry->first_lineno);
529 /* Check if the current character is escaped on the current line. */
531 ParseIsEscaped(const char *line, const char *c)
544 * Add the filename and lineno to the GNode so that we remember where it
550 IFile *curFile = CurFile();
551 gn->fname = curFile->fname;
552 gn->lineno = curFile->lineno;
556 * Look in the table of keywords for one matching the given string.
557 * Return the index of the keyword, or -1 if it isn't there.
560 ParseFindKeyword(const char *str)
563 int end = sizeof parseKeywords / sizeof parseKeywords[0] - 1;
566 int curr = start + (end - start) / 2;
567 int diff = strcmp(str, parseKeywords[curr].name);
575 } while (start <= end);
581 PrintLocation(FILE *f, const char *fname, size_t lineno)
583 char dirbuf[MAXPATHLEN + 1];
586 if (*fname == '/' || strcmp(fname, "(stdin)") == 0) {
587 (void)fprintf(f, "\"%s\" line %u: ", fname, (unsigned)lineno);
591 /* Find out which makefile is the culprit.
592 * We try ${.PARSEDIR} and apply realpath(3) if not absolute. */
594 dir = Var_Value(SCOPE_GLOBAL, ".PARSEDIR");
597 if (dir.str[0] != '/')
598 dir.str = realpath(dir.str, dirbuf);
600 base = Var_Value(SCOPE_GLOBAL, ".PARSEFILE");
601 if (base.str == NULL)
602 base.str = str_basename(fname);
604 (void)fprintf(f, "\"%s/%s\" line %u: ",
605 dir.str, base.str, (unsigned)lineno);
612 ParseVErrorInternal(FILE *f, const char *fname, size_t lineno,
613 ParseErrorLevel type, const char *fmt, va_list ap)
615 static bool fatal_warning_error_printed = false;
617 (void)fprintf(f, "%s: ", progname);
620 PrintLocation(f, fname, lineno);
621 if (type == PARSE_WARNING)
622 (void)fprintf(f, "warning: ");
623 (void)vfprintf(f, fmt, ap);
624 (void)fprintf(f, "\n");
627 if (type == PARSE_INFO)
628 goto print_stack_trace;
629 if (type == PARSE_WARNING && !opts.parseWarnFatal)
630 goto print_stack_trace;
632 if (type == PARSE_WARNING && !fatal_warning_error_printed) {
633 Error("parsing warnings being treated as errors");
634 fatal_warning_error_printed = true;
642 ParseErrorInternal(const char *fname, size_t lineno,
643 ParseErrorLevel type, const char *fmt, ...)
647 (void)fflush(stdout);
649 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
652 if (opts.debug_file != stderr && opts.debug_file != stdout) {
654 ParseVErrorInternal(opts.debug_file, fname, lineno, type,
661 * Print a parse error message, including location information.
663 * If the level is PARSE_FATAL, continue parsing until the end of the
664 * current top-level makefile, then exit (see Parse_File).
666 * Fmt is given without a trailing newline.
669 Parse_Error(ParseErrorLevel type, const char *fmt, ...)
675 if (includes.len == 0) {
679 IFile *curFile = CurFile();
680 fname = curFile->fname;
681 lineno = (size_t)curFile->lineno;
685 (void)fflush(stdout);
686 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
689 if (opts.debug_file != stderr && opts.debug_file != stdout) {
691 ParseVErrorInternal(opts.debug_file, fname, lineno, type,
699 * Parse and handle an .info, .warning or .error directive.
700 * For an .error directive, immediately exit.
703 ParseMessage(ParseErrorLevel level, const char *levelName, const char *umsg)
707 if (umsg[0] == '\0') {
708 Parse_Error(PARSE_FATAL, "Missing argument for \".%s\"",
713 (void)Var_Subst(umsg, SCOPE_CMDLINE, VARE_WANTRES, &xmsg);
714 /* TODO: handle errors */
716 Parse_Error(level, "%s", xmsg);
719 if (level == PARSE_FATAL) {
720 PrintOnError(NULL, NULL);
726 * Add the child to the parent's children.
728 * Additionally, add the parent to the child's parents, but only if the
729 * target is not special. An example for such a special target is .END,
730 * which does not need to be informed once the child target has been made.
733 LinkSource(GNode *pgn, GNode *cgn, bool isSpecial)
735 if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(&pgn->cohorts))
736 pgn = pgn->cohorts.last->datum;
738 Lst_Append(&pgn->children, cgn);
741 /* Special targets like .END don't need any children. */
743 Lst_Append(&cgn->parents, pgn);
746 debug_printf("# %s: added child %s - %s\n",
747 __func__, pgn->name, cgn->name);
748 Targ_PrintNode(pgn, 0);
749 Targ_PrintNode(cgn, 0);
753 /* Add the node to each target from the current dependency group. */
755 LinkToTargets(GNode *gn, bool isSpecial)
759 for (ln = targets->first; ln != NULL; ln = ln->next)
760 LinkSource(ln->datum, gn, isSpecial);
764 TryApplyDependencyOperator(GNode *gn, GNodeType op)
767 * If the node occurred on the left-hand side of a dependency and the
768 * operator also defines a dependency, they must match.
770 if ((op & OP_OPMASK) && (gn->type & OP_OPMASK) &&
771 ((op & OP_OPMASK) != (gn->type & OP_OPMASK))) {
772 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s",
777 if (op == OP_DOUBLEDEP && (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
779 * If the node was of the left-hand side of a '::' operator,
780 * we need to create a new instance of it for the children
781 * and commands on this dependency line since each of these
782 * dependency groups has its own attributes and commands,
783 * separate from the others.
785 * The new instance is placed on the 'cohorts' list of the
786 * initial one (note the initial one is not on its own
787 * cohorts list) and the new instance is linked to all
788 * parents of the initial instance.
793 * Propagate copied bits to the initial node. They'll be
794 * propagated back to the rest of the cohorts later.
796 gn->type |= op & ~OP_OPMASK;
798 cohort = Targ_NewInternalNode(gn->name);
802 * Make the cohort invisible as well to avoid duplicating it
803 * into other variables. True, parents of this target won't
804 * tend to do anything with their local variables, but better
807 * (I think this is pointless now, since the relevant list
808 * traversals will no longer see this node anyway. -mycroft)
810 cohort->type = op | OP_INVISIBLE;
811 Lst_Append(&gn->cohorts, cohort);
812 cohort->centurion = gn;
813 gn->unmade_cohorts++;
814 snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
815 (unsigned int)gn->unmade_cohorts % 1000000);
818 * We don't want to nuke any previous flags (whatever they
819 * were) so we just OR the new operator into the old.
828 ApplyDependencyOperator(GNodeType op)
832 for (ln = targets->first; ln != NULL; ln = ln->next)
833 if (!TryApplyDependencyOperator(ln->datum, op))
838 * We add a .WAIT node in the dependency list. After any dynamic dependencies
839 * (and filename globbing) have happened, it is given a dependency on each
840 * previous child, back until the previous .WAIT node. The next child won't
841 * be scheduled until the .WAIT node is built.
843 * We give each .WAIT node a unique name (mainly for diagnostics).
846 ParseDependencySourceWait(bool isSpecial)
848 static int wait_number = 0;
852 snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
853 gn = Targ_NewInternalNode(wait_src);
856 gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
857 LinkToTargets(gn, isSpecial);
862 ParseDependencySourceKeyword(const char *src, ParseSpecial specType)
867 if (*src != '.' || !ch_isupper(src[1]))
870 keywd = ParseFindKeyword(src);
874 op = parseKeywords[keywd].op;
876 ApplyDependencyOperator(op);
879 if (parseKeywords[keywd].spec == SP_WAIT) {
880 ParseDependencySourceWait(specType != SP_NOT);
887 ParseDependencySourceMain(const char *src)
890 * In a line like ".MAIN: source1 source2", add all sources to the
891 * list of things to create, but only if the user didn't specify a
892 * target on the command line and .MAIN occurs for the first time.
894 * See ParseDependencyTargetSpecial, branch SP_MAIN.
895 * See unit-tests/cond-func-make-main.mk.
897 Lst_Append(&opts.create, bmake_strdup(src));
899 * Add the name to the .TARGETS variable as well, so the user can
900 * employ that, if desired.
902 Global_Append(".TARGETS", src);
906 ParseDependencySourceOrder(const char *src)
910 * Create proper predecessor/successor links between the previous
911 * source and the current one.
913 gn = Targ_GetNode(src);
916 if (order_pred != NULL) {
917 Lst_Append(&order_pred->order_succ, gn);
918 Lst_Append(&gn->order_pred, order_pred);
920 debug_printf("# %s: added Order dependency %s - %s\n",
921 __func__, order_pred->name, gn->name);
922 Targ_PrintNode(order_pred, 0);
923 Targ_PrintNode(gn, 0);
927 * The current source now becomes the predecessor for the next one.
933 ParseDependencySourceOther(const char *src, GNodeType tOp,
934 ParseSpecial specType)
939 * The source is not an attribute, so find/create a node for it.
940 * After that, apply any operator to it from a special target or
941 * link it to its parents, as appropriate.
943 * In the case of a source that was the object of a '::' operator,
944 * the attribute is applied to all of its instances (as kept in
945 * the 'cohorts' list of the node) or all the cohorts are linked
946 * to all the targets.
949 /* Find/create the 'src' node and attach to all targets */
950 gn = Targ_GetNode(src);
956 LinkToTargets(gn, specType != SP_NOT);
960 * Given the name of a source in a dependency line, figure out if it is an
961 * attribute (such as .SILENT) and apply it to the targets if it is. Else
962 * decide if there is some attribute which should be applied *to* the source
963 * because of some special target (such as .PHONY) and apply it if so.
964 * Otherwise, make the source a child of the targets in the list 'targets'.
967 * tOp operator (if any) from special targets
968 * src name of the source to handle
971 ParseDependencySource(GNodeType tOp, const char *src, ParseSpecial specType)
973 if (ParseDependencySourceKeyword(src, specType))
976 if (specType == SP_MAIN)
977 ParseDependencySourceMain(src);
978 else if (specType == SP_ORDER)
979 ParseDependencySourceOrder(src);
981 ParseDependencySourceOther(src, tOp, specType);
985 * If we have yet to decide on a main target to make, in the absence of any
986 * user input, we want the first target on the first dependency line that is
987 * actually a real target (i.e. isn't a .USE or .EXEC rule) to be made.
994 if (mainNode != NULL)
997 for (ln = targets->first; ln != NULL; ln = ln->next) {
998 GNode *gn = ln->datum;
999 if (!(gn->type & OP_NOTARGET)) {
1000 DEBUG1(MAKE, "Setting main node to \"%s\"\n", gn->name);
1009 * We got to the end of the line while we were still looking at targets.
1011 * Ending a dependency line without an operator is a Bozo no-no. As a
1012 * heuristic, this is also often triggered by undetected conflicts from
1016 ParseErrorNoDependency(const char *lstart)
1018 if ((strncmp(lstart, "<<<<<<", 6) == 0) ||
1019 (strncmp(lstart, "======", 6) == 0) ||
1020 (strncmp(lstart, ">>>>>>", 6) == 0))
1021 Parse_Error(PARSE_FATAL,
1022 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
1023 else if (lstart[0] == '.') {
1024 const char *dirstart = lstart + 1;
1026 cpp_skip_whitespace(&dirstart);
1028 while (ch_isalnum(*dirend) || *dirend == '-')
1030 Parse_Error(PARSE_FATAL, "Unknown directive \"%.*s\"",
1031 (int)(dirend - dirstart), dirstart);
1033 Parse_Error(PARSE_FATAL, "Invalid line type");
1037 ParseDependencyTargetWord(const char **pp, const char *lstart)
1039 const char *cp = *pp;
1041 while (*cp != '\0') {
1042 if ((ch_isspace(*cp) || *cp == '!' || *cp == ':' ||
1044 !ParseIsEscaped(lstart, cp))
1049 * Must be a dynamic source (would have been expanded
1050 * otherwise), so call the Var module to parse the
1051 * puppy so we can safely advance beyond it.
1053 * There should be no errors in this, as they would
1054 * have been discovered in the initial Var_Subst and
1055 * we wouldn't be here.
1057 const char *nested_p = cp;
1060 (void)Var_Parse(&nested_p, SCOPE_CMDLINE,
1061 VARE_PARSE_ONLY, &nested_val);
1062 /* TODO: handle errors */
1063 FStr_Done(&nested_val);
1064 cp += nested_p - cp;
1073 * Handle special targets like .PATH, .DEFAULT, .BEGIN, .ORDER.
1075 * See the tests deptgt-*.mk.
1078 ParseDependencyTargetSpecial(ParseSpecial *inout_specType,
1079 const char *targetName,
1080 SearchPathList **inout_paths)
1082 switch (*inout_specType) {
1084 if (*inout_paths == NULL)
1085 *inout_paths = Lst_New();
1086 Lst_Append(*inout_paths, &dirSearchPath);
1090 * Allow targets from the command line to override the
1093 if (!Lst_IsEmpty(&opts.create))
1094 *inout_specType = SP_NOT;
1100 case SP_INTERRUPT: {
1101 GNode *gn = Targ_GetNode(targetName);
1104 gn->type |= OP_NOTMAIN | OP_SPECIAL;
1105 Lst_Append(targets, gn);
1110 * Need to create a node to hang commands on, but we don't
1111 * want it in the graph, nor do we want it to be the Main
1112 * Target. We claim the node is a transformation rule to make
1113 * life easier later, when we'll use Make_HandleUse to
1114 * actually apply the .DEFAULT commands.
1116 GNode *gn = GNode_New(".DEFAULT");
1117 gn->type |= OP_NOTMAIN | OP_TRANSFORM;
1118 Lst_Append(targets, gn);
1122 case SP_DELETE_ON_ERROR:
1123 deleteOnError = true;
1125 case SP_NOTPARALLEL:
1128 case SP_SINGLESHELL:
1129 opts.compatMake = true;
1140 * .PATH<suffix> has to be handled specially.
1141 * Call on the suffix module to give us a path to modify.
1144 ParseDependencyTargetPath(const char *suffixName,
1145 SearchPathList **inout_paths)
1149 path = Suff_GetPath(suffixName);
1151 Parse_Error(PARSE_FATAL,
1152 "Suffix '%s' not defined (yet)", suffixName);
1156 if (*inout_paths == NULL)
1157 *inout_paths = Lst_New();
1158 Lst_Append(*inout_paths, path);
1164 * See if it's a special target and if so set specType to match it.
1167 ParseDependencyTarget(const char *targetName,
1168 ParseSpecial *inout_specType,
1169 GNodeType *out_tOp, SearchPathList **inout_paths)
1173 if (!(targetName[0] == '.' && ch_isupper(targetName[1])))
1177 * See if the target is a special target that must have it
1178 * or its sources handled specially.
1180 keywd = ParseFindKeyword(targetName);
1182 if (*inout_specType == SP_PATH &&
1183 parseKeywords[keywd].spec != SP_PATH) {
1184 Parse_Error(PARSE_FATAL, "Mismatched special targets");
1188 *inout_specType = parseKeywords[keywd].spec;
1189 *out_tOp = parseKeywords[keywd].op;
1191 ParseDependencyTargetSpecial(inout_specType, targetName,
1194 } else if (strncmp(targetName, ".PATH", 5) == 0) {
1195 *inout_specType = SP_PATH;
1196 if (!ParseDependencyTargetPath(targetName + 5, inout_paths))
1203 ParseDependencyTargetMundane(char *targetName, StringList *curTargs)
1205 if (Dir_HasWildcards(targetName)) {
1207 * Targets are to be sought only in the current directory,
1208 * so create an empty path for the thing. Note we need to
1209 * use Dir_Destroy in the destruction of the path as the
1210 * Dir module could have added a directory to the path...
1212 SearchPath *emptyPath = SearchPath_New();
1214 SearchPath_Expand(emptyPath, targetName, curTargs);
1216 SearchPath_Free(emptyPath);
1219 * No wildcards, but we want to avoid code duplication,
1220 * so create a list with the word on it.
1222 Lst_Append(curTargs, targetName);
1225 /* Apply the targets. */
1227 while (!Lst_IsEmpty(curTargs)) {
1228 char *targName = Lst_Dequeue(curTargs);
1229 GNode *gn = Suff_IsTransform(targName)
1230 ? Suff_AddTransform(targName)
1231 : Targ_GetNode(targName);
1235 Lst_Append(targets, gn);
1240 ParseDependencyTargetExtraWarn(char **pp, const char *lstart)
1242 bool warning = false;
1245 while (*cp != '\0') {
1246 if (!ParseIsEscaped(lstart, cp) && (*cp == '!' || *cp == ':'))
1248 if (ParseIsEscaped(lstart, cp) || (*cp != ' ' && *cp != '\t'))
1253 Parse_Error(PARSE_WARNING, "Extra target ignored");
1259 ParseDependencyCheckSpec(ParseSpecial specType)
1263 Parse_Error(PARSE_WARNING,
1264 "Special and mundane targets don't mix. "
1265 "Mundane ones ignored");
1274 * These create nodes on which to hang commands, so targets
1275 * shouldn't be empty.
1278 /* Nothing special here -- targets can be empty if it wants. */
1284 * In a dependency line like 'targets: sources' or 'targets! sources', parse
1285 * the operator ':', '::' or '!' from between the targets and the sources.
1288 ParseDependencyOp(char **pp, const char *lstart, GNodeType *out_op)
1290 const char *cp = *pp;
1300 *out_op = OP_DOUBLEDEP;
1303 *out_op = OP_DEPENDS;
1310 const char *msg = lstart[0] == '.'
1311 ? "Unknown directive" : "Missing dependency operator";
1312 Parse_Error(PARSE_FATAL, "%s", msg);
1318 ClearPaths(SearchPathList *paths)
1320 if (paths != NULL) {
1321 SearchPathListNode *ln;
1322 for (ln = paths->first; ln != NULL; ln = ln->next)
1323 SearchPath_Clear(ln->datum);
1330 * Several special targets take different actions if present with no
1332 * a .SUFFIXES line with no sources clears out all old suffixes
1333 * a .PRECIOUS line makes all targets precious
1334 * a .IGNORE line ignores errors for all targets
1335 * a .SILENT line creates silence when making all targets
1336 * a .PATH removes all directories from the search path(s).
1339 ParseDependencySourcesEmpty(ParseSpecial specType, SearchPathList *paths)
1343 Suff_ClearSuffixes();
1349 opts.ignoreErrors = true;
1352 opts.beSilent = true;
1359 Global_Set("%POSIX", "1003.2");
1368 AddToPaths(const char *dir, SearchPathList *paths)
1370 if (paths != NULL) {
1371 SearchPathListNode *ln;
1372 for (ln = paths->first; ln != NULL; ln = ln->next)
1373 (void)SearchPath_Add(ln->datum, dir);
1378 * If the target was one that doesn't take files as its sources
1379 * but takes something like suffixes, we take each
1380 * space-separated word on the line as a something and deal
1381 * with it accordingly.
1383 * If the target was .SUFFIXES, we take each source as a
1384 * suffix and add it to the list of suffixes maintained by the
1387 * If the target was a .PATH, we add the source as a directory
1388 * to search on the search path.
1390 * If it was .INCLUDES, the source is taken to be the suffix of
1391 * files which will be #included and whose search path should
1392 * be present in the .INCLUDES variable.
1394 * If it was .LIBS, the source is taken to be the suffix of
1395 * files which are considered libraries and whose search path
1396 * should be present in the .LIBS variable.
1398 * If it was .NULL, the source is the suffix to use when a file
1399 * has no valid suffix.
1401 * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1402 * and will cause make to do a new chdir to that path.
1405 ParseDependencySourceSpecial(ParseSpecial specType, char *word,
1406 SearchPathList *paths)
1410 Suff_AddSuffix(word, &mainNode);
1413 AddToPaths(word, paths);
1416 Suff_AddInclude(word);
1425 Main_SetObjdir(false, "%s", word);
1433 ParseDependencyTargets(char **inout_cp,
1436 ParseSpecial *inout_specType,
1437 GNodeType *inout_tOp,
1438 SearchPathList **inout_paths,
1439 StringList *curTargs)
1442 char *tgt = *inout_line;
1448 * Here LINE points to the beginning of the next word, and
1449 * LSTART points to the actual beginning of the line.
1452 /* Find the end of the next word. */
1455 ParseDependencyTargetWord(&p, lstart);
1459 * If the word is followed by a left parenthesis, it's the
1460 * name of an object file inside an archive (ar file).
1462 if (!ParseIsEscaped(lstart, cp) && *cp == '(') {
1464 * Archives must be handled specially to make sure the
1465 * OP_ARCHV flag is set in their 'type' field, for one
1466 * thing, and because things like "archive(file1.o
1467 * file2.o file3.o)" are permissible.
1469 * Arch_ParseArchive will set 'line' to be the first
1470 * non-blank after the archive-spec. It creates/finds
1471 * nodes for the members and places them on the given
1472 * list, returning true if all went well and false if
1473 * there was an error in the specification. On error,
1474 * line should remain untouched.
1476 if (!Arch_ParseArchive(&tgt, targets, SCOPE_CMDLINE)) {
1477 Parse_Error(PARSE_FATAL,
1478 "Error in archive specification: \"%s\"",
1488 ParseErrorNoDependency(lstart);
1492 /* Insert a null terminator. */
1496 if (!ParseDependencyTarget(tgt, inout_specType, inout_tOp,
1501 * Have word in line. Get or create its node and stick it at
1502 * the end of the targets list
1504 if (*inout_specType == SP_NOT && *tgt != '\0')
1505 ParseDependencyTargetMundane(tgt, curTargs);
1506 else if (*inout_specType == SP_PATH && *tgt != '.' &&
1508 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored",
1511 /* Don't need the inserted null terminator any more. */
1515 * If it is a special type and not .PATH, it's the only target
1516 * we allow on this line.
1518 if (*inout_specType != SP_NOT && *inout_specType != SP_PATH)
1519 ParseDependencyTargetExtraWarn(&cp, lstart);
1521 pp_skip_whitespace(&cp);
1526 if ((*tgt == '!' || *tgt == ':') &&
1527 !ParseIsEscaped(lstart, tgt))
1537 ParseDependencySourcesSpecial(char *start, char *end,
1538 ParseSpecial specType, SearchPathList *paths)
1542 while (*start != '\0') {
1543 while (*end != '\0' && !ch_isspace(*end))
1547 ParseDependencySourceSpecial(specType, start, paths);
1551 pp_skip_whitespace(&end);
1557 ParseDependencySourcesMundane(char *start, char *end,
1558 ParseSpecial specType, GNodeType tOp)
1560 while (*start != '\0') {
1562 * The targets take real sources, so we must beware of archive
1563 * specifications (i.e. things with left parentheses in them)
1564 * and handle them accordingly.
1566 for (; *end != '\0' && !ch_isspace(*end); end++) {
1567 if (*end == '(' && end > start && end[-1] != '$') {
1569 * Only stop for a left parenthesis if it
1570 * isn't at the start of a word (that'll be
1571 * for variable changes later) and isn't
1572 * preceded by a dollar sign (a dynamic
1580 GNodeList sources = LST_INIT;
1581 if (!Arch_ParseArchive(&start, &sources,
1583 Parse_Error(PARSE_FATAL,
1584 "Error in source archive spec \"%s\"",
1589 while (!Lst_IsEmpty(&sources)) {
1590 GNode *gn = Lst_Dequeue(&sources);
1591 ParseDependencySource(tOp, gn->name, specType);
1601 ParseDependencySource(tOp, start, specType);
1603 pp_skip_whitespace(&end);
1610 * In a dependency line like 'targets: sources', parse the sources.
1612 * See the tests depsrc-*.mk.
1615 ParseDependencySources(char *const line, char *const cp,
1616 GNodeType const tOp,
1617 ParseSpecial const specType,
1618 SearchPathList ** inout_paths)
1620 if (line[0] == '\0') {
1621 ParseDependencySourcesEmpty(specType, *inout_paths);
1622 } else if (specType == SP_MFLAGS) {
1623 Main_ParseArgLine(line);
1625 * Set the initial character to a null-character so the loop
1626 * to get sources won't get anything.
1629 } else if (specType == SP_SHELL) {
1630 if (!Job_ParseShell(line)) {
1631 Parse_Error(PARSE_FATAL,
1632 "improper shell specification");
1636 } else if (specType == SP_NOTPARALLEL || specType == SP_SINGLESHELL ||
1637 specType == SP_DELETE_ON_ERROR) {
1641 /* Now go for the sources. */
1642 if (specType == SP_SUFFIXES || specType == SP_PATH ||
1643 specType == SP_INCLUDES || specType == SP_LIBS ||
1644 specType == SP_NULL || specType == SP_OBJDIR) {
1645 ParseDependencySourcesSpecial(line, cp, specType,
1647 if (*inout_paths != NULL) {
1648 Lst_Free(*inout_paths);
1649 *inout_paths = NULL;
1651 if (specType == SP_PATH)
1654 assert(*inout_paths == NULL);
1655 if (!ParseDependencySourcesMundane(line, cp, specType, tOp))
1663 * Parse a dependency line consisting of targets, followed by a dependency
1664 * operator, optionally followed by sources.
1666 * The nodes of the sources are linked as children to the nodes of the
1667 * targets. Nodes are created as necessary.
1669 * The operator is applied to each node in the global 'targets' list,
1670 * which is where the nodes found for the targets are kept, by means of
1671 * the ParseOp function.
1673 * The sources are parsed in much the same way as the targets, except
1674 * that they are expanded using the wildcarding scheme of the C-Shell,
1675 * and a target is created for each expanded word. Each of the resulting
1676 * nodes is then linked to each of the targets as one of its children.
1678 * Certain targets and sources such as .PHONY or .PRECIOUS are handled
1679 * specially. These are the ones detailed by the specType variable.
1681 * The storing of transformation rules such as '.c.o' is also taken care of
1682 * here. A target is recognized as a transformation rule by calling
1683 * Suff_IsTransform. If it is a transformation rule, its node is gotten
1684 * from the suffix module via Suff_AddTransform rather than the standard
1685 * Targ_FindNode in the target module.
1687 * Upon return, the value of the line is unspecified.
1690 ParseDependency(char *line)
1692 char *cp; /* our current position */
1693 GNodeType op; /* the operator on the line */
1694 SearchPathList *paths; /* search paths to alter when parsing
1695 * a list of .PATH targets */
1696 GNodeType tOp; /* operator from special target */
1697 /* target names to be found and added to the targets list */
1698 StringList curTargs = LST_INIT;
1699 char *lstart = line;
1702 * specType contains the SPECial TYPE of the current target. It is
1703 * SP_NOT if the target is unspecial. If it *is* special, however, the
1704 * children are linked as children of the parent but not vice versa.
1706 ParseSpecial specType = SP_NOT;
1708 DEBUG1(PARSE, "ParseDependency(%s)\n", line);
1714 * First, grind through the targets.
1716 /* XXX: don't use 'line' as an iterator variable */
1717 if (!ParseDependencyTargets(&cp, &line, lstart, &specType, &tOp,
1722 * Don't need the list of target names anymore.
1723 * The targets themselves are now in the global variable 'targets'.
1725 Lst_Done(&curTargs);
1726 Lst_Init(&curTargs);
1728 if (!Lst_IsEmpty(targets))
1729 ParseDependencyCheckSpec(specType);
1732 * Have now parsed all the target names. Must parse the operator next.
1734 if (!ParseDependencyOp(&cp, lstart, &op))
1738 * Apply the operator to the target. This is how we remember which
1739 * operator a target was defined with. It fails if the operator
1740 * used isn't consistent across all references.
1742 ApplyDependencyOperator(op);
1745 * Onward to the sources.
1747 * LINE will now point to the first source word, if any, or the
1748 * end of the string if not.
1750 pp_skip_whitespace(&cp);
1751 line = cp; /* XXX: 'line' is an inappropriate name */
1753 ParseDependencySources(line, cp, tOp, specType, &paths);
1758 Lst_Done(&curTargs);
1761 typedef struct VarAssignParsed {
1762 const char *nameStart; /* unexpanded */
1763 const char *nameEnd; /* before operator adjustment */
1764 const char *eq; /* the '=' of the assignment operator */
1768 * Determine the assignment operator and adjust the end of the variable
1772 AdjustVarassignOp(const VarAssignParsed *pvar, const char *value,
1775 const char *op = pvar->eq;
1776 const char *const name = pvar->nameStart;
1779 if (op > name && op[-1] == '+') {
1783 } else if (op > name && op[-1] == '?') {
1787 } else if (op > name && op[-1] == ':') {
1791 } else if (op > name && op[-1] == '!') {
1798 while (op > name && ch_isspace(op[-1]))
1801 if (op >= name + 3 && op[-3] == ':' && op[-2] == 's' &&
1810 const char *nameEnd = pvar->nameEnd < op ? pvar->nameEnd : op;
1811 out_var->varname = bmake_strsedup(pvar->nameStart, nameEnd);
1813 out_var->value = value;
1818 * Parse a variable assignment, consisting of a single-word variable name,
1819 * optional whitespace, an assignment operator, optional whitespace and the
1822 * Note: There is a lexical ambiguity with assignment modifier characters
1823 * in variable names. This routine interprets the character before the =
1824 * as a modifier. Therefore, an assignment like
1826 * is interpreted as "C+ +=" instead of "C++ =".
1828 * Used for both lines in a file and command line arguments.
1831 Parse_IsVar(const char *p, VarAssign *out_var)
1833 VarAssignParsed pvar;
1834 const char *firstSpace = NULL;
1837 cpp_skip_hspace(&p); /* Skip to variable name */
1840 * During parsing, the '+' of the '+=' operator is initially parsed
1841 * as part of the variable name. It is later corrected, as is the
1842 * ':sh' modifier. Of these two (nameEnd and op), the earlier one
1843 * determines the actual end of the variable name.
1847 pvar.nameEnd = NULL;
1852 * Scan for one of the assignment operators outside a variable
1855 while (*p != '\0') {
1857 if (ch == '(' || ch == '{') {
1861 if (ch == ')' || ch == '}') {
1869 if (ch == ' ' || ch == '\t')
1870 if (firstSpace == NULL)
1872 while (ch == ' ' || ch == '\t')
1876 if (ch == ':' && p[0] == 's' && p[1] == 'h') {
1883 pvar.nameEnd = firstSpace != NULL ? firstSpace : p - 1;
1884 cpp_skip_whitespace(&p);
1885 AdjustVarassignOp(&pvar, p, out_var);
1889 (ch == '+' || ch == ':' || ch == '?' || ch == '!')) {
1891 pvar.nameEnd = firstSpace != NULL ? firstSpace : p;
1893 cpp_skip_whitespace(&p);
1894 AdjustVarassignOp(&pvar, p, out_var);
1897 if (firstSpace != NULL)
1905 * Check for syntax errors such as unclosed expressions or unknown modifiers.
1908 VarCheckSyntax(VarAssignOp type, const char *uvalue, GNode *scope)
1911 if (type != VAR_SUBST && strchr(uvalue, '$') != NULL) {
1912 char *expandedValue;
1914 (void)Var_Subst(uvalue, scope, VARE_PARSE_ONLY,
1916 /* TODO: handle errors */
1917 free(expandedValue);
1923 VarAssign_EvalSubst(GNode *scope, const char *name, const char *uvalue,
1929 * make sure that we set the variable the first time to nothing
1930 * so that it gets substituted.
1932 * TODO: Add a test that demonstrates why this code is needed,
1933 * apart from making the debug log longer.
1935 if (!Var_ExistsExpand(scope, name))
1936 Var_SetExpand(scope, name, "");
1938 (void)Var_Subst(uvalue, scope, VARE_KEEP_DOLLAR_UNDEF, &evalue);
1939 /* TODO: handle errors */
1941 Var_SetExpand(scope, name, evalue);
1943 *out_avalue = FStr_InitOwn(evalue);
1947 VarAssign_EvalShell(const char *name, const char *uvalue, GNode *scope,
1954 cmd = FStr_InitRefer(uvalue);
1955 if (strchr(cmd.str, '$') != NULL) {
1957 (void)Var_Subst(cmd.str, SCOPE_CMDLINE, VARE_UNDEFERR,
1959 /* TODO: handle errors */
1960 cmd = FStr_InitOwn(expanded);
1963 cmdOut = Cmd_Exec(cmd.str, &errfmt);
1964 Var_SetExpand(scope, name, cmdOut);
1965 *out_avalue = FStr_InitOwn(cmdOut);
1968 Parse_Error(PARSE_WARNING, errfmt, cmd.str);
1974 * Perform a variable assignment.
1976 * The actual value of the variable is returned in *out_true_avalue.
1977 * Especially for VAR_SUBST and VAR_SHELL this can differ from the literal
1980 * Return whether the assignment was actually performed, which is usually
1981 * the case. It is only skipped if the operator is '?=' and the variable
1985 VarAssign_Eval(const char *name, VarAssignOp op, const char *uvalue,
1986 GNode *scope, FStr *out_true_avalue)
1988 FStr avalue = FStr_InitRefer(uvalue);
1990 if (op == VAR_APPEND)
1991 Var_AppendExpand(scope, name, uvalue);
1992 else if (op == VAR_SUBST)
1993 VarAssign_EvalSubst(scope, name, uvalue, &avalue);
1994 else if (op == VAR_SHELL)
1995 VarAssign_EvalShell(name, uvalue, scope, &avalue);
1997 if (op == VAR_DEFAULT && Var_ExistsExpand(scope, name))
2000 /* Normal assignment -- just do it. */
2001 Var_SetExpand(scope, name, uvalue);
2004 *out_true_avalue = avalue;
2009 VarAssignSpecial(const char *name, const char *avalue)
2011 if (strcmp(name, MAKEOVERRIDES) == 0)
2012 Main_ExportMAKEFLAGS(false); /* re-export MAKEFLAGS */
2013 else if (strcmp(name, ".CURDIR") == 0) {
2015 * Someone is being (too?) clever...
2016 * Let's pretend they know what they are doing and
2017 * re-initialize the 'cur' CachedDir.
2019 Dir_InitCur(avalue);
2021 } else if (strcmp(name, MAKE_JOB_PREFIX) == 0)
2023 else if (strcmp(name, MAKE_EXPORTED) == 0)
2024 Var_ExportVars(avalue);
2027 /* Perform the variable variable assignment in the given scope. */
2029 Parse_Var(VarAssign *var, GNode *scope)
2031 FStr avalue; /* actual value (maybe expanded) */
2033 VarCheckSyntax(var->op, var->value, scope);
2034 if (VarAssign_Eval(var->varname, var->op, var->value, scope, &avalue)) {
2035 VarAssignSpecial(var->varname, avalue.str);
2044 * See if the command possibly calls a sub-make by using the variable
2045 * expressions ${.MAKE}, ${MAKE} or the plain word "make".
2048 MaybeSubMake(const char *cmd)
2052 for (start = cmd; *start != '\0'; start++) {
2053 const char *p = start;
2056 /* XXX: What if progname != "make"? */
2057 if (p[0] == 'm' && p[1] == 'a' && p[2] == 'k' && p[3] == 'e')
2058 if (start == cmd || !ch_isalnum(p[-1]))
2059 if (!ch_isalnum(p[4]))
2074 if (*p == '.') /* Accept either ${.MAKE} or ${MAKE}. */
2077 if (p[0] == 'M' && p[1] == 'A' && p[2] == 'K' && p[3] == 'E')
2085 * Append the command to the target node.
2087 * The node may be marked as a submake node if the command is determined to
2091 ParseAddCmd(GNode *gn, char *cmd)
2093 /* Add to last (ie current) cohort for :: targets */
2094 if ((gn->type & OP_DOUBLEDEP) && gn->cohorts.last != NULL)
2095 gn = gn->cohorts.last->datum;
2097 /* if target already supplied, ignore commands */
2098 if (!(gn->type & OP_HAS_COMMANDS)) {
2099 Lst_Append(&gn->commands, cmd);
2100 if (MaybeSubMake(cmd))
2101 gn->type |= OP_SUBMAKE;
2105 /* XXX: We cannot do this until we fix the tree */
2106 Lst_Append(&gn->commands, cmd);
2107 Parse_Error(PARSE_WARNING,
2108 "overriding commands for target \"%s\"; "
2109 "previous commands defined at %s: %d ignored",
2110 gn->name, gn->fname, gn->lineno);
2112 Parse_Error(PARSE_WARNING,
2113 "duplicate script for target \"%s\" ignored",
2115 ParseErrorInternal(gn->fname, (size_t)gn->lineno, PARSE_WARNING,
2116 "using previous script for \"%s\" defined here",
2123 * Add a directory to the path searched for included makefiles bracketed
2127 Parse_AddIncludeDir(const char *dir)
2129 (void)SearchPath_Add(parseIncPath, dir);
2133 * Handle one of the .[-ds]include directives by remembering the current file
2134 * and pushing the included file on the stack. After the included file has
2135 * finished, parsing continues with the including file; see Parse_SetInput
2138 * System includes are looked up in sysIncPath, any other includes are looked
2139 * up in the parsedir and then in the directories specified by the -I command
2143 IncludeFile(char *file, bool isSystem, bool depinc, bool silent)
2145 struct loadedfile *lf;
2146 char *fullname; /* full pathname of file */
2148 char *slash, *incdir;
2152 fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
2154 if (fullname == NULL && !isSystem) {
2156 * Include files contained in double-quotes are first searched
2157 * relative to the including file's location. We don't want to
2158 * cd there, of course, so we just tack on the old file's
2159 * leading path components and call Dir_FindFile to see if
2160 * we can locate the file.
2163 incdir = bmake_strdup(CurFile()->fname);
2164 slash = strrchr(incdir, '/');
2165 if (slash != NULL) {
2168 * Now do lexical processing of leading "../" on the
2171 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
2172 slash = strrchr(incdir + 1, '/');
2173 if (slash == NULL || strcmp(slash, "/..") == 0)
2177 newName = str_concat3(incdir, "/", file + i);
2178 fullname = Dir_FindFile(newName, parseIncPath);
2179 if (fullname == NULL)
2180 fullname = Dir_FindFile(newName,
2186 if (fullname == NULL) {
2188 * Makefile wasn't found in same directory as included
2191 * Search for it first on the -I search path, then on
2192 * the .PATH search path, if not found in a -I
2193 * directory. If we have a suffix-specific path, we
2197 SearchPath *suffPath = NULL;
2199 if ((suff = strrchr(file, '.')) != NULL) {
2200 suffPath = Suff_GetPath(suff);
2201 if (suffPath != NULL)
2202 fullname = Dir_FindFile(file, suffPath);
2204 if (fullname == NULL) {
2205 fullname = Dir_FindFile(file, parseIncPath);
2206 if (fullname == NULL)
2207 fullname = Dir_FindFile(file,
2213 /* Looking for a system file or file still not found */
2214 if (fullname == NULL) {
2216 * Look for it on the system path
2218 SearchPath *path = Lst_IsEmpty(&sysIncPath->dirs)
2219 ? defSysIncPath : sysIncPath;
2220 fullname = Dir_FindFile(file, path);
2223 if (fullname == NULL) {
2225 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2229 /* Actually open the file... */
2230 fd = open(fullname, O_RDONLY);
2233 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
2239 lf = loadfile(fullname, fd);
2241 /* Start reading from this file next */
2242 Parse_SetInput(fullname, 0, -1, loadedfile_readMore, lf);
2245 doing_depend = depinc; /* only turn it on */
2249 ParseInclude(char *directive)
2251 char endc; /* the character which ends the file spec */
2252 char *cp; /* current position in file spec */
2253 bool silent = directive[0] != 'i';
2254 char *file = directive + (silent ? 8 : 7);
2256 /* Skip to delimiter character so we know where to look */
2257 pp_skip_hspace(&file);
2259 if (*file != '"' && *file != '<') {
2260 Parse_Error(PARSE_FATAL,
2261 ".include filename must be delimited by '\"' or '<'");
2266 * Set the search path on which to find the include file based on the
2267 * characters which bracket its name. Angle-brackets imply it's
2268 * a system Makefile while double-quotes imply it's a user makefile
2275 /* Skip to matching delimiter */
2276 for (cp = ++file; *cp != '\0' && *cp != endc; cp++)
2280 Parse_Error(PARSE_FATAL,
2281 "Unclosed .include filename. '%c' expected", endc);
2288 * Substitute for any variables in the filename before trying to
2291 (void)Var_Subst(file, SCOPE_CMDLINE, VARE_WANTRES, &file);
2292 /* TODO: handle errors */
2294 IncludeFile(file, endc == '>', directive[0] == 'd', silent);
2299 * Split filename into dirname + basename, then assign these to the
2303 SetFilenameVars(const char *filename, const char *dirvar, const char *filevar)
2305 const char *slash, *basename;
2308 slash = strrchr(filename, '/');
2309 if (slash == NULL) {
2310 dirname = FStr_InitRefer(curdir);
2311 basename = filename;
2313 dirname = FStr_InitOwn(bmake_strsedup(filename, slash));
2314 basename = slash + 1;
2317 Global_SetExpand(dirvar, dirname.str);
2318 Global_SetExpand(filevar, basename);
2320 DEBUG5(PARSE, "%s: ${%s} = `%s' ${%s} = `%s'\n",
2321 __func__, dirvar, dirname.str, filevar, basename);
2322 FStr_Done(&dirname);
2326 * Return the immediately including file.
2328 * This is made complicated since the .for loop is implemented as a special
2329 * kind of .include; see For_Run.
2332 GetActuallyIncludingFile(void)
2335 const IFile *incs = GetInclude(0);
2337 for (i = includes.len; i >= 2; i--)
2338 if (!incs[i - 1].fromForLoop)
2339 return incs[i - 2].fname;
2343 /* Set .PARSEDIR, .PARSEFILE, .INCLUDEDFROMDIR and .INCLUDEDFROMFILE. */
2345 ParseSetParseFile(const char *filename)
2347 const char *including;
2349 SetFilenameVars(filename, ".PARSEDIR", ".PARSEFILE");
2351 including = GetActuallyIncludingFile();
2352 if (including != NULL) {
2353 SetFilenameVars(including,
2354 ".INCLUDEDFROMDIR", ".INCLUDEDFROMFILE");
2356 Global_Delete(".INCLUDEDFROMDIR");
2357 Global_Delete(".INCLUDEDFROMFILE");
2362 StrContainsWord(const char *str, const char *word)
2364 size_t strLen = strlen(str);
2365 size_t wordLen = strlen(word);
2366 const char *p, *end;
2368 if (strLen < wordLen)
2369 return false; /* str is too short to contain word */
2371 end = str + strLen - wordLen;
2372 for (p = str; p != NULL; p = strchr(p, ' ')) {
2376 return false; /* cannot contain word */
2378 if (memcmp(p, word, wordLen) == 0 &&
2379 (p[wordLen] == '\0' || p[wordLen] == ' '))
2386 * XXX: Searching through a set of words with this linear search is
2387 * inefficient for variables that contain thousands of words.
2389 * XXX: The paths in this list don't seem to be normalized in any way.
2392 VarContainsWord(const char *varname, const char *word)
2394 FStr val = Var_Value(SCOPE_GLOBAL, varname);
2395 bool found = val.str != NULL && StrContainsWord(val.str, word);
2401 * Track the makefiles we read - so makefiles can set dependencies on them.
2402 * Avoid adding anything more than once.
2404 * Time complexity: O(n) per call, in total O(n^2), where n is the number
2405 * of makefiles that have been loaded.
2408 ParseTrackInput(const char *name)
2410 if (!VarContainsWord(MAKE_MAKEFILES, name))
2411 Global_Append(MAKE_MAKEFILES, name);
2416 * Start parsing from the given source.
2418 * The given file is added to the includes stack.
2421 Parse_SetInput(const char *name, int lineno, int fd,
2422 ReadMoreProc readMore, void *readMoreArg)
2427 bool fromForLoop = name == NULL;
2430 name = CurFile()->fname;
2432 ParseTrackInput(name);
2434 DEBUG3(PARSE, "Parse_SetInput: %s %s, line %d\n",
2435 readMore == loadedfile_readMore ? "file" : ".for loop in",
2438 if (fd == -1 && readMore == NULL)
2442 curFile = Vector_Push(&includes);
2443 curFile->fname = bmake_strdup(name);
2444 curFile->fromForLoop = fromForLoop;
2445 curFile->lineno = lineno;
2446 curFile->first_lineno = lineno;
2447 curFile->readMore = readMore;
2448 curFile->readMoreArg = readMoreArg;
2450 curFile->depending = doing_depend; /* restore this on EOF */
2452 assert(readMore != NULL);
2454 /* Get first block of input data */
2455 buf = curFile->readMore(curFile->readMoreArg, &len);
2457 /* Was all a waste of time ... */
2458 if (curFile->fname != NULL)
2459 free(curFile->fname);
2463 curFile->buf_freeIt = buf;
2464 curFile->buf_ptr = buf;
2465 curFile->buf_end = buf + len;
2467 curFile->cond_depth = Cond_save_depth();
2468 ParseSetParseFile(name);
2471 /* Check if the directive is an include directive. */
2473 IsInclude(const char *dir, bool sysv)
2475 if (dir[0] == 's' || dir[0] == '-' || (dir[0] == 'd' && !sysv))
2478 if (strncmp(dir, "include", 7) != 0)
2481 /* Space is not mandatory for BSD .include */
2482 return !sysv || ch_isspace(dir[7]);
2487 /* Check if the line is a SYSV include directive. */
2489 IsSysVInclude(const char *line)
2493 if (!IsInclude(line, true))
2496 /* Avoid interpreting a dependency line as an include */
2497 for (p = line; (p = strchr(p, ':')) != NULL;) {
2499 /* end of line -> it's a dependency */
2503 /* '::' operator or ': ' -> it's a dependency */
2504 if (*p == ':' || ch_isspace(*p))
2510 /* Push to another file. The line points to the word "include". */
2512 ParseTraditionalInclude(char *line)
2514 char *cp; /* current position in file spec */
2516 bool silent = line[0] != 'i';
2517 char *file = line + (silent ? 8 : 7);
2520 DEBUG2(PARSE, "%s: %s\n", __func__, file);
2522 pp_skip_whitespace(&file);
2525 * Substitute for any variables in the file name before trying to
2528 (void)Var_Subst(file, SCOPE_CMDLINE, VARE_WANTRES, &all_files);
2529 /* TODO: handle errors */
2531 if (*file == '\0') {
2532 Parse_Error(PARSE_FATAL, "Filename missing from \"include\"");
2536 for (file = all_files; !done; file = cp + 1) {
2537 /* Skip to end of line or next whitespace */
2538 for (cp = file; *cp != '\0' && !ch_isspace(*cp); cp++)
2546 IncludeFile(file, false, false, silent);
2554 /* Parse "export <variable>=<value>", and actually export it. */
2556 ParseGmakeExport(char *line)
2558 char *variable = line + 6;
2561 DEBUG2(PARSE, "%s: %s\n", __func__, variable);
2563 pp_skip_whitespace(&variable);
2565 for (value = variable; *value != '\0' && *value != '='; value++)
2568 if (*value != '=') {
2569 Parse_Error(PARSE_FATAL,
2570 "Variable/Value missing from \"export\"");
2573 *value++ = '\0'; /* terminate variable */
2576 * Expand the value before putting it in the environment.
2578 (void)Var_Subst(value, SCOPE_CMDLINE, VARE_WANTRES, &value);
2579 /* TODO: handle errors */
2581 setenv(variable, value, 1);
2587 * Called when EOF is reached in the current file. If we were reading an
2588 * include file or a .for loop, the includes stack is popped and things set
2589 * up to go back to reading the previous file at the previous location.
2592 * true to continue parsing, i.e. it had only reached the end of an
2593 * included file, false if the main file has been parsed completely.
2600 IFile *curFile = CurFile();
2602 assert(curFile->readMore != NULL);
2604 doing_depend = curFile->depending; /* restore this */
2605 /* get next input buffer, if any */
2606 ptr = curFile->readMore(curFile->readMoreArg, &len);
2607 curFile->buf_ptr = ptr;
2608 curFile->buf_freeIt = ptr;
2609 curFile->buf_end = ptr == NULL ? NULL : ptr + len;
2610 curFile->lineno = curFile->first_lineno;
2612 return true; /* Iterate again */
2614 /* Ensure the makefile (or loop) didn't have mismatched conditionals */
2615 Cond_restore_depth(curFile->cond_depth);
2617 if (curFile->lf != NULL) {
2618 loadedfile_destroy(curFile->lf);
2622 /* Dispose of curFile info */
2623 /* Leak curFile->fname because all the GNodes have pointers to it. */
2624 free(curFile->buf_freeIt);
2625 Vector_Pop(&includes);
2627 if (includes.len == 0) {
2628 /* We've run out of input */
2629 Global_Delete(".PARSEDIR");
2630 Global_Delete(".PARSEFILE");
2631 Global_Delete(".INCLUDEDFROMDIR");
2632 Global_Delete(".INCLUDEDFROMFILE");
2636 curFile = CurFile();
2637 DEBUG2(PARSE, "ParseEOF: returning to file %s, line %d\n",
2638 curFile->fname, curFile->lineno);
2640 ParseSetParseFile(curFile->fname);
2644 typedef enum ParseRawLineResult {
2648 } ParseRawLineResult;
2651 * Parse until the end of a line, taking into account lines that end with
2652 * backslash-newline.
2654 static ParseRawLineResult
2655 ParseRawLine(IFile *curFile, char **out_line, char **out_line_end,
2656 char **out_firstBackslash, char **out_firstComment)
2658 char *line = curFile->buf_ptr;
2660 char *line_end = line;
2661 char *firstBackslash = NULL;
2662 char *firstComment = NULL;
2663 ParseRawLineResult res = PRLR_LINE;
2670 if (p == curFile->buf_end) {
2677 (ch == '\\' && p + 1 < curFile->buf_end && p[1] == '\0')) {
2678 Parse_Error(PARSE_FATAL, "Zero byte read from file");
2682 /* Treat next character after '\' as literal. */
2684 if (firstBackslash == NULL)
2688 if (p + 2 == curFile->buf_end) {
2697 assert(p <= curFile->buf_end);
2702 * Remember the first '#' for comment stripping, unless
2703 * the previous char was '[', as in the modifier ':[#]'.
2705 if (ch == '#' && firstComment == NULL &&
2706 !(p > line && p[-1] == '['))
2707 firstComment = line_end;
2713 /* We are not interested in trailing whitespace. */
2714 if (!ch_isspace(ch))
2719 curFile->buf_ptr = p;
2720 *out_line_end = line_end;
2721 *out_firstBackslash = firstBackslash;
2722 *out_firstComment = firstComment;
2727 * Beginning at start, unescape '\#' to '#' and replace backslash-newline
2728 * with a single space.
2731 UnescapeBackslash(char *line, char *start)
2735 char *spaceStart = line;
2748 /* Delete '\\' at end of buffer */
2753 /* Delete '\\' from before '#' on non-command lines */
2754 if (ch == '#' && line[0] != '\t') {
2760 /* Leave '\\' in buffer for later */
2763 * Make sure we don't delete an escaped ' ' from the
2766 spaceStart = dst + 1;
2772 * Escaped '\n' -- replace following whitespace with a single
2775 pp_skip_hspace(&src);
2779 /* Delete any trailing spaces - eg from empty continuations */
2780 while (dst > spaceStart && ch_isspace(dst[-1]))
2785 typedef enum GetLineMode {
2787 * Return the next line that is neither empty nor a comment.
2788 * Backslash line continuations are folded into a single space.
2789 * A trailing comment, if any, is discarded.
2794 * Return the next line, even if it is empty or a comment.
2795 * Preserve backslash-newline to keep the line numbers correct.
2797 * Used in .for loops to collect the body of the loop while waiting
2798 * for the corresponding .endfor.
2803 * Return the next line that starts with a dot.
2804 * Backslash line continuations are folded into a single space.
2805 * A trailing comment, if any, is discarded.
2807 * Used in .if directives to skip over irrelevant branches while
2808 * waiting for the corresponding .endif.
2813 /* Return the next "interesting" logical line from the current file. */
2815 ParseGetLine(GetLineMode mode)
2817 IFile *curFile = CurFile();
2820 char *firstBackslash;
2824 ParseRawLineResult res = ParseRawLine(curFile,
2825 &line, &line_end, &firstBackslash, &firstComment);
2826 if (res == PRLR_ERROR)
2829 if (line_end == line || firstComment == line) {
2830 if (res == PRLR_EOF)
2832 if (mode != GLM_FOR_BODY)
2836 /* We now have a line of data */
2837 assert(ch_isspace(*line_end));
2840 if (mode == GLM_FOR_BODY)
2841 return line; /* Don't join the physical lines. */
2843 if (mode == GLM_DOT && line[0] != '.')
2848 /* Brutally ignore anything after a non-escaped '#' in non-commands. */
2849 if (firstComment != NULL && line[0] != '\t')
2850 *firstComment = '\0';
2852 /* If we didn't see a '\\' then the in-situ data is fine. */
2853 if (firstBackslash == NULL)
2856 /* Remove escapes from '\n' and '#' */
2857 UnescapeBackslash(line, firstBackslash);
2863 ParseSkippedBranches(void)
2867 while ((line = ParseGetLine(GLM_DOT)) != NULL) {
2868 if (Cond_EvalLine(line) == COND_PARSE)
2871 * TODO: Check for typos in .elif directives
2872 * such as .elsif or .elseif.
2874 * This check will probably duplicate some of
2875 * the code in ParseLine. Most of the code
2876 * there cannot apply, only ParseVarassign and
2877 * ParseDependencyLine can, and to prevent code
2878 * duplication, these would need to be called
2879 * with a flag called onlyCheckSyntax.
2881 * See directive-elif.mk for details.
2885 return line != NULL;
2889 ParseForLoop(const char *line)
2894 rval = For_Eval(line);
2896 return false; /* Not a .for line */
2898 return true; /* Syntax error - error printed, ignore line */
2900 firstLineno = CurFile()->lineno;
2902 /* Accumulate loop lines until matching .endfor */
2904 line = ParseGetLine(GLM_FOR_BODY);
2906 Parse_Error(PARSE_FATAL,
2907 "Unexpected end of file in for loop.");
2910 } while (For_Accum(line));
2912 For_Run(firstLineno); /* Stash each iteration as a new 'input file' */
2914 return true; /* Read next line from for-loop buffer */
2918 * Read an entire line from the input file.
2920 * Empty lines, .if and .for are completely handled by this function,
2921 * leaving only variable assignments, other directives, dependency lines
2922 * and shell commands to the caller.
2925 * A line without its newline and without any trailing whitespace,
2934 line = ParseGetLine(GLM_NONEMPTY);
2942 * The line might be a conditional. Ask the conditional module
2943 * about it and act accordingly
2945 switch (Cond_EvalLine(line)) {
2947 if (!ParseSkippedBranches())
2952 case COND_INVALID: /* Not a conditional line */
2953 if (ParseForLoop(line))
2962 FinishDependencyGroup(void)
2966 if (targets == NULL)
2969 for (ln = targets->first; ln != NULL; ln = ln->next) {
2970 GNode *gn = ln->datum;
2972 Suff_EndTransform(gn);
2975 * Mark the target as already having commands if it does, to
2976 * keep from having shell commands on multiple dependency
2979 if (!Lst_IsEmpty(&gn->commands))
2980 gn->type |= OP_HAS_COMMANDS;
2987 /* Add the command to each target from the current dependency spec. */
2989 ParseLine_ShellCommand(const char *p)
2991 cpp_skip_whitespace(&p);
2993 return; /* skip empty commands */
2995 if (targets == NULL) {
2996 Parse_Error(PARSE_FATAL,
2997 "Unassociated shell command \"%s\"", p);
3002 char *cmd = bmake_strdup(p);
3005 for (ln = targets->first; ln != NULL; ln = ln->next) {
3006 GNode *gn = ln->datum;
3007 ParseAddCmd(gn, cmd);
3010 Lst_Append(&targCmds, cmd);
3016 IsDirective(const char *dir, size_t dirlen, const char *name)
3018 return dirlen == strlen(name) && memcmp(dir, name, dirlen) == 0;
3022 * See if the line starts with one of the known directives, and if so, handle
3026 ParseDirective(char *line)
3028 char *cp = line + 1;
3029 const char *dir, *arg;
3032 pp_skip_whitespace(&cp);
3033 if (IsInclude(cp, false)) {
3039 while (ch_isalpha(*cp) || *cp == '-')
3041 dirlen = (size_t)(cp - dir);
3043 if (*cp != '\0' && !ch_isspace(*cp))
3046 pp_skip_whitespace(&cp);
3049 if (IsDirective(dir, dirlen, "undef")) {
3052 } else if (IsDirective(dir, dirlen, "export")) {
3053 Var_Export(VEM_PLAIN, arg);
3055 } else if (IsDirective(dir, dirlen, "export-env")) {
3056 Var_Export(VEM_ENV, arg);
3058 } else if (IsDirective(dir, dirlen, "export-literal")) {
3059 Var_Export(VEM_LITERAL, arg);
3061 } else if (IsDirective(dir, dirlen, "unexport")) {
3062 Var_UnExport(false, arg);
3064 } else if (IsDirective(dir, dirlen, "unexport-env")) {
3065 Var_UnExport(true, arg);
3067 } else if (IsDirective(dir, dirlen, "info")) {
3068 ParseMessage(PARSE_INFO, "info", arg);
3070 } else if (IsDirective(dir, dirlen, "warning")) {
3071 ParseMessage(PARSE_WARNING, "warning", arg);
3073 } else if (IsDirective(dir, dirlen, "error")) {
3074 ParseMessage(PARSE_FATAL, "error", arg);
3081 ParseVarassign(const char *line)
3085 if (!Parse_IsVar(line, &var))
3088 FinishDependencyGroup();
3089 Parse_Var(&var, SCOPE_GLOBAL);
3094 FindSemicolon(char *p)
3098 for (; *p != '\0'; p++) {
3099 if (*p == '\\' && p[1] != '\0') {
3104 if (*p == '$' && (p[1] == '(' || p[1] == '{'))
3106 else if (level > 0 && (*p == ')' || *p == '}'))
3108 else if (level == 0 && *p == ';')
3115 * dependency -> target... op [source...] [';' command]
3116 * op -> ':' | '::' | '!'
3119 ParseDependencyLine(char *line)
3122 char *expanded_line;
3123 const char *shellcmd = NULL;
3126 * For some reason - probably to make the parser impossible -
3127 * a ';' can be used to separate commands from dependencies.
3128 * Attempt to avoid ';' inside substitution patterns.
3131 char *semicolon = FindSemicolon(line);
3132 if (*semicolon != '\0') {
3133 /* Terminate the dependency list at the ';' */
3135 shellcmd = semicolon + 1;
3140 * We now know it's a dependency line so it needs to have all
3141 * variables expanded before being parsed.
3143 * XXX: Ideally the dependency line would first be split into
3144 * its left-hand side, dependency operator and right-hand side,
3145 * and then each side would be expanded on its own. This would
3146 * allow for the left-hand side to allow only defined variables
3147 * and to allow variables on the right-hand side to be undefined
3150 * Parsing the line first would also prevent that targets
3151 * generated from variable expressions are interpreted as the
3152 * dependency operator, such as in "target${:U\:} middle: source",
3153 * in which the middle is interpreted as a source, not a target.
3156 /* In lint mode, allow undefined variables to appear in
3159 * Ideally, only the right-hand side would allow undefined
3160 * variables since it is common to have optional dependencies.
3161 * Having undefined variables on the left-hand side is more
3162 * unusual though. Since both sides are expanded in a single
3163 * pass, there is not much choice what to do here.
3165 * In normal mode, it does not matter whether undefined
3166 * variables are allowed or not since as of 2020-09-14,
3167 * Var_Parse does not print any parse errors in such a case.
3168 * It simply returns the special empty string var_Error,
3169 * which cannot be detected in the result of Var_Subst. */
3170 emode = opts.strict ? VARE_WANTRES : VARE_UNDEFERR;
3171 (void)Var_Subst(line, SCOPE_CMDLINE, emode, &expanded_line);
3172 /* TODO: handle errors */
3174 /* Need a fresh list for the target nodes */
3175 if (targets != NULL)
3177 targets = Lst_New();
3179 ParseDependency(expanded_line);
3180 free(expanded_line);
3182 if (shellcmd != NULL)
3183 ParseLine_ShellCommand(shellcmd);
3187 ParseLine(char *line)
3190 * Lines that begin with '.' can be pretty much anything:
3191 * - directives like '.include' or '.if',
3192 * - suffix rules like '.c.o:',
3193 * - dependencies for filenames that start with '.',
3194 * - variable assignments like '.tmp=value'.
3196 if (line[0] == '.' && ParseDirective(line))
3199 if (line[0] == '\t') {
3200 ParseLine_ShellCommand(line + 1);
3205 if (IsSysVInclude(line)) {
3207 * It's an S3/S5-style "include".
3209 ParseTraditionalInclude(line);
3215 if (strncmp(line, "export", 6) == 0 && ch_isspace(line[6]) &&
3216 strchr(line, ':') == NULL) {
3218 * It's a Gmake "export".
3220 ParseGmakeExport(line);
3225 if (ParseVarassign(line))
3228 FinishDependencyGroup();
3230 ParseDependencyLine(line);
3234 * Parse a top-level makefile, incorporating its content into the global
3238 * name The name of the file being read
3239 * fd The open file to parse; will be closed at the end
3242 Parse_File(const char *name, int fd)
3244 char *line; /* the line we're working on */
3245 struct loadedfile *lf;
3247 lf = loadfile(name, fd);
3249 assert(targets == NULL);
3254 Parse_SetInput(name, 0, -1, loadedfile_readMore, lf);
3258 while ((line = ParseReadLine()) != NULL) {
3259 DEBUG2(PARSE, "ParseReadLine (%d): '%s'\n",
3260 CurFile()->lineno, line);
3263 /* Reached EOF, but it may be just EOF of an include file. */
3264 } while (ParseEOF());
3266 FinishDependencyGroup();
3269 (void)fflush(stdout);
3270 (void)fprintf(stderr,
3271 "%s: Fatal errors encountered -- cannot continue",
3273 PrintOnError(NULL, NULL);
3278 /* Initialize the parsing module. */
3283 parseIncPath = SearchPath_New();
3284 sysIncPath = SearchPath_New();
3285 defSysIncPath = SearchPath_New();
3286 Vector_Init(&includes, sizeof(IFile));
3289 /* Clean up the parsing module. */
3294 Lst_DoneCall(&targCmds, free);
3295 assert(targets == NULL);
3296 SearchPath_Free(defSysIncPath);
3297 SearchPath_Free(sysIncPath);
3298 SearchPath_Free(parseIncPath);
3299 assert(includes.len == 0);
3300 Vector_Done(&includes);
3306 * Return a list containing the single main target to create.
3307 * If no such target exists, we Punt with an obnoxious error message.
3310 Parse_MainName(GNodeList *mainList)
3312 if (mainNode == NULL)
3313 Punt("no target to make.");
3315 Lst_Append(mainList, mainNode);
3316 if (mainNode->type & OP_DOUBLEDEP)
3317 Lst_AppendAll(mainList, &mainNode->cohorts);
3319 Global_Append(".TARGETS", mainNode->name);
3323 Parse_GetFatals(void)