VirtualBox

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

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

commands.c: the file::stem isn't in the string cache if set before set_file_variables is called. (CONFIG_WITH_RDONLY_VARIABLE_VALUE)

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