VirtualBox

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

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

kMk builtin command basics. KMK_VERSION variable.

  • 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
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#ifdef CONFIG_WITH_KMK_BUILTIN
353 if (!strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
354 flags |= COMMANDS_BUILTIN;
355#endif
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 /* Signal the same code; this time it will really be fatal. The signal
489 will be unblocked when we return and arrive then to kill us. */
490 if (kill (getpid (), sig) < 0)
491 pfatal_with_name ("kill");
492#endif /* not Amiga */
493#endif /* not __MSDOS__ */
494}
495
496
497/* Delete FILE unless it's precious or not actually a file (phony),
498 and it has changed on disk since we last stat'd it. */
499
500static void
501delete_target (struct file *file, char *on_behalf_of)
502{
503 struct stat st;
504 int e;
505
506 if (file->precious || file->phony)
507 return;
508
509#ifndef NO_ARCHIVES
510 if (ar_name (file->name))
511 {
512 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
513 ? (time_t) -1
514 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
515 if (ar_member_date (file->name) != file_date)
516 {
517 if (on_behalf_of)
518 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
519 on_behalf_of, file->name);
520 else
521 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
522 file->name);
523 }
524 return;
525 }
526#endif
527
528 EINTRLOOP (e, stat (file->name, &st));
529 if (e == 0
530 && S_ISREG (st.st_mode)
531 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
532 {
533 if (on_behalf_of)
534 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
535 else
536 error (NILF, _("*** Deleting file `%s'"), file->name);
537 if (unlink (file->name) < 0
538 && errno != ENOENT) /* It disappeared; so what. */
539 perror_with_name ("unlink: ", file->name);
540 }
541}
542
543
544/* Delete all non-precious targets of CHILD unless they were already deleted.
545 Set the flag in CHILD to say they've been deleted. */
546
547void
548delete_child_targets (struct child *child)
549{
550 struct dep *d;
551
552 if (child->deleted)
553 return;
554
555 /* Delete the target file if it changed. */
556 delete_target (child->file, (char *) 0);
557
558 /* Also remove any non-precious targets listed in the `also_make' member. */
559 for (d = child->file->also_make; d != 0; d = d->next)
560 delete_target (d->file, child->file->name);
561
562 child->deleted = 1;
563}
564
565
566/* Print out the commands in CMDS. */
567
568void
569print_commands (struct commands *cmds)
570{
571 register char *s;
572
573 fputs (_("# commands to execute"), stdout);
574
575 if (cmds->fileinfo.filenm == 0)
576 puts (_(" (built-in):"));
577 else
578 printf (_(" (from `%s', line %lu):\n"),
579 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
580
581 s = cmds->commands;
582 while (*s != '\0')
583 {
584 char *end;
585
586 while (isspace ((unsigned char)*s))
587 ++s;
588
589 end = strchr (s, '\n');
590 if (end == 0)
591 end = s + strlen (s);
592
593 printf ("\t%.*s\n", (int) (end - s), s);
594
595 s = end;
596 }
597}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette