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