- Many fixup patches from Savannah.
[make.git] / commands.c
blobf360bd40c5d4f693f117e7ea88d18ad0ef3576e5
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, 2008, 2009,
4 2010 Free Software 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
43 static unsigned long
44 dep_hash_1 (const void *key)
46 const struct dep *d = key;
47 return_STRING_HASH_1 (dep_name (d));
50 static unsigned long
51 dep_hash_2 (const void *key)
53 const struct dep *d = key;
54 return_STRING_HASH_2 (dep_name (d));
57 static int
58 dep_hash_cmp (const void *x, const void *y)
60 const struct dep *dx = x;
61 const struct dep *dy = y;
62 return strcmp (dep_name (dx), dep_name (dy));
65 /* Set FILE's automatic variables up. */
67 void
68 set_file_variables (struct file *file)
70 struct dep *d;
71 const char *at, *percent, *star, *less;
73 #ifndef NO_ARCHIVES
74 /* If the target is an archive member `lib(member)',
75 then $@ is `lib' and $% is `member'. */
77 if (ar_name (file->name))
79 unsigned int len;
80 const char *cp;
81 char *p;
83 cp = strchr (file->name, '(');
84 p = alloca (cp - file->name + 1);
85 memcpy (p, file->name, cp - file->name);
86 p[cp - file->name] = '\0';
87 at = p;
88 len = strlen (cp + 1);
89 p = alloca (len);
90 memcpy (p, cp + 1, len - 1);
91 p[len - 1] = '\0';
92 percent = p;
94 else
95 #endif /* NO_ARCHIVES. */
97 at = file->name;
98 percent = "";
101 /* $* is the stem from an implicit or static pattern rule. */
102 if (file->stem == 0)
104 /* In Unix make, $* is set to the target name with
105 any suffix in the .SUFFIXES list stripped off for
106 explicit rules. We store this in the `stem' member. */
107 const char *name;
108 unsigned int len;
110 #ifndef NO_ARCHIVES
111 if (ar_name (file->name))
113 name = strchr (file->name, '(') + 1;
114 len = strlen (name) - 1;
116 else
117 #endif
119 name = file->name;
120 len = strlen (name);
123 for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
125 unsigned int slen = strlen (dep_name (d));
126 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
128 file->stem = strcache_add_len (name, len - slen);
129 break;
132 if (d == 0)
133 file->stem = "";
135 star = file->stem;
137 /* $< is the first not order-only dependency. */
138 less = "";
139 for (d = file->deps; d != 0; d = d->next)
140 if (!d->ignore_mtime)
142 if (!d->need_2nd_expansion)
143 less = dep_name (d);
144 break;
147 if (file->cmds == default_file->cmds)
148 /* This file got its commands from .DEFAULT.
149 In this case $< is the same as $@. */
150 less = at;
152 #define DEFINE_VARIABLE(name, len, value) \
153 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
155 /* Define the variables. */
157 DEFINE_VARIABLE ("<", 1, less);
158 DEFINE_VARIABLE ("*", 1, star);
159 DEFINE_VARIABLE ("@", 1, at);
160 DEFINE_VARIABLE ("%", 1, percent);
162 /* Compute the values for $^, $+, $?, and $|. */
165 static char *plus_value=0, *bar_value=0, *qmark_value=0;
166 static unsigned int plus_max=0, bar_max=0, qmark_max=0;
168 unsigned int qmark_len, plus_len, bar_len;
169 char *cp;
170 char *caret_value;
171 char *qp;
172 char *bp;
173 unsigned int len;
175 struct hash_table dep_hash;
176 void **slot;
178 /* Compute first the value for $+, which is supposed to contain
179 duplicate dependencies as they were listed in the makefile. */
181 plus_len = 0;
182 bar_len = 0;
183 for (d = file->deps; d != 0; d = d->next)
185 if (!d->need_2nd_expansion)
187 if (d->ignore_mtime)
188 bar_len += strlen (dep_name (d)) + 1;
189 else
190 plus_len += strlen (dep_name (d)) + 1;
194 if (bar_len == 0)
195 bar_len++;
197 if (plus_len == 0)
198 plus_len++;
200 if (plus_len > plus_max)
201 plus_value = xrealloc (plus_value, plus_max = plus_len);
203 cp = plus_value;
205 qmark_len = plus_len + 1; /* Will be this or less. */
206 for (d = file->deps; d != 0; d = d->next)
207 if (! d->ignore_mtime && ! d->need_2nd_expansion)
209 const char *c = dep_name (d);
211 #ifndef NO_ARCHIVES
212 if (ar_name (c))
214 c = strchr (c, '(') + 1;
215 len = strlen (c) - 1;
217 else
218 #endif
219 len = strlen (c);
221 memcpy (cp, c, len);
222 cp += len;
223 *cp++ = FILE_LIST_SEPARATOR;
224 if (! (d->changed || always_make_flag))
225 qmark_len -= len + 1; /* Don't space in $? for this one. */
228 /* Kill the last space and define the variable. */
230 cp[cp > plus_value ? -1 : 0] = '\0';
231 DEFINE_VARIABLE ("+", 1, plus_value);
233 /* Compute the values for $^, $?, and $|. */
235 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
237 if (qmark_len > qmark_max)
238 qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
239 qp = qmark_value;
241 if (bar_len > bar_max)
242 bar_value = xrealloc (bar_value, bar_max = bar_len);
243 bp = bar_value;
245 /* Make sure that no dependencies are repeated in $^, $?, and $|. It
246 would be natural to combine the next two loops but we can't do it
247 because of a situation where we have two dep entries, the first
248 is order-only and the second is normal (see below). */
250 hash_init (&dep_hash, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
252 for (d = file->deps; d != 0; d = d->next)
254 if (d->need_2nd_expansion)
255 continue;
257 slot = hash_find_slot (&dep_hash, d);
258 if (HASH_VACANT (*slot))
259 hash_insert_at (&dep_hash, d, slot);
260 else
262 /* Check if the two prerequisites have different ignore_mtime.
263 If so then we need to "upgrade" one that is order-only. */
265 struct dep* hd = (struct dep*) *slot;
267 if (d->ignore_mtime != hd->ignore_mtime)
268 d->ignore_mtime = hd->ignore_mtime = 0;
272 for (d = file->deps; d != 0; d = d->next)
274 const char *c;
276 if (d->need_2nd_expansion || hash_find_item (&dep_hash, d) != d)
277 continue;
279 c = dep_name (d);
280 #ifndef NO_ARCHIVES
281 if (ar_name (c))
283 c = strchr (c, '(') + 1;
284 len = strlen (c) - 1;
286 else
287 #endif
288 len = strlen (c);
290 if (d->ignore_mtime)
292 memcpy (bp, c, len);
293 bp += len;
294 *bp++ = FILE_LIST_SEPARATOR;
296 else
298 memcpy (cp, c, len);
299 cp += len;
300 *cp++ = FILE_LIST_SEPARATOR;
301 if (d->changed || always_make_flag)
303 memcpy (qp, c, len);
304 qp += len;
305 *qp++ = FILE_LIST_SEPARATOR;
310 hash_free (&dep_hash, 0);
312 /* Kill the last spaces and define the variables. */
314 cp[cp > caret_value ? -1 : 0] = '\0';
315 DEFINE_VARIABLE ("^", 1, caret_value);
317 qp[qp > qmark_value ? -1 : 0] = '\0';
318 DEFINE_VARIABLE ("?", 1, qmark_value);
320 bp[bp > bar_value ? -1 : 0] = '\0';
321 DEFINE_VARIABLE ("|", 1, bar_value);
324 #undef DEFINE_VARIABLE
327 /* Chop CMDS up into individual command lines if necessary.
328 Also set the `lines_flags' and `any_recurse' members. */
330 void
331 chop_commands (struct commands *cmds)
333 unsigned int nlines, idx;
334 char **lines;
336 /* If we don't have any commands,
337 or we already parsed them, never mind. */
339 if (!cmds || cmds->command_lines != 0)
340 return;
342 /* Chop CMDS->commands up into lines in CMDS->command_lines. */
344 if (one_shell)
346 int l = strlen (cmds->commands);
348 nlines = 1;
349 lines = xmalloc (nlines * sizeof (char *));
350 lines[0] = xstrdup (cmds->commands);
352 /* Strip the trailing newline. */
353 if (l > 0 && lines[0][l-1] == '\n')
354 lines[0][l-1] = '\0';
356 else
358 const char *p;
360 nlines = 5;
361 lines = xmalloc (nlines * sizeof (char *));
362 idx = 0;
363 p = cmds->commands;
364 while (*p != '\0')
366 const char *end = p;
367 find_end:;
368 end = strchr (end, '\n');
369 if (end == 0)
370 end = p + strlen (p);
371 else if (end > p && end[-1] == '\\')
373 int backslash = 1;
374 const char *b;
375 for (b = end - 2; b >= p && *b == '\\'; --b)
376 backslash = !backslash;
377 if (backslash)
379 ++end;
380 goto find_end;
384 if (idx == nlines)
386 nlines += 2;
387 lines = xrealloc (lines, nlines * sizeof (char *));
389 lines[idx++] = xstrndup (p, end - p);
390 p = end;
391 if (*p != '\0')
392 ++p;
395 if (idx != nlines)
397 nlines = idx;
398 lines = xrealloc (lines, nlines * sizeof (char *));
402 /* Finally, set the corresponding CMDS->lines_flags elements and the
403 CMDS->any_recurse flag. */
405 cmds->ncommand_lines = nlines;
406 cmds->command_lines = lines;
408 cmds->any_recurse = 0;
409 cmds->lines_flags = xmalloc (nlines);
411 for (idx = 0; idx < nlines; ++idx)
413 int flags = 0;
414 const char *p = lines[idx];
416 while (isblank (*p) || *p == '-' || *p == '@' || *p == '+')
417 switch (*(p++))
419 case '+':
420 flags |= COMMANDS_RECURSE;
421 break;
422 case '@':
423 flags |= COMMANDS_SILENT;
424 break;
425 case '-':
426 flags |= COMMANDS_NOERROR;
427 break;
430 /* If no explicit '+' was given, look for MAKE variable references. */
431 if (!(flags & COMMANDS_RECURSE)
432 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
433 flags |= COMMANDS_RECURSE;
435 cmds->lines_flags[idx] = flags;
436 cmds->any_recurse |= flags & COMMANDS_RECURSE;
440 /* Execute the commands to remake FILE. If they are currently executing,
441 return or have already finished executing, just return. Otherwise,
442 fork off a child process to run the first command line in the sequence. */
444 void
445 execute_file_commands (struct file *file)
447 const char *p;
449 /* Don't go through all the preparations if
450 the commands are nothing but whitespace. */
452 for (p = file->cmds->commands; *p != '\0'; ++p)
453 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
454 break;
455 if (*p == '\0')
457 /* If there are no commands, assume everything worked. */
458 set_command_state (file, cs_running);
459 file->update_status = 0;
460 notice_finished_file (file);
461 return;
464 /* First set the automatic variables according to this file. */
466 initialize_file_variables (file, 0);
468 set_file_variables (file);
470 /* Start the commands running. */
471 new_job (file);
474 /* This is set while we are inside fatal_error_signal,
475 so things can avoid nonreentrant operations. */
477 int handling_fatal_signal = 0;
479 /* Handle fatal signals. */
481 RETSIGTYPE
482 fatal_error_signal (int sig)
484 #ifdef __MSDOS__
485 extern int dos_status, dos_command_running;
487 if (dos_command_running)
489 /* That was the child who got the signal, not us. */
490 dos_status |= (sig << 8);
491 return;
493 remove_intermediates (1);
494 exit (EXIT_FAILURE);
495 #else /* not __MSDOS__ */
496 #ifdef _AMIGA
497 remove_intermediates (1);
498 if (sig == SIGINT)
499 fputs (_("*** Break.\n"), stderr);
501 exit (10);
502 #else /* not Amiga */
503 #ifdef WINDOWS32
504 extern HANDLE main_thread;
506 /* Windows creates a sperate thread for handling Ctrl+C, so we need
507 to suspend the main thread, or else we will have race conditions
508 when both threads call reap_children. */
509 if (main_thread)
511 DWORD susp_count = SuspendThread (main_thread);
513 if (susp_count != 0)
514 fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
515 else if (susp_count == (DWORD)-1)
517 DWORD ierr = GetLastError ();
519 fprintf (stderr, "SuspendThread: error %ld: %s\n",
520 ierr, map_windows32_error_to_string (ierr));
523 #endif
524 handling_fatal_signal = 1;
526 /* Set the handling for this signal to the default.
527 It is blocked now while we run this handler. */
528 signal (sig, SIG_DFL);
530 /* A termination signal won't be sent to the entire
531 process group, but it means we want to kill the children. */
533 if (sig == SIGTERM)
535 struct child *c;
536 for (c = children; c != 0; c = c->next)
537 if (!c->remote)
538 (void) kill (c->pid, SIGTERM);
541 /* If we got a signal that means the user
542 wanted to kill make, remove pending targets. */
544 if (sig == SIGTERM || sig == SIGINT
545 #ifdef SIGHUP
546 || sig == SIGHUP
547 #endif
548 #ifdef SIGQUIT
549 || sig == SIGQUIT
550 #endif
553 struct child *c;
555 /* Remote children won't automatically get signals sent
556 to the process group, so we must send them. */
557 for (c = children; c != 0; c = c->next)
558 if (c->remote)
559 (void) remote_kill (c->pid, sig);
561 for (c = children; c != 0; c = c->next)
562 delete_child_targets (c);
564 /* Clean up the children. We don't just use the call below because
565 we don't want to print the "Waiting for children" message. */
566 while (job_slots_used > 0)
567 reap_children (1, 0);
569 else
570 /* Wait for our children to die. */
571 while (job_slots_used > 0)
572 reap_children (1, 1);
574 /* Delete any non-precious intermediate files that were made. */
576 remove_intermediates (1);
578 #ifdef SIGQUIT
579 if (sig == SIGQUIT)
580 /* We don't want to send ourselves SIGQUIT, because it will
581 cause a core dump. Just exit instead. */
582 exit (EXIT_FAILURE);
583 #endif
585 #ifdef WINDOWS32
586 if (main_thread)
587 CloseHandle (main_thread);
588 /* Cannot call W32_kill with a pid (it needs a handle). The exit
589 status of 130 emulates what happens in Bash. */
590 exit (130);
591 #else
592 /* Signal the same code; this time it will really be fatal. The signal
593 will be unblocked when we return and arrive then to kill us. */
594 if (kill (getpid (), sig) < 0)
595 pfatal_with_name ("kill");
596 #endif /* not WINDOWS32 */
597 #endif /* not Amiga */
598 #endif /* not __MSDOS__ */
601 /* Delete FILE unless it's precious or not actually a file (phony),
602 and it has changed on disk since we last stat'd it. */
604 static void
605 delete_target (struct file *file, const char *on_behalf_of)
607 struct stat st;
608 int e;
610 if (file->precious || file->phony)
611 return;
613 #ifndef NO_ARCHIVES
614 if (ar_name (file->name))
616 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
617 ? (time_t) -1
618 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
619 if (ar_member_date (file->name) != file_date)
621 if (on_behalf_of)
622 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
623 on_behalf_of, file->name);
624 else
625 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
626 file->name);
628 return;
630 #endif
632 EINTRLOOP (e, stat (file->name, &st));
633 if (e == 0
634 && S_ISREG (st.st_mode)
635 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
637 if (on_behalf_of)
638 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
639 else
640 error (NILF, _("*** Deleting file `%s'"), file->name);
641 if (unlink (file->name) < 0
642 && errno != ENOENT) /* It disappeared; so what. */
643 perror_with_name ("unlink: ", file->name);
648 /* Delete all non-precious targets of CHILD unless they were already deleted.
649 Set the flag in CHILD to say they've been deleted. */
651 void
652 delete_child_targets (struct child *child)
654 struct dep *d;
656 if (child->deleted)
657 return;
659 /* Delete the target file if it changed. */
660 delete_target (child->file, NULL);
662 /* Also remove any non-precious targets listed in the `also_make' member. */
663 for (d = child->file->also_make; d != 0; d = d->next)
664 delete_target (d->file, child->file->name);
666 child->deleted = 1;
669 /* Print out the commands in CMDS. */
671 void
672 print_commands (const struct commands *cmds)
674 const char *s;
676 fputs (_("# recipe to execute"), stdout);
678 if (cmds->fileinfo.filenm == 0)
679 puts (_(" (built-in):"));
680 else
681 printf (_(" (from `%s', line %lu):\n"),
682 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
684 s = cmds->commands;
685 while (*s != '\0')
687 const char *end;
689 end = strchr (s, '\n');
690 if (end == 0)
691 end = s + strlen (s);
693 printf ("%c%.*s\n", cmd_prefix, (int) (end - s), s);
695 s = end + (end[0] == '\n');