Annotation of src/usr.bin/make/make.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: make.c,v 1.7 1995/06/14 15:19:40 christos Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
! 5: * Copyright (c) 1988, 1989 by Adam de Boor
! 6: * Copyright (c) 1989 by Berkeley Softworks
! 7: * All rights reserved.
! 8: *
! 9: * This code is derived from software contributed to Berkeley by
! 10: * Adam de Boor.
! 11: *
! 12: * Redistribution and use in source and binary forms, with or without
! 13: * modification, are permitted provided that the following conditions
! 14: * are met:
! 15: * 1. Redistributions of source code must retain the above copyright
! 16: * notice, this list of conditions and the following disclaimer.
! 17: * 2. Redistributions in binary form must reproduce the above copyright
! 18: * notice, this list of conditions and the following disclaimer in the
! 19: * documentation and/or other materials provided with the distribution.
! 20: * 3. All advertising materials mentioning features or use of this software
! 21: * must display the following acknowledgement:
! 22: * This product includes software developed by the University of
! 23: * California, Berkeley and its contributors.
! 24: * 4. Neither the name of the University nor the names of its contributors
! 25: * may be used to endorse or promote products derived from this software
! 26: * without specific prior written permission.
! 27: *
! 28: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 29: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 30: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 31: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 32: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 33: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 34: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 35: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 36: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 37: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 38: * SUCH DAMAGE.
! 39: */
! 40:
! 41: #ifndef lint
! 42: #if 0
! 43: static char sccsid[] = "@(#)make.c 5.3 (Berkeley) 6/1/90";
! 44: #else
! 45: static char rcsid[] = "$NetBSD: make.c,v 1.7 1995/06/14 15:19:40 christos Exp $";
! 46: #endif
! 47: #endif /* not lint */
! 48:
! 49: /*-
! 50: * make.c --
! 51: * The functions which perform the examination of targets and
! 52: * their suitability for creation
! 53: *
! 54: * Interface:
! 55: * Make_Run Initialize things for the module and recreate
! 56: * whatever needs recreating. Returns TRUE if
! 57: * work was (or would have been) done and FALSE
! 58: * otherwise.
! 59: *
! 60: * Make_Update Update all parents of a given child. Performs
! 61: * various bookkeeping chores like the updating
! 62: * of the cmtime field of the parent, filling
! 63: * of the IMPSRC context variable, etc. It will
! 64: * place the parent on the toBeMade queue if it
! 65: * should be.
! 66: *
! 67: * Make_TimeStamp Function to set the parent's cmtime field
! 68: * based on a child's modification time.
! 69: *
! 70: * Make_DoAllVar Set up the various local variables for a
! 71: * target, including the .ALLSRC variable, making
! 72: * sure that any variable that needs to exist
! 73: * at the very least has the empty value.
! 74: *
! 75: * Make_OODate Determine if a target is out-of-date.
! 76: *
! 77: * Make_HandleUse See if a child is a .USE node for a parent
! 78: * and perform the .USE actions if so.
! 79: */
! 80:
! 81: #include "make.h"
! 82: #include "hash.h"
! 83: #include "dir.h"
! 84: #include "job.h"
! 85:
! 86: static Lst toBeMade; /* The current fringe of the graph. These
! 87: * are nodes which await examination by
! 88: * MakeOODate. It is added to by
! 89: * Make_Update and subtracted from by
! 90: * MakeStartJobs */
! 91: static int numNodes; /* Number of nodes to be processed. If this
! 92: * is non-zero when Job_Empty() returns
! 93: * TRUE, there's a cycle in the graph */
! 94:
! 95: static int MakeAddChild __P((ClientData, ClientData));
! 96: static int MakeAddAllSrc __P((ClientData, ClientData));
! 97: static int MakeTimeStamp __P((ClientData, ClientData));
! 98: static int MakeHandleUse __P((ClientData, ClientData));
! 99: static Boolean MakeStartJobs __P((void));
! 100: static int MakePrintStatus __P((ClientData, ClientData));
! 101: /*-
! 102: *-----------------------------------------------------------------------
! 103: * Make_TimeStamp --
! 104: * Set the cmtime field of a parent node based on the mtime stamp in its
! 105: * child. Called from MakeOODate via Lst_ForEach.
! 106: *
! 107: * Results:
! 108: * Always returns 0.
! 109: *
! 110: * Side Effects:
! 111: * The cmtime of the parent node will be changed if the mtime
! 112: * field of the child is greater than it.
! 113: *-----------------------------------------------------------------------
! 114: */
! 115: int
! 116: Make_TimeStamp (pgn, cgn)
! 117: GNode *pgn; /* the current parent */
! 118: GNode *cgn; /* the child we've just examined */
! 119: {
! 120: if (cgn->mtime > pgn->cmtime) {
! 121: pgn->cmtime = cgn->mtime;
! 122: }
! 123: return (0);
! 124: }
! 125:
! 126: static int
! 127: MakeTimeStamp (pgn, cgn)
! 128: ClientData pgn; /* the current parent */
! 129: ClientData cgn; /* the child we've just examined */
! 130: {
! 131: return Make_TimeStamp((GNode *) pgn, (GNode *) cgn);
! 132: }
! 133:
! 134: /*-
! 135: *-----------------------------------------------------------------------
! 136: * Make_OODate --
! 137: * See if a given node is out of date with respect to its sources.
! 138: * Used by Make_Run when deciding which nodes to place on the
! 139: * toBeMade queue initially and by Make_Update to screen out USE and
! 140: * EXEC nodes. In the latter case, however, any other sort of node
! 141: * must be considered out-of-date since at least one of its children
! 142: * will have been recreated.
! 143: *
! 144: * Results:
! 145: * TRUE if the node is out of date. FALSE otherwise.
! 146: *
! 147: * Side Effects:
! 148: * The mtime field of the node and the cmtime field of its parents
! 149: * will/may be changed.
! 150: *-----------------------------------------------------------------------
! 151: */
! 152: Boolean
! 153: Make_OODate (gn)
! 154: register GNode *gn; /* the node to check */
! 155: {
! 156: Boolean oodate;
! 157:
! 158: /*
! 159: * Certain types of targets needn't even be sought as their datedness
! 160: * doesn't depend on their modification time...
! 161: */
! 162: if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC)) == 0) {
! 163: (void) Dir_MTime (gn);
! 164: if (DEBUG(MAKE)) {
! 165: if (gn->mtime != 0) {
! 166: printf ("modified %s...", Targ_FmtTime(gn->mtime));
! 167: } else {
! 168: printf ("non-existent...");
! 169: }
! 170: }
! 171: }
! 172:
! 173: /*
! 174: * A target is remade in one of the following circumstances:
! 175: * its modification time is smaller than that of its youngest child
! 176: * and it would actually be run (has commands or type OP_NOP)
! 177: * it's the object of a force operator
! 178: * it has no children, was on the lhs of an operator and doesn't exist
! 179: * already.
! 180: *
! 181: * Libraries are only considered out-of-date if the archive module says
! 182: * they are.
! 183: *
! 184: * These weird rules are brought to you by Backward-Compatability and
! 185: * the strange people who wrote 'Make'.
! 186: */
! 187: if (gn->type & OP_USE) {
! 188: /*
! 189: * If the node is a USE node it is *never* out of date
! 190: * no matter *what*.
! 191: */
! 192: if (DEBUG(MAKE)) {
! 193: printf(".USE node...");
! 194: }
! 195: oodate = FALSE;
! 196: } else if (gn->type & OP_LIB) {
! 197: if (DEBUG(MAKE)) {
! 198: printf("library...");
! 199: }
! 200:
! 201: /*
! 202: * always out of date if no children and :: target
! 203: */
! 204:
! 205: oodate = Arch_LibOODate (gn) ||
! 206: ((gn->cmtime == 0) && (gn->type & OP_DOUBLEDEP));
! 207: } else if (gn->type & OP_JOIN) {
! 208: /*
! 209: * A target with the .JOIN attribute is only considered
! 210: * out-of-date if any of its children was out-of-date.
! 211: */
! 212: if (DEBUG(MAKE)) {
! 213: printf(".JOIN node...");
! 214: }
! 215: oodate = gn->childMade;
! 216: } else if (gn->type & (OP_FORCE|OP_EXEC)) {
! 217: /*
! 218: * A node which is the object of the force (!) operator or which has
! 219: * the .EXEC attribute is always considered out-of-date.
! 220: */
! 221: if (DEBUG(MAKE)) {
! 222: if (gn->type & OP_FORCE) {
! 223: printf("! operator...");
! 224: } else {
! 225: printf(".EXEC node...");
! 226: }
! 227: }
! 228: oodate = TRUE;
! 229: } else if ((gn->mtime < gn->cmtime) ||
! 230: ((gn->cmtime == 0) &&
! 231: ((gn->mtime==0) || (gn->type & OP_DOUBLEDEP))))
! 232: {
! 233: /*
! 234: * A node whose modification time is less than that of its
! 235: * youngest child or that has no children (cmtime == 0) and
! 236: * either doesn't exist (mtime == 0) or was the object of a
! 237: * :: operator is out-of-date. Why? Because that's the way Make does
! 238: * it.
! 239: */
! 240: if (DEBUG(MAKE)) {
! 241: if (gn->mtime < gn->cmtime) {
! 242: printf("modified before source...");
! 243: } else if (gn->mtime == 0) {
! 244: printf("non-existent and no sources...");
! 245: } else {
! 246: printf(":: operator and no sources...");
! 247: }
! 248: }
! 249: oodate = TRUE;
! 250: } else {
! 251: #if 0
! 252: /* WHY? */
! 253: if (DEBUG(MAKE)) {
! 254: printf("source %smade...", gn->childMade ? "" : "not ");
! 255: }
! 256: oodate = gn->childMade;
! 257: #else
! 258: oodate = FALSE;
! 259: #endif /* 0 */
! 260: }
! 261:
! 262: /*
! 263: * If the target isn't out-of-date, the parents need to know its
! 264: * modification time. Note that targets that appear to be out-of-date
! 265: * but aren't, because they have no commands and aren't of type OP_NOP,
! 266: * have their mtime stay below their children's mtime to keep parents from
! 267: * thinking they're out-of-date.
! 268: */
! 269: if (!oodate) {
! 270: Lst_ForEach (gn->parents, MakeTimeStamp, (ClientData)gn);
! 271: }
! 272:
! 273: return (oodate);
! 274: }
! 275:
! 276: /*-
! 277: *-----------------------------------------------------------------------
! 278: * MakeAddChild --
! 279: * Function used by Make_Run to add a child to the list l.
! 280: * It will only add the child if its make field is FALSE.
! 281: *
! 282: * Results:
! 283: * Always returns 0
! 284: *
! 285: * Side Effects:
! 286: * The given list is extended
! 287: *-----------------------------------------------------------------------
! 288: */
! 289: static int
! 290: MakeAddChild (gnp, lp)
! 291: ClientData gnp; /* the node to add */
! 292: ClientData lp; /* the list to which to add it */
! 293: {
! 294: GNode *gn = (GNode *) gnp;
! 295: Lst l = (Lst) lp;
! 296: if (!gn->make && !(gn->type & OP_USE)) {
! 297: (void)Lst_EnQueue (l, (ClientData)gn);
! 298: }
! 299: return (0);
! 300: }
! 301:
! 302: /*-
! 303: *-----------------------------------------------------------------------
! 304: * Make_HandleUse --
! 305: * Function called by Make_Run and SuffApplyTransform on the downward
! 306: * pass to handle .USE and transformation nodes. A callback function
! 307: * for Lst_ForEach, it implements the .USE and transformation
! 308: * functionality by copying the node's commands, type flags
! 309: * and children to the parent node. Should be called before the
! 310: * children are enqueued to be looked at by MakeAddChild.
! 311: *
! 312: * A .USE node is much like an explicit transformation rule, except
! 313: * its commands are always added to the target node, even if the
! 314: * target already has commands.
! 315: *
! 316: * Results:
! 317: * returns 0.
! 318: *
! 319: * Side Effects:
! 320: * Children and commands may be added to the parent and the parent's
! 321: * type may be changed.
! 322: *
! 323: *-----------------------------------------------------------------------
! 324: */
! 325: int
! 326: Make_HandleUse (cgn, pgn)
! 327: register GNode *cgn; /* The .USE node */
! 328: register GNode *pgn; /* The target of the .USE node */
! 329: {
! 330: register GNode *gn; /* A child of the .USE node */
! 331: register LstNode ln; /* An element in the children list */
! 332:
! 333: if (cgn->type & (OP_USE|OP_TRANSFORM)) {
! 334: if ((cgn->type & OP_USE) || Lst_IsEmpty(pgn->commands)) {
! 335: /*
! 336: * .USE or transformation and target has no commands -- append
! 337: * the child's commands to the parent.
! 338: */
! 339: (void) Lst_Concat (pgn->commands, cgn->commands, LST_CONCNEW);
! 340: }
! 341:
! 342: if (Lst_Open (cgn->children) == SUCCESS) {
! 343: while ((ln = Lst_Next (cgn->children)) != NILLNODE) {
! 344: gn = (GNode *)Lst_Datum (ln);
! 345:
! 346: if (Lst_Member (pgn->children, gn) == NILLNODE) {
! 347: (void) Lst_AtEnd (pgn->children, gn);
! 348: (void) Lst_AtEnd (gn->parents, pgn);
! 349: pgn->unmade += 1;
! 350: }
! 351: }
! 352: Lst_Close (cgn->children);
! 353: }
! 354:
! 355: pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
! 356:
! 357: /*
! 358: * This child node is now "made", so we decrement the count of
! 359: * unmade children in the parent... We also remove the child
! 360: * from the parent's list to accurately reflect the number of decent
! 361: * children the parent has. This is used by Make_Run to decide
! 362: * whether to queue the parent or examine its children...
! 363: */
! 364: if (cgn->type & OP_USE) {
! 365: pgn->unmade -= 1;
! 366: }
! 367: }
! 368: return (0);
! 369: }
! 370: static int
! 371: MakeHandleUse (pgn, cgn)
! 372: ClientData pgn; /* the current parent */
! 373: ClientData cgn; /* the child we've just examined */
! 374: {
! 375: return Make_HandleUse((GNode *) pgn, (GNode *) cgn);
! 376: }
! 377:
! 378: /*-
! 379: *-----------------------------------------------------------------------
! 380: * Make_Update --
! 381: * Perform update on the parents of a node. Used by JobFinish once
! 382: * a node has been dealt with and by MakeStartJobs if it finds an
! 383: * up-to-date node.
! 384: *
! 385: * Results:
! 386: * Always returns 0
! 387: *
! 388: * Side Effects:
! 389: * The unmade field of pgn is decremented and pgn may be placed on
! 390: * the toBeMade queue if this field becomes 0.
! 391: *
! 392: * If the child was made, the parent's childMade field will be set true
! 393: * and its cmtime set to now.
! 394: *
! 395: * If the child wasn't made, the cmtime field of the parent will be
! 396: * altered if the child's mtime is big enough.
! 397: *
! 398: * Finally, if the child is the implied source for the parent, the
! 399: * parent's IMPSRC variable is set appropriately.
! 400: *
! 401: *-----------------------------------------------------------------------
! 402: */
! 403: void
! 404: Make_Update (cgn)
! 405: register GNode *cgn; /* the child node */
! 406: {
! 407: register GNode *pgn; /* the parent node */
! 408: register char *cname; /* the child's name */
! 409: register LstNode ln; /* Element in parents and iParents lists */
! 410: char *p1;
! 411:
! 412: cname = Var_Value (TARGET, cgn, &p1);
! 413: if (p1)
! 414: free(p1);
! 415:
! 416: /*
! 417: * If the child was actually made, see what its modification time is
! 418: * now -- some rules won't actually update the file. If the file still
! 419: * doesn't exist, make its mtime now.
! 420: */
! 421: if (cgn->made != UPTODATE) {
! 422: #ifndef RECHECK
! 423: /*
! 424: * We can't re-stat the thing, but we can at least take care of rules
! 425: * where a target depends on a source that actually creates the
! 426: * target, but only if it has changed, e.g.
! 427: *
! 428: * parse.h : parse.o
! 429: *
! 430: * parse.o : parse.y
! 431: * yacc -d parse.y
! 432: * cc -c y.tab.c
! 433: * mv y.tab.o parse.o
! 434: * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
! 435: *
! 436: * In this case, if the definitions produced by yacc haven't changed
! 437: * from before, parse.h won't have been updated and cgn->mtime will
! 438: * reflect the current modification time for parse.h. This is
! 439: * something of a kludge, I admit, but it's a useful one..
! 440: * XXX: People like to use a rule like
! 441: *
! 442: * FRC:
! 443: *
! 444: * To force things that depend on FRC to be made, so we have to
! 445: * check for gn->children being empty as well...
! 446: */
! 447: if (!Lst_IsEmpty(cgn->commands) || Lst_IsEmpty(cgn->children)) {
! 448: cgn->mtime = now;
! 449: }
! 450: #else
! 451: /*
! 452: * This is what Make does and it's actually a good thing, as it
! 453: * allows rules like
! 454: *
! 455: * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
! 456: *
! 457: * to function as intended. Unfortunately, thanks to the stateless
! 458: * nature of NFS (by which I mean the loose coupling of two clients
! 459: * using the same file from a common server), there are times
! 460: * when the modification time of a file created on a remote
! 461: * machine will not be modified before the local stat() implied by
! 462: * the Dir_MTime occurs, thus leading us to believe that the file
! 463: * is unchanged, wreaking havoc with files that depend on this one.
! 464: *
! 465: * I have decided it is better to make too much than to make too
! 466: * little, so this stuff is commented out unless you're sure it's ok.
! 467: * -- ardeb 1/12/88
! 468: */
! 469: /*
! 470: * Christos, 4/9/92: If we are saving commands pretend that
! 471: * the target is made now. Otherwise archives with ... rules
! 472: * don't work!
! 473: */
! 474: if (noExecute || (cgn->type & OP_SAVE_CMDS) || Dir_MTime(cgn) == 0) {
! 475: cgn->mtime = now;
! 476: }
! 477: if (DEBUG(MAKE)) {
! 478: printf("update time: %s\n", Targ_FmtTime(cgn->mtime));
! 479: }
! 480: #endif
! 481: }
! 482:
! 483: if (Lst_Open (cgn->parents) == SUCCESS) {
! 484: while ((ln = Lst_Next (cgn->parents)) != NILLNODE) {
! 485: pgn = (GNode *)Lst_Datum (ln);
! 486: if (pgn->make) {
! 487: pgn->unmade -= 1;
! 488:
! 489: if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
! 490: if (cgn->made == MADE) {
! 491: pgn->childMade = TRUE;
! 492: if (pgn->cmtime < cgn->mtime) {
! 493: pgn->cmtime = cgn->mtime;
! 494: }
! 495: } else {
! 496: (void)Make_TimeStamp (pgn, cgn);
! 497: }
! 498: }
! 499: if (pgn->unmade == 0) {
! 500: /*
! 501: * Queue the node up -- any unmade predecessors will
! 502: * be dealt with in MakeStartJobs.
! 503: */
! 504: (void)Lst_EnQueue (toBeMade, (ClientData)pgn);
! 505: } else if (pgn->unmade < 0) {
! 506: Error ("Graph cycles through %s", pgn->name);
! 507: }
! 508: }
! 509: }
! 510: Lst_Close (cgn->parents);
! 511: }
! 512: /*
! 513: * Deal with successor nodes. If any is marked for making and has an unmade
! 514: * count of 0, has not been made and isn't in the examination queue,
! 515: * it means we need to place it in the queue as it restrained itself
! 516: * before.
! 517: */
! 518: for (ln = Lst_First(cgn->successors); ln != NILLNODE; ln = Lst_Succ(ln)) {
! 519: GNode *succ = (GNode *)Lst_Datum(ln);
! 520:
! 521: if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
! 522: Lst_Member(toBeMade, (ClientData)succ) == NILLNODE)
! 523: {
! 524: (void)Lst_EnQueue(toBeMade, (ClientData)succ);
! 525: }
! 526: }
! 527:
! 528: /*
! 529: * Set the .PREFIX and .IMPSRC variables for all the implied parents
! 530: * of this node.
! 531: */
! 532: if (Lst_Open (cgn->iParents) == SUCCESS) {
! 533: char *p1;
! 534: char *cpref = Var_Value(PREFIX, cgn, &p1);
! 535:
! 536: while ((ln = Lst_Next (cgn->iParents)) != NILLNODE) {
! 537: pgn = (GNode *)Lst_Datum (ln);
! 538: if (pgn->make) {
! 539: Var_Set (IMPSRC, cname, pgn);
! 540: Var_Set (PREFIX, cpref, pgn);
! 541: }
! 542: }
! 543: if (p1)
! 544: free(p1);
! 545: Lst_Close (cgn->iParents);
! 546: }
! 547: }
! 548:
! 549: /*-
! 550: *-----------------------------------------------------------------------
! 551: * MakeAddAllSrc --
! 552: * Add a child's name to the ALLSRC and OODATE variables of the given
! 553: * node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
! 554: * if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
! 555: * .EXEC and .USE children are very rarely going to be files, so...
! 556: * A child is added to the OODATE variable if its modification time is
! 557: * later than that of its parent, as defined by Make, except if the
! 558: * parent is a .JOIN node. In that case, it is only added to the OODATE
! 559: * variable if it was actually made (since .JOIN nodes don't have
! 560: * modification times, the comparison is rather unfair...)..
! 561: *
! 562: * Results:
! 563: * Always returns 0
! 564: *
! 565: * Side Effects:
! 566: * The ALLSRC variable for the given node is extended.
! 567: *-----------------------------------------------------------------------
! 568: */
! 569: static int
! 570: MakeAddAllSrc (cgnp, pgnp)
! 571: ClientData cgnp; /* The child to add */
! 572: ClientData pgnp; /* The parent to whose ALLSRC variable it should be */
! 573: /* added */
! 574: {
! 575: GNode *cgn = (GNode *) cgnp;
! 576: GNode *pgn = (GNode *) pgnp;
! 577: if ((cgn->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) == 0) {
! 578: char *child;
! 579: char *p1;
! 580:
! 581: child = Var_Value(TARGET, cgn, &p1);
! 582: Var_Append (ALLSRC, child, pgn);
! 583: if (pgn->type & OP_JOIN) {
! 584: if (cgn->made == MADE) {
! 585: Var_Append(OODATE, child, pgn);
! 586: }
! 587: } else if ((pgn->mtime < cgn->mtime) ||
! 588: (cgn->mtime >= now && cgn->made == MADE))
! 589: {
! 590: /*
! 591: * It goes in the OODATE variable if the parent is younger than the
! 592: * child or if the child has been modified more recently than
! 593: * the start of the make. This is to keep pmake from getting
! 594: * confused if something else updates the parent after the
! 595: * make starts (shouldn't happen, I know, but sometimes it
! 596: * does). In such a case, if we've updated the kid, the parent
! 597: * is likely to have a modification time later than that of
! 598: * the kid and anything that relies on the OODATE variable will
! 599: * be hosed.
! 600: *
! 601: * XXX: This will cause all made children to go in the OODATE
! 602: * variable, even if they're not touched, if RECHECK isn't defined,
! 603: * since cgn->mtime is set to now in Make_Update. According to
! 604: * some people, this is good...
! 605: */
! 606: Var_Append(OODATE, child, pgn);
! 607: }
! 608: if (p1)
! 609: free(p1);
! 610: }
! 611: return (0);
! 612: }
! 613:
! 614: /*-
! 615: *-----------------------------------------------------------------------
! 616: * Make_DoAllVar --
! 617: * Set up the ALLSRC and OODATE variables. Sad to say, it must be
! 618: * done separately, rather than while traversing the graph. This is
! 619: * because Make defined OODATE to contain all sources whose modification
! 620: * times were later than that of the target, *not* those sources that
! 621: * were out-of-date. Since in both compatibility and native modes,
! 622: * the modification time of the parent isn't found until the child
! 623: * has been dealt with, we have to wait until now to fill in the
! 624: * variable. As for ALLSRC, the ordering is important and not
! 625: * guaranteed when in native mode, so it must be set here, too.
! 626: *
! 627: * Results:
! 628: * None
! 629: *
! 630: * Side Effects:
! 631: * The ALLSRC and OODATE variables of the given node is filled in.
! 632: * If the node is a .JOIN node, its TARGET variable will be set to
! 633: * match its ALLSRC variable.
! 634: *-----------------------------------------------------------------------
! 635: */
! 636: void
! 637: Make_DoAllVar (gn)
! 638: GNode *gn;
! 639: {
! 640: Lst_ForEach (gn->children, MakeAddAllSrc, (ClientData) gn);
! 641:
! 642: if (!Var_Exists (OODATE, gn)) {
! 643: Var_Set (OODATE, "", gn);
! 644: }
! 645: if (!Var_Exists (ALLSRC, gn)) {
! 646: Var_Set (ALLSRC, "", gn);
! 647: }
! 648:
! 649: if (gn->type & OP_JOIN) {
! 650: char *p1;
! 651: Var_Set (TARGET, Var_Value (ALLSRC, gn, &p1), gn);
! 652: if (p1)
! 653: free(p1);
! 654: }
! 655: }
! 656:
! 657: /*-
! 658: *-----------------------------------------------------------------------
! 659: * MakeStartJobs --
! 660: * Start as many jobs as possible.
! 661: *
! 662: * Results:
! 663: * If the query flag was given to pmake, no job will be started,
! 664: * but as soon as an out-of-date target is found, this function
! 665: * returns TRUE. At all other times, this function returns FALSE.
! 666: *
! 667: * Side Effects:
! 668: * Nodes are removed from the toBeMade queue and job table slots
! 669: * are filled.
! 670: *
! 671: *-----------------------------------------------------------------------
! 672: */
! 673: static Boolean
! 674: MakeStartJobs ()
! 675: {
! 676: register GNode *gn;
! 677:
! 678: while (!Job_Full() && !Lst_IsEmpty (toBeMade)) {
! 679: gn = (GNode *) Lst_DeQueue (toBeMade);
! 680: if (DEBUG(MAKE)) {
! 681: printf ("Examining %s...", gn->name);
! 682: }
! 683: /*
! 684: * Make sure any and all predecessors that are going to be made,
! 685: * have been.
! 686: */
! 687: if (!Lst_IsEmpty(gn->preds)) {
! 688: LstNode ln;
! 689:
! 690: for (ln = Lst_First(gn->preds); ln != NILLNODE; ln = Lst_Succ(ln)){
! 691: GNode *pgn = (GNode *)Lst_Datum(ln);
! 692:
! 693: if (pgn->make && pgn->made == UNMADE) {
! 694: if (DEBUG(MAKE)) {
! 695: printf("predecessor %s not made yet.\n", pgn->name);
! 696: }
! 697: break;
! 698: }
! 699: }
! 700: /*
! 701: * If ln isn't nil, there's a predecessor as yet unmade, so we
! 702: * just drop this node on the floor. When the node in question
! 703: * has been made, it will notice this node as being ready to
! 704: * make but as yet unmade and will place the node on the queue.
! 705: */
! 706: if (ln != NILLNODE) {
! 707: continue;
! 708: }
! 709: }
! 710:
! 711: numNodes--;
! 712: if (Make_OODate (gn)) {
! 713: if (DEBUG(MAKE)) {
! 714: printf ("out-of-date\n");
! 715: }
! 716: if (queryFlag) {
! 717: return (TRUE);
! 718: }
! 719: Make_DoAllVar (gn);
! 720: Job_Make (gn);
! 721: } else {
! 722: if (DEBUG(MAKE)) {
! 723: printf ("up-to-date\n");
! 724: }
! 725: gn->made = UPTODATE;
! 726: if (gn->type & OP_JOIN) {
! 727: /*
! 728: * Even for an up-to-date .JOIN node, we need it to have its
! 729: * context variables so references to it get the correct
! 730: * value for .TARGET when building up the context variables
! 731: * of its parent(s)...
! 732: */
! 733: Make_DoAllVar (gn);
! 734: }
! 735:
! 736: Make_Update (gn);
! 737: }
! 738: }
! 739: return (FALSE);
! 740: }
! 741:
! 742: /*-
! 743: *-----------------------------------------------------------------------
! 744: * MakePrintStatus --
! 745: * Print the status of a top-level node, viz. it being up-to-date
! 746: * already or not created due to an error in a lower level.
! 747: * Callback function for Make_Run via Lst_ForEach.
! 748: *
! 749: * Results:
! 750: * Always returns 0.
! 751: *
! 752: * Side Effects:
! 753: * A message may be printed.
! 754: *
! 755: *-----------------------------------------------------------------------
! 756: */
! 757: static int
! 758: MakePrintStatus(gnp, cyclep)
! 759: ClientData gnp; /* Node to examine */
! 760: ClientData cyclep; /* True if gn->unmade being non-zero implies
! 761: * a cycle in the graph, not an error in an
! 762: * inferior */
! 763: {
! 764: GNode *gn = (GNode *) gnp;
! 765: Boolean cycle = *(Boolean *) cyclep;
! 766: if (gn->made == UPTODATE) {
! 767: printf ("`%s' is up to date.\n", gn->name);
! 768: } else if (gn->unmade != 0) {
! 769: if (cycle) {
! 770: Boolean t = TRUE;
! 771: /*
! 772: * If printing cycles and came to one that has unmade children,
! 773: * print out the cycle by recursing on its children. Note a
! 774: * cycle like:
! 775: * a : b
! 776: * b : c
! 777: * c : b
! 778: * will cause this to erroneously complain about a being in
! 779: * the cycle, but this is a good approximation.
! 780: */
! 781: if (gn->made == CYCLE) {
! 782: Error("Graph cycles through `%s'", gn->name);
! 783: gn->made = ENDCYCLE;
! 784: Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
! 785: gn->made = UNMADE;
! 786: } else if (gn->made != ENDCYCLE) {
! 787: gn->made = CYCLE;
! 788: Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
! 789: }
! 790: } else {
! 791: printf ("`%s' not remade because of errors.\n", gn->name);
! 792: }
! 793: }
! 794: return (0);
! 795: }
! 796:
! 797: /*-
! 798: *-----------------------------------------------------------------------
! 799: * Make_Run --
! 800: * Initialize the nodes to remake and the list of nodes which are
! 801: * ready to be made by doing a breadth-first traversal of the graph
! 802: * starting from the nodes in the given list. Once this traversal
! 803: * is finished, all the 'leaves' of the graph are in the toBeMade
! 804: * queue.
! 805: * Using this queue and the Job module, work back up the graph,
! 806: * calling on MakeStartJobs to keep the job table as full as
! 807: * possible.
! 808: *
! 809: * Results:
! 810: * TRUE if work was done. FALSE otherwise.
! 811: *
! 812: * Side Effects:
! 813: * The make field of all nodes involved in the creation of the given
! 814: * targets is set to 1. The toBeMade list is set to contain all the
! 815: * 'leaves' of these subgraphs.
! 816: *-----------------------------------------------------------------------
! 817: */
! 818: Boolean
! 819: Make_Run (targs)
! 820: Lst targs; /* the initial list of targets */
! 821: {
! 822: register GNode *gn; /* a temporary pointer */
! 823: register Lst examine; /* List of targets to examine */
! 824: int errors; /* Number of errors the Job module reports */
! 825:
! 826: toBeMade = Lst_Init (FALSE);
! 827:
! 828: examine = Lst_Duplicate(targs, NOCOPY);
! 829: numNodes = 0;
! 830:
! 831: /*
! 832: * Make an initial downward pass over the graph, marking nodes to be made
! 833: * as we go down. We call Suff_FindDeps to find where a node is and
! 834: * to get some children for it if it has none and also has no commands.
! 835: * If the node is a leaf, we stick it on the toBeMade queue to
! 836: * be looked at in a minute, otherwise we add its children to our queue
! 837: * and go on about our business.
! 838: */
! 839: while (!Lst_IsEmpty (examine)) {
! 840: gn = (GNode *) Lst_DeQueue (examine);
! 841:
! 842: if (!gn->make) {
! 843: gn->make = TRUE;
! 844: numNodes++;
! 845:
! 846: /*
! 847: * Apply any .USE rules before looking for implicit dependencies
! 848: * to make sure everything has commands that should...
! 849: */
! 850: Lst_ForEach (gn->children, MakeHandleUse, (ClientData)gn);
! 851: Suff_FindDeps (gn);
! 852:
! 853: if (gn->unmade != 0) {
! 854: Lst_ForEach (gn->children, MakeAddChild, (ClientData)examine);
! 855: } else {
! 856: (void)Lst_EnQueue (toBeMade, (ClientData)gn);
! 857: }
! 858: }
! 859: }
! 860:
! 861: Lst_Destroy (examine, NOFREE);
! 862:
! 863: if (queryFlag) {
! 864: /*
! 865: * We wouldn't do any work unless we could start some jobs in the
! 866: * next loop... (we won't actually start any, of course, this is just
! 867: * to see if any of the targets was out of date)
! 868: */
! 869: return (MakeStartJobs());
! 870: } else {
! 871: /*
! 872: * Initialization. At the moment, no jobs are running and until some
! 873: * get started, nothing will happen since the remaining upward
! 874: * traversal of the graph is performed by the routines in job.c upon
! 875: * the finishing of a job. So we fill the Job table as much as we can
! 876: * before going into our loop.
! 877: */
! 878: (void) MakeStartJobs();
! 879: }
! 880:
! 881: /*
! 882: * Main Loop: The idea here is that the ending of jobs will take
! 883: * care of the maintenance of data structures and the waiting for output
! 884: * will cause us to be idle most of the time while our children run as
! 885: * much as possible. Because the job table is kept as full as possible,
! 886: * the only time when it will be empty is when all the jobs which need
! 887: * running have been run, so that is the end condition of this loop.
! 888: * Note that the Job module will exit if there were any errors unless the
! 889: * keepgoing flag was given.
! 890: */
! 891: while (!Job_Empty ()) {
! 892: Job_CatchOutput ();
! 893: Job_CatchChildren (!usePipes);
! 894: (void)MakeStartJobs();
! 895: }
! 896:
! 897: errors = Job_End();
! 898:
! 899: /*
! 900: * Print the final status of each target. E.g. if it wasn't made
! 901: * because some inferior reported an error.
! 902: */
! 903: errors = ((errors == 0) && (numNodes != 0));
! 904: Lst_ForEach(targs, MakePrintStatus, (ClientData) &errors);
! 905:
! 906: return (TRUE);
! 907: }