targ.c revision 1.132 1 /* $NetBSD: targ.c,v 1.132 2020/11/16 21:53:10 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 graphm 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.132 2020/11/16 21:53:10 rillig Exp $");
123
124 static GNodeList *allTargets; /* the list of all targets found so far */
125 static HashTable targets; /* a hash table of same */
126
127 #ifdef CLEANUP
128 static GNodeList *allGNs; /* List of all the GNodes */
129 #endif
130
131 #ifdef CLEANUP
132 static void GNode_Free(void *);
133 #endif
134
135 void
136 Targ_Init(void)
137 {
138 allTargets = Lst_New();
139 HashTable_Init(&targets);
140 #ifdef CLEANUP
141 allGNs = Lst_New();
142 #endif
143 }
144
145 void
146 Targ_End(void)
147 {
148 Targ_Stats();
149 #ifdef CLEANUP
150 Lst_Free(allTargets);
151 HashTable_Done(&targets);
152 Lst_Destroy(allGNs, GNode_Free);
153 #endif
154 }
155
156 void
157 Targ_Stats(void)
158 {
159 HashTable_DebugStats(&targets, "targets");
160 }
161
162 /*
163 * Return the list of all targets, which are all nodes that appear on the
164 * left-hand side of a dependency declaration such as "target: source".
165 * The returned list does not contain pure sources.
166 */
167 GNodeList *
168 Targ_List(void)
169 {
170 return allTargets;
171 }
172
173 /* Create a new graph node, but don't register it anywhere.
174 *
175 * Graph nodes that appear on the left-hand side of a dependency line such
176 * as "target: source" are called targets. XXX: In some cases (like the
177 * .ALLTARGETS variable), all nodes are called targets as well, even if they
178 * never appear on the left-hand side. This is a mistake.
179 *
180 * Typical names for graph nodes are:
181 * "src.c" (an ordinary file)
182 * "clean" (a .PHONY target)
183 * ".END" (a special hook target)
184 * "-lm" (a library)
185 * "libc.a(isspace.o)" (an archive member)
186 */
187 GNode *
188 GNode_New(const char *name)
189 {
190 GNode *gn;
191
192 gn = bmake_malloc(sizeof *gn);
193 gn->name = bmake_strdup(name);
194 gn->uname = NULL;
195 gn->path = NULL;
196 gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : 0;
197 gn->unmade = 0;
198 gn->unmade_cohorts = 0;
199 gn->cohort_num[0] = '\0';
200 gn->centurion = NULL;
201 gn->made = UNMADE;
202 gn->flags = 0;
203 gn->checked_seqno = 0;
204 gn->mtime = 0;
205 gn->youngestChild = NULL;
206 gn->implicitParents = Lst_New();
207 gn->cohorts = Lst_New();
208 gn->parents = Lst_New();
209 gn->children = Lst_New();
210 gn->order_pred = Lst_New();
211 gn->order_succ = Lst_New();
212 HashTable_Init(&gn->context);
213 gn->commands = Lst_New();
214 gn->suffix = NULL;
215 gn->fname = NULL;
216 gn->lineno = 0;
217
218 #ifdef CLEANUP
219 Lst_Append(allGNs, gn);
220 #endif
221
222 return gn;
223 }
224
225 #ifdef CLEANUP
226 static void
227 GNode_Free(void *gnp)
228 {
229 GNode *gn = gnp;
230
231 free(gn->name);
232 free(gn->uname);
233 free(gn->path);
234
235 Lst_Free(gn->implicitParents);
236 Lst_Free(gn->cohorts);
237 Lst_Free(gn->parents);
238 Lst_Free(gn->children);
239 Lst_Free(gn->order_succ);
240 Lst_Free(gn->order_pred);
241 HashTable_Done(&gn->context);
242 Lst_Free(gn->commands);
243
244 /* XXX: does gn->suffix need to be freed? It is reference-counted. */
245
246 free(gn);
247 }
248 #endif
249
250 /* Get the existing global node, or return NULL. */
251 GNode *
252 Targ_FindNode(const char *name)
253 {
254 return HashTable_FindValue(&targets, name);
255 }
256
257 /* Get the existing global node, or create it. */
258 GNode *
259 Targ_GetNode(const char *name)
260 {
261 Boolean isNew;
262 HashEntry *he = HashTable_CreateEntry(&targets, name, &isNew);
263 if (!isNew)
264 return HashEntry_Get(he);
265
266 {
267 GNode *gn = Targ_NewInternalNode(name);
268 HashEntry_Set(he, gn);
269 return gn;
270 }
271 }
272
273 /*
274 * Create a node, register it in .ALLTARGETS but don't store it in the
275 * table of global nodes. This means it cannot be found by name.
276 *
277 * This is used for internal nodes, such as cohorts or .WAIT nodes.
278 */
279 GNode *
280 Targ_NewInternalNode(const char *name)
281 {
282 GNode *gn = GNode_New(name);
283 Var_Append(".ALLTARGETS", name, VAR_GLOBAL);
284 Lst_Append(allTargets, gn);
285 if (doing_depend)
286 gn->flags |= FROM_DEPEND;
287 return gn;
288 }
289
290 /*
291 * Return the .END node, which contains the commands to be run when
292 * everything else has been made.
293 */
294 GNode *Targ_GetEndNode(void)
295 {
296 /* Save the node locally to avoid having to search for it all the time. */
297 static GNode *endNode = NULL;
298 if (endNode == NULL) {
299 endNode = Targ_GetNode(".END");
300 endNode->type = OP_SPECIAL;
301 }
302 return endNode;
303 }
304
305 /* Return the named nodes, creating them as necessary. */
306 GNodeList *
307 Targ_FindList(StringList *names)
308 {
309 StringListNode *ln;
310 GNodeList *nodes = Lst_New();
311 for (ln = names->first; ln != NULL; ln = ln->next) {
312 const char *name = ln->datum;
313 GNode *gn = Targ_GetNode(name);
314 Lst_Append(nodes, gn);
315 }
316 return nodes;
317 }
318
319 /* Return true if should ignore errors when creating gn. */
320 Boolean
321 Targ_Ignore(const GNode *gn)
322 {
323 return opts.ignoreErrors || gn->type & OP_IGNORE;
324 }
325
326 /* Return true if be silent when creating gn. */
327 Boolean
328 Targ_Silent(const GNode *gn)
329 {
330 return opts.beSilent || gn->type & OP_SILENT;
331 }
332
333 /* See if the given target is precious. */
334 Boolean
335 Targ_Precious(const GNode *gn)
336 {
337 /* XXX: Why are '::' targets precious? */
338 return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
339 }
340
341 /*
342 * The main target to be made; only for debugging output.
343 * See mainNode in parse.c for the definitive source.
344 */
345 static GNode *mainTarg;
346
347 /* Remember the main target to make; only used for debugging. */
348 void
349 Targ_SetMain(GNode *gn)
350 {
351 mainTarg = gn;
352 }
353
354 static void
355 PrintNodeNames(GNodeList *gnodes)
356 {
357 GNodeListNode *node;
358
359 for (node = gnodes->first; node != NULL; node = node->next) {
360 GNode *gn = node->datum;
361 debug_printf(" %s%s", gn->name, gn->cohort_num);
362 }
363 }
364
365 static void
366 PrintNodeNamesLine(const char *label, GNodeList *gnodes)
367 {
368 if (Lst_IsEmpty(gnodes))
369 return;
370 debug_printf("# %s:", label);
371 PrintNodeNames(gnodes);
372 debug_printf("\n");
373 }
374
375 void
376 Targ_PrintCmds(GNode *gn)
377 {
378 StringListNode *ln;
379 for (ln = gn->commands->first; ln != NULL; ln = ln->next) {
380 const char *cmd = ln->datum;
381 debug_printf("\t%s\n", cmd);
382 }
383 }
384
385 /* Format a modification time in some reasonable way and return it.
386 * The time is placed in a static area, so it is overwritten with each call. */
387 char *
388 Targ_FmtTime(time_t tm)
389 {
390 struct tm *parts;
391 static char buf[128];
392
393 parts = localtime(&tm);
394 (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
395 return buf;
396 }
397
398 /* Print out a type field giving only those attributes the user can set. */
399 void
400 Targ_PrintType(int type)
401 {
402 int tbit;
403
404 #define PRINTBIT(attr) case CONCAT(OP_,attr): debug_printf(" ." #attr); break
405 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))debug_printf(" ." #attr); break
406
407 type &= ~OP_OPMASK;
408
409 while (type) {
410 tbit = 1 << (ffs(type) - 1);
411 type &= ~tbit;
412
413 switch(tbit) {
414 PRINTBIT(OPTIONAL);
415 PRINTBIT(USE);
416 PRINTBIT(EXEC);
417 PRINTBIT(IGNORE);
418 PRINTBIT(PRECIOUS);
419 PRINTBIT(SILENT);
420 PRINTBIT(MAKE);
421 PRINTBIT(JOIN);
422 PRINTBIT(INVISIBLE);
423 PRINTBIT(NOTMAIN);
424 PRINTDBIT(LIB);
425 /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
426 case OP_MEMBER: if (DEBUG(TARG))debug_printf(" .MEMBER"); break;
427 PRINTDBIT(ARCHV);
428 PRINTDBIT(MADE);
429 PRINTDBIT(PHONY);
430 }
431 }
432 }
433
434 static const char *
435 made_name(GNodeMade made)
436 {
437 switch (made) {
438 case UNMADE: return "unmade";
439 case DEFERRED: return "deferred";
440 case REQUESTED: return "requested";
441 case BEINGMADE: return "being made";
442 case MADE: return "made";
443 case UPTODATE: return "up-to-date";
444 case ERROR: return "error when made";
445 case ABORTED: return "aborted";
446 default: return "unknown enum_made value";
447 }
448 }
449
450 static const char *
451 GNode_OpName(const GNode *gn)
452 {
453 switch (gn->type & OP_OPMASK) {
454 case OP_DEPENDS:
455 return ":";
456 case OP_FORCE:
457 return "!";
458 case OP_DOUBLEDEP:
459 return "::";
460 }
461 return "";
462 }
463
464 /* Print the contents of a node. */
465 void
466 Targ_PrintNode(GNode *gn, int pass)
467 {
468 debug_printf("# %s%s", gn->name, gn->cohort_num);
469 GNode_FprintDetails(opts.debug_file, ", ", gn, "\n");
470 if (gn->flags == 0)
471 return;
472
473 if (GNode_IsTarget(gn)) {
474 debug_printf("#\n");
475 if (gn == mainTarg) {
476 debug_printf("# *** MAIN TARGET ***\n");
477 }
478 if (pass >= 2) {
479 if (gn->unmade) {
480 debug_printf("# %d unmade children\n", gn->unmade);
481 } else {
482 debug_printf("# No unmade children\n");
483 }
484 if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
485 if (gn->mtime != 0) {
486 debug_printf("# last modified %s: %s\n",
487 Targ_FmtTime(gn->mtime),
488 made_name(gn->made));
489 } else if (gn->made != UNMADE) {
490 debug_printf("# non-existent (maybe): %s\n",
491 made_name(gn->made));
492 } else {
493 debug_printf("# unmade\n");
494 }
495 }
496 PrintNodeNamesLine("implicit parents", gn->implicitParents);
497 } else {
498 if (gn->unmade)
499 debug_printf("# %d unmade children\n", gn->unmade);
500 }
501 PrintNodeNamesLine("parents", gn->parents);
502 PrintNodeNamesLine("order_pred", gn->order_pred);
503 PrintNodeNamesLine("order_succ", gn->order_succ);
504
505 debug_printf("%-16s%s", gn->name, GNode_OpName(gn));
506 Targ_PrintType(gn->type);
507 PrintNodeNames(gn->children);
508 debug_printf("\n");
509 Targ_PrintCmds(gn);
510 debug_printf("\n\n");
511 if (gn->type & OP_DOUBLEDEP) {
512 Targ_PrintNodes(gn->cohorts, pass);
513 }
514 }
515 }
516
517 void
518 Targ_PrintNodes(GNodeList *gnodes, int pass)
519 {
520 GNodeListNode *ln;
521 for (ln = gnodes->first; ln != NULL; ln = ln->next)
522 Targ_PrintNode(ln->datum, pass);
523 }
524
525 /* Print only those targets that are just a source. */
526 static void
527 PrintOnlySources(void)
528 {
529 GNodeListNode *ln;
530
531 for (ln = allTargets->first; ln != NULL; ln = ln->next) {
532 GNode *gn = ln->datum;
533 if (GNode_IsTarget(gn))
534 continue;
535
536 debug_printf("#\t%s [%s]", gn->name, GNode_Path(gn));
537 Targ_PrintType(gn->type);
538 debug_printf("\n");
539 }
540 }
541
542 /* Input:
543 * pass 1 => before processing
544 * 2 => after processing
545 * 3 => after processing, an error occurred
546 */
547 void
548 Targ_PrintGraph(int pass)
549 {
550 debug_printf("#*** Input graph:\n");
551 Targ_PrintNodes(allTargets, pass);
552 debug_printf("\n\n");
553 debug_printf("#\n# Files that are only sources:\n");
554 PrintOnlySources();
555 debug_printf("#*** Global Variables:\n");
556 Var_Dump(VAR_GLOBAL);
557 debug_printf("#*** Command-line Variables:\n");
558 Var_Dump(VAR_CMDLINE);
559 debug_printf("\n");
560 Dir_PrintDirectories();
561 debug_printf("\n");
562 Suff_PrintAll();
563 }
564
565 /* Propagate some type information to cohort nodes (those from the '::'
566 * dependency operator).
567 *
568 * Should be called after the makefiles are parsed but before any action is
569 * taken. */
570 void
571 Targ_Propagate(void)
572 {
573 GNodeListNode *ln, *cln;
574
575 for (ln = allTargets->first; ln != NULL; ln = ln->next) {
576 GNode *gn = ln->datum;
577 GNodeType type = gn->type;
578
579 if (!(type & OP_DOUBLEDEP))
580 continue;
581
582 for (cln = gn->cohorts->first; cln != NULL; cln = cln->next) {
583 GNode *cohort = cln->datum;
584
585 cohort->type |= type & ~OP_OPMASK;
586 }
587 }
588 }
589