Annotation of src/usr.bin/make/targ.c, Revision 1.46
1.31 espie 1: /* $OpenPackages$ */
1.46 ! espie 2: /* $OpenBSD: targ.c,v 1.45 2007/09/17 09:28:36 espie Exp $ */
1.6 millert 3: /* $NetBSD: targ.c,v 1.11 1997/02/20 16:51:50 christos Exp $ */
1.1 deraadt 4:
5: /*
1.31 espie 6: * Copyright (c) 1999 Marc Espie.
7: *
8: * Extensive code changes for the OpenBSD project.
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: *
19: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
23: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: */
31: /*
1.5 millert 32: * Copyright (c) 1988, 1989, 1990, 1993
33: * The Regents of the University of California. All rights reserved.
1.1 deraadt 34: * Copyright (c) 1989 by Berkeley Softworks
35: * All rights reserved.
36: *
37: * This code is derived from software contributed to Berkeley by
38: * Adam de Boor.
39: *
40: * Redistribution and use in source and binary forms, with or without
41: * modification, are permitted provided that the following conditions
42: * are met:
43: * 1. Redistributions of source code must retain the above copyright
44: * notice, this list of conditions and the following disclaimer.
45: * 2. Redistributions in binary form must reproduce the above copyright
46: * notice, this list of conditions and the following disclaimer in the
47: * documentation and/or other materials provided with the distribution.
1.38 millert 48: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 49: * may be used to endorse or promote products derived from this software
50: * without specific prior written permission.
51: *
52: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62: * SUCH DAMAGE.
63: */
64:
65: /*-
66: * targ.c --
1.27 espie 67: * Target nodes are kept into a hash table.
1.1 deraadt 68: *
69: * Interface:
1.31 espie 70: * Targ_Init Initialization procedure.
1.1 deraadt 71: *
1.31 espie 72: * Targ_End Cleanup the module
1.1 deraadt 73: *
1.31 espie 74: * Targ_NewGN Create a new GNode for the passed target
75: * (string). The node is *not* placed in the
76: * hash table, though all its fields are
77: * initialized.
1.1 deraadt 78: *
1.31 espie 79: * Targ_FindNode Find the node for a given target, creating
80: * and storing it if it doesn't exist and the
81: * flags are right (TARG_CREATE)
1.1 deraadt 82: *
1.31 espie 83: * Targ_FindList Given a list of names, find nodes for all
84: * of them, creating nodes if needed.
1.1 deraadt 85: *
1.32 espie 86: * Targ_Ignore Return true if errors should be ignored when
1.31 espie 87: * creating the given target.
1.1 deraadt 88: *
1.32 espie 89: * Targ_Silent Return true if we should be silent when
1.31 espie 90: * creating the given target.
1.1 deraadt 91: *
1.32 espie 92: * Targ_Precious Return true if the target is precious and
1.31 espie 93: * should not be removed if we are interrupted.
1.1 deraadt 94: *
95: * Debugging:
1.31 espie 96: * Targ_PrintGraph Print out the entire graphm all variables
97: * and statistics for the directory cache. Should
98: * print something for suffixes, too, but...
1.1 deraadt 99: */
100:
1.34 espie 101: #include <limits.h>
1.32 espie 102: #include <stddef.h>
103: #include <stdio.h>
1.40 espie 104: #include <stdint.h>
1.33 espie 105: #include <string.h>
1.32 espie 106: #include "config.h"
107: #include "defines.h"
108: #include "ohash.h"
109: #include "stats.h"
110: #include "suff.h"
111: #include "var.h"
112: #include "targ.h"
113: #include "memory.h"
114: #include "gnode.h"
115: #include "extern.h"
116: #include "timestamp.h"
117: #include "lst.h"
1.46 ! espie 118: #include "node_int.h"
! 119: #include "nodehashconsts.h"
1.37 espie 120: #ifdef CLEANUP
121: #include <stdlib.h>
122: #endif
1.31 espie 123:
124: static struct ohash targets; /* a hash table of same */
1.46 ! espie 125: struct ohash_info gnode_info = {
! 126: offsetof(GNode, name), NULL, hash_alloc, hash_free, element_alloc
1.42 espie 127: };
1.1 deraadt 128:
1.31 espie 129: static void TargPrintOnlySrc(GNode *);
130: static void TargPrintName(void *);
131: static void TargPrintNode(GNode *, int);
1.10 espie 132: #ifdef CLEANUP
1.37 espie 133: static LIST allTargets;
1.31 espie 134: static void TargFreeGN(void *);
1.10 espie 135: #endif
1.1 deraadt 136:
1.46 ! espie 137: GNode *begin_node, *end_node, *interrupt_node, *DEFAULT;
! 138:
1.1 deraadt 139: void
1.39 espie 140: Targ_Init(void)
1.1 deraadt 141: {
1.42 espie 142: /* A small make file already creates 200 targets. */
143: ohash_init(&targets, 10, &gnode_info);
1.37 espie 144: #ifdef CLEANUP
1.42 espie 145: Lst_Init(&allTargets);
1.37 espie 146: #endif
1.46 ! espie 147: begin_node = Targ_FindConstantNode(NODE_BEGIN, TARG_CREATE);
! 148: begin_node->type |= OP_DUMMY | OP_NOTMAIN | OP_NODEFAULT;
! 149: end_node = Targ_FindConstantNode(NODE_END, TARG_CREATE);
! 150: end_node->type |= OP_DUMMY | OP_NOTMAIN | OP_NODEFAULT;
! 151: interrupt_node = Targ_FindConstantNode(NODE_INTERRUPT, TARG_CREATE);
! 152: interrupt_node->type |= OP_DUMMY | OP_NOTMAIN | OP_NODEFAULT;
! 153: DEFAULT = Targ_FindConstantNode(NODE_DEFAULT, TARG_CREATE);
! 154: DEFAULT->type |= OP_DUMMY | OP_NOTMAIN| OP_TRANSFORM | OP_NODEFAULT;
! 155:
1.1 deraadt 156: }
157:
1.32 espie 158: #ifdef CLEANUP
1.1 deraadt 159: void
1.39 espie 160: Targ_End(void)
1.1 deraadt 161: {
1.42 espie 162: Lst_Every(&allTargets, TargFreeGN);
163: ohash_delete(&targets);
1.32 espie 164: }
1.10 espie 165: #endif
1.1 deraadt 166:
167: GNode *
1.46 ! espie 168: Targ_NewGNi(const char *name, const char *ename)
1.1 deraadt 169: {
1.42 espie 170: GNode *gn;
1.1 deraadt 171:
1.42 espie 172: gn = ohash_create_entry(&gnode_info, name, &ename);
173: gn->path = NULL;
1.46 ! espie 174: if (name[0] == '-' && name[1] == 'l')
1.42 espie 175: gn->type = OP_LIB;
1.46 ! espie 176: else
1.42 espie 177: gn->type = 0;
1.46 ! espie 178:
! 179: gn->special = SPECIAL_NONE;
! 180: gn->unmade = 0;
! 181: gn->make = false;
! 182: gn->made = UNMADE;
1.42 espie 183: gn->childMade = false;
1.46 ! espie 184: gn->order = 0;
1.42 espie 185: ts_set_out_of_date(gn->mtime);
186: ts_set_out_of_date(gn->cmtime);
187: Lst_Init(&gn->iParents);
188: Lst_Init(&gn->cohorts);
189: Lst_Init(&gn->parents);
190: Lst_Init(&gn->children);
191: Lst_Init(&gn->successors);
192: Lst_Init(&gn->preds);
193: SymTable_Init(&gn->context);
194: gn->lineno = 0;
195: gn->fname = NULL;
196: Lst_Init(&gn->commands);
197: gn->suffix = NULL;
1.1 deraadt 198:
1.31 espie 199: #ifdef STATS_GN_CREATION
1.42 espie 200: STAT_GN_COUNT++;
1.10 espie 201: #endif
1.1 deraadt 202:
1.37 espie 203: #ifdef CLEANUP
1.42 espie 204: Lst_AtEnd(&allTargets, gn);
1.37 espie 205: #endif
1.42 espie 206: return gn;
1.1 deraadt 207: }
208:
1.10 espie 209: #ifdef CLEANUP
1.1 deraadt 210: static void
1.39 espie 211: TargFreeGN(void *gnp)
1.1 deraadt 212: {
1.42 espie 213: GNode *gn = (GNode *)gnp;
1.1 deraadt 214:
1.42 espie 215: efree(gn->path);
216: Lst_Destroy(&gn->iParents, NOFREE);
217: Lst_Destroy(&gn->cohorts, NOFREE);
218: Lst_Destroy(&gn->parents, NOFREE);
219: Lst_Destroy(&gn->children, NOFREE);
220: Lst_Destroy(&gn->successors, NOFREE);
221: Lst_Destroy(&gn->preds, NOFREE);
222: Lst_Destroy(&gn->commands, NOFREE);
223: SymTable_Destroy(&gn->context);
224: free(gn);
1.1 deraadt 225: }
1.10 espie 226: #endif
1.1 deraadt 227:
1.46 ! espie 228: GNode *
! 229: Targ_FindNodei(const char *name, const char *ename, int flags)
! 230: {
! 231: uint32_t hv;
! 232:
! 233: hv = ohash_interval(name, &ename);
! 234: return Targ_FindNodeih(name, ename, hv, flags);
! 235: }
1.1 deraadt 236:
237: GNode *
1.46 ! espie 238: Targ_FindNodeih(const char *name, const char *ename, uint32_t hv, int flags)
1.1 deraadt 239: {
1.46 ! espie 240: GNode *gn;
1.42 espie 241: unsigned int slot;
1.27 espie 242:
1.46 ! espie 243: slot = ohash_lookup_interval(&targets, name, ename, hv);
1.27 espie 244:
1.42 espie 245: gn = ohash_find(&targets, slot);
1.31 espie 246:
1.42 espie 247: if (gn == NULL && (flags & TARG_CREATE)) {
248: gn = Targ_NewGNi(name, ename);
249: ohash_insert(&targets, slot, gn);
250: }
1.1 deraadt 251:
1.42 espie 252: return gn;
1.1 deraadt 253: }
254:
1.20 espie 255: void
1.46 ! espie 256: Targ_FindList(Lst nodes, Lst names)
1.1 deraadt 257: {
1.46 ! espie 258: LstNode ln;
! 259: GNode *gn;
1.42 espie 260: char *name;
261:
262: for (ln = Lst_First(names); ln != NULL; ln = Lst_Adv(ln)) {
263: name = (char *)Lst_Datum(ln);
264: gn = Targ_FindNode(name, TARG_CREATE);
265: /* Note: Lst_AtEnd must come before the Lst_Concat so the nodes
266: * are added to the list in the order in which they were
267: * encountered in the makefile. */
268: Lst_AtEnd(nodes, gn);
269: if (gn->type & OP_DOUBLEDEP)
270: Lst_Concat(nodes, &gn->cohorts);
271: }
1.1 deraadt 272: }
273:
1.32 espie 274: bool
1.39 espie 275: Targ_Ignore(GNode *gn)
1.1 deraadt 276: {
1.42 espie 277: if (ignoreErrors || gn->type & OP_IGNORE)
278: return true;
279: else
280: return false;
1.1 deraadt 281: }
282:
1.32 espie 283: bool
1.39 espie 284: Targ_Silent(GNode *gn)
1.1 deraadt 285: {
1.42 espie 286: if (beSilent || gn->type & OP_SILENT)
287: return true;
288: else
289: return false;
1.1 deraadt 290: }
291:
1.32 espie 292: bool
1.39 espie 293: Targ_Precious(GNode *gn)
1.1 deraadt 294: {
1.42 espie 295: if (allPrecious || (gn->type & (OP_PRECIOUS|OP_DOUBLEDEP)))
296: return true;
297: else
298: return false;
1.1 deraadt 299: }
300:
1.17 espie 301: static void
1.39 espie 302: TargPrintName(void *gnp)
1.1 deraadt 303: {
1.42 espie 304: GNode *gn = (GNode *)gnp;
305: printf("%s ", gn->name);
1.1 deraadt 306: }
307:
308:
1.17 espie 309: void
1.39 espie 310: Targ_PrintCmd(void *cmd)
1.1 deraadt 311: {
1.42 espie 312: printf("\t%s\n", (char *)cmd);
1.1 deraadt 313: }
314:
315: void
1.39 espie 316: Targ_PrintType(int type)
1.1 deraadt 317: {
1.42 espie 318: int tbit;
1.5 millert 319:
1.46 ! espie 320: #define PRINTBIT(attr) case CONCAT(OP_,attr): printf("." #attr " "); break
! 321: #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG)) printf("." #attr " "); break
1.42 espie 322:
323: type &= ~OP_OPMASK;
324:
325: while (type) {
326: tbit = 1 << (ffs(type) - 1);
327: type &= ~tbit;
328:
329: switch (tbit) {
330: PRINTBIT(OPTIONAL);
331: PRINTBIT(USE);
332: PRINTBIT(EXEC);
333: PRINTBIT(IGNORE);
334: PRINTBIT(PRECIOUS);
335: PRINTBIT(SILENT);
336: PRINTBIT(MAKE);
337: PRINTBIT(JOIN);
338: PRINTBIT(INVISIBLE);
339: PRINTBIT(NOTMAIN);
340: PRINTDBIT(LIB);
341: /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
1.45 espie 342: case OP_MEMBER:
343: if (DEBUG(TARG))
344: printf(".MEMBER ");
1.42 espie 345: break;
346: PRINTDBIT(ARCHV);
347: }
1.46 ! espie 348: }
1.1 deraadt 349: }
350:
1.17 espie 351: static void
1.39 espie 352: TargPrintNode(GNode *gn, int pass)
1.1 deraadt 353: {
1.42 espie 354: if (OP_NOP(gn->type))
355: return;
1.1 deraadt 356: printf("#\n");
357: if (pass == 2) {
1.46 ! espie 358: printf("# %d unmade children\n", gn->unmade);
1.42 espie 359: if (! (gn->type & (OP_JOIN|OP_USE|OP_EXEC))) {
360: if (!is_out_of_date(gn->mtime)) {
361: printf("# last modified %s: %s\n",
1.46 ! espie 362: time_to_string(gn->mtime),
! 363: (gn->made == UNMADE ? "unmade" :
! 364: (gn->made == MADE ? "made" :
! 365: (gn->made == UPTODATE ? "up-to-date" :
! 366: "error when made"))));
1.42 espie 367: } else if (gn->made != UNMADE) {
368: printf("# non-existent (maybe): %s\n",
1.46 ! espie 369: (gn->made == MADE ? "made" :
! 370: (gn->made == UPTODATE ? "up-to-date" :
! 371: (gn->made == ERROR ? "error when made" :
! 372: "aborted"))));
1.42 espie 373: } else {
374: printf("# unmade\n");
375: }
376: }
377: if (!Lst_IsEmpty(&gn->iParents)) {
378: printf("# implicit parents: ");
379: Lst_Every(&gn->iParents, TargPrintName);
380: fputc('\n', stdout);
1.31 espie 381: }
1.1 deraadt 382: }
1.19 espie 383: if (!Lst_IsEmpty(&gn->parents)) {
1.42 espie 384: printf("# parents: ");
385: Lst_Every(&gn->parents, TargPrintName);
386: fputc('\n', stdout);
1.1 deraadt 387: }
1.5 millert 388:
1.1 deraadt 389: printf("%-16s", gn->name);
390: switch (gn->type & OP_OPMASK) {
1.42 espie 391: case OP_DEPENDS:
1.1 deraadt 392: printf(": "); break;
1.42 espie 393: case OP_FORCE:
1.1 deraadt 394: printf("! "); break;
1.42 espie 395: case OP_DOUBLEDEP:
1.1 deraadt 396: printf(":: "); break;
397: }
1.17 espie 398: Targ_PrintType(gn->type);
1.19 espie 399: Lst_Every(&gn->children, TargPrintName);
1.17 espie 400: fputc('\n', stdout);
1.19 espie 401: Lst_Every(&gn->commands, Targ_PrintCmd);
1.1 deraadt 402: printf("\n\n");
1.27 espie 403: if (gn->type & OP_DOUBLEDEP) {
1.42 espie 404: LstNode ln;
1.31 espie 405:
1.42 espie 406: for (ln = Lst_First(&gn->cohorts); ln != NULL; ln = Lst_Adv(ln))
407: TargPrintNode((GNode *)Lst_Datum(ln), pass);
1.27 espie 408: }
1.1 deraadt 409: }
410:
1.17 espie 411: static void
1.39 espie 412: TargPrintOnlySrc(GNode *gn)
1.1 deraadt 413: {
1.42 espie 414: if (OP_NOP(gn->type))
415: printf("#\t%s [%s]\n", gn->name,
416: gn->path != NULL ? gn->path : gn->name);
1.1 deraadt 417: }
418:
419: void
1.39 espie 420: Targ_PrintGraph(int pass) /* Which pass this is. 1 => no processing
1.31 espie 421: * 2 => processing done */
1.1 deraadt 422: {
1.42 espie 423: GNode *gn;
424: unsigned int i;
1.27 espie 425:
1.42 espie 426: printf("#*** Input graph:\n");
427: for (gn = ohash_first(&targets, &i); gn != NULL;
428: gn = ohash_next(&targets, &i))
429: TargPrintNode(gn, pass);
430: printf("\n\n");
431: printf("#\n# Files that are only sources:\n");
432: for (gn = ohash_first(&targets, &i); gn != NULL;
433: gn = ohash_next(&targets, &i))
1.46 ! espie 434: TargPrintOnlySrc(gn);
1.42 espie 435: Var_Dump();
436: printf("\n");
1.46 ! espie 437: #ifdef DEBUG_DIRECTORY_CACHE
! 438: Dir_PrintDirectories();
! 439: printf("\n");
! 440: #endif
1.42 espie 441: Suff_PrintAll();
1.1 deraadt 442: }