1 /* $NetBSD: targ.c,v 1.165 2021/02/04 21:42:46 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * Maintaining the targets and sources, which are both implemented as GNode.
75 * Targ_Init Initialize the module.
77 * Targ_End Clean up the module.
79 * Targ_List Return the list of all targets so far.
81 * GNode_New Create a new GNode for the passed target
82 * (string). The node is *not* placed in the
83 * hash table, though all its fields are
86 * Targ_FindNode Find the node, or return NULL.
88 * Targ_GetNode Find the node, or create it.
90 * Targ_NewInternalNode
91 * Create an internal node.
93 * Targ_FindList Given a list of names, find nodes for all
94 * of them, creating them as necessary.
96 * Targ_Precious Return TRUE if the target is precious and
97 * should not be removed if we are interrupted.
99 * Targ_Propagate Propagate information between related nodes.
100 * Should be called after the makefiles are parsed
101 * but before any action is taken.
105 * Print out the entire graph, all variables and
106 * statistics for the directory cache. Should print
107 * something for suffixes, too, but...
115 /* "@(#)targ.c 8.2 (Berkeley) 3/19/94" */
116 MAKE_RCSID("$NetBSD: targ.c,v 1.165 2021/02/04 21:42:46 rillig Exp $");
119 * All target nodes that appeared on the left-hand side of one of the
120 * dependency operators ':', '::', '!'.
122 static GNodeList allTargets = LST_INIT;
123 static HashTable allTargetsByName;
126 static GNodeList allNodes = LST_INIT;
128 static void GNode_Free(void *);
134 HashTable_Init(&allTargetsByName);
142 Lst_Done(&allTargets);
143 HashTable_Done(&allTargetsByName);
144 Lst_DoneCall(&allNodes, GNode_Free);
151 HashTable_DebugStats(&allTargetsByName, "targets");
155 * Return the list of all targets, which are all nodes that appear on the
156 * left-hand side of a dependency declaration such as "target: source".
157 * The returned list does not contain pure sources.
166 * Create a new graph node, but don't register it anywhere.
168 * Graph nodes that appear on the left-hand side of a dependency line such
169 * as "target: source" are called targets. XXX: In some cases (like the
170 * .ALLTARGETS variable), all nodes are called targets as well, even if they
171 * never appear on the left-hand side. This is a mistake.
173 * Typical names for graph nodes are:
174 * "src.c" (an ordinary file)
175 * "clean" (a .PHONY target)
176 * ".END" (a special hook target)
178 * "libc.a(isspace.o)" (an archive member)
181 GNode_New(const char *name)
185 gn = bmake_malloc(sizeof *gn);
186 gn->name = bmake_strdup(name);
189 gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : OP_NONE;
190 gn->flags = GNF_NONE;
194 gn->youngestChild = NULL;
195 Lst_Init(&gn->implicitParents);
196 Lst_Init(&gn->parents);
197 Lst_Init(&gn->children);
198 Lst_Init(&gn->order_pred);
199 Lst_Init(&gn->order_succ);
200 Lst_Init(&gn->cohorts);
201 gn->cohort_num[0] = '\0';
202 gn->unmade_cohorts = 0;
203 gn->centurion = NULL;
204 gn->checked_seqno = 0;
205 HashTable_Init(&gn->vars);
206 Lst_Init(&gn->commands);
212 Lst_Append(&allNodes, gn);
220 GNode_Free(void *gnp)
228 /* Don't free gn->youngestChild since it is not owned by this node. */
231 * In the following lists, only free the list nodes, but not the
232 * GNodes in them since these are not owned by this node.
234 Lst_Done(&gn->implicitParents);
235 Lst_Done(&gn->parents);
236 Lst_Done(&gn->children);
237 Lst_Done(&gn->order_pred);
238 Lst_Done(&gn->order_succ);
239 Lst_Done(&gn->cohorts);
242 * Do not free the variables themselves, even though they are owned
245 * XXX: For the nodes that represent targets or sources (and not
246 * SCOPE_GLOBAL), it should be safe to free the variables as well,
247 * since each node manages the memory for all its variables itself.
249 * XXX: The GNodes that are only used as variable scopes (VAR_CMD,
250 * SCOPE_GLOBAL, SCOPE_INTERNAL) are not freed at all (see Var_End,
251 * where they are not mentioned). These might be freed at all, if
252 * their variable values are indeed not used anywhere else (see
253 * Trace_Init for the only suspicious use).
255 HashTable_Done(&gn->vars);
258 * Do not free the commands themselves, as they may be shared with
261 Lst_Done(&gn->commands);
264 * gn->suffix is not owned by this node.
266 * XXX: gn->suffix should be unreferenced here. This requires a
267 * thorough check that the reference counting is done correctly in
268 * all places, otherwise a suffix might be freed too early.
275 /* Get the existing global node, or return NULL. */
277 Targ_FindNode(const char *name)
279 return HashTable_FindValue(&allTargetsByName, name);
282 /* Get the existing global node, or create it. */
284 Targ_GetNode(const char *name)
287 HashEntry *he = HashTable_CreateEntry(&allTargetsByName, name, &isNew);
289 return HashEntry_Get(he);
292 GNode *gn = Targ_NewInternalNode(name);
293 HashEntry_Set(he, gn);
299 * Create a node, register it in .ALLTARGETS but don't store it in the
300 * table of global nodes. This means it cannot be found by name.
302 * This is used for internal nodes, such as cohorts or .WAIT nodes.
305 Targ_NewInternalNode(const char *name)
307 GNode *gn = GNode_New(name);
308 Global_Append(".ALLTARGETS", name);
309 Lst_Append(&allTargets, gn);
310 DEBUG1(TARG, "Adding \"%s\" to all targets.\n", gn->name);
312 gn->flags |= FROM_DEPEND;
317 * Return the .END node, which contains the commands to be run when
318 * everything else has been made.
321 Targ_GetEndNode(void)
324 * Save the node locally to avoid having to search for it all
327 static GNode *endNode = NULL;
329 if (endNode == NULL) {
330 endNode = Targ_GetNode(".END");
331 endNode->type = OP_SPECIAL;
336 /* Add the named nodes to the list, creating them as necessary. */
338 Targ_FindList(GNodeList *gns, StringList *names)
342 for (ln = names->first; ln != NULL; ln = ln->next) {
343 const char *name = ln->datum;
344 GNode *gn = Targ_GetNode(name);
349 /* See if the given target is precious. */
351 Targ_Precious(const GNode *gn)
353 /* XXX: Why are '::' targets precious? */
354 return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
358 * The main target to be made; only for debugging output.
359 * See mainNode in parse.c for the definitive source.
361 static GNode *mainTarg;
363 /* Remember the main target to make; only used for debugging. */
365 Targ_SetMain(GNode *gn)
371 PrintNodeNames(GNodeList *gnodes)
375 for (ln = gnodes->first; ln != NULL; ln = ln->next) {
376 GNode *gn = ln->datum;
377 debug_printf(" %s%s", gn->name, gn->cohort_num);
382 PrintNodeNamesLine(const char *label, GNodeList *gnodes)
384 if (Lst_IsEmpty(gnodes))
386 debug_printf("# %s:", label);
387 PrintNodeNames(gnodes);
392 Targ_PrintCmds(GNode *gn)
396 for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
397 const char *cmd = ln->datum;
398 debug_printf("\t%s\n", cmd);
403 * Format a modification time in some reasonable way and return it.
404 * The formatted time is placed in a static area, so it is overwritten
408 Targ_FmtTime(time_t tm)
410 static char buf[128];
412 struct tm *parts = localtime(&tm);
413 (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
417 /* Print out a type field giving only those attributes the user can set. */
419 Targ_PrintType(int type)
426 tbit = 1 << (ffs(type) - 1);
430 #define PRINTBIT(bit, attr) case bit: debug_printf(" " attr); break
431 #define PRINTDBIT(bit, attr) case bit: DEBUG0(TARG, " " attr); break
432 PRINTBIT(OP_OPTIONAL, ".OPTIONAL");
433 PRINTBIT(OP_USE, ".USE");
434 PRINTBIT(OP_EXEC, ".EXEC");
435 PRINTBIT(OP_IGNORE, ".IGNORE");
436 PRINTBIT(OP_PRECIOUS, ".PRECIOUS");
437 PRINTBIT(OP_SILENT, ".SILENT");
438 PRINTBIT(OP_MAKE, ".MAKE");
439 PRINTBIT(OP_JOIN, ".JOIN");
440 PRINTBIT(OP_INVISIBLE, ".INVISIBLE");
441 PRINTBIT(OP_NOTMAIN, ".NOTMAIN");
442 PRINTDBIT(OP_LIB, ".LIB");
443 PRINTDBIT(OP_MEMBER, ".MEMBER");
444 PRINTDBIT(OP_ARCHV, ".ARCHV");
445 PRINTDBIT(OP_MADE, ".MADE");
446 PRINTDBIT(OP_PHONY, ".PHONY");
454 GNodeMade_Name(GNodeMade made)
457 case UNMADE: return "unmade";
458 case DEFERRED: return "deferred";
459 case REQUESTED: return "requested";
460 case BEINGMADE: return "being made";
461 case MADE: return "made";
462 case UPTODATE: return "up-to-date";
463 case ERROR: return "error when made";
464 case ABORTED: return "aborted";
465 default: return "unknown enum_made value";
470 GNode_OpName(const GNode *gn)
472 switch (gn->type & OP_OPMASK) {
483 /* Print the contents of a node. */
485 Targ_PrintNode(GNode *gn, int pass)
487 debug_printf("# %s%s", gn->name, gn->cohort_num);
488 GNode_FprintDetails(opts.debug_file, ", ", gn, "\n");
492 if (!GNode_IsTarget(gn))
497 debug_printf("# *** MAIN TARGET ***\n");
501 debug_printf("# %d unmade children\n", gn->unmade);
503 debug_printf("# No unmade children\n");
504 if (!(gn->type & (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC))) {
505 if (gn->mtime != 0) {
506 debug_printf("# last modified %s: %s\n",
507 Targ_FmtTime(gn->mtime),
508 GNodeMade_Name(gn->made));
509 } else if (gn->made != UNMADE) {
510 debug_printf("# nonexistent (maybe): %s\n",
511 GNodeMade_Name(gn->made));
513 debug_printf("# unmade\n");
515 PrintNodeNamesLine("implicit parents", &gn->implicitParents);
518 debug_printf("# %d unmade children\n", gn->unmade);
521 PrintNodeNamesLine("parents", &gn->parents);
522 PrintNodeNamesLine("order_pred", &gn->order_pred);
523 PrintNodeNamesLine("order_succ", &gn->order_succ);
525 debug_printf("%-16s%s", gn->name, GNode_OpName(gn));
526 Targ_PrintType(gn->type);
527 PrintNodeNames(&gn->children);
530 debug_printf("\n\n");
531 if (gn->type & OP_DOUBLEDEP)
532 Targ_PrintNodes(&gn->cohorts, pass);
536 Targ_PrintNodes(GNodeList *gnodes, int pass)
540 for (ln = gnodes->first; ln != NULL; ln = ln->next)
541 Targ_PrintNode(ln->datum, pass);
544 /* Print only those targets that are just a source. */
546 PrintOnlySources(void)
550 for (ln = allTargets.first; ln != NULL; ln = ln->next) {
551 GNode *gn = ln->datum;
552 if (GNode_IsTarget(gn))
555 debug_printf("#\t%s [%s]", gn->name, GNode_Path(gn));
556 Targ_PrintType(gn->type);
563 * pass 1 => before processing
564 * 2 => after processing
565 * 3 => after processing, an error occurred
568 Targ_PrintGraph(int pass)
570 debug_printf("#*** Input graph:\n");
571 Targ_PrintNodes(&allTargets, pass);
576 debug_printf("# Files that are only sources:\n");
579 debug_printf("#*** Global Variables:\n");
580 Var_Dump(SCOPE_GLOBAL);
582 debug_printf("#*** Command-line Variables:\n");
583 Var_Dump(SCOPE_CMDLINE);
586 Dir_PrintDirectories();
593 * Propagate some type information to cohort nodes (those from the '::'
594 * dependency operator).
596 * Should be called after the makefiles are parsed but before any action is
602 GNodeListNode *ln, *cln;
604 for (ln = allTargets.first; ln != NULL; ln = ln->next) {
605 GNode *gn = ln->datum;
606 GNodeType type = gn->type;
608 if (!(type & OP_DOUBLEDEP))
611 for (cln = gn->cohorts.first; cln != NULL; cln = cln->next) {
612 GNode *cohort = cln->datum;
614 cohort->type |= type & ~OP_OPMASK;