VirtualBox

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

Last change on this file since 191 was 154, checked in by bird, 20 years ago

This commit was generated by cvs2svn to compensate for changes in r153,
which included commits to RCS files with non-trunk default branches.

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