Annotation of src/usr.bin/make/compat.c, Revision 1.78
1.78 ! espie 1: /* $OpenBSD: compat.c,v 1.77 2012/09/21 07:55:20 espie Exp $ */
1.8 millert 2: /* $NetBSD: compat.c,v 1.14 1996/11/06 17:59:01 christos Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
6: * Copyright (c) 1988, 1989 by Adam de Boor
7: * Copyright (c) 1989 by Berkeley Softworks
8: * All rights reserved.
9: *
10: * This code is derived from software contributed to Berkeley by
11: * Adam de Boor.
12: *
13: * Redistribution and use in source and binary forms, with or without
14: * modification, are permitted provided that the following conditions
15: * are met:
16: * 1. Redistributions of source code must retain the above copyright
17: * notice, this list of conditions and the following disclaimer.
18: * 2. Redistributions in binary form must reproduce the above copyright
19: * notice, this list of conditions and the following disclaimer in the
20: * documentation and/or other materials provided with the distribution.
1.49 millert 21: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 22: * may be used to endorse or promote products derived from this software
23: * without specific prior written permission.
24: *
25: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35: * SUCH DAMAGE.
36: */
37:
1.39 espie 38: #include <limits.h>
1.37 espie 39: #include <signal.h>
40: #include <stdio.h>
1.39 espie 41: #include <stdlib.h>
1.37 espie 42: #include "config.h"
43: #include "defines.h"
44: #include "dir.h"
1.55 espie 45: #include "engine.h"
1.77 espie 46: #include "job.h"
1.37 espie 47: #include "compat.h"
48: #include "suff.h"
49: #include "var.h"
50: #include "targ.h"
1.70 espie 51: #include "targequiv.h"
1.37 espie 52: #include "error.h"
53: #include "extern.h"
54: #include "gnode.h"
55: #include "timestamp.h"
56: #include "lst.h"
1.1 deraadt 57:
1.36 espie 58: static void CompatMake(void *, void *);
1.60 espie 59:
1.1 deraadt 60: /*-
61: *-----------------------------------------------------------------------
62: * CompatMake --
63: * Make a target.
64: *
65: * Side Effects:
66: * If an error is detected and not being ignored, the process exits.
67: *-----------------------------------------------------------------------
68: */
1.26 espie 69: static void
1.50 espie 70: CompatMake(void *gnp, /* The node to make */
71: void *pgnp) /* Parent to abort if necessary */
1.1 deraadt 72: {
1.57 espie 73: GNode *gn = (GNode *)gnp;
74: GNode *pgn = (GNode *)pgnp;
75:
1.70 espie 76: GNode *sib;
77: bool cmdsOk;
78:
79: if (DEBUG(MAKE))
1.73 espie 80: printf("CompatMake(%s, %s)\n", pgn ? pgn->name : "NULL",
1.70 espie 81: gn->name);
82:
83: /* XXX some loops are not loops, people write dependencies
84: * between siblings to make sure they get built.
85: * Also, we don't recognize direct loops.
86: */
87: if (gn == pgn)
88: return;
1.78 ! espie 89: /* handle .USE right away */
! 90: if (gn->type & OP_USE) {
! 91: Make_HandleUse(gn, pgn);
! 92: return;
! 93: }
! 94:
1.63 espie 95: look_harder_for_target(gn);
96:
1.70 espie 97: if (pgn != NULL && is_sibling(gn, pgn))
98: return;
99:
100: if (pgn == NULL)
101: pgn = gn;
102:
1.57 espie 103: if (pgn->type & OP_MADE) {
1.70 espie 104: sib = gn;
105: do {
1.72 espie 106: sib->mtime = gn->mtime;
1.70 espie 107: sib->built_status = UPTODATE;
108: sib = sib->sibling;
109: } while (sib != gn);
1.57 espie 110: }
111:
1.78 ! espie 112: switch(gn->built_status) {
! 113: case UNKNOWN:
1.57 espie 114: /* First mark ourselves to be made, then apply whatever
1.60 espie 115: * transformations the suffix module thinks are necessary.
116: * Once that's done, we can descend and make all our children.
117: * If any of them has an error but the -k flag was given,
1.67 espie 118: * our 'must_make' field will be set false again. This is our
1.60 espie 119: * signal to not attempt to do anything but abort our
120: * parent as well. */
1.67 espie 121: gn->must_make = true;
122: gn->built_status = BEINGMADE;
1.70 espie 123: /* note that, in case we have siblings, we only check all
124: * children for all siblings, but we don't try to apply
125: * any other rule.
126: */
127: sib = gn;
128: do {
129: Suff_FindDeps(sib);
130: Lst_ForEach(&sib->children, CompatMake, gn);
131: sib = sib->sibling;
132: } while (sib != gn);
133:
1.67 espie 134: if (!gn->must_make) {
1.70 espie 135: Error("Build for %s aborted", gn->name);
1.67 espie 136: gn->built_status = ABORTED;
137: pgn->must_make = false;
1.57 espie 138: return;
139: }
1.5 millert 140:
1.57 espie 141: /* All the children were made ok. Now cmtime contains the
142: * modification time of the newest child, we need to find out
1.60 espie 143: * if we exist and when we were modified last. The criteria
144: * for datedness are defined by the Make_OODate function. */
1.57 espie 145: if (DEBUG(MAKE))
146: printf("Examining %s...", gn->name);
1.60 espie 147: if (!Make_OODate(gn)) {
1.67 espie 148: gn->built_status = UPTODATE;
1.57 espie 149: if (DEBUG(MAKE))
150: printf("up-to-date.\n");
151: return;
152: } else if (DEBUG(MAKE))
153: printf("out-of-date.\n");
154:
1.60 espie 155: /* If the user is just seeing if something is out-of-date,
156: * exit now to tell him/her "yes". */
1.57 espie 157: if (queryFlag)
1.75 deraadt 158: exit(1);
1.57 espie 159:
1.70 espie 160: /* normally, we run the job, but if we can't find any
161: * commands, we defer to siblings instead.
162: */
163: sib = gn;
164: do {
165: /* We need to be re-made. We also have to make sure
166: * we've got a $? variable. To be nice, we also define
1.73 espie 167: * the $> variable using Make_DoAllVar().
1.70 espie 168: */
169: Make_DoAllVar(sib);
1.77 espie 170: cmdsOk = node_find_valid_commands(sib);
1.70 espie 171: if (cmdsOk || (gn->type & OP_OPTIONAL))
172: break;
173:
174: sib = sib->sibling;
175: } while (sib != gn);
1.57 espie 176:
1.70 espie 177: if (cmdsOk) {
1.62 espie 178: /* Our commands are ok, but we still have to worry
179: * about the -t flag... */
180: if (!touchFlag)
1.70 espie 181: run_gnode(sib);
182: else {
183: Job_Touch(sib);
184: if (gn != sib)
185: Job_Touch(gn);
186: }
187: } else {
1.77 espie 188: node_failure(gn);
1.70 espie 189: sib->built_status = ERROR;
190: }
191:
192: /* copy over what we just did */
193: gn->built_status = sib->built_status;
1.57 espie 194:
1.67 espie 195: if (gn->built_status != ERROR) {
1.57 espie 196: /* If the node was made successfully, mark it so,
1.60 espie 197: * update its modification time and timestamp all
198: * its parents.
199: * This is to keep its state from affecting that of
200: * its parent. */
1.67 espie 201: gn->built_status = MADE;
1.70 espie 202: sib->built_status = MADE;
1.57 espie 203: /* This is what Make does and it's actually a good
204: * thing, as it allows rules like
205: *
206: * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
207: *
208: * to function as intended. Unfortunately, thanks to
1.60 espie 209: * the stateless nature of NFS (and the speed of
210: * this program), there are times when the
211: * modification time of a file created on a remote
212: * machine will not be modified before the stat()
213: * implied by the Dir_MTime occurs, thus leading us
214: * to believe that the file is unchanged, wreaking
215: * havoc with files that depend on this one.
1.57 espie 216: */
217: if (noExecute || is_out_of_date(Dir_MTime(gn)))
1.72 espie 218: ts_set_from_now(gn->mtime);
1.57 espie 219: if (is_strictly_before(gn->mtime, gn->cmtime))
220: gn->mtime = gn->cmtime;
1.70 espie 221: if (sib != gn) {
222: if (noExecute || is_out_of_date(Dir_MTime(sib)))
1.72 espie 223: ts_set_from_now(sib->mtime);
1.70 espie 224: if (is_strictly_before(sib->mtime, sib->cmtime))
225: sib->mtime = sib->cmtime;
226: }
1.57 espie 227: if (DEBUG(MAKE))
1.59 espie 228: printf("update time: %s\n",
1.57 espie 229: time_to_string(gn->mtime));
230: if (!(gn->type & OP_EXEC)) {
231: pgn->childMade = true;
232: Make_TimeStamp(pgn, gn);
233: }
234: } else if (keepgoing)
1.67 espie 235: pgn->must_make = false;
1.57 espie 236: else {
1.77 espie 237: print_errors();
1.57 espie 238: exit(1);
239: }
1.78 ! espie 240: break;
! 241: case ERROR:
1.67 espie 242: /* Already had an error when making this beastie. Tell the
243: * parent to abort. */
244: pgn->must_make = false;
1.78 ! espie 245: break;
! 246: case BEINGMADE:
! 247: Error("Graph cycles through %s", gn->name);
! 248: gn->built_status = ERROR;
! 249: pgn->must_make = false;
! 250: break;
! 251: case MADE:
! 252: if ((gn->type & OP_EXEC) == 0) {
! 253: pgn->childMade = true;
! 254: Make_TimeStamp(pgn, gn);
1.57 espie 255: }
1.78 ! espie 256: break;
! 257: case UPTODATE:
! 258: if ((gn->type & OP_EXEC) == 0)
! 259: Make_TimeStamp(pgn, gn);
! 260: break;
! 261: default:
! 262: break;
1.1 deraadt 263: }
264: }
1.36 espie 265:
1.1 deraadt 266: void
1.50 espie 267: Compat_Run(Lst targs) /* List of target nodes to re-create */
1.1 deraadt 268: {
1.60 espie 269: GNode *gn = NULL; /* Current root target */
270: int errors; /* Number of targets not remade due to errors */
1.57 espie 271:
272: /* For each entry in the list of targets to create, call CompatMake on
1.73 espie 273: * it to create the thing. CompatMake will leave the 'built_status'
1.67 espie 274: * field of gn in one of several states:
1.57 espie 275: * UPTODATE gn was already up-to-date
276: * MADE gn was recreated successfully
1.60 espie 277: * ERROR An error occurred while gn was being
278: * created
1.59 espie 279: * ABORTED gn was not remade because one of its
1.60 espie 280: * inferiors could not be made due to errors.
1.57 espie 281: */
282: errors = 0;
283: while ((gn = (GNode *)Lst_DeQueue(targs)) != NULL) {
1.70 espie 284: CompatMake(gn, NULL);
1.57 espie 285:
1.67 espie 286: if (gn->built_status == UPTODATE)
1.57 espie 287: printf("`%s' is up to date.\n", gn->name);
1.67 espie 288: else if (gn->built_status == ABORTED) {
1.59 espie 289: printf("`%s' not remade because of errors.\n",
1.57 espie 290: gn->name);
1.58 espie 291: errors++;
1.57 espie 292: }
293: }
294:
295: /* If the user has defined a .END target, run its commands. */
296: if (errors == 0)
1.69 espie 297: run_gnode(end_node);
1.1 deraadt 298: }