1 /* $NetBSD: targ.c,v 1.63 2020/07/03 08:02:55 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 static char rcsid[] = "$NetBSD: targ.c,v 1.63 2020/07/03 08:02:55 rillig Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid[] = "@(#)targ.c 8.2 (Berkeley) 3/19/94";
79 __RCSID("$NetBSD: targ.c,v 1.63 2020/07/03 08:02:55 rillig Exp $");
86 * Functions for maintaining the Lst allTargets. Target nodes are
87 * kept in two structures: a Lst, maintained by the list library, and a
88 * hash table, maintained by the hash library.
91 * Targ_Init Initialization procedure.
93 * Targ_End Cleanup the module
95 * Targ_List Return the list of all targets so far.
97 * Targ_NewGN Create a new GNode for the passed target
98 * (string). The node is *not* placed in the
99 * hash table, though all its fields are
102 * Targ_FindNode Find the node for a given target, creating
103 * and storing it if it doesn't exist and the
104 * flags are right (TARG_CREATE)
106 * Targ_FindList Given a list of names, find nodes for all
107 * of them. If a name doesn't exist and the
108 * TARG_NOCREATE flag was given, an error message
109 * is printed. Else, if a name doesn't exist,
110 * its node is created.
112 * Targ_Ignore Return TRUE if errors should be ignored when
113 * creating the given target.
115 * Targ_Silent Return TRUE if we should be silent when
116 * creating the given target.
118 * Targ_Precious Return TRUE if the target is precious and
119 * should not be removed if we are interrupted.
121 * Targ_Propagate Propagate information between related
122 * nodes. Should be called after the
123 * makefiles are parsed but before any
127 * Targ_PrintGraph Print out the entire graphm all variables
128 * and statistics for the directory cache. Should
129 * print something for suffixes, too, but...
139 static Lst allTargets; /* the list of all targets found so far */
141 static Lst allGNs; /* List of all the GNodes */
143 static Hash_Table targets; /* a hash table of same */
145 #define HTSIZE 191 /* initial size of hash table */
147 static int TargPrintOnlySrc(void *, void *);
148 static int TargPrintName(void *, void *);
150 static void TargFreeGN(void *);
152 static int TargPropagateCohort(void *, void *);
153 static int TargPropagateNode(void *, void *);
156 *-----------------------------------------------------------------------
158 * Initialize this module
164 * The allTargets list and the targets hash table are initialized
165 *-----------------------------------------------------------------------
170 allTargets = Lst_Init(FALSE);
171 Hash_InitTable(&targets, HTSIZE);
175 *-----------------------------------------------------------------------
177 * Finalize this module
183 * All lists and gnodes are cleared
184 *-----------------------------------------------------------------------
190 Lst_Destroy(allTargets, NULL);
192 Lst_Destroy(allGNs, TargFreeGN);
193 Hash_DeleteTable(&targets);
198 *-----------------------------------------------------------------------
200 * Return the list of all targets
203 * The list of all targets.
207 *-----------------------------------------------------------------------
216 *-----------------------------------------------------------------------
218 * Create and initialize a new graph node
221 * name the name to stick in the new node
224 * An initialized graph node with the name field filled with a copy
228 * The gnode is added to the list of all gnodes.
229 *-----------------------------------------------------------------------
232 Targ_NewGN(const char *name)
236 gn = bmake_malloc(sizeof(GNode));
237 gn->name = bmake_strdup(name);
240 if (name[0] == '-' && name[1] == 'l') {
246 gn->unmade_cohorts = 0;
247 gn->cohort_num[0] = 0;
248 gn->centurion = NULL;
254 gn->iParents = Lst_Init(FALSE);
255 gn->cohorts = Lst_Init(FALSE);
256 gn->parents = Lst_Init(FALSE);
257 gn->children = Lst_Init(FALSE);
258 gn->order_pred = Lst_Init(FALSE);
259 gn->order_succ = Lst_Init(FALSE);
260 Hash_InitTable(&gn->context, 0);
261 gn->commands = Lst_Init(FALSE);
268 allGNs = Lst_Init(FALSE);
269 Lst_AtEnd(allGNs, gn);
277 *-----------------------------------------------------------------------
286 *-----------------------------------------------------------------------
289 TargFreeGN(void *gnp)
291 GNode *gn = (GNode *)gnp;
297 /* gn->fname points to name allocated when file was opened, don't free */
299 Lst_Destroy(gn->iParents, NULL);
300 Lst_Destroy(gn->cohorts, NULL);
301 Lst_Destroy(gn->parents, NULL);
302 Lst_Destroy(gn->children, NULL);
303 Lst_Destroy(gn->order_succ, NULL);
304 Lst_Destroy(gn->order_pred, NULL);
305 Hash_DeleteTable(&gn->context);
306 Lst_Destroy(gn->commands, NULL);
313 *-----------------------------------------------------------------------
315 * Find a node in the list using the given name for matching
318 * name the name to find
319 * flags flags governing events when target not
323 * The node in the list if it was. If it wasn't, return NULL of
324 * flags was TARG_NOCREATE or the newly created and initialized node
325 * if it was TARG_CREATE
328 * Sometimes a node is created and added to the list
329 *-----------------------------------------------------------------------
332 Targ_FindNode(const char *name, int flags)
334 GNode *gn; /* node in that element */
335 Hash_Entry *he = NULL; /* New or used hash entry for node */
336 Boolean isNew; /* Set TRUE if Hash_CreateEntry had to create */
337 /* an entry for the node */
339 if (!(flags & (TARG_CREATE | TARG_NOHASH))) {
340 he = Hash_FindEntry(&targets, name);
343 return (GNode *)Hash_GetValue(he);
346 if (!(flags & TARG_NOHASH)) {
347 he = Hash_CreateEntry(&targets, name, &isNew);
349 return (GNode *)Hash_GetValue(he);
352 gn = Targ_NewGN(name);
353 if (!(flags & TARG_NOHASH))
354 Hash_SetValue(he, gn);
355 Var_Append(".ALLTARGETS", name, VAR_GLOBAL);
356 (void)Lst_AtEnd(allTargets, gn);
358 gn->flags |= FROM_DEPEND;
363 *-----------------------------------------------------------------------
365 * Make a complete list of GNodes from the given list of names
368 * name list of names to find
369 * flags flags used if no node is found for a given name
372 * A complete list of graph nodes corresponding to all instances of all
373 * the names in names.
376 * If flags is TARG_CREATE, nodes will be created for all names in
377 * names which do not yet have graph nodes. If flags is TARG_NOCREATE,
378 * an error message will be printed for each name which can't be found.
379 * -----------------------------------------------------------------------
382 Targ_FindList(Lst names, int flags)
384 Lst nodes; /* result list */
385 LstNode ln; /* name list element */
386 GNode *gn; /* node in tLn */
389 nodes = Lst_Init(FALSE);
391 if (Lst_Open(names) == FAILURE) {
394 while ((ln = Lst_Next(names)) != NULL) {
395 name = (char *)Lst_Datum(ln);
396 gn = Targ_FindNode(name, flags);
399 * Note: Lst_AtEnd must come before the Lst_Concat so the nodes
400 * are added to the list in the order in which they were
401 * encountered in the makefile.
403 (void)Lst_AtEnd(nodes, gn);
404 } else if (flags == TARG_NOCREATE) {
405 Error("\"%s\" -- target unknown.", name);
413 *-----------------------------------------------------------------------
415 * Return true if should ignore errors when creating gn
418 * gn node to check for
421 * TRUE if should ignore errors
425 *-----------------------------------------------------------------------
428 Targ_Ignore(GNode *gn)
430 if (ignoreErrors || gn->type & OP_IGNORE) {
438 *-----------------------------------------------------------------------
440 * Return true if be silent when creating gn
443 * gn node to check for
446 * TRUE if should be silent
450 *-----------------------------------------------------------------------
453 Targ_Silent(GNode *gn)
455 if (beSilent || gn->type & OP_SILENT) {
463 *-----------------------------------------------------------------------
465 * See if the given target is precious
468 * gn the node to check
471 * TRUE if it is precious. FALSE otherwise
475 *-----------------------------------------------------------------------
478 Targ_Precious(GNode *gn)
480 if (allPrecious || (gn->type & (OP_PRECIOUS|OP_DOUBLEDEP))) {
487 /******************* DEBUG INFO PRINTING ****************/
489 static GNode *mainTarg; /* the main target, as set by Targ_SetMain */
491 *-----------------------------------------------------------------------
493 * Set our idea of the main target we'll be creating. Used for
497 * gn The main target we'll create
503 * "mainTarg" is set to the main target's node.
504 *-----------------------------------------------------------------------
507 Targ_SetMain(GNode *gn)
513 TargPrintName(void *gnp, void *pflags MAKE_ATTR_UNUSED)
515 GNode *gn = (GNode *)gnp;
517 fprintf(debug_file, "%s%s ", gn->name, gn->cohort_num);
524 Targ_PrintCmd(void *cmd, void *dummy MAKE_ATTR_UNUSED)
526 fprintf(debug_file, "\t%s\n", (char *)cmd);
531 *-----------------------------------------------------------------------
533 * Format a modification time in some reasonable way and return it.
536 * The time reformatted.
539 * The time is placed in a static area, so it is overwritten
542 *-----------------------------------------------------------------------
545 Targ_FmtTime(time_t tm)
548 static char buf[128];
550 parts = localtime(&tm);
551 (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
556 *-----------------------------------------------------------------------
558 * Print out a type field giving only those attributes the user can
565 *-----------------------------------------------------------------------
568 Targ_PrintType(int type)
572 #define PRINTBIT(attr) case CONCAT(OP_,attr): fprintf(debug_file, "." #attr " "); break
573 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))fprintf(debug_file, "." #attr " "); break
578 tbit = 1 << (ffs(type) - 1);
593 /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
594 case OP_MEMBER: if (DEBUG(TARG))fprintf(debug_file, ".MEMBER "); break;
603 made_name(enum enum_made made)
606 case UNMADE: return "unmade";
607 case DEFERRED: return "deferred";
608 case REQUESTED: return "requested";
609 case BEINGMADE: return "being made";
610 case MADE: return "made";
611 case UPTODATE: return "up-to-date";
612 case ERROR: return "error when made";
613 case ABORTED: return "aborted";
614 default: return "unknown enum_made value";
619 *-----------------------------------------------------------------------
621 * print the contents of a node
622 *-----------------------------------------------------------------------
625 Targ_PrintNode(void *gnp, void *passp)
627 GNode *gn = (GNode *)gnp;
628 int pass = passp ? *(int *)passp : 0;
630 fprintf(debug_file, "# %s%s, flags %x, type %x, made %d\n",
631 gn->name, gn->cohort_num, gn->flags, gn->type, gn->made);
635 if (!OP_NOP(gn->type)) {
636 fprintf(debug_file, "#\n");
637 if (gn == mainTarg) {
638 fprintf(debug_file, "# *** MAIN TARGET ***\n");
642 fprintf(debug_file, "# %d unmade children\n", gn->unmade);
644 fprintf(debug_file, "# No unmade children\n");
646 if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
647 if (gn->mtime != 0) {
648 fprintf(debug_file, "# last modified %s: %s\n",
649 Targ_FmtTime(gn->mtime),
650 made_name(gn->made));
651 } else if (gn->made != UNMADE) {
652 fprintf(debug_file, "# non-existent (maybe): %s\n",
653 made_name(gn->made));
655 fprintf(debug_file, "# unmade\n");
658 if (!Lst_IsEmpty (gn->iParents)) {
659 fprintf(debug_file, "# implicit parents: ");
660 Lst_ForEach(gn->iParents, TargPrintName, NULL);
661 fprintf(debug_file, "\n");
665 fprintf(debug_file, "# %d unmade children\n", gn->unmade);
667 if (!Lst_IsEmpty (gn->parents)) {
668 fprintf(debug_file, "# parents: ");
669 Lst_ForEach(gn->parents, TargPrintName, NULL);
670 fprintf(debug_file, "\n");
672 if (!Lst_IsEmpty (gn->order_pred)) {
673 fprintf(debug_file, "# order_pred: ");
674 Lst_ForEach(gn->order_pred, TargPrintName, NULL);
675 fprintf(debug_file, "\n");
677 if (!Lst_IsEmpty (gn->order_succ)) {
678 fprintf(debug_file, "# order_succ: ");
679 Lst_ForEach(gn->order_succ, TargPrintName, NULL);
680 fprintf(debug_file, "\n");
683 fprintf(debug_file, "%-16s", gn->name);
684 switch (gn->type & OP_OPMASK) {
686 fprintf(debug_file, ": "); break;
688 fprintf(debug_file, "! "); break;
690 fprintf(debug_file, ":: "); break;
692 Targ_PrintType(gn->type);
693 Lst_ForEach(gn->children, TargPrintName, NULL);
694 fprintf(debug_file, "\n");
695 Lst_ForEach(gn->commands, Targ_PrintCmd, NULL);
696 fprintf(debug_file, "\n\n");
697 if (gn->type & OP_DOUBLEDEP) {
698 Lst_ForEach(gn->cohorts, Targ_PrintNode, &pass);
705 *-----------------------------------------------------------------------
706 * TargPrintOnlySrc --
707 * Print only those targets that are just a source.
713 * The name of each file is printed preceded by #\t
715 *-----------------------------------------------------------------------
718 TargPrintOnlySrc(void *gnp, void *dummy MAKE_ATTR_UNUSED)
720 GNode *gn = (GNode *)gnp;
721 if (!OP_NOP(gn->type))
724 fprintf(debug_file, "#\t%s [%s] ",
725 gn->name, gn->path ? gn->path : gn->name);
726 Targ_PrintType(gn->type);
727 fprintf(debug_file, "\n");
733 *-----------------------------------------------------------------------
735 * print the entire graph. heh heh
738 * pass Which pass this is. 1 => no processing
739 * 2 => processing done
746 *-----------------------------------------------------------------------
749 Targ_PrintGraph(int pass)
751 fprintf(debug_file, "#*** Input graph:\n");
752 Lst_ForEach(allTargets, Targ_PrintNode, &pass);
753 fprintf(debug_file, "\n\n");
754 fprintf(debug_file, "#\n# Files that are only sources:\n");
755 Lst_ForEach(allTargets, TargPrintOnlySrc, NULL);
756 fprintf(debug_file, "#*** Global Variables:\n");
757 Var_Dump(VAR_GLOBAL);
758 fprintf(debug_file, "#*** Command-line Variables:\n");
760 fprintf(debug_file, "\n");
761 Dir_PrintDirectories();
762 fprintf(debug_file, "\n");
767 *-----------------------------------------------------------------------
768 * TargPropagateNode --
769 * Propagate information from a single node to related nodes if
773 * gnp The node that we are processing.
776 * Always returns 0, for the benefit of Lst_ForEach().
779 * Information is propagated from this node to cohort or child
782 * If the node was defined with "::", then TargPropagateCohort()
783 * will be called for each cohort node.
785 * If the node has recursive predecessors, then
786 * TargPropagateRecpred() will be called for each recursive
788 *-----------------------------------------------------------------------
791 TargPropagateNode(void *gnp, void *junk MAKE_ATTR_UNUSED)
793 GNode *gn = (GNode *)gnp;
795 if (gn->type & OP_DOUBLEDEP)
796 Lst_ForEach(gn->cohorts, TargPropagateCohort, gnp);
801 *-----------------------------------------------------------------------
802 * TargPropagateCohort --
803 * Propagate some bits in the type mask from a node to
804 * a related cohort node.
807 * cnp The node that we are processing.
808 * gnp Another node that has cnp as a cohort.
811 * Always returns 0, for the benefit of Lst_ForEach().
814 * cnp's type bitmask is modified to incorporate some of the
815 * bits from gnp's type bitmask. (XXX need a better explanation.)
816 *-----------------------------------------------------------------------
819 TargPropagateCohort(void *cgnp, void *pgnp)
821 GNode *cgn = (GNode *)cgnp;
822 GNode *pgn = (GNode *)pgnp;
824 cgn->type |= pgn->type & ~OP_OPMASK;
829 *-----------------------------------------------------------------------
831 * Propagate information between related nodes. Should be called
832 * after the makefiles are parsed but before any action is taken.
838 * Information is propagated between related nodes throughout the
840 *-----------------------------------------------------------------------
845 Lst_ForEach(allTargets, TargPropagateNode, NULL);