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.10 1999/09/11 13:08:02 hoek Exp $
40 * $DragonFly: src/usr.bin/make/targ.c,v 1.17 2004/12/17 07:56:08 okumoto Exp $
45 * Functions for maintaining the Lst allTargets. Target nodes are
46 * kept in two structures: a Lst, maintained by the list library, and a
47 * hash table, maintained by the hash library.
50 * Targ_Init Initialization procedure.
52 * Targ_End Cleanup the module
54 * Targ_NewGN Create a new GNode for the passed target
55 * (string). The node is *not* placed in the
56 * hash table, though all its fields are
59 * Targ_FindNode Find the node for a given target, creating
60 * and storing it if it doesn't exist and the
61 * flags are right (TARG_CREATE)
63 * Targ_FindList Given a list of names, find nodes for all
64 * of them. If a name doesn't exist and the
65 * TARG_NOCREATE flag was given, an error message
66 * is printed. Else, if a name doesn't exist,
67 * its node is created.
69 * Targ_Ignore Return TRUE if errors should be ignored when
70 * creating the given target.
72 * Targ_Silent Return TRUE if we should be silent when
73 * creating the given target.
75 * Targ_Precious Return TRUE if the target is precious and
76 * should not be removed if we are interrupted.
79 * Targ_PrintGraph Print out the entire graphm all variables
80 * and statistics for the directory cache. Should
81 * print something for suffixes, too, but...
90 static Lst *allTargets; /* the list of all targets found so far */
91 static Lst *allGNs; /* List of all the GNodes */
92 static Hash_Table targets; /* a hash table of same */
94 #define HTSIZE 191 /* initial size of hash table */
96 static int TargPrintOnlySrc(void *, void *);
97 static int TargPrintName(void *, void *);
98 static int TargPrintNode(void *, void *);
99 static void TargFreeGN(void *);
102 *-----------------------------------------------------------------------
104 * Initialize this module
110 * The allTargets list and the targets hash table are initialized
111 *-----------------------------------------------------------------------
117 allTargets = Lst_Init();
118 Hash_InitTable(&targets, HTSIZE);
122 *-----------------------------------------------------------------------
124 * Finalize this module
130 * All lists and gnodes are cleared
131 *-----------------------------------------------------------------------
137 Lst_Destroy(allTargets, NOFREE);
139 Lst_Destroy(allGNs, TargFreeGN);
140 Hash_DeleteTable(&targets);
144 *-----------------------------------------------------------------------
146 * Create and initialize a new graph node
149 * An initialized graph node with the name field filled with a copy
153 * The gnode is added to the list of all gnodes.
154 *-----------------------------------------------------------------------
157 Targ_NewGN(char *name)
161 gn = emalloc(sizeof(GNode));
162 gn->name = estrdup(name);
164 if (name[0] == '-' && name[1] == 'l') {
172 gn->childMade = FALSE;
174 gn->mtime = gn->cmtime = 0;
175 gn->iParents = Lst_Init();
176 gn->cohorts = Lst_Init();
177 gn->parents = Lst_Init();
178 gn->children = Lst_Init();
179 gn->successors = Lst_Init();
180 gn->preds = Lst_Init();
181 gn->context = Lst_Init();
182 gn->commands = Lst_Init();
187 Lst_AtEnd(allGNs, (void *)gn);
193 *-----------------------------------------------------------------------
202 *-----------------------------------------------------------------------
205 TargFreeGN(void *gnp)
212 Lst_Destroy(gn->iParents, NOFREE);
213 Lst_Destroy(gn->cohorts, NOFREE);
214 Lst_Destroy(gn->parents, NOFREE);
215 Lst_Destroy(gn->children, NOFREE);
216 Lst_Destroy(gn->successors, NOFREE);
217 Lst_Destroy(gn->preds, NOFREE);
218 Lst_Destroy(gn->context, NOFREE);
219 Lst_Destroy(gn->commands, NOFREE);
224 *-----------------------------------------------------------------------
226 * Find a node in the list using the given name for matching
229 * The node in the list if it was. If it wasn't, return NULL of
230 * flags was TARG_NOCREATE or the newly created and initialized node
231 * if it was TARG_CREATE
234 * Sometimes a node is created and added to the list
235 *-----------------------------------------------------------------------
238 Targ_FindNode(char *name, int flags)
240 GNode *gn; /* node in that element */
241 Hash_Entry *he; /* New or used hash entry for node */
242 Boolean isNew; /* Set TRUE if Hash_CreateEntry had to create */
243 /* an entry for the node */
245 if (flags & TARG_CREATE) {
246 he = Hash_CreateEntry(&targets, name, &isNew);
248 gn = Targ_NewGN(name);
249 Hash_SetValue (he, gn);
250 Lst_AtEnd(allTargets, gn);
253 he = Hash_FindEntry(&targets, name);
259 return (Hash_GetValue(he));
264 *-----------------------------------------------------------------------
266 * Make a complete list of GNodes from the given list of names
269 * A complete list of graph nodes corresponding to all instances of all
270 * the names in names.
273 * If flags is TARG_CREATE, nodes will be created for all names in
274 * names which do not yet have graph nodes. If flags is TARG_NOCREATE,
275 * an error message will be printed for each name which can't be found.
276 * -----------------------------------------------------------------------
279 Targ_FindList(Lst *names, int flags)
281 Lst *nodes; /* result list */
282 LstNode *ln; /* name list element */
283 GNode *gn; /* node in tLn */
288 for (ln = Lst_First(names); ln != NULL; ln = Lst_Succ(ln)) {
289 name = Lst_Datum(ln);
290 gn = Targ_FindNode(name, flags);
293 * Note: Lst_AtEnd must come before the Lst_Concat so the nodes
294 * are added to the list in the order in which they were
295 * encountered in the makefile.
297 Lst_AtEnd(nodes, gn);
298 if (gn->type & OP_DOUBLEDEP) {
299 Lst_Concat(nodes, gn->cohorts, LST_CONCNEW);
301 } else if (flags == TARG_NOCREATE) {
302 Error("\"%s\" -- target unknown.", name);
309 *-----------------------------------------------------------------------
311 * Return true if should ignore errors when creating gn
314 * TRUE if should ignore errors
318 *-----------------------------------------------------------------------
321 Targ_Ignore(GNode *gn)
324 if (ignoreErrors || (gn->type & OP_IGNORE)) {
332 *-----------------------------------------------------------------------
334 * Return true if be silent when creating gn
337 * TRUE if should be silent
341 *-----------------------------------------------------------------------
344 Targ_Silent(GNode *gn)
347 if (beSilent || (gn->type & OP_SILENT)) {
355 *-----------------------------------------------------------------------
357 * See if the given target is precious
360 * TRUE if it is precious. FALSE otherwise
364 *-----------------------------------------------------------------------
367 Targ_Precious(GNode *gn)
370 if (allPrecious || (gn->type & (OP_PRECIOUS | OP_DOUBLEDEP))) {
377 /******************* DEBUG INFO PRINTING ****************/
379 static GNode *mainTarg; /* the main target, as set by Targ_SetMain */
382 *-----------------------------------------------------------------------
384 * Set our idea of the main target we'll be creating. Used for
391 * "mainTarg" is set to the main target's node.
392 *-----------------------------------------------------------------------
395 Targ_SetMain(GNode *gn)
402 TargPrintName(void *gnp, void *ppath)
404 GNode *gn = (GNode *) gnp;
406 printf("%s ", gn->name);
410 printf("[%s] ", gn->path);
412 if (gn == mainTarg) {
413 printf("(MAIN NAME) ");
417 return (ppath ? 0 : 0);
422 Targ_PrintCmd(void *cmd, void *dummy __unused)
425 printf("\t%s\n", (char *)cmd);
430 *-----------------------------------------------------------------------
432 * Format a modification time in some reasonable way and return it.
435 * The time reformatted.
438 * The time is placed in a static area, so it is overwritten
441 *-----------------------------------------------------------------------
444 Targ_FmtTime(time_t modtime)
447 static char buf[128];
449 parts = localtime(&modtime);
451 strftime(buf, sizeof buf, "%H:%M:%S %b %d, %Y", parts);
452 buf[sizeof(buf) - 1] = '\0';
457 *-----------------------------------------------------------------------
459 * Print out a type field giving only those attributes the user can
466 *-----------------------------------------------------------------------
469 Targ_PrintType(int type)
473 #define PRINTBIT(attr) case CONCAT(OP_,attr): printf("." #attr " "); break
474 #define PRINTDBIT(attr) case CONCAT(OP_,attr): DEBUGF(TARG, ("." #attr " ")); break
479 tbit = 1 << (ffs(type) - 1);
494 /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
495 case OP_MEMBER: DEBUGF(TARG, (".MEMBER ")); break;
502 *-----------------------------------------------------------------------
504 * print the contents of a node
505 *-----------------------------------------------------------------------
508 TargPrintNode(void *gnp, void *passp)
511 int pass = *(int *)passp;
513 if (!OP_NOP(gn->type)) {
515 if (gn == mainTarg) {
516 printf("# *** MAIN TARGET ***\n");
520 printf("# %d unmade children\n", gn->unmade);
522 printf("# No unmade children\n");
524 if (!(gn->type & (OP_JOIN | OP_USE | OP_EXEC))) {
525 if (gn->mtime != 0) {
526 printf("# last modified %s: %s\n",
527 Targ_FmtTime(gn->mtime),
528 (gn->made == UNMADE ? "unmade" :
529 (gn->made == MADE ? "made" :
530 (gn->made == UPTODATE ? "up-to-date" :
531 "error when made"))));
532 } else if (gn->made != UNMADE) {
533 printf("# non-existent (maybe): %s\n",
534 (gn->made == MADE ? "made" :
535 (gn->made == UPTODATE ? "up-to-date" :
536 (gn->made == ERROR ? "error when made" :
539 printf("# unmade\n");
542 if (!Lst_IsEmpty (gn->iParents)) {
543 printf("# implicit parents: ");
544 Lst_ForEach(gn->iParents, TargPrintName, (void *)NULL);
548 if (!Lst_IsEmpty (gn->parents)) {
549 printf("# parents: ");
550 Lst_ForEach(gn->parents, TargPrintName, (void *)NULL);
554 printf("%-16s", gn->name);
555 switch (gn->type & OP_OPMASK) {
561 printf(":: "); break;
565 Targ_PrintType(gn->type);
566 Lst_ForEach(gn->children, TargPrintName, (void *)NULL);
568 Lst_ForEach(gn->commands, Targ_PrintCmd, (void *)NULL);
570 if (gn->type & OP_DOUBLEDEP) {
571 Lst_ForEach(gn->cohorts, TargPrintNode, &pass);
578 *-----------------------------------------------------------------------
579 * TargPrintOnlySrc --
580 * Print only those targets that are just a source.
586 * The name of each file is printed preceded by #\t
588 *-----------------------------------------------------------------------
591 TargPrintOnlySrc(void *gnp, void *dummy __unused)
595 if (OP_NOP(gn->type))
596 printf("#\t%s [%s]\n", gn->name, gn->path ? gn->path : gn->name);
602 *-----------------------------------------------------------------------
604 * Print the entire graph.
611 *-----------------------------------------------------------------------
614 Targ_PrintGraph(int pass)
617 printf("#*** Input graph:\n");
618 Lst_ForEach(allTargets, TargPrintNode, &pass);
620 printf("#\n# Files that are only sources:\n");
621 Lst_ForEach(allTargets, TargPrintOnlySrc, (void *)NULL);
622 printf("#*** Global Variables:\n");
623 Var_Dump(VAR_GLOBAL);
624 printf("#*** Command-line Variables:\n");
627 Dir_PrintDirectories();