Home | History | Annotate | Line # | Download | only in make
targ.c revision 1.110
      1 /*	$NetBSD: targ.c,v 1.110 2020/10/05 19:24: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	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.110 2020/10/05 19:24:29 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     StringListNode *ln;
    296     GNodeList *nodes = Lst_Init();
    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 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 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 *node = gn->commands->first;
    363     for (; node != NULL; node = node->next) {
    364 	const char *cmd = LstNode_Datum(node);
    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 /* Print the contents of a node. */
    435 void
    436 Targ_PrintNode(GNode *gn, int pass)
    437 {
    438     debug_printf("# %s%s", gn->name, gn->cohort_num);
    439     GNode_FprintDetails(debug_file, ", ", gn, "\n");
    440     if (gn->flags == 0)
    441 	return;
    442 
    443     if (!OP_NOP(gn->type)) {
    444 	debug_printf("#\n");
    445 	if (gn == mainTarg) {
    446 	    debug_printf("# *** MAIN TARGET ***\n");
    447 	}
    448 	if (pass >= 2) {
    449 	    if (gn->unmade) {
    450 		debug_printf("# %d unmade children\n", gn->unmade);
    451 	    } else {
    452 		debug_printf("# No unmade children\n");
    453 	    }
    454 	    if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
    455 		if (gn->mtime != 0) {
    456 		    debug_printf("# last modified %s: %s\n",
    457 			    Targ_FmtTime(gn->mtime),
    458 			    made_name(gn->made));
    459 		} else if (gn->made != UNMADE) {
    460 		    debug_printf("# non-existent (maybe): %s\n",
    461 			    made_name(gn->made));
    462 		} else {
    463 		    debug_printf("# unmade\n");
    464 		}
    465 	    }
    466 	    PrintNodeNamesLine("implicit parents", gn->implicitParents);
    467 	} else {
    468 	    if (gn->unmade)
    469 		debug_printf("# %d unmade children\n", gn->unmade);
    470 	}
    471 	PrintNodeNamesLine("parents", gn->parents);
    472 	PrintNodeNamesLine("order_pred", gn->order_pred);
    473 	PrintNodeNamesLine("order_succ", gn->order_succ);
    474 
    475 	debug_printf("%-16s", gn->name);
    476 	switch (gn->type & OP_OPMASK) {
    477 	case OP_DEPENDS:
    478 	    debug_printf(":"); break;
    479 	case OP_FORCE:
    480 	    debug_printf("!"); break;
    481 	case OP_DOUBLEDEP:
    482 	    debug_printf("::"); break;
    483 	}
    484 	Targ_PrintType(gn->type);
    485 	PrintNodeNames(gn->children);
    486 	debug_printf("\n");
    487 	Targ_PrintCmds(gn);
    488 	debug_printf("\n\n");
    489 	if (gn->type & OP_DOUBLEDEP) {
    490 	    Targ_PrintNodes(gn->cohorts, pass);
    491 	}
    492     }
    493 }
    494 
    495 void
    496 Targ_PrintNodes(GNodeList *gnodes, int pass)
    497 {
    498     GNodeListNode *ln;
    499     for (ln = gnodes->first; ln != NULL; ln = ln->next)
    500 	Targ_PrintNode(ln->datum, pass);
    501 }
    502 
    503 /* Print only those targets that are just a source. */
    504 static void
    505 PrintOnlySources(void)
    506 {
    507     GNodeListNode *ln;
    508 
    509     for (ln = allTargets->first; ln != NULL; ln = ln->next) {
    510 	GNode *gn = ln->datum;
    511 	if (!OP_NOP(gn->type))
    512 	    continue;
    513 
    514 	debug_printf("#\t%s [%s]",
    515 		gn->name, gn->path ? gn->path : gn->name);
    516 	Targ_PrintType(gn->type);
    517 	debug_printf("\n");
    518     }
    519 }
    520 
    521 /* Input:
    522  *	pass		1 => before processing
    523  *			2 => after processing
    524  *			3 => after processing, an error occurred
    525  */
    526 void
    527 Targ_PrintGraph(int pass)
    528 {
    529     debug_printf("#*** Input graph:\n");
    530     Targ_PrintNodes(allTargets, pass);
    531     debug_printf("\n\n");
    532     debug_printf("#\n#   Files that are only sources:\n");
    533     PrintOnlySources();
    534     debug_printf("#*** Global Variables:\n");
    535     Var_Dump(VAR_GLOBAL);
    536     debug_printf("#*** Command-line Variables:\n");
    537     Var_Dump(VAR_CMD);
    538     debug_printf("\n");
    539     Dir_PrintDirectories();
    540     debug_printf("\n");
    541     Suff_PrintAll();
    542 }
    543 
    544 /* Propagate some type information to cohort nodes (those from the ::
    545  * dependency operator).
    546  *
    547  * Should be called after the makefiles are parsed but before any action is
    548  * taken. */
    549 void
    550 Targ_Propagate(void)
    551 {
    552     GNodeListNode *pn, *cn;
    553 
    554     for (pn = allTargets->first; pn != NULL; pn = pn->next) {
    555 	GNode *pgn = pn->datum;
    556 
    557 	if (!(pgn->type & OP_DOUBLEDEP))
    558 	    continue;
    559 
    560 	for (cn = pgn->cohorts->first; cn != NULL; cn = cn->next) {
    561 	    GNode *cgn = cn->datum;
    562 
    563 	    cgn->type |= pgn->type & ~OP_OPMASK;
    564 	}
    565     }
    566 }
    567