1 /* $NetBSD: make.h,v 1.364 2026/02/10 18:53:34 sjg Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * from: @(#)make.h 8.3 (Berkeley) 6/13/95 35 */ 36 37 /* 38 * Copyright (c) 1989 by Berkeley Softworks 39 * All rights reserved. 40 * 41 * This code is derived from software contributed to Berkeley by 42 * Adam de Boor. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * from: @(#)make.h 8.3 (Berkeley) 6/13/95 73 */ 74 75 /* 76 * make.h -- 77 * The global definitions for make 78 */ 79 80 #ifndef MAKE_MAKE_H 81 #define MAKE_MAKE_H 82 83 #include <sys/types.h> 84 #include <sys/param.h> 85 #include <sys/stat.h> 86 87 #include <assert.h> 88 #include <ctype.h> 89 #include <fcntl.h> 90 #include <stdarg.h> 91 #include <stdio.h> 92 #include <stdlib.h> 93 #include <string.h> 94 #include <unistd.h> 95 96 #ifdef BSD4_4 97 # include <sys/cdefs.h> 98 #endif 99 100 #ifndef FD_CLOEXEC 101 #define FD_CLOEXEC 1 102 #endif 103 104 #if defined(__GNUC__) 105 #define MAKE_GNUC_PREREQ(x, y) \ 106 ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) || \ 107 (__GNUC__ > (x))) 108 #else 109 #define MAKE_GNUC_PREREQ(x, y) 0 110 #endif 111 112 #if MAKE_GNUC_PREREQ(2, 7) || lint 113 #define MAKE_ATTR_UNUSED __attribute__((__unused__)) 114 #else 115 #define MAKE_ATTR_UNUSED /* delete */ 116 #endif 117 118 #if MAKE_GNUC_PREREQ(2, 5) 119 #define MAKE_ATTR_DEAD __attribute__((__noreturn__)) 120 #elif defined(__GNUC__) 121 #define MAKE_ATTR_DEAD __volatile 122 #else 123 #define MAKE_ATTR_DEAD /* delete */ 124 #endif 125 126 #if MAKE_GNUC_PREREQ(2, 7) 127 #define MAKE_ATTR_PRINTFLIKE(fmtarg, firstvararg) \ 128 __attribute__((__format__ (__printf__, fmtarg, firstvararg))) 129 #else 130 #define MAKE_ATTR_PRINTFLIKE(fmtarg, firstvararg) /* delete */ 131 #endif 132 133 #if MAKE_GNUC_PREREQ(4, 0) 134 #define MAKE_ATTR_USE __attribute__((__warn_unused_result__)) 135 #else 136 #define MAKE_ATTR_USE /* delete */ 137 #endif 138 139 #if MAKE_GNUC_PREREQ(8, 0) 140 #define MAKE_ATTR_NOINLINE __attribute__((__noinline__)) 141 #else 142 #define MAKE_ATTR_NOINLINE /* delete */ 143 #endif 144 145 #if __STDC_VERSION__ >= 199901L || defined(lint) 146 #define MAKE_INLINE static inline MAKE_ATTR_UNUSED 147 #else 148 #define MAKE_INLINE static MAKE_ATTR_UNUSED 149 #endif 150 151 /* MAKE_STATIC marks a function that may or may not be inlined. */ 152 #if defined(lint) 153 /* As of 2021-07-31, NetBSD lint ignores __attribute__((unused)). */ 154 #define MAKE_STATIC MAKE_INLINE 155 #else 156 #define MAKE_STATIC static MAKE_ATTR_UNUSED 157 #endif 158 159 #if __STDC_VERSION__ >= 199901L || defined(lint) || defined(USE_C99_BOOLEAN) 160 #include <stdbool.h> 161 #elif defined(__bool_true_false_are_defined) 162 /* 163 * All files of make must be compiled with the same definition of bool. 164 * Since one of the files includes <stdbool.h>, that means the header is 165 * available on this platform. Recompile everything with -DUSE_C99_BOOLEAN. 166 */ 167 #error "<stdbool.h> is included in pre-C99 mode" 168 #elif defined(bool) || defined(true) || defined(false) 169 /* 170 * In pre-C99 mode, make does not expect that bool is already defined. 171 * You need to ensure that all translation units use the same definition for 172 * bool. 173 */ 174 #error "bool/true/false is defined in pre-C99 mode" 175 #else 176 typedef unsigned char bool; 177 #define true 1 178 #define false 0 179 #endif 180 181 /* 182 * In code coverage mode with gcc>=12, calling vfork/exec does not mark any 183 * further code from the parent process as covered. gcc-10.5.0 is fine, as 184 * are fork/exec calls, as well as posix_spawn. 185 */ 186 #ifndef FORK_FUNCTION 187 #define FORK_FUNCTION vfork 188 #endif 189 190 #include "lst.h" 191 #include "make_malloc.h" 192 #include "str.h" 193 #include "hash.h" 194 #include "config.h" 195 #include "buf.h" 196 197 /* 198 * The typical flow of states is: 199 * 200 * The direct successful path: 201 * UNMADE -> BEINGMADE -> MADE. 202 * 203 * The direct error path: 204 * UNMADE -> BEINGMADE -> ERROR. 205 * 206 * The successful path when dependencies need to be made first: 207 * UNMADE -> DEFERRED -> REQUESTED -> BEINGMADE -> MADE. 208 * 209 * A node that has dependencies, and one of the dependencies cannot be made: 210 * UNMADE -> DEFERRED -> ABORTED. 211 * 212 * A node that turns out to be up-to-date: 213 * UNMADE -> BEINGMADE -> UPTODATE. 214 */ 215 typedef enum GNodeMade { 216 /* Not examined yet. */ 217 UNMADE, 218 /* 219 * The node has been examined but is not yet ready since its 220 * dependencies have to be made first. 221 */ 222 DEFERRED, 223 224 /* The node is on the toBeMade list. */ 225 REQUESTED, 226 227 /* 228 * The node is already being made. Trying to build a node in this 229 * state indicates a cycle in the graph. 230 */ 231 BEINGMADE, 232 233 /* Was out-of-date and has been made. */ 234 MADE, 235 /* Was already up-to-date, does not need to be made. */ 236 UPTODATE, 237 /* 238 * An error occurred while it was being made. Used only in compat 239 * mode. 240 */ 241 ERROR, 242 /* 243 * The target was aborted due to an error making a dependency. Used 244 * only in compat mode. 245 */ 246 ABORTED 247 } GNodeMade; 248 249 /* 250 * The OP_ constants are used when parsing a dependency line as a way of 251 * communicating to other parts of the program the way in which a target 252 * should be made. 253 * 254 * Some of the OP_ constants can be combined, others cannot. 255 * 256 * See the tests depsrc-*.mk and deptgt-*.mk. 257 */ 258 typedef enum GNodeType { 259 OP_NONE = 0, 260 261 /* 262 * The dependency operator ':' is the most common one. The commands 263 * of this node are executed if any child is out-of-date. 264 */ 265 OP_DEPENDS = 1 << 0, 266 /* 267 * The dependency operator '!' always executes its commands, even if 268 * its children are up-to-date. 269 */ 270 OP_FORCE = 1 << 1, 271 /* 272 * The dependency operator '::' behaves like ':', except that it 273 * allows multiple dependency groups to be defined. Each of these 274 * groups is executed on its own, independently from the others. Each 275 * individual dependency group is called a cohort. 276 */ 277 OP_DOUBLEDEP = 1 << 2, 278 279 /* Matches the dependency operators ':', '!' and '::'. */ 280 OP_OPMASK = OP_DEPENDS | OP_FORCE | OP_DOUBLEDEP, 281 282 /* Don't care if the target doesn't exist and can't be created. */ 283 OP_OPTIONAL = 1 << 3, 284 /* Use associated commands for parents. */ 285 OP_USE = 1 << 4, 286 /* 287 * Target is never out of date, but always execute commands anyway. 288 * Its time doesn't matter, so it has none...sort of. 289 */ 290 OP_EXEC = 1 << 5, 291 /* 292 * Ignore non-zero exit status from shell commands when creating the 293 * node. 294 */ 295 OP_IGNORE = 1 << 6, 296 /* Don't remove the target when interrupted. */ 297 OP_PRECIOUS = 1 << 7, 298 /* Don't echo commands when executed. */ 299 OP_SILENT = 1 << 8, 300 /* 301 * Target is a recursive make so its commands should always be 302 * executed when it is out of date, regardless of the state of the -n 303 * or -t flags. 304 */ 305 OP_MAKE = 1 << 9, 306 /* 307 * Target is out-of-date only if any of its children was out-of-date. 308 */ 309 OP_JOIN = 1 << 10, 310 /* Assume the children of the node have been already made. */ 311 OP_MADE = 1 << 11, 312 /* Special .BEGIN, .END or .INTERRUPT. */ 313 OP_SPECIAL = 1 << 12, 314 /* Like .USE, only prepend commands. */ 315 OP_USEBEFORE = 1 << 13, 316 /* 317 * The node is invisible to its parents. I.e. it doesn't show up in 318 * the parents' local variables (.IMPSRC, .ALLSRC). 319 */ 320 OP_INVISIBLE = 1 << 14, 321 /* 322 * The node does not become the main target, even if it is the first 323 * target in the first makefile. 324 */ 325 OP_NOTMAIN = 1 << 15, 326 /* Not a file target; run always. */ 327 OP_PHONY = 1 << 16, 328 /* Don't search for the file in the path. */ 329 OP_NOPATH = 1 << 17, 330 /* 331 * In a dependency line "target: source1 .WAIT source2", source1 is 332 * made first, including its children. Once that is finished, 333 * source2 is made, including its children. The .WAIT keyword may 334 * appear more than once in a single dependency declaration. 335 */ 336 OP_WAIT = 1 << 18, 337 /* .NOMETA do not create a .meta file */ 338 OP_NOMETA = 1 << 19, 339 /* .META we _do_ want a .meta file */ 340 OP_META = 1 << 20, 341 /* Do not compare commands in .meta file */ 342 OP_NOMETA_CMP = 1 << 21, 343 /* Possibly a submake node */ 344 OP_SUBMAKE = 1 << 22, 345 346 /* Attributes applied by PMake */ 347 348 /* The node is a transformation rule, such as ".c.o". */ 349 OP_TRANSFORM = 1 << 30, 350 /* Target is a member of an archive */ 351 /* XXX: How does this differ from OP_ARCHV? */ 352 OP_MEMBER = 1 << 29, 353 /* 354 * The node is a library, its name has the form "-l<libname>". 355 */ 356 OP_LIB = 1 << 28, 357 /* 358 * The node is an archive member, its name has the form 359 * "archive(member)". 360 */ 361 /* XXX: How does this differ from OP_MEMBER? */ 362 OP_ARCHV = 1 << 27, 363 /* 364 * Target has all the commands it should. Used when parsing to catch 365 * multiple command groups for a target. Only applies to the 366 * dependency operators ':' and '!', but not to '::'. 367 */ 368 OP_HAS_COMMANDS = 1 << 26, 369 /* 370 * The special command "..." has been seen. All further commands from 371 * this node will be saved on the .END node instead, to be executed 372 * at the very end. 373 */ 374 OP_SAVE_CMDS = 1 << 25, 375 /* 376 * Already processed by Suff_FindDeps, to find dependencies from 377 * suffix transformation rules. 378 */ 379 OP_DEPS_FOUND = 1 << 24, 380 /* Node found while expanding .ALLSRC */ 381 OP_MARK = 1 << 23 382 } GNodeType; 383 384 typedef struct GNodeFlags { 385 /* this target needs to be (re)made */ 386 bool remake:1; 387 /* children of this target were made */ 388 bool childMade:1; 389 /* children don't exist, and we pretend made */ 390 bool force:1; 391 /* Set by Make_ProcessWait() */ 392 bool doneWait:1; 393 /* Build requested by .ORDER processing */ 394 bool doneOrder:1; 395 /* Node created from .depend */ 396 bool fromDepend:1; 397 /* We do it once only */ 398 bool doneAllsrc:1; 399 /* Have we checked for submake? */ 400 bool doneSubmake:1; 401 /* Used by MakePrintStatus */ 402 bool cycle:1; 403 /* Used by MakePrintStatus */ 404 bool doneCycle:1; 405 } GNodeFlags; 406 407 typedef struct List StringList; 408 typedef struct ListNode StringListNode; 409 410 typedef struct List GNodeList; 411 typedef struct ListNode GNodeListNode; 412 413 typedef struct SearchPath { 414 List /* of CachedDir */ dirs; 415 } SearchPath; 416 417 /* 418 * A graph node represents a target that can possibly be made, including its 419 * relation to other targets. 420 */ 421 typedef struct GNode { 422 /* The target's name, such as "clean" or "make.c" */ 423 char *name; 424 /* The unexpanded name of a .USE node */ 425 char *uname; 426 /* 427 * The full pathname of the file belonging to the target. 428 * 429 * XXX: What about .PHONY targets? These don't have an associated 430 * path. 431 */ 432 char *path; 433 434 /* 435 * The type of operator used to define the sources (see the OP flags 436 * below). 437 * 438 * XXX: This looks like a wild mixture of type and flags. 439 */ 440 GNodeType type; 441 GNodeFlags flags; 442 443 /* The state of processing on this node */ 444 GNodeMade made; 445 /* The number of unmade children */ 446 int unmade; 447 448 /* 449 * The modification time; 0 means the node does not have a 450 * corresponding file; see GNode_IsOODate. 451 */ 452 time_t mtime; 453 struct GNode *youngestChild; 454 455 /* 456 * The GNodes for which this node is an implied source. May be empty. 457 * For example, when there is an inference rule for .c.o, the node 458 * for file.c has the node for file.o in this list. 459 */ 460 GNodeList implicitParents; 461 462 /* 463 * The nodes that depend on this one, or in other words, the nodes 464 * for which this is a source. 465 */ 466 GNodeList parents; 467 /* The nodes on which this one depends. */ 468 GNodeList children; 469 470 /* 471 * .ORDER nodes we need made. The nodes that must be made (if they're 472 * made) before this node can be made, but that do not enter into the 473 * datedness of this node. 474 */ 475 GNodeList order_pred; 476 /* 477 * .ORDER nodes who need us. The nodes that must be made (if they're 478 * made at all) after this node is made, but that do not depend on 479 * this node, in the normal sense. 480 */ 481 GNodeList order_succ; 482 483 /* 484 * Other nodes of the same name, for targets that were defined using 485 * the '::' dependency operator (OP_DOUBLEDEP). 486 */ 487 GNodeList cohorts; 488 /* The "#n" suffix for this cohort, or "" for other nodes */ 489 char cohort_num[8]; 490 /* The number of unmade instances on the cohorts list */ 491 int unmade_cohorts; 492 /* 493 * Pointer to the first instance of a '::' node; only set when on a 494 * cohorts list 495 */ 496 struct GNode *centurion; 497 498 /* Last time (sequence number) we tried to make this node */ 499 unsigned checked_seqno; 500 501 /* 502 * The "local" variables that are specific to this target and this 503 * target only, such as $@, $<, $?. 504 * 505 * Also used for the global variable scopes SCOPE_GLOBAL, 506 * SCOPE_CMDLINE, SCOPE_INTERNAL, which contain variables with 507 * arbitrary names. 508 */ 509 HashTable /* of Var pointer */ vars; 510 511 /* The commands to be given to a shell to create this target. */ 512 StringList commands; 513 514 /* 515 * Suffix for the node (determined by Suff_FindDeps and opaque to 516 * everyone but the Suff module) 517 */ 518 struct Suffix *suffix; 519 520 /* Filename where the GNode got defined, unlimited lifetime */ 521 const char *fname; 522 /* Line number where the GNode got defined, 1-based */ 523 unsigned lineno; 524 int exit_status; 525 } GNode; 526 527 /* 528 * Keep track of whether to include <posix.mk> when parsing the line 529 * '.POSIX:'. 530 */ 531 extern enum PosixState { 532 PS_NOT_YET, 533 PS_MAYBE_NEXT_LINE, 534 PS_NOW_OR_NEVER, 535 PS_SET, 536 PS_TOO_LATE 537 } posix_state; 538 539 /* Error levels for diagnostics during parsing. */ 540 typedef enum ParseErrorLevel { 541 /* 542 * Exit when the current top-level makefile has been parsed 543 * completely. 544 */ 545 PARSE_FATAL = 1, 546 /* Print "warning"; may be upgraded to fatal by the -w option. */ 547 PARSE_WARNING, 548 /* Informational, mainly used during development of makefiles. */ 549 PARSE_INFO 550 } ParseErrorLevel; 551 552 /* 553 * Values returned by Cond_EvalLine and Cond_EvalCondition. 554 */ 555 typedef enum CondResult { 556 CR_TRUE, /* Parse the next lines */ 557 CR_FALSE, /* Skip the next lines */ 558 CR_ERROR /* Unknown directive or parse error */ 559 } CondResult; 560 561 typedef struct { 562 enum GuardKind { 563 GK_VARIABLE, 564 GK_TARGET 565 } kind; 566 char *name; 567 } Guard; 568 569 /* Names of the variables that are "local" to a specific target. */ 570 #define TARGET "@" /* Target of dependency */ 571 #define OODATE "?" /* All out-of-date sources */ 572 #define ALLSRC ">" /* All sources */ 573 #define IMPSRC "<" /* Source implied by transformation */ 574 #define PREFIX "*" /* Common prefix */ 575 #define ARCHIVE "!" /* Archive in "archive(member)" syntax */ 576 #define MEMBER "%" /* Member in "archive(member)" syntax */ 577 578 /* 579 * Global Variables 580 */ 581 582 /* True if every target is precious */ 583 extern bool allPrecious; 584 /* True if failed targets should be deleted */ 585 extern bool deleteOnError; 586 /* true while processing .depend */ 587 extern bool doing_depend; 588 /* .DEFAULT rule */ 589 extern GNode *defaultNode; 590 591 /* 592 * Variables defined internally by make which should not override those set 593 * by makefiles. 594 */ 595 extern GNode *SCOPE_INTERNAL; 596 /* Variables defined in a global scope, e.g in the makefile itself. */ 597 extern GNode *SCOPE_GLOBAL; 598 /* Variables defined on the command line. */ 599 extern GNode *SCOPE_CMDLINE; 600 601 /* 602 * Value returned by Var_Parse when an error is encountered. It points to an 603 * empty string, so naive callers needn't worry about it. 604 */ 605 extern char var_Error[]; 606 607 /* The time at the start of this whole process */ 608 extern time_t now; 609 610 /* 611 * The list of directories to search when looking for targets (set by the 612 * special target .PATH). 613 */ 614 extern SearchPath dirSearchPath; 615 /* Used for .include "...". */ 616 extern SearchPath *parseIncPath; 617 /* 618 * Used for .include <...>, for the built-in sys.mk and for makefiles from 619 * the command line arguments. 620 */ 621 extern SearchPath *sysIncPath; 622 /* The default for sysIncPath. */ 623 extern SearchPath *defSysIncPath; 624 625 /* Startup directory */ 626 extern char curdir[]; 627 /* The basename of the program name, suffixed with [n] for sub-makes. */ 628 extern const char *progname; 629 extern int makelevel; 630 /* Name of the .depend makefile */ 631 extern char *makeDependfile; 632 /* If we replaced environ, this will be non-NULL. */ 633 extern char **savedEnv; 634 extern GNode *mainNode; 635 636 extern pid_t myPid; 637 638 #define MAKEFLAGS ".MAKEFLAGS" 639 #ifndef MAKE_LEVEL_ENV 640 # define MAKE_LEVEL_ENV "MAKELEVEL" 641 #endif 642 643 typedef struct DebugFlags { 644 bool DEBUG_ARCH:1; 645 bool DEBUG_COND:1; 646 bool DEBUG_CWD:1; 647 bool DEBUG_DIR:1; 648 bool DEBUG_ERROR:1; 649 bool DEBUG_FOR:1; 650 bool DEBUG_GRAPH1:1; 651 bool DEBUG_GRAPH2:1; 652 bool DEBUG_GRAPH3:1; 653 bool DEBUG_HASH:1; 654 bool DEBUG_JOB:1; 655 bool DEBUG_LOUD:1; 656 bool DEBUG_MAKE:1; 657 bool DEBUG_META:1; 658 bool DEBUG_PARSE:1; 659 bool DEBUG_SCRIPT:1; 660 bool DEBUG_SHELL:1; 661 bool DEBUG_SUFF:1; 662 bool DEBUG_TARG:1; 663 bool DEBUG_VAR:1; 664 } DebugFlags; 665 666 #define CONCAT(a, b) a##b 667 668 #define DEBUG(module) (opts.debug.CONCAT(DEBUG_, module)) 669 670 void debug_printf(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2); 671 672 #define DEBUG_IMPL(module, args) \ 673 do { \ 674 if (DEBUG(module)) \ 675 debug_printf args; \ 676 } while (false) 677 678 #define DEBUG0(module, fmt) \ 679 DEBUG_IMPL(module, (fmt)) 680 #define DEBUG1(module, fmt, arg1) \ 681 DEBUG_IMPL(module, (fmt, arg1)) 682 #define DEBUG2(module, fmt, arg1, arg2) \ 683 DEBUG_IMPL(module, (fmt, arg1, arg2)) 684 #define DEBUG3(module, fmt, arg1, arg2, arg3) \ 685 DEBUG_IMPL(module, (fmt, arg1, arg2, arg3)) 686 #define DEBUG4(module, fmt, arg1, arg2, arg3, arg4) \ 687 DEBUG_IMPL(module, (fmt, arg1, arg2, arg3, arg4)) 688 #define DEBUG5(module, fmt, arg1, arg2, arg3, arg4, arg5) \ 689 DEBUG_IMPL(module, (fmt, arg1, arg2, arg3, arg4, arg5)) 690 691 typedef enum PrintVarsMode { 692 PVM_NONE, 693 PVM_UNEXPANDED, 694 PVM_EXPANDED 695 } PrintVarsMode; 696 697 /* Command line options */ 698 typedef struct CmdOpts { 699 /* -B: whether to be compatible to traditional make */ 700 bool compatMake; 701 702 /* 703 * -d: debug control: There is one flag per module. It is up to the 704 * module what debug information to print. 705 */ 706 DebugFlags debug; 707 708 /* -dF: debug output is written here - default stderr */ 709 FILE *debug_file; 710 711 /* 712 * -dL: lint mode 713 * 714 * Runs make in strict mode, with additional checks and better error 715 * handling. 716 */ 717 bool strict; 718 719 /* -dV: for the -V option, print unexpanded variable values */ 720 bool debugVflag; 721 722 /* -e: check environment variables before global variables */ 723 bool checkEnvFirst; 724 725 /* -f: the makefiles to read */ 726 StringList makefiles; 727 728 /* -i: if true, ignore all errors from shell commands */ 729 bool ignoreErrors; 730 731 /* 732 * -j: the maximum number of jobs that can run in parallel; this is 733 * coordinated with the submakes 734 */ 735 int maxJobs; 736 737 /* 738 * -k: if true and an error occurs while making a node, continue 739 * making nodes that do not depend on the erroneous node 740 */ 741 bool keepgoing; 742 743 /* -N: execute no commands from the targets */ 744 bool noRecursiveExecute; 745 746 /* -n: execute almost no commands from the targets */ 747 bool noExecute; 748 749 /* 750 * -q: if true, do not really make anything, just see if the targets 751 * are out-of-date 752 */ 753 bool query; 754 755 /* -r: raw mode, do not load the builtin rules. */ 756 bool noBuiltins; 757 758 /* -s: don't echo the shell commands before executing them */ 759 bool silent; 760 761 /* 762 * -t: touch the targets if they are out-of-date, but don't actually 763 * make them 764 */ 765 bool touch; 766 767 /* -[Vv]: print expanded or unexpanded selected variables */ 768 PrintVarsMode printVars; 769 /* -[Vv]: the variables to print */ 770 StringList variables; 771 772 /* -W: if true, makefile parsing warnings are treated as errors */ 773 bool parseWarnFatal; 774 775 /* -w: print 'Entering' and 'Leaving' for submakes */ 776 bool enterFlag; 777 778 /* 779 * -X: if true, do not export variables set on the command line to 780 * the environment. 781 */ 782 bool varNoExportEnv; 783 784 /* 785 * The target names specified on the command line. Used to resolve 786 * .if make(...) statements. 787 */ 788 StringList create; 789 790 /* 791 * Randomize the order in which the targets from toBeMade are made, 792 * to catch undeclared dependencies. 793 */ 794 bool randomizeTargets; 795 } CmdOpts; 796 797 extern CmdOpts opts; 798 extern bool forceJobs; 799 extern char **environ; 800 801 /* arch.c */ 802 void Arch_Init(void); 803 #ifdef CLEANUP 804 void Arch_End(void); 805 #endif 806 807 bool Arch_ParseArchive(char **, GNodeList *, GNode *); 808 void Arch_Touch(GNode *); 809 void Arch_TouchLib(GNode *); 810 void Arch_UpdateMTime(GNode *); 811 void Arch_UpdateMemberMTime(GNode *); 812 void Arch_FindLib(GNode *, SearchPath *); 813 bool Arch_LibOODate(GNode *) MAKE_ATTR_USE; 814 bool Arch_IsLib(GNode *) MAKE_ATTR_USE; 815 816 /* compat.c */ 817 bool Compat_RunCommand(const char *, GNode *, StringListNode *); 818 void Compat_MakeAll(GNodeList *); 819 void Compat_Make(GNode *, GNode *); 820 821 /* cond.c */ 822 extern unsigned cond_depth; 823 CondResult Cond_EvalCondition(const char *) MAKE_ATTR_USE; 824 CondResult Cond_EvalLine(const char *) MAKE_ATTR_USE; 825 Guard *Cond_ExtractGuard(const char *) MAKE_ATTR_USE; 826 void Cond_EndFile(void); 827 828 /* dir.c; see also dir.h */ 829 830 MAKE_INLINE const char * MAKE_ATTR_USE 831 str_basename(const char *pathname) 832 { 833 const char *lastSlash = strrchr(pathname, '/'); 834 return lastSlash != NULL ? lastSlash + 1 : pathname; 835 } 836 837 MAKE_INLINE SearchPath * MAKE_ATTR_USE 838 SearchPath_New(void) 839 { 840 SearchPath *path = bmake_malloc(sizeof *path); 841 Lst_Init(&path->dirs); 842 return path; 843 } 844 845 void SearchPath_Free(SearchPath *); 846 847 /* for.c */ 848 struct ForLoop; 849 int For_Eval(const char *) MAKE_ATTR_USE; 850 bool For_Accum(const char *, int *) MAKE_ATTR_USE; 851 void For_Run(unsigned, unsigned); 852 bool For_NextIteration(struct ForLoop *, Buffer *); 853 char *ForLoop_Details(const struct ForLoop *); 854 void ForLoop_Free(struct ForLoop *); 855 void For_Break(struct ForLoop *); 856 857 /* job.c */ 858 void JobReapChild(pid_t, int, bool); 859 860 /* longer than this we use a temp file */ 861 #ifndef MAKE_CMDLEN_LIMIT 862 # define MAKE_CMDLEN_LIMIT 1000 863 #endif 864 /* main.c */ 865 void Main_ParseArgLine(const char *); 866 void Cmd_Argv(const char *, size_t, const char *[5], char *, size_t, 867 bool, bool); 868 char *Cmd_Exec(const char *, char **) MAKE_ATTR_USE; 869 void Var_ExportStackTrace(const char *, const char *); 870 void Error(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2); 871 void Fatal(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2) MAKE_ATTR_DEAD; 872 void Punt(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2) MAKE_ATTR_DEAD; 873 void DieHorribly(void) MAKE_ATTR_DEAD; 874 int unlink_file(const char *) MAKE_ATTR_USE; 875 void execDie(const char *, const char *); 876 char *getTmpdir(void) MAKE_ATTR_USE; 877 bool ParseBoolean(const char *, bool) MAKE_ATTR_USE; 878 const char *cached_realpath(const char *, char *); 879 bool GetBooleanExpr(const char *, bool); 880 881 /* parse.c */ 882 extern int parseErrors; 883 void Parse_Init(void); 884 #ifdef CLEANUP 885 void Parse_End(void); 886 #endif 887 888 void PrintLocation(FILE *, bool, const GNode *); 889 const char *GetParentStackTrace(void); 890 char *GetStackTrace(bool); 891 void PrintStackTrace(FILE *, bool); 892 void Parse_Error(ParseErrorLevel, const char *, ...) MAKE_ATTR_PRINTFLIKE(2, 3); 893 bool Parse_VarAssign(const char *, bool, GNode *) MAKE_ATTR_USE; 894 void Parse_File(const char *, int); 895 void Parse_PushInput(const char *, unsigned, unsigned, Buffer, 896 struct ForLoop *); 897 void Parse_MainName(GNodeList *); 898 unsigned CurFile_CondMinDepth(void) MAKE_ATTR_USE; 899 void Parse_GuardElse(void); 900 void Parse_GuardEndif(void); 901 902 903 /* suff.c */ 904 void Suff_Init(void); 905 #ifdef CLEANUP 906 void Suff_End(void); 907 #endif 908 909 void Suff_ClearSuffixes(void); 910 bool Suff_IsTransform(const char *) MAKE_ATTR_USE; 911 GNode *Suff_AddTransform(const char *); 912 void Suff_EndTransform(GNode *); 913 void Suff_AddSuffix(const char *); 914 SearchPath *Suff_GetPath(const char *) MAKE_ATTR_USE; 915 void Suff_ExtendPaths(void); 916 void Suff_AddInclude(const char *); 917 void Suff_AddLib(const char *); 918 void Suff_FindDeps(GNode *); 919 SearchPath *Suff_FindPath(GNode *) MAKE_ATTR_USE; 920 void Suff_SetNull(const char *); 921 void Suff_PrintAll(void); 922 char *Suff_NamesStr(void) MAKE_ATTR_USE; 923 924 /* targ.c */ 925 void Targ_Init(void); 926 void Targ_End(void); 927 928 void Targ_Stats(void); 929 GNodeList *Targ_List(void) MAKE_ATTR_USE; 930 GNode *GNode_New(const char *) MAKE_ATTR_USE; 931 GNode *Targ_FindNode(const char *) MAKE_ATTR_USE; 932 GNode *Targ_GetNode(const char *) MAKE_ATTR_USE; 933 GNode *Targ_NewInternalNode(const char *) MAKE_ATTR_USE; 934 GNode *Targ_GetEndNode(void); 935 void Targ_FindList(GNodeList *, StringList *); 936 void Targ_PrintCmds(GNode *); 937 void Targ_PrintNode(GNode *, int); 938 void Targ_PrintNodes(GNodeList *, int); 939 const char *Targ_FmtTime(time_t) MAKE_ATTR_USE; 940 void Targ_PrintType(GNodeType); 941 void Targ_PrintGraph(int); 942 void Targ_Propagate(void); 943 const char *GNodeMade_Name(GNodeMade) MAKE_ATTR_USE; 944 #ifdef CLEANUP 945 void Parse_RegisterCommand(char *); 946 #else 947 MAKE_INLINE 948 void Parse_RegisterCommand(char *cmd MAKE_ATTR_UNUSED) 949 { 950 } 951 #endif 952 953 /* var.c */ 954 955 typedef enum VarEvalMode { 956 957 /* 958 * Only parse the expression but don't evaluate any part of it. 959 * 960 * TODO: Document what Var_Parse and Var_Subst return in this mode. 961 * As of 2021-03-15, they return unspecified, inconsistent results. 962 */ 963 VARE_PARSE, 964 965 /* 966 * Parse text in which '${...}' and '$(...)' are not parsed as 967 * subexpressions (with all their individual escaping rules) but 968 * instead simply as text with balanced '${}' or '$()'. Other '$' 969 * are copied verbatim. 970 */ 971 VARE_PARSE_BALANCED, 972 973 /* Parse and evaluate the expression. */ 974 VARE_EVAL, 975 976 /* 977 * Only for Var_Parse, not for Var_Subst or Var_Expand: Parse and 978 * evaluate the expression. It is an error if the expression 979 * evaluates to undefined. Subexpressions or indirect expressions 980 * may evaluate to undefined, though. 981 */ 982 VARE_EVAL_DEFINED_LOUD, 983 984 /* 985 * Parse and evaluate the expression. It is a silent error if a 986 * top-level expression evaluates to undefined. 987 */ 988 VARE_EVAL_DEFINED, 989 990 /* 991 * Parse and evaluate the expression. Keep undefined variables as-is 992 * instead of expanding them to an empty string. 993 * 994 * Example for a ':=' assignment: 995 * CFLAGS = $(.INCLUDES) 996 * CFLAGS := -I.. $(CFLAGS) 997 * # If .INCLUDES (an undocumented special variable, by the 998 * # way) is still undefined, the updated CFLAGS becomes 999 * # "-I.. $(.INCLUDES)". 1000 */ 1001 VARE_EVAL_KEEP_UNDEFINED, 1002 1003 /* 1004 * Parse and evaluate the expression. Keep '$$' as '$$' and preserve 1005 * undefined subexpressions. 1006 */ 1007 VARE_EVAL_KEEP_DOLLAR_AND_UNDEFINED 1008 } VarEvalMode; 1009 1010 typedef enum VarSetFlags { 1011 VAR_SET_NONE = 0, 1012 1013 /* do not export */ 1014 VAR_SET_NO_EXPORT = 1 << 0, 1015 1016 /* 1017 * Make the variable read-only. No further modification is possible, 1018 * except for another call to Var_Set with the same flag. See the 1019 * special targets '.NOREADONLY' and '.READONLY'. 1020 */ 1021 VAR_SET_READONLY = 1 << 1, 1022 VAR_SET_INTERNAL = 1 << 2 1023 } VarSetFlags; 1024 1025 typedef enum VarExportMode { 1026 /* .export-all */ 1027 VEM_ALL, 1028 /* .export-env */ 1029 VEM_ENV, 1030 /* .export: Initial export or update an already exported variable. */ 1031 VEM_PLAIN, 1032 /* .export-literal: Do not expand the variable value. */ 1033 VEM_LITERAL 1034 } VarExportMode; 1035 1036 void Var_Delete(GNode *, const char *); 1037 #ifdef CLEANUP 1038 void Var_DeleteAll(GNode *scope); 1039 #endif 1040 void Var_Undef(const char *); 1041 void Var_Set(GNode *, const char *, const char *); 1042 void Var_SetExpand(GNode *, const char *, const char *); 1043 void Var_SetWithFlags(GNode *, const char *, const char *, VarSetFlags); 1044 void Var_Append(GNode *, const char *, const char *); 1045 void Var_AppendExpand(GNode *, const char *, const char *); 1046 bool Var_Exists(GNode *, const char *) MAKE_ATTR_USE; 1047 bool Var_ExistsExpand(GNode *, const char *) MAKE_ATTR_USE; 1048 FStr Var_Value(GNode *, const char *) MAKE_ATTR_USE; 1049 const char *GNode_ValueDirect(GNode *, const char *) MAKE_ATTR_USE; 1050 FStr Var_Parse(const char **, GNode *, VarEvalMode); 1051 char *Var_Subst(const char *, GNode *, VarEvalMode); 1052 char *Var_SubstInTarget(const char *, GNode *); 1053 void Var_Expand(FStr *, GNode *, VarEvalMode); 1054 void Var_Stats(void); 1055 void Var_Dump(GNode *); 1056 void Var_ReexportVars(GNode *); 1057 void Var_Export(VarExportMode, const char *); 1058 void Var_ExportVars(const char *); 1059 void Var_UnExport(bool, const char *); 1060 void Var_ReadOnly(const char *, bool); 1061 1062 void Global_Set(const char *, const char *); 1063 void Global_Append(const char *, const char *); 1064 void Global_Delete(const char *); 1065 void Global_Set_ReadOnly(const char *, const char *); 1066 1067 void EvalStack_PushMakeflags(const char *); 1068 void EvalStack_Pop(void); 1069 bool EvalStack_Details(Buffer *buf) MAKE_ATTR_USE; 1070 1071 /* util.c */ 1072 typedef void (*SignalProc)(int); 1073 SignalProc bmake_signal(int, SignalProc); 1074 1075 /* make.c */ 1076 void GNode_UpdateYoungestChild(GNode *, GNode *); 1077 bool GNode_IsOODate(GNode *) MAKE_ATTR_USE; 1078 void Make_ExpandUse(GNodeList *); 1079 time_t Make_Recheck(GNode *) MAKE_ATTR_USE; 1080 void Make_HandleUse(GNode *, GNode *); 1081 void Make_Update(GNode *); 1082 void GNode_SetLocalVars(GNode *); 1083 bool Make_MakeParallel(GNodeList *); 1084 bool shouldDieQuietly(GNode *, int) MAKE_ATTR_USE; 1085 void PrintOnError(GNode *, const char *); 1086 void Main_ExportMAKEFLAGS(bool); 1087 bool Main_SetObjdir(bool, const char *, ...) MAKE_ATTR_PRINTFLIKE(2, 3); 1088 int mkTempFile(const char *, char *, size_t) MAKE_ATTR_USE; 1089 void AppendWords(StringList *, char *); 1090 void GNode_FprintDetails(FILE *, const char *, const GNode *, const char *); 1091 bool GNode_ShouldExecute(GNode *gn) MAKE_ATTR_USE; 1092 char *GNodeType_ToString(GNodeType); 1093 1094 /* See if the node was seen on the left-hand side of a dependency operator. */ 1095 MAKE_INLINE bool MAKE_ATTR_USE 1096 GNode_IsTarget(const GNode *gn) 1097 { 1098 return (gn->type & OP_OPMASK) != OP_NONE; 1099 } 1100 1101 MAKE_INLINE const char * MAKE_ATTR_USE 1102 GNode_Path(const GNode *gn) 1103 { 1104 return gn->path != NULL ? gn->path : gn->name; 1105 } 1106 1107 MAKE_INLINE bool MAKE_ATTR_USE 1108 GNode_IsWaitingFor(const GNode *gn) 1109 { 1110 return gn->flags.remake && gn->made <= REQUESTED; 1111 } 1112 1113 MAKE_INLINE bool MAKE_ATTR_USE 1114 GNode_IsReady(const GNode *gn) 1115 { 1116 return gn->made > DEFERRED; 1117 } 1118 1119 MAKE_INLINE bool MAKE_ATTR_USE 1120 GNode_IsDone(const GNode *gn) 1121 { 1122 return gn->made >= MADE; 1123 } 1124 1125 MAKE_INLINE bool MAKE_ATTR_USE 1126 GNode_IsError(const GNode *gn) 1127 { 1128 return gn->made == ERROR || gn->made == ABORTED; 1129 } 1130 1131 MAKE_INLINE bool MAKE_ATTR_USE 1132 GNode_IsMainCandidate(const GNode *gn) 1133 { 1134 return (gn->type & (OP_NOTMAIN | OP_USE | OP_USEBEFORE | 1135 OP_EXEC | OP_TRANSFORM)) == 0; 1136 } 1137 1138 /* Return whether the target file should be preserved on interrupt. */ 1139 MAKE_INLINE bool MAKE_ATTR_USE 1140 GNode_IsPrecious(const GNode *gn) 1141 { 1142 /* XXX: Why are '::' targets precious? */ 1143 return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP); 1144 } 1145 1146 MAKE_INLINE const char * MAKE_ATTR_USE 1147 GNode_VarTarget(GNode *gn) { return GNode_ValueDirect(gn, TARGET); } 1148 MAKE_INLINE const char * MAKE_ATTR_USE 1149 GNode_VarOodate(GNode *gn) { return GNode_ValueDirect(gn, OODATE); } 1150 MAKE_INLINE const char * MAKE_ATTR_USE 1151 GNode_VarAllsrc(GNode *gn) { return GNode_ValueDirect(gn, ALLSRC); } 1152 MAKE_INLINE const char * MAKE_ATTR_USE 1153 GNode_VarImpsrc(GNode *gn) { return GNode_ValueDirect(gn, IMPSRC); } 1154 MAKE_INLINE const char * MAKE_ATTR_USE 1155 GNode_VarPrefix(GNode *gn) { return GNode_ValueDirect(gn, PREFIX); } 1156 MAKE_INLINE const char * MAKE_ATTR_USE 1157 GNode_VarArchive(GNode *gn) { return GNode_ValueDirect(gn, ARCHIVE); } 1158 MAKE_INLINE const char * MAKE_ATTR_USE 1159 GNode_VarMember(GNode *gn) { return GNode_ValueDirect(gn, MEMBER); } 1160 1161 MAKE_INLINE void * MAKE_ATTR_USE 1162 UNCONST(const void *ptr) 1163 { 1164 void *ret; 1165 memcpy(&ret, &ptr, sizeof(ret)); 1166 return ret; 1167 } 1168 1169 /* At least GNU/Hurd systems lack hardcoded MAXPATHLEN/PATH_MAX */ 1170 #include <limits.h> 1171 #ifndef MAXPATHLEN 1172 #define MAXPATHLEN 4096 1173 #endif 1174 #ifndef PATH_MAX 1175 #define PATH_MAX MAXPATHLEN 1176 #endif 1177 1178 #if defined(SYSV) 1179 #define KILLPG(pid, sig) kill(-(pid), (sig)) 1180 #else 1181 #define KILLPG(pid, sig) killpg((pid), (sig)) 1182 #endif 1183 1184 MAKE_INLINE bool MAKE_ATTR_USE 1185 ch_isalnum(char ch) { return isalnum((unsigned char)ch) != 0; } 1186 MAKE_INLINE bool MAKE_ATTR_USE 1187 ch_isalpha(char ch) { return isalpha((unsigned char)ch) != 0; } 1188 MAKE_INLINE bool MAKE_ATTR_USE 1189 ch_isdigit(char ch) { return isdigit((unsigned char)ch) != 0; } 1190 MAKE_INLINE bool MAKE_ATTR_USE 1191 ch_islower(char ch) { return islower((unsigned char)ch) != 0; } 1192 MAKE_INLINE bool MAKE_ATTR_USE 1193 ch_isprint(char ch) { return isprint((unsigned char)ch) != 0; } 1194 MAKE_INLINE bool MAKE_ATTR_USE 1195 ch_isspace(char ch) { return isspace((unsigned char)ch) != 0; } 1196 MAKE_INLINE bool MAKE_ATTR_USE 1197 ch_isupper(char ch) { return isupper((unsigned char)ch) != 0; } 1198 MAKE_INLINE char MAKE_ATTR_USE 1199 ch_tolower(char ch) { return (char)tolower((unsigned char)ch); } 1200 MAKE_INLINE char MAKE_ATTR_USE 1201 ch_toupper(char ch) { return (char)toupper((unsigned char)ch); } 1202 1203 MAKE_INLINE void 1204 cpp_skip_whitespace(const char **pp) 1205 { 1206 while (ch_isspace(**pp)) 1207 (*pp)++; 1208 } 1209 1210 MAKE_INLINE void 1211 cpp_skip_hspace(const char **pp) 1212 { 1213 while (**pp == ' ' || **pp == '\t') 1214 (*pp)++; 1215 } 1216 1217 MAKE_INLINE bool 1218 cpp_skip_string(const char **pp, const char *s) 1219 { 1220 const char *p = *pp; 1221 while (*p == *s && *s != '\0') 1222 p++, s++; 1223 if (*s == '\0') 1224 *pp = p; 1225 return *s == '\0'; 1226 } 1227 1228 MAKE_INLINE void 1229 pp_skip_whitespace(char **pp) 1230 { 1231 while (ch_isspace(**pp)) 1232 (*pp)++; 1233 } 1234 1235 MAKE_INLINE void 1236 pp_skip_hspace(char **pp) 1237 { 1238 while (**pp == ' ' || **pp == '\t') 1239 (*pp)++; 1240 } 1241 1242 #if defined(lint) 1243 void do_not_define_rcsid(void); /* for lint */ 1244 # define MAKE_RCSID(id) void do_not_define_rcsid(void) 1245 #elif defined(MAKE_NATIVE) 1246 # include <sys/cdefs.h> 1247 # define MAKE_RCSID(id) __RCSID(id) 1248 #elif defined(MAKE_ALL_IN_ONE) && defined(__COUNTER__) 1249 # define MAKE_RCSID_CONCAT(x, y) CONCAT(x, y) 1250 # define MAKE_RCSID(id) static volatile char \ 1251 MAKE_RCSID_CONCAT(rcsid_, __COUNTER__)[] = id 1252 #elif defined(MAKE_ALL_IN_ONE) 1253 # define MAKE_RCSID(id) void do_not_define_rcsid(void) 1254 #else 1255 # define MAKE_RCSID(id) static volatile char rcsid[] = id 1256 #endif 1257 1258 #endif 1259