VirtualBox

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

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

kmk: Made struct variable capable of holding read only variables to speed up set_file_variables.

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