VirtualBox

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

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

kmk: replaced strlen with strcache_get_len in a number of place in set_file_variables (snap_deps).

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