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, 2007, 2008, 2009,
4 2010 Free Software 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 3 of the License, or (at your option) any later
10 version.
11
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
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 #ifdef CONFIG_WITH_LAZY_DEPS_VARS
30 # include <assert.h>
31 #endif
32
33 #if VMS
34 # define FILE_LIST_SEPARATOR ','
35 #else
36 # define FILE_LIST_SEPARATOR ' '
37 #endif
38
39 int remote_kill (int id, int sig);
40
41 #ifndef HAVE_UNISTD_H
42 int getpid ();
43 #endif
44
45 #ifndef CONFIG_WITH_STRCACHE2
46
47 static unsigned long
dep_hash_1(const void * key)48 dep_hash_1 (const void *key)
49 {
50 const struct dep *d = key;
51 return_STRING_HASH_1 (dep_name (d));
52 }
53
54 static unsigned long
dep_hash_2(const void * key)55 dep_hash_2 (const void *key)
56 {
57 const struct dep *d = key;
58 return_STRING_HASH_2 (dep_name (d));
59 }
60
61 static int
dep_hash_cmp(const void * x,const void * y)62 dep_hash_cmp (const void *x, const void *y)
63 {
64 const struct dep *dx = x;
65 const struct dep *dy = y;
66 return strcmp (dep_name (dx), dep_name (dy));
67 }
68
69
70 #else /* CONFIG_WITH_STRCACHE2 */
71
72 /* Exploit the fact that all names are in the string cache. This means equal
73 names shall have the same storage and there is no need for hashing or
74 comparing. Use the address as the first hash, avoiding any touching of
75 the name, and the length as the second. */
76
77 static unsigned long
dep_hash_1(const void * key)78 dep_hash_1 (const void *key)
79 {
80 const char *name = dep_name ((struct dep const *) key);
81 assert (strcache2_is_cached (&file_strcache, name));
82 return (size_t) name / sizeof(void *);
83 }
84
85 static unsigned long
dep_hash_2(const void * key)86 dep_hash_2 (const void *key)
87 {
88 const char *name = dep_name ((struct dep const *) key);
89 return strcache2_get_len (&file_strcache, name);
90 }
91
92 static int
dep_hash_cmp(const void * x,const void * y)93 dep_hash_cmp (const void *x, const void *y)
94 {
95 struct dep *dx = (struct dep *) x;
96 struct dep *dy = (struct dep *) y;
97 const char *dxname = dep_name (dx);
98 const char *dyname = dep_name (dy);
99 int cmp = dxname == dyname ? 0 : 1;
100
101 /* check preconds: both cached and the cache contains no duplicates. */
102 assert (strcache2_is_cached (&file_strcache, dxname));
103 assert (strcache2_is_cached (&file_strcache, dyname));
104 assert (cmp == 0 || strcmp (dxname, dyname) != 0);
105
106 /* If the names are the same but ignore_mtimes are not equal, one of these
107 is an order-only prerequisite and one isn't. That means that we should
108 remove the one that isn't and keep the one that is. */
109
110 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
111 dx->ignore_mtime = dy->ignore_mtime = 0;
112
113 return cmp;
114 }
115
116 #endif /* CONFIG_WITH_STRCACHE2 */
117
118 #ifdef CONFIG_WITH_LAZY_DEPS_VARS
119 /* Create as copy of DEPS without duplicates, similar to what
120 set_file_variables does. Used by func_deps. */
121
create_uniqute_deps_chain(struct dep * deps)122 struct dep *create_uniqute_deps_chain (struct dep *deps)
123 {
124 struct dep *d;
125 struct dep *head = NULL;
126 struct dep **ppnext= &head;
127 struct hash_table dep_hash;
128 void **slot;
129
130 hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
131
132 for (d = deps; d != 0; d = d->next)
133 {
134 if (d->need_2nd_expansion)
135 continue;
136
137 slot = hash_find_slot (&dep_hash, d);
138 if (HASH_VACANT (*slot))
139 {
140 struct dep *n = alloc_dep();
141 *n = *d;
142 n->next = NULL;
143 *ppnext = n;
144 ppnext = &n->next;
145 hash_insert_at (&dep_hash, n, slot);
146 }
147 else
148 {
149 /* Upgrade order only if a normal dep exists.
150 Note! Elected not to upgrade the original, only the sanitized
151 list, need to check that out later. FIXME TODO */
152 struct dep *d2 = (struct dep *)*slot;
153 if (d->ignore_mtime != d2->ignore_mtime)
154 d->ignore_mtime = d2->ignore_mtime = 0;
155 }
156 }
157
158 return head;
159 }
160 #endif /* CONFIG_WITH_LAZY_DEPS_VARS */
161
162 /* Set FILE's automatic variables up. */
163
164 void
165 #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
set_file_variables(struct file * file,int called_early)166 set_file_variables (struct file *file, int called_early)
167 #else
168 set_file_variables (struct file *file)
169 #endif
170 {
171 struct dep *d;
172 const char *at, *percent, *star, *less;
173 #ifdef CONFIG_WITH_STRCACHE2
174 const char *org_stem = file->stem;
175 #endif
176
177 #ifndef NO_ARCHIVES
178 /* If the target is an archive member `lib(member)',
179 then $@ is `lib' and $% is `member'. */
180
181 if (ar_name (file->name))
182 {
183 unsigned int len;
184 const char *cp;
185 char *p;
186
187 cp = strchr (file->name, '(');
188 p = alloca (cp - file->name + 1);
189 memcpy (p, file->name, cp - file->name);
190 p[cp - file->name] = '\0';
191 at = p;
192 len = strlen (cp + 1);
193 p = alloca (len);
194 memcpy (p, cp + 1, len - 1);
195 p[len - 1] = '\0';
196 percent = p;
197 }
198 else
199 #endif /* NO_ARCHIVES. */
200 {
201 at = file->name;
202 percent = "";
203 }
204
205 /* $* is the stem from an implicit or static pattern rule. */
206 if (file->stem == 0)
207 {
208 /* In Unix make, $* is set to the target name with
209 any suffix in the .SUFFIXES list stripped off for
210 explicit rules. We store this in the `stem' member. */
211 const char *name;
212 unsigned int len;
213
214 #ifndef NO_ARCHIVES
215 if (ar_name (file->name))
216 {
217 name = strchr (file->name, '(') + 1;
218 len = strlen (name) - 1;
219 }
220 else
221 #endif
222 {
223 name = file->name;
224 #ifndef CONFIG_WITH_STRCACHE2
225 len = strlen (name);
226 #else
227 len = strcache2_get_len (&file_strcache, name);
228 #endif
229 }
230
231 #ifndef CONFIG_WITH_STRCACHE2
232 for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
233 {
234 unsigned int slen = strlen (dep_name (d));
235 #else
236 for (d = enter_file (suffixes_strcached)->deps; d ; d = d->next)
237 {
238 unsigned int slen = strcache2_get_len (&file_strcache, dep_name (d));
239 #endif
240 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
241 {
242 file->stem = strcache_add_len (name, len - slen);
243 break;
244 }
245 }
246 if (d == 0)
247 file->stem = "";
248 }
249 star = file->stem;
250
251 /* $< is the first not order-only dependency. */
252 less = "";
253 for (d = file->deps; d != 0; d = d->next)
254 if (!d->ignore_mtime)
255 {
256 if (!d->need_2nd_expansion)
257 less = dep_name (d);
258 break;
259 }
260
261 if (file->cmds == default_file->cmds)
262 /* This file got its commands from .DEFAULT.
263 In this case $< is the same as $@. */
264 less = at;
265
266 #define DEFINE_VARIABLE(name, len, value) \
267 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
268
269 /* Define the variables. */
270
271 #ifndef CONFIG_WITH_RDONLY_VARIABLE_VALUE
272 DEFINE_VARIABLE ("<", 1, less);
273 DEFINE_VARIABLE ("*", 1, star);
274 DEFINE_VARIABLE ("@", 1, at);
275 DEFINE_VARIABLE ("%", 1, percent);
276 #else /* CONFIG_WITH_RDONLY_VARIABLE_VALUE */
277 # define DEFINE_VARIABLE_RO_VAL(name, len, value, value_len) \
278 define_variable_in_set((name), (len), (value), (value_len), -1, \
279 (o_automatic), 0, (file)->variables->set, NILF)
280
281 if (*less == '\0')
282 DEFINE_VARIABLE_RO_VAL ("<", 1, "", 0);
283 else if (less != at || at == file->name)
284 DEFINE_VARIABLE_RO_VAL ("<", 1, less, strcache_get_len (less));
285 else
286 DEFINE_VARIABLE ("<", 1, less);
287
288 if (*star == '\0')
289 DEFINE_VARIABLE_RO_VAL ("*", 1, "", 0);
290 else if (file->stem != org_stem)
291 DEFINE_VARIABLE_RO_VAL ("*", 1, star, strcache_get_len (star));
292 else
293 DEFINE_VARIABLE ("*", 1, star);
294
295 if (at == file->name)
296 DEFINE_VARIABLE_RO_VAL ("@", 1, at, strcache_get_len (at));
297 else
298 DEFINE_VARIABLE ("@", 1, at);
299
300 if (*percent == '\0')
301 DEFINE_VARIABLE_RO_VAL ("%", 1, "", 0);
302 else
303 DEFINE_VARIABLE ("%", 1, percent);
304 #endif /* CONFIG_WITH_RDONLY_VARIABLE_VALUE */
305
306 #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
307 /* The $^, $+, $? and $| variables should not be set if we're called
308 early by a .MUST_MAKE invocation or $(commands ). */
309 if (called_early)
310 return;
311 #endif
312
313 /* Compute the values for $^, $+, $?, and $|. */
314 #ifdef CONFIG_WITH_LAZY_DEPS_VARS
315 /* Lazy doesn't work for double colon rules with multiple files with
316 commands, nor for files that has been thru rehash_file() (vpath). */
317 if ( ( file->double_colon
318 && ( file->double_colon != file
319 || file->last != file))
320 || file->name != file->hname) /* XXX: Rehashed files should be fixable! */
321 #endif
322 {
323 static char *plus_value=0, *bar_value=0, *qmark_value=0;
324 static unsigned int plus_max=0, bar_max=0, qmark_max=0;
325
326 unsigned int qmark_len, plus_len, bar_len;
327 char *cp;
328 char *caret_value;
329 char *qp;
330 char *bp;
331 unsigned int len;
332
333 struct hash_table dep_hash;
334 void **slot;
335
336 /* Compute first the value for $+, which is supposed to contain
337 duplicate dependencies as they were listed in the makefile. */
338
339 plus_len = 0;
340 bar_len = 0;
341 for (d = file->deps; d != 0; d = d->next)
342 {
343 if (!d->need_2nd_expansion)
344 {
345 if (d->ignore_mtime)
346 #ifndef CONFIG_WITH_STRCACHE2
347 bar_len += strlen (dep_name (d)) + 1;
348 #else
349 bar_len += strcache2_get_len (&file_strcache, dep_name (d)) + 1;
350 #endif
351 else
352 #ifndef CONFIG_WITH_STRCACHE2
353 plus_len += strlen (dep_name (d)) + 1;
354 #else
355 plus_len += strcache2_get_len (&file_strcache, dep_name (d)) + 1;
356 #endif
357 }
358 }
359
360 if (bar_len == 0)
361 bar_len++;
362 if (plus_len == 0)
363 plus_len++;
364
365 if (plus_len > plus_max)
366 plus_value = xrealloc (plus_value, plus_max = plus_len);
367
368 cp = plus_value;
369
370 qmark_len = plus_len + 1; /* Will be this or less. */
371 for (d = file->deps; d != 0; d = d->next)
372 if (! d->ignore_mtime && ! d->need_2nd_expansion)
373 {
374 const char *c = dep_name (d);
375
376 #ifndef NO_ARCHIVES
377 if (ar_name (c))
378 {
379 c = strchr (c, '(') + 1;
380 len = strlen (c) - 1;
381 }
382 else
383 #endif
384 #ifndef CONFIG_WITH_STRCACHE2
385 len = strlen (c);
386 #else
387 len = strcache2_get_len (&file_strcache, c);
388 #endif
389
390 memcpy (cp, c, len);
391 cp += len;
392 *cp++ = FILE_LIST_SEPARATOR;
393 if (! (d->changed || always_make_flag))
394 qmark_len -= len + 1; /* Don't space in $? for this one. */
395 }
396
397 /* Kill the last space and define the variable. */
398
399 cp[cp > plus_value ? -1 : 0] = '\0';
400 DEFINE_VARIABLE ("+", 1, plus_value);
401
402 /* Compute the values for $^, $?, and $|. */
403
404 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
405
406 if (qmark_len > qmark_max)
407 qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
408 qp = qmark_value;
409
410 if (bar_len > bar_max)
411 bar_value = xrealloc (bar_value, bar_max = bar_len);
412 bp = bar_value;
413
414 /* Make sure that no dependencies are repeated in $^, $?, and $|. It
415 would be natural to combine the next two loops but we can't do it
416 because of a situation where we have two dep entries, the first
417 is order-only and the second is normal (see below). */
418
419 hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
420
421 for (d = file->deps; d != 0; d = d->next)
422 {
423 if (d->need_2nd_expansion)
424 continue;
425
426 slot = hash_find_slot (&dep_hash, d);
427 if (HASH_VACANT (*slot))
428 hash_insert_at (&dep_hash, d, slot);
429 else
430 {
431 /* Check if the two prerequisites have different ignore_mtime.
432 If so then we need to "upgrade" one that is order-only. */
433
434 struct dep* hd = (struct dep*) *slot;
435
436 if (d->ignore_mtime != hd->ignore_mtime)
437 d->ignore_mtime = hd->ignore_mtime = 0;
438 }
439 }
440
441 for (d = file->deps; d != 0; d = d->next)
442 {
443 const char *c;
444
445 if (d->need_2nd_expansion || hash_find_item (&dep_hash, d) != d)
446 continue;
447
448 c = dep_name (d);
449 #ifndef NO_ARCHIVES
450 if (ar_name (c))
451 {
452 c = strchr (c, '(') + 1;
453 len = strlen (c) - 1;
454 }
455 else
456 #endif
457 #ifndef CONFIG_WITH_STRCACHE2
458 len = strlen (c);
459 #else
460 len = strcache2_get_len (&file_strcache, c);
461 #endif
462
463 if (d->ignore_mtime)
464 {
465 memcpy (bp, c, len);
466 bp += len;
467 *bp++ = FILE_LIST_SEPARATOR;
468 }
469 else
470 {
471 memcpy (cp, c, len);
472 cp += len;
473 *cp++ = FILE_LIST_SEPARATOR;
474 if (d->changed || always_make_flag)
475 {
476 memcpy (qp, c, len);
477 qp += len;
478 *qp++ = FILE_LIST_SEPARATOR;
479 }
480 }
481 }
482
483 hash_free (&dep_hash, 0);
484
485 /* Kill the last spaces and define the variables. */
486
487 cp[cp > caret_value ? -1 : 0] = '\0';
488 DEFINE_VARIABLE ("^", 1, caret_value);
489
490 qp[qp > qmark_value ? -1 : 0] = '\0';
491 DEFINE_VARIABLE ("?", 1, qmark_value);
492
493 bp[bp > bar_value ? -1 : 0] = '\0';
494 DEFINE_VARIABLE ("|", 1, bar_value);
495 }
496 #undef DEFINE_VARIABLE
497 }
498
499 /* Chop CMDS up into individual command lines if necessary.
500 Also set the `lines_flags' and `any_recurse' members. */
501
502 void
503 chop_commands (struct commands *cmds)
504 {
505 unsigned int nlines, idx;
506 char **lines;
507
508 /* If we don't have any commands,
509 or we already parsed them, never mind. */
510
511 if (!cmds || cmds->command_lines != 0)
512 return;
513
514 /* Chop CMDS->commands up into lines in CMDS->command_lines. */
515
516 if (one_shell)
517 {
518 int l = strlen (cmds->commands);
519
520 nlines = 1;
521 lines = xmalloc (nlines * sizeof (char *));
522 lines[0] = xstrdup (cmds->commands);
523
524 /* Strip the trailing newline. */
525 if (l > 0 && lines[0][l-1] == '\n')
526 lines[0][l-1] = '\0';
527 }
528 else
529 {
530 const char *p;
531
532 nlines = 5;
533 lines = xmalloc (nlines * sizeof (char *));
534 idx = 0;
535 p = cmds->commands;
536 while (*p != '\0')
537 {
538 const char *end = p;
539 find_end:;
540 end = strchr (end, '\n');
541 if (end == 0)
542 end = p + strlen (p);
543 else if (end > p && end[-1] == '\\')
544 {
545 int backslash = 1;
546 const char *b;
547 for (b = end - 2; b >= p && *b == '\\'; --b)
548 backslash = !backslash;
549 if (backslash)
550 {
551 ++end;
552 goto find_end;
553 }
554 }
555
556 if (idx == nlines)
557 {
558 nlines += 2;
559 lines = xrealloc (lines, nlines * sizeof (char *));
560 }
561 lines[idx++] = xstrndup (p, end - p);
562 p = end;
563 if (*p != '\0')
564 ++p;
565 }
566
567 if (idx != nlines)
568 {
569 nlines = idx;
570 lines = xrealloc (lines, nlines * sizeof (char *));
571 }
572 }
573
574 /* Finally, set the corresponding CMDS->lines_flags elements and the
575 CMDS->any_recurse flag. */
576
577 cmds->ncommand_lines = nlines;
578 cmds->command_lines = lines;
579
580 cmds->any_recurse = 0;
581 #ifndef CONFIG_WITH_COMMANDS_FUNC
582 cmds->lines_flags = xmalloc (nlines);
583 #else
584 cmds->lines_flags = xmalloc (nlines * sizeof (cmds->lines_flags[0]));
585 #endif
586
587 for (idx = 0; idx < nlines; ++idx)
588 {
589 int flags = 0;
590 const char *p = lines[idx];
591
592 while (isblank (*p) || *p == '-' || *p == '@' || *p == '+' IF_WITH_COMMANDS_FUNC(|| *p == '%'))
593 switch (*(p++))
594 {
595 case '+':
596 flags |= COMMANDS_RECURSE;
597 break;
598 case '@':
599 flags |= COMMANDS_SILENT;
600 break;
601 case '-':
602 flags |= COMMANDS_NOERROR;
603 break;
604 #ifdef CONFIG_WITH_COMMANDS_FUNC
605 case '%':
606 flags |= COMMAND_GETTER_SKIP_IT;
607 break;
608 #endif
609 }
610
611 /* If no explicit '+' was given, look for MAKE variable references. */
612 if (!(flags & COMMANDS_RECURSE)
613 #ifndef KMK
614 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
615 #else
616 && (strstr (p, "$(KMK)") != 0 || strstr (p, "${KMK}") != 0 ||
617 strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
618 #endif
619 flags |= COMMANDS_RECURSE;
620
621 #ifdef CONFIG_WITH_KMK_BUILTIN
622 /* check if kmk builtin command */
623 if (!strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
624 flags |= COMMANDS_KMK_BUILTIN;
625 #endif
626
627 cmds->lines_flags[idx] = flags;
628 cmds->any_recurse |= flags & COMMANDS_RECURSE;
629 }
630 }
631
632 #ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
633 /* This is for saving memory in func_commands. */
634 void
635 free_chopped_commands (struct commands *cmds)
636 {
637 if ( cmds
638 && cmds->command_lines != 0
639 && cmds->refs == 0)
640 {
641 unsigned idx = cmds->ncommand_lines;
642 while (idx-- > 0)
643 free (cmds->command_lines[idx]);
644 free (cmds->command_lines);
645 free (cmds->lines_flags);
646 cmds->command_lines = 0;
647 cmds->lines_flags = 0;
648 cmds->ncommand_lines = 0;
649 }
650 }
651
652 #endif /* CONFIG_WITH_MEMORY_OPTIMIZATIONS */
653 /* Execute the commands to remake FILE. If they are currently executing,
654 return or have already finished executing, just return. Otherwise,
655 fork off a child process to run the first command line in the sequence. */
656
657 void
658 execute_file_commands (struct file *file)
659 {
660 const char *p;
661
662 /* Don't go through all the preparations if
663 the commands are nothing but whitespace. */
664
665 for (p = file->cmds->commands; *p != '\0'; ++p)
666 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
667 break;
668 if (*p == '\0')
669 {
670 /* If there are no commands, assume everything worked. */
671 #ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
672 file->command_flags |= COMMANDS_NO_COMMANDS;
673 #endif
674 set_command_state (file, cs_running);
675 file->update_status = 0;
676 notice_finished_file (file);
677 return;
678 }
679
680 /* First set the automatic variables according to this file. */
681
682 initialize_file_variables (file, 0);
683
684 #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
685 set_file_variables (file, 0 /* final call */);
686 #else
687 set_file_variables (file);
688 #endif
689
690 /* Start the commands running. */
691 new_job (file);
692 }
693
694 /* This is set while we are inside fatal_error_signal,
695 so things can avoid nonreentrant operations. */
696
697 int handling_fatal_signal = 0;
698
699 /* Handle fatal signals. */
700
701 RETSIGTYPE
702 fatal_error_signal (int sig)
703 {
704 #ifdef __MSDOS__
705 extern int dos_status, dos_command_running;
706
707 if (dos_command_running)
708 {
709 /* That was the child who got the signal, not us. */
710 dos_status |= (sig << 8);
711 return;
712 }
713 remove_intermediates (1);
714 exit (EXIT_FAILURE);
715 #else /* not __MSDOS__ */
716 #ifdef _AMIGA
717 remove_intermediates (1);
718 if (sig == SIGINT)
719 fputs (_("*** Break.\n"), stderr);
720
721 exit (10);
722 #else /* not Amiga */
723 #if defined (WINDOWS32) && !defined (CONFIG_NEW_WIN32_CTRL_EVENT)
724 extern HANDLE main_thread;
725
726 /* Windows creates a sperate thread for handling Ctrl+C, so we need
727 to suspend the main thread, or else we will have race conditions
728 when both threads call reap_children. */
729 if (main_thread)
730 {
731 DWORD susp_count = SuspendThread (main_thread);
732
733 if (susp_count != 0)
734 fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
735 else if (susp_count == (DWORD)-1)
736 {
737 DWORD ierr = GetLastError ();
738
739 fprintf (stderr, "SuspendThread: error %ld: %s\n",
740 ierr, map_windows32_error_to_string (ierr));
741 }
742 }
743 #endif
744 handling_fatal_signal = 1;
745
746 /* Set the handling for this signal to the default.
747 It is blocked now while we run this handler. */
748 signal (sig, SIG_DFL);
749
750 /* A termination signal won't be sent to the entire
751 process group, but it means we want to kill the children. */
752
753 if (sig == SIGTERM)
754 {
755 struct child *c;
756 for (c = children; c != 0; c = c->next)
757 if (!c->remote)
758 (void) kill (c->pid, SIGTERM);
759 }
760
761 /* If we got a signal that means the user
762 wanted to kill make, remove pending targets. */
763
764 if (sig == SIGTERM || sig == SIGINT
765 #ifdef SIGHUP
766 || sig == SIGHUP
767 #endif
768 #ifdef SIGQUIT
769 || sig == SIGQUIT
770 #endif
771 )
772 {
773 struct child *c;
774
775 /* Remote children won't automatically get signals sent
776 to the process group, so we must send them. */
777 for (c = children; c != 0; c = c->next)
778 if (c->remote)
779 (void) remote_kill (c->pid, sig);
780
781 for (c = children; c != 0; c = c->next)
782 delete_child_targets (c);
783
784 /* Clean up the children. We don't just use the call below because
785 we don't want to print the "Waiting for children" message. */
786 while (job_slots_used > 0)
787 reap_children (1, 0);
788 }
789 else
790 /* Wait for our children to die. */
791 while (job_slots_used > 0)
792 reap_children (1, 1);
793
794 /* Delete any non-precious intermediate files that were made. */
795
796 remove_intermediates (1);
797 #ifdef SIGQUIT
798 if (sig == SIGQUIT)
799 /* We don't want to send ourselves SIGQUIT, because it will
800 cause a core dump. Just exit instead. */
801 exit (EXIT_FAILURE);
802 #endif
803
804 #ifdef WINDOWS32
805 # ifndef CONFIG_NEW_WIN32_CTRL_EVENT
806 if (main_thread)
807 CloseHandle (main_thread);
808 # endif /* !CONFIG_NEW_WIN32_CTRL_EVENT */
809 /* Cannot call W32_kill with a pid (it needs a handle). The exit
810 status of 130 emulates what happens in Bash. */
811 exit (130);
812 #else
813 /* Signal the same code; this time it will really be fatal. The signal
814 will be unblocked when we return and arrive then to kill us. */
815 if (kill (getpid (), sig) < 0)
816 pfatal_with_name ("kill");
817 #endif /* not WINDOWS32 */
818 #endif /* not Amiga */
819 #endif /* not __MSDOS__ */
820 }
821
822 /* Delete FILE unless it's precious or not actually a file (phony),
823 and it has changed on disk since we last stat'd it. */
824
825 static void
826 delete_target (struct file *file, const char *on_behalf_of)
827 {
828 struct stat st;
829 int e;
830
831 if (file->precious || file->phony)
832 return;
833 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
834 assert (!file->multi_maybe);
835 #endif
836
837 #ifndef NO_ARCHIVES
838 if (ar_name (file->name))
839 {
840 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
841 ? (time_t) -1
842 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
843 if (ar_member_date (file->name) != file_date)
844 {
845 if (on_behalf_of)
846 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
847 on_behalf_of, file->name);
848 else
849 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
850 file->name);
851 }
852 return;
853 }
854 #endif
855
856 EINTRLOOP (e, stat (file->name, &st));
857 if (e == 0
858 && S_ISREG (st.st_mode)
859 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
860 {
861 if (on_behalf_of)
862 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
863 else
864 error (NILF, _("*** Deleting file `%s'"), file->name);
865 if (unlink (file->name) < 0
866 && errno != ENOENT) /* It disappeared; so what. */
867 perror_with_name ("unlink: ", file->name);
868 }
869 }
870
871
872 /* Delete all non-precious targets of CHILD unless they were already deleted.
873 Set the flag in CHILD to say they've been deleted. */
874
875 void
876 delete_child_targets (struct child *child)
877 {
878 struct dep *d;
879
880 if (child->deleted)
881 return;
882
883 /* Delete the target file if it changed. */
884 delete_target (child->file, NULL);
885
886 /* Also remove any non-precious targets listed in the `also_make' member. */
887 for (d = child->file->also_make; d != 0; d = d->next)
888 delete_target (d->file, child->file->name);
889
890 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
891 /* Also remove any multi target siblings, except for the 'maybe' ones (we
892 handle that here) and precious ones (delete_target deals with that).
893 Note that CHILD is always the multi target head (see remake.c). */
894 if (child->file == child->file->multi_head)
895 {
896 struct file *f2;
897 for (f2 = child->file->multi_next; f2; f2 = f2->multi_next)
898 if (!f2->multi_maybe)
899 delete_target (f2, child->file->name);
900 }
901 #endif
902
903 child->deleted = 1;
904 }
905
906 /* Print out the commands in CMDS. */
907
908 void
909 print_commands (const struct commands *cmds)
910 {
911 const char *s;
912
913 fputs (_("# recipe to execute"), stdout);
914
915 if (cmds->fileinfo.filenm == 0)
916 puts (_(" (built-in):"));
917 else
918 printf (_(" (from `%s', line %lu):\n"),
919 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
920
921 s = cmds->commands;
922 while (*s != '\0')
923 {
924 const char *end;
925
926 end = strchr (s, '\n');
927 if (end == 0)
928 end = s + strlen (s);
929
930 printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);
931
932 s = end + (end[0] == '\n');
933 }
934 }
935