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