Home | History | Annotate | Line # | Download | only in make
targ.c revision 1.152
      1  1.152    rillig /*	$NetBSD: targ.c,v 1.152 2020/12/05 18:38:02 rillig Exp $	*/
      2    1.5  christos 
      3    1.1       cgd /*
      4   1.10  christos  * Copyright (c) 1988, 1989, 1990, 1993
      5   1.10  christos  *	The Regents of the University of California.  All rights reserved.
      6   1.30       agc  *
      7   1.30       agc  * This code is derived from software contributed to Berkeley by
      8   1.30       agc  * Adam de Boor.
      9   1.30       agc  *
     10   1.30       agc  * Redistribution and use in source and binary forms, with or without
     11   1.30       agc  * modification, are permitted provided that the following conditions
     12   1.30       agc  * are met:
     13   1.30       agc  * 1. Redistributions of source code must retain the above copyright
     14   1.30       agc  *    notice, this list of conditions and the following disclaimer.
     15   1.30       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.30       agc  *    notice, this list of conditions and the following disclaimer in the
     17   1.30       agc  *    documentation and/or other materials provided with the distribution.
     18   1.30       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.30       agc  *    may be used to endorse or promote products derived from this software
     20   1.30       agc  *    without specific prior written permission.
     21   1.30       agc  *
     22   1.30       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.30       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.30       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.30       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.30       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.30       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.30       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.30       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.30       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.30       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.30       agc  * SUCH DAMAGE.
     33   1.30       agc  */
     34   1.30       agc 
     35   1.30       agc /*
     36    1.1       cgd  * Copyright (c) 1989 by Berkeley Softworks
     37    1.1       cgd  * All rights reserved.
     38    1.1       cgd  *
     39    1.1       cgd  * This code is derived from software contributed to Berkeley by
     40    1.1       cgd  * Adam de Boor.
     41    1.1       cgd  *
     42    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     43    1.1       cgd  * modification, are permitted provided that the following conditions
     44    1.1       cgd  * are met:
     45    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     46    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     47    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     48    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     49    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     50    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     51    1.1       cgd  *    must display the following acknowledgement:
     52    1.1       cgd  *	This product includes software developed by the University of
     53    1.1       cgd  *	California, Berkeley and its contributors.
     54    1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     55    1.1       cgd  *    may be used to endorse or promote products derived from this software
     56    1.1       cgd  *    without specific prior written permission.
     57    1.1       cgd  *
     58    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68    1.1       cgd  * SUCH DAMAGE.
     69    1.1       cgd  */
     70    1.1       cgd 
     71  1.132    rillig /*
     72  1.132    rillig  * Maintaining the targets and sources, which are both implemented as GNode.
     73    1.1       cgd  *
     74    1.1       cgd  * Interface:
     75  1.132    rillig  *	Targ_Init	Initialize the module.
     76    1.1       cgd  *
     77  1.132    rillig  *	Targ_End	Clean up the module.
     78    1.4       jtc  *
     79  1.106    rillig  *	Targ_List	Return the list of all targets so far.
     80   1.12  christos  *
     81  1.128    rillig  *	GNode_New	Create a new GNode for the passed target
     82  1.106    rillig  *			(string). The node is *not* placed in the
     83  1.106    rillig  *			hash table, though all its fields are
     84  1.106    rillig  *			initialized.
     85    1.1       cgd  *
     86  1.106    rillig  *	Targ_FindNode	Find the node, or return NULL.
     87   1.98    rillig  *
     88  1.106    rillig  *	Targ_GetNode	Find the node, or create it.
     89   1.98    rillig  *
     90  1.106    rillig  *	Targ_NewInternalNode
     91  1.106    rillig  *			Create an internal node.
     92    1.1       cgd  *
     93  1.106    rillig  *	Targ_FindList	Given a list of names, find nodes for all
     94  1.106    rillig  *			of them, creating them as necessary.
     95    1.1       cgd  *
     96  1.106    rillig  *	Targ_Ignore	Return TRUE if errors should be ignored when
     97  1.106    rillig  *			creating the given target.
     98    1.1       cgd  *
     99  1.106    rillig  *	Targ_Silent	Return TRUE if we should be silent when
    100  1.106    rillig  *			creating the given target.
    101    1.1       cgd  *
    102  1.106    rillig  *	Targ_Precious	Return TRUE if the target is precious and
    103  1.106    rillig  *			should not be removed if we are interrupted.
    104    1.1       cgd  *
    105  1.106    rillig  *	Targ_Propagate	Propagate information between related nodes.
    106  1.106    rillig  *			Should be called after the makefiles are parsed
    107  1.106    rillig  *			but before any action is taken.
    108   1.42       apb  *
    109    1.1       cgd  * Debugging:
    110  1.106    rillig  *	Targ_PrintGraph
    111  1.148    rillig  *			Print out the entire graph, all variables and
    112  1.106    rillig  *			statistics for the directory cache. Should print
    113  1.106    rillig  *			something for suffixes, too, but...
    114    1.1       cgd  */
    115    1.1       cgd 
    116  1.111    rillig #include <time.h>
    117   1.28       wiz 
    118  1.111    rillig #include "make.h"
    119  1.111    rillig #include "dir.h"
    120    1.1       cgd 
    121   1.88    rillig /*	"@(#)targ.c	8.2 (Berkeley) 3/19/94"	*/
    122  1.152    rillig MAKE_RCSID("$NetBSD: targ.c,v 1.152 2020/12/05 18:38:02 rillig Exp $");
    123   1.88    rillig 
    124  1.136    rillig /*
    125  1.136    rillig  * All target nodes that appeared on the left-hand side of one of the
    126  1.136    rillig  * dependency operators ':', '::', '!'.
    127  1.136    rillig  */
    128  1.148    rillig static GNodeList allTargets = LST_INIT;
    129  1.133    rillig static HashTable allTargetsByName;
    130  1.130    rillig 
    131   1.18   mycroft #ifdef CLEANUP
    132  1.148    rillig static GNodeList allNodes = LST_INIT;
    133    1.1       cgd 
    134  1.129    rillig static void GNode_Free(void *);
    135   1.18   mycroft #endif
    136    1.4       jtc 
    137    1.1       cgd void
    138   1.28       wiz Targ_Init(void)
    139    1.1       cgd {
    140  1.150    rillig 	HashTable_Init(&allTargetsByName);
    141    1.1       cgd }
    142    1.1       cgd 
    143    1.4       jtc void
    144   1.28       wiz Targ_End(void)
    145    1.4       jtc {
    146  1.150    rillig 	Targ_Stats();
    147   1.18   mycroft #ifdef CLEANUP
    148  1.150    rillig 	Lst_Done(&allTargets);
    149  1.150    rillig 	HashTable_Done(&allTargetsByName);
    150  1.150    rillig 	Lst_DoneCall(&allNodes, GNode_Free);
    151   1.18   mycroft #endif
    152   1.12  christos }
    153   1.12  christos 
    154   1.64       sjg void
    155   1.64       sjg Targ_Stats(void)
    156   1.64       sjg {
    157  1.150    rillig 	HashTable_DebugStats(&allTargetsByName, "targets");
    158   1.64       sjg }
    159   1.64       sjg 
    160  1.132    rillig /*
    161  1.132    rillig  * Return the list of all targets, which are all nodes that appear on the
    162  1.132    rillig  * left-hand side of a dependency declaration such as "target: source".
    163  1.132    rillig  * The returned list does not contain pure sources.
    164  1.132    rillig  */
    165   1.89    rillig GNodeList *
    166   1.28       wiz Targ_List(void)
    167   1.12  christos {
    168  1.150    rillig 	return &allTargets;
    169    1.4       jtc }
    170    1.4       jtc 
    171  1.150    rillig /*
    172  1.150    rillig  * Create a new graph node, but don't register it anywhere.
    173    1.1       cgd  *
    174  1.132    rillig  * Graph nodes that appear on the left-hand side of a dependency line such
    175  1.132    rillig  * as "target: source" are called targets.  XXX: In some cases (like the
    176  1.132    rillig  * .ALLTARGETS variable), all nodes are called targets as well, even if they
    177  1.132    rillig  * never appear on the left-hand side.  This is a mistake.
    178  1.132    rillig  *
    179  1.132    rillig  * Typical names for graph nodes are:
    180  1.132    rillig  *	"src.c" (an ordinary file)
    181  1.132    rillig  *	"clean" (a .PHONY target)
    182  1.132    rillig  *	".END" (a special hook target)
    183  1.132    rillig  *	"-lm" (a library)
    184  1.132    rillig  *	"libc.a(isspace.o)" (an archive member)
    185    1.1       cgd  */
    186    1.1       cgd GNode *
    187  1.128    rillig GNode_New(const char *name)
    188    1.1       cgd {
    189  1.150    rillig 	GNode *gn;
    190    1.1       cgd 
    191  1.150    rillig 	gn = bmake_malloc(sizeof *gn);
    192  1.150    rillig 	gn->name = bmake_strdup(name);
    193  1.150    rillig 	gn->uname = NULL;
    194  1.150    rillig 	gn->path = NULL;
    195  1.152    rillig 	gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : OP_NONE;
    196  1.152    rillig 	gn->flags = GNF_NONE;
    197  1.150    rillig 	gn->made = UNMADE;
    198  1.150    rillig 	gn->unmade = 0;
    199  1.150    rillig 	gn->mtime = 0;
    200  1.150    rillig 	gn->youngestChild = NULL;
    201  1.150    rillig 	Lst_Init(&gn->implicitParents);
    202  1.150    rillig 	Lst_Init(&gn->parents);
    203  1.150    rillig 	Lst_Init(&gn->children);
    204  1.150    rillig 	Lst_Init(&gn->order_pred);
    205  1.150    rillig 	Lst_Init(&gn->order_succ);
    206  1.150    rillig 	Lst_Init(&gn->cohorts);
    207  1.150    rillig 	gn->cohort_num[0] = '\0';
    208  1.150    rillig 	gn->unmade_cohorts = 0;
    209  1.150    rillig 	gn->centurion = NULL;
    210  1.150    rillig 	gn->checked_seqno = 0;
    211  1.150    rillig 	HashTable_Init(&gn->vars);
    212  1.150    rillig 	Lst_Init(&gn->commands);
    213  1.150    rillig 	gn->suffix = NULL;
    214  1.150    rillig 	gn->fname = NULL;
    215  1.150    rillig 	gn->lineno = 0;
    216    1.1       cgd 
    217   1.18   mycroft #ifdef CLEANUP
    218  1.150    rillig 	Lst_Append(&allNodes, gn);
    219   1.18   mycroft #endif
    220    1.4       jtc 
    221  1.150    rillig 	return gn;
    222    1.1       cgd }
    223    1.1       cgd 
    224   1.18   mycroft #ifdef CLEANUP
    225    1.4       jtc static void
    226  1.129    rillig GNode_Free(void *gnp)
    227    1.4       jtc {
    228  1.150    rillig 	GNode *gn = gnp;
    229    1.4       jtc 
    230  1.150    rillig 	free(gn->name);
    231  1.150    rillig 	free(gn->uname);
    232  1.150    rillig 	free(gn->path);
    233  1.150    rillig 
    234  1.150    rillig 	/* Don't free gn->youngestChild since it is not owned by this node. */
    235  1.150    rillig 
    236  1.150    rillig 	/*
    237  1.150    rillig 	 * In the following lists, only free the list nodes, but not the
    238  1.150    rillig 	 * GNodes in them since these are not owned by this node.
    239  1.150    rillig 	 */
    240  1.150    rillig 	Lst_Done(&gn->implicitParents);
    241  1.150    rillig 	Lst_Done(&gn->parents);
    242  1.150    rillig 	Lst_Done(&gn->children);
    243  1.150    rillig 	Lst_Done(&gn->order_pred);
    244  1.150    rillig 	Lst_Done(&gn->order_succ);
    245  1.150    rillig 	Lst_Done(&gn->cohorts);
    246  1.150    rillig 
    247  1.150    rillig 	/*
    248  1.150    rillig 	 * Do not free the variables themselves, even though they are owned
    249  1.150    rillig 	 * by this node.
    250  1.150    rillig 	 *
    251  1.150    rillig 	 * XXX: For the nodes that represent targets or sources (and not
    252  1.150    rillig 	 * VAR_GLOBAL), it should be safe to free the variables as well,
    253  1.150    rillig 	 * since each node manages the memory for all its variables itself.
    254  1.150    rillig 	 *
    255  1.150    rillig 	 * XXX: The GNodes that are only used as variable contexts (VAR_CMD,
    256  1.150    rillig 	 * VAR_GLOBAL, VAR_INTERNAL) are not freed at all (see Var_End, where
    257  1.150    rillig 	 * they are not mentioned).  These might be freed at all, if their
    258  1.150    rillig 	 * variable values are indeed not used anywhere else (see Trace_Init
    259  1.150    rillig 	 * for the only suspicious use).
    260  1.150    rillig 	 */
    261  1.150    rillig 	HashTable_Done(&gn->vars);
    262  1.150    rillig 
    263  1.150    rillig 	/*
    264  1.150    rillig 	 * Do not free the commands themselves, as they may be shared with
    265  1.150    rillig 	 * other nodes.
    266  1.150    rillig 	 */
    267  1.150    rillig 	Lst_Done(&gn->commands);
    268  1.150    rillig 
    269  1.150    rillig 	/*
    270  1.150    rillig 	 * gn->suffix is not owned by this node.
    271  1.150    rillig 	 *
    272  1.150    rillig 	 * XXX: gn->suffix should be unreferenced here.  This requires a
    273  1.150    rillig 	 * thorough check that the reference counting is done correctly in
    274  1.150    rillig 	 * all places, otherwise a suffix might be freed too early.
    275  1.150    rillig 	 */
    276   1.77    rillig 
    277  1.150    rillig 	free(gn);
    278    1.4       jtc }
    279   1.18   mycroft #endif
    280    1.4       jtc 
    281   1.95    rillig /* Get the existing global node, or return NULL. */
    282   1.95    rillig GNode *
    283   1.95    rillig Targ_FindNode(const char *name)
    284   1.95    rillig {
    285  1.150    rillig 	return HashTable_FindValue(&allTargetsByName, name);
    286   1.95    rillig }
    287   1.95    rillig 
    288   1.95    rillig /* Get the existing global node, or create it. */
    289   1.95    rillig GNode *
    290   1.95    rillig Targ_GetNode(const char *name)
    291   1.95    rillig {
    292  1.150    rillig 	Boolean isNew;
    293  1.150    rillig 	HashEntry *he = HashTable_CreateEntry(&allTargetsByName, name, &isNew);
    294  1.150    rillig 	if (!isNew)
    295  1.150    rillig 		return HashEntry_Get(he);
    296  1.150    rillig 
    297  1.150    rillig 	{
    298  1.150    rillig 		GNode *gn = Targ_NewInternalNode(name);
    299  1.150    rillig 		HashEntry_Set(he, gn);
    300  1.150    rillig 		return gn;
    301  1.150    rillig 	}
    302   1.95    rillig }
    303   1.95    rillig 
    304  1.132    rillig /*
    305  1.132    rillig  * Create a node, register it in .ALLTARGETS but don't store it in the
    306   1.95    rillig  * table of global nodes.  This means it cannot be found by name.
    307   1.95    rillig  *
    308  1.132    rillig  * This is used for internal nodes, such as cohorts or .WAIT nodes.
    309  1.132    rillig  */
    310   1.95    rillig GNode *
    311   1.95    rillig Targ_NewInternalNode(const char *name)
    312   1.95    rillig {
    313  1.150    rillig 	GNode *gn = GNode_New(name);
    314  1.150    rillig 	Var_Append(".ALLTARGETS", name, VAR_GLOBAL);
    315  1.150    rillig 	Lst_Append(&allTargets, gn);
    316  1.150    rillig 	DEBUG1(TARG, "Adding \"%s\" to all targets.\n", gn->name);
    317  1.150    rillig 	if (doing_depend)
    318  1.150    rillig 		gn->flags |= FROM_DEPEND;
    319  1.150    rillig 	return gn;
    320   1.95    rillig }
    321   1.95    rillig 
    322  1.132    rillig /*
    323  1.132    rillig  * Return the .END node, which contains the commands to be run when
    324  1.132    rillig  * everything else has been made.
    325  1.132    rillig  */
    326   1.90    rillig GNode *Targ_GetEndNode(void)
    327   1.90    rillig {
    328  1.150    rillig 	/*
    329  1.150    rillig 	 * Save the node locally to avoid having to search for it all
    330  1.150    rillig 	 * the time.
    331  1.150    rillig 	 */
    332  1.150    rillig 	static GNode *endNode = NULL;
    333  1.150    rillig 
    334  1.150    rillig 	if (endNode == NULL) {
    335  1.150    rillig 		endNode = Targ_GetNode(".END");
    336  1.150    rillig 		endNode->type = OP_SPECIAL;
    337  1.150    rillig 	}
    338  1.150    rillig 	return endNode;
    339   1.90    rillig }
    340   1.90    rillig 
    341  1.149    rillig /* Add the named nodes to the list, creating them as necessary. */
    342  1.147    rillig void
    343  1.149    rillig Targ_FindList(GNodeList *gns, StringList *names)
    344    1.1       cgd {
    345  1.150    rillig 	StringListNode *ln;
    346  1.149    rillig 
    347  1.150    rillig 	for (ln = names->first; ln != NULL; ln = ln->next) {
    348  1.150    rillig 		const char *name = ln->datum;
    349  1.150    rillig 		GNode *gn = Targ_GetNode(name);
    350  1.150    rillig 		Lst_Append(gns, gn);
    351  1.150    rillig 	}
    352    1.1       cgd }
    353    1.1       cgd 
    354  1.149    rillig /*
    355  1.149    rillig  * Return true if errors from shell commands should be ignored when
    356  1.149    rillig  * creating gn.
    357  1.149    rillig  */
    358    1.1       cgd Boolean
    359  1.131    rillig Targ_Ignore(const GNode *gn)
    360    1.1       cgd {
    361  1.150    rillig 	return opts.ignoreErrors || gn->type & OP_IGNORE;
    362    1.1       cgd }
    363    1.1       cgd 
    364   1.77    rillig /* Return true if be silent when creating gn. */
    365    1.1       cgd Boolean
    366  1.131    rillig Targ_Silent(const GNode *gn)
    367    1.1       cgd {
    368  1.150    rillig 	return opts.beSilent || gn->type & OP_SILENT;
    369    1.1       cgd }
    370    1.1       cgd 
    371   1.77    rillig /* See if the given target is precious. */
    372    1.1       cgd Boolean
    373  1.131    rillig Targ_Precious(const GNode *gn)
    374    1.1       cgd {
    375  1.150    rillig 	/* XXX: Why are '::' targets precious? */
    376  1.150    rillig 	return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
    377    1.1       cgd }
    378    1.1       cgd 
    379  1.132    rillig /*
    380  1.132    rillig  * The main target to be made; only for debugging output.
    381  1.132    rillig  * See mainNode in parse.c for the definitive source.
    382  1.132    rillig  */
    383  1.132    rillig static GNode *mainTarg;
    384   1.77    rillig 
    385  1.132    rillig /* Remember the main target to make; only used for debugging. */
    386    1.1       cgd void
    387   1.28       wiz Targ_SetMain(GNode *gn)
    388    1.1       cgd {
    389  1.150    rillig 	mainTarg = gn;
    390    1.1       cgd }
    391    1.1       cgd 
    392   1.86    rillig static void
    393   1.89    rillig PrintNodeNames(GNodeList *gnodes)
    394    1.1       cgd {
    395  1.150    rillig 	GNodeListNode *ln;
    396   1.38       dsl 
    397  1.150    rillig 	for (ln = gnodes->first; ln != NULL; ln = ln->next) {
    398  1.150    rillig 		GNode *gn = ln->datum;
    399  1.150    rillig 		debug_printf(" %s%s", gn->name, gn->cohort_num);
    400  1.150    rillig 	}
    401   1.85    rillig }
    402   1.38       dsl 
    403   1.85    rillig static void
    404   1.89    rillig PrintNodeNamesLine(const char *label, GNodeList *gnodes)
    405   1.85    rillig {
    406  1.150    rillig 	if (Lst_IsEmpty(gnodes))
    407  1.150    rillig 		return;
    408  1.150    rillig 	debug_printf("# %s:", label);
    409  1.150    rillig 	PrintNodeNames(gnodes);
    410  1.150    rillig 	debug_printf("\n");
    411    1.1       cgd }
    412    1.1       cgd 
    413   1.84    rillig void
    414   1.84    rillig Targ_PrintCmds(GNode *gn)
    415   1.84    rillig {
    416  1.150    rillig 	StringListNode *ln;
    417  1.150    rillig 
    418  1.150    rillig 	for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
    419  1.150    rillig 		const char *cmd = ln->datum;
    420  1.150    rillig 		debug_printf("\t%s\n", cmd);
    421  1.150    rillig 	}
    422   1.84    rillig }
    423   1.84    rillig 
    424  1.150    rillig /*
    425  1.150    rillig  * Format a modification time in some reasonable way and return it.
    426  1.150    rillig  * The time is placed in a static area, so it is overwritten with each call.
    427  1.150    rillig  */
    428    1.1       cgd char *
    429   1.28       wiz Targ_FmtTime(time_t tm)
    430    1.1       cgd {
    431  1.150    rillig 	struct tm *parts;
    432  1.150    rillig 	static char buf[128];
    433    1.1       cgd 
    434  1.150    rillig 	/* TODO: Add special case for 0, which often means ENOENT, to make it
    435  1.150    rillig 	 * independent from time zones. */
    436  1.150    rillig 	parts = localtime(&tm);
    437  1.150    rillig 	(void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
    438  1.150    rillig 	return buf;
    439    1.1       cgd }
    440   1.10  christos 
    441   1.77    rillig /* Print out a type field giving only those attributes the user can set. */
    442    1.1       cgd void
    443   1.52  christos Targ_PrintType(int type)
    444    1.1       cgd {
    445  1.150    rillig 	int tbit;
    446   1.10  christos 
    447  1.150    rillig 	type &= ~OP_OPMASK;
    448    1.1       cgd 
    449  1.150    rillig 	while (type != 0) {
    450  1.150    rillig 		tbit = 1 << (ffs(type) - 1);
    451  1.150    rillig 		type &= ~tbit;
    452  1.150    rillig 
    453  1.150    rillig 		switch (tbit) {
    454  1.151    rillig #define PRINTBIT(bit, name) case bit: debug_printf(" " name); break
    455  1.151    rillig #define PRINTDBIT(bit, attr) case bit: if (DEBUG(TARG)) debug_printf(" " attr); break
    456  1.151    rillig 		PRINTBIT(OP_OPTIONAL, ".OPTIONAL");
    457  1.151    rillig 		PRINTBIT(OP_USE, ".USE");
    458  1.151    rillig 		PRINTBIT(OP_EXEC, ".EXEC");
    459  1.151    rillig 		PRINTBIT(OP_IGNORE, ".IGNORE");
    460  1.151    rillig 		PRINTBIT(OP_PRECIOUS, ".PRECIOUS");
    461  1.151    rillig 		PRINTBIT(OP_SILENT, ".SILENT");
    462  1.151    rillig 		PRINTBIT(OP_MAKE, ".MAKE");
    463  1.151    rillig 		PRINTBIT(OP_JOIN, ".JOIN");
    464  1.151    rillig 		PRINTBIT(OP_INVISIBLE, ".INVISIBLE");
    465  1.151    rillig 		PRINTBIT(OP_NOTMAIN, ".NOTMAIN");
    466  1.151    rillig 		PRINTDBIT(OP_LIB, ".LIB");
    467  1.151    rillig 		PRINTDBIT(OP_MEMBER, ".MEMBER");
    468  1.151    rillig 		PRINTDBIT(OP_ARCHV, ".ARCHV");
    469  1.151    rillig 		PRINTDBIT(OP_MADE, ".MADE");
    470  1.151    rillig 		PRINTDBIT(OP_PHONY, ".PHONY");
    471  1.151    rillig #undef PRINTBIT
    472  1.151    rillig #undef PRINTDBIT
    473  1.150    rillig 		}
    474    1.1       cgd 	}
    475    1.1       cgd }
    476    1.1       cgd 
    477   1.45       dsl static const char *
    478   1.71    rillig made_name(GNodeMade made)
    479   1.45       dsl {
    480  1.150    rillig 	switch (made) {
    481  1.150    rillig 	case UNMADE:    return "unmade";
    482  1.150    rillig 	case DEFERRED:  return "deferred";
    483  1.150    rillig 	case REQUESTED: return "requested";
    484  1.150    rillig 	case BEINGMADE: return "being made";
    485  1.150    rillig 	case MADE:      return "made";
    486  1.150    rillig 	case UPTODATE:  return "up-to-date";
    487  1.150    rillig 	case ERROR:     return "error when made";
    488  1.150    rillig 	case ABORTED:   return "aborted";
    489  1.150    rillig 	default:        return "unknown enum_made value";
    490  1.150    rillig 	}
    491   1.45       dsl }
    492   1.45       dsl 
    493  1.114    rillig static const char *
    494  1.114    rillig GNode_OpName(const GNode *gn)
    495  1.114    rillig {
    496  1.150    rillig 	switch (gn->type & OP_OPMASK) {
    497  1.150    rillig 	case OP_DEPENDS:
    498  1.150    rillig 		return ":";
    499  1.150    rillig 	case OP_FORCE:
    500  1.150    rillig 		return "!";
    501  1.150    rillig 	case OP_DOUBLEDEP:
    502  1.150    rillig 		return "::";
    503  1.150    rillig 	}
    504  1.150    rillig 	return "";
    505  1.114    rillig }
    506  1.114    rillig 
    507  1.105    rillig /* Print the contents of a node. */
    508  1.105    rillig void
    509  1.105    rillig Targ_PrintNode(GNode *gn, int pass)
    510    1.1       cgd {
    511  1.150    rillig 	debug_printf("# %s%s", gn->name, gn->cohort_num);
    512  1.150    rillig 	GNode_FprintDetails(opts.debug_file, ", ", gn, "\n");
    513  1.150    rillig 	if (gn->flags == 0)
    514  1.150    rillig 		return;
    515  1.150    rillig 
    516  1.150    rillig 	if (!GNode_IsTarget(gn))
    517  1.150    rillig 		return;
    518   1.45       dsl 
    519  1.107    rillig 	debug_printf("#\n");
    520  1.150    rillig 	if (gn == mainTarg)
    521  1.150    rillig 		debug_printf("# *** MAIN TARGET ***\n");
    522  1.150    rillig 
    523   1.45       dsl 	if (pass >= 2) {
    524  1.150    rillig 		if (gn->unmade > 0)
    525  1.150    rillig 			debug_printf("# %d unmade children\n", gn->unmade);
    526  1.150    rillig 		else
    527  1.150    rillig 			debug_printf("# No unmade children\n");
    528  1.150    rillig 		if (!(gn->type & (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC))) {
    529  1.150    rillig 			if (gn->mtime != 0) {
    530  1.150    rillig 				debug_printf("# last modified %s: %s\n",
    531  1.150    rillig 				    Targ_FmtTime(gn->mtime),
    532  1.150    rillig 				    made_name(gn->made));
    533  1.150    rillig 			} else if (gn->made != UNMADE) {
    534  1.150    rillig 				debug_printf("# non-existent (maybe): %s\n",
    535  1.150    rillig 				    made_name(gn->made));
    536  1.150    rillig 			} else
    537  1.150    rillig 				debug_printf("# unmade\n");
    538    1.1       cgd 		}
    539  1.150    rillig 		PrintNodeNamesLine("implicit parents", &gn->implicitParents);
    540   1.41       dsl 	} else {
    541  1.150    rillig 		if (gn->unmade)
    542  1.150    rillig 			debug_printf("# %d unmade children\n", gn->unmade);
    543    1.1       cgd 	}
    544  1.150    rillig 
    545  1.143    rillig 	PrintNodeNamesLine("parents", &gn->parents);
    546  1.144    rillig 	PrintNodeNamesLine("order_pred", &gn->order_pred);
    547  1.144    rillig 	PrintNodeNamesLine("order_succ", &gn->order_succ);
    548   1.10  christos 
    549  1.114    rillig 	debug_printf("%-16s%s", gn->name, GNode_OpName(gn));
    550   1.34  christos 	Targ_PrintType(gn->type);
    551  1.143    rillig 	PrintNodeNames(&gn->children);
    552  1.107    rillig 	debug_printf("\n");
    553   1.84    rillig 	Targ_PrintCmds(gn);
    554  1.107    rillig 	debug_printf("\n\n");
    555  1.150    rillig 	if (gn->type & OP_DOUBLEDEP)
    556  1.150    rillig 		Targ_PrintNodes(&gn->cohorts, pass);
    557    1.1       cgd }
    558    1.1       cgd 
    559   1.87    rillig void
    560   1.89    rillig Targ_PrintNodes(GNodeList *gnodes, int pass)
    561   1.87    rillig {
    562  1.150    rillig 	GNodeListNode *ln;
    563  1.150    rillig 
    564  1.150    rillig 	for (ln = gnodes->first; ln != NULL; ln = ln->next)
    565  1.150    rillig 		Targ_PrintNode(ln->datum, pass);
    566   1.87    rillig }
    567   1.87    rillig 
    568  1.100    rillig /* Print only those targets that are just a source. */
    569  1.103       mrg static void
    570  1.100    rillig PrintOnlySources(void)
    571    1.4       jtc {
    572  1.150    rillig 	GNodeListNode *ln;
    573  1.100    rillig 
    574  1.150    rillig 	for (ln = allTargets.first; ln != NULL; ln = ln->next) {
    575  1.150    rillig 		GNode *gn = ln->datum;
    576  1.150    rillig 		if (GNode_IsTarget(gn))
    577  1.150    rillig 			continue;
    578  1.150    rillig 
    579  1.150    rillig 		debug_printf("#\t%s [%s]", gn->name, GNode_Path(gn));
    580  1.150    rillig 		Targ_PrintType(gn->type);
    581  1.150    rillig 		debug_printf("\n");
    582  1.150    rillig 	}
    583    1.1       cgd }
    584    1.1       cgd 
    585  1.150    rillig /*
    586  1.150    rillig  * Input:
    587   1.77    rillig  *	pass		1 => before processing
    588   1.77    rillig  *			2 => after processing
    589   1.77    rillig  *			3 => after processing, an error occurred
    590    1.1       cgd  */
    591    1.3       cgd void
    592   1.28       wiz Targ_PrintGraph(int pass)
    593    1.1       cgd {
    594  1.150    rillig 	debug_printf("#*** Input graph:\n");
    595  1.150    rillig 	Targ_PrintNodes(&allTargets, pass);
    596  1.150    rillig 	debug_printf("\n");
    597  1.150    rillig 	debug_printf("\n");
    598  1.135    rillig 
    599  1.150    rillig 	debug_printf("#\n");
    600  1.150    rillig 	debug_printf("#   Files that are only sources:\n");
    601  1.150    rillig 	PrintOnlySources();
    602  1.135    rillig 
    603  1.150    rillig 	debug_printf("#*** Global Variables:\n");
    604  1.150    rillig 	Var_Dump(VAR_GLOBAL);
    605  1.135    rillig 
    606  1.150    rillig 	debug_printf("#*** Command-line Variables:\n");
    607  1.150    rillig 	Var_Dump(VAR_CMDLINE);
    608  1.135    rillig 
    609  1.150    rillig 	debug_printf("\n");
    610  1.150    rillig 	Dir_PrintDirectories();
    611  1.150    rillig 	debug_printf("\n");
    612  1.135    rillig 
    613  1.150    rillig 	Suff_PrintAll();
    614   1.19   mycroft }
    615   1.19   mycroft 
    616  1.150    rillig /*
    617  1.150    rillig  * Propagate some type information to cohort nodes (those from the '::'
    618   1.78    rillig  * dependency operator).
    619   1.77    rillig  *
    620   1.78    rillig  * Should be called after the makefiles are parsed but before any action is
    621  1.150    rillig  * taken.
    622  1.150    rillig  */
    623   1.78    rillig void
    624   1.78    rillig Targ_Propagate(void)
    625   1.19   mycroft {
    626  1.150    rillig 	GNodeListNode *ln, *cln;
    627   1.45       dsl 
    628  1.150    rillig 	for (ln = allTargets.first; ln != NULL; ln = ln->next) {
    629  1.150    rillig 		GNode *gn = ln->datum;
    630  1.150    rillig 		GNodeType type = gn->type;
    631   1.19   mycroft 
    632  1.150    rillig 		if (!(type & OP_DOUBLEDEP))
    633  1.150    rillig 			continue;
    634   1.42       apb 
    635  1.150    rillig 		for (cln = gn->cohorts.first; cln != NULL; cln = cln->next) {
    636  1.150    rillig 			GNode *cohort = cln->datum;
    637   1.42       apb 
    638  1.150    rillig 			cohort->type |= type & ~OP_OPMASK;
    639  1.150    rillig 		}
    640   1.78    rillig 	}
    641    1.1       cgd }
    642