1 /* $OpenBSD: engine.c,v 1.69 2020/01/26 12:41:21 espie Exp $ */ 2 /* 3 * Copyright (c) 2012 Marc Espie. 4 * 5 * Extensive code modifications for the OpenBSD project. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD 20 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 /* 29 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 30 * Copyright (c) 1988, 1989 by Adam de Boor 31 * Copyright (c) 1989 by Berkeley Softworks 32 * All rights reserved. 33 * 34 * This code is derived from software contributed to Berkeley by 35 * Adam de Boor. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 */ 61 62 #include <sys/types.h> 63 #include <sys/time.h> 64 #include <sys/wait.h> 65 #include <assert.h> 66 #include <ctype.h> 67 #include <errno.h> 68 #include <fcntl.h> 69 #include <limits.h> 70 #include <signal.h> 71 #include <stdint.h> 72 #include <stdio.h> 73 #include <stdlib.h> 74 #include <string.h> 75 #include <unistd.h> 76 #include "config.h" 77 #include "defines.h" 78 #include "dir.h" 79 #include "engine.h" 80 #include "arch.h" 81 #include "gnode.h" 82 #include "targ.h" 83 #include "var.h" 84 #include "extern.h" 85 #include "lst.h" 86 #include "timestamp.h" 87 #include "make.h" 88 #include "pathnames.h" 89 #include "error.h" 90 #include "str.h" 91 #include "memory.h" 92 #include "buf.h" 93 #include "job.h" 94 #include "lowparse.h" 95 96 static void MakeTimeStamp(void *, void *); 97 static int rewrite_time(const char *); 98 static void setup_meta(void); 99 static void setup_engine(void); 100 static char **recheck_command_for_shell(char **); 101 static void list_parents(GNode *, FILE *); 102 103 /* XXX due to a bug in make's logic, targets looking like *.a or -l* 104 * have been silently dropped when make couldn't figure them out. 105 * Now, we warn about them until all Makefile bugs have been fixed. 106 */ 107 static bool 108 drop_silently(const char *s) 109 { 110 size_t len; 111 112 if (s[0] == '-' && s[1] == 'l') 113 return true; 114 115 len = strlen(s); 116 if (len >=2 && s[len-2] == '.' && s[len-1] == 'a') 117 return true; 118 return false; 119 } 120 121 bool 122 node_find_valid_commands(GNode *gn) 123 { 124 if (DEBUG(DOUBLE) && (gn->type & OP_DOUBLE)) 125 fprintf(stderr, "Warning: target %s had >1 lists of " 126 "shell commands (ignoring later ones)\n", gn->name); 127 if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands)) { 128 if (drop_silently(gn->name)) { 129 printf("Warning: target %s", gn->name); 130 list_parents(gn, stdout); 131 printf(" does not have any command (BUG)\n"); 132 return true; 133 } 134 /* 135 * No commands. Look for .DEFAULT rule from which we might infer 136 * commands 137 */ 138 if ((gn->type & OP_NODEFAULT) == 0 && 139 (DEFAULT->type & OP_DUMMY) == 0 && 140 !Lst_IsEmpty(&DEFAULT->commands)) { 141 /* 142 * Make only looks for a .DEFAULT if the node was never 143 * the target of an operator, so that's what we do too. 144 * If a .DEFAULT was given, we substitute its commands 145 * for gn's commands and set the IMPSRC variable to be 146 * the target's name The DEFAULT node acts like a 147 * transformation rule, in that gn also inherits any 148 * attributes or sources attached to .DEFAULT itself. 149 */ 150 Make_HandleUse(DEFAULT, gn); 151 Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn); 152 } else if (is_out_of_date(Dir_MTime(gn))) { 153 /* 154 * The node wasn't the target of an operator we have no 155 * .DEFAULT rule to go on and the target doesn't 156 * already exist. There's nothing more we can do for 157 * this branch. 158 */ 159 return false; 160 } 161 } 162 return true; 163 } 164 165 static void 166 list_parents(GNode *gn, FILE *out) 167 { 168 LstNode ln; 169 bool first = true; 170 171 for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) { 172 GNode *p = Lst_Datum(ln); 173 if (!p->must_make) 174 continue; 175 if (first) { 176 fprintf(out, " (prerequisite of:"); 177 first = false; 178 } 179 fprintf(out, " %s", p->name); 180 } 181 if (!first) 182 fprintf(out, ")"); 183 } 184 185 void 186 node_failure(GNode *gn) 187 { 188 /* 189 If the -k flag wasn't given, we stop in 190 * our tracks, otherwise we just don't update this 191 * node's parents so they never get examined. 192 */ 193 const char *diag; 194 FILE *out; 195 196 if (gn->type & OP_OPTIONAL) { 197 out = stdout; 198 diag = "(ignored)"; 199 } else if (keepgoing) { 200 out = stdout; 201 diag = "(continuing)"; 202 } else { 203 out = stderr; 204 diag = ""; 205 } 206 fprintf(out, "make: don't know how to make %s", gn->name); 207 list_parents(gn, out); 208 fprintf(out, "%s\n", diag); 209 if (out == stdout) 210 fflush(stdout); 211 else { 212 print_errors(); 213 Punt(NULL); 214 } 215 } 216 217 /* touch files the hard way, by writing stuff to them */ 218 static int 219 rewrite_time(const char *name) 220 { 221 int fd; 222 char c; 223 224 fd = open(name, O_RDWR | O_CREAT, 0666); 225 if (fd < 0) 226 return -1; 227 /* 228 * Read and write a byte to the file to change 229 * the modification time. 230 */ 231 if (read(fd, &c, 1) == 1) { 232 (void)lseek(fd, 0, SEEK_SET); 233 (void)write(fd, &c, 1); 234 } 235 236 (void)close(fd); 237 return 0; 238 } 239 240 void 241 Job_Touch(GNode *gn) 242 { 243 handle_all_signals(); 244 if (gn->type & (OP_USE|OP_OPTIONAL|OP_PHONY)) { 245 /* 246 * .JOIN, .USE, and .OPTIONAL targets are "virtual" targets 247 * and, as such, shouldn't really be created. 248 * Likewise, .PHONY targets are not really files 249 */ 250 return; 251 } 252 253 if (!Targ_Silent(gn)) { 254 (void)fprintf(stdout, "touch %s\n", gn->name); 255 (void)fflush(stdout); 256 } 257 258 if (noExecute) { 259 return; 260 } 261 262 if (gn->type & OP_ARCHV) { 263 Arch_Touch(gn); 264 } else { 265 const char *file = gn->path != NULL ? gn->path : gn->name; 266 267 if (set_times(file) == -1){ 268 if (rewrite_time(file) == -1) { 269 (void)fprintf(stderr, 270 "*** couldn't touch %s: %s", file, 271 strerror(errno)); 272 } 273 } 274 } 275 } 276 277 void 278 Make_TimeStamp(GNode *parent, GNode *child) 279 { 280 if (is_strictly_before(parent->youngest->mtime, child->mtime)) { 281 parent->youngest = child; 282 } 283 } 284 285 void 286 Make_HandleUse(GNode *cgn, /* The .USE node */ 287 GNode *pgn) /* The target of the .USE node */ 288 { 289 GNode *gn; /* A child of the .USE node */ 290 LstNode ln; /* An element in the children list */ 291 292 assert(cgn->type & (OP_USE|OP_TRANSFORM)); 293 294 if (pgn == NULL) 295 Fatal("Trying to apply .USE to '%s' without a parent", 296 cgn->name); 297 298 if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) { 299 /* .USE or transformation and target has no commands 300 * -- append the child's commands to the parent. */ 301 Lst_Concat(&pgn->commands, &cgn->commands); 302 } 303 304 for (ln = Lst_First(&cgn->children); ln != NULL; 305 ln = Lst_Adv(ln)) { 306 gn = Lst_Datum(ln); 307 308 if (Lst_AddNew(&pgn->children, gn)) { 309 Lst_AtEnd(&gn->parents, pgn); 310 pgn->children_left++; 311 } 312 } 313 314 if (DEBUG(DOUBLE) && (cgn->type & OP_DOUBLE)) 315 fprintf(stderr, 316 "Warning: .USE %s expanded in %s had >1 lists of " 317 "shell commands (ignoring later ones)\n", 318 cgn->name, pgn->name); 319 pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM|OP_DOUBLE); 320 321 /* 322 * This child node is now built, so we decrement the count of 323 * not yet built children in the parent... We also remove the child 324 * from the parent's list to accurately reflect the number of 325 * remaining children the parent has. This is used by Make_Run to 326 * decide whether to queue the parent or examine its children... 327 */ 328 if (cgn->type & OP_USE) 329 pgn->children_left--; 330 } 331 332 void 333 Make_DoAllVar(GNode *gn) 334 { 335 GNode *child; 336 LstNode ln; 337 BUFFER allsrc, oodate; 338 char *target; 339 bool do_oodate; 340 int oodate_count, allsrc_count = 0; 341 342 oodate_count = 0; 343 allsrc_count = 0; 344 345 Var(OODATE_INDEX, gn) = ""; 346 Var(ALLSRC_INDEX, gn) = ""; 347 348 for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) { 349 child = Lst_Datum(ln); 350 if ((child->type & (OP_USE|OP_INVISIBLE)) != 0) 351 continue; 352 if (OP_NOP(child->type) || 353 (target = Var(TARGET_INDEX, child)) == NULL) { 354 /* 355 * this node is only source; use the specific pathname 356 * for it 357 */ 358 target = child->path != NULL ? child->path : 359 child->name; 360 } 361 362 /* 363 * It goes in the OODATE variable if the parent is younger than 364 * the child or if the child has been modified more recently 365 * than the start of the make. This is to keep make from 366 * getting confused if something else updates the parent after 367 * the make starts (shouldn't happen, I know, but sometimes it 368 * does). In such a case, if we've updated the kid, the parent 369 * is likely to have a modification time later than that of the 370 * kid and anything that relies on the OODATE variable will be 371 * hosed. 372 */ 373 do_oodate = false; 374 if (is_strictly_before(gn->mtime, child->mtime) || 375 (!is_strictly_before(child->mtime, starttime) && 376 child->built_status == REBUILT)) 377 do_oodate = true; 378 if (do_oodate) { 379 oodate_count++; 380 if (oodate_count == 1) 381 Var(OODATE_INDEX, gn) = target; 382 else { 383 if (oodate_count == 2) { 384 Buf_Init(&oodate, 0); 385 Buf_AddString(&oodate, 386 Var(OODATE_INDEX, gn)); 387 } 388 Buf_AddSpace(&oodate); 389 Buf_AddString(&oodate, target); 390 } 391 } 392 allsrc_count++; 393 if (allsrc_count == 1) 394 Var(ALLSRC_INDEX, gn) = target; 395 else { 396 if (allsrc_count == 2) { 397 Buf_Init(&allsrc, 0); 398 Buf_AddString(&allsrc, 399 Var(ALLSRC_INDEX, gn)); 400 } 401 Buf_AddSpace(&allsrc); 402 Buf_AddString(&allsrc, target); 403 } 404 } 405 406 if (allsrc_count > 1) 407 Var(ALLSRC_INDEX, gn) = Buf_Retrieve(&allsrc); 408 if (oodate_count > 1) 409 Var(OODATE_INDEX, gn) = Buf_Retrieve(&oodate); 410 411 if (gn->impliedsrc) 412 Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn->impliedsrc); 413 } 414 415 /* Wrapper to call Make_TimeStamp from a forEach loop. */ 416 static void 417 MakeTimeStamp(void *parent, void *child) 418 { 419 Make_TimeStamp(parent, child); 420 } 421 422 bool 423 Make_OODate(GNode *gn) 424 { 425 bool oodate; 426 427 /* 428 * Certain types of targets needn't even be sought as their datedness 429 * doesn't depend on their modification time... 430 */ 431 if ((gn->type & (OP_USE|OP_PHONY)) == 0) { 432 (void)Dir_MTime(gn); 433 if (DEBUG(MAKE)) { 434 if (!is_out_of_date(gn->mtime)) 435 printf("modified %s...", 436 time_to_string(&gn->mtime)); 437 else 438 printf("non-existent..."); 439 } 440 } 441 442 /* 443 * A target is rebuilt in one of the following circumstances: 444 * - its modification time is smaller than that of its youngest child 445 * and it would actually be run (has commands or type OP_NOP) 446 * - it's the object of a force operator 447 * - it has no children, was on the lhs of an operator and doesn't 448 * exist already. 449 * 450 */ 451 if (gn->type & OP_USE) { 452 /* 453 * If the node is a USE node it is *never* out of date 454 * no matter *what*. 455 */ 456 if (DEBUG(MAKE)) 457 printf(".USE node..."); 458 oodate = false; 459 } else if (gn->type & (OP_FORCE|OP_PHONY)) { 460 /* 461 * A node which is the object of the force (!) operator or which 462 * has the .EXEC attribute is always considered out-of-date. 463 */ 464 if (DEBUG(MAKE)) { 465 if (gn->type & OP_FORCE) 466 printf("! operator..."); 467 else if (gn->type & OP_PHONY) 468 printf(".PHONY node..."); 469 else 470 printf(".EXEC node..."); 471 } 472 oodate = true; 473 } else if (is_strictly_before(gn->mtime, gn->youngest->mtime) || 474 (gn == gn->youngest && 475 (is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP)))) { 476 /* 477 * A node whose modification time is less than that of its 478 * youngest child or that has no children (gn->youngest == gn) 479 * and either doesn't exist (mtime == OUT_OF_DATE) 480 * or was the object of a :: operator is out-of-date. 481 */ 482 if (DEBUG(MAKE)) { 483 if (is_strictly_before(gn->mtime, gn->youngest->mtime)) 484 printf("modified before source(%s)...", 485 gn->youngest->name); 486 else if (is_out_of_date(gn->mtime)) 487 printf("non-existent and no sources..."); 488 else 489 printf(":: operator and no sources..."); 490 } 491 oodate = true; 492 } else { 493 oodate = false; 494 } 495 496 /* 497 * If the target isn't out-of-date, the parents need to know its 498 * modification time. Note that targets that appear to be out-of-date 499 * but aren't, because they have no commands and aren't of type OP_NOP, 500 * have their mtime stay below their children's mtime to keep parents 501 * from thinking they're out-of-date. 502 */ 503 if (!oodate) 504 Lst_ForEach(&gn->parents, MakeTimeStamp, gn); 505 506 return oodate; 507 } 508 509 /* The following array is used to make a fast determination of which 510 * characters are interpreted specially by the shell. If a command 511 * contains any of these characters, it is executed by the shell, not 512 * directly by us. */ 513 static char meta[256]; 514 515 void 516 setup_meta(void) 517 { 518 char *p; 519 520 for (p = "#=|^(){};&<>*?[]:$`\\\n~"; *p != '\0'; p++) 521 meta[(unsigned char) *p] = 1; 522 /* The null character serves as a sentinel in the string. */ 523 meta[0] = 1; 524 } 525 526 static char ** 527 recheck_command_for_shell(char **av) 528 { 529 char *runsh[] = { 530 "!", "alias", "cd", "eval", "exit", "read", "set", "ulimit", 531 "unalias", "unset", "wait", "umask", NULL 532 }; 533 534 char **p; 535 536 /* optimization: if exec cmd, we avoid the intermediate shell */ 537 if (strcmp(av[0], "exec") == 0) 538 av++; 539 540 if (!av[0]) 541 return NULL; 542 543 for (p = runsh; *p; p++) 544 if (strcmp(av[0], *p) == 0) 545 return NULL; 546 547 return av; 548 } 549 550 static void 551 run_command(const char *cmd, bool errCheck) 552 { 553 const char *p; 554 char *shargv[4]; 555 char **todo; 556 557 shargv[0] = _PATH_BSHELL; 558 559 shargv[1] = errCheck ? "-ec" : "-c"; 560 shargv[2] = (char *)cmd; 561 shargv[3] = NULL; 562 563 todo = shargv; 564 565 566 /* Search for meta characters in the command. If there are no meta 567 * characters, there's no need to execute a shell to execute the 568 * command. */ 569 for (p = cmd; !meta[(unsigned char)*p]; p++) 570 continue; 571 if (*p == '\0') { 572 char *bp; 573 char **av; 574 int argc; 575 /* No meta-characters, so probably no need to exec a shell. 576 * Break the command into words to form an argument vector 577 * we can execute. */ 578 av = brk_string(cmd, &argc, &bp); 579 av = recheck_command_for_shell(av); 580 if (av != NULL) 581 todo = av; 582 } 583 execvp(todo[0], todo); 584 585 if (errno == ENOENT) 586 fprintf(stderr, "%s: not found\n", todo[0]); 587 else 588 perror(todo[0]); 589 _exit(1); 590 } 591 592 void 593 job_attach_node(Job *job, GNode *node) 594 { 595 job->node = node; 596 job->node->built_status = BUILDING; 597 job->next_cmd = Lst_First(&node->commands); 598 job->exit_type = JOB_EXIT_OKAY; 599 job->location = NULL; 600 job->flags = 0; 601 } 602 603 void 604 handle_job_status(Job *job, int status) 605 { 606 bool silent; 607 int dying; 608 609 /* if there's one job running and we don't keep going, no need 610 * to report right now. 611 */ 612 if ((job->flags & JOB_ERRCHECK) && !keepgoing && runningJobs == NULL) 613 silent = !DEBUG(JOB); 614 else 615 silent = false; 616 617 debug_job_printf("Process %ld (%s) exited with status %d.\n", 618 (long)job->pid, job->node->name, status); 619 620 /* classify status */ 621 if (WIFEXITED(status)) { 622 job->code = WEXITSTATUS(status);/* exited */ 623 if (job->code != 0) { 624 /* if we're already dying from that signal, be silent */ 625 if (!silent && job->code > 128 626 && job->code <= 128 + _NSIG) { 627 dying = check_dying_signal(); 628 silent = dying && job->code == dying + 128; 629 } 630 if (!silent) 631 printf("*** Error %d", job->code); 632 job->exit_type = JOB_EXIT_BAD; 633 } else 634 job->exit_type = JOB_EXIT_OKAY; 635 } else { 636 job->exit_type = JOB_SIGNALED; 637 job->code = WTERMSIG(status); /* signaled */ 638 /* if we're already dying from that signal, be silent */ 639 if (!silent) { 640 dying = check_dying_signal(); 641 silent = dying && job->code == dying; 642 } 643 if (!silent) 644 printf("*** Signal %d", job->code); 645 } 646 647 /* if there is a problem, what's going on ? */ 648 if (job->exit_type != JOB_EXIT_OKAY) { 649 if (!silent) 650 printf(" in target '%s'", job->node->name); 651 if (job->flags & JOB_ERRCHECK) { 652 job->node->built_status = ERROR; 653 if (!keepgoing) { 654 if (!silent) 655 printf("\n"); 656 job->flags |= JOB_KEEPERROR; 657 /* XXX don't free the command */ 658 return; 659 } 660 printf(", line %lu of %s", job->location->lineno, 661 job->location->fname); 662 /* Parallel make already determined whether 663 * JOB_IS_EXPENSIVE, perform the computation for 664 * sequential make to figure out whether to display the 665 * command or not. */ 666 if ((job->flags & JOB_SILENT) && sequential) 667 determine_expensive_job(job); 668 if ((job->flags & (JOB_SILENT | JOB_IS_EXPENSIVE)) 669 == JOB_SILENT) 670 printf(": %s", job->cmd); 671 /* Abort the current target, 672 * but let others continue. */ 673 printf(" (continuing)\n"); 674 } else { 675 /* Continue executing commands for 676 * this target. If we return 0, 677 * this will happen... */ 678 printf(" (ignored)\n"); 679 job->exit_type = JOB_EXIT_OKAY; 680 } 681 } 682 free(job->cmd); 683 } 684 685 int 686 run_gnode(GNode *gn) 687 { 688 Job *j; 689 if (!gn || (gn->type & OP_DUMMY)) 690 return NOSUCHNODE; 691 692 Job_Make(gn); 693 loop_handle_running_jobs(); 694 return gn->built_status; 695 } 696 697 698 static void 699 setup_engine(void) 700 { 701 static int already_setup = 0; 702 703 if (!already_setup) { 704 setup_meta(); 705 already_setup = 1; 706 } 707 } 708 709 static bool 710 do_run_command(Job *job, const char *pre) 711 { 712 bool silent; /* Don't print command */ 713 bool doExecute; /* Execute the command */ 714 bool errCheck; /* Check errors */ 715 pid_t cpid; /* Child pid */ 716 717 const char *cmd = job->cmd; 718 silent = Targ_Silent(job->node); 719 errCheck = !Targ_Ignore(job->node); 720 if (job->node->type & OP_MAKE) 721 doExecute = true; 722 else 723 doExecute = !noExecute; 724 725 /* How can we execute a null command ? we warn the user that the 726 * command expanded to nothing (is this the right thing to do?). */ 727 if (*cmd == '\0') { 728 Parse_Error(PARSE_WARNING, 729 "'%s' expands to '' while building %s", 730 pre, job->node->name); 731 return false; 732 } 733 734 for (;; cmd++) { 735 if (*cmd == '@') 736 silent = DEBUG(LOUD) ? false : true; 737 else if (*cmd == '-') 738 errCheck = false; 739 else if (*cmd == '+') 740 doExecute = true; 741 else 742 break; 743 } 744 while (ISSPACE(*cmd)) 745 cmd++; 746 /* Print the command before fork if make -n or !silent*/ 747 if ( noExecute || !silent) 748 printf("%s\n", cmd); 749 750 if (silent) 751 job->flags |= JOB_SILENT; 752 else 753 job->flags &= ~JOB_SILENT; 754 755 /* If we're not supposed to execute any commands, this is as far as 756 * we go... */ 757 if (!doExecute) 758 return false; 759 /* always flush for other stuff */ 760 fflush(stdout); 761 762 /* Optimization: bypass comments entirely */ 763 if (*cmd == '#') 764 return false; 765 766 /* Fork and execute the single command. If the fork fails, we abort. */ 767 switch (cpid = fork()) { 768 case -1: 769 Punt("Could not fork"); 770 /*NOTREACHED*/ 771 case 0: 772 reset_signal_mask(); 773 /* put a random delay unless we're the only job running 774 * and there's nothing left to do. 775 */ 776 if (random_delay) 777 if (!(runningJobs == NULL && nothing_left_to_build())) 778 usleep(arc4random_uniform(random_delay)); 779 run_command(cmd, errCheck); 780 /*NOTREACHED*/ 781 default: 782 job->pid = cpid; 783 job->next = runningJobs; 784 runningJobs = job; 785 if (errCheck) 786 job->flags |= JOB_ERRCHECK; 787 else 788 job->flags &= ~JOB_ERRCHECK; 789 debug_job_printf("Running %ld (%s) %s\n", (long)job->pid, 790 job->node->name, (noExecute || !silent) ? "" : cmd); 791 return true; 792 } 793 } 794 795 bool 796 job_run_next(Job *job) 797 { 798 bool started; 799 GNode *gn = job->node; 800 801 setup_engine(); 802 while (job->next_cmd != NULL) { 803 struct command *command = Lst_Datum(job->next_cmd); 804 805 handle_all_signals(); 806 job->location = &command->location; 807 Parse_SetLocation(job->location); 808 job->cmd = Var_Subst(command->string, &gn->localvars, false); 809 job->next_cmd = Lst_Adv(job->next_cmd); 810 if (fatal_errors) 811 Punt(NULL); 812 started = do_run_command(job, command->string); 813 if (started) 814 return false; 815 else 816 free(job->cmd); 817 } 818 job->exit_type = JOB_EXIT_OKAY; 819 return true; 820 } 821 822