VirtualBox

source: kBuild/trunk/src/gmake/commands.c@ 520

Last change on this file since 520 was 520, checked in by bird, 18 years ago

Cleaning up the modifications. Changes are now either configurable or marked, and dead stuff has been removed (dll shell).

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