VirtualBox

source: kBuild/vendor/gnumake/3.82-cvs/commands.c@ 2581

Last change on this file since 2581 was 2580, checked in by bird, 12 years ago

Importing the make-3-82 CVS tag with --auto-props but no keywords.

  • Property svn:eol-style set to native
File size: 17.2 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
30#if VMS
31# define FILE_LIST_SEPARATOR ','
32#else
33# define FILE_LIST_SEPARATOR ' '
34#endif
35
36int remote_kill (int id, int sig);
37
38#ifndef HAVE_UNISTD_H
39int getpid ();
40#endif
41
42
43
44static unsigned long
45dep_hash_1 (const void *key)
46{
47 const struct dep *d = key;
48 return_STRING_HASH_1 (dep_name (d));
49}
50
51static unsigned long
52dep_hash_2 (const void *key)
53{
54 const struct dep *d = key;
55 return_STRING_HASH_2 (dep_name (d));
56}
57
58static int
59dep_hash_cmp (const void *x, const void *y)
60{
61 const struct dep *dx = x;
62 const struct dep *dy = y;
63 return strcmp (dep_name (dx), dep_name (dy));
64}
65
66/* Set FILE's automatic variables up. */
67
68void
69set_file_variables (struct file *file)
70{
71 struct dep *d;
72 const char *at, *percent, *star, *less;
73
74#ifndef NO_ARCHIVES
75 /* If the target is an archive member `lib(member)',
76 then $@ is `lib' and $% is `member'. */
77
78 if (ar_name (file->name))
79 {
80 unsigned int len;
81 const char *cp;
82 char *p;
83
84 cp = strchr (file->name, '(');
85 p = alloca (cp - file->name + 1);
86 memcpy (p, file->name, cp - file->name);
87 p[cp - file->name] = '\0';
88 at = p;
89 len = strlen (cp + 1);
90 p = alloca (len);
91 memcpy (p, cp + 1, len - 1);
92 p[len - 1] = '\0';
93 percent = p;
94 }
95 else
96#endif /* NO_ARCHIVES. */
97 {
98 at = file->name;
99 percent = "";
100 }
101
102 /* $* is the stem from an implicit or static pattern rule. */
103 if (file->stem == 0)
104 {
105 /* In Unix make, $* is set to the target name with
106 any suffix in the .SUFFIXES list stripped off for
107 explicit rules. We store this in the `stem' member. */
108 const char *name;
109 unsigned int len;
110
111#ifndef NO_ARCHIVES
112 if (ar_name (file->name))
113 {
114 name = strchr (file->name, '(') + 1;
115 len = strlen (name) - 1;
116 }
117 else
118#endif
119 {
120 name = file->name;
121 len = strlen (name);
122 }
123
124 for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
125 {
126 unsigned int slen = strlen (dep_name (d));
127 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
128 {
129 file->stem = strcache_add_len (name, len - slen);
130 break;
131 }
132 }
133 if (d == 0)
134 file->stem = "";
135 }
136 star = file->stem;
137
138 /* $< is the first not order-only dependency. */
139 less = "";
140 for (d = file->deps; d != 0; d = d->next)
141 if (!d->ignore_mtime)
142 {
143 if (!d->need_2nd_expansion)
144 less = dep_name (d);
145 break;
146 }
147
148 if (file->cmds == default_file->cmds)
149 /* This file got its commands from .DEFAULT.
150 In this case $< is the same as $@. */
151 less = at;
152
153#define DEFINE_VARIABLE(name, len, value) \
154 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
155
156 /* Define the variables. */
157
158 DEFINE_VARIABLE ("<", 1, less);
159 DEFINE_VARIABLE ("*", 1, star);
160 DEFINE_VARIABLE ("@", 1, at);
161 DEFINE_VARIABLE ("%", 1, percent);
162
163 /* Compute the values for $^, $+, $?, and $|. */
164
165 {
166 static char *plus_value=0, *bar_value=0, *qmark_value=0;
167 static unsigned int plus_max=0, bar_max=0, qmark_max=0;
168
169 unsigned int qmark_len, plus_len, bar_len;
170 char *cp;
171 char *caret_value;
172 char *qp;
173 char *bp;
174 unsigned int len;
175
176 struct hash_table dep_hash;
177 void **slot;
178
179 /* Compute first the value for $+, which is supposed to contain
180 duplicate dependencies as they were listed in the makefile. */
181
182 plus_len = 0;
183 bar_len = 0;
184 for (d = file->deps; d != 0; d = d->next)
185 {
186 if (!d->need_2nd_expansion)
187 {
188 if (d->ignore_mtime)
189 bar_len += strlen (dep_name (d)) + 1;
190 else
191 plus_len += strlen (dep_name (d)) + 1;
192 }
193 }
194
195 if (bar_len == 0)
196 bar_len++;
197
198 if (plus_len == 0)
199 plus_len++;
200
201 if (plus_len > plus_max)
202 plus_value = xrealloc (plus_value, plus_max = plus_len);
203
204 cp = plus_value;
205
206 qmark_len = plus_len + 1; /* Will be this or less. */
207 for (d = file->deps; d != 0; d = d->next)
208 if (! d->ignore_mtime && ! d->need_2nd_expansion)
209 {
210 const char *c = dep_name (d);
211
212#ifndef NO_ARCHIVES
213 if (ar_name (c))
214 {
215 c = strchr (c, '(') + 1;
216 len = strlen (c) - 1;
217 }
218 else
219#endif
220 len = strlen (c);
221
222 memcpy (cp, c, len);
223 cp += len;
224 *cp++ = FILE_LIST_SEPARATOR;
225 if (! (d->changed || always_make_flag))
226 qmark_len -= len + 1; /* Don't space in $? for this one. */
227 }
228
229 /* Kill the last space and define the variable. */
230
231 cp[cp > plus_value ? -1 : 0] = '\0';
232 DEFINE_VARIABLE ("+", 1, plus_value);
233
234 /* Compute the values for $^, $?, and $|. */
235
236 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
237
238 if (qmark_len > qmark_max)
239 qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
240 qp = qmark_value;
241
242 if (bar_len > bar_max)
243 bar_value = xrealloc (bar_value, bar_max = bar_len);
244 bp = bar_value;
245
246 /* Make sure that no dependencies are repeated in $^, $?, and $|. It
247 would be natural to combine the next two loops but we can't do it
248 because of a situation where we have two dep entries, the first
249 is order-only and the second is normal (see below). */
250
251 hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
252
253 for (d = file->deps; d != 0; d = d->next)
254 {
255 if (d->need_2nd_expansion)
256 continue;
257
258 slot = hash_find_slot (&dep_hash, d);
259 if (HASH_VACANT (*slot))
260 hash_insert_at (&dep_hash, d, slot);
261 else
262 {
263 /* Check if the two prerequisites have different ignore_mtime.
264 If so then we need to "upgrade" one that is order-only. */
265
266 struct dep* hd = (struct dep*) *slot;
267
268 if (d->ignore_mtime != hd->ignore_mtime)
269 d->ignore_mtime = hd->ignore_mtime = 0;
270 }
271 }
272
273 for (d = file->deps; d != 0; d = d->next)
274 {
275 const char *c;
276
277 if (d->need_2nd_expansion || hash_find_item (&dep_hash, d) != d)
278 continue;
279
280 c = dep_name (d);
281#ifndef NO_ARCHIVES
282 if (ar_name (c))
283 {
284 c = strchr (c, '(') + 1;
285 len = strlen (c) - 1;
286 }
287 else
288#endif
289 len = strlen (c);
290
291 if (d->ignore_mtime)
292 {
293 memcpy (bp, c, len);
294 bp += len;
295 *bp++ = FILE_LIST_SEPARATOR;
296 }
297 else
298 {
299 memcpy (cp, c, len);
300 cp += len;
301 *cp++ = FILE_LIST_SEPARATOR;
302 if (d->changed || always_make_flag)
303 {
304 memcpy (qp, c, len);
305 qp += len;
306 *qp++ = FILE_LIST_SEPARATOR;
307 }
308 }
309 }
310
311 hash_free (&dep_hash, 0);
312
313 /* Kill the last spaces and define the variables. */
314
315 cp[cp > caret_value ? -1 : 0] = '\0';
316 DEFINE_VARIABLE ("^", 1, caret_value);
317
318 qp[qp > qmark_value ? -1 : 0] = '\0';
319 DEFINE_VARIABLE ("?", 1, qmark_value);
320
321 bp[bp > bar_value ? -1 : 0] = '\0';
322 DEFINE_VARIABLE ("|", 1, bar_value);
323 }
324
325#undef DEFINE_VARIABLE
326}
327
328
329/* Chop CMDS up into individual command lines if necessary.
330 Also set the `lines_flags' and `any_recurse' members. */
331
332void
333chop_commands (struct commands *cmds)
334{
335 unsigned int nlines, idx;
336 char **lines;
337
338 /* If we don't have any commands,
339 or we already parsed them, never mind. */
340
341 if (!cmds || cmds->command_lines != 0)
342 return;
343
344 /* Chop CMDS->commands up into lines in CMDS->command_lines. */
345
346 if (one_shell)
347 {
348 int l = strlen (cmds->commands);
349
350 nlines = 1;
351 lines = xmalloc (nlines * sizeof (char *));
352 lines[0] = xstrdup (cmds->commands);
353
354 /* Strip the trailing newline. */
355 if (l > 0 && lines[0][l-1] == '\n')
356 lines[0][l-1] = '\0';
357 }
358 else
359 {
360 const char *p;
361
362 nlines = 5;
363 lines = xmalloc (nlines * sizeof (char *));
364 idx = 0;
365 p = cmds->commands;
366 while (*p != '\0')
367 {
368 const char *end = p;
369 find_end:;
370 end = strchr (end, '\n');
371 if (end == 0)
372 end = p + strlen (p);
373 else if (end > p && end[-1] == '\\')
374 {
375 int backslash = 1;
376 const char *b;
377 for (b = end - 2; b >= p && *b == '\\'; --b)
378 backslash = !backslash;
379 if (backslash)
380 {
381 ++end;
382 goto find_end;
383 }
384 }
385
386 if (idx == nlines)
387 {
388 nlines += 2;
389 lines = xrealloc (lines, nlines * sizeof (char *));
390 }
391 lines[idx++] = xstrndup (p, end - p);
392 p = end;
393 if (*p != '\0')
394 ++p;
395 }
396
397 if (idx != nlines)
398 {
399 nlines = idx;
400 lines = xrealloc (lines, nlines * sizeof (char *));
401 }
402 }
403
404 /* Finally, set the corresponding CMDS->lines_flags elements and the
405 CMDS->any_recurse flag. */
406
407 cmds->ncommand_lines = nlines;
408 cmds->command_lines = lines;
409
410 cmds->any_recurse = 0;
411 cmds->lines_flags = xmalloc (nlines);
412
413 for (idx = 0; idx < nlines; ++idx)
414 {
415 int flags = 0;
416 const char *p = lines[idx];
417
418 while (isblank (*p) || *p == '-' || *p == '@' || *p == '+')
419 switch (*(p++))
420 {
421 case '+':
422 flags |= COMMANDS_RECURSE;
423 break;
424 case '@':
425 flags |= COMMANDS_SILENT;
426 break;
427 case '-':
428 flags |= COMMANDS_NOERROR;
429 break;
430 }
431
432 /* If no explicit '+' was given, look for MAKE variable references. */
433 if (!(flags & COMMANDS_RECURSE)
434 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
435 flags |= COMMANDS_RECURSE;
436
437 cmds->lines_flags[idx] = flags;
438 cmds->any_recurse |= flags & COMMANDS_RECURSE;
439 }
440}
441
442
443/* Execute the commands to remake FILE. If they are currently executing,
444 return or have already finished executing, just return. Otherwise,
445 fork off a child process to run the first command line in the sequence. */
446
447void
448execute_file_commands (struct file *file)
449{
450 const char *p;
451
452 /* Don't go through all the preparations if
453 the commands are nothing but whitespace. */
454
455 for (p = file->cmds->commands; *p != '\0'; ++p)
456 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
457 break;
458 if (*p == '\0')
459 {
460 /* If there are no commands, assume everything worked. */
461 set_command_state (file, cs_running);
462 file->update_status = 0;
463 notice_finished_file (file);
464 return;
465 }
466
467 /* First set the automatic variables according to this file. */
468
469 initialize_file_variables (file, 0);
470
471 set_file_variables (file);
472
473 /* Start the commands running. */
474 new_job (file);
475}
476
477
478/* This is set while we are inside fatal_error_signal,
479 so things can avoid nonreentrant operations. */
480
481int handling_fatal_signal = 0;
482
483/* Handle fatal signals. */
484
485RETSIGTYPE
486fatal_error_signal (int sig)
487{
488#ifdef __MSDOS__
489 extern int dos_status, dos_command_running;
490
491 if (dos_command_running)
492 {
493 /* That was the child who got the signal, not us. */
494 dos_status |= (sig << 8);
495 return;
496 }
497 remove_intermediates (1);
498 exit (EXIT_FAILURE);
499#else /* not __MSDOS__ */
500#ifdef _AMIGA
501 remove_intermediates (1);
502 if (sig == SIGINT)
503 fputs (_("*** Break.\n"), stderr);
504
505 exit (10);
506#else /* not Amiga */
507#ifdef WINDOWS32
508 extern HANDLE main_thread;
509
510 /* Windows creates a sperate thread for handling Ctrl+C, so we need
511 to suspend the main thread, or else we will have race conditions
512 when both threads call reap_children. */
513 if (main_thread)
514 {
515 DWORD susp_count = SuspendThread (main_thread);
516
517 if (susp_count != 0)
518 fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
519 else if (susp_count == (DWORD)-1)
520 {
521 DWORD ierr = GetLastError ();
522
523 fprintf (stderr, "SuspendThread: error %ld: %s\n",
524 ierr, map_windows32_error_to_string (ierr));
525 }
526 }
527#endif
528 handling_fatal_signal = 1;
529
530 /* Set the handling for this signal to the default.
531 It is blocked now while we run this handler. */
532 signal (sig, SIG_DFL);
533
534 /* A termination signal won't be sent to the entire
535 process group, but it means we want to kill the children. */
536
537 if (sig == SIGTERM)
538 {
539 struct child *c;
540 for (c = children; c != 0; c = c->next)
541 if (!c->remote)
542 (void) kill (c->pid, SIGTERM);
543 }
544
545 /* If we got a signal that means the user
546 wanted to kill make, remove pending targets. */
547
548 if (sig == SIGTERM || sig == SIGINT
549#ifdef SIGHUP
550 || sig == SIGHUP
551#endif
552#ifdef SIGQUIT
553 || sig == SIGQUIT
554#endif
555 )
556 {
557 struct child *c;
558
559 /* Remote children won't automatically get signals sent
560 to the process group, so we must send them. */
561 for (c = children; c != 0; c = c->next)
562 if (c->remote)
563 (void) remote_kill (c->pid, sig);
564
565 for (c = children; c != 0; c = c->next)
566 delete_child_targets (c);
567
568 /* Clean up the children. We don't just use the call below because
569 we don't want to print the "Waiting for children" message. */
570 while (job_slots_used > 0)
571 reap_children (1, 0);
572 }
573 else
574 /* Wait for our children to die. */
575 while (job_slots_used > 0)
576 reap_children (1, 1);
577
578 /* Delete any non-precious intermediate files that were made. */
579
580 remove_intermediates (1);
581
582#ifdef SIGQUIT
583 if (sig == SIGQUIT)
584 /* We don't want to send ourselves SIGQUIT, because it will
585 cause a core dump. Just exit instead. */
586 exit (EXIT_FAILURE);
587#endif
588
589#ifdef WINDOWS32
590 if (main_thread)
591 CloseHandle (main_thread);
592 /* Cannot call W32_kill with a pid (it needs a handle). The exit
593 status of 130 emulates what happens in Bash. */
594 exit (130);
595#else
596 /* Signal the same code; this time it will really be fatal. The signal
597 will be unblocked when we return and arrive then to kill us. */
598 if (kill (getpid (), sig) < 0)
599 pfatal_with_name ("kill");
600#endif /* not WINDOWS32 */
601#endif /* not Amiga */
602#endif /* not __MSDOS__ */
603}
604
605
606/* Delete FILE unless it's precious or not actually a file (phony),
607 and it has changed on disk since we last stat'd it. */
608
609static void
610delete_target (struct file *file, const char *on_behalf_of)
611{
612 struct stat st;
613 int e;
614
615 if (file->precious || file->phony)
616 return;
617
618#ifndef NO_ARCHIVES
619 if (ar_name (file->name))
620 {
621 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
622 ? (time_t) -1
623 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
624 if (ar_member_date (file->name) != file_date)
625 {
626 if (on_behalf_of)
627 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
628 on_behalf_of, file->name);
629 else
630 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
631 file->name);
632 }
633 return;
634 }
635#endif
636
637 EINTRLOOP (e, stat (file->name, &st));
638 if (e == 0
639 && S_ISREG (st.st_mode)
640 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
641 {
642 if (on_behalf_of)
643 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
644 else
645 error (NILF, _("*** Deleting file `%s'"), file->name);
646 if (unlink (file->name) < 0
647 && errno != ENOENT) /* It disappeared; so what. */
648 perror_with_name ("unlink: ", file->name);
649 }
650}
651
652
653/* Delete all non-precious targets of CHILD unless they were already deleted.
654 Set the flag in CHILD to say they've been deleted. */
655
656void
657delete_child_targets (struct child *child)
658{
659 struct dep *d;
660
661 if (child->deleted)
662 return;
663
664 /* Delete the target file if it changed. */
665 delete_target (child->file, NULL);
666
667 /* Also remove any non-precious targets listed in the `also_make' member. */
668 for (d = child->file->also_make; d != 0; d = d->next)
669 delete_target (d->file, child->file->name);
670
671 child->deleted = 1;
672}
673
674
675/* Print out the commands in CMDS. */
676
677void
678print_commands (const struct commands *cmds)
679{
680 const char *s;
681
682 fputs (_("# recipe to execute"), stdout);
683
684 if (cmds->fileinfo.filenm == 0)
685 puts (_(" (built-in):"));
686 else
687 printf (_(" (from `%s', line %lu):\n"),
688 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
689
690 s = cmds->commands;
691 while (*s != '\0')
692 {
693 const char *end;
694
695 end = strchr (s, '\n');
696 if (end == 0)
697 end = s + strlen (s);
698
699 printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);
700
701 s = end + (end[0] == '\n');
702 }
703}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette