1 /* $NetBSD: targ.c,v 1.135 2020/11/16 22:28:44 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_Ignore Return TRUE if errors should be ignored when
97 * creating the given target.
99 * Targ_Silent Return TRUE if we should be silent when
100 * creating the given target.
102 * Targ_Precious Return TRUE if the target is precious and
103 * should not be removed if we are interrupted.
105 * Targ_Propagate Propagate information between related nodes.
106 * Should be called after the makefiles are parsed
107 * but before any action is taken.
111 * Print out the entire graphm all variables and
112 * statistics for the directory cache. Should print
113 * something for suffixes, too, but...
121 /* "@(#)targ.c 8.2 (Berkeley) 3/19/94" */
122 MAKE_RCSID("$NetBSD: targ.c,v 1.135 2020/11/16 22:28:44 rillig Exp $");
124 /* All target nodes found so far, but not the source nodes. */
125 static GNodeList *allTargets;
126 static HashTable allTargetsByName;
129 static GNodeList *allNodes;
131 static void GNode_Free(void *);
137 allTargets = Lst_New();
138 HashTable_Init(&allTargetsByName);
140 allNodes = Lst_New();
149 Lst_Free(allTargets);
150 HashTable_Done(&allTargetsByName);
151 Lst_Destroy(allNodes, GNode_Free);
158 HashTable_DebugStats(&allTargetsByName, "targets");
162 * Return the list of all targets, which are all nodes that appear on the
163 * left-hand side of a dependency declaration such as "target: source".
164 * The returned list does not contain pure sources.
172 /* Create a new graph node, but don't register it anywhere.
174 * Graph nodes that appear on the left-hand side of a dependency line such
175 * as "target: source" are called targets. XXX: In some cases (like the
176 * .ALLTARGETS variable), all nodes are called targets as well, even if they
177 * never appear on the left-hand side. This is a mistake.
179 * Typical names for graph nodes are:
180 * "src.c" (an ordinary file)
181 * "clean" (a .PHONY target)
182 * ".END" (a special hook target)
184 * "libc.a(isspace.o)" (an archive member)
187 GNode_New(const char *name)
191 gn = bmake_malloc(sizeof *gn);
192 gn->name = bmake_strdup(name);
195 gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : 0;
200 gn->youngestChild = NULL;
201 gn->implicitParents = Lst_New();
202 gn->parents = Lst_New();
203 gn->children = Lst_New();
204 gn->order_pred = Lst_New();
205 gn->order_succ = Lst_New();
206 gn->cohorts = Lst_New();
207 gn->cohort_num[0] = '\0';
208 gn->unmade_cohorts = 0;
209 gn->centurion = NULL;
210 gn->checked_seqno = 0;
211 HashTable_Init(&gn->context);
212 gn->commands = Lst_New();
218 Lst_Append(allNodes, gn);
226 GNode_Free(void *gnp)
233 /* gn->youngestChild is not owned by this node. */
234 Lst_Free(gn->implicitParents); /* ... but not the nodes themselves, */
235 Lst_Free(gn->parents); /* as they are not owned by this node. */
236 Lst_Free(gn->children); /* likewise */
237 Lst_Free(gn->order_pred); /* likewise */
238 Lst_Free(gn->order_succ); /* likewise */
239 Lst_Free(gn->cohorts); /* likewise */
240 HashTable_Done(&gn->context); /* ... but not the variables themselves,
241 * even though they are owned by this node.
242 * XXX: they should probably be freed. */
243 Lst_Free(gn->commands); /* ... but not the commands themselves,
244 * as they may be shared with other nodes. */
245 /* gn->suffix is not owned by this node. */
246 /* XXX: gn->suffix should be unreferenced here. This requires a thorough
247 * check that the reference counting is done correctly in all places,
248 * otherwise a suffix might be freed too early. */
254 /* Get the existing global node, or return NULL. */
256 Targ_FindNode(const char *name)
258 return HashTable_FindValue(&allTargetsByName, name);
261 /* Get the existing global node, or create it. */
263 Targ_GetNode(const char *name)
266 HashEntry *he = HashTable_CreateEntry(&allTargetsByName, name, &isNew);
268 return HashEntry_Get(he);
271 GNode *gn = Targ_NewInternalNode(name);
272 HashEntry_Set(he, gn);
278 * Create a node, register it in .ALLTARGETS but don't store it in the
279 * table of global nodes. This means it cannot be found by name.
281 * This is used for internal nodes, such as cohorts or .WAIT nodes.
284 Targ_NewInternalNode(const char *name)
286 GNode *gn = GNode_New(name);
287 Var_Append(".ALLTARGETS", name, VAR_GLOBAL);
288 Lst_Append(allTargets, gn);
290 gn->flags |= FROM_DEPEND;
295 * Return the .END node, which contains the commands to be run when
296 * everything else has been made.
298 GNode *Targ_GetEndNode(void)
300 /* Save the node locally to avoid having to search for it all the time. */
301 static GNode *endNode = NULL;
302 if (endNode == NULL) {
303 endNode = Targ_GetNode(".END");
304 endNode->type = OP_SPECIAL;
309 /* Return the named nodes, creating them as necessary. */
311 Targ_FindList(StringList *names)
314 GNodeList *nodes = Lst_New();
315 for (ln = names->first; ln != NULL; ln = ln->next) {
316 const char *name = ln->datum;
317 GNode *gn = Targ_GetNode(name);
318 Lst_Append(nodes, gn);
323 /* Return true if should ignore errors when creating gn. */
325 Targ_Ignore(const GNode *gn)
327 return opts.ignoreErrors || gn->type & OP_IGNORE;
330 /* Return true if be silent when creating gn. */
332 Targ_Silent(const GNode *gn)
334 return opts.beSilent || gn->type & OP_SILENT;
337 /* See if the given target is precious. */
339 Targ_Precious(const GNode *gn)
341 /* XXX: Why are '::' targets precious? */
342 return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
346 * The main target to be made; only for debugging output.
347 * See mainNode in parse.c for the definitive source.
349 static GNode *mainTarg;
351 /* Remember the main target to make; only used for debugging. */
353 Targ_SetMain(GNode *gn)
359 PrintNodeNames(GNodeList *gnodes)
363 for (node = gnodes->first; node != NULL; node = node->next) {
364 GNode *gn = node->datum;
365 debug_printf(" %s%s", gn->name, gn->cohort_num);
370 PrintNodeNamesLine(const char *label, GNodeList *gnodes)
372 if (Lst_IsEmpty(gnodes))
374 debug_printf("# %s:", label);
375 PrintNodeNames(gnodes);
380 Targ_PrintCmds(GNode *gn)
383 for (ln = gn->commands->first; ln != NULL; ln = ln->next) {
384 const char *cmd = ln->datum;
385 debug_printf("\t%s\n", cmd);
389 /* Format a modification time in some reasonable way and return it.
390 * The time is placed in a static area, so it is overwritten with each call. */
392 Targ_FmtTime(time_t tm)
395 static char buf[128];
397 parts = localtime(&tm);
398 (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
402 /* Print out a type field giving only those attributes the user can set. */
404 Targ_PrintType(int type)
408 #define PRINTBIT(attr) case CONCAT(OP_,attr): debug_printf(" ." #attr); break
409 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))debug_printf(" ." #attr); break
414 tbit = 1 << (ffs(type) - 1);
429 /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
430 case OP_MEMBER: if (DEBUG(TARG))debug_printf(" .MEMBER"); break;
439 made_name(GNodeMade made)
442 case UNMADE: return "unmade";
443 case DEFERRED: return "deferred";
444 case REQUESTED: return "requested";
445 case BEINGMADE: return "being made";
446 case MADE: return "made";
447 case UPTODATE: return "up-to-date";
448 case ERROR: return "error when made";
449 case ABORTED: return "aborted";
450 default: return "unknown enum_made value";
455 GNode_OpName(const GNode *gn)
457 switch (gn->type & OP_OPMASK) {
468 /* Print the contents of a node. */
470 Targ_PrintNode(GNode *gn, int pass)
472 debug_printf("# %s%s", gn->name, gn->cohort_num);
473 GNode_FprintDetails(opts.debug_file, ", ", gn, "\n");
477 if (GNode_IsTarget(gn)) {
479 if (gn == mainTarg) {
480 debug_printf("# *** MAIN TARGET ***\n");
483 if (gn->unmade > 0) {
484 debug_printf("# %d unmade children\n", gn->unmade);
486 debug_printf("# No unmade children\n");
488 if (!(gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
489 if (gn->mtime != 0) {
490 debug_printf("# last modified %s: %s\n",
491 Targ_FmtTime(gn->mtime),
492 made_name(gn->made));
493 } else if (gn->made != UNMADE) {
494 debug_printf("# non-existent (maybe): %s\n",
495 made_name(gn->made));
497 debug_printf("# unmade\n");
500 PrintNodeNamesLine("implicit parents", gn->implicitParents);
503 debug_printf("# %d unmade children\n", gn->unmade);
505 PrintNodeNamesLine("parents", gn->parents);
506 PrintNodeNamesLine("order_pred", gn->order_pred);
507 PrintNodeNamesLine("order_succ", gn->order_succ);
509 debug_printf("%-16s%s", gn->name, GNode_OpName(gn));
510 Targ_PrintType(gn->type);
511 PrintNodeNames(gn->children);
514 debug_printf("\n\n");
515 if (gn->type & OP_DOUBLEDEP) {
516 Targ_PrintNodes(gn->cohorts, pass);
522 Targ_PrintNodes(GNodeList *gnodes, int pass)
525 for (ln = gnodes->first; ln != NULL; ln = ln->next)
526 Targ_PrintNode(ln->datum, pass);
529 /* Print only those targets that are just a source. */
531 PrintOnlySources(void)
535 for (ln = allTargets->first; ln != NULL; ln = ln->next) {
536 GNode *gn = ln->datum;
537 if (GNode_IsTarget(gn))
540 debug_printf("#\t%s [%s]", gn->name, GNode_Path(gn));
541 Targ_PrintType(gn->type);
547 * pass 1 => before processing
548 * 2 => after processing
549 * 3 => after processing, an error occurred
552 Targ_PrintGraph(int pass)
554 debug_printf("#*** Input graph:\n");
555 Targ_PrintNodes(allTargets, pass);
560 debug_printf("# Files that are only sources:\n");
563 debug_printf("#*** Global Variables:\n");
564 Var_Dump(VAR_GLOBAL);
566 debug_printf("#*** Command-line Variables:\n");
567 Var_Dump(VAR_CMDLINE);
570 Dir_PrintDirectories();
576 /* Propagate some type information to cohort nodes (those from the '::'
577 * dependency operator).
579 * Should be called after the makefiles are parsed but before any action is
584 GNodeListNode *ln, *cln;
586 for (ln = allTargets->first; ln != NULL; ln = ln->next) {
587 GNode *gn = ln->datum;
588 GNodeType type = gn->type;
590 if (!(type & OP_DOUBLEDEP))
593 for (cln = gn->cohorts->first; cln != NULL; cln = cln->next) {
594 GNode *cohort = cln->datum;
596 cohort->type |= type & ~OP_OPMASK;