xref: /openbsd-src/usr.bin/make/gnode.h (revision 18cc03285580818394ff95b469a55f2bc9634566)
1f7923656Sespie #ifndef GNODE_H
2f7923656Sespie #define GNODE_H
3*18cc0328Sespie /*	$OpenBSD: gnode.h,v 1.39 2020/01/26 12:41:21 espie Exp $ */
4f7923656Sespie 
5f7923656Sespie /*
6f7923656Sespie  * Copyright (c) 2001 Marc Espie.
7f7923656Sespie  *
8f7923656Sespie  * Redistribution and use in source and binary forms, with or without
9f7923656Sespie  * modification, are permitted provided that the following conditions
10f7923656Sespie  * are met:
11f7923656Sespie  * 1. Redistributions of source code must retain the above copyright
12f7923656Sespie  *    notice, this list of conditions and the following disclaimer.
13f7923656Sespie  * 2. Redistributions in binary form must reproduce the above copyright
14f7923656Sespie  *    notice, this list of conditions and the following disclaimer in the
15f7923656Sespie  *    documentation and/or other materials provided with the distribution.
16f7923656Sespie  *
17f7923656Sespie  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
18f7923656Sespie  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19f7923656Sespie  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20f7923656Sespie  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
21f7923656Sespie  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22f7923656Sespie  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23f7923656Sespie  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24f7923656Sespie  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25f7923656Sespie  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26f7923656Sespie  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27f7923656Sespie  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28f7923656Sespie  */
29f7923656Sespie 
301f89b472Sespie #include <sys/time.h>
31f7923656Sespie #ifndef LIST_TYPE
32f7923656Sespie #include "lst_t.h"
33f7923656Sespie #endif
34f4f0b16aSespie #ifndef LOCATION_TYPE
35f4f0b16aSespie #include "location.h"
36f4f0b16aSespie #endif
37f7923656Sespie #ifndef SYMTABLE_H
38f7923656Sespie #include "symtable.h"
39f7923656Sespie #endif
404c9de429Sespie #include <assert.h>
41f7923656Sespie 
42f7923656Sespie /*-
4391797de1Sespie  * The structure for an individual graph node. Each node has a lot of
4491797de1Sespie  * of data associated with it.
4591797de1Sespie  *	1) the *name*of the target it describes (at end because ohash)
4691797de1Sespie  *	2) the *path* to the target file
4791797de1Sespie  *	3) the *type* of operator used to define its sources
4891797de1Sespie  *		(cf parse.c, mostly : :: !  but...)
4991797de1Sespie  *	4) *must_make*: whether it is involved in this invocation of make
5091797de1Sespie  *	5) *built_status*: has the target been rebuilt/is up-to-date...
5191797de1Sespie  *	6) *child_rebuild*: at least one of its children has been rebuilt
5291797de1Sespie  *	7) *children_left*: number of children still to consider
5391797de1Sespie  *	8) *mtime*: node's modification time
5491797de1Sespie  *	9) *youngest*: youngest child (cf make.c)
5591797de1Sespie  *	10) *parents*: list of nodes for which this is a dependency
5691797de1Sespie  *	11) *children*: list of nodes on which this depends
5791797de1Sespie  *	12) *cohorts*: list of nodes of the same name created by the :: operator
5891797de1Sespie  *	13) *predecessors*: list of nodes, result of .ORDER:
59194f6eccSespie  *	    if considered for building, they should be built before this node.
6091797de1Sespie  *	14) *successors*: list of nodes, result of .ORDER:
61194f6eccSespie  *	    if considered for building, they should be built after this node.
6291797de1Sespie  *	15) *localvars*: ``local'' variables specific to this target
6391797de1Sespie  *	   and this target only (cf var.c [$@ $< $?, etc.])
6491797de1Sespie  *	16) *commands*: the actual LIST of strings to pass to the shell
65f7923656Sespie  *	   to create this target.
66f7923656Sespie  */
67f7923656Sespie 
681471c3c2Sespie /* constants for specials
6972e6f714Sespie  * Most of these values are only handled by parse.c.
7072e6f714Sespie  * In many cases, there is a corresponding OP_* flag
7172e6f714Sespie  */
725e4a0acaSespie #define SPECIAL_NONE		0U
7372e6f714Sespie #define SPECIAL_PATH		62U	/* handled by parse.c and suff.c */
74866fd206Sespie 
751bae8e1fSespie #define SPECIAL_EXEC		4U
761bae8e1fSespie #define SPECIAL_IGNORE		5U
77771253bdSespie #define SPECIAL_NOTHING 	6U	/* this is used for things we
78771253bdSespie 					 * recognize for compatibility but
79771253bdSespie 					 * don't do anything with... */
806c0d9dcdSespie #define SPECIAL_DEPRECATED	7U	/* this is an old keyword and it will
816c0d9dcdSespie 					 * trigger a fatal error. */
821bae8e1fSespie #define SPECIAL_INVISIBLE	8U
831bae8e1fSespie #define SPECIAL_JOIN		9U
841bae8e1fSespie #define SPECIAL_MADE		11U
851bae8e1fSespie #define SPECIAL_MAIN		12U
861bae8e1fSespie #define SPECIAL_MAKE		13U
871bae8e1fSespie #define SPECIAL_MFLAGS		14U
881bae8e1fSespie #define SPECIAL_NOTMAIN		15U
891bae8e1fSespie #define SPECIAL_NOTPARALLEL	16U
901bae8e1fSespie #define SPECIAL_OPTIONAL	18U
911bae8e1fSespie #define SPECIAL_ORDER		19U
921bae8e1fSespie #define SPECIAL_PARALLEL	20U
931bae8e1fSespie #define SPECIAL_PHONY		22U
941bae8e1fSespie #define SPECIAL_PRECIOUS	23U
951bae8e1fSespie #define SPECIAL_SILENT		25U
961bae8e1fSespie #define SPECIAL_SUFFIXES	27U
971bae8e1fSespie #define SPECIAL_USE		28U
981bae8e1fSespie #define SPECIAL_WAIT		29U
991bae8e1fSespie #define SPECIAL_NOPATH		30U
1001bae8e1fSespie #define SPECIAL_ERROR		31U
1011bae8e1fSespie #define SPECIAL_CHEAP		32U
1021bae8e1fSespie #define SPECIAL_EXPENSIVE	33U
1031bae8e1fSespie 
104866fd206Sespie struct GNode_ {
10591797de1Sespie     unsigned int type;		/* node type (see the OP flags, below) */
10691797de1Sespie     unsigned int special_op;	/* special op to apply (only used in parse.c) */
10791797de1Sespie     unsigned char special;	/* type of special node or SPECIAL_NONE */
1082e487f56Sespie     bool must_make;		/* true if this target needs building */
1092e487f56Sespie     bool child_rebuilt;		/* true if at least one child was rebuilt,
110194f6eccSespie     			 	 * thus triggering timestamps changes */
111194f6eccSespie 
112194f6eccSespie     char built_status;
113194f6eccSespie #define UNKNOWN		0	/* Not examined yet */
114194f6eccSespie #define BUILDING	1	/* In the process of building */
115194f6eccSespie #define REBUILT		2	/* Was out of date and got rebuilt */
116194f6eccSespie #define UPTODATE	3	/* Was already up-to-date */
117194f6eccSespie #define ERROR		4	/* Error occurred while building
118194f6eccSespie 				 * (used only in compat mode) */
119194f6eccSespie #define ABORTED		5	/* Was aborted due to an error
120194f6eccSespie 				 * making an inferior */
121194f6eccSespie #define NOSUCHNODE	6	/* error from run_gnode */
122194f6eccSespie #define HELDBACK	7	/* Another target in the same group is
12391797de1Sespie 				 * currently building, avoid race conditions
12491797de1Sespie 				 * Only used in the parallel engine make.c */
125194f6eccSespie 
12691797de1Sespie     char *path;		/* full pathname of the file */
12791797de1Sespie     int order;		/* wait weight (see .ORDER/predecessors/successors) */
128866fd206Sespie 
12991797de1Sespie     int children_left;	/* number of children left to build */
130f7923656Sespie 
131194f6eccSespie     struct timespec mtime;	/* Node's modification time */
132194f6eccSespie     GNode *youngest;		/* Node's youngest child */
133f7923656Sespie 
13491797de1Sespie     GNode *impliedsrc;	/* found by suff, to help with localvars */
135f7923656Sespie     LIST cohorts;	/* Other nodes for the :: operator */
136f7923656Sespie     LIST parents;	/* Nodes that depend on this one */
137f7923656Sespie     LIST children;	/* Nodes on which this one depends */
138194f6eccSespie     LIST predecessors;
139194f6eccSespie     LIST successors;
140f7923656Sespie 
14191797de1Sespie     SymTable localvars;
142f7923656Sespie     LIST commands;	/* Creation commands */
14329936c8aSespie     Suff *suffix;	/* Suffix for the node (determined by
144f7923656Sespie 			 * Suff_FindDeps and opaque to everyone
145f7923656Sespie 			 * but the Suff module) */
14691797de1Sespie     GNode *groupling;	/* target lists, for HELDBACK: do not build two
14791797de1Sespie     			 * at the same time */
14891797de1Sespie     GNode *watched;	/* the node currently building for HELDBACK */
14991797de1Sespie 
15091797de1Sespie 			/* stuff for target name equivalence: */
15191797de1Sespie     GNode *sibling;	/* equivalent targets (not complete yet) */
1526c2dbbdaSespie     char *basename;	/* pointer to name stripped of path */
15329936c8aSespie     GNode *next;
15491797de1Sespie 
15591797de1Sespie     bool in_cycle;	/* cycle detection */
156f7923656Sespie     char name[1];	/* The target's name */
157f7923656Sespie };
158f7923656Sespie 
159a6b963c8Sespie struct command
160a6b963c8Sespie {
161a6b963c8Sespie 	Location location;
162a6b963c8Sespie 	char string[1];
163a6b963c8Sespie };
164a6b963c8Sespie 
1656c2dbbdaSespie #define has_been_built(gn) \
166ed90eef3Sespie 	((gn)->built_status == REBUILT || (gn)->built_status == UPTODATE)
1676c2dbbdaSespie #define should_have_file(gn) \
1686c2dbbdaSespie 	((gn)->special == SPECIAL_NONE && \
1696c2dbbdaSespie 	((gn)->type & (OP_PHONY | OP_DUMMY)) == 0)
170f7923656Sespie /*
171f7923656Sespie  * The OP_ constants are used when parsing a dependency line as a way of
172f7923656Sespie  * communicating to other parts of the program the way in which a target
173f7923656Sespie  * should be made. These constants are bitwise-OR'ed together and
174f7923656Sespie  * placed in the 'type' field of each node. Any node that has
175f7923656Sespie  * a 'type' field which satisfies the OP_NOP function was never never on
176f7923656Sespie  * the lefthand side of an operator, though it may have been on the
177f7923656Sespie  * righthand side...
178f7923656Sespie  */
17985e3ecf0Sespie #define OP_ZERO		0x00000000  /* No dependency operator seen so far */
180f7923656Sespie #define OP_DEPENDS	0x00000001  /* Execution of commands depends on
181f7923656Sespie 				     * kids (:) */
182f7923656Sespie #define OP_FORCE	0x00000002  /* Always execute commands (!) */
183f7923656Sespie #define OP_DOUBLEDEP	0x00000004  /* Execution of commands depends on kids
184f7923656Sespie 				     * per line (::) */
18585e3ecf0Sespie #define OP_ERROR	0x00000007
186f7923656Sespie #define OP_OPMASK	(OP_DEPENDS|OP_FORCE|OP_DOUBLEDEP)
187f7923656Sespie 
188f7923656Sespie #define OP_OPTIONAL	0x00000008  /* Don't care if the target doesn't
189f7923656Sespie 				     * exist and can't be created */
190f7923656Sespie #define OP_USE		0x00000010  /* Use associated commands for parents */
191f7923656Sespie #define OP_IGNORE	0x00000040  /* Ignore errors when creating the node */
192f7923656Sespie #define OP_PRECIOUS	0x00000080  /* Don't remove the target when
193f7923656Sespie 				     * interrupted */
194f7923656Sespie #define OP_SILENT	0x00000100  /* Don't echo commands when executed */
195866fd206Sespie #define OP_MAKE 	0x00000200  /* Target is a recursive make so its
196f7923656Sespie 				     * commands should always be executed when
197f7923656Sespie 				     * it is out of date, regardless of the
198f7923656Sespie 				     * state of the -n or -t flags */
1996c2dbbdaSespie #define OP_INVISIBLE	0x00001000  /* The node is invisible to its parents.
200f7923656Sespie 				     * I.e. it doesn't show up in the parents's
201*18cc0328Sespie 				     * local variables. Used by :: for
202*18cc0328Sespie 				     * supplementary nodes (cohorts). */
2036c2dbbdaSespie #define OP_NOTMAIN	0x00002000  /* The node is exempt from normal 'main
204f7923656Sespie 				     * target' processing in parse.c */
2056c2dbbdaSespie #define OP_PHONY	0x00004000  /* Not a file target; run always */
2066c2dbbdaSespie #define OP_NOPATH	0x00008000  /* Don't search for file in the path */
2076c2dbbdaSespie #define OP_NODEFAULT	0x00010000  /* Special node that never needs */
208866fd206Sespie 				    /* DEFAULT commands applied */
2096c2dbbdaSespie #define OP_DUMMY	0x00020000  /* node was created by default, but it */
210866fd206Sespie 				    /* does not really exist. */
211f7923656Sespie /* Attributes applied by PMake */
2126c2dbbdaSespie #define OP_TRANSFORM	0x00040000  /* The node is a transformation rule */
2136c2dbbdaSespie #define OP_MEMBER	0x00080000  /* Target is a member of an archive */
21429936c8aSespie #define OP_DOUBLE	0x00100000  /* normal op with double commands */
2156c2dbbdaSespie #define OP_ARCHV	0x00200000  /* Target is an archive construct */
2166c2dbbdaSespie #define OP_HAS_COMMANDS 0x00400000  /* Target has all the commands it should.
217f7923656Sespie 				     * Used when parsing to catch multiple
218f7923656Sespie 				     * commands for a target */
2196c2dbbdaSespie #define OP_DEPS_FOUND	0x00800000  /* Already processed by Suff_FindDeps */
22032e144d7Sespie #define OP_RESOLVED	0x01000000  /* We looked harder already */
221dd41fd19Sespie #define OP_CHEAP	0x02000000  /* Assume job is not recursive */
222dd41fd19Sespie #define OP_EXPENSIVE	0x04000000  /* Recursive job, don't run in parallel */
223f7923656Sespie 
224f7923656Sespie /*
225f7923656Sespie  * OP_NOP will return true if the node with the given type was not the
226f7923656Sespie  * object of a dependency operator
227f7923656Sespie  */
22885e3ecf0Sespie #define OP_NOP(t)	(((t) & OP_OPMASK) == OP_ZERO)
229f7923656Sespie 
230*18cc0328Sespie #define OP_NOTARGET (OP_NOTMAIN|OP_USE|OP_TRANSFORM)
231f7923656Sespie 
232f7923656Sespie 
233f7923656Sespie #endif
234