source: trunk/src/kmk/commands.c@ 2753

Last change on this file since 2753 was 2753, checked in by bird, 10 years ago

kmk: Save 20+ MB of memory for chopped receipt command lines by freeing them after we're done evaluating a target.

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