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