Add some more unit tests for variable flavors.
[make/kirr.git] / commands.c
blobb202f6cded9ad610962efae1e0503ba988dc9340
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 struct dep *d;
45 char *at, *percent, *star, *less;
47 #ifndef NO_ARCHIVES
48 /* If the target is an archive member `lib(member)',
49 then $@ is `lib' and $% is `member'. */
51 if (ar_name (file->name))
53 unsigned int len;
54 char *p;
56 p = strchr (file->name, '(');
57 at = (char *) alloca (p - file->name + 1);
58 bcopy (file->name, at, p - file->name);
59 at[p - file->name] = '\0';
60 len = strlen (p + 1);
61 percent = (char *) alloca (len);
62 bcopy (p + 1, percent, len - 1);
63 percent[len - 1] = '\0';
65 else
66 #endif /* NO_ARCHIVES. */
68 at = file->name;
69 percent = "";
72 /* $* is the stem from an implicit or static pattern rule. */
73 if (file->stem == 0)
75 /* In Unix make, $* is set to the target name with
76 any suffix in the .SUFFIXES list stripped off for
77 explicit rules. We store this in the `stem' member. */
78 register struct dep *d;
79 char *name;
80 unsigned int len;
82 #ifndef NO_ARCHIVES
83 if (ar_name (file->name))
85 name = strchr (file->name, '(') + 1;
86 len = strlen (name) - 1;
88 else
89 #endif
91 name = file->name;
92 len = strlen (name);
95 for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
97 unsigned int slen = strlen (dep_name (d));
98 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
100 file->stem = savestring (name, len - slen);
101 break;
104 if (d == 0)
105 file->stem = "";
107 star = file->stem;
109 /* $< is the first not order-only dependency. */
110 less = "";
111 for (d = file->deps; d != 0; d = d->next)
112 if (!d->ignore_mtime)
114 less = dep_name (d);
115 break;
118 if (file->cmds == default_file->cmds)
119 /* This file got its commands from .DEFAULT.
120 In this case $< is the same as $@. */
121 less = at;
123 #define DEFINE_VARIABLE(name, len, value) \
124 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
126 /* Define the variables. */
128 DEFINE_VARIABLE ("<", 1, less);
129 DEFINE_VARIABLE ("*", 1, star);
130 DEFINE_VARIABLE ("@", 1, at);
131 DEFINE_VARIABLE ("%", 1, percent);
133 /* Compute the values for $^, $+, $?, and $|. */
136 static char *plus_value=0, *bar_value=0, *qmark_value=0;
137 static unsigned int qmark_max=0, plus_max=0, bar_max=0;
139 unsigned int qmark_len, plus_len, bar_len;
140 char *cp;
141 char *caret_value;
142 char *qp;
143 char *bp;
144 unsigned int len;
146 /* Compute first the value for $+, which is supposed to contain
147 duplicate dependencies as they were listed in the makefile. */
149 plus_len = 0;
150 for (d = file->deps; d != 0; d = d->next)
151 if (! d->ignore_mtime)
152 plus_len += strlen (dep_name (d)) + 1;
153 if (plus_len == 0)
154 plus_len++;
156 if (plus_len > plus_max)
157 plus_value = (char *) xmalloc (plus_max = plus_len);
158 cp = plus_value;
160 qmark_len = plus_len + 1; /* Will be this or less. */
161 for (d = file->deps; d != 0; d = d->next)
162 if (! d->ignore_mtime)
164 char *c = dep_name (d);
166 #ifndef NO_ARCHIVES
167 if (ar_name (c))
169 c = strchr (c, '(') + 1;
170 len = strlen (c) - 1;
172 else
173 #endif
174 len = strlen (c);
176 bcopy (c, cp, len);
177 cp += len;
178 *cp++ = FILE_LIST_SEPARATOR;
179 if (! d->changed)
180 qmark_len -= len + 1; /* Don't space in $? for this one. */
183 /* Kill the last space and define the variable. */
185 cp[cp > plus_value ? -1 : 0] = '\0';
186 DEFINE_VARIABLE ("+", 1, plus_value);
188 /* Make sure that no dependencies are repeated. This does not
189 really matter for the purpose of updating targets, but it
190 might make some names be listed twice for $^ and $?. */
192 uniquize_deps (file->deps);
194 bar_len = 0;
195 for (d = file->deps; d != 0; d = d->next)
196 if (d->ignore_mtime)
197 bar_len += strlen (dep_name (d)) + 1;
198 if (bar_len == 0)
199 bar_len++;
201 /* Compute the values for $^, $?, and $|. */
203 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
205 if (qmark_len > qmark_max)
206 qmark_value = (char *) xmalloc (qmark_max = qmark_len);
207 qp = qmark_value;
209 if (bar_len > bar_max)
210 bar_value = (char *) xmalloc (bar_max = bar_len);
211 bp = bar_value;
213 for (d = file->deps; d != 0; d = d->next)
215 char *c = dep_name (d);
217 #ifndef NO_ARCHIVES
218 if (ar_name (c))
220 c = strchr (c, '(') + 1;
221 len = strlen (c) - 1;
223 else
224 #endif
225 len = strlen (c);
227 if (d->ignore_mtime)
229 bcopy (c, bp, len);
230 bp += len;
231 *bp++ = FILE_LIST_SEPARATOR;
233 else
235 bcopy (c, cp, len);
236 cp += len;
237 *cp++ = FILE_LIST_SEPARATOR;
238 if (d->changed)
240 bcopy (c, qp, len);
241 qp += len;
242 *qp++ = FILE_LIST_SEPARATOR;
247 /* Kill the last spaces and define the variables. */
249 cp[cp > caret_value ? -1 : 0] = '\0';
250 DEFINE_VARIABLE ("^", 1, caret_value);
252 qp[qp > qmark_value ? -1 : 0] = '\0';
253 DEFINE_VARIABLE ("?", 1, qmark_value);
255 bp[bp > bar_value ? -1 : 0] = '\0';
256 DEFINE_VARIABLE ("|", 1, bar_value);
259 #undef DEFINE_VARIABLE
262 /* Chop CMDS up into individual command lines if necessary.
263 Also set the `lines_flags' and `any_recurse' members. */
265 void
266 chop_commands (struct commands *cmds)
268 register char *p;
269 unsigned int nlines, idx;
270 char **lines;
272 /* If we don't have any commands,
273 or we already parsed them, never mind. */
275 if (!cmds || cmds->command_lines != 0)
276 return;
278 /* Chop CMDS->commands up into lines in CMDS->command_lines.
279 Also set the corresponding CMDS->lines_flags elements,
280 and the CMDS->any_recurse flag. */
282 nlines = 5;
283 lines = (char **) xmalloc (5 * sizeof (char *));
284 idx = 0;
285 p = cmds->commands;
286 while (*p != '\0')
288 char *end = p;
289 find_end:;
290 end = strchr (end, '\n');
291 if (end == 0)
292 end = p + strlen (p);
293 else if (end > p && end[-1] == '\\')
295 int backslash = 1;
296 register char *b;
297 for (b = end - 2; b >= p && *b == '\\'; --b)
298 backslash = !backslash;
299 if (backslash)
301 ++end;
302 goto find_end;
306 if (idx == nlines)
308 nlines += 2;
309 lines = (char **) xrealloc ((char *) lines,
310 nlines * sizeof (char *));
312 lines[idx++] = savestring (p, end - p);
313 p = end;
314 if (*p != '\0')
315 ++p;
318 if (idx != nlines)
320 nlines = idx;
321 lines = (char **) xrealloc ((char *) lines,
322 nlines * sizeof (char *));
325 cmds->ncommand_lines = nlines;
326 cmds->command_lines = lines;
328 cmds->any_recurse = 0;
329 cmds->lines_flags = (char *) 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;
349 if (!(flags & COMMANDS_RECURSE))
351 unsigned int len = strlen (p);
352 if (sindex (p, len, "$(MAKE)", 7) != 0
353 || sindex (p, len, "${MAKE}", 7) != 0)
354 flags |= COMMANDS_RECURSE;
357 cmds->lines_flags[idx] = flags;
358 cmds->any_recurse |= flags & COMMANDS_RECURSE;
362 /* Execute the commands to remake FILE. If they are currently executing,
363 return or have already finished executing, just return. Otherwise,
364 fork off a child process to run the first command line in the sequence. */
366 void
367 execute_file_commands (struct file *file)
369 register char *p;
371 /* Don't go through all the preparations if
372 the commands are nothing but whitespace. */
374 for (p = file->cmds->commands; *p != '\0'; ++p)
375 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
376 break;
377 if (*p == '\0')
379 /* If there are no commands, assume everything worked. */
380 set_command_state (file, cs_running);
381 file->update_status = 0;
382 notice_finished_file (file);
383 return;
386 /* First set the automatic variables according to this file. */
388 initialize_file_variables (file, 0);
390 set_file_variables (file);
392 /* Start the commands running. */
393 new_job (file);
396 /* This is set while we are inside fatal_error_signal,
397 so things can avoid nonreentrant operations. */
399 int handling_fatal_signal = 0;
401 /* Handle fatal signals. */
403 RETSIGTYPE
404 fatal_error_signal (int sig)
406 #ifdef __MSDOS__
407 extern int dos_status, dos_command_running;
409 if (dos_command_running)
411 /* That was the child who got the signal, not us. */
412 dos_status |= (sig << 8);
413 return;
415 remove_intermediates (1);
416 exit (EXIT_FAILURE);
417 #else /* not __MSDOS__ */
418 #ifdef _AMIGA
419 remove_intermediates (1);
420 if (sig == SIGINT)
421 fputs (_("*** Break.\n"), stderr);
423 exit (10);
424 #else /* not Amiga */
425 handling_fatal_signal = 1;
427 /* Set the handling for this signal to the default.
428 It is blocked now while we run this handler. */
429 signal (sig, SIG_DFL);
431 /* A termination signal won't be sent to the entire
432 process group, but it means we want to kill the children. */
434 if (sig == SIGTERM)
436 register struct child *c;
437 for (c = children; c != 0; c = c->next)
438 if (!c->remote)
439 (void) kill (c->pid, SIGTERM);
442 /* If we got a signal that means the user
443 wanted to kill make, remove pending targets. */
445 if (sig == SIGTERM || sig == SIGINT
446 #ifdef SIGHUP
447 || sig == SIGHUP
448 #endif
449 #ifdef SIGQUIT
450 || sig == SIGQUIT
451 #endif
454 register struct child *c;
456 /* Remote children won't automatically get signals sent
457 to the process group, so we must send them. */
458 for (c = children; c != 0; c = c->next)
459 if (c->remote)
460 (void) remote_kill (c->pid, sig);
462 for (c = children; c != 0; c = c->next)
463 delete_child_targets (c);
465 /* Clean up the children. We don't just use the call below because
466 we don't want to print the "Waiting for children" message. */
467 while (job_slots_used > 0)
468 reap_children (1, 0);
470 else
471 /* Wait for our children to die. */
472 while (job_slots_used > 0)
473 reap_children (1, 1);
475 /* Delete any non-precious intermediate files that were made. */
477 remove_intermediates (1);
479 #ifdef SIGQUIT
480 if (sig == SIGQUIT)
481 /* We don't want to send ourselves SIGQUIT, because it will
482 cause a core dump. Just exit instead. */
483 exit (EXIT_FAILURE);
484 #endif
486 /* Signal the same code; this time it will really be fatal. The signal
487 will be unblocked when we return and arrive then to kill us. */
488 if (kill (getpid (), sig) < 0)
489 pfatal_with_name ("kill");
490 #endif /* not Amiga */
491 #endif /* not __MSDOS__ */
494 /* Delete FILE unless it's precious or not actually a file (phony),
495 and it has changed on disk since we last stat'd it. */
497 static void
498 delete_target (struct file *file, char *on_behalf_of)
500 struct stat st;
501 int e;
503 if (file->precious || file->phony)
504 return;
506 #ifndef NO_ARCHIVES
507 if (ar_name (file->name))
509 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
510 ? (time_t) -1
511 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
512 if (ar_member_date (file->name) != file_date)
514 if (on_behalf_of)
515 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
516 on_behalf_of, file->name);
517 else
518 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
519 file->name);
521 return;
523 #endif
525 EINTRLOOP (e, stat (file->name, &st));
526 if (e == 0
527 && S_ISREG (st.st_mode)
528 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
530 if (on_behalf_of)
531 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
532 else
533 error (NILF, _("*** Deleting file `%s'"), file->name);
534 if (unlink (file->name) < 0
535 && errno != ENOENT) /* It disappeared; so what. */
536 perror_with_name ("unlink: ", file->name);
541 /* Delete all non-precious targets of CHILD unless they were already deleted.
542 Set the flag in CHILD to say they've been deleted. */
544 void
545 delete_child_targets (struct child *child)
547 struct dep *d;
549 if (child->deleted)
550 return;
552 /* Delete the target file if it changed. */
553 delete_target (child->file, (char *) 0);
555 /* Also remove any non-precious targets listed in the `also_make' member. */
556 for (d = child->file->also_make; d != 0; d = d->next)
557 delete_target (d->file, child->file->name);
559 child->deleted = 1;
562 /* Print out the commands in CMDS. */
564 void
565 print_commands (struct commands *cmds)
567 register char *s;
569 fputs (_("# commands to execute"), stdout);
571 if (cmds->fileinfo.filenm == 0)
572 puts (_(" (built-in):"));
573 else
574 printf (_(" (from `%s', line %lu):\n"),
575 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
577 s = cmds->commands;
578 while (*s != '\0')
580 char *end;
582 while (isspace ((unsigned char)*s))
583 ++s;
585 end = strchr (s, '\n');
586 if (end == 0)
587 end = s + strlen (s);
589 printf ("\t%.*s\n", (int) (end - s), s);
591 s = end;