targ.c revision 1.94 1 /* $NetBSD: targ.c,v 1.94 2020/09/26 14:59:21 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. If a name doesn't exist and the
94 * TARG_NOCREATE flag was given, an error message
95 * is printed. Else, if a name doesn't exist,
96 * its node is created.
97 *
98 * Targ_Ignore Return TRUE if errors should be ignored when
99 * creating the given target.
100 *
101 * Targ_Silent Return TRUE if we should be silent when
102 * creating the given target.
103 *
104 * Targ_Precious Return TRUE if the target is precious and
105 * should not be removed if we are interrupted.
106 *
107 * Targ_Propagate Propagate information between related
108 * nodes. Should be called after the
109 * makefiles are parsed but before any
110 * action is taken.
111 *
112 * Debugging:
113 * Targ_PrintGraph Print out the entire graphm all variables
114 * and statistics for the directory cache. Should
115 * print something for suffixes, too, but...
116 */
117
118 #include <stdio.h>
119 #include <time.h>
120
121 #include "make.h"
122 #include "dir.h"
123
124 /* "@(#)targ.c 8.2 (Berkeley) 3/19/94" */
125 MAKE_RCSID("$NetBSD: targ.c,v 1.94 2020/09/26 14:59:21 rillig Exp $");
126
127 static GNodeList *allTargets; /* the list of all targets found so far */
128 #ifdef CLEANUP
129 static GNodeList *allGNs; /* List of all the GNodes */
130 #endif
131 static Hash_Table targets; /* a hash table of same */
132
133 static int TargPrintOnlySrc(void *, void *);
134 #ifdef CLEANUP
135 static void TargFreeGN(void *);
136 #endif
137
138 void
139 Targ_Init(void)
140 {
141 allTargets = Lst_Init();
142 Hash_InitTable(&targets);
143 }
144
145 void
146 Targ_End(void)
147 {
148 Targ_Stats();
149 #ifdef CLEANUP
150 Lst_Free(allTargets);
151 if (allGNs != NULL)
152 Lst_Destroy(allGNs, TargFreeGN);
153 Hash_DeleteTable(&targets);
154 #endif
155 }
156
157 void
158 Targ_Stats(void)
159 {
160 Hash_DebugStats(&targets, "targets");
161 }
162
163 /* Return the list of all targets. */
164 GNodeList *
165 Targ_List(void)
166 {
167 return allTargets;
168 }
169
170 /* Create and initialize a new graph node. The gnode is added to the list of
171 * all gnodes.
172 *
173 * Input:
174 * name the name of the node, such as "clean", "src.c", ".END"
175 */
176 GNode *
177 Targ_NewGN(const char *name)
178 {
179 GNode *gn;
180
181 gn = bmake_malloc(sizeof(GNode));
182 gn->name = bmake_strdup(name);
183 gn->uname = NULL;
184 gn->path = NULL;
185 gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : 0;
186 gn->unmade = 0;
187 gn->unmade_cohorts = 0;
188 gn->cohort_num[0] = 0;
189 gn->centurion = NULL;
190 gn->made = UNMADE;
191 gn->flags = 0;
192 gn->checked = 0;
193 gn->mtime = 0;
194 gn->cmgn = NULL;
195 gn->implicitParents = Lst_Init();
196 gn->cohorts = Lst_Init();
197 gn->parents = Lst_Init();
198 gn->children = Lst_Init();
199 gn->order_pred = Lst_Init();
200 gn->order_succ = Lst_Init();
201 Hash_InitTable(&gn->context);
202 gn->commands = Lst_Init();
203 gn->suffix = NULL;
204 gn->fname = NULL;
205 gn->lineno = 0;
206
207 #ifdef CLEANUP
208 if (allGNs == NULL)
209 allGNs = Lst_Init();
210 Lst_Append(allGNs, gn);
211 #endif
212
213 return gn;
214 }
215
216 #ifdef CLEANUP
217 static void
218 TargFreeGN(void *gnp)
219 {
220 GNode *gn = (GNode *)gnp;
221
222 free(gn->name);
223 free(gn->uname);
224 free(gn->path);
225
226 Lst_Free(gn->implicitParents);
227 Lst_Free(gn->cohorts);
228 Lst_Free(gn->parents);
229 Lst_Free(gn->children);
230 Lst_Free(gn->order_succ);
231 Lst_Free(gn->order_pred);
232 Hash_DeleteTable(&gn->context);
233 Lst_Free(gn->commands);
234
235 /* XXX: does gn->suffix need to be freed? It is reference-counted. */
236 /* gn->fname points to name allocated when file was opened, don't free */
237
238 free(gn);
239 }
240 #endif
241
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 GNode *
256 Targ_FindNode(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 /* Return the .END node, which contains the commands to be executed when
282 * everything else is done. */
283 GNode *Targ_GetEndNode(void)
284 {
285 /* Save the node locally to avoid having to search for it all the time. */
286 static GNode *endNode = NULL;
287 if (endNode == NULL) {
288 endNode = Targ_FindNode(".END", TARG_CREATE);
289 endNode->type = OP_SPECIAL;
290 }
291 return endNode;
292 }
293
294 /* Make a complete list of GNodes from the given list of names.
295 * If flags is TARG_CREATE, nodes will be created for all names in
296 * names which do not yet have graph nodes. If flags is TARG_NOCREATE,
297 * an error message will be printed for each name which can't be found.
298 *
299 * Input:
300 * name list of names to find
301 * flags flags used if no node is found for a given name
302 *
303 * Results:
304 * A complete list of graph nodes corresponding to all instances of all
305 * the names in names.
306 */
307 GNodeList *
308 Targ_FindList(StringList *names, int flags)
309 {
310 GNodeList *nodes;
311 StringListNode *ln;
312
313 nodes = Lst_Init();
314
315 Lst_Open(names);
316 while ((ln = Lst_Next(names)) != NULL) {
317 char *name = LstNode_Datum(ln);
318 GNode *gn = Targ_FindNode(name, flags);
319 if (gn != NULL) {
320 Lst_Append(nodes, gn);
321 } else if (flags == TARG_NOCREATE) {
322 Error("\"%s\" -- target unknown.", name);
323 }
324 }
325 Lst_Close(names);
326 return nodes;
327 }
328
329 /* Return true if should ignore errors when creating gn. */
330 Boolean
331 Targ_Ignore(GNode *gn)
332 {
333 return ignoreErrors || gn->type & OP_IGNORE;
334 }
335
336 /* Return true if be silent when creating gn. */
337 Boolean
338 Targ_Silent(GNode *gn)
339 {
340 return beSilent || gn->type & OP_SILENT;
341 }
342
343 /* See if the given target is precious. */
344 Boolean
345 Targ_Precious(GNode *gn)
346 {
347 return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
348 }
349
350 /******************* DEBUG INFO PRINTING ****************/
351
352 static GNode *mainTarg; /* the main target, as set by Targ_SetMain */
353
354 /* Set our idea of the main target we'll be creating. Used for debugging
355 * output. */
356 void
357 Targ_SetMain(GNode *gn)
358 {
359 mainTarg = gn;
360 }
361
362 static void
363 PrintNodeNames(GNodeList *gnodes)
364 {
365 GNodeListNode *node;
366
367 for (node = Lst_First(gnodes); node != NULL; node = LstNode_Next(node)) {
368 GNode *gn = LstNode_Datum(node);
369 fprintf(debug_file, " %s%s", gn->name, gn->cohort_num);
370 }
371 }
372
373 static void
374 PrintNodeNamesLine(const char *label, GNodeList *gnodes)
375 {
376 if (Lst_IsEmpty(gnodes))
377 return;
378 fprintf(debug_file, "# %s:", label);
379 PrintNodeNames(gnodes);
380 fprintf(debug_file, "\n");
381 }
382
383 void
384 Targ_PrintCmds(GNode *gn)
385 {
386 StringListNode *node = Lst_First(gn->commands);
387 for (; node != NULL; node = LstNode_Next(node)) {
388 const char *cmd = LstNode_Datum(node);
389 fprintf(debug_file, "\t%s\n", cmd);
390 }
391 }
392
393 /* Format a modification time in some reasonable way and return it.
394 * The time is placed in a static area, so it is overwritten with each call. */
395 char *
396 Targ_FmtTime(time_t tm)
397 {
398 struct tm *parts;
399 static char buf[128];
400
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): fprintf(debug_file, " ." #attr); break
413 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))fprintf(debug_file, " ." #attr); break
414
415 type &= ~OP_OPMASK;
416
417 while (type) {
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))fprintf(debug_file, " .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 int
459 PrintNode(void *gnp, void *passp)
460 {
461 GNode *gn = gnp;
462 int pass = *(const int *)passp;
463
464 fprintf(debug_file, "# %s%s", gn->name, gn->cohort_num);
465 GNode_FprintDetails(debug_file, ", ", gn, "\n");
466 if (gn->flags == 0)
467 return 0;
468
469 if (!OP_NOP(gn->type)) {
470 fprintf(debug_file, "#\n");
471 if (gn == mainTarg) {
472 fprintf(debug_file, "# *** MAIN TARGET ***\n");
473 }
474 if (pass >= 2) {
475 if (gn->unmade) {
476 fprintf(debug_file, "# %d unmade children\n", gn->unmade);
477 } else {
478 fprintf(debug_file, "# No unmade children\n");
479 }
480 if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
481 if (gn->mtime != 0) {
482 fprintf(debug_file, "# last modified %s: %s\n",
483 Targ_FmtTime(gn->mtime),
484 made_name(gn->made));
485 } else if (gn->made != UNMADE) {
486 fprintf(debug_file, "# non-existent (maybe): %s\n",
487 made_name(gn->made));
488 } else {
489 fprintf(debug_file, "# unmade\n");
490 }
491 }
492 PrintNodeNamesLine("implicit parents", gn->implicitParents);
493 } else {
494 if (gn->unmade)
495 fprintf(debug_file, "# %d unmade children\n", gn->unmade);
496 }
497 PrintNodeNamesLine("parents", gn->parents);
498 PrintNodeNamesLine("order_pred", gn->order_pred);
499 PrintNodeNamesLine("order_succ", gn->order_succ);
500
501 fprintf(debug_file, "%-16s", gn->name);
502 switch (gn->type & OP_OPMASK) {
503 case OP_DEPENDS:
504 fprintf(debug_file, ":"); break;
505 case OP_FORCE:
506 fprintf(debug_file, "!"); break;
507 case OP_DOUBLEDEP:
508 fprintf(debug_file, "::"); break;
509 }
510 Targ_PrintType(gn->type);
511 PrintNodeNames(gn->children);
512 fprintf(debug_file, "\n");
513 Targ_PrintCmds(gn);
514 fprintf(debug_file, "\n\n");
515 if (gn->type & OP_DOUBLEDEP) {
516 Lst_ForEachUntil(gn->cohorts, PrintNode, passp);
517 }
518 }
519 return 0;
520 }
521
522 /* Print the contents of a node. */
523 void
524 Targ_PrintNode(GNode *gn, int pass)
525 {
526 PrintNode(gn, &pass);
527 }
528
529 void
530 Targ_PrintNodes(GNodeList *gnodes, int pass)
531 {
532 Lst_ForEachUntil(gnodes, PrintNode, &pass);
533 }
534
535 /* Print only those targets that are just a source.
536 * The name of each file is printed, preceded by #\t. */
537 static int
538 TargPrintOnlySrc(void *gnp, void *dummy MAKE_ATTR_UNUSED)
539 {
540 GNode *gn = (GNode *)gnp;
541 if (!OP_NOP(gn->type))
542 return 0;
543
544 fprintf(debug_file, "#\t%s [%s]",
545 gn->name, gn->path ? gn->path : gn->name);
546 Targ_PrintType(gn->type);
547 fprintf(debug_file, "\n");
548
549 return 0;
550 }
551
552 /* Input:
553 * pass 1 => before processing
554 * 2 => after processing
555 * 3 => after processing, an error occurred
556 */
557 void
558 Targ_PrintGraph(int pass)
559 {
560 fprintf(debug_file, "#*** Input graph:\n");
561 Lst_ForEachUntil(allTargets, PrintNode, &pass);
562 fprintf(debug_file, "\n\n");
563 fprintf(debug_file, "#\n# Files that are only sources:\n");
564 Lst_ForEachUntil(allTargets, TargPrintOnlySrc, NULL);
565 fprintf(debug_file, "#*** Global Variables:\n");
566 Var_Dump(VAR_GLOBAL);
567 fprintf(debug_file, "#*** Command-line Variables:\n");
568 Var_Dump(VAR_CMD);
569 fprintf(debug_file, "\n");
570 Dir_PrintDirectories();
571 fprintf(debug_file, "\n");
572 Suff_PrintAll();
573 }
574
575 /* Propagate some type information to cohort nodes (those from the ::
576 * dependency operator).
577 *
578 * Should be called after the makefiles are parsed but before any action is
579 * taken. */
580 void
581 Targ_Propagate(void)
582 {
583 GNodeListNode *pn, *cn;
584
585 for (pn = Lst_First(allTargets); pn != NULL; pn = LstNode_Next(pn)) {
586 GNode *pgn = LstNode_Datum(pn);
587
588 if (!(pgn->type & OP_DOUBLEDEP))
589 continue;
590
591 for (cn = Lst_First(pgn->cohorts); cn != NULL; cn = LstNode_Next(cn)) {
592 GNode *cgn = LstNode_Datum(cn);
593
594 cgn->type |= pgn->type & ~OP_OPMASK;
595 }
596 }
597 }
598