1*6eef5f0cSAntonio Huete Jimenez /* $NetBSD: targ.c,v 1.178 2022/09/27 17:46:58 rillig Exp $ */
201e196c8SJohn Marino
301e196c8SJohn Marino /*
401e196c8SJohn Marino * Copyright (c) 1988, 1989, 1990, 1993
501e196c8SJohn Marino * The Regents of the University of California. All rights reserved.
601e196c8SJohn Marino *
701e196c8SJohn Marino * This code is derived from software contributed to Berkeley by
801e196c8SJohn Marino * Adam de Boor.
901e196c8SJohn Marino *
1001e196c8SJohn Marino * Redistribution and use in source and binary forms, with or without
1101e196c8SJohn Marino * modification, are permitted provided that the following conditions
1201e196c8SJohn Marino * are met:
1301e196c8SJohn Marino * 1. Redistributions of source code must retain the above copyright
1401e196c8SJohn Marino * notice, this list of conditions and the following disclaimer.
1501e196c8SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1601e196c8SJohn Marino * notice, this list of conditions and the following disclaimer in the
1701e196c8SJohn Marino * documentation and/or other materials provided with the distribution.
1801e196c8SJohn Marino * 3. Neither the name of the University nor the names of its contributors
1901e196c8SJohn Marino * may be used to endorse or promote products derived from this software
2001e196c8SJohn Marino * without specific prior written permission.
2101e196c8SJohn Marino *
2201e196c8SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2301e196c8SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2401e196c8SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2501e196c8SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2601e196c8SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2701e196c8SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2801e196c8SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2901e196c8SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3001e196c8SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3101e196c8SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3201e196c8SJohn Marino * SUCH DAMAGE.
3301e196c8SJohn Marino */
3401e196c8SJohn Marino
3501e196c8SJohn Marino /*
3601e196c8SJohn Marino * Copyright (c) 1989 by Berkeley Softworks
3701e196c8SJohn Marino * All rights reserved.
3801e196c8SJohn Marino *
3901e196c8SJohn Marino * This code is derived from software contributed to Berkeley by
4001e196c8SJohn Marino * Adam de Boor.
4101e196c8SJohn Marino *
4201e196c8SJohn Marino * Redistribution and use in source and binary forms, with or without
4301e196c8SJohn Marino * modification, are permitted provided that the following conditions
4401e196c8SJohn Marino * are met:
4501e196c8SJohn Marino * 1. Redistributions of source code must retain the above copyright
4601e196c8SJohn Marino * notice, this list of conditions and the following disclaimer.
4701e196c8SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
4801e196c8SJohn Marino * notice, this list of conditions and the following disclaimer in the
4901e196c8SJohn Marino * documentation and/or other materials provided with the distribution.
5001e196c8SJohn Marino * 3. All advertising materials mentioning features or use of this software
5101e196c8SJohn Marino * must display the following acknowledgement:
5201e196c8SJohn Marino * This product includes software developed by the University of
5301e196c8SJohn Marino * California, Berkeley and its contributors.
5401e196c8SJohn Marino * 4. Neither the name of the University nor the names of its contributors
5501e196c8SJohn Marino * may be used to endorse or promote products derived from this software
5601e196c8SJohn Marino * without specific prior written permission.
5701e196c8SJohn Marino *
5801e196c8SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5901e196c8SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6001e196c8SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6101e196c8SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6201e196c8SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6301e196c8SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6401e196c8SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6501e196c8SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6601e196c8SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6701e196c8SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6801e196c8SJohn Marino * SUCH DAMAGE.
6901e196c8SJohn Marino */
7001e196c8SJohn Marino
71a34d5fb1SAntonio Huete Jimenez /*
72a34d5fb1SAntonio Huete Jimenez * Maintaining the targets and sources, which are both implemented as GNode.
7301e196c8SJohn Marino *
7401e196c8SJohn Marino * Interface:
75a34d5fb1SAntonio Huete Jimenez * Targ_Init Initialize the module.
7601e196c8SJohn Marino *
77a34d5fb1SAntonio Huete Jimenez * Targ_End Clean up the module.
7801e196c8SJohn Marino *
7901e196c8SJohn Marino * Targ_List Return the list of all targets so far.
8001e196c8SJohn Marino *
81*6eef5f0cSAntonio Huete Jimenez * GNode_New Create a new GNode with the given name, don't add it
82*6eef5f0cSAntonio Huete Jimenez * to allNodes.
8301e196c8SJohn Marino *
84a34d5fb1SAntonio Huete Jimenez * Targ_FindNode Find the node, or return NULL.
85a34d5fb1SAntonio Huete Jimenez *
86a34d5fb1SAntonio Huete Jimenez * Targ_GetNode Find the node, or create it.
87a34d5fb1SAntonio Huete Jimenez *
88a34d5fb1SAntonio Huete Jimenez * Targ_NewInternalNode
89a34d5fb1SAntonio Huete Jimenez * Create an internal node.
9001e196c8SJohn Marino *
9101e196c8SJohn Marino * Targ_FindList Given a list of names, find nodes for all
92a34d5fb1SAntonio Huete Jimenez * of them, creating them as necessary.
9301e196c8SJohn Marino *
94a34d5fb1SAntonio Huete Jimenez * Targ_Propagate Propagate information between related nodes.
95a34d5fb1SAntonio Huete Jimenez * Should be called after the makefiles are parsed
96a34d5fb1SAntonio Huete Jimenez * but before any action is taken.
9701e196c8SJohn Marino *
9801e196c8SJohn Marino * Debugging:
99a34d5fb1SAntonio Huete Jimenez * Targ_PrintGraph
100a34d5fb1SAntonio Huete Jimenez * Print out the entire graph, all variables and
101*6eef5f0cSAntonio Huete Jimenez * statistics for the directory cache.
10201e196c8SJohn Marino */
10301e196c8SJohn Marino
10401e196c8SJohn Marino #include <time.h>
10501e196c8SJohn Marino
10601e196c8SJohn Marino #include "make.h"
10701e196c8SJohn Marino #include "dir.h"
10801e196c8SJohn Marino
109a34d5fb1SAntonio Huete Jimenez /* "@(#)targ.c 8.2 (Berkeley) 3/19/94" */
110*6eef5f0cSAntonio Huete Jimenez MAKE_RCSID("$NetBSD: targ.c,v 1.178 2022/09/27 17:46:58 rillig Exp $");
11101e196c8SJohn Marino
112a34d5fb1SAntonio Huete Jimenez /*
113a34d5fb1SAntonio Huete Jimenez * All target nodes that appeared on the left-hand side of one of the
114a34d5fb1SAntonio Huete Jimenez * dependency operators ':', '::', '!'.
11501e196c8SJohn Marino */
116a34d5fb1SAntonio Huete Jimenez static GNodeList allTargets = LST_INIT;
117a34d5fb1SAntonio Huete Jimenez static HashTable allTargetsByName;
118a34d5fb1SAntonio Huete Jimenez
119a34d5fb1SAntonio Huete Jimenez #ifdef CLEANUP
120a34d5fb1SAntonio Huete Jimenez static GNodeList allNodes = LST_INIT;
121a34d5fb1SAntonio Huete Jimenez
122a34d5fb1SAntonio Huete Jimenez static void GNode_Free(void *);
123a34d5fb1SAntonio Huete Jimenez #endif
124a34d5fb1SAntonio Huete Jimenez
12501e196c8SJohn Marino void
Targ_Init(void)12601e196c8SJohn Marino Targ_Init(void)
12701e196c8SJohn Marino {
128a34d5fb1SAntonio Huete Jimenez HashTable_Init(&allTargetsByName);
12901e196c8SJohn Marino }
13001e196c8SJohn Marino
13101e196c8SJohn Marino void
Targ_End(void)13201e196c8SJohn Marino Targ_End(void)
13301e196c8SJohn Marino {
134a34d5fb1SAntonio Huete Jimenez Targ_Stats();
13501e196c8SJohn Marino #ifdef CLEANUP
136a34d5fb1SAntonio Huete Jimenez Lst_Done(&allTargets);
137a34d5fb1SAntonio Huete Jimenez HashTable_Done(&allTargetsByName);
138a34d5fb1SAntonio Huete Jimenez Lst_DoneCall(&allNodes, GNode_Free);
13901e196c8SJohn Marino #endif
14001e196c8SJohn Marino }
14101e196c8SJohn Marino
142a34d5fb1SAntonio Huete Jimenez void
Targ_Stats(void)143a34d5fb1SAntonio Huete Jimenez Targ_Stats(void)
14401e196c8SJohn Marino {
145a34d5fb1SAntonio Huete Jimenez HashTable_DebugStats(&allTargetsByName, "targets");
14601e196c8SJohn Marino }
14701e196c8SJohn Marino
148a34d5fb1SAntonio Huete Jimenez /*
149a34d5fb1SAntonio Huete Jimenez * Return the list of all targets, which are all nodes that appear on the
150a34d5fb1SAntonio Huete Jimenez * left-hand side of a dependency declaration such as "target: source".
151a34d5fb1SAntonio Huete Jimenez * The returned list does not contain pure sources.
152a34d5fb1SAntonio Huete Jimenez */
153a34d5fb1SAntonio Huete Jimenez GNodeList *
Targ_List(void)154a34d5fb1SAntonio Huete Jimenez Targ_List(void)
155a34d5fb1SAntonio Huete Jimenez {
156a34d5fb1SAntonio Huete Jimenez return &allTargets;
157a34d5fb1SAntonio Huete Jimenez }
158a34d5fb1SAntonio Huete Jimenez
159a34d5fb1SAntonio Huete Jimenez /*
160a34d5fb1SAntonio Huete Jimenez * Create a new graph node, but don't register it anywhere.
16101e196c8SJohn Marino *
162a34d5fb1SAntonio Huete Jimenez * Graph nodes that appear on the left-hand side of a dependency line such
163a34d5fb1SAntonio Huete Jimenez * as "target: source" are called targets. XXX: In some cases (like the
164a34d5fb1SAntonio Huete Jimenez * .ALLTARGETS variable), all nodes are called targets as well, even if they
165a34d5fb1SAntonio Huete Jimenez * never appear on the left-hand side. This is a mistake.
16601e196c8SJohn Marino *
167a34d5fb1SAntonio Huete Jimenez * Typical names for graph nodes are:
168a34d5fb1SAntonio Huete Jimenez * "src.c" (an ordinary file)
169a34d5fb1SAntonio Huete Jimenez * "clean" (a .PHONY target)
170a34d5fb1SAntonio Huete Jimenez * ".END" (a special hook target)
171a34d5fb1SAntonio Huete Jimenez * "-lm" (a library)
172a34d5fb1SAntonio Huete Jimenez * "libc.a(isspace.o)" (an archive member)
17301e196c8SJohn Marino */
17401e196c8SJohn Marino GNode *
GNode_New(const char * name)175a34d5fb1SAntonio Huete Jimenez GNode_New(const char *name)
17601e196c8SJohn Marino {
17701e196c8SJohn Marino GNode *gn;
17801e196c8SJohn Marino
179a34d5fb1SAntonio Huete Jimenez gn = bmake_malloc(sizeof *gn);
18001e196c8SJohn Marino gn->name = bmake_strdup(name);
18101e196c8SJohn Marino gn->uname = NULL;
18201e196c8SJohn Marino gn->path = NULL;
183a34d5fb1SAntonio Huete Jimenez gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : OP_NONE;
184*6eef5f0cSAntonio Huete Jimenez memset(&gn->flags, 0, sizeof(gn->flags));
18501e196c8SJohn Marino gn->made = UNMADE;
186a34d5fb1SAntonio Huete Jimenez gn->unmade = 0;
18701e196c8SJohn Marino gn->mtime = 0;
188a34d5fb1SAntonio Huete Jimenez gn->youngestChild = NULL;
189a34d5fb1SAntonio Huete Jimenez Lst_Init(&gn->implicitParents);
190a34d5fb1SAntonio Huete Jimenez Lst_Init(&gn->parents);
191a34d5fb1SAntonio Huete Jimenez Lst_Init(&gn->children);
192a34d5fb1SAntonio Huete Jimenez Lst_Init(&gn->order_pred);
193a34d5fb1SAntonio Huete Jimenez Lst_Init(&gn->order_succ);
194a34d5fb1SAntonio Huete Jimenez Lst_Init(&gn->cohorts);
195a34d5fb1SAntonio Huete Jimenez gn->cohort_num[0] = '\0';
196a34d5fb1SAntonio Huete Jimenez gn->unmade_cohorts = 0;
197a34d5fb1SAntonio Huete Jimenez gn->centurion = NULL;
198a34d5fb1SAntonio Huete Jimenez gn->checked_seqno = 0;
199a34d5fb1SAntonio Huete Jimenez HashTable_Init(&gn->vars);
200a34d5fb1SAntonio Huete Jimenez Lst_Init(&gn->commands);
20101e196c8SJohn Marino gn->suffix = NULL;
20201e196c8SJohn Marino gn->fname = NULL;
203a34d5fb1SAntonio Huete Jimenez gn->lineno = 0;
20401e196c8SJohn Marino
20501e196c8SJohn Marino #ifdef CLEANUP
206a34d5fb1SAntonio Huete Jimenez Lst_Append(&allNodes, gn);
20701e196c8SJohn Marino #endif
20801e196c8SJohn Marino
209ca58f742SDaniel Fojt return gn;
21001e196c8SJohn Marino }
21101e196c8SJohn Marino
21201e196c8SJohn Marino #ifdef CLEANUP
21301e196c8SJohn Marino static void
GNode_Free(void * gnp)214a34d5fb1SAntonio Huete Jimenez GNode_Free(void *gnp)
21501e196c8SJohn Marino {
216a34d5fb1SAntonio Huete Jimenez GNode *gn = gnp;
21701e196c8SJohn Marino
21801e196c8SJohn Marino free(gn->name);
21901e196c8SJohn Marino free(gn->uname);
22001e196c8SJohn Marino free(gn->path);
22101e196c8SJohn Marino
222a34d5fb1SAntonio Huete Jimenez /* Don't free gn->youngestChild since it is not owned by this node. */
223a34d5fb1SAntonio Huete Jimenez
224a34d5fb1SAntonio Huete Jimenez /*
225a34d5fb1SAntonio Huete Jimenez * In the following lists, only free the list nodes, but not the
226a34d5fb1SAntonio Huete Jimenez * GNodes in them since these are not owned by this node.
227a34d5fb1SAntonio Huete Jimenez */
228a34d5fb1SAntonio Huete Jimenez Lst_Done(&gn->implicitParents);
229a34d5fb1SAntonio Huete Jimenez Lst_Done(&gn->parents);
230a34d5fb1SAntonio Huete Jimenez Lst_Done(&gn->children);
231a34d5fb1SAntonio Huete Jimenez Lst_Done(&gn->order_pred);
232a34d5fb1SAntonio Huete Jimenez Lst_Done(&gn->order_succ);
233a34d5fb1SAntonio Huete Jimenez Lst_Done(&gn->cohorts);
234a34d5fb1SAntonio Huete Jimenez
235a34d5fb1SAntonio Huete Jimenez /*
236a34d5fb1SAntonio Huete Jimenez * Do not free the variables themselves, even though they are owned
237a34d5fb1SAntonio Huete Jimenez * by this node.
238a34d5fb1SAntonio Huete Jimenez *
239a34d5fb1SAntonio Huete Jimenez * XXX: For the nodes that represent targets or sources (and not
240a34d5fb1SAntonio Huete Jimenez * SCOPE_GLOBAL), it should be safe to free the variables as well,
241a34d5fb1SAntonio Huete Jimenez * since each node manages the memory for all its variables itself.
242a34d5fb1SAntonio Huete Jimenez *
243ec533708SSascha Wildner * XXX: The GNodes that are only used as variable scopes (SCOPE_CMD,
244a34d5fb1SAntonio Huete Jimenez * SCOPE_GLOBAL, SCOPE_INTERNAL) are not freed at all (see Var_End,
245a34d5fb1SAntonio Huete Jimenez * where they are not mentioned). These might be freed at all, if
246a34d5fb1SAntonio Huete Jimenez * their variable values are indeed not used anywhere else (see
247a34d5fb1SAntonio Huete Jimenez * Trace_Init for the only suspicious use).
248a34d5fb1SAntonio Huete Jimenez */
249a34d5fb1SAntonio Huete Jimenez HashTable_Done(&gn->vars);
250a34d5fb1SAntonio Huete Jimenez
251a34d5fb1SAntonio Huete Jimenez /*
252a34d5fb1SAntonio Huete Jimenez * Do not free the commands themselves, as they may be shared with
253a34d5fb1SAntonio Huete Jimenez * other nodes.
254a34d5fb1SAntonio Huete Jimenez */
255a34d5fb1SAntonio Huete Jimenez Lst_Done(&gn->commands);
256a34d5fb1SAntonio Huete Jimenez
257a34d5fb1SAntonio Huete Jimenez /*
258a34d5fb1SAntonio Huete Jimenez * gn->suffix is not owned by this node.
259a34d5fb1SAntonio Huete Jimenez *
260a34d5fb1SAntonio Huete Jimenez * XXX: gn->suffix should be unreferenced here. This requires a
261a34d5fb1SAntonio Huete Jimenez * thorough check that the reference counting is done correctly in
262a34d5fb1SAntonio Huete Jimenez * all places, otherwise a suffix might be freed too early.
263a34d5fb1SAntonio Huete Jimenez */
264a34d5fb1SAntonio Huete Jimenez
26501e196c8SJohn Marino free(gn);
26601e196c8SJohn Marino }
26701e196c8SJohn Marino #endif
26801e196c8SJohn Marino
269a34d5fb1SAntonio Huete Jimenez /* Get the existing global node, or return NULL. */
270a34d5fb1SAntonio Huete Jimenez GNode *
Targ_FindNode(const char * name)271a34d5fb1SAntonio Huete Jimenez Targ_FindNode(const char *name)
272a34d5fb1SAntonio Huete Jimenez {
273a34d5fb1SAntonio Huete Jimenez return HashTable_FindValue(&allTargetsByName, name);
274a34d5fb1SAntonio Huete Jimenez }
27501e196c8SJohn Marino
276a34d5fb1SAntonio Huete Jimenez /* Get the existing global node, or create it. */
277a34d5fb1SAntonio Huete Jimenez GNode *
Targ_GetNode(const char * name)278a34d5fb1SAntonio Huete Jimenez Targ_GetNode(const char *name)
279a34d5fb1SAntonio Huete Jimenez {
280*6eef5f0cSAntonio Huete Jimenez bool isNew;
281a34d5fb1SAntonio Huete Jimenez HashEntry *he = HashTable_CreateEntry(&allTargetsByName, name, &isNew);
282a34d5fb1SAntonio Huete Jimenez if (!isNew)
283a34d5fb1SAntonio Huete Jimenez return HashEntry_Get(he);
284a34d5fb1SAntonio Huete Jimenez
285a34d5fb1SAntonio Huete Jimenez {
286a34d5fb1SAntonio Huete Jimenez GNode *gn = Targ_NewInternalNode(name);
287a34d5fb1SAntonio Huete Jimenez HashEntry_Set(he, gn);
288a34d5fb1SAntonio Huete Jimenez return gn;
289a34d5fb1SAntonio Huete Jimenez }
290a34d5fb1SAntonio Huete Jimenez }
291a34d5fb1SAntonio Huete Jimenez
292a34d5fb1SAntonio Huete Jimenez /*
293a34d5fb1SAntonio Huete Jimenez * Create a node, register it in .ALLTARGETS but don't store it in the
294a34d5fb1SAntonio Huete Jimenez * table of global nodes. This means it cannot be found by name.
29501e196c8SJohn Marino *
296a34d5fb1SAntonio Huete Jimenez * This is used for internal nodes, such as cohorts or .WAIT nodes.
29701e196c8SJohn Marino */
29801e196c8SJohn Marino GNode *
Targ_NewInternalNode(const char * name)299a34d5fb1SAntonio Huete Jimenez Targ_NewInternalNode(const char *name)
30001e196c8SJohn Marino {
301a34d5fb1SAntonio Huete Jimenez GNode *gn = GNode_New(name);
302a34d5fb1SAntonio Huete Jimenez Global_Append(".ALLTARGETS", name);
303a34d5fb1SAntonio Huete Jimenez Lst_Append(&allTargets, gn);
304a34d5fb1SAntonio Huete Jimenez DEBUG1(TARG, "Adding \"%s\" to all targets.\n", gn->name);
30501e196c8SJohn Marino if (doing_depend)
306*6eef5f0cSAntonio Huete Jimenez gn->flags.fromDepend = true;
30701e196c8SJohn Marino return gn;
30801e196c8SJohn Marino }
30901e196c8SJohn Marino
31001e196c8SJohn Marino /*
311a34d5fb1SAntonio Huete Jimenez * Return the .END node, which contains the commands to be run when
312a34d5fb1SAntonio Huete Jimenez * everything else has been made.
31301e196c8SJohn Marino */
314a34d5fb1SAntonio Huete Jimenez GNode *
Targ_GetEndNode(void)315a34d5fb1SAntonio Huete Jimenez Targ_GetEndNode(void)
31601e196c8SJohn Marino {
317a34d5fb1SAntonio Huete Jimenez /*
318a34d5fb1SAntonio Huete Jimenez * Save the node locally to avoid having to search for it all
319a34d5fb1SAntonio Huete Jimenez * the time.
320a34d5fb1SAntonio Huete Jimenez */
321a34d5fb1SAntonio Huete Jimenez static GNode *endNode = NULL;
322a34d5fb1SAntonio Huete Jimenez
323a34d5fb1SAntonio Huete Jimenez if (endNode == NULL) {
324a34d5fb1SAntonio Huete Jimenez endNode = Targ_GetNode(".END");
325a34d5fb1SAntonio Huete Jimenez endNode->type = OP_SPECIAL;
32601e196c8SJohn Marino }
327a34d5fb1SAntonio Huete Jimenez return endNode;
32801e196c8SJohn Marino }
32901e196c8SJohn Marino
330a34d5fb1SAntonio Huete Jimenez /* Add the named nodes to the list, creating them as necessary. */
331a34d5fb1SAntonio Huete Jimenez void
Targ_FindList(GNodeList * gns,StringList * names)332a34d5fb1SAntonio Huete Jimenez Targ_FindList(GNodeList *gns, StringList *names)
33301e196c8SJohn Marino {
334a34d5fb1SAntonio Huete Jimenez StringListNode *ln;
335a34d5fb1SAntonio Huete Jimenez
336a34d5fb1SAntonio Huete Jimenez for (ln = names->first; ln != NULL; ln = ln->next) {
337a34d5fb1SAntonio Huete Jimenez const char *name = ln->datum;
338a34d5fb1SAntonio Huete Jimenez GNode *gn = Targ_GetNode(name);
339a34d5fb1SAntonio Huete Jimenez Lst_Append(gns, gn);
34001e196c8SJohn Marino }
34101e196c8SJohn Marino }
34201e196c8SJohn Marino
343a34d5fb1SAntonio Huete Jimenez static void
PrintNodeNames(GNodeList * gnodes)344a34d5fb1SAntonio Huete Jimenez PrintNodeNames(GNodeList *gnodes)
34501e196c8SJohn Marino {
346a34d5fb1SAntonio Huete Jimenez GNodeListNode *ln;
34701e196c8SJohn Marino
348a34d5fb1SAntonio Huete Jimenez for (ln = gnodes->first; ln != NULL; ln = ln->next) {
349a34d5fb1SAntonio Huete Jimenez GNode *gn = ln->datum;
350a34d5fb1SAntonio Huete Jimenez debug_printf(" %s%s", gn->name, gn->cohort_num);
351a34d5fb1SAntonio Huete Jimenez }
35201e196c8SJohn Marino }
35301e196c8SJohn Marino
354a34d5fb1SAntonio Huete Jimenez static void
PrintNodeNamesLine(const char * label,GNodeList * gnodes)355a34d5fb1SAntonio Huete Jimenez PrintNodeNamesLine(const char *label, GNodeList *gnodes)
35601e196c8SJohn Marino {
357a34d5fb1SAntonio Huete Jimenez if (Lst_IsEmpty(gnodes))
358a34d5fb1SAntonio Huete Jimenez return;
359a34d5fb1SAntonio Huete Jimenez debug_printf("# %s:", label);
360a34d5fb1SAntonio Huete Jimenez PrintNodeNames(gnodes);
361a34d5fb1SAntonio Huete Jimenez debug_printf("\n");
36201e196c8SJohn Marino }
36301e196c8SJohn Marino
364a34d5fb1SAntonio Huete Jimenez void
Targ_PrintCmds(GNode * gn)365a34d5fb1SAntonio Huete Jimenez Targ_PrintCmds(GNode *gn)
366a34d5fb1SAntonio Huete Jimenez {
367a34d5fb1SAntonio Huete Jimenez StringListNode *ln;
368a34d5fb1SAntonio Huete Jimenez
369a34d5fb1SAntonio Huete Jimenez for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
370a34d5fb1SAntonio Huete Jimenez const char *cmd = ln->datum;
371a34d5fb1SAntonio Huete Jimenez debug_printf("\t%s\n", cmd);
372a34d5fb1SAntonio Huete Jimenez }
373a34d5fb1SAntonio Huete Jimenez }
374a34d5fb1SAntonio Huete Jimenez
375a34d5fb1SAntonio Huete Jimenez /*
37601e196c8SJohn Marino * Format a modification time in some reasonable way and return it.
377a34d5fb1SAntonio Huete Jimenez * The formatted time is placed in a static area, so it is overwritten
37801e196c8SJohn Marino * with each call.
37901e196c8SJohn Marino */
380a34d5fb1SAntonio Huete Jimenez const char *
Targ_FmtTime(time_t tm)38101e196c8SJohn Marino Targ_FmtTime(time_t tm)
38201e196c8SJohn Marino {
38301e196c8SJohn Marino static char buf[128];
38401e196c8SJohn Marino
385a34d5fb1SAntonio Huete Jimenez struct tm *parts = localtime(&tm);
386*6eef5f0cSAntonio Huete Jimenez (void)strftime(buf, sizeof buf, "%H:%M:%S %b %d, %Y", parts);
387ca58f742SDaniel Fojt return buf;
38801e196c8SJohn Marino }
38901e196c8SJohn Marino
390a34d5fb1SAntonio Huete Jimenez /* Print out a type field giving only those attributes the user can set. */
39101e196c8SJohn Marino void
Targ_PrintType(GNodeType type)392*6eef5f0cSAntonio Huete Jimenez Targ_PrintType(GNodeType type)
39301e196c8SJohn Marino {
394*6eef5f0cSAntonio Huete Jimenez static const struct {
395*6eef5f0cSAntonio Huete Jimenez GNodeType bit;
396*6eef5f0cSAntonio Huete Jimenez bool internal;
397*6eef5f0cSAntonio Huete Jimenez const char name[10];
398*6eef5f0cSAntonio Huete Jimenez } names[] = {
399*6eef5f0cSAntonio Huete Jimenez { OP_MEMBER, true, "MEMBER" },
400*6eef5f0cSAntonio Huete Jimenez { OP_LIB, true, "LIB" },
401*6eef5f0cSAntonio Huete Jimenez { OP_ARCHV, true, "ARCHV" },
402*6eef5f0cSAntonio Huete Jimenez { OP_PHONY, true, "PHONY" },
403*6eef5f0cSAntonio Huete Jimenez { OP_NOTMAIN, false, "NOTMAIN" },
404*6eef5f0cSAntonio Huete Jimenez { OP_INVISIBLE, false, "INVISIBLE" },
405*6eef5f0cSAntonio Huete Jimenez { OP_MADE, true, "MADE" },
406*6eef5f0cSAntonio Huete Jimenez { OP_JOIN, false, "JOIN" },
407*6eef5f0cSAntonio Huete Jimenez { OP_MAKE, false, "MAKE" },
408*6eef5f0cSAntonio Huete Jimenez { OP_SILENT, false, "SILENT" },
409*6eef5f0cSAntonio Huete Jimenez { OP_PRECIOUS, false, "PRECIOUS" },
410*6eef5f0cSAntonio Huete Jimenez { OP_IGNORE, false, "IGNORE" },
411*6eef5f0cSAntonio Huete Jimenez { OP_EXEC, false, "EXEC" },
412*6eef5f0cSAntonio Huete Jimenez { OP_USE, false, "USE" },
413*6eef5f0cSAntonio Huete Jimenez { OP_USEBEFORE, false, "USEBEFORE" },
414*6eef5f0cSAntonio Huete Jimenez { OP_OPTIONAL, false, "OPTIONAL" },
415*6eef5f0cSAntonio Huete Jimenez };
416*6eef5f0cSAntonio Huete Jimenez size_t i;
41701e196c8SJohn Marino
418*6eef5f0cSAntonio Huete Jimenez for (i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
419*6eef5f0cSAntonio Huete Jimenez if (type & names[i].bit) {
420*6eef5f0cSAntonio Huete Jimenez if (names[i].internal)
421*6eef5f0cSAntonio Huete Jimenez DEBUG1(TARG, " .%s", names[i].name);
422*6eef5f0cSAntonio Huete Jimenez else
423*6eef5f0cSAntonio Huete Jimenez debug_printf(" .%s", names[i].name);
42401e196c8SJohn Marino }
42501e196c8SJohn Marino }
42601e196c8SJohn Marino }
42701e196c8SJohn Marino
428a34d5fb1SAntonio Huete Jimenez const char *
GNodeMade_Name(GNodeMade made)429a34d5fb1SAntonio Huete Jimenez GNodeMade_Name(GNodeMade made)
43001e196c8SJohn Marino {
43101e196c8SJohn Marino switch (made) {
43201e196c8SJohn Marino case UNMADE: return "unmade";
43301e196c8SJohn Marino case DEFERRED: return "deferred";
43401e196c8SJohn Marino case REQUESTED: return "requested";
43501e196c8SJohn Marino case BEINGMADE: return "being made";
43601e196c8SJohn Marino case MADE: return "made";
43701e196c8SJohn Marino case UPTODATE: return "up-to-date";
43801e196c8SJohn Marino case ERROR: return "error when made";
43901e196c8SJohn Marino case ABORTED: return "aborted";
44001e196c8SJohn Marino default: return "unknown enum_made value";
44101e196c8SJohn Marino }
44201e196c8SJohn Marino }
44301e196c8SJohn Marino
444a34d5fb1SAntonio Huete Jimenez static const char *
GNode_OpName(const GNode * gn)445a34d5fb1SAntonio Huete Jimenez GNode_OpName(const GNode *gn)
44601e196c8SJohn Marino {
44701e196c8SJohn Marino switch (gn->type & OP_OPMASK) {
44801e196c8SJohn Marino case OP_DEPENDS:
449a34d5fb1SAntonio Huete Jimenez return ":";
45001e196c8SJohn Marino case OP_FORCE:
451a34d5fb1SAntonio Huete Jimenez return "!";
45201e196c8SJohn Marino case OP_DOUBLEDEP:
453a34d5fb1SAntonio Huete Jimenez return "::";
45401e196c8SJohn Marino }
455a34d5fb1SAntonio Huete Jimenez return "";
45601e196c8SJohn Marino }
45701e196c8SJohn Marino
458*6eef5f0cSAntonio Huete Jimenez static bool
GNodeFlags_IsNone(GNodeFlags flags)459*6eef5f0cSAntonio Huete Jimenez GNodeFlags_IsNone(GNodeFlags flags)
460*6eef5f0cSAntonio Huete Jimenez {
461*6eef5f0cSAntonio Huete Jimenez return !flags.remake
462*6eef5f0cSAntonio Huete Jimenez && !flags.childMade
463*6eef5f0cSAntonio Huete Jimenez && !flags.force
464*6eef5f0cSAntonio Huete Jimenez && !flags.doneWait
465*6eef5f0cSAntonio Huete Jimenez && !flags.doneOrder
466*6eef5f0cSAntonio Huete Jimenez && !flags.fromDepend
467*6eef5f0cSAntonio Huete Jimenez && !flags.doneAllsrc
468*6eef5f0cSAntonio Huete Jimenez && !flags.cycle
469*6eef5f0cSAntonio Huete Jimenez && !flags.doneCycle;
470*6eef5f0cSAntonio Huete Jimenez }
471*6eef5f0cSAntonio Huete Jimenez
472a34d5fb1SAntonio Huete Jimenez /* Print the contents of a node. */
473a34d5fb1SAntonio Huete Jimenez void
Targ_PrintNode(GNode * gn,int pass)474a34d5fb1SAntonio Huete Jimenez Targ_PrintNode(GNode *gn, int pass)
47501e196c8SJohn Marino {
476a34d5fb1SAntonio Huete Jimenez debug_printf("# %s%s", gn->name, gn->cohort_num);
477a34d5fb1SAntonio Huete Jimenez GNode_FprintDetails(opts.debug_file, ", ", gn, "\n");
478*6eef5f0cSAntonio Huete Jimenez if (GNodeFlags_IsNone(gn->flags))
479a34d5fb1SAntonio Huete Jimenez return;
48001e196c8SJohn Marino
481a34d5fb1SAntonio Huete Jimenez if (!GNode_IsTarget(gn))
482a34d5fb1SAntonio Huete Jimenez return;
48301e196c8SJohn Marino
484a34d5fb1SAntonio Huete Jimenez debug_printf("#\n");
485*6eef5f0cSAntonio Huete Jimenez if (gn == mainNode)
486a34d5fb1SAntonio Huete Jimenez debug_printf("# *** MAIN TARGET ***\n");
487a34d5fb1SAntonio Huete Jimenez
488a34d5fb1SAntonio Huete Jimenez if (pass >= 2) {
489a34d5fb1SAntonio Huete Jimenez if (gn->unmade > 0)
490a34d5fb1SAntonio Huete Jimenez debug_printf("# %d unmade children\n", gn->unmade);
491a34d5fb1SAntonio Huete Jimenez else
492a34d5fb1SAntonio Huete Jimenez debug_printf("# No unmade children\n");
493a34d5fb1SAntonio Huete Jimenez if (!(gn->type & (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC))) {
494a34d5fb1SAntonio Huete Jimenez if (gn->mtime != 0) {
495a34d5fb1SAntonio Huete Jimenez debug_printf("# last modified %s: %s\n",
496a34d5fb1SAntonio Huete Jimenez Targ_FmtTime(gn->mtime),
497a34d5fb1SAntonio Huete Jimenez GNodeMade_Name(gn->made));
498a34d5fb1SAntonio Huete Jimenez } else if (gn->made != UNMADE) {
499a34d5fb1SAntonio Huete Jimenez debug_printf("# nonexistent (maybe): %s\n",
500a34d5fb1SAntonio Huete Jimenez GNodeMade_Name(gn->made));
501a34d5fb1SAntonio Huete Jimenez } else
502a34d5fb1SAntonio Huete Jimenez debug_printf("# unmade\n");
503a34d5fb1SAntonio Huete Jimenez }
504a34d5fb1SAntonio Huete Jimenez PrintNodeNamesLine("implicit parents", &gn->implicitParents);
505a34d5fb1SAntonio Huete Jimenez } else {
506a34d5fb1SAntonio Huete Jimenez if (gn->unmade != 0)
507a34d5fb1SAntonio Huete Jimenez debug_printf("# %d unmade children\n", gn->unmade);
50801e196c8SJohn Marino }
50901e196c8SJohn Marino
510a34d5fb1SAntonio Huete Jimenez PrintNodeNamesLine("parents", &gn->parents);
511a34d5fb1SAntonio Huete Jimenez PrintNodeNamesLine("order_pred", &gn->order_pred);
512a34d5fb1SAntonio Huete Jimenez PrintNodeNamesLine("order_succ", &gn->order_succ);
513a34d5fb1SAntonio Huete Jimenez
514a34d5fb1SAntonio Huete Jimenez debug_printf("%-16s%s", gn->name, GNode_OpName(gn));
515a34d5fb1SAntonio Huete Jimenez Targ_PrintType(gn->type);
516a34d5fb1SAntonio Huete Jimenez PrintNodeNames(&gn->children);
517a34d5fb1SAntonio Huete Jimenez debug_printf("\n");
518a34d5fb1SAntonio Huete Jimenez Targ_PrintCmds(gn);
519a34d5fb1SAntonio Huete Jimenez debug_printf("\n\n");
520a34d5fb1SAntonio Huete Jimenez if (gn->type & OP_DOUBLEDEP)
521a34d5fb1SAntonio Huete Jimenez Targ_PrintNodes(&gn->cohorts, pass);
522a34d5fb1SAntonio Huete Jimenez }
523a34d5fb1SAntonio Huete Jimenez
524a34d5fb1SAntonio Huete Jimenez void
Targ_PrintNodes(GNodeList * gnodes,int pass)525a34d5fb1SAntonio Huete Jimenez Targ_PrintNodes(GNodeList *gnodes, int pass)
526a34d5fb1SAntonio Huete Jimenez {
527a34d5fb1SAntonio Huete Jimenez GNodeListNode *ln;
528a34d5fb1SAntonio Huete Jimenez
529a34d5fb1SAntonio Huete Jimenez for (ln = gnodes->first; ln != NULL; ln = ln->next)
530a34d5fb1SAntonio Huete Jimenez Targ_PrintNode(ln->datum, pass);
531a34d5fb1SAntonio Huete Jimenez }
532a34d5fb1SAntonio Huete Jimenez
533a34d5fb1SAntonio Huete Jimenez static void
PrintOnlySources(void)534a34d5fb1SAntonio Huete Jimenez PrintOnlySources(void)
535a34d5fb1SAntonio Huete Jimenez {
536a34d5fb1SAntonio Huete Jimenez GNodeListNode *ln;
537a34d5fb1SAntonio Huete Jimenez
538a34d5fb1SAntonio Huete Jimenez for (ln = allTargets.first; ln != NULL; ln = ln->next) {
539a34d5fb1SAntonio Huete Jimenez GNode *gn = ln->datum;
540a34d5fb1SAntonio Huete Jimenez if (GNode_IsTarget(gn))
541a34d5fb1SAntonio Huete Jimenez continue;
542a34d5fb1SAntonio Huete Jimenez
543a34d5fb1SAntonio Huete Jimenez debug_printf("#\t%s [%s]", gn->name, GNode_Path(gn));
544a34d5fb1SAntonio Huete Jimenez Targ_PrintType(gn->type);
545a34d5fb1SAntonio Huete Jimenez debug_printf("\n");
546a34d5fb1SAntonio Huete Jimenez }
547a34d5fb1SAntonio Huete Jimenez }
548a34d5fb1SAntonio Huete Jimenez
549a34d5fb1SAntonio Huete Jimenez /*
55001e196c8SJohn Marino * Input:
551a34d5fb1SAntonio Huete Jimenez * pass 1 => before processing
552a34d5fb1SAntonio Huete Jimenez * 2 => after processing
553a34d5fb1SAntonio Huete Jimenez * 3 => after processing, an error occurred
55401e196c8SJohn Marino */
55501e196c8SJohn Marino void
Targ_PrintGraph(int pass)55601e196c8SJohn Marino Targ_PrintGraph(int pass)
55701e196c8SJohn Marino {
558a34d5fb1SAntonio Huete Jimenez debug_printf("#*** Input graph:\n");
559a34d5fb1SAntonio Huete Jimenez Targ_PrintNodes(&allTargets, pass);
560a34d5fb1SAntonio Huete Jimenez debug_printf("\n");
561a34d5fb1SAntonio Huete Jimenez debug_printf("\n");
562a34d5fb1SAntonio Huete Jimenez
563a34d5fb1SAntonio Huete Jimenez debug_printf("#\n");
564a34d5fb1SAntonio Huete Jimenez debug_printf("# Files that are only sources:\n");
565a34d5fb1SAntonio Huete Jimenez PrintOnlySources();
566a34d5fb1SAntonio Huete Jimenez
567a34d5fb1SAntonio Huete Jimenez debug_printf("#*** Global Variables:\n");
568a34d5fb1SAntonio Huete Jimenez Var_Dump(SCOPE_GLOBAL);
569a34d5fb1SAntonio Huete Jimenez
570a34d5fb1SAntonio Huete Jimenez debug_printf("#*** Command-line Variables:\n");
571a34d5fb1SAntonio Huete Jimenez Var_Dump(SCOPE_CMDLINE);
572a34d5fb1SAntonio Huete Jimenez
573a34d5fb1SAntonio Huete Jimenez debug_printf("\n");
57401e196c8SJohn Marino Dir_PrintDirectories();
575a34d5fb1SAntonio Huete Jimenez debug_printf("\n");
576a34d5fb1SAntonio Huete Jimenez
57701e196c8SJohn Marino Suff_PrintAll();
57801e196c8SJohn Marino }
57901e196c8SJohn Marino
580a34d5fb1SAntonio Huete Jimenez /*
581a34d5fb1SAntonio Huete Jimenez * Propagate some type information to cohort nodes (those from the '::'
582a34d5fb1SAntonio Huete Jimenez * dependency operator).
58301e196c8SJohn Marino *
584a34d5fb1SAntonio Huete Jimenez * Should be called after the makefiles are parsed but before any action is
585a34d5fb1SAntonio Huete Jimenez * taken.
58601e196c8SJohn Marino */
58701e196c8SJohn Marino void
Targ_Propagate(void)58801e196c8SJohn Marino Targ_Propagate(void)
58901e196c8SJohn Marino {
590a34d5fb1SAntonio Huete Jimenez GNodeListNode *ln, *cln;
591a34d5fb1SAntonio Huete Jimenez
592a34d5fb1SAntonio Huete Jimenez for (ln = allTargets.first; ln != NULL; ln = ln->next) {
593a34d5fb1SAntonio Huete Jimenez GNode *gn = ln->datum;
594a34d5fb1SAntonio Huete Jimenez GNodeType type = gn->type;
595a34d5fb1SAntonio Huete Jimenez
596a34d5fb1SAntonio Huete Jimenez if (!(type & OP_DOUBLEDEP))
597a34d5fb1SAntonio Huete Jimenez continue;
598a34d5fb1SAntonio Huete Jimenez
599a34d5fb1SAntonio Huete Jimenez for (cln = gn->cohorts.first; cln != NULL; cln = cln->next) {
600a34d5fb1SAntonio Huete Jimenez GNode *cohort = cln->datum;
601a34d5fb1SAntonio Huete Jimenez
602*6eef5f0cSAntonio Huete Jimenez cohort->type |= type & (unsigned)~OP_OPMASK;
603a34d5fb1SAntonio Huete Jimenez }
604a34d5fb1SAntonio Huete Jimenez }
60501e196c8SJohn Marino }
606