Fix problems with losing tokens in the jobserver, reported by Grant
[make.git] / commands.c
blob813610c6c80e1bc2cc58a590b55445cdc2050e3e
1 /* Command processing for GNU Make.
2 Copyright (C) 1988,89,91,92,93,94,95,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
21 #include "dep.h"
22 #include "filedef.h"
23 #include "variable.h"
24 #include "job.h"
25 #include "commands.h"
27 #if VMS
28 # define FILE_LIST_SEPARATOR ','
29 #else
30 # define FILE_LIST_SEPARATOR ' '
31 #endif
33 extern int remote_kill PARAMS ((int id, int sig));
35 #ifndef HAVE_UNISTD_H
36 extern int getpid ();
37 #endif
39 /* Set FILE's automatic variables up. */
41 void
42 set_file_variables (struct file *file)
44 struct dep *d;
45 char *at, *percent, *star, *less;
47 #ifndef NO_ARCHIVES
48 /* If the target is an archive member `lib(member)',
49 then $@ is `lib' and $% is `member'. */
51 if (ar_name (file->name))
53 unsigned int len;
54 char *p;
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';
65 else
66 #endif /* NO_ARCHIVES. */
68 at = file->name;
69 percent = "";
72 /* $* is the stem from an implicit or static pattern rule. */
73 if (file->stem == 0)
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;
82 #ifndef NO_ARCHIVES
83 if (ar_name (file->name))
85 name = strchr (file->name, '(') + 1;
86 len = strlen (name) - 1;
88 else
89 #endif
91 name = file->name;
92 len = strlen (name);
95 for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
97 unsigned int slen = strlen (dep_name (d));
98 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
100 file->stem = savestring (name, len - slen);
101 break;
104 if (d == 0)
105 file->stem = "";
107 star = file->stem;
109 /* $< is the first not order-only dependency. */
110 less = "";
111 for (d = file->deps; d != 0; d = d->next)
112 if (!d->ignore_mtime)
114 less = dep_name (d);
115 break;
118 if (file->cmds == default_file->cmds)
119 /* This file got its commands from .DEFAULT.
120 In this case $< is the same as $@. */
121 less = at;
123 #define DEFINE_VARIABLE(name, len, value) \
124 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
126 /* Define the variables. */
128 DEFINE_VARIABLE ("<", 1, less);
129 DEFINE_VARIABLE ("*", 1, star);
130 DEFINE_VARIABLE ("@", 1, at);
131 DEFINE_VARIABLE ("%", 1, percent);
133 /* Compute the values for $^, $+, $?, and $|. */
136 static char *plus_value=0, *bar_value=0, *qmark_value=0;
137 static unsigned int qmark_max=0, plus_max=0, bar_max=0;
139 unsigned int qmark_len, plus_len, bar_len;
140 char *cp;
141 char *caret_value;
142 char *qp;
143 char *bp;
144 unsigned int len;
146 /* Compute first the value for $+, which is supposed to contain
147 duplicate dependencies as they were listed in the makefile. */
149 plus_len = 0;
150 for (d = file->deps; d != 0; d = d->next)
151 if (! d->ignore_mtime)
152 plus_len += strlen (dep_name (d)) + 1;
153 if (plus_len == 0)
154 plus_len++;
156 if (plus_len > plus_max)
157 plus_value = (char *) xmalloc (plus_max = plus_len);
158 cp = plus_value;
160 qmark_len = plus_len + 1; /* Will be this or less. */
161 for (d = file->deps; d != 0; d = d->next)
162 if (! d->ignore_mtime)
164 char *c = dep_name (d);
166 #ifndef NO_ARCHIVES
167 if (ar_name (c))
169 c = strchr (c, '(') + 1;
170 len = strlen (c) - 1;
172 else
173 #endif
174 len = strlen (c);
176 bcopy (c, cp, len);
177 cp += len;
178 *cp++ = FILE_LIST_SEPARATOR;
179 if (! d->changed)
180 qmark_len -= len + 1; /* Don't space in $? for this one. */
183 /* Kill the last space and define the variable. */
185 cp[cp > plus_value ? -1 : 0] = '\0';
186 DEFINE_VARIABLE ("+", 1, plus_value);
188 /* Make sure that no dependencies are repeated. This does not
189 really matter for the purpose of updating targets, but it
190 might make some names be listed twice for $^ and $?. */
192 uniquize_deps (file->deps);
194 bar_len = 0;
195 for (d = file->deps; d != 0; d = d->next)
196 if (d->ignore_mtime)
197 bar_len += strlen (dep_name (d)) + 1;
198 if (bar_len == 0)
199 bar_len++;
201 /* Compute the values for $^, $?, and $|. */
203 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
205 if (qmark_len > qmark_max)
206 qmark_value = (char *) xmalloc (qmark_max = qmark_len);
207 qp = qmark_value;
209 if (bar_len > bar_max)
210 bar_value = (char *) xmalloc (bar_max = bar_len);
211 bp = bar_value;
213 for (d = file->deps; d != 0; d = d->next)
215 char *c = dep_name (d);
217 #ifndef NO_ARCHIVES
218 if (ar_name (c))
220 c = strchr (c, '(') + 1;
221 len = strlen (c) - 1;
223 else
224 #endif
225 len = strlen (c);
227 if (d->ignore_mtime)
229 bcopy (c, bp, len);
230 bp += len;
231 *bp++ = FILE_LIST_SEPARATOR;
233 else
235 bcopy (c, cp, len);
236 cp += len;
237 *cp++ = FILE_LIST_SEPARATOR;
238 if (d->changed)
240 bcopy (c, qp, len);
241 qp += len;
242 *qp++ = FILE_LIST_SEPARATOR;
247 /* Kill the last spaces and define the variables. */
249 cp[cp > caret_value ? -1 : 0] = '\0';
250 DEFINE_VARIABLE ("^", 1, caret_value);
252 qp[qp > qmark_value ? -1 : 0] = '\0';
253 DEFINE_VARIABLE ("?", 1, qmark_value);
255 bp[bp > bar_value ? -1 : 0] = '\0';
256 DEFINE_VARIABLE ("|", 1, bar_value);
259 #undef DEFINE_VARIABLE
262 /* Chop CMDS up into individual command lines if necessary.
263 Also set the `lines_flags' and `any_recurse' members. */
265 void
266 chop_commands (struct commands *cmds)
268 register char *p;
269 unsigned int nlines, idx;
270 char **lines;
272 /* If we don't have any commands,
273 or we already parsed them, never mind. */
275 if (!cmds || cmds->command_lines != 0)
276 return;
278 /* Chop CMDS->commands up into lines in CMDS->command_lines.
279 Also set the corresponding CMDS->lines_flags elements,
280 and the CMDS->any_recurse flag. */
282 nlines = 5;
283 lines = (char **) xmalloc (5 * sizeof (char *));
284 idx = 0;
285 p = cmds->commands;
286 while (*p != '\0')
288 char *end = p;
289 find_end:;
290 end = strchr (end, '\n');
291 if (end == 0)
292 end = p + strlen (p);
293 else if (end > p && end[-1] == '\\')
295 int backslash = 1;
296 register char *b;
297 for (b = end - 2; b >= p && *b == '\\'; --b)
298 backslash = !backslash;
299 if (backslash)
301 ++end;
302 goto find_end;
306 if (idx == nlines)
308 nlines += 2;
309 lines = (char **) xrealloc ((char *) lines,
310 nlines * sizeof (char *));
312 lines[idx++] = savestring (p, end - p);
313 p = end;
314 if (*p != '\0')
315 ++p;
318 if (idx != nlines)
320 nlines = idx;
321 lines = (char **) xrealloc ((char *) lines,
322 nlines * sizeof (char *));
325 cmds->ncommand_lines = nlines;
326 cmds->command_lines = lines;
328 cmds->any_recurse = 0;
329 cmds->lines_flags = (char *) xmalloc (nlines);
330 for (idx = 0; idx < nlines; ++idx)
332 int flags = 0;
334 for (p = lines[idx];
335 isblank ((unsigned char)*p) || *p == '-' || *p == '@' || *p == '+';
336 ++p)
337 switch (*p)
339 case '+':
340 flags |= COMMANDS_RECURSE;
341 break;
342 case '@':
343 flags |= COMMANDS_SILENT;
344 break;
345 case '-':
346 flags |= COMMANDS_NOERROR;
347 break;
350 /* If no explicit '+' was given, look for MAKE variable references. */
351 if (!(flags & COMMANDS_RECURSE)
352 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
353 flags |= COMMANDS_RECURSE;
355 cmds->lines_flags[idx] = flags;
356 cmds->any_recurse |= flags & COMMANDS_RECURSE;
360 /* Execute the commands to remake FILE. If they are currently executing,
361 return or have already finished executing, just return. Otherwise,
362 fork off a child process to run the first command line in the sequence. */
364 void
365 execute_file_commands (struct file *file)
367 register char *p;
369 /* Don't go through all the preparations if
370 the commands are nothing but whitespace. */
372 for (p = file->cmds->commands; *p != '\0'; ++p)
373 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
374 break;
375 if (*p == '\0')
377 /* If there are no commands, assume everything worked. */
378 set_command_state (file, cs_running);
379 file->update_status = 0;
380 notice_finished_file (file);
381 return;
384 /* First set the automatic variables according to this file. */
386 initialize_file_variables (file, 0);
388 set_file_variables (file);
390 /* Start the commands running. */
391 new_job (file);
394 /* This is set while we are inside fatal_error_signal,
395 so things can avoid nonreentrant operations. */
397 int handling_fatal_signal = 0;
399 /* Handle fatal signals. */
401 RETSIGTYPE
402 fatal_error_signal (int sig)
404 #ifdef __MSDOS__
405 extern int dos_status, dos_command_running;
407 if (dos_command_running)
409 /* That was the child who got the signal, not us. */
410 dos_status |= (sig << 8);
411 return;
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);
421 exit (10);
422 #else /* not Amiga */
423 handling_fatal_signal = 1;
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);
429 /* A termination signal won't be sent to the entire
430 process group, but it means we want to kill the children. */
432 if (sig == SIGTERM)
434 register struct child *c;
435 for (c = children; c != 0; c = c->next)
436 if (!c->remote)
437 (void) kill (c->pid, SIGTERM);
440 /* If we got a signal that means the user
441 wanted to kill make, remove pending targets. */
443 if (sig == SIGTERM || sig == SIGINT
444 #ifdef SIGHUP
445 || sig == SIGHUP
446 #endif
447 #ifdef SIGQUIT
448 || sig == SIGQUIT
449 #endif
452 register struct child *c;
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);
460 for (c = children; c != 0; c = c->next)
461 delete_child_targets (c);
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);
468 else
469 /* Wait for our children to die. */
470 while (job_slots_used > 0)
471 reap_children (1, 1);
473 /* Delete any non-precious intermediate files that were made. */
475 remove_intermediates (1);
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
484 #ifdef WINDOWS32
485 /* Cannot call W32_kill with a pid (it needs a handle) */
486 exit (EXIT_FAILURE);
487 #else
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 WINDOWS32 */
493 #endif /* not Amiga */
494 #endif /* not __MSDOS__ */
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. */
500 static void
501 delete_target (struct file *file, char *on_behalf_of)
503 struct stat st;
504 int e;
506 if (file->precious || file->phony)
507 return;
509 #ifndef NO_ARCHIVES
510 if (ar_name (file->name))
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)
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);
524 return;
526 #endif
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)
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);
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. */
547 void
548 delete_child_targets (struct child *child)
550 struct dep *d;
552 if (child->deleted)
553 return;
555 /* Delete the target file if it changed. */
556 delete_target (child->file, (char *) 0);
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);
562 child->deleted = 1;
565 /* Print out the commands in CMDS. */
567 void
568 print_commands (struct commands *cmds)
570 register char *s;
572 fputs (_("# commands to execute"), stdout);
574 if (cmds->fileinfo.filenm == 0)
575 puts (_(" (built-in):"));
576 else
577 printf (_(" (from `%s', line %lu):\n"),
578 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
580 s = cmds->commands;
581 while (*s != '\0')
583 char *end;
585 while (isspace ((unsigned char)*s))
586 ++s;
588 end = strchr (s, '\n');
589 if (end == 0)
590 end = s + strlen (s);
592 printf ("\t%.*s\n", (int) (end - s), s);
594 s = end;