VirtualBox

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

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

kmk: Fixed CONFIG_WITH_LAZY_DEPS_VARS bug. Files with multiple double colon rules cannot be handled lazily in any easy way.

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