Include <assert.h>.
[make.git] / commands.c
blobf85c9f7465a9739f6cd1d51b156d9da299f743d0
1 /* Command processing for GNU Make.
2 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "dep.h"
21 #include "commands.h"
22 #include "file.h"
23 #include "variable.h"
24 #include "job.h"
26 extern int remote_kill ();
28 #ifndef HAVE_UNISTD_H
29 extern int getpid ();
30 #endif
32 /* Set FILE's automatic variables up. */
34 static void
35 set_file_variables (file)
36 register struct file *file;
38 register char *p;
39 char *at, *percent, *star, *less;
41 #ifndef NO_ARCHIVES
42 /* If the target is an archive member `lib(member)',
43 then $@ is `lib' and $% is `member'. */
45 if (ar_name (file->name))
47 unsigned int len;
48 p = index (file->name, '(');
49 at = (char *) alloca (p - file->name + 1);
50 bcopy (file->name, at, p - file->name);
51 at[p - file->name] = '\0';
52 len = strlen (p + 1);
53 percent = (char *) alloca (len);
54 bcopy (p + 1, percent, len - 1);
55 percent[len - 1] = '\0';
57 else
58 #endif /* NO_ARCHIVES. */
60 at = file->name;
61 percent = "";
64 /* $* is the stem from an implicit or static pattern rule. */
65 if (file->stem == 0)
67 /* In Unix make, $* is set to the target name with
68 any suffix in the .SUFFIXES list stripped off for
69 explicit rules. We store this in the `stem' member. */
70 register struct dep *d;
71 char *name;
72 unsigned int len;
74 #ifndef NO_ARCHIVES
75 if (ar_name (file->name))
77 name = index (file->name, '(') + 1;
78 len = strlen (name) - 1;
80 else
81 #endif
83 name = file->name;
84 len = strlen (name);
87 for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
89 unsigned int slen = strlen (dep_name (d));
90 if (len > slen && !strncmp (dep_name (d), name + (len - slen), slen))
92 file->stem = savestring (name, len - slen);
93 break;
96 if (d == 0)
97 file->stem = "";
99 star = file->stem;
101 /* $< is the first dependency. */
102 less = file->deps != 0 ? dep_name (file->deps) : "";
104 if (file->cmds == default_file->cmds)
105 /* This file got its commands from .DEFAULT.
106 In this case $< is the same as $@. */
107 less = at;
109 #define DEFINE_VARIABLE(name, len, value) \
110 (void) define_variable_for_file (name, len, value, o_automatic, 0, file)
112 /* Define the variables. */
114 DEFINE_VARIABLE ("<", 1, less);
115 DEFINE_VARIABLE ("*", 1, star);
116 DEFINE_VARIABLE ("@", 1, at);
117 DEFINE_VARIABLE ("%", 1, percent);
119 /* Compute the values for $^, $+, and $?. */
122 register unsigned int qmark_len, plus_len;
123 char *caret_value, *plus_value;
124 register char *cp;
125 char *qmark_value;
126 register char *qp;
127 register struct dep *d;
128 unsigned int len;
130 /* Compute first the value for $+, which is supposed to contain
131 duplicate dependencies as they were listed in the makefile. */
133 plus_len = 0;
134 for (d = file->deps; d != 0; d = d->next)
135 plus_len += strlen (dep_name (d)) + 1;
137 len = plus_len == 0 ? 1 : plus_len;
138 cp = plus_value = (char *) alloca (len);
140 qmark_len = plus_len; /* Will be this or less. */
141 for (d = file->deps; d != 0; d = d->next)
143 char *c = dep_name (d);
145 #ifndef NO_ARCHIVES
146 if (ar_name (c))
148 c = index (c, '(') + 1;
149 len = strlen (c) - 1;
151 else
152 #endif
153 len = strlen (c);
155 bcopy (c, cp, len);
156 cp += len;
157 *cp++ = ' ';
159 if (! d->changed)
160 qmark_len -= len + 1; /* Don't space in $? for this one. */
163 /* Kill the last space and define the variable. */
165 cp[cp > plus_value ? -1 : 0] = '\0';
166 DEFINE_VARIABLE ("+", 1, plus_value);
168 /* Make sure that no dependencies are repeated. This does not
169 really matter for the purpose of updating targets, but it
170 might make some names be listed twice for $^ and $?. */
172 uniquize_deps (file->deps);
174 /* Compute the values for $^ and $?. */
176 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
177 len = qmark_len == 0 ? 1 : qmark_len;
178 qp = qmark_value = (char *) alloca (len);
180 for (d = file->deps; d != 0; d = d->next)
182 char *c = dep_name (d);
184 #ifndef NO_ARCHIVES
185 if (ar_name (c))
187 c = index (c, '(') + 1;
188 len = strlen (c) - 1;
190 else
191 #endif
192 len = strlen (c);
194 bcopy (c, cp, len);
195 cp += len;
196 *cp++ = ' ';
198 if (d->changed)
200 bcopy (c, qp, len);
201 qp += len;
202 *qp++ = ' ';
206 /* Kill the last spaces and define the variables. */
208 cp[cp > caret_value ? -1 : 0] = '\0';
209 DEFINE_VARIABLE ("^", 1, caret_value);
211 qp[qp > qmark_value ? -1 : 0] = '\0';
212 DEFINE_VARIABLE ("?", 1, qmark_value);
215 #undef DEFINE_VARIABLE
218 /* Chop CMDS up into individual command lines if necessary.
219 Also set the `lines_flag' and `any_recurse' members. */
221 void
222 chop_commands (cmds)
223 register struct commands *cmds;
225 if (cmds != 0 && cmds->command_lines == 0)
227 /* Chop CMDS->commands up into lines in CMDS->command_lines.
228 Also set the corresponding CMDS->lines_flags elements,
229 and the CMDS->any_recurse flag. */
230 register char *p;
231 unsigned int nlines, idx;
232 char **lines;
234 nlines = 5;
235 lines = (char **) xmalloc (5 * sizeof (char *));
236 idx = 0;
237 p = cmds->commands;
238 while (*p != '\0')
240 char *end = p;
241 find_end:;
242 end = index (end, '\n');
243 if (end == 0)
244 end = p + strlen (p);
245 else if (end > p && end[-1] == '\\')
247 int backslash = 1;
248 register char *b;
249 for (b = end - 2; b >= p && *b == '\\'; --b)
250 backslash = !backslash;
251 if (backslash)
253 ++end;
254 goto find_end;
258 if (idx == nlines)
260 nlines += 2;
261 lines = (char **) xrealloc ((char *) lines,
262 nlines * sizeof (char *));
264 lines[idx++] = savestring (p, end - p);
265 p = end;
266 if (*p != '\0')
267 ++p;
270 if (idx != nlines)
272 nlines = idx;
273 lines = (char **) xrealloc ((char *) lines,
274 nlines * sizeof (char *));
277 cmds->ncommand_lines = nlines;
278 cmds->command_lines = lines;
280 cmds->any_recurse = 0;
281 cmds->lines_flags = (char *) xmalloc (nlines);
282 for (idx = 0; idx < nlines; ++idx)
284 int flags = 0;
286 for (p = lines[idx];
287 isblank (*p) || *p == '-' || *p == '@' || *p == '+';
288 ++p)
289 switch (*p)
291 case '+':
292 flags |= COMMANDS_RECURSE;
293 break;
294 case '@':
295 flags |= COMMANDS_SILENT;
296 break;
297 case '-':
298 flags |= COMMANDS_NOERROR;
299 break;
301 if (!(flags & COMMANDS_RECURSE))
303 unsigned int len = strlen (p);
304 if (sindex (p, len, "$(MAKE)", 7) != 0
305 || sindex (p, len, "${MAKE}", 7) != 0)
306 flags |= COMMANDS_RECURSE;
309 cmds->lines_flags[idx] = flags;
310 cmds->any_recurse |= flags & COMMANDS_RECURSE;
315 /* Execute the commands to remake FILE. If they are currently executing,
316 return or have already finished executing, just return. Otherwise,
317 fork off a child process to run the first command line in the sequence. */
319 void
320 execute_file_commands (file)
321 struct file *file;
323 register char *p;
325 /* Don't go through all the preparations if
326 the commands are nothing but whitespace. */
328 for (p = file->cmds->commands; *p != '\0'; ++p)
329 if (!isspace (*p) && *p != '-' && *p != '@')
330 break;
331 if (*p == '\0')
333 /* We are all out of commands.
334 If we have gotten this far, all the previous commands
335 have run successfully, so we have winning update status. */
336 file->update_status = 0;
337 notice_finished_file (file);
338 return;
341 /* First set the automatic variables according to this file. */
343 initialize_file_variables (file);
345 set_file_variables (file);
347 /* Start the commands running. */
348 new_job (file);
351 /* This is set while we are inside fatal_error_signal,
352 so things can avoid nonreentrant operations. */
354 int handling_fatal_signal = 0;
356 /* Handle fatal signals. */
358 RETSIGTYPE
359 fatal_error_signal (sig)
360 int sig;
362 #ifdef __MSDOS__
363 remove_intermediates (1);
364 exit (1);
365 #else /* Not MSDOS. */
366 handling_fatal_signal = 1;
368 /* Set the handling for this signal to the default.
369 It is blocked now while we run this handler. */
370 signal (sig, SIG_DFL);
372 /* A termination signal won't be sent to the entire
373 process group, but it means we want to kill the children. */
375 if (sig == SIGTERM)
377 register struct child *c;
378 for (c = children; c != 0; c = c->next)
379 if (!c->remote)
380 (void) kill (c->pid, SIGTERM);
383 /* If we got a signal that means the user
384 wanted to kill make, remove pending targets. */
386 if (sig == SIGTERM || sig == SIGINT || sig == SIGHUP || sig == SIGQUIT)
388 register struct child *c;
390 /* Remote children won't automatically get signals sent
391 to the process group, so we must send them. */
392 for (c = children; c != 0; c = c->next)
393 if (c->remote)
394 (void) remote_kill (c->pid, sig);
396 for (c = children; c != 0; c = c->next)
397 delete_child_targets (c);
399 /* Clean up the children. We don't just use the call below because
400 we don't want to print the "Waiting for children" message. */
401 while (job_slots_used > 0)
402 reap_children (1, 0);
404 else
405 /* Wait for our children to die. */
406 while (job_slots_used > 0)
407 reap_children (1, 1);
409 /* Delete any non-precious intermediate files that were made. */
411 remove_intermediates (1);
413 if (sig == SIGQUIT)
414 /* We don't want to send ourselves SIGQUIT, because it will
415 cause a core dump. Just exit instead. */
416 exit (1);
418 /* Signal the same code; this time it will really be fatal. The signal
419 will be unblocked when we return and arrive then to kill us. */
420 if (kill (getpid (), sig) < 0)
421 pfatal_with_name ("kill");
422 #endif /* MSDOS. */
425 /* Delete FILE unless it's precious or not actually a file (phony),
426 and it has changed on disk since we last stat'd it. */
428 static void
429 delete_target (file, on_behalf_of)
430 struct file *file;
431 char *on_behalf_of;
433 struct stat st;
435 if (file->precious || file->phony)
436 return;
438 #ifndef NO_ARCHIVES
439 if (ar_name (file->name))
441 if (ar_member_date (file->name) != file->last_mtime)
443 if (on_behalf_of)
444 error ("*** [%s] Archive member `%s' may be bogus; not deleted",
445 on_behalf_of, file->name);
446 else
447 error ("*** Archive member `%s' may be bogus; not deleted",
448 file->name);
450 return;
452 #endif
454 if (safe_stat (file->name, &st) == 0
455 && S_ISREG (st.st_mode)
456 && (time_t) st.st_mtime != file->last_mtime)
458 if (on_behalf_of)
459 error ("*** [%s] Deleting file `%s'", on_behalf_of, file->name);
460 else
461 error ("*** Deleting file `%s'", file->name);
462 if (unlink (file->name) < 0)
463 perror_with_name ("unlink: ", file->name);
468 /* Delete all non-precious targets of CHILD unless they were already deleted.
469 Set the flag in CHILD to say they've been deleted. */
471 void
472 delete_child_targets (child)
473 struct child *child;
475 struct dep *d;
477 if (child->deleted)
478 return;
480 /* Delete the target file if it changed. */
481 delete_target (child->file, (char *) 0);
483 /* Also remove any non-precious targets listed in the `also_make' member. */
484 for (d = child->file->also_make; d != 0; d = d->next)
485 delete_target (d->file, child->file->name);
487 child->deleted = 1;
490 /* Print out the commands in CMDS. */
492 void
493 print_commands (cmds)
494 register struct commands *cmds;
496 register char *s;
498 fputs ("# commands to execute", stdout);
500 if (cmds->filename == 0)
501 puts (" (built-in):");
502 else
503 printf (" (from `%s', line %u):\n", cmds->filename, cmds->lineno);
505 s = cmds->commands;
506 while (*s != '\0')
508 char *end;
510 while (isspace (*s))
511 ++s;
513 end = index (s, '\n');
514 if (end == 0)
515 end = s + strlen (s);
517 printf ("\t%.*s\n", (int) (end - s), s);
519 s = end;