- Include <alloca.h> even on non-__GNUC__ systems.
[make.git] / commands.c
blobd9d7f539aade0e4e1e28a46d01a35e2209e81c1a
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, 2007 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 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
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 const struct dep *d;
48 const 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 const char *cp;
58 char *p;
60 cp = strchr (file->name, '(');
61 p = alloca (cp - file->name + 1);
62 memcpy (p, file->name, cp - file->name);
63 p[cp - file->name] = '\0';
64 at = p;
65 len = strlen (cp + 1);
66 p = alloca (len);
67 memcpy (p, cp + 1, len - 1);
68 p[len - 1] = '\0';
69 percent = p;
71 else
72 #endif /* NO_ARCHIVES. */
74 at = file->name;
75 percent = "";
78 /* $* is the stem from an implicit or static pattern rule. */
79 if (file->stem == 0)
81 /* In Unix make, $* is set to the target name with
82 any suffix in the .SUFFIXES list stripped off for
83 explicit rules. We store this in the `stem' member. */
84 const char *name;
85 unsigned int len;
87 #ifndef NO_ARCHIVES
88 if (ar_name (file->name))
90 name = strchr (file->name, '(') + 1;
91 len = strlen (name) - 1;
93 else
94 #endif
96 name = file->name;
97 len = strlen (name);
100 for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
102 unsigned int slen = strlen (dep_name (d));
103 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
105 file->stem = strcache_add_len (name, len - slen);
106 break;
109 if (d == 0)
110 file->stem = "";
112 star = file->stem;
114 /* $< is the first not order-only dependency. */
115 less = "";
116 for (d = file->deps; d != 0; d = d->next)
117 if (!d->ignore_mtime)
119 if (!d->need_2nd_expansion)
120 less = dep_name (d);
121 break;
124 if (file->cmds == default_file->cmds)
125 /* This file got its commands from .DEFAULT.
126 In this case $< is the same as $@. */
127 less = at;
129 #define DEFINE_VARIABLE(name, len, value) \
130 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
132 /* Define the variables. */
134 DEFINE_VARIABLE ("<", 1, less);
135 DEFINE_VARIABLE ("*", 1, star);
136 DEFINE_VARIABLE ("@", 1, at);
137 DEFINE_VARIABLE ("%", 1, percent);
139 /* Compute the values for $^, $+, $?, and $|. */
142 static char *plus_value=0, *bar_value=0, *qmark_value=0;
143 static unsigned int plus_max=0, bar_max=0, qmark_max=0;
145 unsigned int qmark_len, plus_len, bar_len;
146 char *cp;
147 char *caret_value;
148 char *qp;
149 char *bp;
150 unsigned int len;
152 /* Compute first the value for $+, which is supposed to contain
153 duplicate dependencies as they were listed in the makefile. */
155 plus_len = 0;
156 for (d = file->deps; d != 0; d = d->next)
157 if (! d->ignore_mtime && ! d->need_2nd_expansion)
158 plus_len += strlen (dep_name (d)) + 1;
159 if (plus_len == 0)
160 plus_len++;
162 if (plus_len > plus_max)
163 plus_value = xrealloc (plus_value, plus_max = plus_len);
164 cp = plus_value;
166 qmark_len = plus_len + 1; /* Will be this or less. */
167 for (d = file->deps; d != 0; d = d->next)
168 if (! d->ignore_mtime && ! d->need_2nd_expansion)
170 const char *c = dep_name (d);
172 #ifndef NO_ARCHIVES
173 if (ar_name (c))
175 c = strchr (c, '(') + 1;
176 len = strlen (c) - 1;
178 else
179 #endif
180 len = strlen (c);
182 memcpy (cp, c, len);
183 cp += len;
184 *cp++ = FILE_LIST_SEPARATOR;
185 if (! (d->changed || always_make_flag))
186 qmark_len -= len + 1; /* Don't space in $? for this one. */
189 /* Kill the last space and define the variable. */
191 cp[cp > plus_value ? -1 : 0] = '\0';
192 DEFINE_VARIABLE ("+", 1, plus_value);
194 /* Make sure that no dependencies are repeated. This does not
195 really matter for the purpose of updating targets, but it
196 might make some names be listed twice for $^ and $?. */
198 uniquize_deps (file->deps);
200 bar_len = 0;
201 for (d = file->deps; d != 0; d = d->next)
202 if (d->ignore_mtime && ! d->need_2nd_expansion)
203 bar_len += strlen (dep_name (d)) + 1;
204 if (bar_len == 0)
205 bar_len++;
207 /* Compute the values for $^, $?, and $|. */
209 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
211 if (qmark_len > qmark_max)
212 qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
213 qp = qmark_value;
215 if (bar_len > bar_max)
216 bar_value = xrealloc (bar_value, bar_max = bar_len);
217 bp = bar_value;
219 for (d = file->deps; d != 0; d = d->next)
221 const char *c;
223 if (d->need_2nd_expansion)
224 continue;
226 c = dep_name (d);
227 #ifndef NO_ARCHIVES
228 if (ar_name (c))
230 c = strchr (c, '(') + 1;
231 len = strlen (c) - 1;
233 else
234 #endif
235 len = strlen (c);
237 if (d->ignore_mtime)
239 memcpy (bp, c, len);
240 bp += len;
241 *bp++ = FILE_LIST_SEPARATOR;
243 else
245 memcpy (cp, c, len);
246 cp += len;
247 *cp++ = FILE_LIST_SEPARATOR;
248 if (d->changed || always_make_flag)
250 memcpy (qp, c, len);
251 qp += len;
252 *qp++ = FILE_LIST_SEPARATOR;
257 /* Kill the last spaces and define the variables. */
259 cp[cp > caret_value ? -1 : 0] = '\0';
260 DEFINE_VARIABLE ("^", 1, caret_value);
262 qp[qp > qmark_value ? -1 : 0] = '\0';
263 DEFINE_VARIABLE ("?", 1, qmark_value);
265 bp[bp > bar_value ? -1 : 0] = '\0';
266 DEFINE_VARIABLE ("|", 1, bar_value);
269 #undef DEFINE_VARIABLE
272 /* Chop CMDS up into individual command lines if necessary.
273 Also set the `lines_flags' and `any_recurse' members. */
275 void
276 chop_commands (struct commands *cmds)
278 const char *p;
279 unsigned int nlines, idx;
280 char **lines;
282 /* If we don't have any commands,
283 or we already parsed them, never mind. */
285 if (!cmds || cmds->command_lines != 0)
286 return;
288 /* Chop CMDS->commands up into lines in CMDS->command_lines.
289 Also set the corresponding CMDS->lines_flags elements,
290 and the CMDS->any_recurse flag. */
292 nlines = 5;
293 lines = xmalloc (5 * sizeof (char *));
294 idx = 0;
295 p = cmds->commands;
296 while (*p != '\0')
298 const char *end = p;
299 find_end:;
300 end = strchr (end, '\n');
301 if (end == 0)
302 end = p + strlen (p);
303 else if (end > p && end[-1] == '\\')
305 int backslash = 1;
306 const char *b;
307 for (b = end - 2; b >= p && *b == '\\'; --b)
308 backslash = !backslash;
309 if (backslash)
311 ++end;
312 goto find_end;
316 if (idx == nlines)
318 nlines += 2;
319 lines = xrealloc (lines, nlines * sizeof (char *));
321 lines[idx++] = xstrndup (p, end - p);
322 p = end;
323 if (*p != '\0')
324 ++p;
327 if (idx != nlines)
329 nlines = idx;
330 lines = xrealloc (lines, nlines * sizeof (char *));
333 cmds->ncommand_lines = nlines;
334 cmds->command_lines = lines;
336 cmds->any_recurse = 0;
337 cmds->lines_flags = xmalloc (nlines);
338 for (idx = 0; idx < nlines; ++idx)
340 int flags = 0;
342 for (p = lines[idx];
343 isblank ((unsigned char)*p) || *p == '-' || *p == '@' || *p == '+';
344 ++p)
345 switch (*p)
347 case '+':
348 flags |= COMMANDS_RECURSE;
349 break;
350 case '@':
351 flags |= COMMANDS_SILENT;
352 break;
353 case '-':
354 flags |= COMMANDS_NOERROR;
355 break;
358 /* If no explicit '+' was given, look for MAKE variable references. */
359 if (!(flags & COMMANDS_RECURSE)
360 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
361 flags |= COMMANDS_RECURSE;
363 cmds->lines_flags[idx] = flags;
364 cmds->any_recurse |= flags & COMMANDS_RECURSE;
368 /* Execute the commands to remake FILE. If they are currently executing,
369 return or have already finished executing, just return. Otherwise,
370 fork off a child process to run the first command line in the sequence. */
372 void
373 execute_file_commands (struct file *file)
375 const char *p;
377 /* Don't go through all the preparations if
378 the commands are nothing but whitespace. */
380 for (p = file->cmds->commands; *p != '\0'; ++p)
381 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
382 break;
383 if (*p == '\0')
385 /* If there are no commands, assume everything worked. */
386 set_command_state (file, cs_running);
387 file->update_status = 0;
388 notice_finished_file (file);
389 return;
392 /* First set the automatic variables according to this file. */
394 initialize_file_variables (file, 0);
396 set_file_variables (file);
398 /* Start the commands running. */
399 new_job (file);
402 /* This is set while we are inside fatal_error_signal,
403 so things can avoid nonreentrant operations. */
405 int handling_fatal_signal = 0;
407 /* Handle fatal signals. */
409 RETSIGTYPE
410 fatal_error_signal (int sig)
412 #ifdef __MSDOS__
413 extern int dos_status, dos_command_running;
415 if (dos_command_running)
417 /* That was the child who got the signal, not us. */
418 dos_status |= (sig << 8);
419 return;
421 remove_intermediates (1);
422 exit (EXIT_FAILURE);
423 #else /* not __MSDOS__ */
424 #ifdef _AMIGA
425 remove_intermediates (1);
426 if (sig == SIGINT)
427 fputs (_("*** Break.\n"), stderr);
429 exit (10);
430 #else /* not Amiga */
431 #ifdef WINDOWS32
432 extern HANDLE main_thread;
434 /* Windows creates a sperate thread for handling Ctrl+C, so we need
435 to suspend the main thread, or else we will have race conditions
436 when both threads call reap_children. */
437 if (main_thread)
439 DWORD susp_count = SuspendThread (main_thread);
441 if (susp_count != 0)
442 fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
443 else if (susp_count == (DWORD)-1)
445 DWORD ierr = GetLastError ();
447 fprintf (stderr, "SuspendThread: error %ld: %s\n",
448 ierr, map_windows32_error_to_string (ierr));
451 #endif
452 handling_fatal_signal = 1;
454 /* Set the handling for this signal to the default.
455 It is blocked now while we run this handler. */
456 signal (sig, SIG_DFL);
458 /* A termination signal won't be sent to the entire
459 process group, but it means we want to kill the children. */
461 if (sig == SIGTERM)
463 struct child *c;
464 for (c = children; c != 0; c = c->next)
465 if (!c->remote)
466 (void) kill (c->pid, SIGTERM);
469 /* If we got a signal that means the user
470 wanted to kill make, remove pending targets. */
472 if (sig == SIGTERM || sig == SIGINT
473 #ifdef SIGHUP
474 || sig == SIGHUP
475 #endif
476 #ifdef SIGQUIT
477 || sig == SIGQUIT
478 #endif
481 struct child *c;
483 /* Remote children won't automatically get signals sent
484 to the process group, so we must send them. */
485 for (c = children; c != 0; c = c->next)
486 if (c->remote)
487 (void) remote_kill (c->pid, sig);
489 for (c = children; c != 0; c = c->next)
490 delete_child_targets (c);
492 /* Clean up the children. We don't just use the call below because
493 we don't want to print the "Waiting for children" message. */
494 while (job_slots_used > 0)
495 reap_children (1, 0);
497 else
498 /* Wait for our children to die. */
499 while (job_slots_used > 0)
500 reap_children (1, 1);
502 /* Delete any non-precious intermediate files that were made. */
504 remove_intermediates (1);
506 #ifdef SIGQUIT
507 if (sig == SIGQUIT)
508 /* We don't want to send ourselves SIGQUIT, because it will
509 cause a core dump. Just exit instead. */
510 exit (EXIT_FAILURE);
511 #endif
513 #ifdef WINDOWS32
514 if (main_thread)
515 CloseHandle (main_thread);
516 /* Cannot call W32_kill with a pid (it needs a handle). The exit
517 status of 130 emulates what happens in Bash. */
518 exit (130);
519 #else
520 /* Signal the same code; this time it will really be fatal. The signal
521 will be unblocked when we return and arrive then to kill us. */
522 if (kill (getpid (), sig) < 0)
523 pfatal_with_name ("kill");
524 #endif /* not WINDOWS32 */
525 #endif /* not Amiga */
526 #endif /* not __MSDOS__ */
529 /* Delete FILE unless it's precious or not actually a file (phony),
530 and it has changed on disk since we last stat'd it. */
532 static void
533 delete_target (struct file *file, const char *on_behalf_of)
535 struct stat st;
536 int e;
538 if (file->precious || file->phony)
539 return;
541 #ifndef NO_ARCHIVES
542 if (ar_name (file->name))
544 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
545 ? (time_t) -1
546 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
547 if (ar_member_date (file->name) != file_date)
549 if (on_behalf_of)
550 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
551 on_behalf_of, file->name);
552 else
553 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
554 file->name);
556 return;
558 #endif
560 EINTRLOOP (e, stat (file->name, &st));
561 if (e == 0
562 && S_ISREG (st.st_mode)
563 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
565 if (on_behalf_of)
566 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
567 else
568 error (NILF, _("*** Deleting file `%s'"), file->name);
569 if (unlink (file->name) < 0
570 && errno != ENOENT) /* It disappeared; so what. */
571 perror_with_name ("unlink: ", file->name);
576 /* Delete all non-precious targets of CHILD unless they were already deleted.
577 Set the flag in CHILD to say they've been deleted. */
579 void
580 delete_child_targets (struct child *child)
582 struct dep *d;
584 if (child->deleted)
585 return;
587 /* Delete the target file if it changed. */
588 delete_target (child->file, NULL);
590 /* Also remove any non-precious targets listed in the `also_make' member. */
591 for (d = child->file->also_make; d != 0; d = d->next)
592 delete_target (d->file, child->file->name);
594 child->deleted = 1;
597 /* Print out the commands in CMDS. */
599 void
600 print_commands (const struct commands *cmds)
602 const char *s;
604 fputs (_("# recipe to execute"), stdout);
606 if (cmds->fileinfo.filenm == 0)
607 puts (_(" (built-in):"));
608 else
609 printf (_(" (from `%s', line %lu):\n"),
610 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
612 s = cmds->commands;
613 while (*s != '\0')
615 const char *end;
617 end = strchr (s, '\n');
618 if (end == 0)
619 end = s + strlen (s);
621 printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);
623 s = end + (end[0] == '\n');