1 /* $NetBSD: targ.c,v 1.1.1.1 2002/09/19 10:40:12 agc Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1989 by Berkeley Softworks
9 * This code is derived from software contributed to Berkeley by
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static char rcsid[] = "$NetBSD: targ.c,v 1.1.1.1 2002/09/19 10:40:12 agc Exp $";
44 #include <sys/cdefs.h>
47 static char sccsid[] = "@(#)targ.c 8.2 (Berkeley) 3/19/94";
49 __RCSID("$NetBSD: targ.c,v 1.1.1.1 2002/09/19 10:40:12 agc Exp $");
56 * Functions for maintaining the Lst allTargets. Target nodes are
57 * kept in two structures: a Lst, maintained by the list library, and a
58 * hash table, maintained by the hash library.
61 * Targ_Init Initialization procedure.
63 * Targ_End Cleanup the module
65 * Targ_List Return the list of all targets so far.
67 * Targ_NewGN Create a new GNode for the passed target
68 * (string). The node is *not* placed in the
69 * hash table, though all its fields are
72 * Targ_FindNode Find the node for a given target, creating
73 * and storing it if it doesn't exist and the
74 * flags are right (TARG_CREATE)
76 * Targ_FindList Given a list of names, find nodes for all
77 * of them. If a name doesn't exist and the
78 * TARG_NOCREATE flag was given, an error message
79 * is printed. Else, if a name doesn't exist,
80 * its node is created.
82 * Targ_Ignore Return TRUE if errors should be ignored when
83 * creating the given target.
85 * Targ_Silent Return TRUE if we should be silent when
86 * creating the given target.
88 * Targ_Precious Return TRUE if the target is precious and
89 * should not be removed if we are interrupted.
92 * Targ_PrintGraph Print out the entire graphm all variables
93 * and statistics for the directory cache. Should
94 * print something for suffixes, too, but...
103 static Lst allTargets; /* the list of all targets found so far */
105 static Lst allGNs; /* List of all the GNodes */
107 static Hash_Table targets; /* a hash table of same */
109 #define HTSIZE 191 /* initial size of hash table */
111 static int TargPrintOnlySrc __P((ClientData, ClientData));
112 static int TargPrintName __P((ClientData, ClientData));
113 static int TargPrintNode __P((ClientData, ClientData));
115 static void TargFreeGN __P((ClientData));
117 static int TargPropagateCohort __P((ClientData, ClientData));
118 static int TargPropagateNode __P((ClientData, ClientData));
121 *-----------------------------------------------------------------------
123 * Initialize this module
129 * The allTargets list and the targets hash table are initialized
130 *-----------------------------------------------------------------------
135 allTargets = Lst_Init (FALSE);
136 Hash_InitTable (&targets, HTSIZE);
140 *-----------------------------------------------------------------------
142 * Finalize this module
148 * All lists and gnodes are cleared
149 *-----------------------------------------------------------------------
155 Lst_Destroy(allTargets, NOFREE);
157 Lst_Destroy(allGNs, TargFreeGN);
158 Hash_DeleteTable(&targets);
163 *-----------------------------------------------------------------------
165 * Return the list of all targets
168 * The list of all targets.
172 *-----------------------------------------------------------------------
181 *-----------------------------------------------------------------------
183 * Create and initialize a new graph node
186 * An initialized graph node with the name field filled with a copy
190 * The gnode is added to the list of all gnodes.
191 *-----------------------------------------------------------------------
195 char *name; /* the name to stick in the new node */
199 gn = (GNode *) emalloc (sizeof (GNode));
200 gn->name = estrdup (name);
202 gn->path = (char *) 0;
203 if (name[0] == '-' && name[1] == 'l') {
212 gn->mtime = gn->cmtime = 0;
213 gn->iParents = Lst_Init (FALSE);
214 gn->cohorts = Lst_Init (FALSE);
215 gn->parents = Lst_Init (FALSE);
216 gn->children = Lst_Init (FALSE);
217 gn->successors = Lst_Init (FALSE);
218 gn->preds = Lst_Init (FALSE);
219 Hash_InitTable(&gn->context, 0);
220 gn->commands = Lst_Init (FALSE);
227 allGNs = Lst_Init(FALSE);
228 Lst_AtEnd(allGNs, (ClientData) gn);
236 *-----------------------------------------------------------------------
245 *-----------------------------------------------------------------------
251 GNode *gn = (GNode *) gnp;
262 Lst_Destroy(gn->iParents, NOFREE);
263 Lst_Destroy(gn->cohorts, NOFREE);
264 Lst_Destroy(gn->parents, NOFREE);
265 Lst_Destroy(gn->children, NOFREE);
266 Lst_Destroy(gn->successors, NOFREE);
267 Lst_Destroy(gn->preds, NOFREE);
268 Hash_DeleteTable(&gn->context);
269 Lst_Destroy(gn->commands, NOFREE);
276 *-----------------------------------------------------------------------
278 * Find a node in the list using the given name for matching
281 * The node in the list if it was. If it wasn't, return NILGNODE of
282 * flags was TARG_NOCREATE or the newly created and initialized node
283 * if it was TARG_CREATE
286 * Sometimes a node is created and added to the list
287 *-----------------------------------------------------------------------
290 Targ_FindNode (name, flags)
291 char *name; /* the name to find */
292 int flags; /* flags governing events when target not
295 GNode *gn; /* node in that element */
296 Hash_Entry *he; /* New or used hash entry for node */
297 Boolean isNew; /* Set TRUE if Hash_CreateEntry had to create */
298 /* an entry for the node */
301 if (flags & TARG_CREATE) {
302 he = Hash_CreateEntry (&targets, name, &isNew);
304 gn = Targ_NewGN (name);
305 Hash_SetValue (he, gn);
306 (void) Lst_AtEnd (allTargets, (ClientData)gn);
309 he = Hash_FindEntry (&targets, name);
312 if (he == (Hash_Entry *) NULL) {
315 return ((GNode *) Hash_GetValue (he));
320 *-----------------------------------------------------------------------
322 * Make a complete list of GNodes from the given list of names
325 * A complete list of graph nodes corresponding to all instances of all
326 * the names in names.
329 * If flags is TARG_CREATE, nodes will be created for all names in
330 * names which do not yet have graph nodes. If flags is TARG_NOCREATE,
331 * an error message will be printed for each name which can't be found.
332 * -----------------------------------------------------------------------
335 Targ_FindList (names, flags)
336 Lst names; /* list of names to find */
337 int flags; /* flags used if no node is found for a given
340 Lst nodes; /* result list */
341 register LstNode ln; /* name list element */
342 register GNode *gn; /* node in tLn */
345 nodes = Lst_Init (FALSE);
347 if (Lst_Open (names) == FAILURE) {
350 while ((ln = Lst_Next (names)) != NILLNODE) {
351 name = (char *)Lst_Datum(ln);
352 gn = Targ_FindNode (name, flags);
353 if (gn != NILGNODE) {
355 * Note: Lst_AtEnd must come before the Lst_Concat so the nodes
356 * are added to the list in the order in which they were
357 * encountered in the makefile.
359 (void) Lst_AtEnd (nodes, (ClientData)gn);
360 } else if (flags == TARG_NOCREATE) {
361 Error ("\"%s\" -- target unknown.", name);
369 *-----------------------------------------------------------------------
371 * Return true if should ignore errors when creating gn
374 * TRUE if should ignore errors
378 *-----------------------------------------------------------------------
382 GNode *gn; /* node to check for */
384 if (ignoreErrors || gn->type & OP_IGNORE) {
392 *-----------------------------------------------------------------------
394 * Return true if be silent when creating gn
397 * TRUE if should be silent
401 *-----------------------------------------------------------------------
405 GNode *gn; /* node to check for */
407 if (beSilent || gn->type & OP_SILENT) {
415 *-----------------------------------------------------------------------
417 * See if the given target is precious
420 * TRUE if it is precious. FALSE otherwise
424 *-----------------------------------------------------------------------
428 GNode *gn; /* the node to check */
430 if (allPrecious || (gn->type & (OP_PRECIOUS|OP_DOUBLEDEP))) {
437 /******************* DEBUG INFO PRINTING ****************/
439 static GNode *mainTarg; /* the main target, as set by Targ_SetMain */
441 *-----------------------------------------------------------------------
443 * Set our idea of the main target we'll be creating. Used for
450 * "mainTarg" is set to the main target's node.
451 *-----------------------------------------------------------------------
455 GNode *gn; /* The main target we'll create */
461 TargPrintName (gnp, ppath)
465 GNode *gn = (GNode *) gnp;
466 printf ("%s ", gn->name);
470 printf ("[%s] ", gn->path);
472 if (gn == mainTarg) {
473 printf ("(MAIN NAME) ");
477 return (ppath ? 0 : 0);
482 Targ_PrintCmd (cmd, dummy)
486 printf ("\t%s\n", (char *) cmd);
487 return (dummy ? 0 : 0);
491 *-----------------------------------------------------------------------
493 * Format a modification time in some reasonable way and return it.
496 * The time reformatted.
499 * The time is placed in a static area, so it is overwritten
502 *-----------------------------------------------------------------------
509 static char buf[128];
511 parts = localtime(&time);
512 (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
517 *-----------------------------------------------------------------------
519 * Print out a type field giving only those attributes the user can
526 *-----------------------------------------------------------------------
529 Targ_PrintType (type)
535 #define PRINTBIT(attr) case CONCAT(OP_,attr): printf("." #attr " "); break
536 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG)) printf("." #attr " "); break
538 #define PRINTBIT(attr) case CONCAT(OP_,attr): printf(".attr "); break
539 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG)) printf(".attr "); break
540 #endif /* __STDC__ */
545 tbit = 1 << (ffs(type) - 1);
560 /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
561 case OP_MEMBER: if (DEBUG(TARG)) printf(".MEMBER "); break;
568 *-----------------------------------------------------------------------
570 * print the contents of a node
571 *-----------------------------------------------------------------------
574 TargPrintNode (gnp, passp)
578 GNode *gn = (GNode *) gnp;
579 int pass = *(int *) passp;
580 if (!OP_NOP(gn->type)) {
582 if (gn == mainTarg) {
583 printf("# *** MAIN TARGET ***\n");
587 printf("# %d unmade children\n", gn->unmade);
589 printf("# No unmade children\n");
591 if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
592 if (gn->mtime != 0) {
593 printf("# last modified %s: %s\n",
594 Targ_FmtTime(gn->mtime),
595 (gn->made == UNMADE ? "unmade" :
596 (gn->made == MADE ? "made" :
597 (gn->made == UPTODATE ? "up-to-date" :
598 "error when made"))));
599 } else if (gn->made != UNMADE) {
600 printf("# non-existent (maybe): %s\n",
601 (gn->made == MADE ? "made" :
602 (gn->made == UPTODATE ? "up-to-date" :
603 (gn->made == ERROR ? "error when made" :
606 printf("# unmade\n");
609 if (!Lst_IsEmpty (gn->iParents)) {
610 printf("# implicit parents: ");
611 Lst_ForEach (gn->iParents, TargPrintName, (ClientData)0);
612 fputc ('\n', stdout);
615 if (!Lst_IsEmpty (gn->parents)) {
616 printf("# parents: ");
617 Lst_ForEach (gn->parents, TargPrintName, (ClientData)0);
618 fputc ('\n', stdout);
621 printf("%-16s", gn->name);
622 switch (gn->type & OP_OPMASK) {
628 printf(":: "); break;
630 Targ_PrintType (gn->type);
631 Lst_ForEach (gn->children, TargPrintName, (ClientData)0);
632 fputc ('\n', stdout);
633 Lst_ForEach (gn->commands, Targ_PrintCmd, (ClientData)0);
635 if (gn->type & OP_DOUBLEDEP) {
636 Lst_ForEach (gn->cohorts, TargPrintNode, (ClientData)&pass);
643 *-----------------------------------------------------------------------
644 * TargPrintOnlySrc --
645 * Print only those targets that are just a source.
651 * The name of each file is printed preceded by #\t
653 *-----------------------------------------------------------------------
656 TargPrintOnlySrc(gnp, dummy)
660 GNode *gn = (GNode *) gnp;
661 if (OP_NOP(gn->type))
662 printf("#\t%s [%s]\n", gn->name, gn->path ? gn->path : gn->name);
664 return (dummy ? 0 : 0);
668 *-----------------------------------------------------------------------
670 * print the entire graph. heh heh
677 *-----------------------------------------------------------------------
680 Targ_PrintGraph (pass)
681 int pass; /* Which pass this is. 1 => no processing
682 * 2 => processing done */
684 printf("#*** Input graph:\n");
685 Lst_ForEach (allTargets, TargPrintNode, (ClientData)&pass);
687 printf("#\n# Files that are only sources:\n");
688 Lst_ForEach (allTargets, TargPrintOnlySrc, (ClientData) 0);
689 printf("#*** Global Variables:\n");
690 Var_Dump (VAR_GLOBAL);
691 printf("#*** Command-line Variables:\n");
694 Dir_PrintDirectories();
700 TargPropagateCohort (cgnp, pgnp)
704 GNode *cgn = (GNode *) cgnp;
705 GNode *pgn = (GNode *) pgnp;
707 cgn->type |= pgn->type & ~OP_OPMASK;
712 TargPropagateNode (gnp, junk)
716 GNode *gn = (GNode *) gnp;
717 if (gn->type & OP_DOUBLEDEP)
718 Lst_ForEach (gn->cohorts, TargPropagateCohort, gnp);
725 Lst_ForEach (allTargets, TargPropagateNode, (ClientData)0);