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.3 2003/11/03 19:31:30 eirikn 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(ClientData, ClientData);
97 static int TargPrintName(ClientData, ClientData);
98 static int TargPrintNode(ClientData, ClientData);
99 static void TargFreeGN(ClientData);
102 *-----------------------------------------------------------------------
104 * Initialize this module
110 * The allTargets list and the targets hash table are initialized
111 *-----------------------------------------------------------------------
116 allTargets = Lst_Init (FALSE);
117 Hash_InitTable (&targets, HTSIZE);
121 *-----------------------------------------------------------------------
123 * Finalize this module
129 * All lists and gnodes are cleared
130 *-----------------------------------------------------------------------
135 Lst_Destroy(allTargets, NOFREE);
137 Lst_Destroy(allGNs, TargFreeGN);
138 Hash_DeleteTable(&targets);
142 *-----------------------------------------------------------------------
144 * Create and initialize a new graph node
147 * An initialized graph node with the name field filled with a copy
151 * The gnode is added to the list of all gnodes.
152 *-----------------------------------------------------------------------
156 char *name; /* the name to stick in the new node */
160 gn = (GNode *) emalloc (sizeof (GNode));
161 gn->name = estrdup (name);
162 gn->path = (char *) 0;
163 if (name[0] == '-' && name[1] == 'l') {
171 gn->childMade = FALSE;
173 gn->mtime = gn->cmtime = 0;
174 gn->iParents = Lst_Init (FALSE);
175 gn->cohorts = Lst_Init (FALSE);
176 gn->parents = Lst_Init (FALSE);
177 gn->children = Lst_Init (FALSE);
178 gn->successors = Lst_Init (FALSE);
179 gn->preds = Lst_Init (FALSE);
180 gn->context = Lst_Init (FALSE);
181 gn->commands = Lst_Init (FALSE);
185 allGNs = Lst_Init(FALSE);
186 Lst_AtEnd(allGNs, (ClientData) gn);
192 *-----------------------------------------------------------------------
201 *-----------------------------------------------------------------------
207 GNode *gn = (GNode *) gnp;
213 Lst_Destroy(gn->iParents, NOFREE);
214 Lst_Destroy(gn->cohorts, NOFREE);
215 Lst_Destroy(gn->parents, NOFREE);
216 Lst_Destroy(gn->children, NOFREE);
217 Lst_Destroy(gn->successors, NOFREE);
218 Lst_Destroy(gn->preds, NOFREE);
219 Lst_Destroy(gn->context, NOFREE);
220 Lst_Destroy(gn->commands, NOFREE);
226 *-----------------------------------------------------------------------
228 * Find a node in the list using the given name for matching
231 * The node in the list if it was. If it wasn't, return NILGNODE of
232 * flags was TARG_NOCREATE or the newly created and initialized node
233 * if it was TARG_CREATE
236 * Sometimes a node is created and added to the list
237 *-----------------------------------------------------------------------
240 Targ_FindNode (name, flags)
241 char *name; /* the name to find */
242 int flags; /* flags governing events when target not
245 GNode *gn; /* node in that element */
246 Hash_Entry *he; /* New or used hash entry for node */
247 Boolean isNew; /* Set TRUE if Hash_CreateEntry had to create */
248 /* an entry for the node */
251 if (flags & TARG_CREATE) {
252 he = Hash_CreateEntry (&targets, name, &isNew);
254 gn = Targ_NewGN (name);
255 Hash_SetValue (he, gn);
256 (void) Lst_AtEnd (allTargets, (ClientData)gn);
259 he = Hash_FindEntry (&targets, name);
262 if (he == (Hash_Entry *) NULL) {
265 return ((GNode *) Hash_GetValue (he));
270 *-----------------------------------------------------------------------
272 * Make a complete list of GNodes from the given list of names
275 * A complete list of graph nodes corresponding to all instances of all
276 * the names in names.
279 * If flags is TARG_CREATE, nodes will be created for all names in
280 * names which do not yet have graph nodes. If flags is TARG_NOCREATE,
281 * an error message will be printed for each name which can't be found.
282 * -----------------------------------------------------------------------
285 Targ_FindList (names, flags)
286 Lst names; /* list of names to find */
287 int flags; /* flags used if no node is found for a given
290 Lst nodes; /* result list */
291 register LstNode ln; /* name list element */
292 register GNode *gn; /* node in tLn */
295 nodes = Lst_Init (FALSE);
297 if (Lst_Open (names) == FAILURE) {
300 while ((ln = Lst_Next (names)) != NILLNODE) {
301 name = (char *)Lst_Datum(ln);
302 gn = Targ_FindNode (name, flags);
303 if (gn != NILGNODE) {
305 * Note: Lst_AtEnd must come before the Lst_Concat so the nodes
306 * are added to the list in the order in which they were
307 * encountered in the makefile.
309 (void) Lst_AtEnd (nodes, (ClientData)gn);
310 if (gn->type & OP_DOUBLEDEP) {
311 (void)Lst_Concat (nodes, gn->cohorts, LST_CONCNEW);
313 } else if (flags == TARG_NOCREATE) {
314 Error ("\"%s\" -- target unknown.", name);
322 *-----------------------------------------------------------------------
324 * Return true if should ignore errors when creating gn
327 * TRUE if should ignore errors
331 *-----------------------------------------------------------------------
335 GNode *gn; /* node to check for */
337 if (ignoreErrors || gn->type & OP_IGNORE) {
345 *-----------------------------------------------------------------------
347 * Return true if be silent when creating gn
350 * TRUE if should be silent
354 *-----------------------------------------------------------------------
358 GNode *gn; /* node to check for */
360 if (beSilent || gn->type & OP_SILENT) {
368 *-----------------------------------------------------------------------
370 * See if the given target is precious
373 * TRUE if it is precious. FALSE otherwise
377 *-----------------------------------------------------------------------
381 GNode *gn; /* the node to check */
383 if (allPrecious || (gn->type & (OP_PRECIOUS|OP_DOUBLEDEP))) {
390 /******************* DEBUG INFO PRINTING ****************/
392 static GNode *mainTarg; /* the main target, as set by Targ_SetMain */
394 *-----------------------------------------------------------------------
396 * Set our idea of the main target we'll be creating. Used for
403 * "mainTarg" is set to the main target's node.
404 *-----------------------------------------------------------------------
408 GNode *gn; /* The main target we'll create */
414 TargPrintName (gnp, ppath)
418 GNode *gn = (GNode *) gnp;
419 printf ("%s ", gn->name);
423 printf ("[%s] ", gn->path);
425 if (gn == mainTarg) {
426 printf ("(MAIN NAME) ");
430 return (ppath ? 0 : 0);
435 Targ_PrintCmd (cmd, dummy)
439 printf ("\t%s\n", (char *) cmd);
440 return (dummy ? 0 : 0);
444 *-----------------------------------------------------------------------
446 * Format a modification time in some reasonable way and return it.
449 * The time reformatted.
452 * The time is placed in a static area, so it is overwritten
455 *-----------------------------------------------------------------------
462 static char buf[128];
464 parts = localtime(&time);
466 strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
467 buf[sizeof(buf) - 1] = '\0';
472 *-----------------------------------------------------------------------
474 * Print out a type field giving only those attributes the user can
481 *-----------------------------------------------------------------------
484 Targ_PrintType (type)
490 #define PRINTBIT(attr) case CONCAT(OP_,attr): printf("." #attr " "); break
491 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG)) printf("." #attr " "); break
493 #define PRINTBIT(attr) case CONCAT(OP_,attr): printf(".attr "); break
494 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG)) printf(".attr "); break
495 #endif /* __STDC__ */
500 tbit = 1 << (ffs(type) - 1);
515 /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
516 case OP_MEMBER: if (DEBUG(TARG)) printf(".MEMBER "); break;
523 *-----------------------------------------------------------------------
525 * print the contents of a node
526 *-----------------------------------------------------------------------
529 TargPrintNode (gnp, passp)
533 GNode *gn = (GNode *) gnp;
534 int pass = *(int *) passp;
535 if (!OP_NOP(gn->type)) {
537 if (gn == mainTarg) {
538 printf("# *** MAIN TARGET ***\n");
542 printf("# %d unmade children\n", gn->unmade);
544 printf("# No unmade children\n");
546 if (! (gn->type & (OP_JOIN|OP_USE|OP_EXEC))) {
547 if (gn->mtime != 0) {
548 printf("# last modified %s: %s\n",
549 Targ_FmtTime(gn->mtime),
550 (gn->made == UNMADE ? "unmade" :
551 (gn->made == MADE ? "made" :
552 (gn->made == UPTODATE ? "up-to-date" :
553 "error when made"))));
554 } else if (gn->made != UNMADE) {
555 printf("# non-existent (maybe): %s\n",
556 (gn->made == MADE ? "made" :
557 (gn->made == UPTODATE ? "up-to-date" :
558 (gn->made == ERROR ? "error when made" :
561 printf("# unmade\n");
564 if (!Lst_IsEmpty (gn->iParents)) {
565 printf("# implicit parents: ");
566 Lst_ForEach (gn->iParents, TargPrintName, (ClientData)0);
567 fputc ('\n', stdout);
570 if (!Lst_IsEmpty (gn->parents)) {
571 printf("# parents: ");
572 Lst_ForEach (gn->parents, TargPrintName, (ClientData)0);
573 fputc ('\n', stdout);
576 printf("%-16s", gn->name);
577 switch (gn->type & OP_OPMASK) {
583 printf(":: "); break;
585 Targ_PrintType (gn->type);
586 Lst_ForEach (gn->children, TargPrintName, (ClientData)0);
587 fputc ('\n', stdout);
588 Lst_ForEach (gn->commands, Targ_PrintCmd, (ClientData)0);
590 if (gn->type & OP_DOUBLEDEP) {
591 Lst_ForEach (gn->cohorts, TargPrintNode, (ClientData)&pass);
598 *-----------------------------------------------------------------------
599 * TargPrintOnlySrc --
600 * Print only those targets that are just a source.
606 * The name of each file is printed preceeded by #\t
608 *-----------------------------------------------------------------------
611 TargPrintOnlySrc(gnp, dummy)
615 GNode *gn = (GNode *) gnp;
616 if (OP_NOP(gn->type))
617 printf("#\t%s [%s]\n", gn->name, gn->path ? gn->path : gn->name);
619 return (dummy ? 0 : 0);
623 *-----------------------------------------------------------------------
625 * print the entire graph. heh heh
632 *-----------------------------------------------------------------------
635 Targ_PrintGraph (pass)
636 int pass; /* Which pass this is. 1 => no processing
637 * 2 => processing done */
639 printf("#*** Input graph:\n");
640 Lst_ForEach (allTargets, TargPrintNode, (ClientData)&pass);
642 printf("#\n# Files that are only sources:\n");
643 Lst_ForEach (allTargets, TargPrintOnlySrc, (ClientData) 0);
644 printf("#*** Global Variables:\n");
645 Var_Dump (VAR_GLOBAL);
646 printf("#*** Command-line Variables:\n");
649 Dir_PrintDirectories();