xref: /netbsd-src/usr.bin/make/make.h (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: make.h,v 1.59 2006/03/10 15:53:55 christos 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_H_
81 #define _MAKE_H_
82 
83 #include <sys/types.h>
84 #include <sys/param.h>
85 
86 #include <ctype.h>
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <unistd.h>
91 
92 #ifdef BSD4_4
93 # include <sys/cdefs.h>
94 #endif
95 
96 #if !defined(__GNUC_PREREQ__)
97 #if defined(__GNUC__)
98 #define	__GNUC_PREREQ__(x, y)						\
99 	((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) ||			\
100 	 (__GNUC__ > (x)))
101 #else /* defined(__GNUC__) */
102 #define	__GNUC_PREREQ__(x, y)	0
103 #endif /* defined(__GNUC__) */
104 #endif /* !defined(__GNUC_PREREQ__) */
105 
106 #if !defined(__unused)
107 #if __GNUC_PREREQ__(2, 7)
108 #define __unused        __attribute__((__unused__))
109 #else
110 #define __unused        /* delete */
111 #endif
112 #endif
113 
114 #include "sprite.h"
115 #include "lst.h"
116 #include "hash.h"
117 #include "config.h"
118 #include "buf.h"
119 
120 /*-
121  * The structure for an individual graph node. Each node has several
122  * pieces of data associated with it.
123  *	1) the name of the target it describes
124  *	2) the location of the target file in the file system.
125  *	3) the type of operator used to define its sources (qv. parse.c)
126  *	4) whether it is involved in this invocation of make
127  *	5) whether the target has been remade
128  *	6) whether any of its children has been remade
129  *	7) the number of its children that are, as yet, unmade
130  *	8) its modification time
131  *	9) the modification time of its youngest child (qv. make.c)
132  *	10) a list of nodes for which this is a source (parents)
133  *	11) a list of nodes on which this depends (children)
134  *	12) a list of nodes that depend on this, as gleaned from the
135  *	    transformation rules (iParents)
136  *	13) a list of ancestor nodes, which includes parents, iParents,
137  *	    and recursive parents of parents
138  *	14) a list of nodes of the same name created by the :: operator
139  *	15) a list of nodes that must be made (if they're made) before
140  *	    this node can be, but that do not enter into the datedness of
141  *	    this node.
142  *	16) a list of nodes that must be made (if they're made) before
143  *	    this node or any child of this node can be, but that do not
144  *	    enter into the datedness of this node.
145  *	17) a list of nodes that must be made (if they're made) after
146  *	    this node is, but that do not depend on this node, in the
147  *	    normal sense.
148  *	18) a Lst of ``local'' variables that are specific to this target
149  *	   and this target only (qv. var.c [$@ $< $?, etc.])
150  *	19) a Lst of strings that are commands to be given to a shell
151  *	   to create this target.
152  */
153 typedef struct GNode {
154     char            *name;     	/* The target's name */
155     char            *uname;    	/* The unexpanded name of a .USE node */
156     char    	    *path;     	/* The full pathname of the file */
157     int             type;      	/* Its type (see the OP flags, below) */
158     int		    order;	/* Its wait weight */
159 
160     int             flags;
161 #define REMAKE		0x1    	/* this target needs to be remade */
162 #define	CHILDMADE	0x2	/* children of this target were made */
163 #define FORCE		0x4	/* children don't exist, and we pretend made */
164     enum {
165 	UNMADE, BEINGMADE, MADE, UPTODATE, ERROR, ABORTED,
166 	CYCLE, ENDCYCLE
167     }	    	    made;    	/* Set to reflect the state of processing
168 				 * on this node:
169 				 *  UNMADE - Not examined yet
170 				 *  BEINGMADE - Target is already being made.
171 				 *  	Indicates a cycle in the graph. (compat
172 				 *  	mode only)
173 				 *  MADE - Was out-of-date and has been made
174 				 *  UPTODATE - Was already up-to-date
175 				 *  ERROR - An error occurred while it was being
176 				 *  	made (used only in compat mode)
177 				 *  ABORTED - The target was aborted due to
178 				 *  	an error making an inferior (compat).
179 				 *  CYCLE - Marked as potentially being part of
180 				 *  	a graph cycle. If we come back to a
181 				 *  	node marked this way, it is printed
182 				 *  	and 'made' is changed to ENDCYCLE.
183 				 *  ENDCYCLE - the cycle has been completely
184 				 *  	printed. Go back and unmark all its
185 				 *  	members.
186 				 */
187     int             unmade;    	/* The number of unmade children */
188 
189     time_t          mtime;     	/* Its modification time */
190     time_t     	    cmtime;    	/* The modification time of its youngest
191 				 * child */
192 
193     Lst     	    iParents;  	/* Links to parents for which this is an
194 				 * implied source, if any */
195     Lst	    	    cohorts;  	/* Other nodes for the :: operator */
196     Lst             parents;   	/* Nodes that depend on this one */
197     Lst             ancestors; 	/* Parents, parents of parents, ... */
198     Lst             children;  	/* Nodes on which this one depends */
199     Lst	    	    successors;	/* Nodes that must be made after this one */
200     Lst	    	    preds;  	/* Nodes that must be made before this one */
201     Lst	    	    recpreds;	/* Nodes that must be added to preds
202 				 * recursively for all child nodes */
203     int		    unmade_cohorts;/* # of unmade instances on the
204 				      cohorts list */
205     struct GNode    *centurion;	/* Pointer to the first instance of a ::
206 				   node; only set when on a cohorts list */
207 
208     Hash_Table      context;	/* The local variables */
209     Lst             commands;  	/* Creation commands */
210 
211     struct _Suff    *suffix;	/* Suffix for the node (determined by
212 				 * Suff_FindDeps and opaque to everyone
213 				 * but the Suff module) */
214     char	    *fname;	/* filename where the GNode got defined */
215     int		     lineno;	/* line number where the GNode got defined */
216 } GNode;
217 
218 /*
219  * Manifest constants
220  */
221 #define NILGNODE	((GNode *) NIL)
222 
223 /*
224  * The OP_ constants are used when parsing a dependency line as a way of
225  * communicating to other parts of the program the way in which a target
226  * should be made. These constants are bitwise-OR'ed together and
227  * placed in the 'type' field of each node. Any node that has
228  * a 'type' field which satisfies the OP_NOP function was never never on
229  * the lefthand side of an operator, though it may have been on the
230  * righthand side...
231  */
232 #define OP_DEPENDS	0x00000001  /* Execution of commands depends on
233 				     * kids (:) */
234 #define OP_FORCE	0x00000002  /* Always execute commands (!) */
235 #define OP_DOUBLEDEP	0x00000004  /* Execution of commands depends on kids
236 				     * per line (::) */
237 #define OP_OPMASK	(OP_DEPENDS|OP_FORCE|OP_DOUBLEDEP)
238 
239 #define OP_OPTIONAL	0x00000008  /* Don't care if the target doesn't
240 				     * exist and can't be created */
241 #define OP_USE		0x00000010  /* Use associated commands for parents */
242 #define OP_EXEC	  	0x00000020  /* Target is never out of date, but always
243 				     * execute commands anyway. Its time
244 				     * doesn't matter, so it has none...sort
245 				     * of */
246 #define OP_IGNORE	0x00000040  /* Ignore errors when creating the node */
247 #define OP_PRECIOUS	0x00000080  /* Don't remove the target when
248 				     * interrupted */
249 #define OP_SILENT	0x00000100  /* Don't echo commands when executed */
250 #define OP_MAKE		0x00000200  /* Target is a recursive make so its
251 				     * commands should always be executed when
252 				     * it is out of date, regardless of the
253 				     * state of the -n or -t flags */
254 #define OP_JOIN 	0x00000400  /* Target is out-of-date only if any of its
255 				     * children was out-of-date */
256 #define	OP_MADE		0x00000800  /* Assume the children of the node have
257 				     * been already made */
258 #define OP_SPECIAL	0x00001000  /* Special .BEGIN, .END, .INTERRUPT */
259 #define	OP_USEBEFORE	0x00002000  /* Like .USE, only prepend commands */
260 #define OP_INVISIBLE	0x00004000  /* The node is invisible to its parents.
261 				     * I.e. it doesn't show up in the parents's
262 				     * local variables. */
263 #define OP_NOTMAIN	0x00008000  /* The node is exempt from normal 'main
264 				     * target' processing in parse.c */
265 #define OP_PHONY	0x00010000  /* Not a file target; run always */
266 #define OP_NOPATH	0x00020000  /* Don't search for file in the path */
267 /* Attributes applied by PMake */
268 #define OP_TRANSFORM	0x80000000  /* The node is a transformation rule */
269 #define OP_MEMBER 	0x40000000  /* Target is a member of an archive */
270 #define OP_LIB	  	0x20000000  /* Target is a library */
271 #define OP_ARCHV  	0x10000000  /* Target is an archive construct */
272 #define OP_HAS_COMMANDS	0x08000000  /* Target has all the commands it should.
273 				     * Used when parsing to catch multiple
274 				     * commands for a target */
275 #define OP_SAVE_CMDS	0x04000000  /* Saving commands on .END (Compat) */
276 #define OP_DEPS_FOUND	0x02000000  /* Already processed by Suff_FindDeps */
277 #define	OP_MARK		0x01000000  /* Node found while expanding .ALLSRC */
278 
279 #define NoExecute(gn) ((gn->type & OP_MAKE) ? noRecursiveExecute : noExecute)
280 /*
281  * OP_NOP will return TRUE if the node with the given type was not the
282  * object of a dependency operator
283  */
284 #define OP_NOP(t)	(((t) & OP_OPMASK) == 0x00000000)
285 
286 #define OP_NOTARGET (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)
287 
288 /*
289  * The TARG_ constants are used when calling the Targ_FindNode and
290  * Targ_FindList functions in targ.c. They simply tell the functions what to
291  * do if the desired node(s) is (are) not found. If the TARG_CREATE constant
292  * is given, a new, empty node will be created for the target, placed in the
293  * table of all targets and its address returned. If TARG_NOCREATE is given,
294  * a NIL pointer will be returned.
295  */
296 #define TARG_CREATE	0x01	  /* create node if not found */
297 #define TARG_NOCREATE	0x00	  /* don't create it */
298 
299 /*
300  * There are several places where expandable buffers are used (parse.c and
301  * var.c). This constant is merely the starting point for those buffers. If
302  * lines tend to be much shorter than this, it would be best to reduce BSIZE.
303  * If longer, it should be increased. Reducing it will cause more copying to
304  * be done for longer lines, but will save space for shorter ones. In any
305  * case, it ought to be a power of two simply because most storage allocation
306  * schemes allocate in powers of two.
307  */
308 #define MAKE_BSIZE		256	/* starting size for expandable buffers */
309 
310 /*
311  * These constants are all used by the Str_Concat function to decide how the
312  * final string should look. If STR_ADDSPACE is given, a space will be
313  * placed between the two strings. If STR_ADDSLASH is given, a '/' will
314  * be used instead of a space. If neither is given, no intervening characters
315  * will be placed between the two strings in the final output. If the
316  * STR_DOFREE bit is set, the two input strings will be freed before
317  * Str_Concat returns.
318  */
319 #define STR_ADDSPACE	0x01	/* add a space when Str_Concat'ing */
320 #define STR_ADDSLASH	0x02	/* add a slash when Str_Concat'ing */
321 
322 /*
323  * Error levels for parsing. PARSE_FATAL means the process cannot continue
324  * once the makefile has been parsed. PARSE_WARNING means it can. Passed
325  * as the first argument to Parse_Error.
326  */
327 #define PARSE_WARNING	2
328 #define PARSE_FATAL	1
329 
330 /*
331  * Values returned by Cond_Eval.
332  */
333 #define COND_PARSE	0   	/* Parse the next lines */
334 #define COND_SKIP 	1   	/* Skip the next lines */
335 #define COND_INVALID	2   	/* Not a conditional statement */
336 
337 /*
338  * Definitions for the "local" variables. Used only for clarity.
339  */
340 #define TARGET	  	  "@" 	/* Target of dependency */
341 #define OODATE	  	  "?" 	/* All out-of-date sources */
342 #define ALLSRC	  	  ">" 	/* All sources */
343 #define IMPSRC	  	  "<" 	/* Source implied by transformation */
344 #define PREFIX	  	  "*" 	/* Common prefix */
345 #define ARCHIVE	  	  "!" 	/* Archive in "archive(member)" syntax */
346 #define MEMBER	  	  "%" 	/* Member in "archive(member)" syntax */
347 
348 #define FTARGET           "@F"  /* file part of TARGET */
349 #define DTARGET           "@D"  /* directory part of TARGET */
350 #define FIMPSRC           "<F"  /* file part of IMPSRC */
351 #define DIMPSRC           "<D"  /* directory part of IMPSRC */
352 #define FPREFIX           "*F"  /* file part of PREFIX */
353 #define DPREFIX           "*D"  /* directory part of PREFIX */
354 
355 /*
356  * Global Variables
357  */
358 extern Lst  	create;	    	/* The list of target names specified on the
359 				 * command line. used to resolve #if
360 				 * make(...) statements */
361 extern Lst     	dirSearchPath; 	/* The list of directories to search when
362 				 * looking for targets */
363 
364 extern Boolean	compatMake;	/* True if we are make compatible */
365 extern Boolean	ignoreErrors;  	/* True if should ignore all errors */
366 extern Boolean  beSilent;    	/* True if should print no commands */
367 extern Boolean  noExecute;    	/* True if should execute nothing */
368 extern Boolean  noRecursiveExecute;    	/* True if should execute nothing */
369 extern Boolean  allPrecious;   	/* True if every target is precious */
370 extern Boolean  keepgoing;    	/* True if should continue on unaffected
371 				 * portions of the graph when have an error
372 				 * in one portion */
373 extern Boolean 	touchFlag;    	/* TRUE if targets should just be 'touched'
374 				 * if out of date. Set by the -t flag */
375 extern Boolean  usePipes;    	/* TRUE if should capture the output of
376 				 * subshells by means of pipes. Otherwise it
377 				 * is routed to temporary files from which it
378 				 * is retrieved when the shell exits */
379 extern Boolean 	queryFlag;    	/* TRUE if we aren't supposed to really make
380 				 * anything, just see if the targets are out-
381 				 * of-date */
382 
383 extern Boolean	checkEnvFirst;	/* TRUE if environment should be searched for
384 				 * variables before the global context */
385 extern Boolean	jobServer;	/* a jobServer already exists */
386 
387 extern Boolean	parseWarnFatal;	/* TRUE if makefile parsing warnings are
388 				 * treated as errors */
389 
390 extern Boolean	varNoExportEnv;	/* TRUE if we should not export variables
391 				 * set on the command line to the env. */
392 
393 extern GNode    *DEFAULT;    	/* .DEFAULT rule */
394 
395 extern GNode    *VAR_GLOBAL;   	/* Variables defined in a global context, e.g
396 				 * in the Makefile itself */
397 extern GNode    *VAR_CMD;    	/* Variables defined on the command line */
398 extern GNode	*VAR_FOR;	/* Iteration variables */
399 extern char    	var_Error[];   	/* Value returned by Var_Parse when an error
400 				 * is encountered. It actually points to
401 				 * an empty string, so naive callers needn't
402 				 * worry about it. */
403 
404 extern time_t 	now;	    	/* The time at the start of this whole
405 				 * process */
406 
407 extern Boolean	oldVars;    	/* Do old-style variable substitution */
408 
409 extern Lst	sysIncPath;	/* The system include path. */
410 extern Lst	defIncPath;	/* The default include path. */
411 
412 extern char	*progname;	/* The program name */
413 
414 #define	MAKEFLAGS	".MAKEFLAGS"
415 #define	MAKEOVERRIDES	".MAKEOVERRIDES"
416 
417 /*
418  * debug control:
419  *	There is one bit per module.  It is up to the module what debug
420  *	information to print.
421  */
422 extern int debug;
423 #define	DEBUG_ARCH	0x0001
424 #define	DEBUG_COND	0x0002
425 #define	DEBUG_DIR	0x0004
426 #define	DEBUG_GRAPH1	0x0008
427 #define	DEBUG_GRAPH2	0x0010
428 #define	DEBUG_JOB	0x0020
429 #define	DEBUG_MAKE	0x0040
430 #define	DEBUG_SUFF	0x0080
431 #define	DEBUG_TARG	0x0100
432 #define	DEBUG_VAR	0x0200
433 #define DEBUG_FOR	0x0400
434 #define DEBUG_SHELL	0x0800
435 #define DEBUG_ERROR	0x1000
436 #define	DEBUG_GRAPH3	0x10000
437 #define DEBUG_SCRIPT	0x20000
438 #define DEBUG_PARSE	0x40000
439 
440 #define CONCAT(a,b)	a##b
441 
442 #define	DEBUG(module)	(debug & CONCAT(DEBUG_,module))
443 
444 /*
445  * Since there are so many, all functions that return non-integer values are
446  * extracted by means of a sed script or two and stuck in the file "nonints.h"
447  */
448 #include "nonints.h"
449 
450 int Make_TimeStamp(GNode *, GNode *);
451 Boolean Make_OODate(GNode *);
452 Lst Make_ExpandUse(Lst);
453 time_t Make_Recheck(GNode *);
454 void Make_HandleUse(GNode *, GNode *);
455 void Make_Update(GNode *);
456 void Make_DoAllVar(GNode *);
457 Boolean Make_Run(Lst);
458 char * Check_Cwd_Cmd(const char *);
459 void Check_Cwd(const char **);
460 void PrintOnError(const char *);
461 void Main_ExportMAKEFLAGS(Boolean);
462 Boolean Main_SetObjdir(const char *);
463 
464 #ifdef __GNUC__
465 #define UNCONST(ptr)	({ 		\
466     union __unconst {			\
467 	const void *__cp;		\
468 	void *__p;			\
469     } __d;				\
470     __d.__cp = ptr, __d.__p; })
471 #else
472 #define UNCONST(ptr)	(void *)(ptr)
473 #endif
474 
475 #ifndef MIN
476 #define MIN(a, b) ((a < b) ? a : b)
477 #endif
478 #ifndef MAX
479 #define MAX(a, b) ((a > b) ? a : b)
480 #endif
481 
482 #endif /* _MAKE_H_ */
483