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