1 /* $NetBSD: parse.c,v 1.734 2024/07/09 19:43:01 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
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.
57 *
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
68 * SUCH DAMAGE.
69 */
70
71 /*
72 * Parsing of makefiles.
73 *
74 * Parse_File is the main entry point and controls most of the other
75 * functions in this module.
76 *
77 * Interface:
78 * Parse_Init Initialize the module
79 *
80 * Parse_End Clean up the module
81 *
82 * Parse_File Parse a top-level makefile. Included files are
83 * handled by IncludeFile instead.
84 *
85 * Parse_VarAssign
86 * Try to parse the given line as a variable assignment.
87 * Used by MainParseArgs to determine if an argument is
88 * a target or a variable assignment. Used internally
89 * for pretty much the same thing.
90 *
91 * Parse_Error Report a parse error, a warning or an informational
92 * message.
93 *
94 * Parse_MainName Populate the list of targets to create.
95 */
96
97 #include <sys/types.h>
98 #include <sys/stat.h>
99 #include <errno.h>
100 #include <stdarg.h>
101
102 #include "make.h"
103
104 #ifdef HAVE_STDINT_H
105 #include <stdint.h>
106 #endif
107
108 #include "dir.h"
109 #include "job.h"
110 #include "pathnames.h"
111
112 /* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
113 MAKE_RCSID("$NetBSD: parse.c,v 1.734 2024/07/09 19:43:01 rillig Exp $");
114
115 /* Detects a multiple-inclusion guard in a makefile. */
116 typedef enum {
117 GS_START, /* at the beginning of the file */
118 GS_COND, /* after the guard condition */
119 GS_DONE, /* after the closing .endif */
120 GS_NO /* the file is not guarded */
121 } GuardState;
122
123 /* A file being parsed. */
124 typedef struct IncludedFile {
125 FStr name; /* absolute or relative to the cwd */
126 unsigned lineno; /* 1-based */
127 unsigned readLines; /* the number of physical lines that have
128 * been read from the file */
129 unsigned forHeadLineno; /* 1-based */
130 unsigned forBodyReadLines; /* the number of physical lines that have
131 * been read from the file above the body of
132 * the .for loop */
133 unsigned int condMinDepth; /* depth of nested 'if' directives, at the
134 * beginning of the file */
135 bool depending; /* state of doing_depend on EOF */
136
137 Buffer buf; /* the file's content or the body of the .for
138 * loop; either empty or ends with '\n' */
139 char *buf_ptr; /* next char to be read from buf */
140 char *buf_end; /* buf_end[-1] == '\n' */
141
142 GuardState guardState;
143 Guard *guard;
144
145 struct ForLoop *forLoop;
146 } IncludedFile;
147
148 /* Special attributes for target nodes. */
149 typedef enum ParseSpecial {
150 SP_ATTRIBUTE, /* Generic attribute */
151 SP_BEGIN, /* .BEGIN */
152 SP_DEFAULT, /* .DEFAULT */
153 SP_DELETE_ON_ERROR, /* .DELETE_ON_ERROR */
154 SP_END, /* .END */
155 SP_ERROR, /* .ERROR */
156 SP_IGNORE, /* .IGNORE */
157 SP_INCLUDES, /* .INCLUDES; not mentioned in the manual page */
158 SP_INTERRUPT, /* .INTERRUPT */
159 SP_LIBS, /* .LIBS; not mentioned in the manual page */
160 SP_MAIN, /* .MAIN and no user-specified targets to make */
161 SP_META, /* .META */
162 SP_MFLAGS, /* .MFLAGS or .MAKEFLAGS */
163 SP_NOMETA, /* .NOMETA */
164 SP_NOMETA_CMP, /* .NOMETA_CMP */
165 SP_NOPATH, /* .NOPATH */
166 SP_NOREADONLY, /* .NOREADONLY */
167 SP_NOT, /* Not special */
168 SP_NOTPARALLEL, /* .NOTPARALLEL or .NO_PARALLEL */
169 SP_NULL, /* .NULL; not mentioned in the manual page */
170 SP_OBJDIR, /* .OBJDIR */
171 SP_ORDER, /* .ORDER */
172 SP_PARALLEL, /* .PARALLEL; not mentioned in the manual page */
173 SP_PATH, /* .PATH or .PATH.suffix */
174 SP_PHONY, /* .PHONY */
175 SP_POSIX, /* .POSIX; not mentioned in the manual page */
176 SP_PRECIOUS, /* .PRECIOUS */
177 SP_READONLY, /* .READONLY */
178 SP_SHELL, /* .SHELL */
179 SP_SILENT, /* .SILENT */
180 SP_SINGLESHELL, /* .SINGLESHELL; not mentioned in the manual page */
181 SP_STALE, /* .STALE */
182 SP_SUFFIXES, /* .SUFFIXES */
183 SP_SYSPATH, /* .SYSPATH */
184 SP_WAIT /* .WAIT */
185 } ParseSpecial;
186
187 typedef List SearchPathList;
188 typedef ListNode SearchPathListNode;
189
190
191 typedef enum VarAssignOp {
192 VAR_NORMAL, /* = */
193 VAR_APPEND, /* += */
194 VAR_DEFAULT, /* ?= */
195 VAR_SUBST, /* := */
196 VAR_SHELL /* != or :sh= */
197 } VarAssignOp;
198
199 typedef struct VarAssign {
200 char *varname; /* unexpanded */
201 VarAssignOp op;
202 const char *value; /* unexpanded */
203 } VarAssign;
204
205 static bool Parse_IsVar(const char *, VarAssign *);
206 static void Parse_Var(VarAssign *, GNode *);
207
208 /*
209 * The target to be made if no targets are specified in the command line.
210 * This is the first target defined in any of the makefiles.
211 */
212 GNode *mainNode;
213
214 /*
215 * During parsing, the targets from the left-hand side of the currently
216 * active dependency line, or NULL if the current line does not belong to a
217 * dependency line, for example because it is a variable assignment.
218 *
219 * See unit-tests/deptgt.mk, keyword "parse.c:targets".
220 */
221 static GNodeList *targets;
222
223 #ifdef CLEANUP
224 /*
225 * All shell commands for all targets, in no particular order and possibly
226 * with duplicate values. Kept in a separate list since the commands from
227 * .USE or .USEBEFORE nodes are shared with other GNodes, thereby giving up
228 * the easily understandable ownership over the allocated strings.
229 */
230 static StringList targCmds = LST_INIT;
231 #endif
232
233 /*
234 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
235 * is seen, then set to each successive source on the line.
236 */
237 static GNode *order_pred;
238
239 int parseErrors;
240
241 /*
242 * The include chain of makefiles. At index 0 is the top-level makefile from
243 * the command line, followed by the included files or .for loops, up to and
244 * including the current file.
245 *
246 * See PrintStackTrace for how to interpret the data.
247 */
248 static Vector /* of IncludedFile */ includes;
249
250 SearchPath *parseIncPath; /* directories for "..." includes */
251 SearchPath *sysIncPath; /* directories for <...> includes */
252 SearchPath *defSysIncPath; /* default for sysIncPath */
253
254 /*
255 * The parseKeywords table is searched using binary search when deciding
256 * if a target or source is special.
257 */
258 static const struct {
259 const char name[17];
260 ParseSpecial special; /* when used as a target */
261 GNodeType targetAttr; /* when used as a source */
262 } parseKeywords[] = {
263 { ".BEGIN", SP_BEGIN, OP_NONE },
264 { ".DEFAULT", SP_DEFAULT, OP_NONE },
265 { ".DELETE_ON_ERROR", SP_DELETE_ON_ERROR, OP_NONE },
266 { ".END", SP_END, OP_NONE },
267 { ".ERROR", SP_ERROR, OP_NONE },
268 { ".EXEC", SP_ATTRIBUTE, OP_EXEC },
269 { ".IGNORE", SP_IGNORE, OP_IGNORE },
270 { ".INCLUDES", SP_INCLUDES, OP_NONE },
271 { ".INTERRUPT", SP_INTERRUPT, OP_NONE },
272 { ".INVISIBLE", SP_ATTRIBUTE, OP_INVISIBLE },
273 { ".JOIN", SP_ATTRIBUTE, OP_JOIN },
274 { ".LIBS", SP_LIBS, OP_NONE },
275 { ".MADE", SP_ATTRIBUTE, OP_MADE },
276 { ".MAIN", SP_MAIN, OP_NONE },
277 { ".MAKE", SP_ATTRIBUTE, OP_MAKE },
278 { ".MAKEFLAGS", SP_MFLAGS, OP_NONE },
279 { ".META", SP_META, OP_META },
280 { ".MFLAGS", SP_MFLAGS, OP_NONE },
281 { ".NOMETA", SP_NOMETA, OP_NOMETA },
282 { ".NOMETA_CMP", SP_NOMETA_CMP, OP_NOMETA_CMP },
283 { ".NOPATH", SP_NOPATH, OP_NOPATH },
284 { ".NOREADONLY", SP_NOREADONLY, OP_NONE },
285 { ".NOTMAIN", SP_ATTRIBUTE, OP_NOTMAIN },
286 { ".NOTPARALLEL", SP_NOTPARALLEL, OP_NONE },
287 { ".NO_PARALLEL", SP_NOTPARALLEL, OP_NONE },
288 { ".NULL", SP_NULL, OP_NONE },
289 { ".OBJDIR", SP_OBJDIR, OP_NONE },
290 { ".OPTIONAL", SP_ATTRIBUTE, OP_OPTIONAL },
291 { ".ORDER", SP_ORDER, OP_NONE },
292 { ".PARALLEL", SP_PARALLEL, OP_NONE },
293 { ".PATH", SP_PATH, OP_NONE },
294 { ".PHONY", SP_PHONY, OP_PHONY },
295 { ".POSIX", SP_POSIX, OP_NONE },
296 { ".PRECIOUS", SP_PRECIOUS, OP_PRECIOUS },
297 { ".READONLY", SP_READONLY, OP_NONE },
298 { ".RECURSIVE", SP_ATTRIBUTE, OP_MAKE },
299 { ".SHELL", SP_SHELL, OP_NONE },
300 { ".SILENT", SP_SILENT, OP_SILENT },
301 { ".SINGLESHELL", SP_SINGLESHELL, OP_NONE },
302 { ".STALE", SP_STALE, OP_NONE },
303 { ".SUFFIXES", SP_SUFFIXES, OP_NONE },
304 { ".SYSPATH", SP_SYSPATH, OP_NONE },
305 { ".USE", SP_ATTRIBUTE, OP_USE },
306 { ".USEBEFORE", SP_ATTRIBUTE, OP_USEBEFORE },
307 { ".WAIT", SP_WAIT, OP_NONE },
308 };
309
310 enum PosixState posix_state = PS_NOT_YET;
311
312 static HashTable /* full file name -> Guard */ guards;
313
314
315 static List *
Lst_New(void)316 Lst_New(void)
317 {
318 List *list = bmake_malloc(sizeof *list);
319 Lst_Init(list);
320 return list;
321 }
322
323 static void
Lst_Free(List * list)324 Lst_Free(List *list)
325 {
326
327 Lst_Done(list);
328 free(list);
329 }
330
331 static IncludedFile *
GetInclude(size_t i)332 GetInclude(size_t i)
333 {
334 assert(i < includes.len);
335 return Vector_Get(&includes, i);
336 }
337
338 /* The makefile or the body of a .for loop that is currently being read. */
339 static IncludedFile *
CurFile(void)340 CurFile(void)
341 {
342 return GetInclude(includes.len - 1);
343 }
344
345 unsigned int
CurFile_CondMinDepth(void)346 CurFile_CondMinDepth(void)
347 {
348 return CurFile()->condMinDepth;
349 }
350
351 static Buffer
LoadFile(const char * path,int fd)352 LoadFile(const char *path, int fd)
353 {
354 ssize_t n;
355 Buffer buf;
356 size_t bufSize;
357 struct stat st;
358
359 bufSize = fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
360 st.st_size > 0 && st.st_size < 1024 * 1024 * 1024
361 ? (size_t)st.st_size : 1024;
362 Buf_InitSize(&buf, bufSize);
363
364 for (;;) {
365 if (buf.len == buf.cap) {
366 if (buf.cap >= 512 * 1024 * 1024) {
367 Error("%s: file too large", path);
368 exit(2); /* Not 1 so -q can distinguish error */
369 }
370 Buf_Expand(&buf);
371 }
372 assert(buf.len < buf.cap);
373 n = read(fd, buf.data + buf.len, buf.cap - buf.len);
374 if (n < 0) {
375 Error("%s: read error: %s", path, strerror(errno));
376 exit(2); /* Not 1 so -q can distinguish error */
377 }
378 if (n == 0)
379 break;
380
381 buf.len += (size_t)n;
382 }
383 assert(buf.len <= buf.cap);
384
385 if (buf.len > 0 && !Buf_EndsWith(&buf, '\n'))
386 Buf_AddByte(&buf, '\n');
387
388 return buf; /* may not be null-terminated */
389 }
390
391 /*
392 * Print the current chain of .include and .for directives. In Parse_Fatal
393 * or other functions that already print the location, includingInnermost
394 * would be redundant, but in other cases like Error or Fatal it needs to be
395 * included.
396 */
397 void
PrintStackTrace(bool includingInnermost)398 PrintStackTrace(bool includingInnermost)
399 {
400 const IncludedFile *entries;
401 size_t i, n;
402
403 n = includes.len;
404 if (n == 0)
405 return;
406
407 entries = GetInclude(0);
408 if (!includingInnermost && entries[n - 1].forLoop == NULL)
409 n--; /* already in the diagnostic */
410
411 for (i = n; i-- > 0;) {
412 const IncludedFile *entry = entries + i;
413 const char *fname = entry->name.str;
414 char dirbuf[MAXPATHLEN + 1];
415
416 if (fname[0] != '/' && strcmp(fname, "(stdin)") != 0) {
417 const char *realPath = realpath(fname, dirbuf);
418 if (realPath != NULL)
419 fname = realPath;
420 }
421
422 if (entry->forLoop != NULL) {
423 char *details = ForLoop_Details(entry->forLoop);
424 debug_printf("\tin .for loop from %s:%u with %s\n",
425 fname, entry->forHeadLineno, details);
426 free(details);
427 } else if (i + 1 < n && entries[i + 1].forLoop != NULL) {
428 /* entry->lineno is not a useful line number */
429 } else
430 debug_printf("\tin %s:%u\n", fname, entry->lineno);
431 }
432 if (makelevel > 0)
433 debug_printf("\tin directory %s\n", curdir);
434 }
435
436 /* Check if the current character is escaped on the current line. */
437 static bool
IsEscaped(const char * line,const char * p)438 IsEscaped(const char *line, const char *p)
439 {
440 bool escaped = false;
441 while (p > line && *--p == '\\')
442 escaped = !escaped;
443 return escaped;
444 }
445
446 /*
447 * Remember the location (filename and lineno) where the last command was
448 * added or where the node was mentioned in a .depend file.
449 */
450 static void
RememberLocation(GNode * gn)451 RememberLocation(GNode *gn)
452 {
453 IncludedFile *curFile = CurFile();
454 gn->fname = Str_Intern(curFile->name.str);
455 gn->lineno = curFile->lineno;
456 }
457
458 /*
459 * Look in the table of keywords for one matching the given string.
460 * Return the index of the keyword, or -1 if it isn't there.
461 */
462 static int
FindKeyword(const char * str)463 FindKeyword(const char *str)
464 {
465 int start = 0;
466 int end = sizeof parseKeywords / sizeof parseKeywords[0] - 1;
467
468 while (start <= end) {
469 int curr = start + (end - start) / 2;
470 int diff = strcmp(str, parseKeywords[curr].name);
471
472 if (diff == 0)
473 return curr;
474 if (diff < 0)
475 end = curr - 1;
476 else
477 start = curr + 1;
478 }
479
480 return -1;
481 }
482
483 void
PrintLocation(FILE * f,bool useVars,const GNode * gn)484 PrintLocation(FILE *f, bool useVars, const GNode *gn)
485 {
486 char dirbuf[MAXPATHLEN + 1];
487 FStr dir, base;
488 const char *fname;
489 unsigned lineno;
490
491 if (gn != NULL) {
492 fname = gn->fname;
493 lineno = gn->lineno;
494 } else if (includes.len > 0) {
495 IncludedFile *curFile = CurFile();
496 fname = curFile->name.str;
497 lineno = curFile->lineno;
498 } else
499 return;
500
501 if (!useVars || fname[0] == '/' || strcmp(fname, "(stdin)") == 0) {
502 (void)fprintf(f, "\"%s\" line %u: ", fname, lineno);
503 return;
504 }
505
506 dir = Var_Value(SCOPE_GLOBAL, ".PARSEDIR");
507 if (dir.str == NULL)
508 dir.str = ".";
509 if (dir.str[0] != '/')
510 dir.str = realpath(dir.str, dirbuf);
511
512 base = Var_Value(SCOPE_GLOBAL, ".PARSEFILE");
513 if (base.str == NULL)
514 base.str = str_basename(fname);
515
516 (void)fprintf(f, "\"%s/%s\" line %u: ", dir.str, base.str, lineno);
517
518 FStr_Done(&base);
519 FStr_Done(&dir);
520 }
521
522 static void MAKE_ATTR_PRINTFLIKE(5, 0)
ParseVErrorInternal(FILE * f,bool useVars,const GNode * gn,ParseErrorLevel level,const char * fmt,va_list ap)523 ParseVErrorInternal(FILE *f, bool useVars, const GNode *gn,
524 ParseErrorLevel level, const char *fmt, va_list ap)
525 {
526 static bool fatal_warning_error_printed = false;
527
528 (void)fprintf(f, "%s: ", progname);
529
530 PrintLocation(f, useVars, gn);
531 if (level == PARSE_WARNING)
532 (void)fprintf(f, "warning: ");
533 fprintf(f, "%s", EvalStack_Details());
534 (void)vfprintf(f, fmt, ap);
535 (void)fprintf(f, "\n");
536 (void)fflush(f);
537
538 if (level == PARSE_FATAL)
539 parseErrors++;
540 if (level == PARSE_WARNING && opts.parseWarnFatal) {
541 if (!fatal_warning_error_printed) {
542 Error("parsing warnings being treated as errors");
543 fatal_warning_error_printed = true;
544 }
545 parseErrors++;
546 }
547
548 if (level == PARSE_FATAL || DEBUG(PARSE))
549 PrintStackTrace(false);
550 }
551
552 static void MAKE_ATTR_PRINTFLIKE(3, 4)
ParseErrorInternal(const GNode * gn,ParseErrorLevel level,const char * fmt,...)553 ParseErrorInternal(const GNode *gn,
554 ParseErrorLevel level, const char *fmt, ...)
555 {
556 va_list ap;
557
558 (void)fflush(stdout);
559 va_start(ap, fmt);
560 ParseVErrorInternal(stderr, false, gn, level, fmt, ap);
561 va_end(ap);
562
563 if (opts.debug_file != stdout && opts.debug_file != stderr) {
564 va_start(ap, fmt);
565 ParseVErrorInternal(opts.debug_file, false, gn,
566 level, fmt, ap);
567 va_end(ap);
568 }
569 }
570
571 /*
572 * Print a message, including location information.
573 *
574 * If the level is PARSE_FATAL, continue parsing until the end of the
575 * current top-level makefile, then exit (see Parse_File).
576 *
577 * Fmt is given without a trailing newline.
578 */
579 void
Parse_Error(ParseErrorLevel level,const char * fmt,...)580 Parse_Error(ParseErrorLevel level, const char *fmt, ...)
581 {
582 va_list ap;
583
584 (void)fflush(stdout);
585 va_start(ap, fmt);
586 ParseVErrorInternal(stderr, true, NULL, level, fmt, ap);
587 va_end(ap);
588
589 if (opts.debug_file != stdout && opts.debug_file != stderr) {
590 va_start(ap, fmt);
591 ParseVErrorInternal(opts.debug_file, true, NULL,
592 level, fmt, ap);
593 va_end(ap);
594 }
595 }
596
597
598 /*
599 * Handle an .info, .warning or .error directive. For an .error directive,
600 * exit immediately.
601 */
602 static void
HandleMessage(ParseErrorLevel level,const char * levelName,const char * umsg)603 HandleMessage(ParseErrorLevel level, const char *levelName, const char *umsg)
604 {
605 char *xmsg;
606
607 if (umsg[0] == '\0') {
608 Parse_Error(PARSE_FATAL, "Missing argument for \".%s\"",
609 levelName);
610 return;
611 }
612
613 xmsg = Var_Subst(umsg, SCOPE_CMDLINE, VARE_EVAL);
614 /* TODO: handle errors */
615
616 Parse_Error(level, "%s", xmsg);
617 free(xmsg);
618
619 if (level == PARSE_FATAL) {
620 PrintOnError(NULL, "\n");
621 exit(1);
622 }
623 }
624
625 /*
626 * Add the child to the parent's children, and for non-special targets, vice
627 * versa.
628 */
629 static void
LinkSource(GNode * pgn,GNode * cgn,bool isSpecial)630 LinkSource(GNode *pgn, GNode *cgn, bool isSpecial)
631 {
632 if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(&pgn->cohorts))
633 pgn = pgn->cohorts.last->datum;
634
635 Lst_Append(&pgn->children, cgn);
636 pgn->unmade++;
637
638 /*
639 * Special targets like .END do not need to be informed once the child
640 * target has been made.
641 */
642 if (!isSpecial)
643 Lst_Append(&cgn->parents, pgn);
644
645 if (DEBUG(PARSE)) {
646 debug_printf("Target \"%s\" depends on \"%s\"\n",
647 pgn->name, cgn->name);
648 Targ_PrintNode(pgn, 0);
649 Targ_PrintNode(cgn, 0);
650 }
651 }
652
653 /* Add the node to each target from the current dependency group. */
654 static void
LinkToTargets(GNode * gn,bool isSpecial)655 LinkToTargets(GNode *gn, bool isSpecial)
656 {
657 GNodeListNode *ln;
658
659 for (ln = targets->first; ln != NULL; ln = ln->next)
660 LinkSource(ln->datum, gn, isSpecial);
661 }
662
663 static bool
TryApplyDependencyOperator(GNode * gn,GNodeType op)664 TryApplyDependencyOperator(GNode *gn, GNodeType op)
665 {
666 /*
667 * If the node occurred on the left-hand side of a dependency and the
668 * operator also defines a dependency, they must match.
669 */
670 if ((op & OP_OPMASK) && (gn->type & OP_OPMASK) &&
671 ((op & OP_OPMASK) != (gn->type & OP_OPMASK))) {
672 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s",
673 gn->name);
674 return false;
675 }
676
677 if (op == OP_DOUBLEDEP && (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
678 /*
679 * If the node was on the left-hand side of a '::' operator,
680 * create a new node for the children and commands on this
681 * dependency line, since each of these dependency groups has
682 * its own attributes and commands, separate from the others.
683 *
684 * The new instance is placed on the 'cohorts' list of the
685 * initial one (note the initial one is not on its own
686 * cohorts list) and the new instance is linked to all
687 * parents of the initial instance.
688 */
689 GNode *cohort;
690
691 /*
692 * Propagate copied bits to the initial node. They'll be
693 * propagated back to the rest of the cohorts later.
694 */
695 gn->type |= op & (unsigned)~OP_OPMASK;
696
697 cohort = Targ_NewInternalNode(gn->name);
698 if (doing_depend)
699 RememberLocation(cohort);
700 /*
701 * Make the cohort invisible to avoid duplicating it
702 * into other variables. True, parents of this target won't
703 * tend to do anything with their local variables, but better
704 * safe than sorry.
705 *
706 * (I think this is pointless now, since the relevant list
707 * traversals will no longer see this node anyway. -mycroft)
708 */
709 cohort->type = op | OP_INVISIBLE;
710 Lst_Append(&gn->cohorts, cohort);
711 cohort->centurion = gn;
712 gn->unmade_cohorts++;
713 snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
714 (unsigned int)gn->unmade_cohorts % 1000000);
715 } else {
716 gn->type |= op; /* preserve any previous flags */
717 }
718
719 return true;
720 }
721
722 static void
ApplyDependencyOperator(GNodeType op)723 ApplyDependencyOperator(GNodeType op)
724 {
725 GNodeListNode *ln;
726
727 for (ln = targets->first; ln != NULL; ln = ln->next)
728 if (!TryApplyDependencyOperator(ln->datum, op))
729 break;
730 }
731
732 /*
733 * Add a .WAIT node in the dependency list. After any dynamic dependencies
734 * (and filename globbing) have happened, it is given a dependency on each
735 * previous child, back until the previous .WAIT node. The next child won't
736 * be scheduled until the .WAIT node is built.
737 *
738 * Give each .WAIT node a unique name (mainly for diagnostics).
739 */
740 static void
ApplyDependencySourceWait(bool isSpecial)741 ApplyDependencySourceWait(bool isSpecial)
742 {
743 static unsigned wait_number = 0;
744 char name[6 + 10 + 1];
745 GNode *gn;
746
747 snprintf(name, sizeof name, ".WAIT_%u", ++wait_number);
748 gn = Targ_NewInternalNode(name);
749 if (doing_depend)
750 RememberLocation(gn);
751 gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
752 LinkToTargets(gn, isSpecial);
753 }
754
755 static bool
ApplyDependencySourceKeyword(const char * src,ParseSpecial special)756 ApplyDependencySourceKeyword(const char *src, ParseSpecial special)
757 {
758 int keywd;
759 GNodeType targetAttr;
760
761 if (*src != '.' || !ch_isupper(src[1]))
762 return false;
763
764 keywd = FindKeyword(src);
765 if (keywd == -1)
766 return false;
767
768 targetAttr = parseKeywords[keywd].targetAttr;
769 if (targetAttr != OP_NONE) {
770 ApplyDependencyOperator(targetAttr);
771 return true;
772 }
773 if (parseKeywords[keywd].special == SP_WAIT) {
774 ApplyDependencySourceWait(special != SP_NOT);
775 return true;
776 }
777 return false;
778 }
779
780 /*
781 * In a line like ".MAIN: source1 source2", add all sources to the list of
782 * things to create, but only if the user didn't specify a target on the
783 * command line and .MAIN occurs for the first time.
784 *
785 * See HandleDependencyTargetSpecial, branch SP_MAIN.
786 * See unit-tests/cond-func-make-main.mk.
787 */
788 static void
ApplyDependencySourceMain(const char * src)789 ApplyDependencySourceMain(const char *src)
790 {
791 Lst_Append(&opts.create, bmake_strdup(src));
792 /*
793 * Add the name to the .TARGETS variable as well, so the user can
794 * employ that, if desired.
795 */
796 Global_Append(".TARGETS", src);
797 }
798
799 /*
800 * For the sources of a .ORDER target, create predecessor/successor links
801 * between the previous source and the current one.
802 */
803 static void
ApplyDependencySourceOrder(const char * src)804 ApplyDependencySourceOrder(const char *src)
805 {
806 GNode *gn;
807
808 gn = Targ_GetNode(src);
809 if (doing_depend)
810 RememberLocation(gn);
811 if (order_pred != NULL) {
812 Lst_Append(&order_pred->order_succ, gn);
813 Lst_Append(&gn->order_pred, order_pred);
814 if (DEBUG(PARSE)) {
815 debug_printf(
816 "# .ORDER forces '%s' to be made before '%s'\n",
817 order_pred->name, gn->name);
818 Targ_PrintNode(order_pred, 0);
819 Targ_PrintNode(gn, 0);
820 }
821 }
822 /* The current source now becomes the predecessor for the next one. */
823 order_pred = gn;
824 }
825
826 /* The source is not an attribute, so find/create a node for it. */
827 static void
ApplyDependencySourceOther(const char * src,GNodeType targetAttr,ParseSpecial special)828 ApplyDependencySourceOther(const char *src, GNodeType targetAttr,
829 ParseSpecial special)
830 {
831 GNode *gn;
832
833 gn = Targ_GetNode(src);
834 if (doing_depend)
835 RememberLocation(gn);
836 if (targetAttr != OP_NONE)
837 gn->type |= targetAttr;
838 else
839 LinkToTargets(gn, special != SP_NOT);
840 }
841
842 /*
843 * Given the name of a source in a dependency line, figure out if it is an
844 * attribute (such as .SILENT) and if so, apply it to all targets. Otherwise
845 * decide if there is some attribute which should be applied *to* the source
846 * because of some special target (such as .PHONY) and apply it if so.
847 * Otherwise, make the source a child of the targets.
848 */
849 static void
ApplyDependencySource(GNodeType targetAttr,const char * src,ParseSpecial special)850 ApplyDependencySource(GNodeType targetAttr, const char *src,
851 ParseSpecial special)
852 {
853 if (ApplyDependencySourceKeyword(src, special))
854 return;
855
856 if (special == SP_MAIN)
857 ApplyDependencySourceMain(src);
858 else if (special == SP_ORDER)
859 ApplyDependencySourceOrder(src);
860 else
861 ApplyDependencySourceOther(src, targetAttr, special);
862 }
863
864 /*
865 * If we have yet to decide on a main target to make, in the absence of any
866 * user input, we want the first target on the first dependency line that is
867 * actually a real target (i.e. isn't a .USE or .EXEC rule) to be made.
868 */
869 static void
MaybeUpdateMainTarget(void)870 MaybeUpdateMainTarget(void)
871 {
872 GNodeListNode *ln;
873
874 if (mainNode != NULL)
875 return;
876
877 for (ln = targets->first; ln != NULL; ln = ln->next) {
878 GNode *gn = ln->datum;
879 if (GNode_IsMainCandidate(gn)) {
880 DEBUG1(MAKE, "Setting main node to \"%s\"\n",
881 gn->name);
882 mainNode = gn;
883 return;
884 }
885 }
886 }
887
888 static void
InvalidLineType(const char * line,const char * unexpanded_line)889 InvalidLineType(const char *line, const char *unexpanded_line)
890 {
891 if (unexpanded_line[0] == '.') {
892 const char *dirstart = unexpanded_line + 1;
893 const char *dirend;
894 cpp_skip_whitespace(&dirstart);
895 dirend = dirstart;
896 while (ch_isalnum(*dirend) || *dirend == '-')
897 dirend++;
898 Parse_Error(PARSE_FATAL, "Unknown directive \"%.*s\"",
899 (int)(dirend - dirstart), dirstart);
900 } else if (strcmp(line, unexpanded_line) == 0)
901 Parse_Error(PARSE_FATAL, "Invalid line '%s'", line);
902 else
903 Parse_Error(PARSE_FATAL,
904 "Invalid line '%s', expanded to '%s'",
905 unexpanded_line, line);
906 }
907
908 static void
ParseDependencyTargetWord(char ** pp,const char * lstart)909 ParseDependencyTargetWord(char **pp, const char *lstart)
910 {
911 const char *p = *pp;
912
913 while (*p != '\0') {
914 if ((ch_isspace(*p) || *p == '!' || *p == ':' || *p == '(')
915 && !IsEscaped(lstart, p))
916 break;
917
918 if (*p == '$') {
919 FStr val = Var_Parse(&p, SCOPE_CMDLINE, VARE_PARSE);
920 /* TODO: handle errors */
921 FStr_Done(&val);
922 } else
923 p++;
924 }
925
926 *pp += p - *pp;
927 }
928
929 /*
930 * Handle special targets like .PATH, .DEFAULT, .BEGIN, .ORDER.
931 *
932 * See the tests deptgt-*.mk.
933 */
934 static void
HandleDependencyTargetSpecial(const char * targetName,ParseSpecial * inout_special,SearchPathList ** inout_paths)935 HandleDependencyTargetSpecial(const char *targetName,
936 ParseSpecial *inout_special,
937 SearchPathList **inout_paths)
938 {
939 switch (*inout_special) {
940 case SP_PATH:
941 if (*inout_paths == NULL)
942 *inout_paths = Lst_New();
943 Lst_Append(*inout_paths, &dirSearchPath);
944 break;
945 case SP_SYSPATH:
946 if (*inout_paths == NULL)
947 *inout_paths = Lst_New();
948 Lst_Append(*inout_paths, sysIncPath);
949 break;
950 case SP_MAIN:
951 /*
952 * Allow targets from the command line to override the
953 * .MAIN node.
954 */
955 if (!Lst_IsEmpty(&opts.create))
956 *inout_special = SP_NOT;
957 break;
958 case SP_BEGIN:
959 case SP_END:
960 case SP_STALE:
961 case SP_ERROR:
962 case SP_INTERRUPT: {
963 GNode *gn = Targ_GetNode(targetName);
964 if (doing_depend)
965 RememberLocation(gn);
966 gn->type |= OP_NOTMAIN | OP_SPECIAL;
967 Lst_Append(targets, gn);
968 break;
969 }
970 case SP_DEFAULT: {
971 /*
972 * Need to create a node to hang commands on, but we don't
973 * want it in the graph, nor do we want it to be the Main
974 * Target. We claim the node is a transformation rule to make
975 * life easier later, when we'll use Make_HandleUse to
976 * actually apply the .DEFAULT commands.
977 */
978 GNode *gn = GNode_New(".DEFAULT");
979 gn->type |= OP_NOTMAIN | OP_TRANSFORM;
980 Lst_Append(targets, gn);
981 defaultNode = gn;
982 break;
983 }
984 case SP_DELETE_ON_ERROR:
985 deleteOnError = true;
986 break;
987 case SP_NOTPARALLEL:
988 opts.maxJobs = 1;
989 break;
990 case SP_SINGLESHELL:
991 opts.compatMake = true;
992 break;
993 case SP_ORDER:
994 order_pred = NULL;
995 break;
996 default:
997 break;
998 }
999 }
1000
1001 static bool
HandleDependencyTargetPath(const char * suffixName,SearchPathList ** inout_paths)1002 HandleDependencyTargetPath(const char *suffixName,
1003 SearchPathList **inout_paths)
1004 {
1005 SearchPath *path;
1006
1007 path = Suff_GetPath(suffixName);
1008 if (path == NULL) {
1009 Parse_Error(PARSE_FATAL,
1010 "Suffix '%s' not defined (yet)", suffixName);
1011 return false;
1012 }
1013
1014 if (*inout_paths == NULL)
1015 *inout_paths = Lst_New();
1016 Lst_Append(*inout_paths, path);
1017
1018 return true;
1019 }
1020
1021 /* See if it's a special target and if so set inout_special to match it. */
1022 static bool
HandleDependencyTarget(const char * targetName,ParseSpecial * inout_special,GNodeType * inout_targetAttr,SearchPathList ** inout_paths)1023 HandleDependencyTarget(const char *targetName,
1024 ParseSpecial *inout_special,
1025 GNodeType *inout_targetAttr,
1026 SearchPathList **inout_paths)
1027 {
1028 int keywd;
1029
1030 if (!(targetName[0] == '.' && ch_isupper(targetName[1])))
1031 return true;
1032
1033 /*
1034 * See if the target is a special target that must have it
1035 * or its sources handled specially.
1036 */
1037 keywd = FindKeyword(targetName);
1038 if (keywd != -1) {
1039 if (*inout_special == SP_PATH &&
1040 parseKeywords[keywd].special != SP_PATH) {
1041 Parse_Error(PARSE_FATAL, "Mismatched special targets");
1042 return false;
1043 }
1044
1045 *inout_special = parseKeywords[keywd].special;
1046 *inout_targetAttr = parseKeywords[keywd].targetAttr;
1047
1048 HandleDependencyTargetSpecial(targetName, inout_special,
1049 inout_paths);
1050
1051 } else if (strncmp(targetName, ".PATH", 5) == 0) {
1052 *inout_special = SP_PATH;
1053 if (!HandleDependencyTargetPath(targetName + 5, inout_paths))
1054 return false;
1055 }
1056 return true;
1057 }
1058
1059 static void
HandleSingleDependencyTargetMundane(const char * name)1060 HandleSingleDependencyTargetMundane(const char *name)
1061 {
1062 GNode *gn = Suff_IsTransform(name)
1063 ? Suff_AddTransform(name)
1064 : Targ_GetNode(name);
1065 if (doing_depend)
1066 RememberLocation(gn);
1067
1068 Lst_Append(targets, gn);
1069 }
1070
1071 static void
HandleDependencyTargetMundane(const char * targetName)1072 HandleDependencyTargetMundane(const char *targetName)
1073 {
1074 if (Dir_HasWildcards(targetName)) {
1075 StringList targetNames = LST_INIT;
1076
1077 SearchPath *emptyPath = SearchPath_New();
1078 SearchPath_Expand(emptyPath, targetName, &targetNames);
1079 SearchPath_Free(emptyPath);
1080
1081 while (!Lst_IsEmpty(&targetNames)) {
1082 char *targName = Lst_Dequeue(&targetNames);
1083 HandleSingleDependencyTargetMundane(targName);
1084 free(targName);
1085 }
1086 } else
1087 HandleSingleDependencyTargetMundane(targetName);
1088 }
1089
1090 static void
SkipExtraTargets(char ** pp,const char * lstart)1091 SkipExtraTargets(char **pp, const char *lstart)
1092 {
1093 bool warning = false;
1094 const char *p = *pp;
1095
1096 while (*p != '\0') {
1097 if (!IsEscaped(lstart, p) && (*p == '!' || *p == ':'))
1098 break;
1099 if (IsEscaped(lstart, p) || (*p != ' ' && *p != '\t'))
1100 warning = true;
1101 p++;
1102 }
1103 if (warning) {
1104 const char *start = *pp;
1105 cpp_skip_whitespace(&start);
1106 Parse_Error(PARSE_WARNING, "Extra target '%.*s' ignored",
1107 (int)(p - start), start);
1108 }
1109
1110 *pp += p - *pp;
1111 }
1112
1113 static void
CheckSpecialMundaneMixture(ParseSpecial special)1114 CheckSpecialMundaneMixture(ParseSpecial special)
1115 {
1116 switch (special) {
1117 case SP_DEFAULT:
1118 case SP_STALE:
1119 case SP_BEGIN:
1120 case SP_END:
1121 case SP_ERROR:
1122 case SP_INTERRUPT:
1123 /*
1124 * These create nodes on which to hang commands, so targets
1125 * shouldn't be empty.
1126 */
1127 case SP_NOT:
1128 /* Nothing special here -- targets may be empty. */
1129 break;
1130 default:
1131 Parse_Error(PARSE_WARNING,
1132 "Special and mundane targets don't mix. "
1133 "Mundane ones ignored");
1134 break;
1135 }
1136 }
1137
1138 /*
1139 * In a dependency line like 'targets: sources' or 'targets! sources', parse
1140 * the operator ':', '::' or '!' from between the targets and the sources.
1141 */
1142 static GNodeType
ParseDependencyOp(char ** pp)1143 ParseDependencyOp(char **pp)
1144 {
1145 if (**pp == '!')
1146 return (*pp)++, OP_FORCE;
1147 if (**pp == ':' && (*pp)[1] == ':')
1148 return *pp += 2, OP_DOUBLEDEP;
1149 else if (**pp == ':')
1150 return (*pp)++, OP_DEPENDS;
1151 else
1152 return OP_NONE;
1153 }
1154
1155 static void
ClearPaths(ParseSpecial special,SearchPathList * paths)1156 ClearPaths(ParseSpecial special, SearchPathList *paths)
1157 {
1158 if (paths != NULL) {
1159 SearchPathListNode *ln;
1160 for (ln = paths->first; ln != NULL; ln = ln->next)
1161 SearchPath_Clear(ln->datum);
1162 }
1163 if (special == SP_SYSPATH)
1164 Dir_SetSYSPATH();
1165 else
1166 Dir_SetPATH();
1167 }
1168
1169 static char *
FindInDirOfIncludingFile(const char * file)1170 FindInDirOfIncludingFile(const char *file)
1171 {
1172 char *fullname, *incdir, *slash, *newName;
1173 int i;
1174
1175 fullname = NULL;
1176 incdir = bmake_strdup(CurFile()->name.str);
1177 slash = strrchr(incdir, '/');
1178 if (slash != NULL) {
1179 *slash = '\0';
1180 /*
1181 * Now do lexical processing of leading "../" on the
1182 * filename.
1183 */
1184 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
1185 slash = strrchr(incdir + 1, '/');
1186 if (slash == NULL || strcmp(slash, "/..") == 0)
1187 break;
1188 *slash = '\0';
1189 }
1190 newName = str_concat3(incdir, "/", file + i);
1191 fullname = Dir_FindFile(newName, parseIncPath);
1192 if (fullname == NULL)
1193 fullname = Dir_FindFile(newName, &dirSearchPath);
1194 free(newName);
1195 }
1196 free(incdir);
1197 return fullname;
1198 }
1199
1200 static char *
FindInQuotPath(const char * file)1201 FindInQuotPath(const char *file)
1202 {
1203 const char *suff;
1204 SearchPath *suffPath;
1205 char *fullname;
1206
1207 fullname = FindInDirOfIncludingFile(file);
1208 if (fullname == NULL &&
1209 (suff = strrchr(file, '.')) != NULL &&
1210 (suffPath = Suff_GetPath(suff)) != NULL)
1211 fullname = Dir_FindFile(file, suffPath);
1212 if (fullname == NULL)
1213 fullname = Dir_FindFile(file, parseIncPath);
1214 if (fullname == NULL)
1215 fullname = Dir_FindFile(file, &dirSearchPath);
1216 return fullname;
1217 }
1218
1219 static bool
SkipGuarded(const char * fullname)1220 SkipGuarded(const char *fullname)
1221 {
1222 Guard *guard = HashTable_FindValue(&guards, fullname);
1223 if (guard != NULL && guard->kind == GK_VARIABLE
1224 && GNode_ValueDirect(SCOPE_GLOBAL, guard->name) != NULL)
1225 goto skip;
1226 if (guard != NULL && guard->kind == GK_TARGET
1227 && Targ_FindNode(guard->name) != NULL)
1228 goto skip;
1229 return false;
1230
1231 skip:
1232 DEBUG2(PARSE, "Skipping '%s' because '%s' is defined\n",
1233 fullname, guard->name);
1234 return true;
1235 }
1236
1237 /*
1238 * Handle one of the .[-ds]include directives by remembering the current file
1239 * and pushing the included file on the stack. After the included file has
1240 * finished, parsing continues with the including file; see Parse_PushInput
1241 * and ParseEOF.
1242 *
1243 * System includes are looked up in sysIncPath, any other includes are looked
1244 * up in the parsedir and then in the directories specified by the -I command
1245 * line options.
1246 */
1247 static void
IncludeFile(const char * file,bool isSystem,bool depinc,bool silent)1248 IncludeFile(const char *file, bool isSystem, bool depinc, bool silent)
1249 {
1250 Buffer buf;
1251 char *fullname; /* full pathname of file */
1252 int fd;
1253
1254 fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
1255
1256 if (fullname == NULL && !isSystem)
1257 fullname = FindInQuotPath(file);
1258
1259 if (fullname == NULL) {
1260 SearchPath *path = Lst_IsEmpty(&sysIncPath->dirs)
1261 ? defSysIncPath : sysIncPath;
1262 fullname = Dir_FindInclude(file, path);
1263 }
1264
1265 if (fullname == NULL) {
1266 if (!silent)
1267 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1268 return;
1269 }
1270
1271 if (SkipGuarded(fullname))
1272 goto done;
1273
1274 if ((fd = open(fullname, O_RDONLY)) == -1) {
1275 if (!silent)
1276 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
1277 goto done;
1278 }
1279
1280 buf = LoadFile(fullname, fd);
1281 (void)close(fd);
1282
1283 Parse_PushInput(fullname, 1, 0, buf, NULL);
1284 if (depinc)
1285 doing_depend = depinc; /* only turn it on */
1286 done:
1287 free(fullname);
1288 }
1289
1290 /* Handle a "dependency" line like '.SPECIAL:' without any sources. */
1291 static void
HandleDependencySourcesEmpty(ParseSpecial special,SearchPathList * paths)1292 HandleDependencySourcesEmpty(ParseSpecial special, SearchPathList *paths)
1293 {
1294 switch (special) {
1295 case SP_SUFFIXES:
1296 Suff_ClearSuffixes();
1297 break;
1298 case SP_PRECIOUS:
1299 allPrecious = true;
1300 break;
1301 case SP_IGNORE:
1302 opts.ignoreErrors = true;
1303 break;
1304 case SP_SILENT:
1305 opts.silent = true;
1306 break;
1307 case SP_PATH:
1308 case SP_SYSPATH:
1309 ClearPaths(special, paths);
1310 break;
1311 case SP_POSIX:
1312 if (posix_state == PS_NOW_OR_NEVER) {
1313 /*
1314 * With '-r', 'posix.mk' (if it exists)
1315 * can effectively substitute for 'sys.mk',
1316 * otherwise it is an extension.
1317 */
1318 Global_Set("%POSIX", "1003.2");
1319 IncludeFile("posix.mk", true, false, true);
1320 }
1321 break;
1322 default:
1323 break;
1324 }
1325 }
1326
1327 static void
AddToPaths(const char * dir,SearchPathList * paths)1328 AddToPaths(const char *dir, SearchPathList *paths)
1329 {
1330 if (paths != NULL) {
1331 SearchPathListNode *ln;
1332 for (ln = paths->first; ln != NULL; ln = ln->next)
1333 (void)SearchPath_Add(ln->datum, dir);
1334 }
1335 }
1336
1337 /*
1338 * If the target was one that doesn't take files as its sources but takes
1339 * something like suffixes, we take each space-separated word on the line as
1340 * a something and deal with it accordingly.
1341 */
1342 static void
ParseDependencySourceSpecial(ParseSpecial special,const char * word,SearchPathList * paths)1343 ParseDependencySourceSpecial(ParseSpecial special, const char *word,
1344 SearchPathList *paths)
1345 {
1346 switch (special) {
1347 case SP_SUFFIXES:
1348 Suff_AddSuffix(word);
1349 break;
1350 case SP_PATH:
1351 case SP_SYSPATH:
1352 AddToPaths(word, paths);
1353 break;
1354 case SP_INCLUDES:
1355 Suff_AddInclude(word);
1356 break;
1357 case SP_LIBS:
1358 Suff_AddLib(word);
1359 break;
1360 case SP_NOREADONLY:
1361 Var_ReadOnly(word, false);
1362 break;
1363 case SP_NULL:
1364 Suff_SetNull(word);
1365 break;
1366 case SP_OBJDIR:
1367 Main_SetObjdir(false, "%s", word);
1368 break;
1369 case SP_READONLY:
1370 Var_ReadOnly(word, true);
1371 break;
1372 default:
1373 break;
1374 }
1375 }
1376
1377 static bool
ApplyDependencyTarget(char * name,char * nameEnd,ParseSpecial * inout_special,GNodeType * inout_targetAttr,SearchPathList ** inout_paths)1378 ApplyDependencyTarget(char *name, char *nameEnd, ParseSpecial *inout_special,
1379 GNodeType *inout_targetAttr,
1380 SearchPathList **inout_paths)
1381 {
1382 char savedNameEnd = *nameEnd;
1383 *nameEnd = '\0';
1384
1385 if (!HandleDependencyTarget(name, inout_special,
1386 inout_targetAttr, inout_paths))
1387 return false;
1388
1389 if (*inout_special == SP_NOT && *name != '\0')
1390 HandleDependencyTargetMundane(name);
1391 else if (*inout_special == SP_PATH && *name != '.' && *name != '\0')
1392 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", name);
1393
1394 *nameEnd = savedNameEnd;
1395 return true;
1396 }
1397
1398 static bool
ParseDependencyTargets(char ** pp,const char * lstart,ParseSpecial * inout_special,GNodeType * inout_targetAttr,SearchPathList ** inout_paths,const char * unexpanded_line)1399 ParseDependencyTargets(char **pp,
1400 const char *lstart,
1401 ParseSpecial *inout_special,
1402 GNodeType *inout_targetAttr,
1403 SearchPathList **inout_paths,
1404 const char *unexpanded_line)
1405 {
1406 char *p = *pp;
1407
1408 for (;;) {
1409 char *tgt = p;
1410
1411 ParseDependencyTargetWord(&p, lstart);
1412
1413 /*
1414 * If the word is followed by a left parenthesis, it's the
1415 * name of one or more files inside an archive.
1416 */
1417 if (!IsEscaped(lstart, p) && *p == '(') {
1418 p = tgt;
1419 if (!Arch_ParseArchive(&p, targets, SCOPE_CMDLINE)) {
1420 Parse_Error(PARSE_FATAL,
1421 "Error in archive specification: \"%s\"",
1422 tgt);
1423 return false;
1424 }
1425 continue;
1426 }
1427
1428 if (*p == '\0') {
1429 InvalidLineType(lstart, unexpanded_line);
1430 return false;
1431 }
1432
1433 if (!ApplyDependencyTarget(tgt, p, inout_special,
1434 inout_targetAttr, inout_paths))
1435 return false;
1436
1437 if (*inout_special != SP_NOT && *inout_special != SP_PATH)
1438 SkipExtraTargets(&p, lstart);
1439 else
1440 pp_skip_whitespace(&p);
1441
1442 if (*p == '\0')
1443 break;
1444 if ((*p == '!' || *p == ':') && !IsEscaped(lstart, p))
1445 break;
1446 }
1447
1448 *pp = p;
1449 return true;
1450 }
1451
1452 static void
ParseDependencySourcesSpecial(char * start,ParseSpecial special,SearchPathList * paths)1453 ParseDependencySourcesSpecial(char *start,
1454 ParseSpecial special, SearchPathList *paths)
1455 {
1456
1457 while (*start != '\0') {
1458 char savedEnd;
1459 char *end = start;
1460 while (*end != '\0' && !ch_isspace(*end))
1461 end++;
1462 savedEnd = *end;
1463 *end = '\0';
1464 ParseDependencySourceSpecial(special, start, paths);
1465 *end = savedEnd;
1466 if (savedEnd != '\0')
1467 end++;
1468 pp_skip_whitespace(&end);
1469 start = end;
1470 }
1471 }
1472
1473 static void
LinkVarToTargets(VarAssign * var)1474 LinkVarToTargets(VarAssign *var)
1475 {
1476 GNodeListNode *ln;
1477
1478 for (ln = targets->first; ln != NULL; ln = ln->next)
1479 Parse_Var(var, ln->datum);
1480 }
1481
1482 static bool
ParseDependencySourcesMundane(char * start,ParseSpecial special,GNodeType targetAttr)1483 ParseDependencySourcesMundane(char *start,
1484 ParseSpecial special, GNodeType targetAttr)
1485 {
1486 while (*start != '\0') {
1487 char *end = start;
1488 VarAssign var;
1489
1490 /*
1491 * Check for local variable assignment,
1492 * rest of the line is the value.
1493 */
1494 if (Parse_IsVar(start, &var)) {
1495 bool targetVarsEnabled = GetBooleanExpr(
1496 "${.MAKE.TARGET_LOCAL_VARIABLES}", true);
1497
1498 if (targetVarsEnabled)
1499 LinkVarToTargets(&var);
1500 free(var.varname);
1501 if (targetVarsEnabled)
1502 return true;
1503 }
1504
1505 /*
1506 * The targets take real sources, so we must beware of archive
1507 * specifications (i.e. things with left parentheses in them)
1508 * and handle them accordingly.
1509 */
1510 for (; *end != '\0' && !ch_isspace(*end); end++) {
1511 if (*end == '(' && end > start && end[-1] != '$') {
1512 /*
1513 * Only stop for a left parenthesis if it
1514 * isn't at the start of a word (that'll be
1515 * for variable changes later) and isn't
1516 * preceded by a dollar sign (a dynamic
1517 * source).
1518 */
1519 break;
1520 }
1521 }
1522
1523 if (*end == '(') {
1524 GNodeList sources = LST_INIT;
1525 if (!Arch_ParseArchive(&start, &sources,
1526 SCOPE_CMDLINE)) {
1527 Parse_Error(PARSE_FATAL,
1528 "Error in source archive spec \"%s\"",
1529 start);
1530 return false;
1531 }
1532
1533 while (!Lst_IsEmpty(&sources)) {
1534 GNode *gn = Lst_Dequeue(&sources);
1535 ApplyDependencySource(targetAttr, gn->name,
1536 special);
1537 }
1538 Lst_Done(&sources);
1539 end = start;
1540 } else {
1541 if (*end != '\0') {
1542 *end = '\0';
1543 end++;
1544 }
1545
1546 ApplyDependencySource(targetAttr, start, special);
1547 }
1548 pp_skip_whitespace(&end);
1549 start = end;
1550 }
1551 return true;
1552 }
1553
1554 /*
1555 * From a dependency line like 'targets: sources', parse the sources.
1556 *
1557 * See the tests depsrc-*.mk.
1558 */
1559 static void
ParseDependencySources(char * p,GNodeType targetAttr,ParseSpecial special,SearchPathList ** inout_paths)1560 ParseDependencySources(char *p, GNodeType targetAttr,
1561 ParseSpecial special, SearchPathList **inout_paths)
1562 {
1563 if (*p == '\0') {
1564 HandleDependencySourcesEmpty(special, *inout_paths);
1565 } else if (special == SP_MFLAGS) {
1566 Main_ParseArgLine(p);
1567 return;
1568 } else if (special == SP_SHELL) {
1569 if (!Job_ParseShell(p)) {
1570 Parse_Error(PARSE_FATAL,
1571 "improper shell specification");
1572 return;
1573 }
1574 return;
1575 } else if (special == SP_NOTPARALLEL || special == SP_SINGLESHELL ||
1576 special == SP_DELETE_ON_ERROR) {
1577 return;
1578 }
1579
1580 switch (special) {
1581 case SP_INCLUDES:
1582 case SP_LIBS:
1583 case SP_NOREADONLY:
1584 case SP_NULL:
1585 case SP_OBJDIR:
1586 case SP_PATH:
1587 case SP_READONLY:
1588 case SP_SUFFIXES:
1589 case SP_SYSPATH:
1590 ParseDependencySourcesSpecial(p, special, *inout_paths);
1591 if (*inout_paths != NULL) {
1592 Lst_Free(*inout_paths);
1593 *inout_paths = NULL;
1594 }
1595 if (special == SP_PATH)
1596 Dir_SetPATH();
1597 if (special == SP_SYSPATH)
1598 Dir_SetSYSPATH();
1599 break;
1600 default:
1601 assert(*inout_paths == NULL);
1602 if (!ParseDependencySourcesMundane(p, special, targetAttr))
1603 return;
1604 break;
1605 }
1606
1607 MaybeUpdateMainTarget();
1608 }
1609
1610 /*
1611 * Parse a dependency line consisting of targets, followed by a dependency
1612 * operator, optionally followed by sources.
1613 *
1614 * The nodes of the sources are linked as children to the nodes of the
1615 * targets. Nodes are created as necessary.
1616 *
1617 * The operator is applied to each node in the global 'targets' list,
1618 * which is where the nodes found for the targets are kept.
1619 *
1620 * The sources are parsed in much the same way as the targets, except
1621 * that they are expanded using the wildcarding scheme of the C-Shell,
1622 * and a target is created for each expanded word. Each of the resulting
1623 * nodes is then linked to each of the targets as one of its children.
1624 *
1625 * Certain targets and sources such as .PHONY or .PRECIOUS are handled
1626 * specially, see ParseSpecial.
1627 *
1628 * Transformation rules such as '.c.o' are also handled here, see
1629 * Suff_AddTransform.
1630 *
1631 * Upon return, the value of expandedLine is unspecified.
1632 */
1633 static void
ParseDependency(char * expandedLine,const char * unexpandedLine)1634 ParseDependency(char *expandedLine, const char *unexpandedLine)
1635 {
1636 char *p;
1637 SearchPathList *paths; /* search paths to alter when parsing a list
1638 * of .PATH targets */
1639 GNodeType targetAttr; /* from special sources */
1640 ParseSpecial special; /* in special targets, the children are
1641 * linked as children of the parent but not
1642 * vice versa */
1643 GNodeType op;
1644
1645 DEBUG1(PARSE, "ParseDependency(%s)\n", expandedLine);
1646 p = expandedLine;
1647 paths = NULL;
1648 targetAttr = OP_NONE;
1649 special = SP_NOT;
1650
1651 if (!ParseDependencyTargets(&p, expandedLine, &special, &targetAttr,
1652 &paths, unexpandedLine))
1653 goto out;
1654
1655 if (!Lst_IsEmpty(targets))
1656 CheckSpecialMundaneMixture(special);
1657
1658 op = ParseDependencyOp(&p);
1659 if (op == OP_NONE) {
1660 InvalidLineType(expandedLine, unexpandedLine);
1661 goto out;
1662 }
1663 ApplyDependencyOperator(op);
1664
1665 pp_skip_whitespace(&p);
1666
1667 ParseDependencySources(p, targetAttr, special, &paths);
1668
1669 out:
1670 if (paths != NULL)
1671 Lst_Free(paths);
1672 }
1673
1674 /*
1675 * Determine the assignment operator and adjust the end of the variable
1676 * name accordingly.
1677 */
1678 static VarAssign
AdjustVarassignOp(const char * name,const char * nameEnd,const char * op,const char * value)1679 AdjustVarassignOp(const char *name, const char *nameEnd, const char *op,
1680 const char *value)
1681 {
1682 VarAssignOp type;
1683 VarAssign va;
1684
1685 if (op > name && op[-1] == '+') {
1686 op--;
1687 type = VAR_APPEND;
1688
1689 } else if (op > name && op[-1] == '?') {
1690 op--;
1691 type = VAR_DEFAULT;
1692
1693 } else if (op > name && op[-1] == ':') {
1694 op--;
1695 type = VAR_SUBST;
1696
1697 } else if (op > name && op[-1] == '!') {
1698 op--;
1699 type = VAR_SHELL;
1700
1701 } else {
1702 type = VAR_NORMAL;
1703 while (op > name && ch_isspace(op[-1]))
1704 op--;
1705
1706 if (op - name >= 3 && memcmp(op - 3, ":sh", 3) == 0) {
1707 op -= 3;
1708 type = VAR_SHELL;
1709 }
1710 }
1711
1712 va.varname = bmake_strsedup(name, nameEnd < op ? nameEnd : op);
1713 va.op = type;
1714 va.value = value;
1715 return va;
1716 }
1717
1718 /*
1719 * Parse a variable assignment, consisting of a single-word variable name,
1720 * optional whitespace, an assignment operator, optional whitespace and the
1721 * variable value.
1722 *
1723 * Note: There is a lexical ambiguity with assignment modifier characters
1724 * in variable names. This routine interprets the character before the =
1725 * as a modifier. Therefore, an assignment like
1726 * C++=/usr/bin/CC
1727 * is interpreted as "C+ +=" instead of "C++ =".
1728 *
1729 * Used for both lines in a file and command line arguments.
1730 */
1731 static bool
Parse_IsVar(const char * p,VarAssign * out_var)1732 Parse_IsVar(const char *p, VarAssign *out_var)
1733 {
1734 const char *nameStart, *nameEnd, *firstSpace, *eq;
1735 int level = 0;
1736
1737 cpp_skip_hspace(&p); /* Skip to variable name */
1738
1739 /*
1740 * During parsing, the '+' of the operator '+=' is initially parsed
1741 * as part of the variable name. It is later corrected, as is the
1742 * ':sh' modifier. Of these two (nameEnd and eq), the earlier one
1743 * determines the actual end of the variable name.
1744 */
1745
1746 nameStart = p;
1747 firstSpace = NULL;
1748
1749 /* Scan for one of the assignment operators outside an expression. */
1750 while (*p != '\0') {
1751 char ch = *p++;
1752 if (ch == '(' || ch == '{') {
1753 level++;
1754 continue;
1755 }
1756 if (ch == ')' || ch == '}') {
1757 level--;
1758 continue;
1759 }
1760
1761 if (level != 0)
1762 continue;
1763
1764 if ((ch == ' ' || ch == '\t') && firstSpace == NULL)
1765 firstSpace = p - 1;
1766 while (ch == ' ' || ch == '\t')
1767 ch = *p++;
1768
1769 if (ch == '\0')
1770 return false;
1771 if (ch == ':' && p[0] == 's' && p[1] == 'h') {
1772 p += 2;
1773 continue;
1774 }
1775 if (ch == '=')
1776 eq = p - 1;
1777 else if (*p == '=' &&
1778 (ch == '+' || ch == ':' || ch == '?' || ch == '!'))
1779 eq = p;
1780 else if (firstSpace != NULL)
1781 return false;
1782 else
1783 continue;
1784
1785 nameEnd = firstSpace != NULL ? firstSpace : eq;
1786 p = eq + 1;
1787 cpp_skip_whitespace(&p);
1788 *out_var = AdjustVarassignOp(nameStart, nameEnd, eq, p);
1789 return true;
1790 }
1791
1792 return false;
1793 }
1794
1795 /*
1796 * Check for syntax errors such as unclosed expressions or unknown modifiers.
1797 */
1798 static void
VarCheckSyntax(VarAssignOp op,const char * uvalue,GNode * scope)1799 VarCheckSyntax(VarAssignOp op, const char *uvalue, GNode *scope)
1800 {
1801 if (opts.strict) {
1802 if (op != VAR_SUBST && strchr(uvalue, '$') != NULL) {
1803 char *parsedValue = Var_Subst(uvalue,
1804 scope, VARE_PARSE);
1805 /* TODO: handle errors */
1806 free(parsedValue);
1807 }
1808 }
1809 }
1810
1811 /* Perform a variable assignment that uses the operator ':='. */
1812 static void
VarAssign_EvalSubst(GNode * scope,const char * name,const char * uvalue,FStr * out_avalue)1813 VarAssign_EvalSubst(GNode *scope, const char *name, const char *uvalue,
1814 FStr *out_avalue)
1815 {
1816 char *evalue;
1817
1818 /*
1819 * Make sure that we set the variable the first time to nothing
1820 * so that it gets substituted.
1821 *
1822 * TODO: Add a test that demonstrates why this code is needed,
1823 * apart from making the debug log longer.
1824 *
1825 * XXX: The variable name is expanded up to 3 times.
1826 */
1827 if (!Var_ExistsExpand(scope, name))
1828 Var_SetExpand(scope, name, "");
1829
1830 evalue = Var_Subst(uvalue, scope,
1831 VARE_EVAL_KEEP_DOLLAR_AND_UNDEFINED);
1832 /* TODO: handle errors */
1833
1834 Var_SetExpand(scope, name, evalue);
1835
1836 *out_avalue = FStr_InitOwn(evalue);
1837 }
1838
1839 /* Perform a variable assignment that uses the operator '!='. */
1840 static void
VarAssign_EvalShell(const char * name,const char * uvalue,GNode * scope,FStr * out_avalue)1841 VarAssign_EvalShell(const char *name, const char *uvalue, GNode *scope,
1842 FStr *out_avalue)
1843 {
1844 FStr cmd;
1845 char *output, *error;
1846
1847 cmd = FStr_InitRefer(uvalue);
1848 Var_Expand(&cmd, SCOPE_CMDLINE, VARE_EVAL_DEFINED);
1849
1850 output = Cmd_Exec(cmd.str, &error);
1851 Var_SetExpand(scope, name, output);
1852 *out_avalue = FStr_InitOwn(output);
1853 if (error != NULL) {
1854 Parse_Error(PARSE_WARNING, "%s", error);
1855 free(error);
1856 }
1857
1858 FStr_Done(&cmd);
1859 }
1860
1861 /*
1862 * Perform a variable assignment.
1863 *
1864 * The actual value of the variable is returned in *out_true_avalue.
1865 * Especially for VAR_SUBST and VAR_SHELL this can differ from the literal
1866 * value.
1867 *
1868 * Return whether the assignment was actually performed, which is usually
1869 * the case. It is only skipped if the operator is '?=' and the variable
1870 * already exists.
1871 */
1872 static bool
VarAssign_Eval(const char * name,VarAssignOp op,const char * uvalue,GNode * scope,FStr * out_true_avalue)1873 VarAssign_Eval(const char *name, VarAssignOp op, const char *uvalue,
1874 GNode *scope, FStr *out_true_avalue)
1875 {
1876 FStr avalue = FStr_InitRefer(uvalue);
1877
1878 if (op == VAR_APPEND)
1879 Var_AppendExpand(scope, name, uvalue);
1880 else if (op == VAR_SUBST)
1881 VarAssign_EvalSubst(scope, name, uvalue, &avalue);
1882 else if (op == VAR_SHELL)
1883 VarAssign_EvalShell(name, uvalue, scope, &avalue);
1884 else {
1885 /* XXX: The variable name is expanded up to 2 times. */
1886 if (op == VAR_DEFAULT && Var_ExistsExpand(scope, name))
1887 return false;
1888
1889 /* Normal assignment -- just do it. */
1890 Var_SetExpand(scope, name, uvalue);
1891 }
1892
1893 *out_true_avalue = avalue;
1894 return true;
1895 }
1896
1897 static void
VarAssignSpecial(const char * name,const char * avalue)1898 VarAssignSpecial(const char *name, const char *avalue)
1899 {
1900 if (strcmp(name, ".MAKEOVERRIDES") == 0)
1901 Main_ExportMAKEFLAGS(false); /* re-export MAKEFLAGS */
1902 else if (strcmp(name, ".CURDIR") == 0) {
1903 /*
1904 * Someone is being (too?) clever...
1905 * Let's pretend they know what they are doing and
1906 * re-initialize the 'cur' CachedDir.
1907 */
1908 Dir_InitCur(avalue);
1909 Dir_SetPATH();
1910 } else if (strcmp(name, ".MAKE.JOB.PREFIX") == 0)
1911 Job_SetPrefix();
1912 else if (strcmp(name, ".MAKE.EXPORTED") == 0)
1913 Var_ExportVars(avalue);
1914 }
1915
1916 /* Perform the variable assignment in the given scope. */
1917 static void
Parse_Var(VarAssign * var,GNode * scope)1918 Parse_Var(VarAssign *var, GNode *scope)
1919 {
1920 FStr avalue; /* actual value (maybe expanded) */
1921
1922 VarCheckSyntax(var->op, var->value, scope);
1923 if (VarAssign_Eval(var->varname, var->op, var->value, scope, &avalue)) {
1924 VarAssignSpecial(var->varname, avalue.str);
1925 FStr_Done(&avalue);
1926 }
1927 }
1928
1929
1930 /*
1931 * See if the command possibly calls a sub-make by using the
1932 * expressions ${.MAKE}, ${MAKE} or the plain word "make".
1933 */
1934 static bool
MaybeSubMake(const char * cmd)1935 MaybeSubMake(const char *cmd)
1936 {
1937 const char *start;
1938
1939 for (start = cmd; *start != '\0'; start++) {
1940 const char *p = start;
1941 char endc;
1942
1943 /* XXX: What if progname != "make"? */
1944 if (strncmp(p, "make", 4) == 0)
1945 if (start == cmd || !ch_isalnum(p[-1]))
1946 if (!ch_isalnum(p[4]))
1947 return true;
1948
1949 if (*p != '$')
1950 continue;
1951 p++;
1952
1953 if (*p == '{')
1954 endc = '}';
1955 else if (*p == '(')
1956 endc = ')';
1957 else
1958 continue;
1959 p++;
1960
1961 if (*p == '.') /* Accept either ${.MAKE} or ${MAKE}. */
1962 p++;
1963
1964 if (strncmp(p, "MAKE", 4) == 0 && p[4] == endc)
1965 return true;
1966 }
1967 return false;
1968 }
1969
1970 /* Append the command to the target node. */
1971 static void
GNode_AddCommand(GNode * gn,char * cmd)1972 GNode_AddCommand(GNode *gn, char *cmd)
1973 {
1974 if ((gn->type & OP_DOUBLEDEP) && gn->cohorts.last != NULL)
1975 gn = gn->cohorts.last->datum;
1976
1977 /* if target already supplied, ignore commands */
1978 if (!(gn->type & OP_HAS_COMMANDS)) {
1979 Lst_Append(&gn->commands, cmd);
1980 if (MaybeSubMake(cmd))
1981 gn->type |= OP_SUBMAKE;
1982 RememberLocation(gn);
1983 } else {
1984 Parse_Error(PARSE_WARNING,
1985 "duplicate script for target \"%s\" ignored",
1986 gn->name);
1987 ParseErrorInternal(gn, PARSE_WARNING,
1988 "using previous script for \"%s\" defined here",
1989 gn->name);
1990 }
1991 }
1992
1993 /*
1994 * Parse a directive like '.include' or '.-include'.
1995 *
1996 * .include "user-makefile.mk"
1997 * .include <system-makefile.mk>
1998 */
1999 static void
ParseInclude(char * directive)2000 ParseInclude(char *directive)
2001 {
2002 char endc; /* '>' or '"' */
2003 char *p;
2004 bool silent = directive[0] != 'i';
2005 FStr file;
2006
2007 p = directive + (silent ? 8 : 7);
2008 pp_skip_hspace(&p);
2009
2010 if (*p != '"' && *p != '<') {
2011 Parse_Error(PARSE_FATAL,
2012 ".include filename must be delimited by '\"' or '<'");
2013 return;
2014 }
2015
2016 endc = *p++ == '<' ? '>' : '"';
2017 file = FStr_InitRefer(p);
2018
2019 while (*p != '\0' && *p != endc)
2020 p++;
2021
2022 if (*p != endc) {
2023 Parse_Error(PARSE_FATAL,
2024 "Unclosed .include filename. '%c' expected", endc);
2025 return;
2026 }
2027
2028 *p = '\0';
2029
2030 Var_Expand(&file, SCOPE_CMDLINE, VARE_EVAL);
2031 IncludeFile(file.str, endc == '>', directive[0] == 'd', silent);
2032 FStr_Done(&file);
2033 }
2034
2035 /*
2036 * Split filename into dirname + basename, then assign these to the
2037 * given variables.
2038 */
2039 static void
SetFilenameVars(const char * filename,const char * dirvar,const char * filevar)2040 SetFilenameVars(const char *filename, const char *dirvar, const char *filevar)
2041 {
2042 const char *slash, *basename;
2043 FStr dirname;
2044
2045 slash = strrchr(filename, '/');
2046 if (slash == NULL) {
2047 dirname = FStr_InitRefer(curdir);
2048 basename = filename;
2049 } else {
2050 dirname = FStr_InitOwn(bmake_strsedup(filename, slash));
2051 basename = slash + 1;
2052 }
2053
2054 Global_Set(dirvar, dirname.str);
2055 Global_Set(filevar, basename);
2056
2057 DEBUG4(PARSE, "SetFilenameVars: ${%s} = `%s' ${%s} = `%s'\n",
2058 dirvar, dirname.str, filevar, basename);
2059 FStr_Done(&dirname);
2060 }
2061
2062 /*
2063 * Return the immediately including file.
2064 *
2065 * This is made complicated since the .for loop is implemented as a special
2066 * kind of .include; see For_Run.
2067 */
2068 static const char *
GetActuallyIncludingFile(void)2069 GetActuallyIncludingFile(void)
2070 {
2071 size_t i;
2072 const IncludedFile *incs = GetInclude(0);
2073
2074 for (i = includes.len; i >= 2; i--)
2075 if (incs[i - 1].forLoop == NULL)
2076 return incs[i - 2].name.str;
2077 return NULL;
2078 }
2079
2080 /* Set .PARSEDIR, .PARSEFILE, .INCLUDEDFROMDIR and .INCLUDEDFROMFILE. */
2081 static void
SetParseFile(const char * filename)2082 SetParseFile(const char *filename)
2083 {
2084 const char *including;
2085
2086 SetFilenameVars(filename, ".PARSEDIR", ".PARSEFILE");
2087
2088 including = GetActuallyIncludingFile();
2089 if (including != NULL) {
2090 SetFilenameVars(including,
2091 ".INCLUDEDFROMDIR", ".INCLUDEDFROMFILE");
2092 } else {
2093 Global_Delete(".INCLUDEDFROMDIR");
2094 Global_Delete(".INCLUDEDFROMFILE");
2095 }
2096 }
2097
2098 static bool
StrContainsWord(const char * str,const char * word)2099 StrContainsWord(const char *str, const char *word)
2100 {
2101 size_t strLen = strlen(str);
2102 size_t wordLen = strlen(word);
2103 const char *p;
2104
2105 if (strLen < wordLen)
2106 return false;
2107
2108 for (p = str; p != NULL; p = strchr(p, ' ')) {
2109 if (*p == ' ')
2110 p++;
2111 if (p > str + strLen - wordLen)
2112 return false;
2113
2114 if (memcmp(p, word, wordLen) == 0 &&
2115 (p[wordLen] == '\0' || p[wordLen] == ' '))
2116 return true;
2117 }
2118 return false;
2119 }
2120
2121 /*
2122 * XXX: Searching through a set of words with this linear search is
2123 * inefficient for variables that contain thousands of words.
2124 *
2125 * XXX: The paths in this list don't seem to be normalized in any way.
2126 */
2127 static bool
VarContainsWord(const char * varname,const char * word)2128 VarContainsWord(const char *varname, const char *word)
2129 {
2130 FStr val = Var_Value(SCOPE_GLOBAL, varname);
2131 bool found = val.str != NULL && StrContainsWord(val.str, word);
2132 FStr_Done(&val);
2133 return found;
2134 }
2135
2136 /*
2137 * Track the makefiles we read - so makefiles can set dependencies on them.
2138 * Avoid adding anything more than once.
2139 *
2140 * Time complexity: O(n) per call, in total O(n^2), where n is the number
2141 * of makefiles that have been loaded.
2142 */
2143 static void
TrackInput(const char * name)2144 TrackInput(const char *name)
2145 {
2146 if (!VarContainsWord(".MAKE.MAKEFILES", name))
2147 Global_Append(".MAKE.MAKEFILES", name);
2148 }
2149
2150
2151 /* Parse from the given buffer, later return to the current file. */
2152 void
Parse_PushInput(const char * name,unsigned lineno,unsigned readLines,Buffer buf,struct ForLoop * forLoop)2153 Parse_PushInput(const char *name, unsigned lineno, unsigned readLines,
2154 Buffer buf, struct ForLoop *forLoop)
2155 {
2156 IncludedFile *curFile;
2157
2158 if (forLoop != NULL)
2159 name = CurFile()->name.str;
2160 else
2161 TrackInput(name);
2162
2163 DEBUG3(PARSE, "Parse_PushInput: %s %s, line %u\n",
2164 forLoop != NULL ? ".for loop in": "file", name, lineno);
2165
2166 curFile = Vector_Push(&includes);
2167 curFile->name = FStr_InitOwn(bmake_strdup(name));
2168 curFile->lineno = lineno;
2169 curFile->readLines = readLines;
2170 curFile->forHeadLineno = lineno;
2171 curFile->forBodyReadLines = readLines;
2172 curFile->buf = buf;
2173 curFile->depending = doing_depend; /* restore this on EOF */
2174 curFile->guardState = forLoop == NULL ? GS_START : GS_NO;
2175 curFile->guard = NULL;
2176 curFile->forLoop = forLoop;
2177
2178 if (forLoop != NULL && !For_NextIteration(forLoop, &curFile->buf))
2179 abort(); /* see For_Run */
2180
2181 curFile->buf_ptr = curFile->buf.data;
2182 curFile->buf_end = curFile->buf.data + curFile->buf.len;
2183 curFile->condMinDepth = cond_depth;
2184 SetParseFile(name);
2185 }
2186
2187 /* Check if the directive is an include directive. */
2188 static bool
IsInclude(const char * dir,bool sysv)2189 IsInclude(const char *dir, bool sysv)
2190 {
2191 if (dir[0] == 's' || dir[0] == '-' || (dir[0] == 'd' && !sysv))
2192 dir++;
2193
2194 if (strncmp(dir, "include", 7) != 0)
2195 return false;
2196
2197 /* Space is not mandatory for BSD .include */
2198 return !sysv || ch_isspace(dir[7]);
2199 }
2200
2201
2202 /* Check if the line is a SYSV include directive. */
2203 static bool
IsSysVInclude(const char * line)2204 IsSysVInclude(const char *line)
2205 {
2206 const char *p;
2207
2208 if (!IsInclude(line, true))
2209 return false;
2210
2211 /* Avoid interpreting a dependency line as an include */
2212 for (p = line; (p = strchr(p, ':')) != NULL;) {
2213
2214 /* end of line -> it's a dependency */
2215 if (*++p == '\0')
2216 return false;
2217
2218 /* '::' operator or ': ' -> it's a dependency */
2219 if (*p == ':' || ch_isspace(*p))
2220 return false;
2221 }
2222 return true;
2223 }
2224
2225 /* Push to another file. The line points to the word "include". */
2226 static void
ParseTraditionalInclude(char * line)2227 ParseTraditionalInclude(char *line)
2228 {
2229 char *p; /* current position in file spec */
2230 bool done = false;
2231 bool silent = line[0] != 'i';
2232 char *file = line + (silent ? 8 : 7);
2233 char *all_files;
2234
2235 DEBUG1(PARSE, "ParseTraditionalInclude: %s\n", file);
2236
2237 pp_skip_whitespace(&file);
2238
2239 all_files = Var_Subst(file, SCOPE_CMDLINE, VARE_EVAL);
2240 /* TODO: handle errors */
2241
2242 for (file = all_files; !done; file = p + 1) {
2243 /* Skip to end of line or next whitespace */
2244 for (p = file; *p != '\0' && !ch_isspace(*p); p++)
2245 continue;
2246
2247 if (*p != '\0')
2248 *p = '\0';
2249 else
2250 done = true;
2251
2252 IncludeFile(file, false, false, silent);
2253 }
2254
2255 free(all_files);
2256 }
2257
2258 /* Parse "export <variable>=<value>", and actually export it. */
2259 static void
ParseGmakeExport(char * line)2260 ParseGmakeExport(char *line)
2261 {
2262 char *variable = line + 6;
2263 char *value;
2264
2265 DEBUG1(PARSE, "ParseGmakeExport: %s\n", variable);
2266
2267 pp_skip_whitespace(&variable);
2268
2269 for (value = variable; *value != '\0' && *value != '='; value++)
2270 continue;
2271
2272 if (*value != '=') {
2273 Parse_Error(PARSE_FATAL,
2274 "Variable/Value missing from \"export\"");
2275 return;
2276 }
2277 *value++ = '\0'; /* terminate variable */
2278
2279 /*
2280 * Expand the value before putting it in the environment.
2281 */
2282 value = Var_Subst(value, SCOPE_CMDLINE, VARE_EVAL);
2283 /* TODO: handle errors */
2284
2285 setenv(variable, value, 1);
2286 free(value);
2287 }
2288
2289 /*
2290 * When the end of the current file or .for loop is reached, continue reading
2291 * the previous file at the previous location.
2292 *
2293 * Results:
2294 * true to continue parsing, i.e. it had only reached the end of an
2295 * included file, false if the main file has been parsed completely.
2296 */
2297 static bool
ParseEOF(void)2298 ParseEOF(void)
2299 {
2300 IncludedFile *curFile = CurFile();
2301
2302 doing_depend = curFile->depending;
2303 if (curFile->forLoop != NULL &&
2304 For_NextIteration(curFile->forLoop, &curFile->buf)) {
2305 curFile->buf_ptr = curFile->buf.data;
2306 curFile->buf_end = curFile->buf.data + curFile->buf.len;
2307 curFile->readLines = curFile->forBodyReadLines;
2308 return true;
2309 }
2310
2311 Cond_EndFile();
2312
2313 if (curFile->guardState == GS_DONE) {
2314 HashEntry *he = HashTable_CreateEntry(&guards,
2315 curFile->name.str, NULL);
2316 if (he->value != NULL) {
2317 free(((Guard *)he->value)->name);
2318 free(he->value);
2319 }
2320 HashEntry_Set(he, curFile->guard);
2321 } else if (curFile->guard != NULL) {
2322 free(curFile->guard->name);
2323 free(curFile->guard);
2324 }
2325
2326 FStr_Done(&curFile->name);
2327 Buf_Done(&curFile->buf);
2328 if (curFile->forLoop != NULL)
2329 ForLoop_Free(curFile->forLoop);
2330 Vector_Pop(&includes);
2331
2332 if (includes.len == 0) {
2333 /* We've run out of input */
2334 Global_Delete(".PARSEDIR");
2335 Global_Delete(".PARSEFILE");
2336 Global_Delete(".INCLUDEDFROMDIR");
2337 Global_Delete(".INCLUDEDFROMFILE");
2338 return false;
2339 }
2340
2341 curFile = CurFile();
2342 DEBUG2(PARSE, "ParseEOF: returning to file %s, line %u\n",
2343 curFile->name.str, curFile->readLines + 1);
2344
2345 SetParseFile(curFile->name.str);
2346 return true;
2347 }
2348
2349 typedef enum ParseRawLineResult {
2350 PRLR_LINE,
2351 PRLR_EOF,
2352 PRLR_ERROR
2353 } ParseRawLineResult;
2354
2355 /*
2356 * Parse until the end of a line, taking into account lines that end with
2357 * backslash-newline. The resulting line goes from out_line to out_line_end;
2358 * the line is not null-terminated.
2359 */
2360 static ParseRawLineResult
ParseRawLine(IncludedFile * curFile,char ** out_line,char ** out_line_end,char ** out_firstBackslash,char ** out_commentLineEnd)2361 ParseRawLine(IncludedFile *curFile, char **out_line, char **out_line_end,
2362 char **out_firstBackslash, char **out_commentLineEnd)
2363 {
2364 char *line = curFile->buf_ptr;
2365 char *buf_end = curFile->buf_end;
2366 char *p = line;
2367 char *line_end = line;
2368 char *firstBackslash = NULL;
2369 char *commentLineEnd = NULL;
2370 ParseRawLineResult res = PRLR_LINE;
2371
2372 curFile->readLines++;
2373
2374 for (;;) {
2375 char ch;
2376
2377 if (p == buf_end) {
2378 res = PRLR_EOF;
2379 break;
2380 }
2381
2382 ch = *p;
2383 if (ch == '\0' || (ch == '\\' && p[1] == '\0')) {
2384 Parse_Error(PARSE_FATAL, "Zero byte read from file");
2385 exit(2);
2386 }
2387
2388 /* Treat next character after '\' as literal. */
2389 if (ch == '\\') {
2390 if (firstBackslash == NULL)
2391 firstBackslash = p;
2392 if (p[1] == '\n') {
2393 curFile->readLines++;
2394 if (p + 2 == buf_end) {
2395 line_end = p;
2396 *line_end = '\n';
2397 p += 2;
2398 continue;
2399 }
2400 }
2401 p += 2;
2402 line_end = p;
2403 assert(p <= buf_end);
2404 continue;
2405 }
2406
2407 /*
2408 * Remember the first '#' for comment stripping, unless
2409 * the previous char was '[', as in the modifier ':[#]'.
2410 */
2411 if (ch == '#' && commentLineEnd == NULL &&
2412 !(p > line && p[-1] == '['))
2413 commentLineEnd = line_end;
2414
2415 p++;
2416 if (ch == '\n')
2417 break;
2418
2419 /* We are not interested in trailing whitespace. */
2420 if (!ch_isspace(ch))
2421 line_end = p;
2422 }
2423
2424 curFile->buf_ptr = p;
2425 *out_line = line;
2426 *out_line_end = line_end;
2427 *out_firstBackslash = firstBackslash;
2428 *out_commentLineEnd = commentLineEnd;
2429 return res;
2430 }
2431
2432 /*
2433 * Beginning at start, unescape '\#' to '#' and replace backslash-newline
2434 * with a single space.
2435 */
2436 static void
UnescapeBackslash(char * line,char * start)2437 UnescapeBackslash(char *line, char *start)
2438 {
2439 const char *src = start;
2440 char *dst = start;
2441 char *spaceStart = line;
2442
2443 for (;;) {
2444 char ch = *src++;
2445 if (ch != '\\') {
2446 if (ch == '\0')
2447 break;
2448 *dst++ = ch;
2449 continue;
2450 }
2451
2452 ch = *src++;
2453 if (ch == '\0') {
2454 /* Delete '\\' at the end of the buffer. */
2455 dst--;
2456 break;
2457 }
2458
2459 /* Delete '\\' from before '#' on non-command lines. */
2460 if (ch == '#' && line[0] != '\t')
2461 *dst++ = ch;
2462 else if (ch == '\n') {
2463 cpp_skip_hspace(&src);
2464 *dst++ = ' ';
2465 } else {
2466 /* Leave '\\' in the buffer for later. */
2467 *dst++ = '\\';
2468 *dst++ = ch;
2469 /* Keep an escaped ' ' at the line end. */
2470 spaceStart = dst;
2471 }
2472 }
2473
2474 /* Delete any trailing spaces - eg from empty continuations */
2475 while (dst > spaceStart && ch_isspace(dst[-1]))
2476 dst--;
2477 *dst = '\0';
2478 }
2479
2480 typedef enum LineKind {
2481 /*
2482 * Return the next line that is neither empty nor a comment.
2483 * Backslash line continuations are folded into a single space.
2484 * A trailing comment, if any, is discarded.
2485 */
2486 LK_NONEMPTY,
2487
2488 /*
2489 * Return the next line, even if it is empty or a comment.
2490 * Preserve backslash-newline to keep the line numbers correct.
2491 *
2492 * Used in .for loops to collect the body of the loop while waiting
2493 * for the corresponding .endfor.
2494 */
2495 LK_FOR_BODY,
2496
2497 /*
2498 * Return the next line that starts with a dot.
2499 * Backslash line continuations are folded into a single space.
2500 * A trailing comment, if any, is discarded.
2501 *
2502 * Used in .if directives to skip over irrelevant branches while
2503 * waiting for the corresponding .endif.
2504 */
2505 LK_DOT
2506 } LineKind;
2507
2508 /*
2509 * Return the next "interesting" logical line from the current file. The
2510 * returned string will be freed at the end of including the file.
2511 */
2512 static char *
ReadLowLevelLine(LineKind kind)2513 ReadLowLevelLine(LineKind kind)
2514 {
2515 IncludedFile *curFile = CurFile();
2516 ParseRawLineResult res;
2517 char *line;
2518 char *line_end;
2519 char *firstBackslash;
2520 char *commentLineEnd;
2521
2522 for (;;) {
2523 curFile->lineno = curFile->readLines + 1;
2524 res = ParseRawLine(curFile,
2525 &line, &line_end, &firstBackslash, &commentLineEnd);
2526 if (res == PRLR_ERROR)
2527 return NULL;
2528
2529 if (line == line_end || line == commentLineEnd) {
2530 if (res == PRLR_EOF)
2531 return NULL;
2532 if (kind != LK_FOR_BODY)
2533 continue;
2534 }
2535
2536 /* We now have a line of data */
2537 assert(ch_isspace(*line_end));
2538 *line_end = '\0';
2539
2540 if (kind == LK_FOR_BODY)
2541 return line; /* Don't join the physical lines. */
2542
2543 if (kind == LK_DOT && line[0] != '.')
2544 continue;
2545 break;
2546 }
2547
2548 if (commentLineEnd != NULL && line[0] != '\t')
2549 *commentLineEnd = '\0';
2550 if (firstBackslash != NULL)
2551 UnescapeBackslash(line, firstBackslash);
2552 return line;
2553 }
2554
2555 static bool
SkipIrrelevantBranches(void)2556 SkipIrrelevantBranches(void)
2557 {
2558 const char *line;
2559
2560 while ((line = ReadLowLevelLine(LK_DOT)) != NULL)
2561 if (Cond_EvalLine(line) == CR_TRUE)
2562 return true;
2563 return false;
2564 }
2565
2566 static bool
ParseForLoop(const char * line)2567 ParseForLoop(const char *line)
2568 {
2569 int rval;
2570 unsigned forHeadLineno;
2571 unsigned bodyReadLines;
2572 int forLevel;
2573
2574 rval = For_Eval(line);
2575 if (rval == 0)
2576 return false; /* Not a .for line */
2577 if (rval < 0)
2578 return true; /* Syntax error - error printed, ignore line */
2579
2580 forHeadLineno = CurFile()->lineno;
2581 bodyReadLines = CurFile()->readLines;
2582
2583 /* Accumulate the loop body until the matching '.endfor'. */
2584 forLevel = 1;
2585 do {
2586 line = ReadLowLevelLine(LK_FOR_BODY);
2587 if (line == NULL) {
2588 Parse_Error(PARSE_FATAL,
2589 "Unexpected end of file in .for loop");
2590 break;
2591 }
2592 } while (For_Accum(line, &forLevel));
2593
2594 For_Run(forHeadLineno, bodyReadLines);
2595 return true;
2596 }
2597
2598 /*
2599 * Read an entire line from the input file.
2600 *
2601 * Empty lines, .if and .for are handled by this function, while variable
2602 * assignments, other directives, dependency lines and shell commands are
2603 * handled by the caller.
2604 *
2605 * Return a line without trailing whitespace, or NULL for EOF. The returned
2606 * string will be freed at the end of including the file.
2607 */
2608 static char *
ReadHighLevelLine(void)2609 ReadHighLevelLine(void)
2610 {
2611 char *line;
2612 CondResult condResult;
2613
2614 for (;;) {
2615 IncludedFile *curFile = CurFile();
2616 line = ReadLowLevelLine(LK_NONEMPTY);
2617 if (posix_state == PS_MAYBE_NEXT_LINE)
2618 posix_state = PS_NOW_OR_NEVER;
2619 else
2620 posix_state = PS_TOO_LATE;
2621 if (line == NULL)
2622 return NULL;
2623
2624 DEBUG2(PARSE, "Parsing line %u: %s\n", curFile->lineno, line);
2625 if (curFile->guardState != GS_NO
2626 && ((curFile->guardState == GS_START && line[0] != '.')
2627 || curFile->guardState == GS_DONE))
2628 curFile->guardState = GS_NO;
2629 if (line[0] != '.')
2630 return line;
2631
2632 condResult = Cond_EvalLine(line);
2633 if (curFile->guardState == GS_START) {
2634 Guard *guard;
2635 if (condResult != CR_ERROR
2636 && (guard = Cond_ExtractGuard(line)) != NULL) {
2637 curFile->guardState = GS_COND;
2638 curFile->guard = guard;
2639 } else
2640 curFile->guardState = GS_NO;
2641 }
2642 switch (condResult) {
2643 case CR_FALSE: /* May also mean a syntax error. */
2644 if (!SkipIrrelevantBranches())
2645 return NULL;
2646 continue;
2647 case CR_TRUE:
2648 continue;
2649 case CR_ERROR: /* Not a conditional line */
2650 if (ParseForLoop(line))
2651 continue;
2652 break;
2653 }
2654 return line;
2655 }
2656 }
2657
2658 static void
FinishDependencyGroup(void)2659 FinishDependencyGroup(void)
2660 {
2661 GNodeListNode *ln;
2662
2663 if (targets == NULL)
2664 return;
2665
2666 for (ln = targets->first; ln != NULL; ln = ln->next) {
2667 GNode *gn = ln->datum;
2668
2669 Suff_EndTransform(gn);
2670
2671 /*
2672 * Mark the target as already having commands if it does, to
2673 * keep from having shell commands on multiple dependency
2674 * lines.
2675 */
2676 if (!Lst_IsEmpty(&gn->commands))
2677 gn->type |= OP_HAS_COMMANDS;
2678 }
2679
2680 Lst_Free(targets);
2681 targets = NULL;
2682 }
2683
2684 #ifdef CLEANUP
Parse_RegisterCommand(char * cmd)2685 void Parse_RegisterCommand(char *cmd)
2686 {
2687 Lst_Append(&targCmds, cmd);
2688 }
2689 #endif
2690
2691 /* Add the command to each target from the current dependency spec. */
2692 static void
ParseLine_ShellCommand(const char * p)2693 ParseLine_ShellCommand(const char *p)
2694 {
2695 cpp_skip_whitespace(&p);
2696 if (*p == '\0')
2697 return; /* skip empty commands */
2698
2699 if (targets == NULL) {
2700 Parse_Error(PARSE_FATAL,
2701 "Unassociated shell command \"%s\"", p);
2702 return;
2703 }
2704
2705 {
2706 char *cmd = bmake_strdup(p);
2707 GNodeListNode *ln;
2708
2709 for (ln = targets->first; ln != NULL; ln = ln->next) {
2710 GNode *gn = ln->datum;
2711 GNode_AddCommand(gn, cmd);
2712 }
2713 Parse_RegisterCommand(cmd);
2714 }
2715 }
2716
2717 static void
HandleBreak(const char * arg)2718 HandleBreak(const char *arg)
2719 {
2720 IncludedFile *curFile = CurFile();
2721
2722 if (arg[0] != '\0')
2723 Parse_Error(PARSE_FATAL,
2724 "The .break directive does not take arguments");
2725
2726 if (curFile->forLoop != NULL) {
2727 /* pretend we reached EOF */
2728 For_Break(curFile->forLoop);
2729 cond_depth = CurFile_CondMinDepth();
2730 ParseEOF();
2731 } else
2732 Parse_Error(PARSE_FATAL, "break outside of for loop");
2733 }
2734
2735 /*
2736 * See if the line starts with one of the known directives, and if so, handle
2737 * the directive.
2738 */
2739 static bool
ParseDirective(char * line)2740 ParseDirective(char *line)
2741 {
2742 char *p = line + 1;
2743 const char *arg;
2744 Substring dir;
2745
2746 pp_skip_whitespace(&p);
2747 if (IsInclude(p, false)) {
2748 ParseInclude(p);
2749 return true;
2750 }
2751
2752 dir.start = p;
2753 while (ch_islower(*p) || *p == '-')
2754 p++;
2755 dir.end = p;
2756
2757 if (*p != '\0' && !ch_isspace(*p))
2758 return false;
2759
2760 pp_skip_whitespace(&p);
2761 arg = p;
2762
2763 if (Substring_Equals(dir, "break"))
2764 HandleBreak(arg);
2765 else if (Substring_Equals(dir, "undef"))
2766 Var_Undef(arg);
2767 else if (Substring_Equals(dir, "export"))
2768 Var_Export(VEM_PLAIN, arg);
2769 else if (Substring_Equals(dir, "export-all"))
2770 Var_Export(VEM_ALL, arg);
2771 else if (Substring_Equals(dir, "export-env"))
2772 Var_Export(VEM_ENV, arg);
2773 else if (Substring_Equals(dir, "export-literal"))
2774 Var_Export(VEM_LITERAL, arg);
2775 else if (Substring_Equals(dir, "unexport"))
2776 Var_UnExport(false, arg);
2777 else if (Substring_Equals(dir, "unexport-env"))
2778 Var_UnExport(true, arg);
2779 else if (Substring_Equals(dir, "info"))
2780 HandleMessage(PARSE_INFO, "info", arg);
2781 else if (Substring_Equals(dir, "warning"))
2782 HandleMessage(PARSE_WARNING, "warning", arg);
2783 else if (Substring_Equals(dir, "error"))
2784 HandleMessage(PARSE_FATAL, "error", arg);
2785 else
2786 return false;
2787 return true;
2788 }
2789
2790 bool
Parse_VarAssign(const char * line,bool finishDependencyGroup,GNode * scope)2791 Parse_VarAssign(const char *line, bool finishDependencyGroup, GNode *scope)
2792 {
2793 VarAssign var;
2794
2795 if (!Parse_IsVar(line, &var))
2796 return false;
2797 if (finishDependencyGroup)
2798 FinishDependencyGroup();
2799 Parse_Var(&var, scope);
2800 free(var.varname);
2801 return true;
2802 }
2803
2804 void
Parse_GuardElse(void)2805 Parse_GuardElse(void)
2806 {
2807 IncludedFile *curFile = CurFile();
2808 if (cond_depth == curFile->condMinDepth + 1)
2809 curFile->guardState = GS_NO;
2810 }
2811
2812 void
Parse_GuardEndif(void)2813 Parse_GuardEndif(void)
2814 {
2815 IncludedFile *curFile = CurFile();
2816 if (cond_depth == curFile->condMinDepth
2817 && curFile->guardState == GS_COND)
2818 curFile->guardState = GS_DONE;
2819 }
2820
2821 static char *
FindSemicolon(char * p)2822 FindSemicolon(char *p)
2823 {
2824 int depth = 0;
2825
2826 for (; *p != '\0'; p++) {
2827 if (*p == '\\' && p[1] != '\0') {
2828 p++;
2829 continue;
2830 }
2831
2832 if (*p == '$' && (p[1] == '(' || p[1] == '{'))
2833 depth++;
2834 else if (depth > 0 && (*p == ')' || *p == '}'))
2835 depth--;
2836 else if (depth == 0 && *p == ';')
2837 break;
2838 }
2839 return p;
2840 }
2841
2842 static void
ParseDependencyLine(char * line)2843 ParseDependencyLine(char *line)
2844 {
2845 VarEvalMode emode;
2846 char *expanded_line;
2847 const char *shellcmd = NULL;
2848
2849 {
2850 char *semicolon = FindSemicolon(line);
2851 if (*semicolon != '\0') {
2852 /* Terminate the dependency list at the ';' */
2853 *semicolon = '\0';
2854 shellcmd = semicolon + 1;
2855 }
2856 }
2857
2858 /*
2859 * We now know it's a dependency line, so it needs to have all
2860 * variables expanded before being parsed.
2861 *
2862 * XXX: Ideally the dependency line would first be split into
2863 * its left-hand side, dependency operator and right-hand side,
2864 * and then each side would be expanded on its own. This would
2865 * allow for the left-hand side to allow only defined variables
2866 * and to allow variables on the right-hand side to be undefined
2867 * as well.
2868 *
2869 * Parsing the line first would also prevent that targets
2870 * generated from expressions are interpreted as the
2871 * dependency operator, such as in "target${:U\:} middle: source",
2872 * in which the middle is interpreted as a source, not a target.
2873 */
2874
2875 /*
2876 * In lint mode, allow undefined variables to appear in dependency
2877 * lines.
2878 *
2879 * Ideally, only the right-hand side would allow undefined variables
2880 * since it is common to have optional dependencies. Having undefined
2881 * variables on the left-hand side is more unusual though. Since
2882 * both sides are expanded in a single pass, there is not much choice
2883 * what to do here.
2884 *
2885 * In normal mode, it does not matter whether undefined variables are
2886 * allowed or not since as of 2020-09-14, Var_Parse does not print
2887 * any parse errors in such a case. It simply returns the special
2888 * empty string var_Error, which cannot be detected in the result of
2889 * Var_Subst.
2890 */
2891 emode = opts.strict ? VARE_EVAL : VARE_EVAL_DEFINED;
2892 expanded_line = Var_Subst(line, SCOPE_CMDLINE, emode);
2893 /* TODO: handle errors */
2894
2895 /* Need a fresh list for the target nodes */
2896 if (targets != NULL)
2897 Lst_Free(targets);
2898 targets = Lst_New();
2899
2900 ParseDependency(expanded_line, line);
2901 free(expanded_line);
2902
2903 if (shellcmd != NULL)
2904 ParseLine_ShellCommand(shellcmd);
2905 }
2906
2907 static void
ParseLine(char * line)2908 ParseLine(char *line)
2909 {
2910 if (line[0] == '.' && ParseDirective(line))
2911 return;
2912
2913 if (line[0] == '\t') {
2914 ParseLine_ShellCommand(line + 1);
2915 return;
2916 }
2917
2918 if (IsSysVInclude(line)) {
2919 ParseTraditionalInclude(line);
2920 return;
2921 }
2922
2923 if (strncmp(line, "export", 6) == 0 && ch_isspace(line[6]) &&
2924 strchr(line, ':') == NULL) {
2925 ParseGmakeExport(line);
2926 return;
2927 }
2928
2929 if (Parse_VarAssign(line, true, SCOPE_GLOBAL))
2930 return;
2931
2932 FinishDependencyGroup();
2933
2934 ParseDependencyLine(line);
2935 }
2936
2937 /* Interpret a top-level makefile. */
2938 void
Parse_File(const char * name,int fd)2939 Parse_File(const char *name, int fd)
2940 {
2941 char *line;
2942 Buffer buf;
2943
2944 buf = LoadFile(name, fd != -1 ? fd : STDIN_FILENO);
2945 if (fd != -1)
2946 (void)close(fd);
2947
2948 assert(targets == NULL);
2949
2950 Parse_PushInput(name, 1, 0, buf, NULL);
2951
2952 do {
2953 while ((line = ReadHighLevelLine()) != NULL) {
2954 ParseLine(line);
2955 }
2956 } while (ParseEOF());
2957
2958 FinishDependencyGroup();
2959
2960 if (parseErrors != 0) {
2961 (void)fflush(stdout);
2962 (void)fprintf(stderr,
2963 "%s: Fatal errors encountered -- cannot continue\n",
2964 progname);
2965 PrintOnError(NULL, "");
2966 exit(1);
2967 }
2968 }
2969
2970 /* Initialize the parsing module. */
2971 void
Parse_Init(void)2972 Parse_Init(void)
2973 {
2974 mainNode = NULL;
2975 parseIncPath = SearchPath_New();
2976 sysIncPath = SearchPath_New();
2977 defSysIncPath = SearchPath_New();
2978 Vector_Init(&includes, sizeof(IncludedFile));
2979 HashTable_Init(&guards);
2980 }
2981
2982 #ifdef CLEANUP
2983 /* Clean up the parsing module. */
2984 void
Parse_End(void)2985 Parse_End(void)
2986 {
2987 HashIter hi;
2988
2989 Lst_DoneFree(&targCmds);
2990 assert(targets == NULL);
2991 SearchPath_Free(defSysIncPath);
2992 SearchPath_Free(sysIncPath);
2993 SearchPath_Free(parseIncPath);
2994 assert(includes.len == 0);
2995 Vector_Done(&includes);
2996 HashIter_Init(&hi, &guards);
2997 while (HashIter_Next(&hi)) {
2998 Guard *guard = hi.entry->value;
2999 free(guard->name);
3000 free(guard);
3001 }
3002 HashTable_Done(&guards);
3003 }
3004 #endif
3005
3006
3007 /* Populate the list with the single main target to create, or error out. */
3008 void
Parse_MainName(GNodeList * mainList)3009 Parse_MainName(GNodeList *mainList)
3010 {
3011 if (mainNode == NULL)
3012 Punt("no target to make.");
3013
3014 Lst_Append(mainList, mainNode);
3015 if (mainNode->type & OP_DOUBLEDEP)
3016 Lst_AppendAll(mainList, &mainNode->cohorts);
3017
3018 Global_Append(".TARGETS", mainNode->name);
3019 }
3020