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