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