targ.c revision 1.149 1 /* $NetBSD: targ.c,v 1.149 2020/12/04 14:39:56 rillig 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
35 /*
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
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.
57 *
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
68 * SUCH DAMAGE.
69 */
70
71 /*
72 * Maintaining the targets and sources, which are both implemented as GNode.
73 *
74 * Interface:
75 * Targ_Init Initialize the module.
76 *
77 * Targ_End Clean up the module.
78 *
79 * Targ_List Return the list of all targets so far.
80 *
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
84 * initialized.
85 *
86 * Targ_FindNode Find the node, or return NULL.
87 *
88 * Targ_GetNode Find the node, or create it.
89 *
90 * Targ_NewInternalNode
91 * Create an internal node.
92 *
93 * Targ_FindList Given a list of names, find nodes for all
94 * of them, creating them as necessary.
95 *
96 * Targ_Ignore Return TRUE if errors should be ignored when
97 * creating the given target.
98 *
99 * Targ_Silent Return TRUE if we should be silent when
100 * creating the given target.
101 *
102 * Targ_Precious Return TRUE if the target is precious and
103 * should not be removed if we are interrupted.
104 *
105 * Targ_Propagate Propagate information between related nodes.
106 * Should be called after the makefiles are parsed
107 * but before any action is taken.
108 *
109 * Debugging:
110 * Targ_PrintGraph
111 * Print out the entire graph, all variables and
112 * statistics for the directory cache. Should print
113 * something for suffixes, too, but...
114 */
115
116 #include <time.h>
117
118 #include "make.h"
119 #include "dir.h"
120
121 /* "@(#)targ.c 8.2 (Berkeley) 3/19/94" */
122 MAKE_RCSID("$NetBSD: targ.c,v 1.149 2020/12/04 14:39:56 rillig Exp $");
123
124 /*
125 * All target nodes that appeared on the left-hand side of one of the
126 * dependency operators ':', '::', '!'.
127 */
128 static GNodeList allTargets = LST_INIT;
129 static HashTable allTargetsByName;
130
131 #ifdef CLEANUP
132 static GNodeList allNodes = LST_INIT;
133
134 static void GNode_Free(void *);
135 #endif
136
137 void
138 Targ_Init(void)
139 {
140 HashTable_Init(&allTargetsByName);
141 }
142
143 void
144 Targ_End(void)
145 {
146 Targ_Stats();
147 #ifdef CLEANUP
148 Lst_Done(&allTargets);
149 HashTable_Done(&allTargetsByName);
150 Lst_DoneCall(&allNodes, GNode_Free);
151 #endif
152 }
153
154 void
155 Targ_Stats(void)
156 {
157 HashTable_DebugStats(&allTargetsByName, "targets");
158 }
159
160 /*
161 * Return the list of all targets, which are all nodes that appear on the
162 * left-hand side of a dependency declaration such as "target: source".
163 * The returned list does not contain pure sources.
164 */
165 GNodeList *
166 Targ_List(void)
167 {
168 return &allTargets;
169 }
170
171 /* Create a new graph node, but don't register it anywhere.
172 *
173 * Graph nodes that appear on the left-hand side of a dependency line such
174 * as "target: source" are called targets. XXX: In some cases (like the
175 * .ALLTARGETS variable), all nodes are called targets as well, even if they
176 * never appear on the left-hand side. This is a mistake.
177 *
178 * Typical names for graph nodes are:
179 * "src.c" (an ordinary file)
180 * "clean" (a .PHONY target)
181 * ".END" (a special hook target)
182 * "-lm" (a library)
183 * "libc.a(isspace.o)" (an archive member)
184 */
185 GNode *
186 GNode_New(const char *name)
187 {
188 GNode *gn;
189
190 gn = bmake_malloc(sizeof *gn);
191 gn->name = bmake_strdup(name);
192 gn->uname = NULL;
193 gn->path = NULL;
194 gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : 0;
195 gn->flags = 0;
196 gn->made = UNMADE;
197 gn->unmade = 0;
198 gn->mtime = 0;
199 gn->youngestChild = NULL;
200 Lst_Init(&gn->implicitParents);
201 Lst_Init(&gn->parents);
202 Lst_Init(&gn->children);
203 Lst_Init(&gn->order_pred);
204 Lst_Init(&gn->order_succ);
205 Lst_Init(&gn->cohorts);
206 gn->cohort_num[0] = '\0';
207 gn->unmade_cohorts = 0;
208 gn->centurion = NULL;
209 gn->checked_seqno = 0;
210 HashTable_Init(&gn->vars);
211 Lst_Init(&gn->commands);
212 gn->suffix = NULL;
213 gn->fname = NULL;
214 gn->lineno = 0;
215
216 #ifdef CLEANUP
217 Lst_Append(&allNodes, gn);
218 #endif
219
220 return gn;
221 }
222
223 #ifdef CLEANUP
224 static void
225 GNode_Free(void *gnp)
226 {
227 GNode *gn = gnp;
228
229 free(gn->name);
230 free(gn->uname);
231 free(gn->path);
232 /* gn->youngestChild is not owned by this node. */
233 Lst_Done(&gn->implicitParents); /* Do not free the nodes themselves, */
234 Lst_Done(&gn->parents); /* as they are not owned by this node. */
235 Lst_Done(&gn->children); /* likewise */
236 Lst_Done(&gn->order_pred); /* likewise */
237 Lst_Done(&gn->order_succ); /* likewise */
238 Lst_Done(&gn->cohorts); /* likewise */
239 HashTable_Done(&gn->vars); /* Do not free the variables themselves,
240 * even though they are owned by this node.
241 * XXX: they should probably be freed. */
242 Lst_Done(&gn->commands); /* Do not free the commands themselves,
243 * as they may be shared with other nodes. */
244 /* gn->suffix is not owned by this node. */
245 /* XXX: gn->suffix should be unreferenced here. This requires a thorough
246 * check that the reference counting is done correctly in all places,
247 * otherwise a suffix might be freed too early. */
248
249 free(gn);
250 }
251 #endif
252
253 /* Get the existing global node, or return NULL. */
254 GNode *
255 Targ_FindNode(const char *name)
256 {
257 return HashTable_FindValue(&allTargetsByName, name);
258 }
259
260 /* Get the existing global node, or create it. */
261 GNode *
262 Targ_GetNode(const char *name)
263 {
264 Boolean isNew;
265 HashEntry *he = HashTable_CreateEntry(&allTargetsByName, name, &isNew);
266 if (!isNew)
267 return HashEntry_Get(he);
268
269 {
270 GNode *gn = Targ_NewInternalNode(name);
271 HashEntry_Set(he, gn);
272 return gn;
273 }
274 }
275
276 /*
277 * Create a node, register it in .ALLTARGETS but don't store it in the
278 * table of global nodes. This means it cannot be found by name.
279 *
280 * This is used for internal nodes, such as cohorts or .WAIT nodes.
281 */
282 GNode *
283 Targ_NewInternalNode(const char *name)
284 {
285 GNode *gn = GNode_New(name);
286 Var_Append(".ALLTARGETS", name, VAR_GLOBAL);
287 Lst_Append(&allTargets, gn);
288 DEBUG1(TARG, "Adding \"%s\" to all targets.\n", gn->name);
289 if (doing_depend)
290 gn->flags |= FROM_DEPEND;
291 return gn;
292 }
293
294 /*
295 * Return the .END node, which contains the commands to be run when
296 * everything else has been made.
297 */
298 GNode *Targ_GetEndNode(void)
299 {
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;
305 }
306 return endNode;
307 }
308
309 /* Add the named nodes to the list, creating them as necessary. */
310 void
311 Targ_FindList(GNodeList *gns, StringList *names)
312 {
313 StringListNode *ln;
314
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(gns, gn);
319 }
320 }
321
322 /*
323 * Return true if errors from shell commands should be ignored when
324 * creating gn.
325 */
326 Boolean
327 Targ_Ignore(const GNode *gn)
328 {
329 return opts.ignoreErrors || gn->type & OP_IGNORE;
330 }
331
332 /* Return true if be silent when creating gn. */
333 Boolean
334 Targ_Silent(const GNode *gn)
335 {
336 return opts.beSilent || gn->type & OP_SILENT;
337 }
338
339 /* See if the given target is precious. */
340 Boolean
341 Targ_Precious(const GNode *gn)
342 {
343 /* XXX: Why are '::' targets precious? */
344 return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
345 }
346
347 /*
348 * The main target to be made; only for debugging output.
349 * See mainNode in parse.c for the definitive source.
350 */
351 static GNode *mainTarg;
352
353 /* Remember the main target to make; only used for debugging. */
354 void
355 Targ_SetMain(GNode *gn)
356 {
357 mainTarg = gn;
358 }
359
360 static void
361 PrintNodeNames(GNodeList *gnodes)
362 {
363 GNodeListNode *ln;
364
365 for (ln = gnodes->first; ln != NULL; ln = ln->next) {
366 GNode *gn = ln->datum;
367 debug_printf(" %s%s", gn->name, gn->cohort_num);
368 }
369 }
370
371 static void
372 PrintNodeNamesLine(const char *label, GNodeList *gnodes)
373 {
374 if (Lst_IsEmpty(gnodes))
375 return;
376 debug_printf("# %s:", label);
377 PrintNodeNames(gnodes);
378 debug_printf("\n");
379 }
380
381 void
382 Targ_PrintCmds(GNode *gn)
383 {
384 StringListNode *ln;
385 for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
386 const char *cmd = ln->datum;
387 debug_printf("\t%s\n", cmd);
388 }
389 }
390
391 /* Format a modification time in some reasonable way and return it.
392 * The time is placed in a static area, so it is overwritten with each call. */
393 char *
394 Targ_FmtTime(time_t tm)
395 {
396 struct tm *parts;
397 static char buf[128];
398
399 /* TODO: Add special case for 0, which often means ENOENT, to make it
400 * independent from time zones. */
401 parts = localtime(&tm);
402 (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
403 return buf;
404 }
405
406 /* Print out a type field giving only those attributes the user can set. */
407 void
408 Targ_PrintType(int type)
409 {
410 int tbit;
411
412 #define PRINTBIT(attr) case CONCAT(OP_,attr): debug_printf(" ." #attr); break
413 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))debug_printf(" ." #attr); break
414
415 type &= ~OP_OPMASK;
416
417 while (type != 0) {
418 tbit = 1 << (ffs(type) - 1);
419 type &= ~tbit;
420
421 switch(tbit) {
422 PRINTBIT(OPTIONAL);
423 PRINTBIT(USE);
424 PRINTBIT(EXEC);
425 PRINTBIT(IGNORE);
426 PRINTBIT(PRECIOUS);
427 PRINTBIT(SILENT);
428 PRINTBIT(MAKE);
429 PRINTBIT(JOIN);
430 PRINTBIT(INVISIBLE);
431 PRINTBIT(NOTMAIN);
432 PRINTDBIT(LIB);
433 /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
434 case OP_MEMBER: if (DEBUG(TARG))debug_printf(" .MEMBER"); break;
435 PRINTDBIT(ARCHV);
436 PRINTDBIT(MADE);
437 PRINTDBIT(PHONY);
438 }
439 }
440 }
441
442 static const char *
443 made_name(GNodeMade made)
444 {
445 switch (made) {
446 case UNMADE: return "unmade";
447 case DEFERRED: return "deferred";
448 case REQUESTED: return "requested";
449 case BEINGMADE: return "being made";
450 case MADE: return "made";
451 case UPTODATE: return "up-to-date";
452 case ERROR: return "error when made";
453 case ABORTED: return "aborted";
454 default: return "unknown enum_made value";
455 }
456 }
457
458 static const char *
459 GNode_OpName(const GNode *gn)
460 {
461 switch (gn->type & OP_OPMASK) {
462 case OP_DEPENDS:
463 return ":";
464 case OP_FORCE:
465 return "!";
466 case OP_DOUBLEDEP:
467 return "::";
468 }
469 return "";
470 }
471
472 /* Print the contents of a node. */
473 void
474 Targ_PrintNode(GNode *gn, int pass)
475 {
476 debug_printf("# %s%s", gn->name, gn->cohort_num);
477 GNode_FprintDetails(opts.debug_file, ", ", gn, "\n");
478 if (gn->flags == 0)
479 return;
480
481 if (GNode_IsTarget(gn)) {
482 debug_printf("#\n");
483 if (gn == mainTarg) {
484 debug_printf("# *** MAIN TARGET ***\n");
485 }
486 if (pass >= 2) {
487 if (gn->unmade > 0) {
488 debug_printf("# %d unmade children\n", gn->unmade);
489 } else {
490 debug_printf("# No unmade children\n");
491 }
492 if (!(gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
493 if (gn->mtime != 0) {
494 debug_printf("# last modified %s: %s\n",
495 Targ_FmtTime(gn->mtime),
496 made_name(gn->made));
497 } else if (gn->made != UNMADE) {
498 debug_printf("# non-existent (maybe): %s\n",
499 made_name(gn->made));
500 } else {
501 debug_printf("# unmade\n");
502 }
503 }
504 PrintNodeNamesLine("implicit parents", &gn->implicitParents);
505 } else {
506 if (gn->unmade)
507 debug_printf("# %d unmade children\n", gn->unmade);
508 }
509 PrintNodeNamesLine("parents", &gn->parents);
510 PrintNodeNamesLine("order_pred", &gn->order_pred);
511 PrintNodeNamesLine("order_succ", &gn->order_succ);
512
513 debug_printf("%-16s%s", gn->name, GNode_OpName(gn));
514 Targ_PrintType(gn->type);
515 PrintNodeNames(&gn->children);
516 debug_printf("\n");
517 Targ_PrintCmds(gn);
518 debug_printf("\n\n");
519 if (gn->type & OP_DOUBLEDEP) {
520 Targ_PrintNodes(&gn->cohorts, pass);
521 }
522 }
523 }
524
525 void
526 Targ_PrintNodes(GNodeList *gnodes, int pass)
527 {
528 GNodeListNode *ln;
529 for (ln = gnodes->first; ln != NULL; ln = ln->next)
530 Targ_PrintNode(ln->datum, pass);
531 }
532
533 /* Print only those targets that are just a source. */
534 static void
535 PrintOnlySources(void)
536 {
537 GNodeListNode *ln;
538
539 for (ln = allTargets.first; ln != NULL; ln = ln->next) {
540 GNode *gn = ln->datum;
541 if (GNode_IsTarget(gn))
542 continue;
543
544 debug_printf("#\t%s [%s]", gn->name, GNode_Path(gn));
545 Targ_PrintType(gn->type);
546 debug_printf("\n");
547 }
548 }
549
550 /* Input:
551 * pass 1 => before processing
552 * 2 => after processing
553 * 3 => after processing, an error occurred
554 */
555 void
556 Targ_PrintGraph(int pass)
557 {
558 debug_printf("#*** Input graph:\n");
559 Targ_PrintNodes(&allTargets, pass);
560 debug_printf("\n");
561 debug_printf("\n");
562
563 debug_printf("#\n");
564 debug_printf("# Files that are only sources:\n");
565 PrintOnlySources();
566
567 debug_printf("#*** Global Variables:\n");
568 Var_Dump(VAR_GLOBAL);
569
570 debug_printf("#*** Command-line Variables:\n");
571 Var_Dump(VAR_CMDLINE);
572
573 debug_printf("\n");
574 Dir_PrintDirectories();
575 debug_printf("\n");
576
577 Suff_PrintAll();
578 }
579
580 /* Propagate some type information to cohort nodes (those from the '::'
581 * dependency operator).
582 *
583 * Should be called after the makefiles are parsed but before any action is
584 * taken. */
585 void
586 Targ_Propagate(void)
587 {
588 GNodeListNode *ln, *cln;
589
590 for (ln = allTargets.first; ln != NULL; ln = ln->next) {
591 GNode *gn = ln->datum;
592 GNodeType type = gn->type;
593
594 if (!(type & OP_DOUBLEDEP))
595 continue;
596
597 for (cln = gn->cohorts.first; cln != NULL; cln = cln->next) {
598 GNode *cohort = cln->datum;
599
600 cohort->type |= type & ~OP_OPMASK;
601 }
602 }
603 }
604