parse.c revision 1.443 1 /* $NetBSD: parse.c,v 1.443 2020/11/16 21:39:22 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 * 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'.
80 *
81 * Interface:
82 * Parse_Init Initialize the module
83 *
84 * Parse_End Clean up the module
85 *
86 * Parse_File Parse a top-level makefile. Included files are
87 * handled by Parse_include_file though.
88 *
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.
93 *
94 * Parse_Error Report a parse error, a warning or an informational
95 * message.
96 *
97 * Parse_MainName Returns a list of the main target to create.
98 */
99
100 #include <sys/types.h>
101 #include <sys/mman.h>
102 #include <sys/stat.h>
103 #include <errno.h>
104 #include <stdarg.h>
105 #include <stdint.h>
106
107 #ifndef MAP_FILE
108 #define MAP_FILE 0
109 #endif
110 #ifndef MAP_COPY
111 #define MAP_COPY MAP_PRIVATE
112 #endif
113
114 #include "make.h"
115 #include "dir.h"
116 #include "job.h"
117 #include "pathnames.h"
118
119 /* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
120 MAKE_RCSID("$NetBSD: parse.c,v 1.443 2020/11/16 21:39:22 rillig Exp $");
121
122 /* types and constants */
123
124 /*
125 * Structure for a file being read ("included file")
126 */
127 typedef struct IFile {
128 char *fname; /* name of file (relative? absolute?) */
129 Boolean fromForLoop; /* simulated .include by the .for loop */
130 int lineno; /* current line number in file */
131 int first_lineno; /* line number of start of text */
132 unsigned int cond_depth; /* 'if' nesting when file opened */
133 Boolean depending; /* state of doing_depend on EOF */
134
135 /* The buffer from which the file's content is read. */
136 char *buf_freeIt;
137 char *buf_ptr; /* next char to be read */
138 char *buf_end;
139
140 char *(*nextbuf)(void *, size_t *); /* Function to get more data */
141 void *nextbuf_arg; /* Opaque arg for nextbuf() */
142 struct loadedfile *lf; /* loadedfile object, if any */
143 } IFile;
144
145 /*
146 * Tokens for target attributes
147 */
148 typedef enum ParseSpecial {
149 SP_ATTRIBUTE, /* Generic attribute */
150 SP_BEGIN, /* .BEGIN */
151 SP_DEFAULT, /* .DEFAULT */
152 SP_DELETE_ON_ERROR, /* .DELETE_ON_ERROR */
153 SP_END, /* .END */
154 SP_ERROR, /* .ERROR */
155 SP_IGNORE, /* .IGNORE */
156 SP_INCLUDES, /* .INCLUDES; not mentioned in the manual page */
157 SP_INTERRUPT, /* .INTERRUPT */
158 SP_LIBS, /* .LIBS; not mentioned in the manual page */
159 SP_MAIN, /* .MAIN and we don't have anything user-specified to
160 * 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_NOT, /* Not special */
167 SP_NOTPARALLEL, /* .NOTPARALLEL or .NO_PARALLEL */
168 SP_NULL, /* .NULL; not mentioned in the manual page */
169 SP_OBJDIR, /* .OBJDIR */
170 SP_ORDER, /* .ORDER */
171 SP_PARALLEL, /* .PARALLEL; not mentioned in the manual page */
172 SP_PATH, /* .PATH or .PATH.suffix */
173 SP_PHONY, /* .PHONY */
174 #ifdef POSIX
175 SP_POSIX, /* .POSIX; not mentioned in the manual page */
176 #endif
177 SP_PRECIOUS, /* .PRECIOUS */
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_WAIT /* .WAIT */
184 } ParseSpecial;
185
186 typedef List SearchPathList;
187 typedef ListNode SearchPathListNode;
188
189 /* result data */
190
191 /*
192 * The main target to create. This is the first target on the first
193 * dependency line in the first makefile.
194 */
195 static GNode *mainNode;
196
197 /* eval state */
198
199 /* During parsing, the targets from the left-hand side of the currently
200 * active dependency line, or NULL if the current line does not belong to a
201 * dependency line, for example because it is a variable assignment.
202 *
203 * See unit-tests/deptgt.mk, keyword "parse.c:targets". */
204 static GNodeList *targets;
205
206 #ifdef CLEANUP
207 /* All shell commands for all targets, in no particular order and possibly
208 * with duplicates. Kept in a separate list since the commands from .USE or
209 * .USEBEFORE nodes are shared with other GNodes, thereby giving up the
210 * easily understandable ownership over the allocated strings. */
211 static StringList *targCmds;
212 #endif
213
214 /*
215 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
216 * seen, then set to each successive source on the line.
217 */
218 static GNode *order_pred;
219
220 /* parser state */
221
222 /* number of fatal errors */
223 static int fatals = 0;
224
225 /*
226 * Variables for doing includes
227 */
228
229 /* The include chain of makefiles. At the bottom is the top-level makefile
230 * from the command line, and on top of that, there are the included files or
231 * .for loops, up to and including the current file.
232 *
233 * This data could be used to print stack traces on parse errors. As of
234 * 2020-09-14, this is not done though. It seems quite simple to print the
235 * tuples (fname:lineno:fromForLoop), from top to bottom. This simple idea is
236 * made complicated by the fact that the .for loops also use this stack for
237 * storing information.
238 *
239 * The lineno fields of the IFiles with fromForLoop == TRUE look confusing,
240 * which is demonstrated by the test 'include-main.mk'. They seem sorted
241 * backwards since they tell the number of completely parsed lines, which for
242 * a .for loop is right after the terminating .endfor. To compensate for this
243 * confusion, there is another field first_lineno pointing at the start of the
244 * .for loop, 1-based for human consumption.
245 *
246 * To make the stack trace intuitive, the entry below the first .for loop must
247 * be ignored completely since neither its lineno nor its first_lineno is
248 * useful. Instead, the topmost of each chain of .for loop needs to be
249 * printed twice, once with its first_lineno and once with its lineno.
250 *
251 * As of 2020-10-28, using the above rules, the stack trace for the .info line
252 * in include-subsub.mk would be:
253 *
254 * includes[5]: include-subsub.mk:4
255 * (lineno, from an .include)
256 * includes[4]: include-sub.mk:32
257 * (lineno, from a .for loop below an .include)
258 * includes[4]: include-sub.mk:31
259 * (first_lineno, from a .for loop, lineno == 32)
260 * includes[3]: include-sub.mk:30
261 * (first_lineno, from a .for loop, lineno == 33)
262 * includes[2]: include-sub.mk:29
263 * (first_lineno, from a .for loop, lineno == 34)
264 * includes[1]: include-sub.mk:35
265 * (not printed since it is below a .for loop)
266 * includes[0]: include-main.mk:27
267 */
268 static Vector /* of IFile */ includes;
269
270 static IFile *
271 GetInclude(size_t i)
272 {
273 return Vector_Get(&includes, i);
274 }
275
276 /* The file that is currently being read. */
277 static IFile *
278 CurFile(void)
279 {
280 return GetInclude(includes.len - 1);
281 }
282
283 /* include paths */
284 SearchPath *parseIncPath; /* dirs for "..." includes */
285 SearchPath *sysIncPath; /* dirs for <...> includes */
286 SearchPath *defSysIncPath; /* default for sysIncPath */
287
288 /* parser tables */
289
290 /*
291 * The parseKeywords table is searched using binary search when deciding
292 * if a target or source is special. The 'spec' field is the ParseSpecial
293 * type of the keyword (SP_NOT if the keyword isn't special as a target) while
294 * the 'op' field is the operator to apply to the list of targets if the
295 * keyword is used as a source ("0" if the keyword isn't special as a source)
296 */
297 static const struct {
298 const char *name; /* Name of keyword */
299 ParseSpecial spec; /* Type when used as a target */
300 GNodeType op; /* Operator when used as a source */
301 } parseKeywords[] = {
302 { ".BEGIN", SP_BEGIN, 0 },
303 { ".DEFAULT", SP_DEFAULT, 0 },
304 { ".DELETE_ON_ERROR", SP_DELETE_ON_ERROR, 0 },
305 { ".END", SP_END, 0 },
306 { ".ERROR", SP_ERROR, 0 },
307 { ".EXEC", SP_ATTRIBUTE, OP_EXEC },
308 { ".IGNORE", SP_IGNORE, OP_IGNORE },
309 { ".INCLUDES", SP_INCLUDES, 0 },
310 { ".INTERRUPT", SP_INTERRUPT, 0 },
311 { ".INVISIBLE", SP_ATTRIBUTE, OP_INVISIBLE },
312 { ".JOIN", SP_ATTRIBUTE, OP_JOIN },
313 { ".LIBS", SP_LIBS, 0 },
314 { ".MADE", SP_ATTRIBUTE, OP_MADE },
315 { ".MAIN", SP_MAIN, 0 },
316 { ".MAKE", SP_ATTRIBUTE, OP_MAKE },
317 { ".MAKEFLAGS", SP_MFLAGS, 0 },
318 { ".META", SP_META, OP_META },
319 { ".MFLAGS", SP_MFLAGS, 0 },
320 { ".NOMETA", SP_NOMETA, OP_NOMETA },
321 { ".NOMETA_CMP", SP_NOMETA_CMP, OP_NOMETA_CMP },
322 { ".NOPATH", SP_NOPATH, OP_NOPATH },
323 { ".NOTMAIN", SP_ATTRIBUTE, OP_NOTMAIN },
324 { ".NOTPARALLEL", SP_NOTPARALLEL, 0 },
325 { ".NO_PARALLEL", SP_NOTPARALLEL, 0 },
326 { ".NULL", SP_NULL, 0 },
327 { ".OBJDIR", SP_OBJDIR, 0 },
328 { ".OPTIONAL", SP_ATTRIBUTE, OP_OPTIONAL },
329 { ".ORDER", SP_ORDER, 0 },
330 { ".PARALLEL", SP_PARALLEL, 0 },
331 { ".PATH", SP_PATH, 0 },
332 { ".PHONY", SP_PHONY, OP_PHONY },
333 #ifdef POSIX
334 { ".POSIX", SP_POSIX, 0 },
335 #endif
336 { ".PRECIOUS", SP_PRECIOUS, OP_PRECIOUS },
337 { ".RECURSIVE", SP_ATTRIBUTE, OP_MAKE },
338 { ".SHELL", SP_SHELL, 0 },
339 { ".SILENT", SP_SILENT, OP_SILENT },
340 { ".SINGLESHELL", SP_SINGLESHELL, 0 },
341 { ".STALE", SP_STALE, 0 },
342 { ".SUFFIXES", SP_SUFFIXES, 0 },
343 { ".USE", SP_ATTRIBUTE, OP_USE },
344 { ".USEBEFORE", SP_ATTRIBUTE, OP_USEBEFORE },
345 { ".WAIT", SP_WAIT, 0 },
346 };
347
348 /* file loader */
349
350 struct loadedfile {
351 /* XXX: What is the lifetime of this path? Who manages the memory? */
352 const char *path; /* name, for error reports */
353 char *buf; /* contents buffer */
354 size_t len; /* length of contents */
355 size_t maplen; /* length of mmap area, or 0 */
356 Boolean used; /* XXX: have we used the data yet */
357 };
358
359 /* XXX: What is the lifetime of the path? Who manages the memory? */
360 static struct loadedfile *
361 loadedfile_create(const char *path)
362 {
363 struct loadedfile *lf;
364
365 lf = bmake_malloc(sizeof *lf);
366 lf->path = path == NULL ? "(stdin)" : path;
367 lf->buf = NULL;
368 lf->len = 0;
369 lf->maplen = 0;
370 lf->used = FALSE;
371 return lf;
372 }
373
374 static void
375 loadedfile_destroy(struct loadedfile *lf)
376 {
377 if (lf->buf != NULL) {
378 if (lf->maplen > 0)
379 munmap(lf->buf, lf->maplen);
380 else
381 free(lf->buf);
382 }
383 free(lf);
384 }
385
386 /*
387 * nextbuf() operation for loadedfile, as needed by the weird and twisted
388 * logic below. Once that's cleaned up, we can get rid of lf->used...
389 */
390 static char *
391 loadedfile_nextbuf(void *x, size_t *len)
392 {
393 struct loadedfile *lf = x;
394
395 if (lf->used)
396 return NULL;
397
398 lf->used = TRUE;
399 *len = lf->len;
400 return lf->buf;
401 }
402
403 /*
404 * Try to get the size of a file.
405 */
406 static Boolean
407 load_getsize(int fd, size_t *ret)
408 {
409 struct stat st;
410
411 if (fstat(fd, &st) < 0)
412 return FALSE;
413
414 if (!S_ISREG(st.st_mode))
415 return FALSE;
416
417 /*
418 * st_size is an off_t, which is 64 bits signed; *ret is
419 * size_t, which might be 32 bits unsigned or 64 bits
420 * unsigned. Rather than being elaborate, just punt on
421 * files that are more than 2^31 bytes. We should never
422 * see a makefile that size in practice...
423 *
424 * While we're at it reject negative sizes too, just in case.
425 */
426 if (st.st_size < 0 || st.st_size > 0x7fffffff)
427 return FALSE;
428
429 *ret = (size_t)st.st_size;
430 return TRUE;
431 }
432
433 static Boolean
434 loadedfile_mmap(struct loadedfile *lf, int fd)
435 {
436 static unsigned long pagesize = 0;
437
438 if (!load_getsize(fd, &lf->len))
439 return FALSE;
440
441 /* found a size, try mmap */
442 if (pagesize == 0)
443 pagesize = (unsigned long)sysconf(_SC_PAGESIZE);
444 if (pagesize == 0 || pagesize == (unsigned long)-1)
445 pagesize = 0x1000;
446
447 /* round size up to a page */
448 lf->maplen = pagesize * ((lf->len + pagesize - 1) / pagesize);
449
450 /*
451 * XXX hack for dealing with empty files; remove when
452 * we're no longer limited by interfacing to the old
453 * logic elsewhere in this file.
454 */
455 if (lf->maplen == 0)
456 lf->maplen = pagesize;
457
458 /*
459 * FUTURE: remove PROT_WRITE when the parser no longer
460 * needs to scribble on the input.
461 */
462 lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE,
463 MAP_FILE|MAP_COPY, fd, 0);
464 if (lf->buf == MAP_FAILED)
465 return FALSE;
466
467 if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
468 char *b = bmake_malloc(lf->len + 1);
469 b[lf->len] = '\n';
470 memcpy(b, lf->buf, lf->len++);
471 munmap(lf->buf, lf->maplen);
472 lf->maplen = 0;
473 lf->buf = b;
474 }
475
476 return TRUE;
477 }
478
479 /*
480 * Read in a file.
481 *
482 * Until the path search logic can be moved under here instead of
483 * being in the caller in another source file, we need to have the fd
484 * passed in already open. Bleh.
485 *
486 * If the path is NULL, use stdin.
487 */
488 static struct loadedfile *
489 loadfile(const char *path, int fd)
490 {
491 struct loadedfile *lf;
492 ssize_t result;
493 size_t bufpos;
494
495 lf = loadedfile_create(path);
496
497 if (path == NULL) {
498 assert(fd == -1);
499 fd = STDIN_FILENO;
500 } else {
501 #if 0 /* notyet */
502 fd = open(path, O_RDONLY);
503 if (fd < 0) {
504 ...
505 Error("%s: %s", path, strerror(errno));
506 exit(1);
507 }
508 #endif
509 }
510
511 if (loadedfile_mmap(lf, fd))
512 goto done;
513
514 /* cannot mmap; load the traditional way */
515
516 lf->maplen = 0;
517 lf->len = 1024;
518 lf->buf = bmake_malloc(lf->len);
519
520 bufpos = 0;
521 for (;;) {
522 assert(bufpos <= lf->len);
523 if (bufpos == lf->len) {
524 if (lf->len > SIZE_MAX/2) {
525 errno = EFBIG;
526 Error("%s: file too large", path);
527 exit(1);
528 }
529 lf->len *= 2;
530 lf->buf = bmake_realloc(lf->buf, lf->len);
531 }
532 assert(bufpos < lf->len);
533 result = read(fd, lf->buf + bufpos, lf->len - bufpos);
534 if (result < 0) {
535 Error("%s: read error: %s", path, strerror(errno));
536 exit(1);
537 }
538 if (result == 0)
539 break;
540
541 bufpos += (size_t)result;
542 }
543 assert(bufpos <= lf->len);
544 lf->len = bufpos;
545
546 /* truncate malloc region to actual length (maybe not useful) */
547 if (lf->len > 0) {
548 /* as for mmap case, ensure trailing \n */
549 if (lf->buf[lf->len - 1] != '\n')
550 lf->len++;
551 lf->buf = bmake_realloc(lf->buf, lf->len);
552 lf->buf[lf->len - 1] = '\n';
553 }
554
555 done:
556 if (path != NULL)
557 close(fd);
558
559 return lf;
560 }
561
562 /* old code */
563
564 /* Check if the current character is escaped on the current line. */
565 static Boolean
566 ParseIsEscaped(const char *line, const char *c)
567 {
568 Boolean active = FALSE;
569 for (;;) {
570 if (line == c)
571 return active;
572 if (*--c != '\\')
573 return active;
574 active = !active;
575 }
576 }
577
578 /* Add the filename and lineno to the GNode so that we remember where it
579 * was first defined. */
580 static void
581 ParseMark(GNode *gn)
582 {
583 IFile *curFile = CurFile();
584 gn->fname = curFile->fname;
585 gn->lineno = curFile->lineno;
586 }
587
588 /* Look in the table of keywords for one matching the given string.
589 * Return the index of the keyword, or -1 if it isn't there. */
590 static int
591 ParseFindKeyword(const char *str)
592 {
593 int start = 0;
594 int end = sizeof parseKeywords / sizeof parseKeywords[0] - 1;
595
596 do {
597 int cur = start + (end - start) / 2;
598 int diff = strcmp(str, parseKeywords[cur].name);
599
600 if (diff == 0)
601 return cur;
602 if (diff < 0)
603 end = cur - 1;
604 else
605 start = cur + 1;
606 } while (start <= end);
607
608 return -1;
609 }
610
611 static void
612 PrintLocation(FILE *f, const char *fname, size_t lineno)
613 {
614 char dirbuf[MAXPATHLEN+1];
615 const char *dir, *base;
616 void *dir_freeIt, *base_freeIt;
617
618 if (*fname == '/' || strcmp(fname, "(stdin)") == 0) {
619 (void)fprintf(f, "\"%s\" line %zu: ", fname, lineno);
620 return;
621 }
622
623 /* Find out which makefile is the culprit.
624 * We try ${.PARSEDIR} and apply realpath(3) if not absolute. */
625
626 dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &dir_freeIt);
627 if (dir == NULL)
628 dir = ".";
629 if (*dir != '/')
630 dir = realpath(dir, dirbuf);
631
632 base = Var_Value(".PARSEFILE", VAR_GLOBAL, &base_freeIt);
633 if (base == NULL) {
634 const char *slash = strrchr(fname, '/');
635 base = slash != NULL ? slash + 1 : fname;
636 }
637
638 (void)fprintf(f, "\"%s/%s\" line %zu: ", dir, base, lineno);
639 bmake_free(base_freeIt);
640 bmake_free(dir_freeIt);
641 }
642
643 static void
644 ParseVErrorInternal(FILE *f, const char *fname, size_t lineno,
645 ParseErrorLevel type, const char *fmt, va_list ap)
646 {
647 static Boolean fatal_warning_error_printed = FALSE;
648
649 (void)fprintf(f, "%s: ", progname);
650
651 if (fname != NULL)
652 PrintLocation(f, fname, lineno);
653 if (type == PARSE_WARNING)
654 (void)fprintf(f, "warning: ");
655 (void)vfprintf(f, fmt, ap);
656 (void)fprintf(f, "\n");
657 (void)fflush(f);
658
659 if (type == PARSE_INFO)
660 return;
661 if (type == PARSE_FATAL || opts.parseWarnFatal)
662 fatals++;
663 if (opts.parseWarnFatal && !fatal_warning_error_printed) {
664 Error("parsing warnings being treated as errors");
665 fatal_warning_error_printed = TRUE;
666 }
667 }
668
669 static void
670 ParseErrorInternal(const char *fname, size_t lineno,
671 ParseErrorLevel type, const char *fmt, ...)
672 {
673 va_list ap;
674
675 (void)fflush(stdout);
676 va_start(ap, fmt);
677 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
678 va_end(ap);
679
680 if (opts.debug_file != stderr && opts.debug_file != stdout) {
681 va_start(ap, fmt);
682 ParseVErrorInternal(opts.debug_file, fname, lineno, type,
683 fmt, ap);
684 va_end(ap);
685 }
686 }
687
688 /* Print a parse error message, including location information.
689 *
690 * If the level is PARSE_FATAL, continue parsing until the end of the
691 * current top-level makefile, then exit (see Parse_File).
692 *
693 * Fmt is given without a trailing newline. */
694 void
695 Parse_Error(ParseErrorLevel type, const char *fmt, ...)
696 {
697 va_list ap;
698 const char *fname;
699 size_t lineno;
700
701 if (includes.len == 0) {
702 fname = NULL;
703 lineno = 0;
704 } else {
705 IFile *curFile = CurFile();
706 fname = curFile->fname;
707 lineno = (size_t)curFile->lineno;
708 }
709
710 va_start(ap, fmt);
711 (void)fflush(stdout);
712 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
713 va_end(ap);
714
715 if (opts.debug_file != stderr && opts.debug_file != stdout) {
716 va_start(ap, fmt);
717 ParseVErrorInternal(opts.debug_file, fname, lineno, type,
718 fmt, ap);
719 va_end(ap);
720 }
721 }
722
723
724 /* Parse and handle a .info, .warning or .error directive.
725 * For an .error directive, immediately exit. */
726 static Boolean
727 ParseMessage(const char *directive)
728 {
729 const char *p = directive;
730 int mtype = *p == 'i' ? PARSE_INFO :
731 *p == 'w' ? PARSE_WARNING : PARSE_FATAL;
732 char *arg;
733
734 while (ch_isalpha(*p))
735 p++;
736 if (!ch_isspace(*p))
737 return FALSE; /* missing argument */
738
739 cpp_skip_whitespace(&p);
740 (void)Var_Subst(p, VAR_CMDLINE, VARE_WANTRES, &arg);
741 /* TODO: handle errors */
742
743 Parse_Error(mtype, "%s", arg);
744 free(arg);
745
746 if (mtype == PARSE_FATAL) {
747 PrintOnError(NULL, NULL);
748 exit(1);
749 }
750 return TRUE;
751 }
752
753 /* Add the child to the parent's children.
754 *
755 * Additionally, add the parent to the child's parents, but only if the
756 * target is not special. An example for such a special target is .END,
757 * which does not need to be informed once the child target has been made. */
758 static void
759 LinkSource(GNode *pgn, GNode *cgn, Boolean isSpecial)
760 {
761 if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(pgn->cohorts))
762 pgn = pgn->cohorts->last->datum;
763
764 Lst_Append(pgn->children, cgn);
765 pgn->unmade++;
766
767 /* Special targets like .END don't need any children. */
768 if (!isSpecial)
769 Lst_Append(cgn->parents, pgn);
770
771 if (DEBUG(PARSE)) {
772 debug_printf("# %s: added child %s - %s\n",
773 __func__, pgn->name, cgn->name);
774 Targ_PrintNode(pgn, 0);
775 Targ_PrintNode(cgn, 0);
776 }
777 }
778
779 /* Add the node to each target from the current dependency group. */
780 static void
781 LinkToTargets(GNode *gn, Boolean isSpecial)
782 {
783 GNodeListNode *ln;
784 for (ln = targets->first; ln != NULL; ln = ln->next)
785 LinkSource(ln->datum, gn, isSpecial);
786 }
787
788 static Boolean
789 TryApplyDependencyOperator(GNode *gn, GNodeType op)
790 {
791 /*
792 * If the node occurred on the left-hand side of a dependency and the
793 * operator also defines a dependency, they must match.
794 */
795 if ((op & OP_OPMASK) && (gn->type & OP_OPMASK) &&
796 ((op & OP_OPMASK) != (gn->type & OP_OPMASK)))
797 {
798 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
799 return FALSE;
800 }
801
802 if (op == OP_DOUBLEDEP && (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
803 /*
804 * If the node was of the left-hand side of a '::' operator, we need
805 * to create a new instance of it for the children and commands on
806 * this dependency line since each of these dependency groups has its
807 * own attributes and commands, separate from the others.
808 *
809 * The new instance is placed on the 'cohorts' list of the
810 * initial one (note the initial one is not on its own cohorts list)
811 * and the new instance is linked to all parents of the initial
812 * instance.
813 */
814 GNode *cohort;
815
816 /*
817 * Propagate copied bits to the initial node. They'll be propagated
818 * back to the rest of the cohorts later.
819 */
820 gn->type |= op & ~OP_OPMASK;
821
822 cohort = Targ_NewInternalNode(gn->name);
823 if (doing_depend)
824 ParseMark(cohort);
825 /*
826 * Make the cohort invisible as well to avoid duplicating it into
827 * other variables. True, parents of this target won't tend to do
828 * anything with their local variables, but better safe than
829 * sorry. (I think this is pointless now, since the relevant list
830 * traversals will no longer see this node anyway. -mycroft)
831 */
832 cohort->type = op | OP_INVISIBLE;
833 Lst_Append(gn->cohorts, cohort);
834 cohort->centurion = gn;
835 gn->unmade_cohorts++;
836 snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
837 (unsigned int)gn->unmade_cohorts % 1000000);
838 } else {
839 /*
840 * We don't want to nuke any previous flags (whatever they were) so we
841 * just OR the new operator into the old.
842 */
843 gn->type |= op;
844 }
845
846 return TRUE;
847 }
848
849 static void
850 ApplyDependencyOperator(GNodeType op)
851 {
852 GNodeListNode *ln;
853 for (ln = targets->first; ln != NULL; ln = ln->next)
854 if (!TryApplyDependencyOperator(ln->datum, op))
855 break;
856 }
857
858 static Boolean
859 ParseDoSrcKeyword(const char *src, ParseSpecial specType)
860 {
861 static int wait_number = 0;
862 char wait_src[16];
863 GNode *gn;
864
865 if (*src == '.' && ch_isupper(src[1])) {
866 int keywd = ParseFindKeyword(src);
867 if (keywd != -1) {
868 GNodeType op = parseKeywords[keywd].op;
869 if (op != 0) {
870 ApplyDependencyOperator(op);
871 return TRUE;
872 }
873 if (parseKeywords[keywd].spec == SP_WAIT) {
874 /*
875 * We add a .WAIT node in the dependency list.
876 * After any dynamic dependencies (and filename globbing)
877 * have happened, it is given a dependency on each
878 * previous child, back until the previous .WAIT node.
879 * The next child won't be scheduled until the .WAIT node
880 * is built.
881 * We give each .WAIT node a unique name (mainly for
882 * diagnostics).
883 */
884 snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
885 gn = Targ_NewInternalNode(wait_src);
886 if (doing_depend)
887 ParseMark(gn);
888 gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
889 LinkToTargets(gn, specType != SP_NOT);
890 return TRUE;
891 }
892 }
893 }
894 return FALSE;
895 }
896
897 static void
898 ParseDoSrcMain(const char *src)
899 {
900 /*
901 * In a line like ".MAIN: source1 source2", it means we need to add
902 * the sources of said target to the list of things to create.
903 *
904 * Note that this will only be invoked if the user didn't specify a
905 * target on the command line. This is to allow .ifmake to succeed.
906 *
907 * XXX: Double-check all of the above comment.
908 */
909 Lst_Append(opts.create, bmake_strdup(src));
910 /*
911 * Add the name to the .TARGETS variable as well, so the user can
912 * employ that, if desired.
913 */
914 Var_Append(".TARGETS", src, VAR_GLOBAL);
915 }
916
917 static void
918 ParseDoSrcOrder(const char *src)
919 {
920 GNode *gn;
921 /*
922 * Create proper predecessor/successor links between the previous
923 * source and the current one.
924 */
925 gn = Targ_GetNode(src);
926 if (doing_depend)
927 ParseMark(gn);
928 if (order_pred != NULL) {
929 Lst_Append(order_pred->order_succ, gn);
930 Lst_Append(gn->order_pred, order_pred);
931 if (DEBUG(PARSE)) {
932 debug_printf("# %s: added Order dependency %s - %s\n",
933 __func__, order_pred->name, gn->name);
934 Targ_PrintNode(order_pred, 0);
935 Targ_PrintNode(gn, 0);
936 }
937 }
938 /*
939 * The current source now becomes the predecessor for the next one.
940 */
941 order_pred = gn;
942 }
943
944 static void
945 ParseDoSrcOther(const char *src, GNodeType tOp, ParseSpecial specType)
946 {
947 GNode *gn;
948
949 /*
950 * If the source is not an attribute, we need to find/create
951 * a node for it. After that we can apply any operator to it
952 * from a special target or link it to its parents, as
953 * appropriate.
954 *
955 * In the case of a source that was the object of a :: operator,
956 * the attribute is applied to all of its instances (as kept in
957 * the 'cohorts' list of the node) or all the cohorts are linked
958 * to all the targets.
959 */
960
961 /* Find/create the 'src' node and attach to all targets */
962 gn = Targ_GetNode(src);
963 if (doing_depend)
964 ParseMark(gn);
965 if (tOp != OP_NONE)
966 gn->type |= tOp;
967 else
968 LinkToTargets(gn, specType != SP_NOT);
969 }
970
971 /* Given the name of a source in a dependency line, figure out if it is an
972 * attribute (such as .SILENT) and apply it to the targets if it is. Else
973 * decide if there is some attribute which should be applied *to* the source
974 * because of some special target (such as .PHONY) and apply it if so.
975 * Otherwise, make the source a child of the targets in the list 'targets'.
976 *
977 * Input:
978 * tOp operator (if any) from special targets
979 * src name of the source to handle
980 */
981 static void
982 ParseDoSrc(GNodeType tOp, const char *src, ParseSpecial specType)
983 {
984 if (ParseDoSrcKeyword(src, specType))
985 return;
986
987 if (specType == SP_MAIN)
988 ParseDoSrcMain(src);
989 else if (specType == SP_ORDER)
990 ParseDoSrcOrder(src);
991 else
992 ParseDoSrcOther(src, tOp, specType);
993 }
994
995 /* If we have yet to decide on a main target to make, in the absence of any
996 * user input, we want the first target on the first dependency line that is
997 * actually a real target (i.e. isn't a .USE or .EXEC rule) to be made. */
998 static void
999 FindMainTarget(void)
1000 {
1001 GNodeListNode *ln;
1002
1003 if (mainNode != NULL)
1004 return;
1005
1006 for (ln = targets->first; ln != NULL; ln = ln->next) {
1007 GNode *gn = ln->datum;
1008 if (!(gn->type & OP_NOTARGET)) {
1009 mainNode = gn;
1010 Targ_SetMain(gn);
1011 return;
1012 }
1013 }
1014 }
1015
1016 /*
1017 * We got to the end of the line while we were still looking at targets.
1018 *
1019 * Ending a dependency line without an operator is a Bozo no-no. As a
1020 * heuristic, this is also often triggered by undetected conflicts from
1021 * cvs/rcs merges.
1022 */
1023 static void
1024 ParseErrorNoDependency(const char *lstart)
1025 {
1026 if ((strncmp(lstart, "<<<<<<", 6) == 0) ||
1027 (strncmp(lstart, "======", 6) == 0) ||
1028 (strncmp(lstart, ">>>>>>", 6) == 0))
1029 Parse_Error(PARSE_FATAL,
1030 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
1031 else if (lstart[0] == '.') {
1032 const char *dirstart = lstart + 1;
1033 const char *dirend;
1034 cpp_skip_whitespace(&dirstart);
1035 dirend = dirstart;
1036 while (ch_isalnum(*dirend) || *dirend == '-')
1037 dirend++;
1038 Parse_Error(PARSE_FATAL, "Unknown directive \"%.*s\"",
1039 (int)(dirend - dirstart), dirstart);
1040 } else
1041 Parse_Error(PARSE_FATAL, "Need an operator");
1042 }
1043
1044 static void
1045 ParseDependencyTargetWord(/*const*/ char **pp, const char *lstart)
1046 {
1047 /*const*/ char *cp = *pp;
1048
1049 while (*cp != '\0') {
1050 if ((ch_isspace(*cp) || *cp == '!' || *cp == ':' || *cp == '(') &&
1051 !ParseIsEscaped(lstart, cp))
1052 break;
1053
1054 if (*cp == '$') {
1055 /*
1056 * Must be a dynamic source (would have been expanded
1057 * otherwise), so call the Var module to parse the puppy
1058 * so we can safely advance beyond it...There should be
1059 * no errors in this, as they would have been discovered
1060 * in the initial Var_Subst and we wouldn't be here.
1061 */
1062 const char *nested_p = cp;
1063 const char *nested_val;
1064 void *freeIt;
1065
1066 (void)Var_Parse(&nested_p, VAR_CMDLINE,
1067 VARE_WANTRES | VARE_UNDEFERR, &nested_val, &freeIt);
1068 /* TODO: handle errors */
1069 free(freeIt);
1070 cp += nested_p - cp;
1071 } else
1072 cp++;
1073 }
1074
1075 *pp = cp;
1076 }
1077
1078 /* Handle special targets like .PATH, .DEFAULT, .BEGIN, .ORDER. */
1079 static void
1080 ParseDoDependencyTargetSpecial(ParseSpecial *inout_specType,
1081 const char *line,
1082 SearchPathList **inout_paths)
1083 {
1084 switch (*inout_specType) {
1085 case SP_PATH:
1086 if (*inout_paths == NULL)
1087 *inout_paths = Lst_New();
1088 Lst_Append(*inout_paths, dirSearchPath);
1089 break;
1090 case SP_MAIN:
1091 /* Allow targets from the command line to override the .MAIN node. */
1092 if (!Lst_IsEmpty(opts.create))
1093 *inout_specType = SP_NOT;
1094 break;
1095 case SP_BEGIN:
1096 case SP_END:
1097 case SP_STALE:
1098 case SP_ERROR:
1099 case SP_INTERRUPT: {
1100 GNode *gn = Targ_GetNode(line);
1101 if (doing_depend)
1102 ParseMark(gn);
1103 gn->type |= OP_NOTMAIN|OP_SPECIAL;
1104 Lst_Append(targets, gn);
1105 break;
1106 }
1107 case SP_DEFAULT: {
1108 /* Need to create a node to hang commands on, but we don't want it
1109 * in the graph, nor do we want it to be the Main Target. We claim
1110 * the node is a transformation rule to make life easier later,
1111 * when we'll use Make_HandleUse to actually apply the .DEFAULT
1112 * commands. */
1113 GNode *gn = GNode_New(".DEFAULT");
1114 gn->type |= OP_NOTMAIN|OP_TRANSFORM;
1115 Lst_Append(targets, gn);
1116 defaultNode = gn;
1117 break;
1118 }
1119 case SP_DELETE_ON_ERROR:
1120 deleteOnError = TRUE;
1121 break;
1122 case SP_NOTPARALLEL:
1123 opts.maxJobs = 1;
1124 break;
1125 case SP_SINGLESHELL:
1126 opts.compatMake = TRUE;
1127 break;
1128 case SP_ORDER:
1129 order_pred = NULL;
1130 break;
1131 default:
1132 break;
1133 }
1134 }
1135
1136 /*
1137 * .PATH<suffix> has to be handled specially.
1138 * Call on the suffix module to give us a path to modify.
1139 */
1140 static Boolean
1141 ParseDoDependencyTargetPath(const char *line, SearchPathList **inout_paths)
1142 {
1143 SearchPath *path;
1144
1145 path = Suff_GetPath(&line[5]);
1146 if (path == NULL) {
1147 Parse_Error(PARSE_FATAL,
1148 "Suffix '%s' not defined (yet)",
1149 &line[5]);
1150 return FALSE;
1151 }
1152
1153 if (*inout_paths == NULL)
1154 *inout_paths = Lst_New();
1155 Lst_Append(*inout_paths, path);
1156
1157 return TRUE;
1158 }
1159
1160 /*
1161 * See if it's a special target and if so set specType to match it.
1162 */
1163 static Boolean
1164 ParseDoDependencyTarget(const char *line, ParseSpecial *inout_specType,
1165 GNodeType *out_tOp, SearchPathList **inout_paths)
1166 {
1167 int keywd;
1168
1169 if (!(*line == '.' && ch_isupper(line[1])))
1170 return TRUE;
1171
1172 /*
1173 * See if the target is a special target that must have it
1174 * or its sources handled specially.
1175 */
1176 keywd = ParseFindKeyword(line);
1177 if (keywd != -1) {
1178 if (*inout_specType == SP_PATH && parseKeywords[keywd].spec != SP_PATH) {
1179 Parse_Error(PARSE_FATAL, "Mismatched special targets");
1180 return FALSE;
1181 }
1182
1183 *inout_specType = parseKeywords[keywd].spec;
1184 *out_tOp = parseKeywords[keywd].op;
1185
1186 ParseDoDependencyTargetSpecial(inout_specType, line, inout_paths);
1187
1188 } else if (strncmp(line, ".PATH", 5) == 0) {
1189 *inout_specType = SP_PATH;
1190 if (!ParseDoDependencyTargetPath(line, inout_paths))
1191 return FALSE;
1192 }
1193 return TRUE;
1194 }
1195
1196 static void
1197 ParseDoDependencyTargetMundane(char *line, StringList *curTargs)
1198 {
1199 if (Dir_HasWildcards(line)) {
1200 /*
1201 * Targets are to be sought only in the current directory,
1202 * so create an empty path for the thing. Note we need to
1203 * use Dir_Destroy in the destruction of the path as the
1204 * Dir module could have added a directory to the path...
1205 */
1206 SearchPath *emptyPath = Lst_New();
1207
1208 Dir_Expand(line, emptyPath, curTargs);
1209
1210 Lst_Destroy(emptyPath, Dir_Destroy);
1211 } else {
1212 /*
1213 * No wildcards, but we want to avoid code duplication,
1214 * so create a list with the word on it.
1215 */
1216 Lst_Append(curTargs, line);
1217 }
1218
1219 /* Apply the targets. */
1220
1221 while (!Lst_IsEmpty(curTargs)) {
1222 char *targName = Lst_Dequeue(curTargs);
1223 GNode *gn = Suff_IsTransform(targName)
1224 ? Suff_AddTransform(targName)
1225 : Targ_GetNode(targName);
1226 if (doing_depend)
1227 ParseMark(gn);
1228
1229 Lst_Append(targets, gn);
1230 }
1231 }
1232
1233 static void
1234 ParseDoDependencyTargetExtraWarn(char **pp, const char *lstart)
1235 {
1236 Boolean warning = FALSE;
1237 char *cp = *pp;
1238
1239 while (*cp != '\0') {
1240 if (!ParseIsEscaped(lstart, cp) && (*cp == '!' || *cp == ':'))
1241 break;
1242 if (ParseIsEscaped(lstart, cp) || (*cp != ' ' && *cp != '\t'))
1243 warning = TRUE;
1244 cp++;
1245 }
1246 if (warning)
1247 Parse_Error(PARSE_WARNING, "Extra target ignored");
1248
1249 *pp = cp;
1250 }
1251
1252 static void
1253 ParseDoDependencyCheckSpec(ParseSpecial specType)
1254 {
1255 switch (specType) {
1256 default:
1257 Parse_Error(PARSE_WARNING,
1258 "Special and mundane targets don't mix. "
1259 "Mundane ones ignored");
1260 break;
1261 case SP_DEFAULT:
1262 case SP_STALE:
1263 case SP_BEGIN:
1264 case SP_END:
1265 case SP_ERROR:
1266 case SP_INTERRUPT:
1267 /*
1268 * These create nodes on which to hang commands, so targets
1269 * shouldn't be empty...
1270 */
1271 case SP_NOT:
1272 /* Nothing special here -- targets can be empty if it wants. */
1273 break;
1274 }
1275 }
1276
1277 static Boolean
1278 ParseDoDependencyParseOp(char **pp, const char *lstart, GNodeType *out_op)
1279 {
1280 const char *cp = *pp;
1281
1282 if (*cp == '!') {
1283 *out_op = OP_FORCE;
1284 (*pp)++;
1285 return TRUE;
1286 }
1287
1288 if (*cp == ':') {
1289 if (cp[1] == ':') {
1290 *out_op = OP_DOUBLEDEP;
1291 (*pp) += 2;
1292 } else {
1293 *out_op = OP_DEPENDS;
1294 (*pp)++;
1295 }
1296 return TRUE;
1297 }
1298
1299 {
1300 const char *msg = lstart[0] == '.' ? "Unknown directive"
1301 : "Missing dependency operator";
1302 Parse_Error(PARSE_FATAL, "%s", msg);
1303 return FALSE;
1304 }
1305 }
1306
1307 static void
1308 ClearPaths(SearchPathList *paths)
1309 {
1310 if (paths != NULL) {
1311 SearchPathListNode *ln;
1312 for (ln = paths->first; ln != NULL; ln = ln->next)
1313 Dir_ClearPath(ln->datum);
1314 }
1315
1316 Dir_SetPATH();
1317 }
1318
1319 static void
1320 ParseDoDependencySourcesEmpty(ParseSpecial specType, SearchPathList *paths)
1321 {
1322 switch (specType) {
1323 case SP_SUFFIXES:
1324 Suff_ClearSuffixes();
1325 break;
1326 case SP_PRECIOUS:
1327 allPrecious = TRUE;
1328 break;
1329 case SP_IGNORE:
1330 opts.ignoreErrors = TRUE;
1331 break;
1332 case SP_SILENT:
1333 opts.beSilent = TRUE;
1334 break;
1335 case SP_PATH:
1336 ClearPaths(paths);
1337 break;
1338 #ifdef POSIX
1339 case SP_POSIX:
1340 Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
1341 break;
1342 #endif
1343 default:
1344 break;
1345 }
1346 }
1347
1348 static void
1349 AddToPaths(const char *dir, SearchPathList *paths)
1350 {
1351 if (paths != NULL) {
1352 SearchPathListNode *ln;
1353 for (ln = paths->first; ln != NULL; ln = ln->next)
1354 (void)Dir_AddDir(ln->datum, dir);
1355 }
1356 }
1357
1358 /*
1359 * If the target was one that doesn't take files as its sources
1360 * but takes something like suffixes, we take each
1361 * space-separated word on the line as a something and deal
1362 * with it accordingly.
1363 *
1364 * If the target was .SUFFIXES, we take each source as a
1365 * suffix and add it to the list of suffixes maintained by the
1366 * Suff module.
1367 *
1368 * If the target was a .PATH, we add the source as a directory
1369 * to search on the search path.
1370 *
1371 * If it was .INCLUDES, the source is taken to be the suffix of
1372 * files which will be #included and whose search path should
1373 * be present in the .INCLUDES variable.
1374 *
1375 * If it was .LIBS, the source is taken to be the suffix of
1376 * files which are considered libraries and whose search path
1377 * should be present in the .LIBS variable.
1378 *
1379 * If it was .NULL, the source is the suffix to use when a file
1380 * has no valid suffix.
1381 *
1382 * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1383 * and will cause make to do a new chdir to that path.
1384 */
1385 static void
1386 ParseDoDependencySourceSpecial(ParseSpecial specType, char *word,
1387 SearchPathList *paths)
1388 {
1389 switch (specType) {
1390 case SP_SUFFIXES:
1391 Suff_AddSuffix(word, &mainNode);
1392 break;
1393 case SP_PATH:
1394 AddToPaths(word, paths);
1395 break;
1396 case SP_INCLUDES:
1397 Suff_AddInclude(word);
1398 break;
1399 case SP_LIBS:
1400 Suff_AddLib(word);
1401 break;
1402 case SP_NULL:
1403 Suff_SetNull(word);
1404 break;
1405 case SP_OBJDIR:
1406 Main_SetObjdir(FALSE, "%s", word);
1407 break;
1408 default:
1409 break;
1410 }
1411 }
1412
1413 static Boolean
1414 ParseDoDependencyTargets(char **inout_cp,
1415 char **inout_line,
1416 const char *lstart,
1417 ParseSpecial *inout_specType,
1418 GNodeType *inout_tOp,
1419 SearchPathList **inout_paths,
1420 StringList *curTargs)
1421 {
1422 char *cp = *inout_cp;
1423 char *line = *inout_line;
1424 char savec;
1425
1426 for (;;) {
1427 /*
1428 * Here LINE points to the beginning of the next word, and
1429 * LSTART points to the actual beginning of the line.
1430 */
1431
1432 /* Find the end of the next word. */
1433 cp = line;
1434 ParseDependencyTargetWord(&cp, lstart);
1435
1436 /*
1437 * If the word is followed by a left parenthesis, it's the
1438 * name of an object file inside an archive (ar file).
1439 */
1440 if (!ParseIsEscaped(lstart, cp) && *cp == '(') {
1441 /*
1442 * Archives must be handled specially to make sure the OP_ARCHV
1443 * flag is set in their 'type' field, for one thing, and because
1444 * things like "archive(file1.o file2.o file3.o)" are permissible.
1445 * Arch_ParseArchive will set 'line' to be the first non-blank
1446 * after the archive-spec. It creates/finds nodes for the members
1447 * and places them on the given list, returning TRUE if all
1448 * went well and FALSE if there was an error in the
1449 * specification. On error, line should remain untouched.
1450 */
1451 if (!Arch_ParseArchive(&line, targets, VAR_CMDLINE)) {
1452 Parse_Error(PARSE_FATAL,
1453 "Error in archive specification: \"%s\"", line);
1454 return FALSE;
1455 } else {
1456 /* Done with this word; on to the next. */
1457 cp = line;
1458 continue;
1459 }
1460 }
1461
1462 if (!*cp) {
1463 ParseErrorNoDependency(lstart);
1464 return FALSE;
1465 }
1466
1467 /* Insert a null terminator. */
1468 savec = *cp;
1469 *cp = '\0';
1470
1471 if (!ParseDoDependencyTarget(line, inout_specType, inout_tOp,
1472 inout_paths))
1473 return FALSE;
1474
1475 /*
1476 * Have word in line. Get or create its node and stick it at
1477 * the end of the targets list
1478 */
1479 if (*inout_specType == SP_NOT && *line != '\0')
1480 ParseDoDependencyTargetMundane(line, curTargs);
1481 else if (*inout_specType == SP_PATH && *line != '.' && *line != '\0')
1482 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
1483
1484 /* Don't need the inserted null terminator any more. */
1485 *cp = savec;
1486
1487 /*
1488 * If it is a special type and not .PATH, it's the only target we
1489 * allow on this line...
1490 */
1491 if (*inout_specType != SP_NOT && *inout_specType != SP_PATH)
1492 ParseDoDependencyTargetExtraWarn(&cp, lstart);
1493 else
1494 pp_skip_whitespace(&cp);
1495
1496 line = cp;
1497 if (*line == '\0')
1498 break;
1499 if ((*line == '!' || *line == ':') && !ParseIsEscaped(lstart, line))
1500 break;
1501 }
1502
1503 *inout_cp = cp;
1504 *inout_line = line;
1505 return TRUE;
1506 }
1507
1508 static void
1509 ParseDoDependencySourcesSpecial(char *start, char *end,
1510 ParseSpecial specType, SearchPathList *paths)
1511 {
1512 char savec;
1513
1514 while (*start) {
1515 while (*end && !ch_isspace(*end))
1516 end++;
1517 savec = *end;
1518 *end = '\0';
1519 ParseDoDependencySourceSpecial(specType, start, paths);
1520 *end = savec;
1521 if (savec != '\0')
1522 end++;
1523 pp_skip_whitespace(&end);
1524 start = end;
1525 }
1526 }
1527
1528 static Boolean
1529 ParseDoDependencySourcesMundane(char *start, char *end,
1530 ParseSpecial specType, GNodeType tOp)
1531 {
1532 while (*start != '\0') {
1533 /*
1534 * The targets take real sources, so we must beware of archive
1535 * specifications (i.e. things with left parentheses in them)
1536 * and handle them accordingly.
1537 */
1538 for (; *end && !ch_isspace(*end); end++) {
1539 if (*end == '(' && end > start && end[-1] != '$') {
1540 /*
1541 * Only stop for a left parenthesis if it isn't at the
1542 * start of a word (that'll be for variable changes
1543 * later) and isn't preceded by a dollar sign (a dynamic
1544 * source).
1545 */
1546 break;
1547 }
1548 }
1549
1550 if (*end == '(') {
1551 GNodeList *sources = Lst_New();
1552 if (!Arch_ParseArchive(&start, sources, VAR_CMDLINE)) {
1553 Parse_Error(PARSE_FATAL,
1554 "Error in source archive spec \"%s\"", start);
1555 return FALSE;
1556 }
1557
1558 while (!Lst_IsEmpty(sources)) {
1559 GNode *gn = Lst_Dequeue(sources);
1560 ParseDoSrc(tOp, gn->name, specType);
1561 }
1562 Lst_Free(sources);
1563 end = start;
1564 } else {
1565 if (*end) {
1566 *end = '\0';
1567 end++;
1568 }
1569
1570 ParseDoSrc(tOp, start, specType);
1571 }
1572 pp_skip_whitespace(&end);
1573 start = end;
1574 }
1575 return TRUE;
1576 }
1577
1578 /* Parse a dependency line consisting of targets, followed by a dependency
1579 * operator, optionally followed by sources.
1580 *
1581 * The nodes of the sources are linked as children to the nodes of the
1582 * targets. Nodes are created as necessary.
1583 *
1584 * The operator is applied to each node in the global 'targets' list,
1585 * which is where the nodes found for the targets are kept, by means of
1586 * the ParseDoOp function.
1587 *
1588 * The sources are parsed in much the same way as the targets, except
1589 * that they are expanded using the wildcarding scheme of the C-Shell,
1590 * and a target is created for each expanded word. Each of the resulting
1591 * nodes is then linked to each of the targets as one of its children.
1592 *
1593 * Certain targets and sources such as .PHONY or .PRECIOUS are handled
1594 * specially. These are the ones detailed by the specType variable.
1595 *
1596 * The storing of transformation rules such as '.c.o' is also taken care of
1597 * here. A target is recognized as a transformation rule by calling
1598 * Suff_IsTransform. If it is a transformation rule, its node is gotten
1599 * from the suffix module via Suff_AddTransform rather than the standard
1600 * Targ_FindNode in the target module.
1601 *
1602 * Upon return, the value of the line is unspecified.
1603 */
1604 static void
1605 ParseDoDependency(char *line)
1606 {
1607 char *cp; /* our current position */
1608 GNodeType op; /* the operator on the line */
1609 SearchPathList *paths; /* search paths to alter when parsing
1610 * a list of .PATH targets */
1611 GNodeType tOp; /* operator from special target */
1612 StringList *curTargs; /* target names to be found and added
1613 * to the targets list */
1614 char *lstart = line;
1615
1616 /*
1617 * specType contains the SPECial TYPE of the current target. It is SP_NOT
1618 * if the target is unspecial. If it *is* special, however, the children
1619 * are linked as children of the parent but not vice versa.
1620 */
1621 ParseSpecial specType = SP_NOT;
1622
1623 DEBUG1(PARSE, "ParseDoDependency(%s)\n", line);
1624 tOp = OP_NONE;
1625
1626 paths = NULL;
1627
1628 curTargs = Lst_New();
1629
1630 /*
1631 * First, grind through the targets.
1632 */
1633 if (!ParseDoDependencyTargets(&cp, &line, lstart, &specType, &tOp, &paths,
1634 curTargs))
1635 goto out;
1636
1637 /* Don't need the list of target names anymore.
1638 * The targets themselves are now in the global variable 'targets'. */
1639 Lst_Free(curTargs);
1640 curTargs = NULL;
1641
1642 if (!Lst_IsEmpty(targets))
1643 ParseDoDependencyCheckSpec(specType);
1644
1645 /*
1646 * Have now parsed all the target names. Must parse the operator next.
1647 */
1648 if (!ParseDoDependencyParseOp(&cp, lstart, &op))
1649 goto out;
1650
1651 /*
1652 * Apply the operator to the target. This is how we remember which
1653 * operator a target was defined with. It fails if the operator
1654 * used isn't consistent across all references.
1655 */
1656 ApplyDependencyOperator(op);
1657
1658 /*
1659 * Onward to the sources.
1660 *
1661 * LINE will now point to the first source word, if any, or the
1662 * end of the string if not.
1663 */
1664 pp_skip_whitespace(&cp);
1665 line = cp; /* XXX: 'line' is an inappropriate name */
1666
1667 /*
1668 * Several special targets take different actions if present with no
1669 * sources:
1670 * a .SUFFIXES line with no sources clears out all old suffixes
1671 * a .PRECIOUS line makes all targets precious
1672 * a .IGNORE line ignores errors for all targets
1673 * a .SILENT line creates silence when making all targets
1674 * a .PATH removes all directories from the search path(s).
1675 */
1676 if (line[0] == '\0') {
1677 ParseDoDependencySourcesEmpty(specType, paths);
1678 } else if (specType == SP_MFLAGS) {
1679 /*
1680 * Call on functions in main.c to deal with these arguments and
1681 * set the initial character to a null-character so the loop to
1682 * get sources won't get anything
1683 */
1684 Main_ParseArgLine(line);
1685 *line = '\0';
1686 } else if (specType == SP_SHELL) {
1687 if (!Job_ParseShell(line)) {
1688 Parse_Error(PARSE_FATAL, "improper shell specification");
1689 goto out;
1690 }
1691 *line = '\0';
1692 } else if (specType == SP_NOTPARALLEL || specType == SP_SINGLESHELL ||
1693 specType == SP_DELETE_ON_ERROR) {
1694 *line = '\0';
1695 }
1696
1697 /* Now go for the sources. */
1698 if (specType == SP_SUFFIXES || specType == SP_PATH ||
1699 specType == SP_INCLUDES || specType == SP_LIBS ||
1700 specType == SP_NULL || specType == SP_OBJDIR)
1701 {
1702 ParseDoDependencySourcesSpecial(line, cp, specType, paths);
1703 if (paths) {
1704 Lst_Free(paths);
1705 paths = NULL;
1706 }
1707 if (specType == SP_PATH)
1708 Dir_SetPATH();
1709 } else {
1710 assert(paths == NULL);
1711 if (!ParseDoDependencySourcesMundane(line, cp, specType, tOp))
1712 goto out;
1713 }
1714
1715 FindMainTarget();
1716
1717 out:
1718 if (paths != NULL)
1719 Lst_Free(paths);
1720 if (curTargs != NULL)
1721 Lst_Free(curTargs);
1722 }
1723
1724 typedef struct VarAssignParsed {
1725 const char *nameStart; /* unexpanded */
1726 const char *nameEnd; /* before operator adjustment */
1727 const char *eq; /* the '=' of the assignment operator */
1728 } VarAssignParsed;
1729
1730 /* Determine the assignment operator and adjust the end of the variable
1731 * name accordingly. */
1732 static void
1733 AdjustVarassignOp(const VarAssignParsed *pvar, const char *value,
1734 VarAssign *out_var)
1735 {
1736 const char *op = pvar->eq;
1737 const char * const name = pvar->nameStart;
1738 VarAssignOp type;
1739
1740 if (op > name && op[-1] == '+') {
1741 type = VAR_APPEND;
1742 op--;
1743
1744 } else if (op > name && op[-1] == '?') {
1745 op--;
1746 type = VAR_DEFAULT;
1747
1748 } else if (op > name && op[-1] == ':') {
1749 op--;
1750 type = VAR_SUBST;
1751
1752 } else if (op > name && op[-1] == '!') {
1753 op--;
1754 type = VAR_SHELL;
1755
1756 } else {
1757 type = VAR_NORMAL;
1758 #ifdef SUNSHCMD
1759 while (op > name && ch_isspace(op[-1]))
1760 op--;
1761
1762 if (op >= name + 3 && op[-3] == ':' && op[-2] == 's' && op[-1] == 'h') {
1763 type = VAR_SHELL;
1764 op -= 3;
1765 }
1766 #endif
1767 }
1768
1769 {
1770 const char *nameEnd = pvar->nameEnd < op ? pvar->nameEnd : op;
1771 out_var->varname = bmake_strsedup(pvar->nameStart, nameEnd);
1772 out_var->op = type;
1773 out_var->value = value;
1774 }
1775 }
1776
1777 /* Parse a variable assignment, consisting of a single-word variable name,
1778 * optional whitespace, an assignment operator, optional whitespace and the
1779 * variable value.
1780 *
1781 * Note: There is a lexical ambiguity with assignment modifier characters
1782 * in variable names. This routine interprets the character before the =
1783 * as a modifier. Therefore, an assignment like
1784 * C++=/usr/bin/CC
1785 * is interpreted as "C+ +=" instead of "C++ =".
1786 *
1787 * Used for both lines in a file and command line arguments. */
1788 Boolean
1789 Parse_IsVar(const char *p, VarAssign *out_var)
1790 {
1791 VarAssignParsed pvar;
1792 const char *firstSpace = NULL;
1793 int level = 0;
1794
1795 cpp_skip_hspace(&p); /* Skip to variable name */
1796
1797 /* During parsing, the '+' of the '+=' operator is initially parsed
1798 * as part of the variable name. It is later corrected, as is the ':sh'
1799 * modifier. Of these two (nameEnd and op), the earlier one determines the
1800 * actual end of the variable name. */
1801 pvar.nameStart = p;
1802 #ifdef CLEANUP
1803 pvar.nameEnd = NULL;
1804 pvar.eq = NULL;
1805 #endif
1806
1807 /* Scan for one of the assignment operators outside a variable expansion */
1808 while (*p != '\0') {
1809 char ch = *p++;
1810 if (ch == '(' || ch == '{') {
1811 level++;
1812 continue;
1813 }
1814 if (ch == ')' || ch == '}') {
1815 level--;
1816 continue;
1817 }
1818
1819 if (level != 0)
1820 continue;
1821
1822 if (ch == ' ' || ch == '\t')
1823 if (firstSpace == NULL)
1824 firstSpace = p - 1;
1825 while (ch == ' ' || ch == '\t')
1826 ch = *p++;
1827
1828 #ifdef SUNSHCMD
1829 if (ch == ':' && p[0] == 's' && p[1] == 'h') {
1830 p += 2;
1831 continue;
1832 }
1833 #endif
1834 if (ch == '=') {
1835 pvar.eq = p - 1;
1836 pvar.nameEnd = firstSpace != NULL ? firstSpace : p - 1;
1837 cpp_skip_whitespace(&p);
1838 AdjustVarassignOp(&pvar, p, out_var);
1839 return TRUE;
1840 }
1841 if (*p == '=' && (ch == '+' || ch == ':' || ch == '?' || ch == '!')) {
1842 pvar.eq = p;
1843 pvar.nameEnd = firstSpace != NULL ? firstSpace : p;
1844 p++;
1845 cpp_skip_whitespace(&p);
1846 AdjustVarassignOp(&pvar, p, out_var);
1847 return TRUE;
1848 }
1849 if (firstSpace != NULL)
1850 return FALSE;
1851 }
1852
1853 return FALSE;
1854 }
1855
1856 static void
1857 VarCheckSyntax(VarAssignOp type, const char *uvalue, GNode *ctxt)
1858 {
1859 if (opts.lint) {
1860 if (type != VAR_SUBST && strchr(uvalue, '$') != NULL) {
1861 /* Check for syntax errors such as unclosed expressions or
1862 * unknown modifiers. */
1863 char *expandedValue;
1864
1865 (void)Var_Subst(uvalue, ctxt, VARE_NONE, &expandedValue);
1866 /* TODO: handle errors */
1867 free(expandedValue);
1868 }
1869 }
1870 }
1871
1872 static void
1873 VarAssign_EvalSubst(const char *name, const char *uvalue, GNode *ctxt,
1874 const char **out_avalue, void **out_avalue_freeIt)
1875 {
1876 const char *avalue = uvalue;
1877 char *evalue;
1878 Boolean savedPreserveUndefined = preserveUndefined;
1879
1880 /* TODO: Can this assignment to preserveUndefined be moved further down
1881 * to the actually interesting Var_Subst call, without affecting any
1882 * edge cases?
1883 *
1884 * It might affect the implicit expansion of the variable name in the
1885 * Var_Exists and Var_Set calls, even though it's unlikely that anyone
1886 * cared about this edge case when adding this code. In addition,
1887 * variable assignments should not refer to any undefined variables in
1888 * the variable name. */
1889 preserveUndefined = TRUE;
1890
1891 /*
1892 * make sure that we set the variable the first time to nothing
1893 * so that it gets substituted!
1894 */
1895 if (!Var_Exists(name, ctxt))
1896 Var_Set(name, "", ctxt);
1897
1898 (void)Var_Subst(uvalue, ctxt, VARE_WANTRES|VARE_KEEP_DOLLAR, &evalue);
1899 /* TODO: handle errors */
1900 preserveUndefined = savedPreserveUndefined;
1901 avalue = evalue;
1902 Var_Set(name, avalue, ctxt);
1903
1904 *out_avalue = avalue;
1905 *out_avalue_freeIt = evalue;
1906 }
1907
1908 static void
1909 VarAssign_EvalShell(const char *name, const char *uvalue, GNode *ctxt,
1910 const char **out_avalue, void **out_avalue_freeIt)
1911 {
1912 const char *cmd, *errfmt;
1913 char *cmdOut;
1914 void *cmd_freeIt = NULL;
1915
1916 cmd = uvalue;
1917 if (strchr(cmd, '$') != NULL) {
1918 char *ecmd;
1919 (void)Var_Subst(cmd, VAR_CMDLINE, VARE_WANTRES | VARE_UNDEFERR, &ecmd);
1920 /* TODO: handle errors */
1921 cmd = cmd_freeIt = ecmd;
1922 }
1923
1924 cmdOut = Cmd_Exec(cmd, &errfmt);
1925 Var_Set(name, cmdOut, ctxt);
1926 *out_avalue = *out_avalue_freeIt = cmdOut;
1927
1928 if (errfmt)
1929 Parse_Error(PARSE_WARNING, errfmt, cmd);
1930
1931 free(cmd_freeIt);
1932 }
1933
1934 /* Perform a variable assignment.
1935 *
1936 * The actual value of the variable is returned in *out_avalue and
1937 * *out_avalue_freeIt. Especially for VAR_SUBST and VAR_SHELL this can differ
1938 * from the literal value.
1939 *
1940 * Return whether the assignment was actually done. The assignment is only
1941 * skipped if the operator is '?=' and the variable already exists. */
1942 static Boolean
1943 VarAssign_Eval(const char *name, VarAssignOp op, const char *uvalue,
1944 GNode *ctxt, const char **out_avalue, void **out_avalue_freeIt)
1945 {
1946 const char *avalue = uvalue;
1947 void *avalue_freeIt = NULL;
1948
1949 if (op == VAR_APPEND)
1950 Var_Append(name, uvalue, ctxt);
1951 else if (op == VAR_SUBST)
1952 VarAssign_EvalSubst(name, uvalue, ctxt, &avalue, &avalue_freeIt);
1953 else if (op == VAR_SHELL)
1954 VarAssign_EvalShell(name, uvalue, ctxt, &avalue, &avalue_freeIt);
1955 else {
1956 if (op == VAR_DEFAULT && Var_Exists(name, ctxt)) {
1957 *out_avalue_freeIt = NULL;
1958 return FALSE;
1959 }
1960
1961 /* Normal assignment -- just do it. */
1962 Var_Set(name, uvalue, ctxt);
1963 }
1964
1965 *out_avalue = avalue;
1966 *out_avalue_freeIt = avalue_freeIt;
1967 return TRUE;
1968 }
1969
1970 static void
1971 VarAssignSpecial(const char *name, const char *avalue)
1972 {
1973 if (strcmp(name, MAKEOVERRIDES) == 0)
1974 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */
1975 else if (strcmp(name, ".CURDIR") == 0) {
1976 /*
1977 * Someone is being (too?) clever...
1978 * Let's pretend they know what they are doing and
1979 * re-initialize the 'cur' CachedDir.
1980 */
1981 Dir_InitCur(avalue);
1982 Dir_SetPATH();
1983 } else if (strcmp(name, MAKE_JOB_PREFIX) == 0)
1984 Job_SetPrefix();
1985 else if (strcmp(name, MAKE_EXPORTED) == 0)
1986 Var_Export(avalue, FALSE);
1987 }
1988
1989 /* Perform the variable variable assignment in the given context. */
1990 void
1991 Parse_DoVar(VarAssign *var, GNode *ctxt)
1992 {
1993 const char *avalue; /* actual value (maybe expanded) */
1994 void *avalue_freeIt;
1995
1996 VarCheckSyntax(var->op, var->value, ctxt);
1997 if (VarAssign_Eval(var->varname, var->op, var->value, ctxt,
1998 &avalue, &avalue_freeIt))
1999 VarAssignSpecial(var->varname, avalue);
2000
2001 free(avalue_freeIt);
2002 free(var->varname);
2003 }
2004
2005
2006 /* See if the command possibly calls a sub-make by using the variable
2007 * expressions ${.MAKE}, ${MAKE} or the plain word "make". */
2008 static Boolean
2009 MaybeSubMake(const char *cmd)
2010 {
2011 const char *start;
2012
2013 for (start = cmd; *start != '\0'; start++) {
2014 const char *p = start;
2015 char endc;
2016
2017 /* XXX: What if progname != "make"? */
2018 if (p[0] == 'm' && p[1] == 'a' && p[2] == 'k' && p[3] == 'e')
2019 if (start == cmd || !ch_isalnum(p[-1]))
2020 if (!ch_isalnum(p[4]))
2021 return TRUE;
2022
2023 if (*p != '$')
2024 continue;
2025 p++;
2026
2027 if (*p == '{')
2028 endc = '}';
2029 else if (*p == '(')
2030 endc = ')';
2031 else
2032 continue;
2033 p++;
2034
2035 if (*p == '.') /* Accept either ${.MAKE} or ${MAKE}. */
2036 p++;
2037
2038 if (p[0] == 'M' && p[1] == 'A' && p[2] == 'K' && p[3] == 'E')
2039 if (p[4] == endc)
2040 return TRUE;
2041 }
2042 return FALSE;
2043 }
2044
2045 /* Append the command to the target node.
2046 *
2047 * The node may be marked as a submake node if the command is determined to
2048 * be that. */
2049 static void
2050 ParseAddCmd(GNode *gn, char *cmd)
2051 {
2052 /* Add to last (ie current) cohort for :: targets */
2053 if ((gn->type & OP_DOUBLEDEP) && gn->cohorts->last != NULL)
2054 gn = gn->cohorts->last->datum;
2055
2056 /* if target already supplied, ignore commands */
2057 if (!(gn->type & OP_HAS_COMMANDS)) {
2058 Lst_Append(gn->commands, cmd);
2059 if (MaybeSubMake(cmd))
2060 gn->type |= OP_SUBMAKE;
2061 ParseMark(gn);
2062 } else {
2063 #if 0
2064 /* XXX: We cannot do this until we fix the tree */
2065 Lst_Append(gn->commands, cmd);
2066 Parse_Error(PARSE_WARNING,
2067 "overriding commands for target \"%s\"; "
2068 "previous commands defined at %s: %d ignored",
2069 gn->name, gn->fname, gn->lineno);
2070 #else
2071 Parse_Error(PARSE_WARNING,
2072 "duplicate script for target \"%s\" ignored",
2073 gn->name);
2074 ParseErrorInternal(gn->fname, (size_t)gn->lineno, PARSE_WARNING,
2075 "using previous script for \"%s\" defined here",
2076 gn->name);
2077 #endif
2078 }
2079 }
2080
2081 /* Add a directory to the path searched for included makefiles bracketed
2082 * by double-quotes. */
2083 void
2084 Parse_AddIncludeDir(const char *dir)
2085 {
2086 (void)Dir_AddDir(parseIncPath, dir);
2087 }
2088
2089 /* Handle one of the .[-ds]include directives by remembering the current file
2090 * and pushing the included file on the stack. After the included file has
2091 * finished, parsing continues with the including file; see Parse_SetInput
2092 * and ParseEOF.
2093 *
2094 * System includes are looked up in sysIncPath, any other includes are looked
2095 * up in the parsedir and then in the directories specified by the -I command
2096 * line options.
2097 */
2098 static void
2099 Parse_include_file(char *file, Boolean isSystem, Boolean depinc, Boolean silent)
2100 {
2101 struct loadedfile *lf;
2102 char *fullname; /* full pathname of file */
2103 char *newName;
2104 char *slash, *incdir;
2105 int fd;
2106 int i;
2107
2108 fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
2109
2110 if (fullname == NULL && !isSystem) {
2111 /*
2112 * Include files contained in double-quotes are first searched
2113 * relative to the including file's location. We don't want to
2114 * cd there, of course, so we just tack on the old file's
2115 * leading path components and call Dir_FindFile to see if
2116 * we can locate the file.
2117 */
2118
2119 incdir = bmake_strdup(CurFile()->fname);
2120 slash = strrchr(incdir, '/');
2121 if (slash != NULL) {
2122 *slash = '\0';
2123 /* Now do lexical processing of leading "../" on the filename */
2124 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
2125 slash = strrchr(incdir + 1, '/');
2126 if (slash == NULL || strcmp(slash, "/..") == 0)
2127 break;
2128 *slash = '\0';
2129 }
2130 newName = str_concat3(incdir, "/", file + i);
2131 fullname = Dir_FindFile(newName, parseIncPath);
2132 if (fullname == NULL)
2133 fullname = Dir_FindFile(newName, dirSearchPath);
2134 free(newName);
2135 }
2136 free(incdir);
2137
2138 if (fullname == NULL) {
2139 /*
2140 * Makefile wasn't found in same directory as included makefile.
2141 * Search for it first on the -I search path,
2142 * then on the .PATH search path, if not found in a -I directory.
2143 * If we have a suffix specific path we should use that.
2144 */
2145 const char *suff;
2146 SearchPath *suffPath = NULL;
2147
2148 if ((suff = strrchr(file, '.'))) {
2149 suffPath = Suff_GetPath(suff);
2150 if (suffPath != NULL)
2151 fullname = Dir_FindFile(file, suffPath);
2152 }
2153 if (fullname == NULL) {
2154 fullname = Dir_FindFile(file, parseIncPath);
2155 if (fullname == NULL)
2156 fullname = Dir_FindFile(file, dirSearchPath);
2157 }
2158 }
2159 }
2160
2161 /* Looking for a system file or file still not found */
2162 if (fullname == NULL) {
2163 /*
2164 * Look for it on the system path
2165 */
2166 SearchPath *path = Lst_IsEmpty(sysIncPath) ? defSysIncPath : sysIncPath;
2167 fullname = Dir_FindFile(file, path);
2168 }
2169
2170 if (fullname == NULL) {
2171 if (!silent)
2172 Parse_Error(PARSE_FATAL, "Could not find %s", file);
2173 return;
2174 }
2175
2176 /* Actually open the file... */
2177 fd = open(fullname, O_RDONLY);
2178 if (fd == -1) {
2179 if (!silent)
2180 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
2181 free(fullname);
2182 return;
2183 }
2184
2185 /* load it */
2186 lf = loadfile(fullname, fd);
2187
2188 /* Start reading from this file next */
2189 Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf);
2190 CurFile()->lf = lf;
2191 if (depinc)
2192 doing_depend = depinc; /* only turn it on */
2193 }
2194
2195 static void
2196 ParseDoInclude(char *line)
2197 {
2198 char endc; /* the character which ends the file spec */
2199 char *cp; /* current position in file spec */
2200 Boolean silent = *line != 'i';
2201 char *file = line + (silent ? 8 : 7);
2202
2203 /* Skip to delimiter character so we know where to look */
2204 pp_skip_hspace(&file);
2205
2206 if (*file != '"' && *file != '<') {
2207 Parse_Error(PARSE_FATAL,
2208 ".include filename must be delimited by '\"' or '<'");
2209 return;
2210 }
2211
2212 /*
2213 * Set the search path on which to find the include file based on the
2214 * characters which bracket its name. Angle-brackets imply it's
2215 * a system Makefile while double-quotes imply it's a user makefile
2216 */
2217 if (*file == '<')
2218 endc = '>';
2219 else
2220 endc = '"';
2221
2222 /* Skip to matching delimiter */
2223 for (cp = ++file; *cp && *cp != endc; cp++)
2224 continue;
2225
2226 if (*cp != endc) {
2227 Parse_Error(PARSE_FATAL,
2228 "Unclosed .include filename. '%c' expected", endc);
2229 return;
2230 }
2231
2232 *cp = '\0';
2233
2234 /*
2235 * Substitute for any variables in the filename before trying to
2236 * find the file.
2237 */
2238 (void)Var_Subst(file, VAR_CMDLINE, VARE_WANTRES, &file);
2239 /* TODO: handle errors */
2240
2241 Parse_include_file(file, endc == '>', *line == 'd', silent);
2242 free(file);
2243 }
2244
2245 /* Split filename into dirname + basename, then assign these to the
2246 * given variables. */
2247 static void
2248 SetFilenameVars(const char *filename, const char *dirvar, const char *filevar)
2249 {
2250 const char *slash, *dirname, *basename;
2251 void *freeIt;
2252
2253 slash = strrchr(filename, '/');
2254 if (slash == NULL) {
2255 dirname = curdir;
2256 basename = filename;
2257 freeIt = NULL;
2258 } else {
2259 dirname = freeIt = bmake_strsedup(filename, slash);
2260 basename = slash + 1;
2261 }
2262
2263 Var_Set(dirvar, dirname, VAR_GLOBAL);
2264 Var_Set(filevar, basename, VAR_GLOBAL);
2265
2266 DEBUG5(PARSE, "%s: ${%s} = `%s' ${%s} = `%s'\n",
2267 __func__, dirvar, dirname, filevar, basename);
2268 free(freeIt);
2269 }
2270
2271 /* Return the immediately including file.
2272 *
2273 * This is made complicated since the .for loop is implemented as a special
2274 * kind of .include; see For_Run. */
2275 static const char *
2276 GetActuallyIncludingFile(void)
2277 {
2278 size_t i;
2279 const IFile *incs = GetInclude(0);
2280
2281 for (i = includes.len; i >= 2; i--)
2282 if (!incs[i - 1].fromForLoop)
2283 return incs[i - 2].fname;
2284 return NULL;
2285 }
2286
2287 /* Set .PARSEDIR, .PARSEFILE, .INCLUDEDFROMDIR and .INCLUDEDFROMFILE. */
2288 static void
2289 ParseSetParseFile(const char *filename)
2290 {
2291 const char *including;
2292
2293 SetFilenameVars(filename, ".PARSEDIR", ".PARSEFILE");
2294
2295 including = GetActuallyIncludingFile();
2296 if (including != NULL) {
2297 SetFilenameVars(including,
2298 ".INCLUDEDFROMDIR", ".INCLUDEDFROMFILE");
2299 } else {
2300 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
2301 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
2302 }
2303 }
2304
2305 static Boolean
2306 StrContainsWord(const char *str, const char *word)
2307 {
2308 size_t strLen = strlen(str);
2309 size_t wordLen = strlen(word);
2310 const char *p, *end;
2311
2312 if (strLen < wordLen)
2313 return FALSE; /* str is too short to contain word */
2314
2315 end = str + strLen - wordLen;
2316 for (p = str; p != NULL; p = strchr(p, ' ')) {
2317 if (*p == ' ')
2318 p++;
2319 if (p > end)
2320 return FALSE; /* cannot contain word */
2321
2322 if (memcmp(p, word, wordLen) == 0 &&
2323 (p[wordLen] == '\0' || p[wordLen] == ' '))
2324 return TRUE;
2325 }
2326 return FALSE;
2327 }
2328
2329 /* XXX: Searching through a set of words with this linear search is
2330 * inefficient for variables that contain thousands of words. */
2331 /* XXX: The paths in this list don't seem to be normalized in any way. */
2332 static Boolean
2333 VarContainsWord(const char *varname, const char *word)
2334 {
2335 void *val_freeIt;
2336 const char *val = Var_Value(varname, VAR_GLOBAL, &val_freeIt);
2337 Boolean found = val != NULL && StrContainsWord(val, word);
2338 bmake_free(val_freeIt);
2339 return found;
2340 }
2341
2342 /* Track the makefiles we read - so makefiles can set dependencies on them.
2343 * Avoid adding anything more than once.
2344 *
2345 * Time complexity: O(n) per call, in total O(n^2), where n is the number
2346 * of makefiles that have been loaded. */
2347 static void
2348 ParseTrackInput(const char *name)
2349 {
2350 if (!VarContainsWord(MAKE_MAKEFILES, name))
2351 Var_Append(MAKE_MAKEFILES, name, VAR_GLOBAL);
2352 }
2353
2354
2355 /* Start parsing from the given source.
2356 *
2357 * The given file is added to the includes stack. */
2358 void
2359 Parse_SetInput(const char *name, int line, int fd,
2360 char *(*nextbuf)(void *, size_t *), void *arg)
2361 {
2362 IFile *curFile;
2363 char *buf;
2364 size_t len;
2365 Boolean fromForLoop = name == NULL;
2366
2367 if (fromForLoop)
2368 name = CurFile()->fname;
2369 else
2370 ParseTrackInput(name);
2371
2372 if (DEBUG(PARSE))
2373 debug_printf("%s: file %s, line %d, fd %d, nextbuf %s, arg %p\n",
2374 __func__, name, line, fd,
2375 nextbuf == loadedfile_nextbuf ? "loadedfile" : "other",
2376 arg);
2377
2378 if (fd == -1 && nextbuf == NULL)
2379 /* sanity */
2380 return;
2381
2382 curFile = Vector_Push(&includes);
2383 curFile->fname = bmake_strdup(name);
2384 curFile->fromForLoop = fromForLoop;
2385 curFile->lineno = line;
2386 curFile->first_lineno = line;
2387 curFile->nextbuf = nextbuf;
2388 curFile->nextbuf_arg = arg;
2389 curFile->lf = NULL;
2390 curFile->depending = doing_depend; /* restore this on EOF */
2391
2392 assert(nextbuf != NULL);
2393
2394 /* Get first block of input data */
2395 buf = curFile->nextbuf(curFile->nextbuf_arg, &len);
2396 if (buf == NULL) {
2397 /* Was all a waste of time ... */
2398 if (curFile->fname)
2399 free(curFile->fname);
2400 free(curFile);
2401 return;
2402 }
2403 curFile->buf_freeIt = buf;
2404 curFile->buf_ptr = buf;
2405 curFile->buf_end = buf + len;
2406
2407 curFile->cond_depth = Cond_save_depth();
2408 ParseSetParseFile(name);
2409 }
2410
2411 /* Check if the directive is an include directive. */
2412 static Boolean
2413 IsInclude(const char *dir, Boolean sysv)
2414 {
2415 if (dir[0] == 's' || dir[0] == '-' || (dir[0] == 'd' && !sysv))
2416 dir++;
2417
2418 if (strncmp(dir, "include", 7) != 0)
2419 return FALSE;
2420
2421 /* Space is not mandatory for BSD .include */
2422 return !sysv || ch_isspace(dir[7]);
2423 }
2424
2425
2426 #ifdef SYSVINCLUDE
2427 /* Check if the line is a SYSV include directive. */
2428 static Boolean
2429 IsSysVInclude(const char *line)
2430 {
2431 const char *p;
2432
2433 if (!IsInclude(line, TRUE))
2434 return FALSE;
2435
2436 /* Avoid interpreting a dependency line as an include */
2437 for (p = line; (p = strchr(p, ':')) != NULL;) {
2438
2439 /* end of line -> it's a dependency */
2440 if (*++p == '\0')
2441 return FALSE;
2442
2443 /* '::' operator or ': ' -> it's a dependency */
2444 if (*p == ':' || ch_isspace(*p))
2445 return FALSE;
2446 }
2447 return TRUE;
2448 }
2449
2450 /* Push to another file. The line points to the word "include". */
2451 static void
2452 ParseTraditionalInclude(char *line)
2453 {
2454 char *cp; /* current position in file spec */
2455 Boolean done = FALSE;
2456 Boolean silent = line[0] != 'i';
2457 char *file = line + (silent ? 8 : 7);
2458 char *all_files;
2459
2460 DEBUG2(PARSE, "%s: %s\n", __func__, file);
2461
2462 pp_skip_whitespace(&file);
2463
2464 /*
2465 * Substitute for any variables in the file name before trying to
2466 * find the thing.
2467 */
2468 (void)Var_Subst(file, VAR_CMDLINE, VARE_WANTRES, &all_files);
2469 /* TODO: handle errors */
2470
2471 if (*file == '\0') {
2472 Parse_Error(PARSE_FATAL, "Filename missing from \"include\"");
2473 goto out;
2474 }
2475
2476 for (file = all_files; !done; file = cp + 1) {
2477 /* Skip to end of line or next whitespace */
2478 for (cp = file; *cp && !ch_isspace(*cp); cp++)
2479 continue;
2480
2481 if (*cp != '\0')
2482 *cp = '\0';
2483 else
2484 done = TRUE;
2485
2486 Parse_include_file(file, FALSE, FALSE, silent);
2487 }
2488 out:
2489 free(all_files);
2490 }
2491 #endif
2492
2493 #ifdef GMAKEEXPORT
2494 /* Parse "export <variable>=<value>", and actually export it. */
2495 static void
2496 ParseGmakeExport(char *line)
2497 {
2498 char *variable = line + 6;
2499 char *value;
2500
2501 DEBUG2(PARSE, "%s: %s\n", __func__, variable);
2502
2503 pp_skip_whitespace(&variable);
2504
2505 for (value = variable; *value && *value != '='; value++)
2506 continue;
2507
2508 if (*value != '=') {
2509 Parse_Error(PARSE_FATAL,
2510 "Variable/Value missing from \"export\"");
2511 return;
2512 }
2513 *value++ = '\0'; /* terminate variable */
2514
2515 /*
2516 * Expand the value before putting it in the environment.
2517 */
2518 (void)Var_Subst(value, VAR_CMDLINE, VARE_WANTRES, &value);
2519 /* TODO: handle errors */
2520
2521 setenv(variable, value, 1);
2522 free(value);
2523 }
2524 #endif
2525
2526 /* Called when EOF is reached in the current file. If we were reading an
2527 * include file or a .for loop, the includes stack is popped and things set
2528 * up to go back to reading the previous file at the previous location.
2529 *
2530 * Results:
2531 * TRUE to continue parsing, i.e. it had only reached the end of an
2532 * included file, FALSE if the main file has been parsed completely.
2533 */
2534 static Boolean
2535 ParseEOF(void)
2536 {
2537 char *ptr;
2538 size_t len;
2539 IFile *curFile = CurFile();
2540
2541 assert(curFile->nextbuf != NULL);
2542
2543 doing_depend = curFile->depending; /* restore this */
2544 /* get next input buffer, if any */
2545 ptr = curFile->nextbuf(curFile->nextbuf_arg, &len);
2546 curFile->buf_ptr = ptr;
2547 curFile->buf_freeIt = ptr;
2548 curFile->buf_end = ptr + len; /* XXX: undefined behavior if ptr == NULL */
2549 curFile->lineno = curFile->first_lineno;
2550 if (ptr != NULL)
2551 return TRUE; /* Iterate again */
2552
2553 /* Ensure the makefile (or loop) didn't have mismatched conditionals */
2554 Cond_restore_depth(curFile->cond_depth);
2555
2556 if (curFile->lf != NULL) {
2557 loadedfile_destroy(curFile->lf);
2558 curFile->lf = NULL;
2559 }
2560
2561 /* Dispose of curFile info */
2562 /* Leak curFile->fname because all the gnodes have pointers to it */
2563 free(curFile->buf_freeIt);
2564 Vector_Pop(&includes);
2565
2566 if (includes.len == 0) {
2567 /* We've run out of input */
2568 Var_Delete(".PARSEDIR", VAR_GLOBAL);
2569 Var_Delete(".PARSEFILE", VAR_GLOBAL);
2570 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
2571 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
2572 return FALSE;
2573 }
2574
2575 curFile = CurFile();
2576 DEBUG2(PARSE, "ParseEOF: returning to file %s, line %d\n",
2577 curFile->fname, curFile->lineno);
2578
2579 ParseSetParseFile(curFile->fname);
2580 return TRUE;
2581 }
2582
2583 typedef enum GetLineMode {
2584 PARSE_NORMAL,
2585 PARSE_RAW,
2586 PARSE_SKIP
2587 } GetLineMode;
2588
2589 static char *
2590 ParseGetLine(GetLineMode mode)
2591 {
2592 IFile *cf = CurFile();
2593 char *ptr;
2594 char ch;
2595 char *line;
2596 char *line_end;
2597 char *escaped;
2598 char *comment;
2599 char *tp;
2600
2601 /* Loop through blank lines and comment lines */
2602 for (;;) {
2603 cf->lineno++;
2604 line = cf->buf_ptr;
2605 ptr = line;
2606 line_end = line;
2607 escaped = NULL;
2608 comment = NULL;
2609 for (;;) {
2610 /* XXX: can buf_end ever be null? */
2611 if (cf->buf_end != NULL && ptr == cf->buf_end) {
2612 /* end of buffer */
2613 ch = '\0';
2614 break;
2615 }
2616 ch = *ptr;
2617 if (ch == '\0' || (ch == '\\' && ptr[1] == '\0')) {
2618 /* XXX: can buf_end ever be null? */
2619 if (cf->buf_end == NULL)
2620 /* End of string (aka for loop) data */
2621 break;
2622 /* see if there is more we can parse */
2623 while (ptr++ < cf->buf_end) {
2624 if ((ch = *ptr) == '\n') {
2625 if (ptr > line && ptr[-1] == '\\')
2626 continue;
2627 Parse_Error(PARSE_WARNING,
2628 "Zero byte read from file, "
2629 "skipping rest of line.");
2630 break;
2631 }
2632 }
2633 /* XXX: Can cf->nextbuf ever be NULL? */
2634 if (cf->nextbuf != NULL) {
2635 /*
2636 * End of this buffer; return EOF and outer logic
2637 * will get the next one. (eww)
2638 */
2639 break;
2640 }
2641 Parse_Error(PARSE_FATAL, "Zero byte read from file");
2642 return NULL;
2643 }
2644
2645 if (ch == '\\') {
2646 /* Don't treat next character as special, remember first one */
2647 if (escaped == NULL)
2648 escaped = ptr;
2649 if (ptr[1] == '\n')
2650 cf->lineno++;
2651 ptr += 2;
2652 line_end = ptr;
2653 continue;
2654 }
2655 if (ch == '#' && comment == NULL) {
2656 /* Remember first '#' for comment stripping */
2657 /* Unless previous char was '[', as in modifier :[#] */
2658 if (!(ptr > line && ptr[-1] == '['))
2659 comment = line_end;
2660 }
2661 ptr++;
2662 if (ch == '\n')
2663 break;
2664 if (!ch_isspace(ch))
2665 /* We are not interested in trailing whitespace */
2666 line_end = ptr;
2667 }
2668
2669 /* Save next 'to be processed' location */
2670 cf->buf_ptr = ptr;
2671
2672 /* Check we have a non-comment, non-blank line */
2673 if (line_end == line || comment == line) {
2674 if (ch == '\0')
2675 /* At end of file */
2676 return NULL;
2677 /* Parse another line */
2678 continue;
2679 }
2680
2681 /* We now have a line of data */
2682 *line_end = '\0';
2683
2684 if (mode == PARSE_RAW) {
2685 /* Leave '\' (etc) in line buffer (eg 'for' lines) */
2686 return line;
2687 }
2688
2689 if (mode == PARSE_SKIP) {
2690 /* Completely ignore non-directives */
2691 if (line[0] != '.')
2692 continue;
2693 /* We could do more of the .else/.elif/.endif checks here */
2694 }
2695 break;
2696 }
2697
2698 /* Brutally ignore anything after a non-escaped '#' in non-commands */
2699 if (comment != NULL && line[0] != '\t') {
2700 line_end = comment;
2701 *line_end = '\0';
2702 }
2703
2704 /* If we didn't see a '\\' then the in-situ data is fine */
2705 if (escaped == NULL)
2706 return line;
2707
2708 /* Remove escapes from '\n' and '#' */
2709 tp = ptr = escaped;
2710 escaped = line;
2711 for (; ; *tp++ = ch) {
2712 ch = *ptr++;
2713 if (ch != '\\') {
2714 if (ch == '\0')
2715 break;
2716 continue;
2717 }
2718
2719 ch = *ptr++;
2720 if (ch == '\0') {
2721 /* Delete '\\' at end of buffer */
2722 tp--;
2723 break;
2724 }
2725
2726 if (ch == '#' && line[0] != '\t')
2727 /* Delete '\\' from before '#' on non-command lines */
2728 continue;
2729
2730 if (ch != '\n') {
2731 /* Leave '\\' in buffer for later */
2732 *tp++ = '\\';
2733 /* Make sure we don't delete an escaped ' ' from the line end */
2734 escaped = tp + 1;
2735 continue;
2736 }
2737
2738 /* Escaped '\n' -- replace following whitespace with a single ' '. */
2739 pp_skip_hspace(&ptr);
2740 ch = ' ';
2741 }
2742
2743 /* Delete any trailing spaces - eg from empty continuations */
2744 while (tp > escaped && ch_isspace(tp[-1]))
2745 tp--;
2746
2747 *tp = '\0';
2748 return line;
2749 }
2750
2751 /* Read an entire line from the input file. Called only by Parse_File.
2752 *
2753 * Results:
2754 * A line without its newline and without any trailing whitespace.
2755 */
2756 static char *
2757 ParseReadLine(void)
2758 {
2759 char *line; /* Result */
2760 int lineno; /* Saved line # */
2761 int rval;
2762
2763 for (;;) {
2764 line = ParseGetLine(PARSE_NORMAL);
2765 if (line == NULL)
2766 return NULL;
2767
2768 if (line[0] != '.')
2769 return line;
2770
2771 /*
2772 * The line might be a conditional. Ask the conditional module
2773 * about it and act accordingly
2774 */
2775 switch (Cond_EvalLine(line)) {
2776 case COND_SKIP:
2777 /* Skip to next conditional that evaluates to COND_PARSE. */
2778 do {
2779 line = ParseGetLine(PARSE_SKIP);
2780 } while (line && Cond_EvalLine(line) != COND_PARSE);
2781 if (line == NULL)
2782 break;
2783 continue;
2784 case COND_PARSE:
2785 continue;
2786 case COND_INVALID: /* Not a conditional line */
2787 /* Check for .for loops */
2788 rval = For_Eval(line);
2789 if (rval == 0)
2790 /* Not a .for line */
2791 break;
2792 if (rval < 0)
2793 /* Syntax error - error printed, ignore line */
2794 continue;
2795 /* Start of a .for loop */
2796 lineno = CurFile()->lineno;
2797 /* Accumulate loop lines until matching .endfor */
2798 do {
2799 line = ParseGetLine(PARSE_RAW);
2800 if (line == NULL) {
2801 Parse_Error(PARSE_FATAL,
2802 "Unexpected end of file in for loop.");
2803 break;
2804 }
2805 } while (For_Accum(line));
2806 /* Stash each iteration as a new 'input file' */
2807 For_Run(lineno);
2808 /* Read next line from for-loop buffer */
2809 continue;
2810 }
2811 return line;
2812 }
2813 }
2814
2815 static void
2816 FinishDependencyGroup(void)
2817 {
2818 GNodeListNode *ln;
2819
2820 if (targets == NULL)
2821 return;
2822
2823 for (ln = targets->first; ln != NULL; ln = ln->next) {
2824 GNode *gn = ln->datum;
2825
2826 Suff_EndTransform(gn);
2827
2828 /* Mark the target as already having commands if it does, to
2829 * keep from having shell commands on multiple dependency lines. */
2830 if (!Lst_IsEmpty(gn->commands))
2831 gn->type |= OP_HAS_COMMANDS;
2832 }
2833
2834 Lst_Free(targets);
2835 targets = NULL;
2836 }
2837
2838 /* Add the command to each target from the current dependency spec. */
2839 static void
2840 ParseLine_ShellCommand(const char *p)
2841 {
2842 cpp_skip_whitespace(&p);
2843 if (*p == '\0')
2844 return; /* skip empty commands */
2845
2846 if (targets == NULL) {
2847 Parse_Error(PARSE_FATAL, "Unassociated shell command \"%s\"", p);
2848 return;
2849 }
2850
2851 {
2852 char *cmd = bmake_strdup(p);
2853 GNodeListNode *ln;
2854
2855 for (ln = targets->first; ln != NULL; ln = ln->next) {
2856 GNode *gn = ln->datum;
2857 ParseAddCmd(gn, cmd);
2858 }
2859 #ifdef CLEANUP
2860 Lst_Append(targCmds, cmd);
2861 #endif
2862 }
2863 }
2864
2865 static Boolean
2866 ParseDirective(char *line)
2867 {
2868 char *cp;
2869
2870 if (*line == '.') {
2871 /*
2872 * Lines that begin with '.' can be pretty much anything:
2873 * - directives like '.include' or '.if',
2874 * - suffix rules like '.c.o:',
2875 * - dependencies for filenames that start with '.',
2876 * - variable assignments like '.tmp=value'.
2877 */
2878 cp = line + 1;
2879 pp_skip_whitespace(&cp);
2880 if (IsInclude(cp, FALSE)) {
2881 ParseDoInclude(cp);
2882 return TRUE;
2883 }
2884 if (strncmp(cp, "undef", 5) == 0) {
2885 const char *varname;
2886 cp += 5;
2887 pp_skip_whitespace(&cp);
2888 varname = cp;
2889 for (; !ch_isspace(*cp) && *cp != '\0'; cp++)
2890 continue;
2891 *cp = '\0';
2892 Var_Delete(varname, VAR_GLOBAL);
2893 /* TODO: undefine all variables, not only the first */
2894 /* TODO: use Str_Words, like everywhere else */
2895 return TRUE;
2896 } else if (strncmp(cp, "export", 6) == 0) {
2897 cp += 6;
2898 pp_skip_whitespace(&cp);
2899 Var_Export(cp, TRUE);
2900 return TRUE;
2901 } else if (strncmp(cp, "unexport", 8) == 0) {
2902 Var_UnExport(cp);
2903 return TRUE;
2904 } else if (strncmp(cp, "info", 4) == 0 ||
2905 strncmp(cp, "error", 5) == 0 ||
2906 strncmp(cp, "warning", 7) == 0) {
2907 if (ParseMessage(cp))
2908 return TRUE;
2909 }
2910 }
2911 return FALSE;
2912 }
2913
2914 static Boolean
2915 ParseVarassign(const char *line)
2916 {
2917 VarAssign var;
2918
2919 if (!Parse_IsVar(line, &var))
2920 return FALSE;
2921
2922 FinishDependencyGroup();
2923 Parse_DoVar(&var, VAR_GLOBAL);
2924 return TRUE;
2925 }
2926
2927 static char *
2928 FindSemicolon(char *p)
2929 {
2930 int level = 0;
2931
2932 for (; *p != '\0'; p++) {
2933 if (*p == '\\' && p[1] != '\0') {
2934 p++;
2935 continue;
2936 }
2937
2938 if (*p == '$' && (p[1] == '(' || p[1] == '{'))
2939 level++;
2940 else if (level > 0 && (*p == ')' || *p == '}'))
2941 level--;
2942 else if (level == 0 && *p == ';')
2943 break;
2944 }
2945 return p;
2946 }
2947
2948 /* dependency -> target... op [source...]
2949 * op -> ':' | '::' | '!' */
2950 static void
2951 ParseDependency(char *line)
2952 {
2953 VarEvalFlags eflags;
2954 char *expanded_line;
2955 const char *shellcmd = NULL;
2956
2957 /*
2958 * For some reason - probably to make the parser impossible -
2959 * a ';' can be used to separate commands from dependencies.
2960 * Attempt to avoid ';' inside substitution patterns.
2961 */
2962 {
2963 char *semicolon = FindSemicolon(line);
2964 if (*semicolon != '\0') {
2965 /* Terminate the dependency list at the ';' */
2966 *semicolon = '\0';
2967 shellcmd = semicolon + 1;
2968 }
2969 }
2970
2971 /*
2972 * We now know it's a dependency line so it needs to have all
2973 * variables expanded before being parsed.
2974 *
2975 * XXX: Ideally the dependency line would first be split into
2976 * its left-hand side, dependency operator and right-hand side,
2977 * and then each side would be expanded on its own. This would
2978 * allow for the left-hand side to allow only defined variables
2979 * and to allow variables on the right-hand side to be undefined
2980 * as well.
2981 *
2982 * Parsing the line first would also prevent that targets
2983 * generated from variable expressions are interpreted as the
2984 * dependency operator, such as in "target${:U\:} middle: source",
2985 * in which the middle is interpreted as a source, not a target.
2986 */
2987
2988 /* In lint mode, allow undefined variables to appear in
2989 * dependency lines.
2990 *
2991 * Ideally, only the right-hand side would allow undefined
2992 * variables since it is common to have optional dependencies.
2993 * Having undefined variables on the left-hand side is more
2994 * unusual though. Since both sides are expanded in a single
2995 * pass, there is not much choice what to do here.
2996 *
2997 * In normal mode, it does not matter whether undefined
2998 * variables are allowed or not since as of 2020-09-14,
2999 * Var_Parse does not print any parse errors in such a case.
3000 * It simply returns the special empty string var_Error,
3001 * which cannot be detected in the result of Var_Subst. */
3002 eflags = opts.lint ? VARE_WANTRES : VARE_WANTRES | VARE_UNDEFERR;
3003 (void)Var_Subst(line, VAR_CMDLINE, eflags, &expanded_line);
3004 /* TODO: handle errors */
3005
3006 /* Need a fresh list for the target nodes */
3007 if (targets != NULL)
3008 Lst_Free(targets);
3009 targets = Lst_New();
3010
3011 ParseDoDependency(expanded_line);
3012 free(expanded_line);
3013
3014 if (shellcmd != NULL)
3015 ParseLine_ShellCommand(shellcmd);
3016 }
3017
3018 static void
3019 ParseLine(char *line)
3020 {
3021 if (ParseDirective(line))
3022 return;
3023
3024 if (*line == '\t') {
3025 ParseLine_ShellCommand(line + 1);
3026 return;
3027 }
3028
3029 #ifdef SYSVINCLUDE
3030 if (IsSysVInclude(line)) {
3031 /*
3032 * It's an S3/S5-style "include".
3033 */
3034 ParseTraditionalInclude(line);
3035 return;
3036 }
3037 #endif
3038
3039 #ifdef GMAKEEXPORT
3040 if (strncmp(line, "export", 6) == 0 && ch_isspace(line[6]) &&
3041 strchr(line, ':') == NULL) {
3042 /*
3043 * It's a Gmake "export".
3044 */
3045 ParseGmakeExport(line);
3046 return;
3047 }
3048 #endif
3049
3050 if (ParseVarassign(line))
3051 return;
3052
3053 FinishDependencyGroup();
3054
3055 ParseDependency(line);
3056 }
3057
3058 /* Parse a top-level makefile, incorporating its content into the global
3059 * dependency graph.
3060 *
3061 * Input:
3062 * name The name of the file being read
3063 * fd The open file to parse; will be closed at the end
3064 */
3065 void
3066 Parse_File(const char *name, int fd)
3067 {
3068 char *line; /* the line we're working on */
3069 struct loadedfile *lf;
3070
3071 lf = loadfile(name, fd);
3072
3073 assert(targets == NULL);
3074
3075 if (name == NULL)
3076 name = "(stdin)";
3077
3078 Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf);
3079 CurFile()->lf = lf;
3080
3081 do {
3082 while ((line = ParseReadLine()) != NULL) {
3083 DEBUG2(PARSE, "ParseReadLine (%d): '%s'\n",
3084 CurFile()->lineno, line);
3085 ParseLine(line);
3086 }
3087 /*
3088 * Reached EOF, but it may be just EOF of an include file...
3089 */
3090 } while (ParseEOF());
3091
3092 FinishDependencyGroup();
3093
3094 if (fatals != 0) {
3095 (void)fflush(stdout);
3096 (void)fprintf(stderr,
3097 "%s: Fatal errors encountered -- cannot continue",
3098 progname);
3099 PrintOnError(NULL, NULL);
3100 exit(1);
3101 }
3102 }
3103
3104 /* Initialize the parsing module. */
3105 void
3106 Parse_Init(void)
3107 {
3108 mainNode = NULL;
3109 parseIncPath = Lst_New();
3110 sysIncPath = Lst_New();
3111 defSysIncPath = Lst_New();
3112 Vector_Init(&includes, sizeof(IFile));
3113 #ifdef CLEANUP
3114 targCmds = Lst_New();
3115 #endif
3116 }
3117
3118 /* Clean up the parsing module. */
3119 void
3120 Parse_End(void)
3121 {
3122 #ifdef CLEANUP
3123 Lst_Destroy(targCmds, free);
3124 assert(targets == NULL);
3125 Lst_Destroy(defSysIncPath, Dir_Destroy);
3126 Lst_Destroy(sysIncPath, Dir_Destroy);
3127 Lst_Destroy(parseIncPath, Dir_Destroy);
3128 assert(includes.len == 0);
3129 Vector_Done(&includes);
3130 #endif
3131 }
3132
3133
3134 /*
3135 * Return a list containing the single main target to create.
3136 * If no such target exists, we Punt with an obnoxious error message.
3137 */
3138 GNodeList *
3139 Parse_MainName(void)
3140 {
3141 GNodeList *mainList;
3142
3143 mainList = Lst_New();
3144
3145 if (mainNode == NULL)
3146 Punt("no target to make.");
3147
3148 if (mainNode->type & OP_DOUBLEDEP) {
3149 Lst_Append(mainList, mainNode);
3150 Lst_AppendAll(mainList, mainNode->cohorts);
3151 } else
3152 Lst_Append(mainList, mainNode);
3153
3154 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
3155
3156 return mainList;
3157 }
3158
3159 int
3160 Parse_GetFatals(void)
3161 {
3162 return fatals;
3163 }
3164