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