Home | History | Annotate | Line # | Download | only in make
targ.c revision 1.106
      1 /*	$NetBSD: targ.c,v 1.106 2020/09/27 21:35:16 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
     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	  <stdio.h>
    119 #include	  <time.h>
    120 
    121 #include	  "make.h"
    122 #include	  "dir.h"
    123 
    124 /*	"@(#)targ.c	8.2 (Berkeley) 3/19/94"	*/
    125 MAKE_RCSID("$NetBSD: targ.c,v 1.106 2020/09/27 21:35:16 rillig Exp $");
    126 
    127 static GNodeList *allTargets;	/* the list of all targets found so far */
    128 #ifdef CLEANUP
    129 static GNodeList *allGNs;	/* List of all the GNodes */
    130 #endif
    131 static Hash_Table targets;	/* a hash table of same */
    132 
    133 #ifdef CLEANUP
    134 static void TargFreeGN(void *);
    135 #endif
    136 
    137 void
    138 Targ_Init(void)
    139 {
    140     allTargets = Lst_Init();
    141     Hash_InitTable(&targets);
    142 }
    143 
    144 void
    145 Targ_End(void)
    146 {
    147     Targ_Stats();
    148 #ifdef CLEANUP
    149     Lst_Free(allTargets);
    150     if (allGNs != NULL)
    151 	Lst_Destroy(allGNs, TargFreeGN);
    152     Hash_DeleteTable(&targets);
    153 #endif
    154 }
    155 
    156 void
    157 Targ_Stats(void)
    158 {
    159     Hash_DebugStats(&targets, "targets");
    160 }
    161 
    162 /* Return the list of all targets. */
    163 GNodeList *
    164 Targ_List(void)
    165 {
    166     return allTargets;
    167 }
    168 
    169 /* Create and initialize a new graph node. The gnode is added to the list of
    170  * all gnodes.
    171  *
    172  * Input:
    173  *	name		the name of the node, such as "clean", "src.c", ".END"
    174  */
    175 GNode *
    176 Targ_NewGN(const char *name)
    177 {
    178     GNode *gn;
    179 
    180     gn = bmake_malloc(sizeof(GNode));
    181     gn->name = bmake_strdup(name);
    182     gn->uname = NULL;
    183     gn->path = NULL;
    184     gn->type = name[0] == '-' && name[1] == 'l' ? OP_LIB : 0;
    185     gn->unmade = 0;
    186     gn->unmade_cohorts = 0;
    187     gn->cohort_num[0] = 0;
    188     gn->centurion = NULL;
    189     gn->made = UNMADE;
    190     gn->flags = 0;
    191     gn->checked = 0;
    192     gn->mtime = 0;
    193     gn->cmgn = NULL;
    194     gn->implicitParents = Lst_Init();
    195     gn->cohorts = Lst_Init();
    196     gn->parents = Lst_Init();
    197     gn->children = Lst_Init();
    198     gn->order_pred = Lst_Init();
    199     gn->order_succ = Lst_Init();
    200     Hash_InitTable(&gn->context);
    201     gn->commands = Lst_Init();
    202     gn->suffix = NULL;
    203     gn->fname = NULL;
    204     gn->lineno = 0;
    205 
    206 #ifdef CLEANUP
    207     if (allGNs == NULL)
    208 	allGNs = Lst_Init();
    209     Lst_Append(allGNs, gn);
    210 #endif
    211 
    212     return gn;
    213 }
    214 
    215 #ifdef CLEANUP
    216 static void
    217 TargFreeGN(void *gnp)
    218 {
    219     GNode *gn = (GNode *)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     Hash_DeleteTable(&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 Hash_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     Hash_Entry *he = Hash_CreateEntry(&targets, name, &isNew);
    253     if (!isNew)
    254 	return Hash_GetValue(he);
    255 
    256     {
    257 	GNode *gn = Targ_NewInternalNode(name);
    258 	Hash_SetValue(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 = Targ_NewGN(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     GNodeList *nodes;
    296     StringListNode *ln;
    297 
    298     nodes = Lst_Init();
    299 
    300     Lst_Open(names);
    301     while ((ln = Lst_Next(names)) != NULL) {
    302 	char *name = LstNode_Datum(ln);
    303 	GNode *gn = Targ_GetNode(name);
    304 	Lst_Append(nodes, gn);
    305     }
    306     Lst_Close(names);
    307     return nodes;
    308 }
    309 
    310 /* Return true if should ignore errors when creating gn. */
    311 Boolean
    312 Targ_Ignore(GNode *gn)
    313 {
    314     return ignoreErrors || gn->type & OP_IGNORE;
    315 }
    316 
    317 /* Return true if be silent when creating gn. */
    318 Boolean
    319 Targ_Silent(GNode *gn)
    320 {
    321     return beSilent || gn->type & OP_SILENT;
    322 }
    323 
    324 /* See if the given target is precious. */
    325 Boolean
    326 Targ_Precious(GNode *gn)
    327 {
    328     return allPrecious || gn->type & (OP_PRECIOUS | OP_DOUBLEDEP);
    329 }
    330 
    331 /******************* DEBUG INFO PRINTING ****************/
    332 
    333 static GNode	  *mainTarg;	/* the main target, as set by Targ_SetMain */
    334 
    335 /* Set our idea of the main target we'll be creating. Used for debugging
    336  * output. */
    337 void
    338 Targ_SetMain(GNode *gn)
    339 {
    340     mainTarg = gn;
    341 }
    342 
    343 static void
    344 PrintNodeNames(GNodeList *gnodes)
    345 {
    346     GNodeListNode *node;
    347 
    348     for (node = gnodes->first; node != NULL; node = node->next) {
    349 	GNode *gn = node->datum;
    350 	fprintf(debug_file, " %s%s", gn->name, gn->cohort_num);
    351     }
    352 }
    353 
    354 static void
    355 PrintNodeNamesLine(const char *label, GNodeList *gnodes)
    356 {
    357     if (Lst_IsEmpty(gnodes))
    358 	return;
    359     fprintf(debug_file, "# %s:", label);
    360     PrintNodeNames(gnodes);
    361     fprintf(debug_file, "\n");
    362 }
    363 
    364 void
    365 Targ_PrintCmds(GNode *gn)
    366 {
    367     StringListNode *node = gn->commands->first;
    368     for (; node != NULL; node = node->next) {
    369 	const char *cmd = LstNode_Datum(node);
    370 	fprintf(debug_file, "\t%s\n", cmd);
    371     }
    372 }
    373 
    374 /* Format a modification time in some reasonable way and return it.
    375  * The time is placed in a static area, so it is overwritten with each call. */
    376 char *
    377 Targ_FmtTime(time_t tm)
    378 {
    379     struct tm *parts;
    380     static char buf[128];
    381 
    382     parts = localtime(&tm);
    383     (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
    384     return buf;
    385 }
    386 
    387 /* Print out a type field giving only those attributes the user can set. */
    388 void
    389 Targ_PrintType(int type)
    390 {
    391     int    tbit;
    392 
    393 #define PRINTBIT(attr)	case CONCAT(OP_,attr): fprintf(debug_file, " ." #attr); break
    394 #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))fprintf(debug_file, " ." #attr); break
    395 
    396     type &= ~OP_OPMASK;
    397 
    398     while (type) {
    399 	tbit = 1 << (ffs(type) - 1);
    400 	type &= ~tbit;
    401 
    402 	switch(tbit) {
    403 	PRINTBIT(OPTIONAL);
    404 	PRINTBIT(USE);
    405 	PRINTBIT(EXEC);
    406 	PRINTBIT(IGNORE);
    407 	PRINTBIT(PRECIOUS);
    408 	PRINTBIT(SILENT);
    409 	PRINTBIT(MAKE);
    410 	PRINTBIT(JOIN);
    411 	PRINTBIT(INVISIBLE);
    412 	PRINTBIT(NOTMAIN);
    413 	PRINTDBIT(LIB);
    414 	    /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
    415 	case OP_MEMBER: if (DEBUG(TARG))fprintf(debug_file, " .MEMBER"); break;
    416 	PRINTDBIT(ARCHV);
    417 	PRINTDBIT(MADE);
    418 	PRINTDBIT(PHONY);
    419 	}
    420     }
    421 }
    422 
    423 static const char *
    424 made_name(GNodeMade made)
    425 {
    426     switch (made) {
    427     case UNMADE:     return "unmade";
    428     case DEFERRED:   return "deferred";
    429     case REQUESTED:  return "requested";
    430     case BEINGMADE:  return "being made";
    431     case MADE:       return "made";
    432     case UPTODATE:   return "up-to-date";
    433     case ERROR:      return "error when made";
    434     case ABORTED:    return "aborted";
    435     default:         return "unknown enum_made value";
    436     }
    437 }
    438 
    439 /* Print the contents of a node. */
    440 void
    441 Targ_PrintNode(GNode *gn, int pass)
    442 {
    443     fprintf(debug_file, "# %s%s", gn->name, gn->cohort_num);
    444     GNode_FprintDetails(debug_file, ", ", gn, "\n");
    445     if (gn->flags == 0)
    446 	return;
    447 
    448     if (!OP_NOP(gn->type)) {
    449 	fprintf(debug_file, "#\n");
    450 	if (gn == mainTarg) {
    451 	    fprintf(debug_file, "# *** MAIN TARGET ***\n");
    452 	}
    453 	if (pass >= 2) {
    454 	    if (gn->unmade) {
    455 		fprintf(debug_file, "# %d unmade children\n", gn->unmade);
    456 	    } else {
    457 		fprintf(debug_file, "# No unmade children\n");
    458 	    }
    459 	    if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
    460 		if (gn->mtime != 0) {
    461 		    fprintf(debug_file, "# last modified %s: %s\n",
    462 			    Targ_FmtTime(gn->mtime),
    463 			    made_name(gn->made));
    464 		} else if (gn->made != UNMADE) {
    465 		    fprintf(debug_file, "# non-existent (maybe): %s\n",
    466 			    made_name(gn->made));
    467 		} else {
    468 		    fprintf(debug_file, "# unmade\n");
    469 		}
    470 	    }
    471 	    PrintNodeNamesLine("implicit parents", gn->implicitParents);
    472 	} else {
    473 	    if (gn->unmade)
    474 		fprintf(debug_file, "# %d unmade children\n", gn->unmade);
    475 	}
    476 	PrintNodeNamesLine("parents", gn->parents);
    477 	PrintNodeNamesLine("order_pred", gn->order_pred);
    478 	PrintNodeNamesLine("order_succ", gn->order_succ);
    479 
    480 	fprintf(debug_file, "%-16s", gn->name);
    481 	switch (gn->type & OP_OPMASK) {
    482 	case OP_DEPENDS:
    483 	    fprintf(debug_file, ":"); break;
    484 	case OP_FORCE:
    485 	    fprintf(debug_file, "!"); break;
    486 	case OP_DOUBLEDEP:
    487 	    fprintf(debug_file, "::"); break;
    488 	}
    489 	Targ_PrintType(gn->type);
    490 	PrintNodeNames(gn->children);
    491 	fprintf(debug_file, "\n");
    492 	Targ_PrintCmds(gn);
    493 	fprintf(debug_file, "\n\n");
    494 	if (gn->type & OP_DOUBLEDEP) {
    495 	    Targ_PrintNodes(gn->cohorts, pass);
    496 	}
    497     }
    498 }
    499 
    500 void
    501 Targ_PrintNodes(GNodeList *gnodes, int pass)
    502 {
    503     GNodeListNode *ln;
    504     for (ln = gnodes->first; ln != NULL; ln = ln->next)
    505 	Targ_PrintNode(ln->datum, pass);
    506 }
    507 
    508 /* Print only those targets that are just a source. */
    509 static void
    510 PrintOnlySources(void)
    511 {
    512     GNodeListNode *ln;
    513 
    514     for (ln = allTargets->first; ln != NULL; ln = ln->next) {
    515 	GNode *gn = ln->datum;
    516 	if (!OP_NOP(gn->type))
    517 	    continue;
    518 
    519 	fprintf(debug_file, "#\t%s [%s]",
    520 		gn->name, gn->path ? gn->path : gn->name);
    521 	Targ_PrintType(gn->type);
    522 	fprintf(debug_file, "\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     fprintf(debug_file, "#*** Input graph:\n");
    535     Targ_PrintNodes(allTargets, pass);
    536     fprintf(debug_file, "\n\n");
    537     fprintf(debug_file, "#\n#   Files that are only sources:\n");
    538     PrintOnlySources();
    539     fprintf(debug_file, "#*** Global Variables:\n");
    540     Var_Dump(VAR_GLOBAL);
    541     fprintf(debug_file, "#*** Command-line Variables:\n");
    542     Var_Dump(VAR_CMD);
    543     fprintf(debug_file, "\n");
    544     Dir_PrintDirectories();
    545     fprintf(debug_file, "\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 *pn, *cn;
    558 
    559     for (pn = allTargets->first; pn != NULL; pn = pn->next) {
    560 	GNode *pgn = pn->datum;
    561 
    562 	if (!(pgn->type & OP_DOUBLEDEP))
    563 	    continue;
    564 
    565 	for (cn = pgn->cohorts->first; cn != NULL; cn = cn->next) {
    566 	    GNode *cgn = cn->datum;
    567 
    568 	    cgn->type |= pgn->type & ~OP_OPMASK;
    569 	}
    570     }
    571 }
    572