make.h revision 1.127 1 /* $NetBSD: make.h,v 1.127 2020/08/26 23:00:47 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 pmake
78 */
79
80 #ifndef MAKE_MAKE_H
81 #define MAKE_MAKE_H
82
83 #include <sys/types.h>
84 #include <sys/param.h>
85
86 #include <assert.h>
87 #include <ctype.h>
88 #include <fcntl.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <unistd.h>
93
94 #ifdef BSD4_4
95 # include <sys/cdefs.h>
96 #endif
97
98 #ifndef FD_CLOEXEC
99 #define FD_CLOEXEC 1
100 #endif
101
102 #if defined(__GNUC__)
103 #define MAKE_GNUC_PREREQ(x, y) \
104 ((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) || \
105 (__GNUC__ > (x)))
106 #else /* defined(__GNUC__) */
107 #define MAKE_GNUC_PREREQ(x, y) 0
108 #endif /* defined(__GNUC__) */
109
110 #if MAKE_GNUC_PREREQ(2, 7)
111 #define MAKE_ATTR_UNUSED __attribute__((__unused__))
112 #else
113 #define MAKE_ATTR_UNUSED /* delete */
114 #endif
115
116 #if MAKE_GNUC_PREREQ(2, 5)
117 #define MAKE_ATTR_DEAD __attribute__((__noreturn__))
118 #elif defined(__GNUC__)
119 #define MAKE_ATTR_DEAD __volatile
120 #else
121 #define MAKE_ATTR_DEAD /* delete */
122 #endif
123
124 #if MAKE_GNUC_PREREQ(2, 7)
125 #define MAKE_ATTR_PRINTFLIKE(fmtarg, firstvararg) \
126 __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
127 #else
128 #define MAKE_ATTR_PRINTFLIKE(fmtarg, firstvararg) /* delete */
129 #endif
130
131 /*
132 * A boolean type is defined as an integer, not an enum. This allows a
133 * boolean argument to be an expression that isn't strictly 0 or 1 valued.
134 */
135
136 typedef int Boolean;
137 #ifndef TRUE
138 #define TRUE 1
139 #endif /* TRUE */
140 #ifndef FALSE
141 #define FALSE 0
142 #endif /* FALSE */
143
144 /*
145 * Functions that must return a status can return a ReturnStatus to
146 * indicate success or type of failure.
147 */
148
149 typedef int ReturnStatus;
150
151 /*
152 * The following statuses overlap with the first 2 generic statuses
153 * defined in status.h:
154 *
155 * SUCCESS There was no error.
156 * FAILURE There was a general error.
157 */
158
159 #define SUCCESS 0x00000000
160 #define FAILURE 0x00000001
161
162 #include "lst.h"
163 #include "hash.h"
164 #include "config.h"
165 #include "buf.h"
166 #include "make_malloc.h"
167
168 typedef enum {
169 UNMADE, /* Not examined yet */
170 DEFERRED, /* Examined once (building child) */
171 REQUESTED, /* on toBeMade list */
172 BEINGMADE, /* Target is already being made.
173 * Indicates a cycle in the graph. */
174 MADE, /* Was out-of-date and has been made */
175 UPTODATE, /* Was already up-to-date */
176 ERROR, /* An error occurred while it was being
177 * made (used only in compat mode) */
178 ABORTED /* The target was aborted due to an error
179 * making an inferior (compat). */
180 } GNodeMade;
181
182 /* The OP_ constants are used when parsing a dependency line as a way of
183 * communicating to other parts of the program the way in which a target
184 * should be made.
185 *
186 * These constants are bitwise-OR'ed together and placed in the 'type' field
187 * of each node. Any node that has a 'type' field which satisfies the OP_NOP
188 * function was never never on the left-hand side of an operator, though it
189 * may have been on the right-hand side... */
190 typedef enum {
191 /* Execution of commands depends on children (:) */
192 OP_DEPENDS = 1 << 0,
193 /* Always execute commands (!) */
194 OP_FORCE = 1 << 1,
195 /* Execution of commands depends on children per line (::) */
196 OP_DOUBLEDEP = 1 << 2,
197
198 OP_OPMASK = OP_DEPENDS|OP_FORCE|OP_DOUBLEDEP,
199
200 /* Don't care if the target doesn't exist and can't be created */
201 OP_OPTIONAL = 1 << 3,
202 /* Use associated commands for parents */
203 OP_USE = 1 << 4,
204 /* Target is never out of date, but always execute commands anyway.
205 * Its time doesn't matter, so it has none...sort of */
206 OP_EXEC = 1 << 5,
207 /* Ignore errors when creating the node */
208 OP_IGNORE = 1 << 6,
209 /* Don't remove the target when interrupted */
210 OP_PRECIOUS = 1 << 7,
211 /* Don't echo commands when executed */
212 OP_SILENT = 1 << 8,
213 /* Target is a recursive make so its commands should always be executed
214 * when it is out of date, regardless of the state of the -n or -t flags */
215 OP_MAKE = 1 << 9,
216 /* Target is out-of-date only if any of its children was out-of-date */
217 OP_JOIN = 1 << 10,
218 /* Assume the children of the node have been already made */
219 OP_MADE = 1 << 11,
220 /* Special .BEGIN, .END, .INTERRUPT */
221 OP_SPECIAL = 1 << 12,
222 /* Like .USE, only prepend commands */
223 OP_USEBEFORE = 1 << 13,
224 /* The node is invisible to its parents. I.e. it doesn't show up in the
225 * parents' local variables. */
226 OP_INVISIBLE = 1 << 14,
227 /* The node is exempt from normal 'main target' processing in parse.c */
228 OP_NOTMAIN = 1 << 15,
229 /* Not a file target; run always */
230 OP_PHONY = 1 << 16,
231 /* Don't search for file in the path */
232 OP_NOPATH = 1 << 17,
233 /* .WAIT phony node */
234 OP_WAIT = 1 << 18,
235 /* .NOMETA do not create a .meta file */
236 OP_NOMETA = 1 << 19,
237 /* .META we _do_ want a .meta file */
238 OP_META = 1 << 20,
239 /* Do not compare commands in .meta file */
240 OP_NOMETA_CMP = 1 << 21,
241 /* Possibly a submake node */
242 OP_SUBMAKE = 1 << 22,
243
244 /* Attributes applied by PMake */
245
246 /* The node is a transformation rule */
247 OP_TRANSFORM = 1 << 31,
248 /* Target is a member of an archive */
249 OP_MEMBER = 1 << 30,
250 /* Target is a library */
251 OP_LIB = 1 << 29,
252 /* Target is an archive construct */
253 OP_ARCHV = 1 << 28,
254 /* Target has all the commands it should. Used when parsing to catch
255 * multiple commands for a target. */
256 OP_HAS_COMMANDS = 1 << 27,
257 /* Saving commands on .END (Compat) */
258 OP_SAVE_CMDS = 1 << 26,
259 /* Already processed by Suff_FindDeps */
260 OP_DEPS_FOUND = 1 << 25,
261 /* Node found while expanding .ALLSRC */
262 OP_MARK = 1 << 24
263 } GNodeType;
264
265 typedef enum {
266 REMAKE = 0x0001, /* this target needs to be (re)made */
267 CHILDMADE = 0x0002, /* children of this target were made */
268 FORCE = 0x0004, /* children don't exist, and we pretend made */
269 DONE_WAIT = 0x0008, /* Set by Make_ProcessWait() */
270 DONE_ORDER = 0x0010, /* Build requested by .ORDER processing */
271 FROM_DEPEND = 0x0020, /* Node created from .depend */
272 DONE_ALLSRC = 0x0040, /* We do it once only */
273 CYCLE = 0x1000, /* Used by MakePrintStatus */
274 DONECYCLE = 0x2000, /* Used by MakePrintStatus */
275 INTERNAL = 0x4000 /* Internal use only */
276 } GNodeFlags;
277
278 /* A graph node represents a target that can possibly be made, including its
279 * relation to other targets and a lot of other details. */
280 typedef struct GNode {
281 /* The target's name, such as "clean" or "make.c" */
282 char *name;
283 /* The unexpanded name of a .USE node */
284 char *uname;
285 /* The full pathname of the file belonging to the target.
286 * XXX: What about .PHONY targets? These don't have an associated path. */
287 char *path;
288
289 /* The type of operator used to define the sources (see the OP flags below).
290 * XXX: This looks like a wild mixture of type and flags. */
291 GNodeType type;
292 /* whether it is involved in this invocation of make */
293 GNodeFlags flags;
294
295 /* The state of processing on this node */
296 GNodeMade made;
297 int unmade; /* The number of unmade children */
298
299 time_t mtime; /* Its modification time */
300 struct GNode *cmgn; /* The youngest child */
301
302 /* Links to parents for which this is an implied source. May be empty.
303 * Nodes that depend on this, as gleaned from the transformation rules. */
304 Lst iParents;
305
306 /* Other nodes of the same name for the :: operator. */
307 Lst cohorts;
308
309 /* The nodes that depend on this one, or in other words, the nodes for
310 * which this is a source. */
311 Lst parents;
312 /* The nodes on which this one depends. */
313 Lst children;
314
315 /* .ORDER nodes we need made. The nodes that must be made (if they're
316 * made) before this node can be made, but that do not enter into the
317 * datedness of this node. */
318 Lst order_pred;
319 /* .ORDER nodes who need us. The nodes that must be made (if they're made
320 * at all) after this node is made, but that do not depend on this node,
321 * in the normal sense. */
322 Lst order_succ;
323
324 /* #n for this cohort */
325 char cohort_num[8];
326 /* The number of unmade instances on the cohorts list */
327 int unmade_cohorts;
328 /* Pointer to the first instance of a :: node; only set when on a cohorts
329 * list */
330 struct GNode *centurion;
331
332 /* Last time (sequence number) we tried to make this node */
333 unsigned int checked;
334
335 /* The "local" variables that are specific to this target and this target
336 * only, such as $@, $<, $?. */
337 Hash_Table context;
338
339 /* The commands to be given to a shell to create this target. */
340 Lst commands;
341
342 /* Suffix for the node (determined by Suff_FindDeps and opaque to everyone
343 * but the Suff module) */
344 struct Suff *suffix;
345
346 /* filename where the GNode got defined */
347 const char *fname;
348 /* line number where the GNode got defined */
349 int lineno;
350 } GNode;
351
352 #define NoExecute(gn) ((gn->type & OP_MAKE) ? noRecursiveExecute : noExecute)
353 /*
354 * OP_NOP will return TRUE if the node with the given type was not the
355 * object of a dependency operator
356 */
357 #define OP_NOP(t) (((t) & OP_OPMASK) == 0x00000000)
358
359 #define OP_NOTARGET (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)
360
361 /*
362 * The TARG_ constants are used when calling the Targ_FindNode and
363 * Targ_FindList functions in targ.c. They simply tell the functions what to
364 * do if the desired node(s) is (are) not found. If the TARG_CREATE constant
365 * is given, a new, empty node will be created for the target, placed in the
366 * table of all targets and its address returned. If TARG_NOCREATE is given,
367 * a NULL pointer will be returned.
368 */
369 #define TARG_NOCREATE 0x00 /* don't create it */
370 #define TARG_CREATE 0x01 /* create node if not found */
371 #define TARG_NOHASH 0x02 /* don't look in/add to hash table */
372
373 /*
374 * Error levels for parsing. PARSE_FATAL means the process cannot continue
375 * once the makefile has been parsed. PARSE_WARNING means it can. Passed
376 * as the first argument to Parse_Error.
377 */
378 #define PARSE_INFO 3
379 #define PARSE_WARNING 2
380 #define PARSE_FATAL 1
381
382 /*
383 * Values returned by Cond_Eval.
384 */
385 typedef enum {
386 COND_PARSE, /* Parse the next lines */
387 COND_SKIP, /* Skip the next lines */
388 COND_INVALID /* Not a conditional statement */
389 } CondEvalResult;
390
391 /*
392 * Definitions for the "local" variables. Used only for clarity.
393 */
394 #define TARGET "@" /* Target of dependency */
395 #define OODATE "?" /* All out-of-date sources */
396 #define ALLSRC ">" /* All sources */
397 #define IMPSRC "<" /* Source implied by transformation */
398 #define PREFIX "*" /* Common prefix */
399 #define ARCHIVE "!" /* Archive in "archive(member)" syntax */
400 #define MEMBER "%" /* Member in "archive(member)" syntax */
401
402 #define FTARGET "@F" /* file part of TARGET */
403 #define DTARGET "@D" /* directory part of TARGET */
404 #define FIMPSRC "<F" /* file part of IMPSRC */
405 #define DIMPSRC "<D" /* directory part of IMPSRC */
406 #define FPREFIX "*F" /* file part of PREFIX */
407 #define DPREFIX "*D" /* directory part of PREFIX */
408
409 /*
410 * Global Variables
411 */
412 extern Lst create; /* The list of target names specified on the
413 * command line. used to resolve #if
414 * make(...) statements */
415 extern Lst dirSearchPath; /* The list of directories to search when
416 * looking for targets */
417
418 extern Boolean compatMake; /* True if we are make compatible */
419 extern Boolean ignoreErrors; /* True if should ignore all errors */
420 extern Boolean beSilent; /* True if should print no commands */
421 extern Boolean noExecute; /* True if should execute nothing */
422 extern Boolean noRecursiveExecute; /* True if should execute nothing */
423 extern Boolean allPrecious; /* True if every target is precious */
424 extern Boolean deleteOnError; /* True if failed targets should be deleted */
425 extern Boolean keepgoing; /* True if should continue on unaffected
426 * portions of the graph when have an error
427 * in one portion */
428 extern Boolean touchFlag; /* TRUE if targets should just be 'touched'
429 * if out of date. Set by the -t flag */
430 extern Boolean queryFlag; /* TRUE if we aren't supposed to really make
431 * anything, just see if the targets are out-
432 * of-date */
433 extern Boolean doing_depend; /* TRUE if processing .depend */
434
435 extern Boolean checkEnvFirst; /* TRUE if environment should be searched for
436 * variables before the global context */
437
438 extern Boolean parseWarnFatal; /* TRUE if makefile parsing warnings are
439 * treated as errors */
440
441 extern Boolean varNoExportEnv; /* TRUE if we should not export variables
442 * set on the command line to the env. */
443
444 extern GNode *DEFAULT; /* .DEFAULT rule */
445
446 extern GNode *VAR_INTERNAL; /* Variables defined internally by make
447 * which should not override those set by
448 * makefiles.
449 */
450 extern GNode *VAR_GLOBAL; /* Variables defined in a global context, e.g
451 * in the Makefile itself */
452 extern GNode *VAR_CMD; /* Variables defined on the command line */
453 extern char var_Error[]; /* Value returned by Var_Parse when an error
454 * is encountered. It actually points to
455 * an empty string, so naive callers needn't
456 * worry about it. */
457
458 extern time_t now; /* The time at the start of this whole
459 * process */
460
461 extern Boolean oldVars; /* Do old-style variable substitution */
462
463 extern Lst sysIncPath; /* The system include path. */
464 extern Lst defIncPath; /* The default include path. */
465
466 extern char curdir[]; /* Startup directory */
467 extern char *progname; /* The program name */
468 extern char *makeDependfile; /* .depend */
469 extern char **savedEnv; /* if we replaced environ this will be non-NULL */
470
471 extern int makelevel;
472
473 /*
474 * We cannot vfork() in a child of vfork().
475 * Most systems do not enforce this but some do.
476 */
477 #define vFork() ((getpid() == myPid) ? vfork() : fork())
478 extern pid_t myPid;
479
480 #define MAKEFLAGS ".MAKEFLAGS"
481 #define MAKEOVERRIDES ".MAKEOVERRIDES"
482 #define MAKE_JOB_PREFIX ".MAKE.JOB.PREFIX" /* prefix for job target output */
483 #define MAKE_EXPORTED ".MAKE.EXPORTED" /* variables we export */
484 #define MAKE_MAKEFILES ".MAKE.MAKEFILES" /* all the makefiles we read */
485 #define MAKE_LEVEL ".MAKE.LEVEL" /* recursion level */
486 #define MAKEFILE_PREFERENCE ".MAKE.MAKEFILE_PREFERENCE"
487 #define MAKE_DEPENDFILE ".MAKE.DEPENDFILE" /* .depend */
488 #define MAKE_MODE ".MAKE.MODE"
489 #ifndef MAKE_LEVEL_ENV
490 # define MAKE_LEVEL_ENV "MAKELEVEL"
491 #endif
492
493 /*
494 * debug control:
495 * There is one bit per module. It is up to the module what debug
496 * information to print.
497 */
498 extern FILE *debug_file; /* Output is written here - default stderr */
499 extern int debug;
500 #define DEBUG_ARCH 0x00001
501 #define DEBUG_COND 0x00002
502 #define DEBUG_DIR 0x00004
503 #define DEBUG_GRAPH1 0x00008
504 #define DEBUG_GRAPH2 0x00010
505 #define DEBUG_JOB 0x00020
506 #define DEBUG_MAKE 0x00040
507 #define DEBUG_SUFF 0x00080
508 #define DEBUG_TARG 0x00100
509 #define DEBUG_VAR 0x00200
510 #define DEBUG_FOR 0x00400
511 #define DEBUG_SHELL 0x00800
512 #define DEBUG_ERROR 0x01000
513 #define DEBUG_LOUD 0x02000
514 #define DEBUG_META 0x04000
515 #define DEBUG_HASH 0x08000
516
517 #define DEBUG_GRAPH3 0x10000
518 #define DEBUG_SCRIPT 0x20000
519 #define DEBUG_PARSE 0x40000
520 #define DEBUG_CWD 0x80000
521
522 #define DEBUG_LINT 0x100000
523
524 #define CONCAT(a,b) a##b
525
526 #define DEBUG(module) (debug & CONCAT(DEBUG_,module))
527
528 #include "nonints.h"
529
530 int Make_TimeStamp(GNode *, GNode *);
531 Boolean Make_OODate(GNode *);
532 void Make_ExpandUse(Lst);
533 time_t Make_Recheck(GNode *);
534 void Make_HandleUse(GNode *, GNode *);
535 void Make_Update(GNode *);
536 void Make_DoAllVar(GNode *);
537 Boolean Make_Run(Lst);
538 int dieQuietly(GNode *, int);
539 void PrintOnError(GNode *, const char *);
540 void Main_ExportMAKEFLAGS(Boolean);
541 Boolean Main_SetObjdir(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2);
542 int mkTempFile(const char *, char **);
543 int str2Lst_Append(Lst, char *, const char *);
544 int cached_lstat(const char *, void *);
545 int cached_stat(const char *, void *);
546 void GNode_FprintDetails(FILE *, const char *, const GNode *, const char *);
547
548 #ifdef __GNUC__
549 #define UNCONST(ptr) ({ \
550 union __unconst { \
551 const void *__cp; \
552 void *__p; \
553 } __d; \
554 __d.__cp = ptr, __d.__p; })
555 #else
556 #define UNCONST(ptr) (void *)(ptr)
557 #endif
558
559 #ifndef MIN
560 #define MIN(a, b) ((a < b) ? a : b)
561 #endif
562 #ifndef MAX
563 #define MAX(a, b) ((a > b) ? a : b)
564 #endif
565
566 /* At least GNU/Hurd systems lack hardcoded MAXPATHLEN/PATH_MAX */
567 #include <limits.h>
568 #ifndef MAXPATHLEN
569 #define MAXPATHLEN 4096
570 #endif
571 #ifndef PATH_MAX
572 #define PATH_MAX MAXPATHLEN
573 #endif
574
575 #if defined(SYSV)
576 #define KILLPG(pid, sig) kill(-(pid), (sig))
577 #else
578 #define KILLPG(pid, sig) killpg((pid), (sig))
579 #endif
580
581 #endif /* MAKE_MAKE_H */
582