targ.c revision 1.92 1 /* $NetBSD: targ.c,v 1.92 2020/09/24 07:11:29 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.92 2020/09/24 07:11:29 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 = NULL; /* New or used hash entry for node */
260 Boolean isNew; /* Set TRUE if Hash_CreateEntry had to create */
261 /* an entry for the node */
262
263 if (!(flags & (TARG_CREATE | TARG_NOHASH))) {
264 he = Hash_FindEntry(&targets, name);
265 if (he == NULL)
266 return NULL;
267 return (GNode *)Hash_GetValue(he);
268 }
269
270 if (!(flags & TARG_NOHASH)) {
271 he = Hash_CreateEntry(&targets, name, &isNew);
272 if (!isNew)
273 return (GNode *)Hash_GetValue(he);
274 }
275
276 gn = Targ_NewGN(name);
277 if (!(flags & TARG_NOHASH))
278 Hash_SetValue(he, gn);
279 Var_Append(".ALLTARGETS", name, VAR_GLOBAL);
280 Lst_Append(allTargets, gn);
281 if (doing_depend)
282 gn->flags |= FROM_DEPEND;
283 return gn;
284 }
285
286 /* Return the .END node, which contains the commands to be executed when
287 * everything else is done. */
288 GNode *Targ_GetEndNode(void)
289 {
290 /* Save the node locally to avoid having to search for it all the time. */
291 static GNode *endNode = NULL;
292 if (endNode == NULL) {
293 endNode = Targ_FindNode(".END", TARG_CREATE);
294 endNode->type = OP_SPECIAL;
295 }
296 return endNode;
297 }
298
299 /* Make a complete list of GNodes from the given list of names.
300 * If flags is TARG_CREATE, nodes will be created for all names in
301 * names which do not yet have graph nodes. If flags is TARG_NOCREATE,
302 * an error message will be printed for each name which can't be found.
303 *
304 * Input:
305 * name list of names to find
306 * flags flags used if no node is found for a given name
307 *
308 * Results:
309 * A complete list of graph nodes corresponding to all instances of all
310 * the names in names.
311 */
312 GNodeList *
313 Targ_FindList(StringList *names, int flags)
314 {
315 GNodeList *nodes;
316 StringListNode *ln;
317
318 nodes = Lst_Init();
319
320 Lst_Open(names);
321 while ((ln = Lst_Next(names)) != NULL) {
322 char *name = LstNode_Datum(ln);
323 GNode *gn = Targ_FindNode(name, flags);
324 if (gn != NULL) {
325 Lst_Append(nodes, gn);
326 } else if (flags == TARG_NOCREATE) {
327 Error("\"%s\" -- target unknown.", name);
328 }
329 }
330 Lst_Close(names);
331 return nodes;
332 }
333
334 /* Return true if should ignore errors when creating gn. */
335 Boolean
336 Targ_Ignore(GNode *gn)
337 {
338 return ignoreErrors || gn->type & OP_IGNORE;
339 }
340
341 /* Return true if be silent when creating gn. */
342 Boolean
343 Targ_Silent(GNode *gn)
344 {
345 return beSilent || gn->type & OP_SILENT;
346 }
347
348 /* See if the given target is precious. */
349 Boolean
350 Targ_Precious(GNode *gn)
351 {
352 return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
353 }
354
355 /******************* DEBUG INFO PRINTING ****************/
356
357 static GNode *mainTarg; /* the main target, as set by Targ_SetMain */
358
359 /* Set our idea of the main target we'll be creating. Used for debugging
360 * output. */
361 void
362 Targ_SetMain(GNode *gn)
363 {
364 mainTarg = gn;
365 }
366
367 static void
368 PrintNodeNames(GNodeList *gnodes)
369 {
370 GNodeListNode *node;
371
372 for (node = Lst_First(gnodes); node != NULL; node = LstNode_Next(node)) {
373 GNode *gn = LstNode_Datum(node);
374 fprintf(debug_file, " %s%s", gn->name, gn->cohort_num);
375 }
376 }
377
378 static void
379 PrintNodeNamesLine(const char *label, GNodeList *gnodes)
380 {
381 if (Lst_IsEmpty(gnodes))
382 return;
383 fprintf(debug_file, "# %s:", label);
384 PrintNodeNames(gnodes);
385 fprintf(debug_file, "\n");
386 }
387
388 static int
389 TargPrintCmd(void *cmd, void *dummy MAKE_ATTR_UNUSED)
390 {
391 fprintf(debug_file, "\t%s\n", (char *)cmd);
392 return 0;
393 }
394
395 void
396 Targ_PrintCmds(GNode *gn)
397 {
398 Lst_ForEachUntil(gn->commands, TargPrintCmd, NULL);
399 }
400
401 /* Format a modification time in some reasonable way and return it.
402 * The time is placed in a static area, so it is overwritten with each call. */
403 char *
404 Targ_FmtTime(time_t tm)
405 {
406 struct tm *parts;
407 static char buf[128];
408
409 parts = localtime(&tm);
410 (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
411 return buf;
412 }
413
414 /* Print out a type field giving only those attributes the user can set. */
415 void
416 Targ_PrintType(int type)
417 {
418 int tbit;
419
420 #define PRINTBIT(attr) case CONCAT(OP_,attr): fprintf(debug_file, " ." #attr); break
421 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))fprintf(debug_file, " ." #attr); break
422
423 type &= ~OP_OPMASK;
424
425 while (type) {
426 tbit = 1 << (ffs(type) - 1);
427 type &= ~tbit;
428
429 switch(tbit) {
430 PRINTBIT(OPTIONAL);
431 PRINTBIT(USE);
432 PRINTBIT(EXEC);
433 PRINTBIT(IGNORE);
434 PRINTBIT(PRECIOUS);
435 PRINTBIT(SILENT);
436 PRINTBIT(MAKE);
437 PRINTBIT(JOIN);
438 PRINTBIT(INVISIBLE);
439 PRINTBIT(NOTMAIN);
440 PRINTDBIT(LIB);
441 /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
442 case OP_MEMBER: if (DEBUG(TARG))fprintf(debug_file, " .MEMBER"); break;
443 PRINTDBIT(ARCHV);
444 PRINTDBIT(MADE);
445 PRINTDBIT(PHONY);
446 }
447 }
448 }
449
450 static const char *
451 made_name(GNodeMade made)
452 {
453 switch (made) {
454 case UNMADE: return "unmade";
455 case DEFERRED: return "deferred";
456 case REQUESTED: return "requested";
457 case BEINGMADE: return "being made";
458 case MADE: return "made";
459 case UPTODATE: return "up-to-date";
460 case ERROR: return "error when made";
461 case ABORTED: return "aborted";
462 default: return "unknown enum_made value";
463 }
464 }
465
466 static int
467 PrintNode(void *gnp, void *passp)
468 {
469 GNode *gn = gnp;
470 int pass = *(const int *)passp;
471
472 fprintf(debug_file, "# %s%s", gn->name, gn->cohort_num);
473 GNode_FprintDetails(debug_file, ", ", gn, "\n");
474 if (gn->flags == 0)
475 return 0;
476
477 if (!OP_NOP(gn->type)) {
478 fprintf(debug_file, "#\n");
479 if (gn == mainTarg) {
480 fprintf(debug_file, "# *** MAIN TARGET ***\n");
481 }
482 if (pass >= 2) {
483 if (gn->unmade) {
484 fprintf(debug_file, "# %d unmade children\n", gn->unmade);
485 } else {
486 fprintf(debug_file, "# No unmade children\n");
487 }
488 if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
489 if (gn->mtime != 0) {
490 fprintf(debug_file, "# last modified %s: %s\n",
491 Targ_FmtTime(gn->mtime),
492 made_name(gn->made));
493 } else if (gn->made != UNMADE) {
494 fprintf(debug_file, "# non-existent (maybe): %s\n",
495 made_name(gn->made));
496 } else {
497 fprintf(debug_file, "# unmade\n");
498 }
499 }
500 PrintNodeNamesLine("implicit parents", gn->implicitParents);
501 } else {
502 if (gn->unmade)
503 fprintf(debug_file, "# %d unmade children\n", gn->unmade);
504 }
505 PrintNodeNamesLine("parents", gn->parents);
506 PrintNodeNamesLine("order_pred", gn->order_pred);
507 PrintNodeNamesLine("order_succ", gn->order_succ);
508
509 fprintf(debug_file, "%-16s", gn->name);
510 switch (gn->type & OP_OPMASK) {
511 case OP_DEPENDS:
512 fprintf(debug_file, ":"); break;
513 case OP_FORCE:
514 fprintf(debug_file, "!"); break;
515 case OP_DOUBLEDEP:
516 fprintf(debug_file, "::"); break;
517 }
518 Targ_PrintType(gn->type);
519 PrintNodeNames(gn->children);
520 fprintf(debug_file, "\n");
521 Targ_PrintCmds(gn);
522 fprintf(debug_file, "\n\n");
523 if (gn->type & OP_DOUBLEDEP) {
524 Lst_ForEachUntil(gn->cohorts, PrintNode, passp);
525 }
526 }
527 return 0;
528 }
529
530 /* Print the contents of a node. */
531 void
532 Targ_PrintNode(GNode *gn, int pass)
533 {
534 PrintNode(gn, &pass);
535 }
536
537 void
538 Targ_PrintNodes(GNodeList *gnodes, int pass)
539 {
540 Lst_ForEachUntil(gnodes, PrintNode, &pass);
541 }
542
543 /* Print only those targets that are just a source.
544 * The name of each file is printed, preceded by #\t. */
545 static int
546 TargPrintOnlySrc(void *gnp, void *dummy MAKE_ATTR_UNUSED)
547 {
548 GNode *gn = (GNode *)gnp;
549 if (!OP_NOP(gn->type))
550 return 0;
551
552 fprintf(debug_file, "#\t%s [%s]",
553 gn->name, gn->path ? gn->path : gn->name);
554 Targ_PrintType(gn->type);
555 fprintf(debug_file, "\n");
556
557 return 0;
558 }
559
560 /* Input:
561 * pass 1 => before processing
562 * 2 => after processing
563 * 3 => after processing, an error occurred
564 */
565 void
566 Targ_PrintGraph(int pass)
567 {
568 fprintf(debug_file, "#*** Input graph:\n");
569 Lst_ForEachUntil(allTargets, PrintNode, &pass);
570 fprintf(debug_file, "\n\n");
571 fprintf(debug_file, "#\n# Files that are only sources:\n");
572 Lst_ForEachUntil(allTargets, TargPrintOnlySrc, NULL);
573 fprintf(debug_file, "#*** Global Variables:\n");
574 Var_Dump(VAR_GLOBAL);
575 fprintf(debug_file, "#*** Command-line Variables:\n");
576 Var_Dump(VAR_CMD);
577 fprintf(debug_file, "\n");
578 Dir_PrintDirectories();
579 fprintf(debug_file, "\n");
580 Suff_PrintAll();
581 }
582
583 /* Propagate some type information to cohort nodes (those from the ::
584 * dependency operator).
585 *
586 * Should be called after the makefiles are parsed but before any action is
587 * taken. */
588 void
589 Targ_Propagate(void)
590 {
591 GNodeListNode *pn, *cn;
592
593 for (pn = Lst_First(allTargets); pn != NULL; pn = LstNode_Next(pn)) {
594 GNode *pgn = LstNode_Datum(pn);
595
596 if (!(pgn->type & OP_DOUBLEDEP))
597 continue;
598
599 for (cn = Lst_First(pgn->cohorts); cn != NULL; cn = LstNode_Next(cn)) {
600 GNode *cgn = LstNode_Datum(cn);
601
602 cgn->type |= pgn->type & ~OP_OPMASK;
603 }
604 }
605 }
606