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
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
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
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
67 * Targ_Silent Return true if we should be silent when creating the
70 * Targ_Precious Return true if the target is precious and should not
71 * be removed if we are interrupted.
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...
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 */
101 * Initialize this module
104 * The allTargets list and the targets hash table are initialized
110 Hash_InitTable(&targets
, HTSIZE
);
115 * Create and initialize a new graph node
118 * An initialized graph node with the name field filled with a copy
122 * The gnode is added to the list of all gnodes.
125 Targ_NewGN(const char *name
)
129 gn
= emalloc(sizeof(GNode
));
130 gn
->name
= estrdup(name
);
132 if (name
[0] == '-' && name
[1] == 'l') {
140 gn
->childMade
= false;
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
);
158 * Find a node in the list using the given name for matching
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
166 * Sometimes a node is created and added to the list
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
);
179 gn
= Targ_NewGN(name
);
180 Hash_SetValue(he
, gn
);
181 Lst_AtEnd(&allTargets
, gn
);
184 he
= Hash_FindEntry(&targets
, name
);
190 return (Hash_GetValue(he
));
196 * Make a complete list of GNodes from the given list of names
199 * A complete list of graph nodes corresponding to all instances of all
200 * the names in names.
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.
208 Targ_FindList(Lst
*nodes
, Lst
*names
, int flags
)
210 LstNode
*ln
; /* name list element */
211 GNode
*gn
; /* node in tLn */
214 for (ln
= Lst_First(names
); ln
!= NULL
; ln
= Lst_Succ(ln
)) {
215 name
= Lst_Datum(ln
);
216 gn
= Targ_FindNode(name
, flags
);
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
);
236 * Return true if should ignore errors when creating gn
239 * true if should ignore errors
242 Targ_Ignore(GNode
*gn
)
245 if (ignoreErrors
|| (gn
->type
& OP_IGNORE
)) {
254 * Return true if be silent when creating gn
257 * true if should be silent
260 Targ_Silent(GNode
*gn
)
263 if (beSilent
|| (gn
->type
& OP_SILENT
)) {
272 * See if the given target is precious
275 * true if it is precious. false otherwise
278 Targ_Precious(GNode
*gn
)
281 if (allPrecious
|| (gn
->type
& (OP_PRECIOUS
| OP_DOUBLEDEP
))) {
288 static GNode
*mainTarg
; /* the main target, as set by Targ_SetMain */
292 * Set our idea of the main target we'll be creating. Used for
296 * "mainTarg" is set to the main target's node.
299 Targ_SetMain(GNode
*gn
)
307 * Format a modification time in some reasonable way and return it.
310 * The time reformatted.
313 * The time is placed in a static area, so it is overwritten
317 Targ_FmtTime(time_t modtime
)
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';
331 * Print out a type field giving only those attributes the user can
335 Targ_PrintType(int type
)
337 static const struct flag2str type2str
[] = {
338 { OP_OPTIONAL
, ".OPTIONAL" },
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" },
350 { OP_MEMBER
, ".MEMBER" },
351 { OP_ARCHV
, ".ARCHV" },
357 type
&= ~(OP_ARCHV
| OP_LIB
| OP_MEMBER
);
358 print_flags(stdout
, type2str
, type
, 0);
363 * print the contents of a node
366 TargPrintNode(const GNode
*gn
, int pass
)
370 if (!OP_NOP(gn
->type
)) {
372 if (gn
== mainTarg
) {
373 printf("# *** MAIN TARGET ***\n");
377 printf("# %d unmade children\n", gn
->unmade
);
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":
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");
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
);
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
);
414 printf("%-16s", gn
->name
);
415 switch (gn
->type
& OP_OPMASK
) {
428 Targ_PrintType(gn
->type
);
429 LST_FOREACH(tln
, &gn
->children
)
430 printf("%s ", ((const GNode
*)Lst_Datum(tln
))->name
);
432 LST_FOREACH(tln
, &gn
->commands
)
433 printf("\t%s\n", (const char *)Lst_Datum(tln
));
435 if (gn
->type
& OP_DOUBLEDEP
) {
436 LST_FOREACH(tln
, &gn
->cohorts
)
437 TargPrintNode((const GNode
*)Lst_Datum(tln
),
446 * Print the entire graph.
449 Targ_PrintGraph(int pass
)
454 printf("#*** Input graph:\n");
455 LST_FOREACH(tln
, &allTargets
)
456 TargPrintNode((const GNode
*)Lst_Datum(tln
), pass
);
459 printf("#\n# Files that are only sources:\n");
460 LST_FOREACH(tln
, &allTargets
) {
462 if (OP_NOP(gn
->type
))
463 printf("#\t%s [%s]\n", gn
->name
,
464 gn
->path
? gn
->path
: gn
->name
);
468 Dir_PrintDirectories();