Home | History | Annotate | Line # | Download | only in make
targ.c revision 1.130
      1 /*	$NetBSD: targ.c,v 1.130 2020/11/16 21:44:29 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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.
     21  *
     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
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1989 by Berkeley Softworks
     37  * All rights reserved.
     38  *
     39  * This code is derived from software contributed to Berkeley by
     40  * Adam de Boor.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     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.
     57  *
     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
     68  * SUCH DAMAGE.
     69  */
     70 
     71 /*-
     72  * targ.c --
     73  *	Functions for maintaining the Lst allTargets. Target nodes are
     74  *	kept in two structures: a Lst and a hash table.
     75  *
     76  * Interface:
     77  *	Targ_Init	Initialization procedure.
     78  *
     79  *	Targ_End	Clean up the module
     80  *
     81  *	Targ_List	Return the list of all targets so far.
     82  *
     83  *	GNode_New	Create a new GNode for the passed target
     84  *			(string). The node is *not* placed in the
     85  *			hash table, though all its fields are
     86  *			initialized.
     87  *
     88  *	Targ_FindNode	Find the node, or return NULL.
     89  *
     90  *	Targ_GetNode	Find the node, or create it.
     91  *
     92  *	Targ_NewInternalNode
     93  *			Create an internal node.
     94  *
     95  *	Targ_FindList	Given a list of names, find nodes for all
     96  *			of them, creating them as necessary.
     97  *
     98  *	Targ_Ignore	Return TRUE if errors should be ignored when
     99  *			creating the given target.
    100  *
    101  *	Targ_Silent	Return TRUE if we should be silent when
    102  *			creating the given target.
    103  *
    104  *	Targ_Precious	Return TRUE if the target is precious and
    105  *			should not be removed if we are interrupted.
    106  *
    107  *	Targ_Propagate	Propagate information between related nodes.
    108  *			Should be called after the makefiles are parsed
    109  *			but before any action is taken.
    110  *
    111  * Debugging:
    112  *	Targ_PrintGraph
    113  *			Print out the entire graphm all variables and
    114  *			statistics for the directory cache. Should print
    115  *			something for suffixes, too, but...
    116  */
    117 
    118 #include <time.h>
    119 
    120 #include "make.h"
    121 #include "dir.h"
    122 
    123 /*	"@(#)targ.c	8.2 (Berkeley) 3/19/94"	*/
    124 MAKE_RCSID("$NetBSD: targ.c,v 1.130 2020/11/16 21:44:29 rillig Exp $");
    125 
    126 static GNodeList *allTargets;	/* the list of all targets found so far */
    127 static HashTable targets;	/* a hash table of same */
    128 
    129 #ifdef CLEANUP
    130 static GNodeList *allGNs;	/* List of all the GNodes */
    131 #endif
    132 
    133 #ifdef CLEANUP
    134 static void GNode_Free(void *);
    135 #endif
    136 
    137 void
    138 Targ_Init(void)
    139 {
    140     allTargets = Lst_New();
    141     HashTable_Init(&targets);
    142 #ifdef CLEANUP
    143     allGNs = Lst_New();
    144 #endif
    145 }
    146 
    147 void
    148 Targ_End(void)
    149 {
    150     Targ_Stats();
    151 #ifdef CLEANUP
    152     Lst_Free(allTargets);
    153     HashTable_Done(&targets);
    154     Lst_Destroy(allGNs, GNode_Free);
    155 #endif
    156 }
    157 
    158 void
    159 Targ_Stats(void)
    160 {
    161     HashTable_DebugStats(&targets, "targets");
    162 }
    163 
    164 /* Return the list of all targets. */
    165 GNodeList *
    166 Targ_List(void)
    167 {
    168     return allTargets;
    169 }
    170 
    171 /* Create and initialize a new graph node. The gnode is added to the list of
    172  * all gnodes.
    173  *
    174  * Input:
    175  *	name		the name of the node, such as "clean", "src.c", ".END"
    176  */
    177 GNode *
    178 GNode_New(const char *name)
    179 {
    180     GNode *gn;
    181 
    182     gn = bmake_malloc(sizeof *gn);
    183     gn->name = bmake_strdup(name);
    184     gn->uname = NULL;
    185     gn->path = NULL;
    186     gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : 0;
    187     gn->unmade = 0;
    188     gn->unmade_cohorts = 0;
    189     gn->cohort_num[0] = '\0';
    190     gn->centurion = NULL;
    191     gn->made = UNMADE;
    192     gn->flags = 0;
    193     gn->checked_seqno = 0;
    194     gn->mtime = 0;
    195     gn->youngestChild = NULL;
    196     gn->implicitParents = Lst_New();
    197     gn->cohorts = Lst_New();
    198     gn->parents = Lst_New();
    199     gn->children = Lst_New();
    200     gn->order_pred = Lst_New();
    201     gn->order_succ = Lst_New();
    202     HashTable_Init(&gn->context);
    203     gn->commands = Lst_New();
    204     gn->suffix = NULL;
    205     gn->fname = NULL;
    206     gn->lineno = 0;
    207 
    208 #ifdef CLEANUP
    209     Lst_Append(allGNs, gn);
    210 #endif
    211 
    212     return gn;
    213 }
    214 
    215 #ifdef CLEANUP
    216 static void
    217 GNode_Free(void *gnp)
    218 {
    219     GNode *gn = gnp;
    220 
    221     free(gn->name);
    222     free(gn->uname);
    223     free(gn->path);
    224 
    225     Lst_Free(gn->implicitParents);
    226     Lst_Free(gn->cohorts);
    227     Lst_Free(gn->parents);
    228     Lst_Free(gn->children);
    229     Lst_Free(gn->order_succ);
    230     Lst_Free(gn->order_pred);
    231     HashTable_Done(&gn->context);
    232     Lst_Free(gn->commands);
    233 
    234     /* XXX: does gn->suffix need to be freed? It is reference-counted. */
    235 
    236     free(gn);
    237 }
    238 #endif
    239 
    240 /* Get the existing global node, or return NULL. */
    241 GNode *
    242 Targ_FindNode(const char *name)
    243 {
    244     return HashTable_FindValue(&targets, name);
    245 }
    246 
    247 /* Get the existing global node, or create it. */
    248 GNode *
    249 Targ_GetNode(const char *name)
    250 {
    251     Boolean isNew;
    252     HashEntry *he = HashTable_CreateEntry(&targets, name, &isNew);
    253     if (!isNew)
    254 	return HashEntry_Get(he);
    255 
    256     {
    257 	GNode *gn = Targ_NewInternalNode(name);
    258 	HashEntry_Set(he, gn);
    259 	return gn;
    260     }
    261 }
    262 
    263 /* Create a node, register it in .ALLTARGETS but don't store it in the
    264  * table of global nodes.  This means it cannot be found by name.
    265  *
    266  * This is used for internal nodes, such as cohorts or .WAIT nodes. */
    267 GNode *
    268 Targ_NewInternalNode(const char *name)
    269 {
    270     GNode *gn = GNode_New(name);
    271     Var_Append(".ALLTARGETS", name, VAR_GLOBAL);
    272     Lst_Append(allTargets, gn);
    273     if (doing_depend)
    274 	gn->flags |= FROM_DEPEND;
    275     return gn;
    276 }
    277 
    278 /* Return the .END node, which contains the commands to be executed when
    279  * everything else is done. */
    280 GNode *Targ_GetEndNode(void)
    281 {
    282     /* Save the node locally to avoid having to search for it all the time. */
    283     static GNode *endNode = NULL;
    284     if (endNode == NULL) {
    285 	endNode = Targ_GetNode(".END");
    286 	endNode->type = OP_SPECIAL;
    287     }
    288     return endNode;
    289 }
    290 
    291 /* Return the named nodes, creating them as necessary. */
    292 GNodeList *
    293 Targ_FindList(StringList *names)
    294 {
    295     StringListNode *ln;
    296     GNodeList *nodes = Lst_New();
    297     for (ln = names->first; ln != NULL; ln = ln->next) {
    298 	const char *name = ln->datum;
    299 	GNode *gn = Targ_GetNode(name);
    300 	Lst_Append(nodes, gn);
    301     }
    302     return nodes;
    303 }
    304 
    305 /* Return true if should ignore errors when creating gn. */
    306 Boolean
    307 Targ_Ignore(GNode *gn)
    308 {
    309     return opts.ignoreErrors || gn->type & OP_IGNORE;
    310 }
    311 
    312 /* Return true if be silent when creating gn. */
    313 Boolean
    314 Targ_Silent(GNode *gn)
    315 {
    316     return opts.beSilent || gn->type & OP_SILENT;
    317 }
    318 
    319 /* See if the given target is precious. */
    320 Boolean
    321 Targ_Precious(GNode *gn)
    322 {
    323     return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
    324 }
    325 
    326 /******************* DEBUG INFO PRINTING ****************/
    327 
    328 static GNode	  *mainTarg;	/* the main target, as set by Targ_SetMain */
    329 
    330 /* Set our idea of the main target we'll be creating. Used for debugging
    331  * output. */
    332 void
    333 Targ_SetMain(GNode *gn)
    334 {
    335     mainTarg = gn;
    336 }
    337 
    338 static void
    339 PrintNodeNames(GNodeList *gnodes)
    340 {
    341     GNodeListNode *node;
    342 
    343     for (node = gnodes->first; node != NULL; node = node->next) {
    344 	GNode *gn = node->datum;
    345 	debug_printf(" %s%s", gn->name, gn->cohort_num);
    346     }
    347 }
    348 
    349 static void
    350 PrintNodeNamesLine(const char *label, GNodeList *gnodes)
    351 {
    352     if (Lst_IsEmpty(gnodes))
    353 	return;
    354     debug_printf("# %s:", label);
    355     PrintNodeNames(gnodes);
    356     debug_printf("\n");
    357 }
    358 
    359 void
    360 Targ_PrintCmds(GNode *gn)
    361 {
    362     StringListNode *ln;
    363     for (ln = gn->commands->first; ln != NULL; ln = ln->next) {
    364 	const char *cmd = ln->datum;
    365 	debug_printf("\t%s\n", cmd);
    366     }
    367 }
    368 
    369 /* Format a modification time in some reasonable way and return it.
    370  * The time is placed in a static area, so it is overwritten with each call. */
    371 char *
    372 Targ_FmtTime(time_t tm)
    373 {
    374     struct tm *parts;
    375     static char buf[128];
    376 
    377     parts = localtime(&tm);
    378     (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
    379     return buf;
    380 }
    381 
    382 /* Print out a type field giving only those attributes the user can set. */
    383 void
    384 Targ_PrintType(int type)
    385 {
    386     int    tbit;
    387 
    388 #define PRINTBIT(attr)	case CONCAT(OP_,attr): debug_printf(" ." #attr); break
    389 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))debug_printf(" ." #attr); break
    390 
    391     type &= ~OP_OPMASK;
    392 
    393     while (type) {
    394 	tbit = 1 << (ffs(type) - 1);
    395 	type &= ~tbit;
    396 
    397 	switch(tbit) {
    398 	PRINTBIT(OPTIONAL);
    399 	PRINTBIT(USE);
    400 	PRINTBIT(EXEC);
    401 	PRINTBIT(IGNORE);
    402 	PRINTBIT(PRECIOUS);
    403 	PRINTBIT(SILENT);
    404 	PRINTBIT(MAKE);
    405 	PRINTBIT(JOIN);
    406 	PRINTBIT(INVISIBLE);
    407 	PRINTBIT(NOTMAIN);
    408 	PRINTDBIT(LIB);
    409 	    /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
    410 	case OP_MEMBER: if (DEBUG(TARG))debug_printf(" .MEMBER"); break;
    411 	PRINTDBIT(ARCHV);
    412 	PRINTDBIT(MADE);
    413 	PRINTDBIT(PHONY);
    414 	}
    415     }
    416 }
    417 
    418 static const char *
    419 made_name(GNodeMade made)
    420 {
    421     switch (made) {
    422     case UNMADE:     return "unmade";
    423     case DEFERRED:   return "deferred";
    424     case REQUESTED:  return "requested";
    425     case BEINGMADE:  return "being made";
    426     case MADE:       return "made";
    427     case UPTODATE:   return "up-to-date";
    428     case ERROR:      return "error when made";
    429     case ABORTED:    return "aborted";
    430     default:         return "unknown enum_made value";
    431     }
    432 }
    433 
    434 static const char *
    435 GNode_OpName(const GNode *gn)
    436 {
    437     switch (gn->type & OP_OPMASK) {
    438     case OP_DEPENDS:
    439 	return ":";
    440     case OP_FORCE:
    441 	return "!";
    442     case OP_DOUBLEDEP:
    443 	return "::";
    444     }
    445     return "";
    446 }
    447 
    448 /* Print the contents of a node. */
    449 void
    450 Targ_PrintNode(GNode *gn, int pass)
    451 {
    452     debug_printf("# %s%s", gn->name, gn->cohort_num);
    453     GNode_FprintDetails(opts.debug_file, ", ", gn, "\n");
    454     if (gn->flags == 0)
    455 	return;
    456 
    457     if (GNode_IsTarget(gn)) {
    458 	debug_printf("#\n");
    459 	if (gn == mainTarg) {
    460 	    debug_printf("# *** MAIN TARGET ***\n");
    461 	}
    462 	if (pass >= 2) {
    463 	    if (gn->unmade) {
    464 		debug_printf("# %d unmade children\n", gn->unmade);
    465 	    } else {
    466 		debug_printf("# No unmade children\n");
    467 	    }
    468 	    if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
    469 		if (gn->mtime != 0) {
    470 		    debug_printf("# last modified %s: %s\n",
    471 			    Targ_FmtTime(gn->mtime),
    472 			    made_name(gn->made));
    473 		} else if (gn->made != UNMADE) {
    474 		    debug_printf("# non-existent (maybe): %s\n",
    475 			    made_name(gn->made));
    476 		} else {
    477 		    debug_printf("# unmade\n");
    478 		}
    479 	    }
    480 	    PrintNodeNamesLine("implicit parents", gn->implicitParents);
    481 	} else {
    482 	    if (gn->unmade)
    483 		debug_printf("# %d unmade children\n", gn->unmade);
    484 	}
    485 	PrintNodeNamesLine("parents", gn->parents);
    486 	PrintNodeNamesLine("order_pred", gn->order_pred);
    487 	PrintNodeNamesLine("order_succ", gn->order_succ);
    488 
    489 	debug_printf("%-16s%s", gn->name, GNode_OpName(gn));
    490 	Targ_PrintType(gn->type);
    491 	PrintNodeNames(gn->children);
    492 	debug_printf("\n");
    493 	Targ_PrintCmds(gn);
    494 	debug_printf("\n\n");
    495 	if (gn->type & OP_DOUBLEDEP) {
    496 	    Targ_PrintNodes(gn->cohorts, pass);
    497 	}
    498     }
    499 }
    500 
    501 void
    502 Targ_PrintNodes(GNodeList *gnodes, int pass)
    503 {
    504     GNodeListNode *ln;
    505     for (ln = gnodes->first; ln != NULL; ln = ln->next)
    506 	Targ_PrintNode(ln->datum, pass);
    507 }
    508 
    509 /* Print only those targets that are just a source. */
    510 static void
    511 PrintOnlySources(void)
    512 {
    513     GNodeListNode *ln;
    514 
    515     for (ln = allTargets->first; ln != NULL; ln = ln->next) {
    516 	GNode *gn = ln->datum;
    517 	if (GNode_IsTarget(gn))
    518 	    continue;
    519 
    520 	debug_printf("#\t%s [%s]", gn->name, GNode_Path(gn));
    521 	Targ_PrintType(gn->type);
    522 	debug_printf("\n");
    523     }
    524 }
    525 
    526 /* Input:
    527  *	pass		1 => before processing
    528  *			2 => after processing
    529  *			3 => after processing, an error occurred
    530  */
    531 void
    532 Targ_PrintGraph(int pass)
    533 {
    534     debug_printf("#*** Input graph:\n");
    535     Targ_PrintNodes(allTargets, pass);
    536     debug_printf("\n\n");
    537     debug_printf("#\n#   Files that are only sources:\n");
    538     PrintOnlySources();
    539     debug_printf("#*** Global Variables:\n");
    540     Var_Dump(VAR_GLOBAL);
    541     debug_printf("#*** Command-line Variables:\n");
    542     Var_Dump(VAR_CMDLINE);
    543     debug_printf("\n");
    544     Dir_PrintDirectories();
    545     debug_printf("\n");
    546     Suff_PrintAll();
    547 }
    548 
    549 /* Propagate some type information to cohort nodes (those from the ::
    550  * dependency operator).
    551  *
    552  * Should be called after the makefiles are parsed but before any action is
    553  * taken. */
    554 void
    555 Targ_Propagate(void)
    556 {
    557     GNodeListNode *ln, *cln;
    558 
    559     for (ln = allTargets->first; ln != NULL; ln = ln->next) {
    560 	GNode *gn = ln->datum;
    561 	GNodeType type = gn->type;
    562 
    563 	if (!(type & OP_DOUBLEDEP))
    564 	    continue;
    565 
    566 	for (cln = gn->cohorts->first; cln != NULL; cln = cln->next) {
    567 	    GNode *cohort = cln->datum;
    568 
    569 	    cohort->type |= type & ~OP_OPMASK;
    570 	}
    571     }
    572 }
    573