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