1 /* 2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 3 * Copyright (c) 1988, 1989 by Adam de Boor 4 * Copyright (c) 1989 by Berkeley Softworks 5 * 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #ifndef lint 40 /* from: static char sccsid[] = "@(#)compat.c 5.7 (Berkeley) 3/1/91"; */ 41 static char *rcsid = "$Id: compat.c,v 1.6 1994/03/07 22:22:01 cgd Exp $"; 42 #endif /* not lint */ 43 44 /*- 45 * compat.c -- 46 * The routines in this file implement the full-compatibility 47 * mode of PMake. Most of the special functionality of PMake 48 * is available in this mode. Things not supported: 49 * - different shells. 50 * - friendly variable substitution. 51 * 52 * Interface: 53 * Compat_Run Initialize things for this module and recreate 54 * thems as need creatin' 55 */ 56 57 #include <stdio.h> 58 #include <sys/types.h> 59 #include <sys/signal.h> 60 #include <sys/wait.h> 61 #include <sys/errno.h> 62 #include <sys/stat.h> 63 #include <ctype.h> 64 #include "make.h" 65 #include "hash.h" 66 #include "dir.h" 67 #include "job.h" 68 extern int errno; 69 70 /* 71 * The following array is used to make a fast determination of which 72 * characters are interpreted specially by the shell. If a command 73 * contains any of these characters, it is executed by the shell, not 74 * directly by us. 75 */ 76 77 static char meta[256]; 78 79 static GNode *curTarg = NILGNODE; 80 static GNode *ENDNode; 81 static void CompatInterrupt __P((int)); 82 static int CompatRunCommand __P((char *, GNode *)); 83 static int CompatMake __P((GNode *, GNode *)); 84 85 /*- 86 *----------------------------------------------------------------------- 87 * CompatInterrupt -- 88 * Interrupt the creation of the current target and remove it if 89 * it ain't precious. 90 * 91 * Results: 92 * None. 93 * 94 * Side Effects: 95 * The target is removed and the process exits. If .INTERRUPT exists, 96 * its commands are run first WITH INTERRUPTS IGNORED.. 97 * 98 *----------------------------------------------------------------------- 99 */ 100 static void 101 CompatInterrupt (signo) 102 int signo; 103 { 104 GNode *gn; 105 106 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) { 107 char *file = Var_Value (TARGET, curTarg); 108 struct stat st; 109 110 if (lstat(file, &st) != -1 && !S_ISDIR(st.st_mode) && 111 unlink(file) != -1) { 112 printf ("*** %s removed\n", file); 113 } 114 115 /* 116 * Run .INTERRUPT only if hit with interrupt signal 117 */ 118 if (signo == SIGINT) { 119 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 120 if (gn != NILGNODE) { 121 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn); 122 } 123 } 124 } 125 exit (0); 126 } 127 128 /*- 129 *----------------------------------------------------------------------- 130 * CompatRunCommand -- 131 * Execute the next command for a target. If the command returns an 132 * error, the node's made field is set to ERROR and creation stops. 133 * 134 * Results: 135 * 0 if the command succeeded, 1 if an error occurred. 136 * 137 * Side Effects: 138 * The node's 'made' field may be set to ERROR. 139 * 140 *----------------------------------------------------------------------- 141 */ 142 static int 143 CompatRunCommand (cmd, gn) 144 char *cmd; /* Command to execute */ 145 GNode *gn; /* Node from which the command came */ 146 { 147 char *cmdStart; /* Start of expanded command */ 148 register char *cp; 149 Boolean silent, /* Don't print command */ 150 errCheck; /* Check errors */ 151 union wait reason; /* Reason for child's death */ 152 int status; /* Description of child's death */ 153 int cpid; /* Child actually found */ 154 ReturnStatus stat; /* Status of fork */ 155 LstNode cmdNode; /* Node where current command is located */ 156 char **av; /* Argument vector for thing to exec */ 157 int argc; /* Number of arguments in av or 0 if not 158 * dynamically allocated */ 159 Boolean local; /* TRUE if command should be executed 160 * locally */ 161 162 /* 163 * Avoid clobbered variable warnings by forcing the compiler 164 * to ``unregister'' variables 165 */ 166 #if __GNUC__ 167 (void) &av; 168 (void) &errCheck; 169 #endif 170 silent = gn->type & OP_SILENT; 171 errCheck = !(gn->type & OP_IGNORE); 172 173 cmdNode = Lst_Member (gn->commands, (ClientData)cmd); 174 cmdStart = Var_Subst (NULL, cmd, gn, FALSE); 175 176 /* 177 * brk_string will return an argv with a NULL in av[1], thus causing 178 * execvp to choke and die horribly. Besides, how can we execute a null 179 * command? In any case, we warn the user that the command expanded to 180 * nothing (is this the right thing to do?). 181 */ 182 183 if (*cmdStart == '\0') { 184 Error("%s expands to empty string", cmd); 185 return(0); 186 } else { 187 cmd = cmdStart; 188 } 189 Lst_Replace (cmdNode, (ClientData)cmdStart); 190 191 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { 192 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart); 193 return(0); 194 } else if (strcmp(cmdStart, "...") == 0) { 195 gn->type |= OP_SAVE_CMDS; 196 return(0); 197 } 198 199 while ((*cmd == '@') || (*cmd == '-')) { 200 if (*cmd == '@') { 201 silent = TRUE; 202 } else { 203 errCheck = FALSE; 204 } 205 cmd++; 206 } 207 208 while (isspace((unsigned char)*cmd)) 209 cmd++; 210 211 /* 212 * Search for meta characters in the command. If there are no meta 213 * characters, there's no need to execute a shell to execute the 214 * command. 215 */ 216 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) { 217 continue; 218 } 219 220 /* 221 * Print the command before echoing if we're not supposed to be quiet for 222 * this one. We also print the command if -n given. 223 */ 224 if (!silent || noExecute) { 225 printf ("%s\n", cmd); 226 fflush(stdout); 227 } 228 229 /* 230 * If we're not supposed to execute any commands, this is as far as 231 * we go... 232 */ 233 if (noExecute) { 234 return (0); 235 } 236 237 if (*cp != '\0') { 238 /* 239 * If *cp isn't the null character, we hit a "meta" character and 240 * need to pass the command off to the shell. We give the shell the 241 * -e flag as well as -c if it's supposed to exit when it hits an 242 * error. 243 */ 244 static char *shargv[4] = { "/bin/sh" }; 245 246 shargv[1] = (errCheck ? "-ec" : "-c"); 247 shargv[2] = cmd; 248 shargv[3] = (char *)NULL; 249 av = shargv; 250 argc = 0; 251 } else { 252 /* 253 * No meta-characters, so no need to exec a shell. Break the command 254 * into words to form an argument vector we can execute. 255 * brk_string sticks our name in av[0], so we have to 256 * skip over it... 257 */ 258 av = brk_string(cmd, &argc); 259 av += 1; 260 } 261 262 local = TRUE; 263 264 /* 265 * Fork and execute the single command. If the fork fails, we abort. 266 */ 267 cpid = vfork(); 268 if (cpid < 0) { 269 Fatal("Could not fork"); 270 } 271 if (cpid == 0) { 272 if (local) { 273 execvp(av[0], av); 274 (void) write (2, av[0], strlen (av[0])); 275 (void) write (2, ": not found\n", sizeof(": not found")); 276 } else { 277 (void)execv(av[0], av); 278 } 279 exit(1); 280 } 281 282 /* 283 * The child is off and running. Now all we can do is wait... 284 */ 285 while (1) { 286 int id; 287 288 if (!local) { 289 id = 0; 290 } 291 292 while ((stat = wait((int *)&reason)) != cpid) { 293 if (stat == -1 && errno != EINTR) { 294 break; 295 } 296 } 297 298 if (stat > -1) { 299 if (WIFSTOPPED(reason)) { 300 status = reason.w_stopval; /* stopped */ 301 } else if (WIFEXITED(reason)) { 302 status = reason.w_retcode; /* exited */ 303 if (status != 0) { 304 printf ("*** Error code %d", status); 305 } 306 } else { 307 status = reason.w_termsig; /* signaled */ 308 printf ("*** Signal %d", status); 309 } 310 311 312 if (!WIFEXITED(reason) || (status != 0)) { 313 if (errCheck) { 314 gn->made = ERROR; 315 if (keepgoing) { 316 /* 317 * Abort the current target, but let others 318 * continue. 319 */ 320 printf (" (continuing)\n"); 321 } 322 } else { 323 /* 324 * Continue executing commands for this target. 325 * If we return 0, this will happen... 326 */ 327 printf (" (ignored)\n"); 328 status = 0; 329 } 330 } 331 break; 332 } else { 333 Fatal ("error in wait: %d", stat); 334 /*NOTREACHED*/ 335 } 336 } 337 338 return (status); 339 } 340 341 /*- 342 *----------------------------------------------------------------------- 343 * CompatMake -- 344 * Make a target. 345 * 346 * Results: 347 * 0 348 * 349 * Side Effects: 350 * If an error is detected and not being ignored, the process exits. 351 * 352 *----------------------------------------------------------------------- 353 */ 354 static int 355 CompatMake (gn, pgn) 356 GNode *gn; /* The node to make */ 357 GNode *pgn; /* Parent to abort if necessary */ 358 { 359 if (gn->type & OP_USE) { 360 Make_HandleUse(gn, pgn); 361 } else if (gn->made == UNMADE) { 362 /* 363 * First mark ourselves to be made, then apply whatever transformations 364 * the suffix module thinks are necessary. Once that's done, we can 365 * descend and make all our children. If any of them has an error 366 * but the -k flag was given, our 'make' field will be set FALSE again. 367 * This is our signal to not attempt to do anything but abort our 368 * parent as well. 369 */ 370 gn->make = TRUE; 371 gn->made = BEINGMADE; 372 Suff_FindDeps (gn); 373 Lst_ForEach (gn->children, CompatMake, (ClientData)gn); 374 if (!gn->make) { 375 gn->made = ABORTED; 376 pgn->make = FALSE; 377 return (0); 378 } 379 380 if (Lst_Member (gn->iParents, pgn) != NILLNODE) { 381 Var_Set (IMPSRC, Var_Value(TARGET, gn), pgn); 382 } 383 384 /* 385 * All the children were made ok. Now cmtime contains the modification 386 * time of the newest child, we need to find out if we exist and when 387 * we were modified last. The criteria for datedness are defined by the 388 * Make_OODate function. 389 */ 390 if (DEBUG(MAKE)) { 391 printf("Examining %s...", gn->name); 392 } 393 if (! Make_OODate(gn)) { 394 gn->made = UPTODATE; 395 if (DEBUG(MAKE)) { 396 printf("up-to-date.\n"); 397 } 398 return (0); 399 } else if (DEBUG(MAKE)) { 400 printf("out-of-date.\n"); 401 } 402 403 /* 404 * If the user is just seeing if something is out-of-date, exit now 405 * to tell him/her "yes". 406 */ 407 if (queryFlag) { 408 exit (-1); 409 } 410 411 /* 412 * We need to be re-made. We also have to make sure we've got a $? 413 * variable. To be nice, we also define the $> variable using 414 * Make_DoAllVar(). 415 */ 416 Make_DoAllVar(gn); 417 418 /* 419 * Alter our type to tell if errors should be ignored or things 420 * should not be printed so CompatRunCommand knows what to do. 421 */ 422 if (Targ_Ignore (gn)) { 423 gn->type |= OP_IGNORE; 424 } 425 if (Targ_Silent (gn)) { 426 gn->type |= OP_SILENT; 427 } 428 429 if (Job_CheckCommands (gn, Fatal)) { 430 /* 431 * Our commands are ok, but we still have to worry about the -t 432 * flag... 433 */ 434 if (!touchFlag) { 435 curTarg = gn; 436 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn); 437 curTarg = NILGNODE; 438 } else { 439 Job_Touch (gn, gn->type & OP_SILENT); 440 } 441 } else { 442 gn->made = ERROR; 443 } 444 445 if (gn->made != ERROR) { 446 /* 447 * If the node was made successfully, mark it so, update 448 * its modification time and timestamp all its parents. Note 449 * that for .ZEROTIME targets, the timestamping isn't done. 450 * This is to keep its state from affecting that of its parent. 451 */ 452 gn->made = MADE; 453 #ifndef RECHECK 454 /* 455 * We can't re-stat the thing, but we can at least take care of 456 * rules where a target depends on a source that actually creates 457 * the target, but only if it has changed, e.g. 458 * 459 * parse.h : parse.o 460 * 461 * parse.o : parse.y 462 * yacc -d parse.y 463 * cc -c y.tab.c 464 * mv y.tab.o parse.o 465 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h 466 * 467 * In this case, if the definitions produced by yacc haven't 468 * changed from before, parse.h won't have been updated and 469 * gn->mtime will reflect the current modification time for 470 * parse.h. This is something of a kludge, I admit, but it's a 471 * useful one.. 472 * 473 * XXX: People like to use a rule like 474 * 475 * FRC: 476 * 477 * To force things that depend on FRC to be made, so we have to 478 * check for gn->children being empty as well... 479 */ 480 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) { 481 gn->mtime = now; 482 } 483 #else 484 /* 485 * This is what Make does and it's actually a good thing, as it 486 * allows rules like 487 * 488 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h 489 * 490 * to function as intended. Unfortunately, thanks to the stateless 491 * nature of NFS (and the speed of this program), there are times 492 * when the modification time of a file created on a remote 493 * machine will not be modified before the stat() implied by 494 * the Dir_MTime occurs, thus leading us to believe that the file 495 * is unchanged, wreaking havoc with files that depend on this one. 496 * 497 * I have decided it is better to make too much than to make too 498 * little, so this stuff is commented out unless you're sure it's 499 * ok. 500 * -- ardeb 1/12/88 501 */ 502 if (noExecute || Dir_MTime(gn) == 0) { 503 gn->mtime = now; 504 } 505 if (gn->cmtime > gn->mtime) 506 gn->mtime = gn->cmtime; 507 if (DEBUG(MAKE)) { 508 printf("update time: %s\n", Targ_FmtTime(gn->mtime)); 509 } 510 #endif 511 if (!(gn->type & OP_EXEC)) { 512 pgn->childMade = TRUE; 513 Make_TimeStamp(pgn, gn); 514 } 515 } else if (keepgoing) { 516 pgn->make = FALSE; 517 } else { 518 printf ("\n\nStop.\n"); 519 exit (1); 520 } 521 } else if (gn->made == ERROR) { 522 /* 523 * Already had an error when making this beastie. Tell the parent 524 * to abort. 525 */ 526 pgn->make = FALSE; 527 } else { 528 if (Lst_Member (gn->iParents, pgn) != NILLNODE) { 529 Var_Set (IMPSRC, Var_Value(TARGET, gn), pgn); 530 } 531 switch(gn->made) { 532 case BEINGMADE: 533 Error("Graph cycles through %s\n", gn->name); 534 gn->made = ERROR; 535 pgn->make = FALSE; 536 break; 537 case MADE: 538 if ((gn->type & OP_EXEC) == 0) { 539 pgn->childMade = TRUE; 540 Make_TimeStamp(pgn, gn); 541 } 542 break; 543 case UPTODATE: 544 if ((gn->type & OP_EXEC) == 0) { 545 Make_TimeStamp(pgn, gn); 546 } 547 break; 548 default: 549 break; 550 } 551 } 552 553 return (0); 554 } 555 556 /*- 557 *----------------------------------------------------------------------- 558 * Compat_Run -- 559 * Initialize this mode and start making. 560 * 561 * Results: 562 * None. 563 * 564 * Side Effects: 565 * Guess what? 566 * 567 *----------------------------------------------------------------------- 568 */ 569 void 570 Compat_Run(targs) 571 Lst targs; /* List of target nodes to re-create */ 572 { 573 char *cp; /* Pointer to string of shell meta-characters */ 574 GNode *gn = NULL;/* Current root target */ 575 int errors; /* Number of targets not remade due to errors */ 576 577 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 578 signal(SIGINT, CompatInterrupt); 579 } 580 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 581 signal(SIGTERM, CompatInterrupt); 582 } 583 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 584 signal(SIGHUP, CompatInterrupt); 585 } 586 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 587 signal(SIGQUIT, CompatInterrupt); 588 } 589 590 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) { 591 meta[(unsigned char) *cp] = 1; 592 } 593 /* 594 * The null character serves as a sentinel in the string. 595 */ 596 meta[0] = 1; 597 598 ENDNode = Targ_FindNode(".END", TARG_CREATE); 599 /* 600 * If the user has defined a .BEGIN target, execute the commands attached 601 * to it. 602 */ 603 if (!queryFlag) { 604 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 605 if (gn != NILGNODE) { 606 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn); 607 } 608 } 609 610 /* 611 * For each entry in the list of targets to create, call CompatMake on 612 * it to create the thing. CompatMake will leave the 'made' field of gn 613 * in one of several states: 614 * UPTODATE gn was already up-to-date 615 * MADE gn was recreated successfully 616 * ERROR An error occurred while gn was being created 617 * ABORTED gn was not remade because one of its inferiors 618 * could not be made due to errors. 619 */ 620 errors = 0; 621 while (!Lst_IsEmpty (targs)) { 622 gn = (GNode *) Lst_DeQueue (targs); 623 CompatMake (gn, gn); 624 625 if (gn->made == UPTODATE) { 626 printf ("`%s' is up to date.\n", gn->name); 627 } else if (gn->made == ABORTED) { 628 printf ("`%s' not remade because of errors.\n", gn->name); 629 errors += 1; 630 } 631 } 632 633 /* 634 * If the user has defined a .END target, run its commands. 635 */ 636 if (errors == 0) { 637 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn); 638 } 639 } 640