1 /* Command processing for GNU Make. 2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software 4 Foundation, Inc. 5 This file is part of GNU Make. 6 7 GNU Make is free software; you can redistribute it and/or modify it under the 8 terms of the GNU General Public License as published by the Free Software 9 Foundation; either version 2, or (at your option) any later version. 10 11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 13 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along with 16 GNU Make; see the file COPYING. If not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ 18 19 #include "make.h" 20 #include "dep.h" 21 #include "filedef.h" 22 #include "variable.h" 23 #include "job.h" 24 #include "commands.h" 25 #ifdef WINDOWS32 26 #include <windows.h> 27 #include "w32err.h" 28 #endif 29 30 #if VMS 31 # define FILE_LIST_SEPARATOR ',' 32 #else 33 # define FILE_LIST_SEPARATOR ' ' 34 #endif 35 36 extern int remote_kill PARAMS ((int id, int sig)); 37 38 #ifndef HAVE_UNISTD_H 39 extern int getpid (); 40 #endif 41 42 /* Set FILE's automatic variables up. */ 43 44 void 45 set_file_variables (struct file *file) 46 { 47 struct dep *d; 48 char *at, *percent, *star, *less; 49 50 #ifndef NO_ARCHIVES 51 /* If the target is an archive member `lib(member)', 52 then $@ is `lib' and $% is `member'. */ 53 54 if (ar_name (file->name)) 55 { 56 unsigned int len; 57 char *p; 58 59 p = strchr (file->name, '('); 60 at = (char *) alloca (p - file->name + 1); 61 bcopy (file->name, at, p - file->name); 62 at[p - file->name] = '\0'; 63 len = strlen (p + 1); 64 percent = (char *) alloca (len); 65 bcopy (p + 1, percent, len - 1); 66 percent[len - 1] = '\0'; 67 } 68 else 69 #endif /* NO_ARCHIVES. */ 70 { 71 at = file->name; 72 percent = ""; 73 } 74 75 /* $* is the stem from an implicit or static pattern rule. */ 76 if (file->stem == 0) 77 { 78 /* In Unix make, $* is set to the target name with 79 any suffix in the .SUFFIXES list stripped off for 80 explicit rules. We store this in the `stem' member. */ 81 register struct dep *d; 82 char *name; 83 unsigned int len; 84 85 #ifndef NO_ARCHIVES 86 if (ar_name (file->name)) 87 { 88 name = strchr (file->name, '(') + 1; 89 len = strlen (name) - 1; 90 } 91 else 92 #endif 93 { 94 name = file->name; 95 len = strlen (name); 96 } 97 98 for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next) 99 { 100 unsigned int slen = strlen (dep_name (d)); 101 if (len > slen && strneq (dep_name (d), name + (len - slen), slen)) 102 { 103 file->stem = savestring (name, len - slen); 104 break; 105 } 106 } 107 if (d == 0) 108 file->stem = ""; 109 } 110 star = file->stem; 111 112 /* $< is the first not order-only dependency. */ 113 less = ""; 114 for (d = file->deps; d != 0; d = d->next) 115 if (!d->ignore_mtime) 116 { 117 less = dep_name (d); 118 break; 119 } 120 121 if (file->cmds == default_file->cmds) 122 /* This file got its commands from .DEFAULT. 123 In this case $< is the same as $@. */ 124 less = at; 125 126 #define DEFINE_VARIABLE(name, len, value) \ 127 (void) define_variable_for_file (name,len,value,o_automatic,0,file) 128 129 /* Define the variables. */ 130 131 DEFINE_VARIABLE ("<", 1, less); 132 DEFINE_VARIABLE ("*", 1, star); 133 DEFINE_VARIABLE ("@", 1, at); 134 DEFINE_VARIABLE ("%", 1, percent); 135 136 /* Compute the values for $^, $+, $?, and $|. */ 137 138 { 139 static char *plus_value=0, *bar_value=0, *qmark_value=0; 140 static unsigned int qmark_max=0, plus_max=0, bar_max=0; 141 142 unsigned int qmark_len, plus_len, bar_len; 143 char *cp; 144 char *caret_value; 145 char *qp; 146 char *bp; 147 unsigned int len; 148 149 /* Compute first the value for $+, which is supposed to contain 150 duplicate dependencies as they were listed in the makefile. */ 151 152 plus_len = 0; 153 for (d = file->deps; d != 0; d = d->next) 154 if (! d->ignore_mtime) 155 plus_len += strlen (dep_name (d)) + 1; 156 if (plus_len == 0) 157 plus_len++; 158 159 if (plus_len > plus_max) 160 plus_value = xrealloc (plus_value, plus_max = plus_len); 161 cp = plus_value; 162 163 qmark_len = plus_len + 1; /* Will be this or less. */ 164 for (d = file->deps; d != 0; d = d->next) 165 if (! d->ignore_mtime) 166 { 167 char *c = dep_name (d); 168 169 #ifndef NO_ARCHIVES 170 if (ar_name (c)) 171 { 172 c = strchr (c, '(') + 1; 173 len = strlen (c) - 1; 174 } 175 else 176 #endif 177 len = strlen (c); 178 179 bcopy (c, cp, len); 180 cp += len; 181 *cp++ = FILE_LIST_SEPARATOR; 182 if (! d->changed) 183 qmark_len -= len + 1; /* Don't space in $? for this one. */ 184 } 185 186 /* Kill the last space and define the variable. */ 187 188 cp[cp > plus_value ? -1 : 0] = '\0'; 189 DEFINE_VARIABLE ("+", 1, plus_value); 190 191 /* Make sure that no dependencies are repeated. This does not 192 really matter for the purpose of updating targets, but it 193 might make some names be listed twice for $^ and $?. */ 194 195 uniquize_deps (file->deps); 196 197 bar_len = 0; 198 for (d = file->deps; d != 0; d = d->next) 199 if (d->ignore_mtime) 200 bar_len += strlen (dep_name (d)) + 1; 201 if (bar_len == 0) 202 bar_len++; 203 204 /* Compute the values for $^, $?, and $|. */ 205 206 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */ 207 208 if (qmark_len > qmark_max) 209 qmark_value = xrealloc (qmark_value, qmark_max = qmark_len); 210 qp = qmark_value; 211 212 if (bar_len > bar_max) 213 bar_value = xrealloc (bar_value, bar_max = bar_len); 214 bp = bar_value; 215 216 for (d = file->deps; d != 0; d = d->next) 217 { 218 char *c = dep_name (d); 219 220 #ifndef NO_ARCHIVES 221 if (ar_name (c)) 222 { 223 c = strchr (c, '(') + 1; 224 len = strlen (c) - 1; 225 } 226 else 227 #endif 228 len = strlen (c); 229 230 if (d->ignore_mtime) 231 { 232 bcopy (c, bp, len); 233 bp += len; 234 *bp++ = FILE_LIST_SEPARATOR; 235 } 236 else 237 { 238 bcopy (c, cp, len); 239 cp += len; 240 *cp++ = FILE_LIST_SEPARATOR; 241 if (d->changed) 242 { 243 bcopy (c, qp, len); 244 qp += len; 245 *qp++ = FILE_LIST_SEPARATOR; 246 } 247 } 248 } 249 250 /* Kill the last spaces and define the variables. */ 251 252 cp[cp > caret_value ? -1 : 0] = '\0'; 253 DEFINE_VARIABLE ("^", 1, caret_value); 254 255 qp[qp > qmark_value ? -1 : 0] = '\0'; 256 DEFINE_VARIABLE ("?", 1, qmark_value); 257 258 bp[bp > bar_value ? -1 : 0] = '\0'; 259 DEFINE_VARIABLE ("|", 1, bar_value); 260 } 261 262 #undef DEFINE_VARIABLE 263 } 264 265 /* Chop CMDS up into individual command lines if necessary. 266 Also set the `lines_flags' and `any_recurse' members. */ 267 268 void 269 chop_commands (struct commands *cmds) 270 { 271 register char *p; 272 unsigned int nlines, idx; 273 char **lines; 274 275 /* If we don't have any commands, 276 or we already parsed them, never mind. */ 277 278 if (!cmds || cmds->command_lines != 0) 279 return; 280 281 /* Chop CMDS->commands up into lines in CMDS->command_lines. 282 Also set the corresponding CMDS->lines_flags elements, 283 and the CMDS->any_recurse flag. */ 284 285 nlines = 5; 286 lines = (char **) xmalloc (5 * sizeof (char *)); 287 idx = 0; 288 p = cmds->commands; 289 while (*p != '\0') 290 { 291 char *end = p; 292 find_end:; 293 end = strchr (end, '\n'); 294 if (end == 0) 295 end = p + strlen (p); 296 else if (end > p && end[-1] == '\\') 297 { 298 int backslash = 1; 299 register char *b; 300 for (b = end - 2; b >= p && *b == '\\'; --b) 301 backslash = !backslash; 302 if (backslash) 303 { 304 ++end; 305 goto find_end; 306 } 307 } 308 309 if (idx == nlines) 310 { 311 nlines += 2; 312 lines = (char **) xrealloc ((char *) lines, 313 nlines * sizeof (char *)); 314 } 315 lines[idx++] = savestring (p, end - p); 316 p = end; 317 if (*p != '\0') 318 ++p; 319 } 320 321 if (idx != nlines) 322 { 323 nlines = idx; 324 lines = (char **) xrealloc ((char *) lines, 325 nlines * sizeof (char *)); 326 } 327 328 cmds->ncommand_lines = nlines; 329 cmds->command_lines = lines; 330 331 cmds->any_recurse = 0; 332 cmds->lines_flags = (char *) xmalloc (nlines); 333 for (idx = 0; idx < nlines; ++idx) 334 { 335 int flags = 0; 336 337 for (p = lines[idx]; 338 isblank ((unsigned char)*p) || *p == '-' || *p == '@' || *p == '+'; 339 ++p) 340 switch (*p) 341 { 342 case '+': 343 flags |= COMMANDS_RECURSE; 344 break; 345 case '@': 346 flags |= COMMANDS_SILENT; 347 break; 348 case '-': 349 flags |= COMMANDS_NOERROR; 350 break; 351 } 352 353 /* If no explicit '+' was given, look for MAKE variable references. */ 354 if (!(flags & COMMANDS_RECURSE) 355 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0)) 356 flags |= COMMANDS_RECURSE; 357 358 cmds->lines_flags[idx] = flags; 359 cmds->any_recurse |= flags & COMMANDS_RECURSE; 360 } 361 } 362 363 /* Execute the commands to remake FILE. If they are currently executing, 364 return or have already finished executing, just return. Otherwise, 365 fork off a child process to run the first command line in the sequence. */ 366 367 void 368 execute_file_commands (struct file *file) 369 { 370 register char *p; 371 372 /* Don't go through all the preparations if 373 the commands are nothing but whitespace. */ 374 375 for (p = file->cmds->commands; *p != '\0'; ++p) 376 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@') 377 break; 378 if (*p == '\0') 379 { 380 /* If there are no commands, assume everything worked. */ 381 set_command_state (file, cs_running); 382 file->update_status = 0; 383 notice_finished_file (file); 384 return; 385 } 386 387 /* First set the automatic variables according to this file. */ 388 389 initialize_file_variables (file, 0); 390 391 set_file_variables (file); 392 393 /* Start the commands running. */ 394 new_job (file); 395 } 396 397 /* This is set while we are inside fatal_error_signal, 398 so things can avoid nonreentrant operations. */ 399 400 int handling_fatal_signal = 0; 401 402 /* Handle fatal signals. */ 403 404 RETSIGTYPE 405 fatal_error_signal (int sig) 406 { 407 #ifdef __MSDOS__ 408 extern int dos_status, dos_command_running; 409 410 if (dos_command_running) 411 { 412 /* That was the child who got the signal, not us. */ 413 dos_status |= (sig << 8); 414 return; 415 } 416 remove_intermediates (1); 417 exit (EXIT_FAILURE); 418 #else /* not __MSDOS__ */ 419 #ifdef _AMIGA 420 remove_intermediates (1); 421 if (sig == SIGINT) 422 fputs (_("*** Break.\n"), stderr); 423 424 exit (10); 425 #else /* not Amiga */ 426 #ifdef WINDOWS32 427 extern HANDLE main_thread; 428 429 /* Windows creates a sperate thread for handling Ctrl+C, so we need 430 to suspend the main thread, or else we will have race conditions 431 when both threads call reap_children. */ 432 if (main_thread) 433 { 434 DWORD susp_count = SuspendThread (main_thread); 435 436 if (susp_count != 0) 437 fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count); 438 else if (susp_count == (DWORD)-1) 439 { 440 DWORD ierr = GetLastError (); 441 442 fprintf (stderr, "SuspendThread: error %ld: %s\n", 443 ierr, map_windows32_error_to_string (ierr)); 444 } 445 } 446 #endif 447 handling_fatal_signal = 1; 448 449 /* Set the handling for this signal to the default. 450 It is blocked now while we run this handler. */ 451 signal (sig, SIG_DFL); 452 453 /* A termination signal won't be sent to the entire 454 process group, but it means we want to kill the children. */ 455 456 if (sig == SIGTERM) 457 { 458 register struct child *c; 459 for (c = children; c != 0; c = c->next) 460 if (!c->remote) 461 (void) kill (c->pid, SIGTERM); 462 } 463 464 /* If we got a signal that means the user 465 wanted to kill make, remove pending targets. */ 466 467 if (sig == SIGTERM || sig == SIGINT 468 #ifdef SIGHUP 469 || sig == SIGHUP 470 #endif 471 #ifdef SIGQUIT 472 || sig == SIGQUIT 473 #endif 474 ) 475 { 476 register struct child *c; 477 478 /* Remote children won't automatically get signals sent 479 to the process group, so we must send them. */ 480 for (c = children; c != 0; c = c->next) 481 if (c->remote) 482 (void) remote_kill (c->pid, sig); 483 484 for (c = children; c != 0; c = c->next) 485 delete_child_targets (c); 486 487 /* Clean up the children. We don't just use the call below because 488 we don't want to print the "Waiting for children" message. */ 489 while (job_slots_used > 0) 490 reap_children (1, 0); 491 } 492 else 493 /* Wait for our children to die. */ 494 while (job_slots_used > 0) 495 reap_children (1, 1); 496 497 /* Delete any non-precious intermediate files that were made. */ 498 499 remove_intermediates (1); 500 501 #ifdef SIGQUIT 502 if (sig == SIGQUIT) 503 /* We don't want to send ourselves SIGQUIT, because it will 504 cause a core dump. Just exit instead. */ 505 exit (EXIT_FAILURE); 506 #endif 507 508 #ifdef WINDOWS32 509 if (main_thread) 510 CloseHandle (main_thread); 511 /* Cannot call W32_kill with a pid (it needs a handle). The exit 512 status of 130 emulates what happens in Bash. */ 513 exit (130); 514 #else 515 /* Signal the same code; this time it will really be fatal. The signal 516 will be unblocked when we return and arrive then to kill us. */ 517 if (kill (getpid (), sig) < 0) 518 pfatal_with_name ("kill"); 519 #endif /* not WINDOWS32 */ 520 #endif /* not Amiga */ 521 #endif /* not __MSDOS__ */ 522 } 523 524 /* Delete FILE unless it's precious or not actually a file (phony), 525 and it has changed on disk since we last stat'd it. */ 526 527 static void 528 delete_target (struct file *file, char *on_behalf_of) 529 { 530 struct stat st; 531 int e; 532 533 if (file->precious || file->phony) 534 return; 535 536 #ifndef NO_ARCHIVES 537 if (ar_name (file->name)) 538 { 539 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME 540 ? (time_t) -1 541 : (time_t) FILE_TIMESTAMP_S (file->last_mtime)); 542 if (ar_member_date (file->name) != file_date) 543 { 544 if (on_behalf_of) 545 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"), 546 on_behalf_of, file->name); 547 else 548 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"), 549 file->name); 550 } 551 return; 552 } 553 #endif 554 555 EINTRLOOP (e, stat (file->name, &st)); 556 if (e == 0 557 && S_ISREG (st.st_mode) 558 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime) 559 { 560 if (on_behalf_of) 561 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name); 562 else 563 error (NILF, _("*** Deleting file `%s'"), file->name); 564 if (unlink (file->name) < 0 565 && errno != ENOENT) /* It disappeared; so what. */ 566 perror_with_name ("unlink: ", file->name); 567 } 568 } 569 570 571 /* Delete all non-precious targets of CHILD unless they were already deleted. 572 Set the flag in CHILD to say they've been deleted. */ 573 574 void 575 delete_child_targets (struct child *child) 576 { 577 struct dep *d; 578 579 if (child->deleted) 580 return; 581 582 /* Delete the target file if it changed. */ 583 delete_target (child->file, (char *) 0); 584 585 /* Also remove any non-precious targets listed in the `also_make' member. */ 586 for (d = child->file->also_make; d != 0; d = d->next) 587 delete_target (d->file, child->file->name); 588 589 child->deleted = 1; 590 } 591 592 /* Print out the commands in CMDS. */ 593 594 void 595 print_commands (struct commands *cmds) 596 { 597 register char *s; 598 599 fputs (_("# commands to execute"), stdout); 600 601 if (cmds->fileinfo.filenm == 0) 602 puts (_(" (built-in):")); 603 else 604 printf (_(" (from `%s', line %lu):\n"), 605 cmds->fileinfo.filenm, cmds->fileinfo.lineno); 606 607 s = cmds->commands; 608 while (*s != '\0') 609 { 610 char *end; 611 612 while (isspace ((unsigned char)*s)) 613 ++s; 614 615 end = strchr (s, '\n'); 616 if (end == 0) 617 end = s + strlen (s); 618 619 printf ("\t%.*s\n", (int) (end - s), s); 620 621 s = end; 622 } 623 } 624