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