1 /*-
2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
5 * 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)targ.c 8.2 (Berkeley) 3/19/94
39 */
40
41 #include <sys/cdefs.h>
42
43 /*
44 * Functions for maintaining the Lst allTargets. Target nodes are
45 * kept in two structures: a Lst, maintained by the list library, and a
46 * hash table, maintained by the hash library.
47 *
48 * Interface:
49 * Targ_Init Initialization procedure.
50 *
51 * Targ_NewGN Create a new GNode for the passed target (string).
52 * The node is *not* placed in the hash table, though all
53 * its fields are initialized.
54 *
55 * Targ_FindNode Find the node for a given target, creating and storing
56 * it if it doesn't exist and the flags are right
57 * (TARG_CREATE)
58 *
59 * Targ_FindList Given a list of names, find nodes for all of them. If a
60 * name doesn't exist and the TARG_NOCREATE flag was given,
61 * an error message is printed. Else, if a name doesn't
62 * exist, its node is created.
63 *
64 * Targ_Ignore Return TRUE if errors should be ignored when creating
65 * the given target.
66 *
67 * Targ_Silent Return TRUE if we should be silent when creating the
68 * given target.
69 *
70 * Targ_Precious Return TRUE if the target is precious and should not
71 * be removed if we are interrupted.
72 *
73 * Debugging:
74 * Targ_PrintGraph Print out the entire graphm all variables and statistics
75 * for the directory cache. Should print something for
76 * suffixes, too, but...
77 */
78
79 #include <stdio.h>
80
81 #include "dir.h"
82 #include "globals.h"
83 #include "GNode.h"
84 #include "hash.h"
85 #include "suff.h"
86 #include "targ.h"
87 #include "util.h"
88 #include "var.h"
89
90 /* the list of all targets found so far */
91 static Lst allTargets = Lst_Initializer(allTargets);
92
93 static Hash_Table targets; /* a hash table of same */
94
95 #define HTSIZE 191 /* initial size of hash table */
96
97 /**
98 * Targ_Init
99 * Initialize this module
100 *
101 * Side Effects:
102 * The allTargets list and the targets hash table are initialized
103 */
104 void
Targ_Init(void)105 Targ_Init(void)
106 {
107
108 Hash_InitTable(&targets, HTSIZE);
109 }
110
111 /**
112 * Targ_NewGN
113 * Create and initialize a new graph node
114 *
115 * Results:
116 * An initialized graph node with the name field filled with a copy
117 * of the passed name
118 *
119 * Side Effects:
120 * The gnode is added to the list of all gnodes.
121 */
122 GNode *
Targ_NewGN(const char * name)123 Targ_NewGN(const char *name)
124 {
125 GNode *gn;
126
127 gn = emalloc(sizeof(GNode));
128 gn->name = estrdup(name);
129 gn->path = NULL;
130 if (name[0] == '-' && name[1] == 'l') {
131 gn->type = OP_LIB;
132 } else {
133 gn->type = 0;
134 }
135 gn->unmade = 0;
136 gn->make = FALSE;
137 gn->made = UNMADE;
138 gn->childMade = FALSE;
139 gn->order = 0;
140 gn->mtime = gn->cmtime = 0;
141 gn->cmtime_gn = NULL;
142 Lst_Init(&gn->iParents);
143 Lst_Init(&gn->cohorts);
144 Lst_Init(&gn->parents);
145 Lst_Init(&gn->children);
146 Lst_Init(&gn->successors);
147 Lst_Init(&gn->preds);
148 Lst_Init(&gn->context);
149 Lst_Init(&gn->commands);
150 gn->suffix = NULL;
151
152 return (gn);
153 }
154
155 /**
156 * Targ_FindNode
157 * Find a node in the list using the given name for matching
158 *
159 * Results:
160 * The node in the list if it was. If it wasn't, return NULL of
161 * flags was TARG_NOCREATE or the newly created and initialized node
162 * if it was TARG_CREATE
163 *
164 * Side Effects:
165 * Sometimes a node is created and added to the list
166 */
167 GNode *
Targ_FindNode(const char * name,int flags)168 Targ_FindNode(const char *name, int flags)
169 {
170 GNode *gn; /* node in that element */
171 Hash_Entry *he; /* New or used hash entry for node */
172 Boolean isNew; /* Set TRUE if Hash_CreateEntry had to create */
173 /* an entry for the node */
174
175 if (flags & TARG_CREATE) {
176 he = Hash_CreateEntry(&targets, name, &isNew);
177 if (isNew) {
178 gn = Targ_NewGN(name);
179 Hash_SetValue(he, gn);
180 Lst_AtEnd(&allTargets, gn);
181 }
182 } else {
183 he = Hash_FindEntry(&targets, name);
184 }
185
186 if (he == NULL) {
187 return (NULL);
188 } else {
189 return (Hash_GetValue(he));
190 }
191 }
192
193 /**
194 * Targ_FindList
195 * Make a complete list of GNodes from the given list of names
196 *
197 * Results:
198 * A complete list of graph nodes corresponding to all instances of all
199 * the names in names.
200 *
201 * Side Effects:
202 * If flags is TARG_CREATE, nodes will be created for all names in
203 * names which do not yet have graph nodes. If flags is TARG_NOCREATE,
204 * an error message will be printed for each name which can't be found.
205 */
206 void
Targ_FindList(Lst * nodes,Lst * names,int flags)207 Targ_FindList(Lst *nodes, Lst *names, int flags)
208 {
209 LstNode *ln; /* name list element */
210 GNode *gn; /* node in tLn */
211 char *name;
212
213 for (ln = Lst_First(names); ln != NULL; ln = Lst_Succ(ln)) {
214 name = Lst_Datum(ln);
215 gn = Targ_FindNode(name, flags);
216 if (gn != NULL) {
217 /*
218 * Note: Lst_AtEnd must come before the Lst_Concat so
219 * the nodes are added to the list in the order in which
220 * they were encountered in the makefile.
221 */
222 Lst_AtEnd(nodes, gn);
223 if (gn->type & OP_DOUBLEDEP) {
224 Lst_Concat(nodes, &gn->cohorts, LST_CONCNEW);
225 }
226
227 } else if (flags == TARG_NOCREATE) {
228 Error("\"%s\" -- target unknown.", name);
229 }
230 }
231 }
232
233 /**
234 * Targ_Ignore
235 * Return true if should ignore errors when creating gn
236 *
237 * Results:
238 * TRUE if should ignore errors
239 */
240 Boolean
Targ_Ignore(GNode * gn)241 Targ_Ignore(GNode *gn)
242 {
243
244 if (ignoreErrors || (gn->type & OP_IGNORE)) {
245 return (TRUE);
246 } else {
247 return (FALSE);
248 }
249 }
250
251 /**
252 * Targ_Silent
253 * Return true if be silent when creating gn
254 *
255 * Results:
256 * TRUE if should be silent
257 */
258 Boolean
Targ_Silent(GNode * gn)259 Targ_Silent(GNode *gn)
260 {
261
262 if (beSilent || (gn->type & OP_SILENT)) {
263 return (TRUE);
264 } else {
265 return (FALSE);
266 }
267 }
268
269 /**
270 * Targ_Precious
271 * See if the given target is precious
272 *
273 * Results:
274 * TRUE if it is precious. FALSE otherwise
275 */
276 Boolean
Targ_Precious(GNode * gn)277 Targ_Precious(GNode *gn)
278 {
279
280 if (allPrecious || (gn->type & (OP_PRECIOUS | OP_DOUBLEDEP))) {
281 return (TRUE);
282 } else {
283 return (FALSE);
284 }
285 }
286
287 static GNode *mainTarg; /* the main target, as set by Targ_SetMain */
288
289 /**
290 * Targ_SetMain
291 * Set our idea of the main target we'll be creating. Used for
292 * debugging output.
293 *
294 * Side Effects:
295 * "mainTarg" is set to the main target's node.
296 */
297 void
Targ_SetMain(GNode * gn)298 Targ_SetMain(GNode *gn)
299 {
300
301 mainTarg = gn;
302 }
303
304 /**
305 * Targ_FmtTime
306 * Format a modification time in some reasonable way and return it.
307 *
308 * Results:
309 * The time reformatted.
310 *
311 * Side Effects:
312 * The time is placed in a static area, so it is overwritten
313 * with each call.
314 */
315 char *
Targ_FmtTime(time_t modtime)316 Targ_FmtTime(time_t modtime)
317 {
318 struct tm *parts;
319 static char buf[128];
320
321 parts = localtime(&modtime);
322
323 strftime(buf, sizeof(buf), "%H:%M:%S %b %d, %Y", parts);
324 buf[sizeof(buf) - 1] = '\0';
325 return (buf);
326 }
327
328 /**
329 * Targ_PrintType
330 * Print out a type field giving only those attributes the user can
331 * set.
332 */
333 void
Targ_PrintType(int type)334 Targ_PrintType(int type)
335 {
336 static const struct flag2str type2str[] = {
337 { OP_OPTIONAL, ".OPTIONAL" },
338 { OP_USE, ".USE" },
339 { OP_EXEC, ".EXEC" },
340 { OP_IGNORE, ".IGNORE" },
341 { OP_PRECIOUS, ".PRECIOUS" },
342 { OP_SILENT, ".SILENT" },
343 { OP_MAKE, ".MAKE" },
344 { OP_JOIN, ".JOIN" },
345 { OP_INVISIBLE, ".INVISIBLE" },
346 { OP_NOTMAIN, ".NOTMAIN" },
347 { OP_PHONY, ".PHONY" },
348 { OP_LIB, ".LIB" },
349 { OP_MEMBER, ".MEMBER" },
350 { OP_ARCHV, ".ARCHV" },
351 { 0, NULL }
352 };
353
354 type &= ~OP_OPMASK;
355 if (!DEBUG(TARG))
356 type &= ~(OP_ARCHV | OP_LIB | OP_MEMBER);
357 print_flags(stdout, type2str, type, 0);
358 }
359
360 /**
361 * TargPrintNode
362 * print the contents of a node
363 */
364 static int
TargPrintNode(const GNode * gn,int pass)365 TargPrintNode(const GNode *gn, int pass)
366 {
367 const LstNode *tln;
368
369 if (!OP_NOP(gn->type)) {
370 printf("#\n");
371 if (gn == mainTarg) {
372 printf("# *** MAIN TARGET ***\n");
373 }
374 if (pass == 2) {
375 if (gn->unmade) {
376 printf("# %d unmade children\n", gn->unmade);
377 } else {
378 printf("# No unmade children\n");
379 }
380 if (!(gn->type & (OP_JOIN | OP_USE | OP_EXEC))) {
381 if (gn->mtime != 0) {
382 printf("# last modified %s: %s\n",
383 Targ_FmtTime(gn->mtime),
384 gn->made == UNMADE ? "unmade" :
385 gn->made == MADE ? "made" :
386 gn->made == UPTODATE ? "up-to-date":
387 "error when made");
388 } else if (gn->made != UNMADE) {
389 printf("# non-existent (maybe): %s\n",
390 gn->made == MADE ? "made" :
391 gn->made == UPTODATE ? "up-to-date":
392 gn->made == ERROR?"error when made":
393 "aborted");
394 } else {
395 printf("# unmade\n");
396 }
397 }
398 if (!Lst_IsEmpty(&gn->iParents)) {
399 printf("# implicit parents: ");
400 LST_FOREACH(tln, &gn->iParents)
401 printf("%s ", ((const GNode *)
402 Lst_Datum(tln))->name);
403 printf("\n");
404 }
405 }
406 if (!Lst_IsEmpty(&gn->parents)) {
407 printf("# parents: ");
408 LST_FOREACH(tln, &gn->parents)
409 printf("%s ", ((const GNode *)
410 Lst_Datum(tln))->name);
411 printf("\n");
412 }
413
414 printf("%-16s", gn->name);
415 switch (gn->type & OP_OPMASK) {
416 case OP_DEPENDS:
417 printf(": ");
418 break;
419 case OP_FORCE:
420 printf("! ");
421 break;
422 case OP_DOUBLEDEP:
423 printf(":: ");
424 break;
425 default:
426 break;
427 }
428 Targ_PrintType(gn->type);
429 LST_FOREACH(tln, &gn->children)
430 printf("%s ", ((const GNode *)Lst_Datum(tln))->name);
431 printf("\n");
432 LST_FOREACH(tln, &gn->commands)
433 printf("\t%s\n", (const char *)Lst_Datum(tln));
434 printf("\n\n");
435 if (gn->type & OP_DOUBLEDEP) {
436 LST_FOREACH(tln, &gn->cohorts)
437 TargPrintNode((const GNode *)Lst_Datum(tln),
438 pass);
439 }
440 }
441 return (0);
442 }
443
444 /**
445 * Targ_PrintGraph
446 * Print the entire graph.
447 */
448 void
Targ_PrintGraph(int pass)449 Targ_PrintGraph(int pass)
450 {
451 const GNode *gn;
452 const LstNode *tln;
453
454 printf("#*** Input graph:\n");
455 LST_FOREACH(tln, &allTargets)
456 TargPrintNode((const GNode *)Lst_Datum(tln), pass);
457 printf("\n\n");
458
459 printf("#\n# Files that are only sources:\n");
460 LST_FOREACH(tln, &allTargets) {
461 gn = Lst_Datum(tln);
462 if (OP_NOP(gn->type))
463 printf("#\t%s [%s]\n", gn->name,
464 gn->path ? gn->path : gn->name);
465 }
466 Var_Dump();
467 printf("\n");
468 Dir_PrintDirectories();
469 printf("\n");
470 Suff_PrintAll();
471 }
472