targ.c revision 1.103 1 /* $NetBSD: targ.c,v 1.103 2020/09/27 01:07:12 mrg 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 * targ.c --
73 * Functions for maintaining the Lst allTargets. Target nodes are
74 * kept in two structures: a Lst and a hash table.
75 *
76 * Interface:
77 * Targ_Init Initialization procedure.
78 *
79 * Targ_End Cleanup the module
80 *
81 * Targ_List Return the list of all targets so far.
82 *
83 * Targ_NewGN Create a new GNode for the passed target
84 * (string). The node is *not* placed in the
85 * hash table, though all its fields are
86 * initialized.
87 *
88 * Targ_FindNode Find the node, or return NULL.
89 *
90 * Targ_GetNode Find the node, or create it.
91 *
92 * Targ_NewInternalNode Create an internal node.
93 *
94 * Targ_FindList Given a list of names, find nodes for all
95 * of them, creating them as necessary.
96 *
97 * Targ_Ignore Return TRUE if errors should be ignored when
98 * creating the given target.
99 *
100 * Targ_Silent Return TRUE if we should be silent when
101 * creating the given target.
102 *
103 * Targ_Precious Return TRUE if the target is precious and
104 * should not be removed if we are interrupted.
105 *
106 * Targ_Propagate Propagate information between related
107 * nodes. Should be called after the
108 * makefiles are parsed but before any
109 * action is taken.
110 *
111 * Debugging:
112 * Targ_PrintGraph Print out the entire graphm all variables
113 * and statistics for the directory cache. Should
114 * print something for suffixes, too, but...
115 */
116
117 #include <stdio.h>
118 #include <time.h>
119
120 #include "make.h"
121 #include "dir.h"
122
123 /* "@(#)targ.c 8.2 (Berkeley) 3/19/94" */
124 MAKE_RCSID("$NetBSD: targ.c,v 1.103 2020/09/27 01:07:12 mrg Exp $");
125
126 static GNodeList *allTargets; /* the list of all targets found so far */
127 #ifdef CLEANUP
128 static GNodeList *allGNs; /* List of all the GNodes */
129 #endif
130 static Hash_Table targets; /* a hash table of same */
131
132 #ifdef CLEANUP
133 static void TargFreeGN(void *);
134 #endif
135
136 void
137 Targ_Init(void)
138 {
139 allTargets = Lst_Init();
140 Hash_InitTable(&targets);
141 }
142
143 void
144 Targ_End(void)
145 {
146 Targ_Stats();
147 #ifdef CLEANUP
148 Lst_Free(allTargets);
149 if (allGNs != NULL)
150 Lst_Destroy(allGNs, TargFreeGN);
151 Hash_DeleteTable(&targets);
152 #endif
153 }
154
155 void
156 Targ_Stats(void)
157 {
158 Hash_DebugStats(&targets, "targets");
159 }
160
161 /* Return the list of all targets. */
162 GNodeList *
163 Targ_List(void)
164 {
165 return allTargets;
166 }
167
168 /* Create and initialize a new graph node. The gnode is added to the list of
169 * all gnodes.
170 *
171 * Input:
172 * name the name of the node, such as "clean", "src.c", ".END"
173 */
174 GNode *
175 Targ_NewGN(const char *name)
176 {
177 GNode *gn;
178
179 gn = bmake_malloc(sizeof(GNode));
180 gn->name = bmake_strdup(name);
181 gn->uname = NULL;
182 gn->path = NULL;
183 gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : 0;
184 gn->unmade = 0;
185 gn->unmade_cohorts = 0;
186 gn->cohort_num[0] = 0;
187 gn->centurion = NULL;
188 gn->made = UNMADE;
189 gn->flags = 0;
190 gn->checked = 0;
191 gn->mtime = 0;
192 gn->cmgn = NULL;
193 gn->implicitParents = Lst_Init();
194 gn->cohorts = Lst_Init();
195 gn->parents = Lst_Init();
196 gn->children = Lst_Init();
197 gn->order_pred = Lst_Init();
198 gn->order_succ = Lst_Init();
199 Hash_InitTable(&gn->context);
200 gn->commands = Lst_Init();
201 gn->suffix = NULL;
202 gn->fname = NULL;
203 gn->lineno = 0;
204
205 #ifdef CLEANUP
206 if (allGNs == NULL)
207 allGNs = Lst_Init();
208 Lst_Append(allGNs, gn);
209 #endif
210
211 return gn;
212 }
213
214 #ifdef CLEANUP
215 static void
216 TargFreeGN(void *gnp)
217 {
218 GNode *gn = (GNode *)gnp;
219
220 free(gn->name);
221 free(gn->uname);
222 free(gn->path);
223
224 Lst_Free(gn->implicitParents);
225 Lst_Free(gn->cohorts);
226 Lst_Free(gn->parents);
227 Lst_Free(gn->children);
228 Lst_Free(gn->order_succ);
229 Lst_Free(gn->order_pred);
230 Hash_DeleteTable(&gn->context);
231 Lst_Free(gn->commands);
232
233 /* XXX: does gn->suffix need to be freed? It is reference-counted. */
234
235 free(gn);
236 }
237 #endif
238
239 /* Get the existing global node, or return NULL. */
240 GNode *
241 Targ_FindNode(const char *name)
242 {
243 return Hash_FindValue(&targets, name);
244 }
245
246 /* Get the existing global node, or create it. */
247 GNode *
248 Targ_GetNode(const char *name)
249 {
250 Boolean isNew;
251 Hash_Entry *he = Hash_CreateEntry(&targets, name, &isNew);
252 if (!isNew)
253 return Hash_GetValue(he);
254
255 {
256 GNode *gn = Targ_NewInternalNode(name);
257 Hash_SetValue(he, gn);
258 return gn;
259 }
260 }
261
262 /* Create a node, register it in .ALLTARGETS but don't store it in the
263 * table of global nodes. This means it cannot be found by name.
264 *
265 * This is used for internal nodes, such as cohorts or .WAIT nodes. */
266 GNode *
267 Targ_NewInternalNode(const char *name)
268 {
269 GNode *gn = Targ_NewGN(name);
270 Var_Append(".ALLTARGETS", name, VAR_GLOBAL);
271 Lst_Append(allTargets, gn);
272 if (doing_depend)
273 gn->flags |= FROM_DEPEND;
274 return gn;
275 }
276
277 /* Return the .END node, which contains the commands to be executed when
278 * everything else is done. */
279 GNode *Targ_GetEndNode(void)
280 {
281 /* Save the node locally to avoid having to search for it all the time. */
282 static GNode *endNode = NULL;
283 if (endNode == NULL) {
284 endNode = Targ_GetNode(".END");
285 endNode->type = OP_SPECIAL;
286 }
287 return endNode;
288 }
289
290 /* Return the named nodes, creating them as necessary. */
291 GNodeList *
292 Targ_FindList(StringList *names)
293 {
294 GNodeList *nodes;
295 StringListNode *ln;
296
297 nodes = Lst_Init();
298
299 Lst_Open(names);
300 while ((ln = Lst_Next(names)) != NULL) {
301 char *name = LstNode_Datum(ln);
302 GNode *gn = Targ_GetNode(name);
303 Lst_Append(nodes, gn);
304 }
305 Lst_Close(names);
306 return nodes;
307 }
308
309 /* Return true if should ignore errors when creating gn. */
310 Boolean
311 Targ_Ignore(GNode *gn)
312 {
313 return ignoreErrors || gn->type & OP_IGNORE;
314 }
315
316 /* Return true if be silent when creating gn. */
317 Boolean
318 Targ_Silent(GNode *gn)
319 {
320 return beSilent || gn->type & OP_SILENT;
321 }
322
323 /* See if the given target is precious. */
324 Boolean
325 Targ_Precious(GNode *gn)
326 {
327 return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
328 }
329
330 /******************* DEBUG INFO PRINTING ****************/
331
332 static GNode *mainTarg; /* the main target, as set by Targ_SetMain */
333
334 /* Set our idea of the main target we'll be creating. Used for debugging
335 * output. */
336 void
337 Targ_SetMain(GNode *gn)
338 {
339 mainTarg = gn;
340 }
341
342 static void
343 PrintNodeNames(GNodeList *gnodes)
344 {
345 GNodeListNode *node;
346
347 for (node = gnodes->first; node != NULL; node = node->next) {
348 GNode *gn = node->datum;
349 fprintf(debug_file, " %s%s", gn->name, gn->cohort_num);
350 }
351 }
352
353 static void
354 PrintNodeNamesLine(const char *label, GNodeList *gnodes)
355 {
356 if (Lst_IsEmpty(gnodes))
357 return;
358 fprintf(debug_file, "# %s:", label);
359 PrintNodeNames(gnodes);
360 fprintf(debug_file, "\n");
361 }
362
363 void
364 Targ_PrintCmds(GNode *gn)
365 {
366 StringListNode *node = gn->commands->first;
367 for (; node != NULL; node = node->next) {
368 const char *cmd = LstNode_Datum(node);
369 fprintf(debug_file, "\t%s\n", cmd);
370 }
371 }
372
373 /* Format a modification time in some reasonable way and return it.
374 * The time is placed in a static area, so it is overwritten with each call. */
375 char *
376 Targ_FmtTime(time_t tm)
377 {
378 struct tm *parts;
379 static char buf[128];
380
381 parts = localtime(&tm);
382 (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
383 return buf;
384 }
385
386 /* Print out a type field giving only those attributes the user can set. */
387 void
388 Targ_PrintType(int type)
389 {
390 int tbit;
391
392 #define PRINTBIT(attr) case CONCAT(OP_,attr): fprintf(debug_file, " ." #attr); break
393 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))fprintf(debug_file, " ." #attr); break
394
395 type &= ~OP_OPMASK;
396
397 while (type) {
398 tbit = 1 << (ffs(type) - 1);
399 type &= ~tbit;
400
401 switch(tbit) {
402 PRINTBIT(OPTIONAL);
403 PRINTBIT(USE);
404 PRINTBIT(EXEC);
405 PRINTBIT(IGNORE);
406 PRINTBIT(PRECIOUS);
407 PRINTBIT(SILENT);
408 PRINTBIT(MAKE);
409 PRINTBIT(JOIN);
410 PRINTBIT(INVISIBLE);
411 PRINTBIT(NOTMAIN);
412 PRINTDBIT(LIB);
413 /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
414 case OP_MEMBER: if (DEBUG(TARG))fprintf(debug_file, " .MEMBER"); break;
415 PRINTDBIT(ARCHV);
416 PRINTDBIT(MADE);
417 PRINTDBIT(PHONY);
418 }
419 }
420 }
421
422 static const char *
423 made_name(GNodeMade made)
424 {
425 switch (made) {
426 case UNMADE: return "unmade";
427 case DEFERRED: return "deferred";
428 case REQUESTED: return "requested";
429 case BEINGMADE: return "being made";
430 case MADE: return "made";
431 case UPTODATE: return "up-to-date";
432 case ERROR: return "error when made";
433 case ABORTED: return "aborted";
434 default: return "unknown enum_made value";
435 }
436 }
437
438 static int
439 PrintNode(void *gnp, void *passp)
440 {
441 GNode *gn = gnp;
442 int pass = *(const int *)passp;
443
444 fprintf(debug_file, "# %s%s", gn->name, gn->cohort_num);
445 GNode_FprintDetails(debug_file, ", ", gn, "\n");
446 if (gn->flags == 0)
447 return 0;
448
449 if (!OP_NOP(gn->type)) {
450 fprintf(debug_file, "#\n");
451 if (gn == mainTarg) {
452 fprintf(debug_file, "# *** MAIN TARGET ***\n");
453 }
454 if (pass >= 2) {
455 if (gn->unmade) {
456 fprintf(debug_file, "# %d unmade children\n", gn->unmade);
457 } else {
458 fprintf(debug_file, "# No unmade children\n");
459 }
460 if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
461 if (gn->mtime != 0) {
462 fprintf(debug_file, "# last modified %s: %s\n",
463 Targ_FmtTime(gn->mtime),
464 made_name(gn->made));
465 } else if (gn->made != UNMADE) {
466 fprintf(debug_file, "# non-existent (maybe): %s\n",
467 made_name(gn->made));
468 } else {
469 fprintf(debug_file, "# unmade\n");
470 }
471 }
472 PrintNodeNamesLine("implicit parents", gn->implicitParents);
473 } else {
474 if (gn->unmade)
475 fprintf(debug_file, "# %d unmade children\n", gn->unmade);
476 }
477 PrintNodeNamesLine("parents", gn->parents);
478 PrintNodeNamesLine("order_pred", gn->order_pred);
479 PrintNodeNamesLine("order_succ", gn->order_succ);
480
481 fprintf(debug_file, "%-16s", gn->name);
482 switch (gn->type & OP_OPMASK) {
483 case OP_DEPENDS:
484 fprintf(debug_file, ":"); break;
485 case OP_FORCE:
486 fprintf(debug_file, "!"); break;
487 case OP_DOUBLEDEP:
488 fprintf(debug_file, "::"); break;
489 }
490 Targ_PrintType(gn->type);
491 PrintNodeNames(gn->children);
492 fprintf(debug_file, "\n");
493 Targ_PrintCmds(gn);
494 fprintf(debug_file, "\n\n");
495 if (gn->type & OP_DOUBLEDEP) {
496 Lst_ForEachUntil(gn->cohorts, PrintNode, passp);
497 }
498 }
499 return 0;
500 }
501
502 /* Print the contents of a node. */
503 void
504 Targ_PrintNode(GNode *gn, int pass)
505 {
506 PrintNode(gn, &pass);
507 }
508
509 void
510 Targ_PrintNodes(GNodeList *gnodes, int pass)
511 {
512 Lst_ForEachUntil(gnodes, PrintNode, &pass);
513 }
514
515 /* Print only those targets that are just a source. */
516 static void
517 PrintOnlySources(void)
518 {
519 GNodeListNode *ln;
520
521 for (ln = allTargets->first; ln != NULL; ln = ln->next) {
522 GNode *gn = ln->datum;
523 if (!OP_NOP(gn->type))
524 continue;
525
526 fprintf(debug_file, "#\t%s [%s]",
527 gn->name, gn->path ? gn->path : gn->name);
528 Targ_PrintType(gn->type);
529 fprintf(debug_file, "\n");
530 }
531 }
532
533 /* Input:
534 * pass 1 => before processing
535 * 2 => after processing
536 * 3 => after processing, an error occurred
537 */
538 void
539 Targ_PrintGraph(int pass)
540 {
541 fprintf(debug_file, "#*** Input graph:\n");
542 Targ_PrintNodes(allTargets, pass);
543 fprintf(debug_file, "\n\n");
544 fprintf(debug_file, "#\n# Files that are only sources:\n");
545 PrintOnlySources();
546 fprintf(debug_file, "#*** Global Variables:\n");
547 Var_Dump(VAR_GLOBAL);
548 fprintf(debug_file, "#*** Command-line Variables:\n");
549 Var_Dump(VAR_CMD);
550 fprintf(debug_file, "\n");
551 Dir_PrintDirectories();
552 fprintf(debug_file, "\n");
553 Suff_PrintAll();
554 }
555
556 /* Propagate some type information to cohort nodes (those from the ::
557 * dependency operator).
558 *
559 * Should be called after the makefiles are parsed but before any action is
560 * taken. */
561 void
562 Targ_Propagate(void)
563 {
564 GNodeListNode *pn, *cn;
565
566 for (pn = allTargets->first; pn != NULL; pn = pn->next) {
567 GNode *pgn = pn->datum;
568
569 if (!(pgn->type & OP_DOUBLEDEP))
570 continue;
571
572 for (cn = pgn->cohorts->first; cn != NULL; cn = cn->next) {
573 GNode *cgn = cn->datum;
574
575 cgn->type |= pgn->type & ~OP_OPMASK;
576 }
577 }
578 }
579