Fix the origin regression test.
[make.git] / commands.c
bloba1766b46add43e0b5608968c7d092511d5919112
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 static void
42 set_file_variables (struct file *file)
44 char *at, *percent, *star, *less;
46 #ifndef NO_ARCHIVES
47 /* If the target is an archive member `lib(member)',
48 then $@ is `lib' and $% is `member'. */
50 if (ar_name (file->name))
52 unsigned int len;
53 char *p;
55 p = strchr (file->name, '(');
56 at = (char *) alloca (p - file->name + 1);
57 bcopy (file->name, at, p - file->name);
58 at[p - file->name] = '\0';
59 len = strlen (p + 1);
60 percent = (char *) alloca (len);
61 bcopy (p + 1, percent, len - 1);
62 percent[len - 1] = '\0';
64 else
65 #endif /* NO_ARCHIVES. */
67 at = file->name;
68 percent = "";
71 /* $* is the stem from an implicit or static pattern rule. */
72 if (file->stem == 0)
74 /* In Unix make, $* is set to the target name with
75 any suffix in the .SUFFIXES list stripped off for
76 explicit rules. We store this in the `stem' member. */
77 register struct dep *d;
78 char *name;
79 unsigned int len;
81 #ifndef NO_ARCHIVES
82 if (ar_name (file->name))
84 name = strchr (file->name, '(') + 1;
85 len = strlen (name) - 1;
87 else
88 #endif
90 name = file->name;
91 len = strlen (name);
94 for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
96 unsigned int slen = strlen (dep_name (d));
97 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
99 file->stem = savestring (name, len - slen);
100 break;
103 if (d == 0)
104 file->stem = "";
106 star = file->stem;
108 /* $< is the first dependency. */
109 less = file->deps != 0 ? dep_name (file->deps) : "";
111 if (file->cmds == default_file->cmds)
112 /* This file got its commands from .DEFAULT.
113 In this case $< is the same as $@. */
114 less = at;
116 #define DEFINE_VARIABLE(name, len, value) \
117 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
119 /* Define the variables. */
121 DEFINE_VARIABLE ("<", 1, less);
122 DEFINE_VARIABLE ("*", 1, star);
123 DEFINE_VARIABLE ("@", 1, at);
124 DEFINE_VARIABLE ("%", 1, percent);
126 /* Compute the values for $^, $+, $?, and $|. */
129 static char *plus_value=0, *bar_value=0, *qmark_value=0;
130 static unsigned int qmark_max=0, plus_max=0, bar_max=0;
132 unsigned int qmark_len, plus_len, bar_len;
133 char *cp;
134 char *caret_value;
135 char *qp;
136 char *bp;
137 struct dep *d;
138 unsigned int len;
140 /* Compute first the value for $+, which is supposed to contain
141 duplicate dependencies as they were listed in the makefile. */
143 plus_len = 0;
144 for (d = file->deps; d != 0; d = d->next)
145 if (! d->ignore_mtime)
146 plus_len += strlen (dep_name (d)) + 1;
147 if (plus_len == 0)
148 plus_len++;
150 if (plus_len > plus_max)
151 plus_value = (char *) xmalloc (plus_max = plus_len);
152 cp = plus_value;
154 qmark_len = plus_len + 1; /* Will be this or less. */
155 for (d = file->deps; d != 0; d = d->next)
156 if (! d->ignore_mtime)
158 char *c = dep_name (d);
160 #ifndef NO_ARCHIVES
161 if (ar_name (c))
163 c = strchr (c, '(') + 1;
164 len = strlen (c) - 1;
166 else
167 #endif
168 len = strlen (c);
170 bcopy (c, cp, len);
171 cp += len;
172 *cp++ = FILE_LIST_SEPARATOR;
173 if (! d->changed)
174 qmark_len -= len + 1; /* Don't space in $? for this one. */
177 /* Kill the last space and define the variable. */
179 cp[cp > plus_value ? -1 : 0] = '\0';
180 DEFINE_VARIABLE ("+", 1, plus_value);
182 /* Make sure that no dependencies are repeated. This does not
183 really matter for the purpose of updating targets, but it
184 might make some names be listed twice for $^ and $?. */
186 uniquize_deps (file->deps);
188 bar_len = 0;
189 for (d = file->deps; d != 0; d = d->next)
190 if (d->ignore_mtime)
191 bar_len += strlen (dep_name (d)) + 1;
192 if (bar_len == 0)
193 bar_len++;
195 /* Compute the values for $^, $?, and $|. */
197 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
199 if (qmark_len > qmark_max)
200 qmark_value = (char *) xmalloc (qmark_max = qmark_len);
201 qp = qmark_value;
203 if (bar_len > bar_max)
204 bar_value = (char *) xmalloc (bar_max = bar_len);
205 bp = bar_value;
207 for (d = file->deps; d != 0; d = d->next)
209 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 if (d->ignore_mtime)
223 bcopy (c, bp, len);
224 bp += len;
225 *bp++ = FILE_LIST_SEPARATOR;
227 else
229 bcopy (c, cp, len);
230 cp += len;
231 *cp++ = FILE_LIST_SEPARATOR;
232 if (d->changed)
234 bcopy (c, qp, len);
235 qp += len;
236 *qp++ = FILE_LIST_SEPARATOR;
241 /* Kill the last spaces and define the variables. */
243 cp[cp > caret_value ? -1 : 0] = '\0';
244 DEFINE_VARIABLE ("^", 1, caret_value);
246 qp[qp > qmark_value ? -1 : 0] = '\0';
247 DEFINE_VARIABLE ("?", 1, qmark_value);
249 bp[bp > bar_value ? -1 : 0] = '\0';
250 DEFINE_VARIABLE ("|", 1, bar_value);
253 #undef DEFINE_VARIABLE
256 /* Chop CMDS up into individual command lines if necessary.
257 Also set the `lines_flags' and `any_recurse' members. */
259 void
260 chop_commands (struct commands *cmds)
262 register char *p;
263 unsigned int nlines, idx;
264 char **lines;
266 /* If we don't have any commands,
267 or we already parsed them, never mind. */
269 if (!cmds || cmds->command_lines != 0)
270 return;
272 /* Chop CMDS->commands up into lines in CMDS->command_lines.
273 Also set the corresponding CMDS->lines_flags elements,
274 and the CMDS->any_recurse flag. */
276 nlines = 5;
277 lines = (char **) xmalloc (5 * sizeof (char *));
278 idx = 0;
279 p = cmds->commands;
280 while (*p != '\0')
282 char *end = p;
283 find_end:;
284 end = strchr (end, '\n');
285 if (end == 0)
286 end = p + strlen (p);
287 else if (end > p && end[-1] == '\\')
289 int backslash = 1;
290 register char *b;
291 for (b = end - 2; b >= p && *b == '\\'; --b)
292 backslash = !backslash;
293 if (backslash)
295 ++end;
296 goto find_end;
300 if (idx == nlines)
302 nlines += 2;
303 lines = (char **) xrealloc ((char *) lines,
304 nlines * sizeof (char *));
306 lines[idx++] = savestring (p, end - p);
307 p = end;
308 if (*p != '\0')
309 ++p;
312 if (idx != nlines)
314 nlines = idx;
315 lines = (char **) xrealloc ((char *) lines,
316 nlines * sizeof (char *));
319 cmds->ncommand_lines = nlines;
320 cmds->command_lines = lines;
322 cmds->any_recurse = 0;
323 cmds->lines_flags = (char *) xmalloc (nlines);
324 for (idx = 0; idx < nlines; ++idx)
326 int flags = 0;
328 for (p = lines[idx];
329 isblank ((unsigned char)*p) || *p == '-' || *p == '@' || *p == '+';
330 ++p)
331 switch (*p)
333 case '+':
334 flags |= COMMANDS_RECURSE;
335 break;
336 case '@':
337 flags |= COMMANDS_SILENT;
338 break;
339 case '-':
340 flags |= COMMANDS_NOERROR;
341 break;
343 if (!(flags & COMMANDS_RECURSE))
345 unsigned int len = strlen (p);
346 if (sindex (p, len, "$(MAKE)", 7) != 0
347 || sindex (p, len, "${MAKE}", 7) != 0)
348 flags |= COMMANDS_RECURSE;
351 cmds->lines_flags[idx] = flags;
352 cmds->any_recurse |= flags & COMMANDS_RECURSE;
356 /* Execute the commands to remake FILE. If they are currently executing,
357 return or have already finished executing, just return. Otherwise,
358 fork off a child process to run the first command line in the sequence. */
360 void
361 execute_file_commands (struct file *file)
363 register char *p;
365 /* Don't go through all the preparations if
366 the commands are nothing but whitespace. */
368 for (p = file->cmds->commands; *p != '\0'; ++p)
369 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
370 break;
371 if (*p == '\0')
373 /* If there are no commands, assume everything worked. */
374 set_command_state (file, cs_running);
375 file->update_status = 0;
376 notice_finished_file (file);
377 return;
380 /* First set the automatic variables according to this file. */
382 initialize_file_variables (file, 0);
384 set_file_variables (file);
386 /* Start the commands running. */
387 new_job (file);
390 /* This is set while we are inside fatal_error_signal,
391 so things can avoid nonreentrant operations. */
393 int handling_fatal_signal = 0;
395 /* Handle fatal signals. */
397 RETSIGTYPE
398 fatal_error_signal (int sig)
400 #ifdef __MSDOS__
401 extern int dos_status, dos_command_running;
403 if (dos_command_running)
405 /* That was the child who got the signal, not us. */
406 dos_status |= (sig << 8);
407 return;
409 remove_intermediates (1);
410 exit (EXIT_FAILURE);
411 #else /* not __MSDOS__ */
412 #ifdef _AMIGA
413 remove_intermediates (1);
414 if (sig == SIGINT)
415 fputs (_("*** Break.\n"), stderr);
417 exit (10);
418 #else /* not Amiga */
419 handling_fatal_signal = 1;
421 /* Set the handling for this signal to the default.
422 It is blocked now while we run this handler. */
423 signal (sig, SIG_DFL);
425 /* A termination signal won't be sent to the entire
426 process group, but it means we want to kill the children. */
428 if (sig == SIGTERM)
430 register struct child *c;
431 for (c = children; c != 0; c = c->next)
432 if (!c->remote)
433 (void) kill (c->pid, SIGTERM);
436 /* If we got a signal that means the user
437 wanted to kill make, remove pending targets. */
439 if (sig == SIGTERM || sig == SIGINT
440 #ifdef SIGHUP
441 || sig == SIGHUP
442 #endif
443 #ifdef SIGQUIT
444 || sig == SIGQUIT
445 #endif
448 register struct child *c;
450 /* Remote children won't automatically get signals sent
451 to the process group, so we must send them. */
452 for (c = children; c != 0; c = c->next)
453 if (c->remote)
454 (void) remote_kill (c->pid, sig);
456 for (c = children; c != 0; c = c->next)
457 delete_child_targets (c);
459 /* Clean up the children. We don't just use the call below because
460 we don't want to print the "Waiting for children" message. */
461 while (job_slots_used > 0)
462 reap_children (1, 0);
464 else
465 /* Wait for our children to die. */
466 while (job_slots_used > 0)
467 reap_children (1, 1);
469 /* Delete any non-precious intermediate files that were made. */
471 remove_intermediates (1);
473 #ifdef SIGQUIT
474 if (sig == SIGQUIT)
475 /* We don't want to send ourselves SIGQUIT, because it will
476 cause a core dump. Just exit instead. */
477 exit (EXIT_FAILURE);
478 #endif
480 /* Signal the same code; this time it will really be fatal. The signal
481 will be unblocked when we return and arrive then to kill us. */
482 if (kill (getpid (), sig) < 0)
483 pfatal_with_name ("kill");
484 #endif /* not Amiga */
485 #endif /* not __MSDOS__ */
488 /* Delete FILE unless it's precious or not actually a file (phony),
489 and it has changed on disk since we last stat'd it. */
491 static void
492 delete_target (struct file *file, char *on_behalf_of)
494 struct stat st;
495 int e;
497 if (file->precious || file->phony)
498 return;
500 #ifndef NO_ARCHIVES
501 if (ar_name (file->name))
503 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
504 ? (time_t) -1
505 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
506 if (ar_member_date (file->name) != file_date)
508 if (on_behalf_of)
509 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
510 on_behalf_of, file->name);
511 else
512 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
513 file->name);
515 return;
517 #endif
519 EINTRLOOP (e, stat (file->name, &st));
520 if (e == 0
521 && S_ISREG (st.st_mode)
522 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
524 if (on_behalf_of)
525 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
526 else
527 error (NILF, _("*** Deleting file `%s'"), file->name);
528 if (unlink (file->name) < 0
529 && errno != ENOENT) /* It disappeared; so what. */
530 perror_with_name ("unlink: ", file->name);
535 /* Delete all non-precious targets of CHILD unless they were already deleted.
536 Set the flag in CHILD to say they've been deleted. */
538 void
539 delete_child_targets (struct child *child)
541 struct dep *d;
543 if (child->deleted)
544 return;
546 /* Delete the target file if it changed. */
547 delete_target (child->file, (char *) 0);
549 /* Also remove any non-precious targets listed in the `also_make' member. */
550 for (d = child->file->also_make; d != 0; d = d->next)
551 delete_target (d->file, child->file->name);
553 child->deleted = 1;
556 /* Print out the commands in CMDS. */
558 void
559 print_commands (struct commands *cmds)
561 register char *s;
563 fputs (_("# commands to execute"), stdout);
565 if (cmds->fileinfo.filenm == 0)
566 puts (_(" (built-in):"));
567 else
568 printf (_(" (from `%s', line %lu):\n"),
569 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
571 s = cmds->commands;
572 while (*s != '\0')
574 char *end;
576 while (isspace ((unsigned char)*s))
577 ++s;
579 end = strchr (s, '\n');
580 if (end == 0)
581 end = s + strlen (s);
583 printf ("\t%.*s\n", (int) (end - s), s);
585 s = end;