VirtualBox

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

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

kmk: Implemented lazy resolving of $+, $, $? and $|. This saves > 30% memory and also a bit of time.

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