VirtualBox

source: kBuild/trunk/src/kmk/commands.c@ 3387

Last change on this file since 3387 was 3156, checked in by bird, 6 years ago

kmk/win: Reworking child process handling. This effort will hopefully handle processor groups better and allow executing internal function off the main thread.

  • Property svn:eol-style set to native
File size: 25.7 KB
Line 
1/* Command processing for GNU Make.
2Copyright (C) 1988-2016 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify it under the
6terms of the GNU General Public License as published by the Free Software
7Foundation; either version 3 of the License, or (at your option) any later
8version.
9
10GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see <http://www.gnu.org/licenses/>. */
16
17#include "makeint.h"
18#include "filedef.h"
19#include "dep.h"
20#include "variable.h"
21#include "job.h"
22#include "commands.h"
23#ifdef WINDOWS32
24#include <windows.h>
25#include "w32err.h"
26# ifdef CONFIG_NEW_WIN_CHILDREN
27# include "w32/winchildren.h"
28# endif
29#endif
30#ifdef CONFIG_WITH_LAZY_DEPS_VARS
31# include <assert.h>
32#endif
33
34#if VMS
35# define FILE_LIST_SEPARATOR (vms_comma_separator ? ',' : ' ')
36#else
37# define FILE_LIST_SEPARATOR ' '
38#endif
39
40#ifndef HAVE_UNISTD_H
41int getpid ();
42#endif
43
44
45#ifndef CONFIG_WITH_STRCACHE2
46
47static unsigned long
48dep_hash_1 (const void *key)
49{
50 const struct dep *d = key;
51 return_STRING_HASH_1 (dep_name (d));
52}
53
54static unsigned long
55dep_hash_2 (const void *key)
56{
57 const struct dep *d = key;
58 return_STRING_HASH_2 (dep_name (d));
59}
60
61static int
62dep_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
77static unsigned long
78dep_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
85static unsigned long
86dep_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
92static int
93dep_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
122struct 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
164void
165#if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
166set_file_variables (struct file *file, int called_early)
167#else
168set_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
500/* Chop CMDS up into individual command lines if necessary.
501 Also set the 'lines_flags' and 'any_recurse' members. */
502
503void
504chop_commands (struct commands *cmds)
505{
506 unsigned int nlines, idx;
507 char **lines;
508
509 /* If we don't have any commands,
510 or we already parsed them, never mind. */
511
512 if (!cmds || cmds->command_lines != 0)
513 return;
514
515 /* Chop CMDS->commands up into lines in CMDS->command_lines. */
516
517 if (one_shell)
518 {
519 int l = strlen (cmds->commands);
520
521 nlines = 1;
522 lines = xmalloc (nlines * sizeof (char *));
523 lines[0] = xstrdup (cmds->commands);
524
525 /* Strip the trailing newline. */
526 if (l > 0 && lines[0][l-1] == '\n')
527 lines[0][l-1] = '\0';
528 }
529 else
530 {
531 const char *p;
532
533 nlines = 5;
534 lines = xmalloc (nlines * sizeof (char *));
535 idx = 0;
536 p = cmds->commands;
537 while (*p != '\0')
538 {
539 const char *end = p;
540 find_end:;
541 end = strchr (end, '\n');
542 if (end == 0)
543 end = p + strlen (p);
544 else if (end > p && end[-1] == '\\')
545 {
546 int backslash = 1;
547 const char *b;
548 for (b = end - 2; b >= p && *b == '\\'; --b)
549 backslash = !backslash;
550 if (backslash)
551 {
552 ++end;
553 goto find_end;
554 }
555 }
556
557 if (idx == nlines)
558 {
559 nlines += 2;
560 lines = xrealloc (lines, nlines * sizeof (char *));
561 }
562 lines[idx++] = xstrndup (p, end - p);
563 p = end;
564 if (*p != '\0')
565 ++p;
566 }
567
568 if (idx != nlines)
569 {
570 nlines = idx;
571 lines = xrealloc (lines, nlines * sizeof (char *));
572 }
573 }
574
575 /* Finally, set the corresponding CMDS->lines_flags elements and the
576 CMDS->any_recurse flag. */
577
578 if (nlines > USHRT_MAX)
579 ON (fatal, &cmds->fileinfo, _("Recipe has too many lines (%ud)"), nlines);
580
581 cmds->ncommand_lines = nlines;
582 cmds->command_lines = lines;
583
584 cmds->any_recurse = 0;
585#ifndef CONFIG_WITH_COMMANDS_FUNC
586 cmds->lines_flags = xmalloc (nlines);
587#else
588 cmds->lines_flags = xmalloc (nlines * sizeof (cmds->lines_flags[0]));
589#endif
590
591 for (idx = 0; idx < nlines; ++idx)
592 {
593 unsigned char flags = 0;
594 const char *p = lines[idx];
595
596 while (ISBLANK (*p) || *p == '-' || *p == '@' || *p == '+' IF_WITH_COMMANDS_FUNC(|| *p == '%'))
597 switch (*(p++))
598 {
599 case '+':
600 flags |= COMMANDS_RECURSE;
601 break;
602 case '@':
603 flags |= COMMANDS_SILENT;
604 break;
605 case '-':
606 flags |= COMMANDS_NOERROR;
607 break;
608#ifdef CONFIG_WITH_COMMANDS_FUNC
609 case '%':
610 flags |= COMMAND_GETTER_SKIP_IT;
611 break;
612#endif
613 }
614
615 /* If no explicit '+' was given, look for MAKE variable references. */
616 if (!(flags & COMMANDS_RECURSE)
617#ifndef KMK
618 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
619#else
620 && (strstr (p, "$(KMK)") != 0 || strstr (p, "${KMK}") != 0 ||
621 strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
622#endif
623 flags |= COMMANDS_RECURSE;
624
625#ifdef CONFIG_WITH_KMK_BUILTIN
626 /* check if kmk builtin command */
627 if (!strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
628 flags |= COMMANDS_KMK_BUILTIN;
629#endif
630
631 cmds->lines_flags[idx] = flags;
632 cmds->any_recurse |= flags & COMMANDS_RECURSE ? 1 : 0;
633 }
634}
635
636
637#ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
638/* This is for saving memory in func_commands. */
639void
640free_chopped_commands (struct commands *cmds)
641{
642 if ( cmds
643 && cmds->command_lines != 0
644 && cmds->refs == 0)
645 {
646 unsigned idx = cmds->ncommand_lines;
647 while (idx-- > 0)
648 free (cmds->command_lines[idx]);
649 free (cmds->command_lines);
650 free (cmds->lines_flags);
651 cmds->command_lines = 0;
652 cmds->lines_flags = 0;
653 cmds->ncommand_lines = 0;
654 }
655}
656
657
658#endif /* CONFIG_WITH_MEMORY_OPTIMIZATIONS */
659/* Execute the commands to remake FILE. If they are currently executing,
660 return or have already finished executing, just return. Otherwise,
661 fork off a child process to run the first command line in the sequence. */
662
663void
664execute_file_commands (struct file *file)
665{
666 const char *p;
667
668 /* Don't go through all the preparations if
669 the commands are nothing but whitespace. */
670
671 for (p = file->cmds->commands; *p != '\0'; ++p)
672 if (!ISSPACE (*p) && *p != '-' && *p != '@' && *p != '+')
673 break;
674 if (*p == '\0')
675 {
676 /* If there are no commands, assume everything worked. */
677#ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
678 file->command_flags |= COMMANDS_NO_COMMANDS;
679#endif
680 set_command_state (file, cs_running);
681 file->update_status = us_success;
682 notice_finished_file (file);
683 return;
684 }
685
686 /* First set the automatic variables according to this file. */
687
688 initialize_file_variables (file, 0);
689
690#if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
691 set_file_variables (file, 0 /* final call */);
692#else
693 set_file_variables (file);
694#endif
695
696 /* If this is a loaded dynamic object, unload it before remaking.
697 Some systems don't support overwriting a loaded object. */
698 if (file->loaded)
699 unload_file (file->name);
700
701 /* Start the commands running. */
702 new_job (file);
703}
704
705
706/* This is set while we are inside fatal_error_signal,
707 so things can avoid nonreentrant operations. */
708
709int handling_fatal_signal = 0;
710
711/* Handle fatal signals. */
712
713RETSIGTYPE
714fatal_error_signal (int sig)
715{
716#ifdef __MSDOS__
717 extern int dos_status, dos_command_running;
718
719 if (dos_command_running)
720 {
721 /* That was the child who got the signal, not us. */
722 dos_status |= (sig << 8);
723 return;
724 }
725 remove_intermediates (1);
726 exit (EXIT_FAILURE);
727#else /* not __MSDOS__ */
728#ifdef _AMIGA
729 remove_intermediates (1);
730 if (sig == SIGINT)
731 fputs (_("*** Break.\n"), stderr);
732
733 exit (10);
734#else /* not Amiga */
735#if defined (WINDOWS32) && !defined (CONFIG_NEW_WIN32_CTRL_EVENT)
736 extern HANDLE main_thread;
737
738 /* Windows creates a sperate thread for handling Ctrl+C, so we need
739 to suspend the main thread, or else we will have race conditions
740 when both threads call reap_children. */
741 if (main_thread)
742 {
743 DWORD susp_count = SuspendThread (main_thread);
744
745 if (susp_count != 0)
746 fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
747 else if (susp_count == (DWORD)-1)
748 {
749 DWORD ierr = GetLastError ();
750
751 fprintf (stderr, "SuspendThread: error %ld: %s\n",
752 ierr, map_windows32_error_to_string (ierr));
753 }
754 }
755#endif
756 handling_fatal_signal = 1;
757
758 /* Set the handling for this signal to the default.
759 It is blocked now while we run this handler. */
760 signal (sig, SIG_DFL);
761
762 /* A termination signal won't be sent to the entire
763 process group, but it means we want to kill the children. */
764
765 if (sig == SIGTERM)
766 {
767 struct child *c;
768 for (c = children; c != 0; c = c->next)
769 if (!c->remote)
770# if defined (CONFIG_NEW_WIN_CHILDREN) && defined (WINDOWS32)
771 MkWinChildKill (c->pid, SIGTERM, c);
772# else
773 (void) kill (c->pid, SIGTERM);
774# endif
775 }
776
777 /* If we got a signal that means the user
778 wanted to kill make, remove pending targets. */
779
780 if (sig == SIGTERM || sig == SIGINT
781#ifdef SIGHUP
782 || sig == SIGHUP
783#endif
784#ifdef SIGQUIT
785 || sig == SIGQUIT
786#endif
787 )
788 {
789 struct child *c;
790
791 /* Remote children won't automatically get signals sent
792 to the process group, so we must send them. */
793 for (c = children; c != 0; c = c->next)
794 if (c->remote)
795 (void) remote_kill (c->pid, sig);
796
797 for (c = children; c != 0; c = c->next)
798 delete_child_targets (c);
799
800 /* Clean up the children. We don't just use the call below because
801 we don't want to print the "Waiting for children" message. */
802 while (job_slots_used > 0)
803 reap_children (1, 0);
804 }
805 else
806 /* Wait for our children to die. */
807 while (job_slots_used > 0)
808 reap_children (1, 1);
809
810 /* Delete any non-precious intermediate files that were made. */
811
812 remove_intermediates (1);
813#ifdef SIGQUIT
814 if (sig == SIGQUIT)
815 /* We don't want to send ourselves SIGQUIT, because it will
816 cause a core dump. Just exit instead. */
817 exit (MAKE_TROUBLE);
818#endif
819
820#ifdef WINDOWS32
821# ifndef CONFIG_NEW_WIN32_CTRL_EVENT
822 if (main_thread)
823 CloseHandle (main_thread);
824# endif /* !CONFIG_NEW_WIN32_CTRL_EVENT */
825 /* Cannot call W32_kill with a pid (it needs a handle). The exit
826 status of 130 emulates what happens in Bash. */
827 exit (130);
828#else
829 /* Signal the same code; this time it will really be fatal. The signal
830 will be unblocked when we return and arrive then to kill us. */
831 if (kill (getpid (), sig) < 0)
832 pfatal_with_name ("kill");
833#endif /* not WINDOWS32 */
834#endif /* not Amiga */
835#endif /* not __MSDOS__ */
836}
837
838
839/* Delete FILE unless it's precious or not actually a file (phony),
840 and it has changed on disk since we last stat'd it. */
841
842static void
843delete_target (struct file *file, const char *on_behalf_of)
844{
845 struct stat st;
846 int e;
847
848 if (file->precious || file->phony)
849 return;
850#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
851 assert (!file->multi_maybe);
852#endif
853
854#ifndef NO_ARCHIVES
855 if (ar_name (file->name))
856 {
857 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
858 ? (time_t) -1
859 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
860 if (ar_member_date (file->name) != file_date)
861 {
862 if (on_behalf_of)
863 OSS (error, NILF,
864 _("*** [%s] Archive member '%s' may be bogus; not deleted"),
865 on_behalf_of, file->name);
866 else
867 OS (error, NILF,
868 _("*** Archive member '%s' may be bogus; not deleted"),
869 file->name);
870 }
871 return;
872 }
873#endif
874
875 EINTRLOOP (e, stat (file->name, &st));
876 if (e == 0
877 && S_ISREG (st.st_mode)
878 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
879 {
880 if (on_behalf_of)
881 OSS (error, NILF,
882 _("*** [%s] Deleting file '%s'"), on_behalf_of, file->name);
883 else
884 OS (error, NILF, _("*** Deleting file '%s'"), file->name);
885 if (unlink (file->name) < 0
886 && errno != ENOENT) /* It disappeared; so what. */
887 perror_with_name ("unlink: ", file->name);
888 }
889}
890
891
892/* Delete all non-precious targets of CHILD unless they were already deleted.
893 Set the flag in CHILD to say they've been deleted. */
894
895void
896delete_child_targets (struct child *child)
897{
898 struct dep *d;
899
900 if (child->deleted)
901 return;
902
903 /* Delete the target file if it changed. */
904 delete_target (child->file, NULL);
905
906 /* Also remove any non-precious targets listed in the 'also_make' member. */
907 for (d = child->file->also_make; d != 0; d = d->next)
908 delete_target (d->file, child->file->name);
909
910#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
911 /* Also remove any multi target siblings, except for the 'maybe' ones (we
912 handle that here) and precious ones (delete_target deals with that).
913 Note that CHILD is always the multi target head (see remake.c). */
914 if (child->file == child->file->multi_head)
915 {
916 struct file *f2;
917 for (f2 = child->file->multi_next; f2; f2 = f2->multi_next)
918 if (!f2->multi_maybe)
919 delete_target (f2, child->file->name);
920 }
921#endif
922
923 child->deleted = 1;
924}
925
926
927/* Print out the commands in CMDS. */
928
929void
930print_commands (const struct commands *cmds)
931{
932 const char *s;
933
934 fputs (_("# recipe to execute"), stdout);
935
936 if (cmds->fileinfo.filenm == 0)
937 puts (_(" (built-in):"));
938 else
939 printf (_(" (from '%s', line %lu):\n"),
940 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
941
942 s = cmds->commands;
943 while (*s != '\0')
944 {
945 const char *end;
946 int bs;
947
948 /* Print one full logical recipe line: find a non-escaped newline. */
949 for (end = s, bs = 0; *end != '\0'; ++end)
950 {
951 if (*end == '\n' && !bs)
952 break;
953
954 bs = *end == '\\' ? !bs : 0;
955 }
956
957 printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);
958
959 s = end + (end[0] == '\n');
960 }
961}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use