Update gpt.8:
[dragonfly.git] / usr.bin / make / targ.c
bloba01a0cfe0b917878948571918d7d9dbf19f06874
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.
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
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.
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.
38 * @(#)targ.c 8.2 (Berkeley) 3/19/94
39 * $FreeBSD: src/usr.bin/make/targ.c,v 1.37 2005/02/04 12:38:57 harti Exp $
40 * $DragonFly: src/usr.bin/make/targ.c,v 1.31 2005/07/29 22:48:41 okumoto Exp $
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.
48 * Interface:
49 * Targ_Init Initialization procedure.
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.
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)
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.
64 * Targ_Ignore Return true if errors should be ignored when creating
65 * the given target.
67 * Targ_Silent Return true if we should be silent when creating the
68 * given target.
70 * Targ_Precious Return true if the target is precious and should not
71 * be removed if we are interrupted.
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...
79 #include <stdio.h>
80 #include <string.h>
82 #include "dir.h"
83 #include "globals.h"
84 #include "GNode.h"
85 #include "hash.h"
86 #include "make.h"
87 #include "suff.h"
88 #include "targ.h"
89 #include "util.h"
90 #include "var.h"
92 /* the list of all targets found so far */
93 static Lst allTargets = Lst_Initializer(allTargets);
95 static Hash_Table targets; /* a hash table of same */
97 #define HTSIZE 191 /* initial size of hash table */
99 /**
100 * Targ_Init
101 * Initialize this module
103 * Side Effects:
104 * The allTargets list and the targets hash table are initialized
106 void
107 Targ_Init(void)
110 Hash_InitTable(&targets, HTSIZE);
114 * Targ_NewGN
115 * Create and initialize a new graph node
117 * Results:
118 * An initialized graph node with the name field filled with a copy
119 * of the passed name
121 * Side Effects:
122 * The gnode is added to the list of all gnodes.
124 GNode *
125 Targ_NewGN(const char *name)
127 GNode *gn;
129 gn = emalloc(sizeof(GNode));
130 gn->name = estrdup(name);
131 gn->path = NULL;
132 if (name[0] == '-' && name[1] == 'l') {
133 gn->type = OP_LIB;
134 } else {
135 gn->type = 0;
137 gn->unmade = 0;
138 gn->make = false;
139 gn->made = UNMADE;
140 gn->childMade = false;
141 gn->order = 0;
142 gn->mtime = gn->cmtime = 0;
143 Lst_Init(&gn->iParents);
144 Lst_Init(&gn->cohorts);
145 Lst_Init(&gn->parents);
146 Lst_Init(&gn->children);
147 Lst_Init(&gn->successors);
148 Lst_Init(&gn->preds);
149 Lst_Init(&gn->context);
150 Lst_Init(&gn->commands);
151 gn->suffix = NULL;
153 return (gn);
157 * Targ_FindNode
158 * Find a node in the list using the given name for matching
160 * Results:
161 * The node in the list if it was. If it wasn't, return NULL of
162 * flags was TARG_NOCREATE or the newly created and initialized node
163 * if it was TARG_CREATE
165 * Side Effects:
166 * Sometimes a node is created and added to the list
168 GNode *
169 Targ_FindNode(const char *name, int flags)
171 GNode *gn; /* node in that element */
172 Hash_Entry *he; /* New or used hash entry for node */
173 bool isNew; /* Set true if Hash_CreateEntry had to create */
174 /* an entry for the node */
176 if (flags & TARG_CREATE) {
177 he = Hash_CreateEntry(&targets, name, &isNew);
178 if (isNew) {
179 gn = Targ_NewGN(name);
180 Hash_SetValue(he, gn);
181 Lst_AtEnd(&allTargets, gn);
183 } else {
184 he = Hash_FindEntry(&targets, name);
187 if (he == NULL) {
188 return (NULL);
189 } else {
190 return (Hash_GetValue(he));
195 * Targ_FindList
196 * Make a complete list of GNodes from the given list of names
198 * Results:
199 * A complete list of graph nodes corresponding to all instances of all
200 * the names in names.
202 * Side Effects:
203 * If flags is TARG_CREATE, nodes will be created for all names in
204 * names which do not yet have graph nodes. If flags is TARG_NOCREATE,
205 * an error message will be printed for each name which can't be found.
207 void
208 Targ_FindList(Lst *nodes, Lst *names, int flags)
210 LstNode *ln; /* name list element */
211 GNode *gn; /* node in tLn */
212 char *name;
214 for (ln = Lst_First(names); ln != NULL; ln = Lst_Succ(ln)) {
215 name = Lst_Datum(ln);
216 gn = Targ_FindNode(name, flags);
217 if (gn != NULL) {
219 * Note: Lst_AtEnd must come before the Lst_Concat so
220 * the nodes are added to the list in the order in which
221 * they were encountered in the makefile.
223 Lst_AtEnd(nodes, gn);
224 if (gn->type & OP_DOUBLEDEP) {
225 Lst_Concat(nodes, &gn->cohorts, LST_CONCNEW);
228 } else if (flags == TARG_NOCREATE) {
229 Error("\"%s\" -- target unknown.", name);
235 * Targ_Ignore
236 * Return true if should ignore errors when creating gn
238 * Results:
239 * true if should ignore errors
241 bool
242 Targ_Ignore(GNode *gn)
245 if (ignoreErrors || (gn->type & OP_IGNORE)) {
246 return (true);
247 } else {
248 return (false);
253 * Targ_Silent
254 * Return true if be silent when creating gn
256 * Results:
257 * true if should be silent
259 bool
260 Targ_Silent(GNode *gn)
263 if (beSilent || (gn->type & OP_SILENT)) {
264 return (true);
265 } else {
266 return (false);
271 * Targ_Precious
272 * See if the given target is precious
274 * Results:
275 * true if it is precious. false otherwise
277 bool
278 Targ_Precious(GNode *gn)
281 if (allPrecious || (gn->type & (OP_PRECIOUS | OP_DOUBLEDEP))) {
282 return (true);
283 } else {
284 return (false);
288 static GNode *mainTarg; /* the main target, as set by Targ_SetMain */
291 * Targ_SetMain
292 * Set our idea of the main target we'll be creating. Used for
293 * debugging output.
295 * Side Effects:
296 * "mainTarg" is set to the main target's node.
298 void
299 Targ_SetMain(GNode *gn)
302 mainTarg = gn;
306 * Targ_FmtTime
307 * Format a modification time in some reasonable way and return it.
309 * Results:
310 * The time reformatted.
312 * Side Effects:
313 * The time is placed in a static area, so it is overwritten
314 * with each call.
316 char *
317 Targ_FmtTime(time_t modtime)
319 struct tm *parts;
320 static char buf[128];
322 parts = localtime(&modtime);
324 strftime(buf, sizeof(buf), "%H:%M:%S %b %d, %Y", parts);
325 buf[sizeof(buf) - 1] = '\0';
326 return (buf);
330 * Targ_PrintType
331 * Print out a type field giving only those attributes the user can
332 * set.
334 void
335 Targ_PrintType(int type)
337 static const struct flag2str type2str[] = {
338 { OP_OPTIONAL, ".OPTIONAL" },
339 { OP_USE, ".USE" },
340 { OP_EXEC, ".EXEC" },
341 { OP_IGNORE, ".IGNORE" },
342 { OP_PRECIOUS, ".PRECIOUS" },
343 { OP_SILENT, ".SILENT" },
344 { OP_MAKE, ".MAKE" },
345 { OP_JOIN, ".JOIN" },
346 { OP_INVISIBLE, ".INVISIBLE" },
347 { OP_NOTMAIN, ".NOTMAIN" },
348 { OP_PHONY, ".PHONY" },
349 { OP_LIB, ".LIB" },
350 { OP_MEMBER, ".MEMBER" },
351 { OP_ARCHV, ".ARCHV" },
352 { 0, NULL }
355 type &= ~OP_OPMASK;
356 if (!DEBUG(TARG))
357 type &= ~(OP_ARCHV | OP_LIB | OP_MEMBER);
358 print_flags(stdout, type2str, type, 0);
362 * TargPrintNode
363 * print the contents of a node
365 static int
366 TargPrintNode(const GNode *gn, int pass)
368 const LstNode *tln;
370 if (!OP_NOP(gn->type)) {
371 printf("#\n");
372 if (gn == mainTarg) {
373 printf("# *** MAIN TARGET ***\n");
375 if (pass == 2) {
376 if (gn->unmade) {
377 printf("# %d unmade children\n", gn->unmade);
378 } else {
379 printf("# No unmade children\n");
381 if (!(gn->type & (OP_JOIN | OP_USE | OP_EXEC))) {
382 if (gn->mtime != 0) {
383 printf("# last modified %s: %s\n",
384 Targ_FmtTime(gn->mtime),
385 gn->made == UNMADE ? "unmade" :
386 gn->made == MADE ? "made" :
387 gn->made == UPTODATE ? "up-to-date":
388 "error when made");
389 } else if (gn->made != UNMADE) {
390 printf("# non-existent (maybe): %s\n",
391 gn->made == MADE ? "made" :
392 gn->made == UPTODATE ? "up-to-date":
393 gn->made == ERROR ? "error when made" : "aborted");
394 } else {
395 printf("# unmade\n");
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");
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");
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;
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);
441 return (0);
445 * Targ_PrintGraph
446 * Print the entire graph.
448 void
449 Targ_PrintGraph(int pass)
451 const GNode *gn;
452 const LstNode *tln;
454 printf("#*** Input graph:\n");
455 LST_FOREACH(tln, &allTargets)
456 TargPrintNode((const GNode *)Lst_Datum(tln), pass);
457 printf("\n\n");
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);
466 Var_Dump();
467 printf("\n");
468 Dir_PrintDirectories();
469 printf("\n");
470 Suff_PrintAll();