VirtualBox

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

Last change on this file since 1673 was 1440, checked in by bird, 17 years ago

Added comp-cmds-ex, commands, commands-sc and commands-usr. Added a '%' command prefix that make the commands functions skip the line. Added a -c flag to append that'll make it call commands on each argument (similar to -v). Fixed a little bug in comp-cmds/vars.

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