VirtualBox

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

Last change on this file since 1993 was 1993, checked in by bird, 16 years ago

Merged in current GNU Make code (CVS from 2008-10-28). Ref #55.

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