Another round of cleanups:
[make.git] / commands.c
blob923dc7c75135b77a11eb982242d629c06716ff20
1 /* Command processing for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 GNU Make; see the file COPYING. If not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
19 #include "make.h"
20 #include "dep.h"
21 #include "filedef.h"
22 #include "variable.h"
23 #include "job.h"
24 #include "commands.h"
25 #ifdef WINDOWS32
26 #include <windows.h>
27 #include "w32err.h"
28 #endif
30 #if VMS
31 # define FILE_LIST_SEPARATOR ','
32 #else
33 # define FILE_LIST_SEPARATOR ' '
34 #endif
36 int remote_kill (int id, int sig);
38 #ifndef HAVE_UNISTD_H
39 int getpid ();
40 #endif
42 /* Set FILE's automatic variables up. */
44 void
45 set_file_variables (struct file *file)
47 struct dep *d;
48 char *at, *percent, *star, *less;
50 #ifndef NO_ARCHIVES
51 /* If the target is an archive member `lib(member)',
52 then $@ is `lib' and $% is `member'. */
54 if (ar_name (file->name))
56 unsigned int len;
57 char *p;
59 p = strchr (file->name, '(');
60 at = alloca (p - file->name + 1);
61 memcpy (at, file->name, p - file->name);
62 at[p - file->name] = '\0';
63 len = strlen (p + 1);
64 percent = alloca (len);
65 memcpy (percent, p + 1, len - 1);
66 percent[len - 1] = '\0';
68 else
69 #endif /* NO_ARCHIVES. */
71 at = file->name;
72 percent = "";
75 /* $* is the stem from an implicit or static pattern rule. */
76 if (file->stem == 0)
78 /* In Unix make, $* is set to the target name with
79 any suffix in the .SUFFIXES list stripped off for
80 explicit rules. We store this in the `stem' member. */
81 char *name;
82 unsigned int len;
84 #ifndef NO_ARCHIVES
85 if (ar_name (file->name))
87 name = strchr (file->name, '(') + 1;
88 len = strlen (name) - 1;
90 else
91 #endif
93 name = file->name;
94 len = strlen (name);
97 for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
99 unsigned int slen = strlen (dep_name (d));
100 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
102 file->stem = savestring (name, len - slen);
103 break;
106 if (d == 0)
107 file->stem = "";
109 star = file->stem;
111 /* $< is the first not order-only dependency. */
112 less = "";
113 for (d = file->deps; d != 0; d = d->next)
114 if (!d->ignore_mtime)
116 less = dep_name (d);
117 break;
120 if (file->cmds == default_file->cmds)
121 /* This file got its commands from .DEFAULT.
122 In this case $< is the same as $@. */
123 less = at;
125 #define DEFINE_VARIABLE(name, len, value) \
126 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
128 /* Define the variables. */
130 DEFINE_VARIABLE ("<", 1, less);
131 DEFINE_VARIABLE ("*", 1, star);
132 DEFINE_VARIABLE ("@", 1, at);
133 DEFINE_VARIABLE ("%", 1, percent);
135 /* Compute the values for $^, $+, $?, and $|. */
138 static char *plus_value=0, *bar_value=0, *qmark_value=0;
139 static unsigned int qmark_max=0, plus_max=0, bar_max=0;
141 unsigned int qmark_len, plus_len, bar_len;
142 char *cp;
143 char *caret_value;
144 char *qp;
145 char *bp;
146 unsigned int len;
148 /* Compute first the value for $+, which is supposed to contain
149 duplicate dependencies as they were listed in the makefile. */
151 plus_len = 0;
152 for (d = file->deps; d != 0; d = d->next)
153 if (! d->ignore_mtime)
154 plus_len += strlen (dep_name (d)) + 1;
155 if (plus_len == 0)
156 plus_len++;
158 if (plus_len > plus_max)
159 plus_value = xrealloc (plus_value, plus_max = plus_len);
160 cp = plus_value;
162 qmark_len = plus_len + 1; /* Will be this or less. */
163 for (d = file->deps; d != 0; d = d->next)
164 if (! d->ignore_mtime)
166 char *c = dep_name (d);
168 #ifndef NO_ARCHIVES
169 if (ar_name (c))
171 c = strchr (c, '(') + 1;
172 len = strlen (c) - 1;
174 else
175 #endif
176 len = strlen (c);
178 memcpy (cp, c, len);
179 cp += len;
180 *cp++ = FILE_LIST_SEPARATOR;
181 if (! d->changed)
182 qmark_len -= len + 1; /* Don't space in $? for this one. */
185 /* Kill the last space and define the variable. */
187 cp[cp > plus_value ? -1 : 0] = '\0';
188 DEFINE_VARIABLE ("+", 1, plus_value);
190 /* Make sure that no dependencies are repeated. This does not
191 really matter for the purpose of updating targets, but it
192 might make some names be listed twice for $^ and $?. */
194 uniquize_deps (file->deps);
196 bar_len = 0;
197 for (d = file->deps; d != 0; d = d->next)
198 if (d->ignore_mtime)
199 bar_len += strlen (dep_name (d)) + 1;
200 if (bar_len == 0)
201 bar_len++;
203 /* Compute the values for $^, $?, and $|. */
205 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
207 if (qmark_len > qmark_max)
208 qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
209 qp = qmark_value;
211 if (bar_len > bar_max)
212 bar_value = xrealloc (bar_value, bar_max = bar_len);
213 bp = bar_value;
215 for (d = file->deps; d != 0; d = d->next)
217 char *c = dep_name (d);
219 #ifndef NO_ARCHIVES
220 if (ar_name (c))
222 c = strchr (c, '(') + 1;
223 len = strlen (c) - 1;
225 else
226 #endif
227 len = strlen (c);
229 if (d->ignore_mtime)
231 memcpy (bp, c, len);
232 bp += len;
233 *bp++ = FILE_LIST_SEPARATOR;
235 else
237 memcpy (cp, c, len);
238 cp += len;
239 *cp++ = FILE_LIST_SEPARATOR;
240 if (d->changed)
242 memcpy (qp, c, len);
243 qp += len;
244 *qp++ = FILE_LIST_SEPARATOR;
249 /* Kill the last spaces and define the variables. */
251 cp[cp > caret_value ? -1 : 0] = '\0';
252 DEFINE_VARIABLE ("^", 1, caret_value);
254 qp[qp > qmark_value ? -1 : 0] = '\0';
255 DEFINE_VARIABLE ("?", 1, qmark_value);
257 bp[bp > bar_value ? -1 : 0] = '\0';
258 DEFINE_VARIABLE ("|", 1, bar_value);
261 #undef DEFINE_VARIABLE
264 /* Chop CMDS up into individual command lines if necessary.
265 Also set the `lines_flags' and `any_recurse' members. */
267 void
268 chop_commands (struct commands *cmds)
270 register char *p;
271 unsigned int nlines, idx;
272 char **lines;
274 /* If we don't have any commands,
275 or we already parsed them, never mind. */
277 if (!cmds || cmds->command_lines != 0)
278 return;
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. */
284 nlines = 5;
285 lines = xmalloc (5 * sizeof (char *));
286 idx = 0;
287 p = cmds->commands;
288 while (*p != '\0')
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] == '\\')
297 int backslash = 1;
298 register char *b;
299 for (b = end - 2; b >= p && *b == '\\'; --b)
300 backslash = !backslash;
301 if (backslash)
303 ++end;
304 goto find_end;
308 if (idx == nlines)
310 nlines += 2;
311 lines = xrealloc (lines, nlines * sizeof (char *));
313 lines[idx++] = savestring (p, end - p);
314 p = end;
315 if (*p != '\0')
316 ++p;
319 if (idx != nlines)
321 nlines = idx;
322 lines = xrealloc (lines, nlines * sizeof (char *));
325 cmds->ncommand_lines = nlines;
326 cmds->command_lines = lines;
328 cmds->any_recurse = 0;
329 cmds->lines_flags = 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 #ifdef WINDOWS32
424 extern HANDLE main_thread;
426 /* Windows creates a sperate thread for handling Ctrl+C, so we need
427 to suspend the main thread, or else we will have race conditions
428 when both threads call reap_children. */
429 if (main_thread)
431 DWORD susp_count = SuspendThread (main_thread);
433 if (susp_count != 0)
434 fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
435 else if (susp_count == (DWORD)-1)
437 DWORD ierr = GetLastError ();
439 fprintf (stderr, "SuspendThread: error %ld: %s\n",
440 ierr, map_windows32_error_to_string (ierr));
443 #endif
444 handling_fatal_signal = 1;
446 /* Set the handling for this signal to the default.
447 It is blocked now while we run this handler. */
448 signal (sig, SIG_DFL);
450 /* A termination signal won't be sent to the entire
451 process group, but it means we want to kill the children. */
453 if (sig == SIGTERM)
455 register struct child *c;
456 for (c = children; c != 0; c = c->next)
457 if (!c->remote)
458 (void) kill (c->pid, SIGTERM);
461 /* If we got a signal that means the user
462 wanted to kill make, remove pending targets. */
464 if (sig == SIGTERM || sig == SIGINT
465 #ifdef SIGHUP
466 || sig == SIGHUP
467 #endif
468 #ifdef SIGQUIT
469 || sig == SIGQUIT
470 #endif
473 register struct child *c;
475 /* Remote children won't automatically get signals sent
476 to the process group, so we must send them. */
477 for (c = children; c != 0; c = c->next)
478 if (c->remote)
479 (void) remote_kill (c->pid, sig);
481 for (c = children; c != 0; c = c->next)
482 delete_child_targets (c);
484 /* Clean up the children. We don't just use the call below because
485 we don't want to print the "Waiting for children" message. */
486 while (job_slots_used > 0)
487 reap_children (1, 0);
489 else
490 /* Wait for our children to die. */
491 while (job_slots_used > 0)
492 reap_children (1, 1);
494 /* Delete any non-precious intermediate files that were made. */
496 remove_intermediates (1);
498 #ifdef SIGQUIT
499 if (sig == SIGQUIT)
500 /* We don't want to send ourselves SIGQUIT, because it will
501 cause a core dump. Just exit instead. */
502 exit (EXIT_FAILURE);
503 #endif
505 #ifdef WINDOWS32
506 if (main_thread)
507 CloseHandle (main_thread);
508 /* Cannot call W32_kill with a pid (it needs a handle). The exit
509 status of 130 emulates what happens in Bash. */
510 exit (130);
511 #else
512 /* Signal the same code; this time it will really be fatal. The signal
513 will be unblocked when we return and arrive then to kill us. */
514 if (kill (getpid (), sig) < 0)
515 pfatal_with_name ("kill");
516 #endif /* not WINDOWS32 */
517 #endif /* not Amiga */
518 #endif /* not __MSDOS__ */
521 /* Delete FILE unless it's precious or not actually a file (phony),
522 and it has changed on disk since we last stat'd it. */
524 static void
525 delete_target (struct file *file, char *on_behalf_of)
527 struct stat st;
528 int e;
530 if (file->precious || file->phony)
531 return;
533 #ifndef NO_ARCHIVES
534 if (ar_name (file->name))
536 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
537 ? (time_t) -1
538 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
539 if (ar_member_date (file->name) != file_date)
541 if (on_behalf_of)
542 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
543 on_behalf_of, file->name);
544 else
545 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
546 file->name);
548 return;
550 #endif
552 EINTRLOOP (e, stat (file->name, &st));
553 if (e == 0
554 && S_ISREG (st.st_mode)
555 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
557 if (on_behalf_of)
558 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
559 else
560 error (NILF, _("*** Deleting file `%s'"), file->name);
561 if (unlink (file->name) < 0
562 && errno != ENOENT) /* It disappeared; so what. */
563 perror_with_name ("unlink: ", file->name);
568 /* Delete all non-precious targets of CHILD unless they were already deleted.
569 Set the flag in CHILD to say they've been deleted. */
571 void
572 delete_child_targets (struct child *child)
574 struct dep *d;
576 if (child->deleted)
577 return;
579 /* Delete the target file if it changed. */
580 delete_target (child->file, 0);
582 /* Also remove any non-precious targets listed in the `also_make' member. */
583 for (d = child->file->also_make; d != 0; d = d->next)
584 delete_target (d->file, child->file->name);
586 child->deleted = 1;
589 /* Print out the commands in CMDS. */
591 void
592 print_commands (struct commands *cmds)
594 register char *s;
596 fputs (_("# commands to execute"), stdout);
598 if (cmds->fileinfo.filenm == 0)
599 puts (_(" (built-in):"));
600 else
601 printf (_(" (from `%s', line %lu):\n"),
602 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
604 s = cmds->commands;
605 while (*s != '\0')
607 char *end;
609 while (isspace ((unsigned char)*s))
610 ++s;
612 end = strchr (s, '\n');
613 if (end == 0)
614 end = s + strlen (s);
616 printf ("\t%.*s\n", (int) (end - s), s);
618 s = end;