VirtualBox

source: kBuild/vendor/gnumake/2005-05-16/commands.c

Last change on this file was 280, checked in by bird, 19 years ago

Current make snaphot, 2005-05-16.

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