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