VirtualBox

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

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

join + optimizations.

  • Property svn:eol-style set to native
File size: 14.8 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#ifdef CONFIG_WITH_KMK_BUILTIN
358 /* check if kmk builtin command */
359 if (!strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
360 flags |= COMMANDS_BUILTIN;
361#endif
362
363 cmds->lines_flags[idx] = flags;
364 cmds->any_recurse |= flags & COMMANDS_RECURSE;
365 }
366}
367
368
369/* Execute the commands to remake FILE. If they are currently executing,
370 return or have already finished executing, just return. Otherwise,
371 fork off a child process to run the first command line in the sequence. */
372
373void
374execute_file_commands (struct file *file)
375{
376 register char *p;
377
378 /* Don't go through all the preparations if
379 the commands are nothing but whitespace. */
380
381 for (p = file->cmds->commands; *p != '\0'; ++p)
382 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
383 break;
384 if (*p == '\0')
385 {
386 /* If there are no commands, assume everything worked. */
387 set_command_state (file, cs_running);
388 file->update_status = 0;
389 notice_finished_file (file);
390 return;
391 }
392
393 /* First set the automatic variables according to this file. */
394
395 initialize_file_variables (file, 0);
396
397 set_file_variables (file);
398
399 /* Start the commands running. */
400 new_job (file);
401}
402
403
404/* This is set while we are inside fatal_error_signal,
405 so things can avoid nonreentrant operations. */
406
407int handling_fatal_signal = 0;
408
409/* Handle fatal signals. */
410
411RETSIGTYPE
412fatal_error_signal (int sig)
413{
414#ifdef __MSDOS__
415 extern int dos_status, dos_command_running;
416
417 if (dos_command_running)
418 {
419 /* That was the child who got the signal, not us. */
420 dos_status |= (sig << 8);
421 return;
422 }
423 remove_intermediates (1);
424 exit (EXIT_FAILURE);
425#else /* not __MSDOS__ */
426#ifdef _AMIGA
427 remove_intermediates (1);
428 if (sig == SIGINT)
429 fputs (_("*** Break.\n"), stderr);
430
431 exit (10);
432#else /* not Amiga */
433 handling_fatal_signal = 1;
434
435 /* Set the handling for this signal to the default.
436 It is blocked now while we run this handler. */
437 signal (sig, SIG_DFL);
438
439 /* A termination signal won't be sent to the entire
440 process group, but it means we want to kill the children. */
441
442 if (sig == SIGTERM)
443 {
444 register struct child *c;
445 for (c = children; c != 0; c = c->next)
446 if (!c->remote)
447 (void) kill (c->pid, SIGTERM);
448 }
449
450 /* If we got a signal that means the user
451 wanted to kill make, remove pending targets. */
452
453 if (sig == SIGTERM || sig == SIGINT
454#ifdef SIGHUP
455 || sig == SIGHUP
456#endif
457#ifdef SIGQUIT
458 || sig == SIGQUIT
459#endif
460 )
461 {
462 register struct child *c;
463
464 /* Remote children won't automatically get signals sent
465 to the process group, so we must send them. */
466 for (c = children; c != 0; c = c->next)
467 if (c->remote)
468 (void) remote_kill (c->pid, sig);
469
470 for (c = children; c != 0; c = c->next)
471 delete_child_targets (c);
472
473 /* Clean up the children. We don't just use the call below because
474 we don't want to print the "Waiting for children" message. */
475 while (job_slots_used > 0)
476 reap_children (1, 0);
477 }
478 else
479 /* Wait for our children to die. */
480 while (job_slots_used > 0)
481 reap_children (1, 1);
482
483 /* Delete any non-precious intermediate files that were made. */
484
485 remove_intermediates (1);
486
487#ifdef SIGQUIT
488 if (sig == SIGQUIT)
489 /* We don't want to send ourselves SIGQUIT, because it will
490 cause a core dump. Just exit instead. */
491 exit (EXIT_FAILURE);
492#endif
493
494#ifdef WINDOWS32
495 /* Cannot call W32_kill with a pid (it needs a handle) */
496 exit (EXIT_FAILURE);
497#else
498 /* Signal the same code; this time it will really be fatal. The signal
499 will be unblocked when we return and arrive then to kill us. */
500 if (kill (getpid (), sig) < 0)
501 pfatal_with_name ("kill");
502#endif /* not WINDOWS32 */
503#endif /* not Amiga */
504#endif /* not __MSDOS__ */
505}
506
507
508/* Delete FILE unless it's precious or not actually a file (phony),
509 and it has changed on disk since we last stat'd it. */
510
511static void
512delete_target (struct file *file, char *on_behalf_of)
513{
514 struct stat st;
515 int e;
516
517 if (file->precious || file->phony)
518 return;
519
520#ifndef NO_ARCHIVES
521 if (ar_name (file->name))
522 {
523 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
524 ? (time_t) -1
525 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
526 if (ar_member_date (file->name) != file_date)
527 {
528 if (on_behalf_of)
529 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
530 on_behalf_of, file->name);
531 else
532 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
533 file->name);
534 }
535 return;
536 }
537#endif
538
539 EINTRLOOP (e, stat (file->name, &st));
540 if (e == 0
541 && S_ISREG (st.st_mode)
542 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
543 {
544 if (on_behalf_of)
545 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
546 else
547 error (NILF, _("*** Deleting file `%s'"), file->name);
548 if (unlink (file->name) < 0
549 && errno != ENOENT) /* It disappeared; so what. */
550 perror_with_name ("unlink: ", file->name);
551 }
552}
553
554
555/* Delete all non-precious targets of CHILD unless they were already deleted.
556 Set the flag in CHILD to say they've been deleted. */
557
558void
559delete_child_targets (struct child *child)
560{
561 struct dep *d;
562
563 if (child->deleted)
564 return;
565
566 /* Delete the target file if it changed. */
567 delete_target (child->file, (char *) 0);
568
569 /* Also remove any non-precious targets listed in the `also_make' member. */
570 for (d = child->file->also_make; d != 0; d = d->next)
571 delete_target (d->file, child->file->name);
572
573 child->deleted = 1;
574}
575
576
577/* Print out the commands in CMDS. */
578
579void
580print_commands (struct commands *cmds)
581{
582 register char *s;
583
584 fputs (_("# commands to execute"), stdout);
585
586 if (cmds->fileinfo.filenm == 0)
587 puts (_(" (built-in):"));
588 else
589 printf (_(" (from `%s', line %lu):\n"),
590 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
591
592 s = cmds->commands;
593 while (*s != '\0')
594 {
595 char *end;
596
597 while (isspace ((unsigned char)*s))
598 ++s;
599
600 end = strchr (s, '\n');
601 if (end == 0)
602 end = s + strlen (s);
603
604 printf ("\t%.*s\n", (int) (end - s), s);
605
606 s = end;
607 }
608}
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