Update the make.1 man page.
[make/kirr.git] / commands.c
blobc70a009ba884a26d4d500b55477066449f7b9659
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"
26 #ifdef WINDOWS32
27 #include <windows.h>
28 #include "w32err.h"
29 #endif
31 #if VMS
32 # define FILE_LIST_SEPARATOR ','
33 #else
34 # define FILE_LIST_SEPARATOR ' '
35 #endif
37 extern int remote_kill PARAMS ((int id, int sig));
39 #ifndef HAVE_UNISTD_H
40 extern int getpid ();
41 #endif
43 /* Set FILE's automatic variables up. */
45 void
46 set_file_variables (struct file *file)
48 struct dep *d;
49 char *at, *percent, *star, *less;
51 #ifndef NO_ARCHIVES
52 /* If the target is an archive member `lib(member)',
53 then $@ is `lib' and $% is `member'. */
55 if (ar_name (file->name))
57 unsigned int len;
58 char *p;
60 p = strchr (file->name, '(');
61 at = (char *) alloca (p - file->name + 1);
62 bcopy (file->name, at, p - file->name);
63 at[p - file->name] = '\0';
64 len = strlen (p + 1);
65 percent = (char *) alloca (len);
66 bcopy (p + 1, percent, len - 1);
67 percent[len - 1] = '\0';
69 else
70 #endif /* NO_ARCHIVES. */
72 at = file->name;
73 percent = "";
76 /* $* is the stem from an implicit or static pattern rule. */
77 if (file->stem == 0)
79 /* In Unix make, $* is set to the target name with
80 any suffix in the .SUFFIXES list stripped off for
81 explicit rules. We store this in the `stem' member. */
82 register struct dep *d;
83 char *name;
84 unsigned int len;
86 #ifndef NO_ARCHIVES
87 if (ar_name (file->name))
89 name = strchr (file->name, '(') + 1;
90 len = strlen (name) - 1;
92 else
93 #endif
95 name = file->name;
96 len = strlen (name);
99 for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
101 unsigned int slen = strlen (dep_name (d));
102 if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
104 file->stem = savestring (name, len - slen);
105 break;
108 if (d == 0)
109 file->stem = "";
111 star = file->stem;
113 /* $< is the first not order-only dependency. */
114 less = "";
115 for (d = file->deps; d != 0; d = d->next)
116 if (!d->ignore_mtime)
118 less = dep_name (d);
119 break;
122 if (file->cmds == default_file->cmds)
123 /* This file got its commands from .DEFAULT.
124 In this case $< is the same as $@. */
125 less = at;
127 #define DEFINE_VARIABLE(name, len, value) \
128 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
130 /* Define the variables. */
132 DEFINE_VARIABLE ("<", 1, less);
133 DEFINE_VARIABLE ("*", 1, star);
134 DEFINE_VARIABLE ("@", 1, at);
135 DEFINE_VARIABLE ("%", 1, percent);
137 /* Compute the values for $^, $+, $?, and $|. */
140 static char *plus_value=0, *bar_value=0, *qmark_value=0;
141 static unsigned int qmark_max=0, plus_max=0, bar_max=0;
143 unsigned int qmark_len, plus_len, bar_len;
144 char *cp;
145 char *caret_value;
146 char *qp;
147 char *bp;
148 unsigned int len;
150 /* Compute first the value for $+, which is supposed to contain
151 duplicate dependencies as they were listed in the makefile. */
153 plus_len = 0;
154 for (d = file->deps; d != 0; d = d->next)
155 if (! d->ignore_mtime)
156 plus_len += strlen (dep_name (d)) + 1;
157 if (plus_len == 0)
158 plus_len++;
160 if (plus_len > plus_max)
161 plus_value = (char *) xmalloc (plus_max = plus_len);
162 cp = plus_value;
164 qmark_len = plus_len + 1; /* Will be this or less. */
165 for (d = file->deps; d != 0; d = d->next)
166 if (! d->ignore_mtime)
168 char *c = dep_name (d);
170 #ifndef NO_ARCHIVES
171 if (ar_name (c))
173 c = strchr (c, '(') + 1;
174 len = strlen (c) - 1;
176 else
177 #endif
178 len = strlen (c);
180 bcopy (c, cp, len);
181 cp += len;
182 *cp++ = FILE_LIST_SEPARATOR;
183 if (! d->changed)
184 qmark_len -= len + 1; /* Don't space in $? for this one. */
187 /* Kill the last space and define the variable. */
189 cp[cp > plus_value ? -1 : 0] = '\0';
190 DEFINE_VARIABLE ("+", 1, plus_value);
192 /* Make sure that no dependencies are repeated. This does not
193 really matter for the purpose of updating targets, but it
194 might make some names be listed twice for $^ and $?. */
196 uniquize_deps (file->deps);
198 bar_len = 0;
199 for (d = file->deps; d != 0; d = d->next)
200 if (d->ignore_mtime)
201 bar_len += strlen (dep_name (d)) + 1;
202 if (bar_len == 0)
203 bar_len++;
205 /* Compute the values for $^, $?, and $|. */
207 cp = caret_value = plus_value; /* Reuse the buffer; it's big enough. */
209 if (qmark_len > qmark_max)
210 qmark_value = (char *) xmalloc (qmark_max = qmark_len);
211 qp = qmark_value;
213 if (bar_len > bar_max)
214 bar_value = (char *) xmalloc (bar_max = bar_len);
215 bp = bar_value;
217 for (d = file->deps; d != 0; d = d->next)
219 char *c = dep_name (d);
221 #ifndef NO_ARCHIVES
222 if (ar_name (c))
224 c = strchr (c, '(') + 1;
225 len = strlen (c) - 1;
227 else
228 #endif
229 len = strlen (c);
231 if (d->ignore_mtime)
233 bcopy (c, bp, len);
234 bp += len;
235 *bp++ = FILE_LIST_SEPARATOR;
237 else
239 bcopy (c, cp, len);
240 cp += len;
241 *cp++ = FILE_LIST_SEPARATOR;
242 if (d->changed)
244 bcopy (c, qp, len);
245 qp += len;
246 *qp++ = FILE_LIST_SEPARATOR;
251 /* Kill the last spaces and define the variables. */
253 cp[cp > caret_value ? -1 : 0] = '\0';
254 DEFINE_VARIABLE ("^", 1, caret_value);
256 qp[qp > qmark_value ? -1 : 0] = '\0';
257 DEFINE_VARIABLE ("?", 1, qmark_value);
259 bp[bp > bar_value ? -1 : 0] = '\0';
260 DEFINE_VARIABLE ("|", 1, bar_value);
263 #undef DEFINE_VARIABLE
266 /* Chop CMDS up into individual command lines if necessary.
267 Also set the `lines_flags' and `any_recurse' members. */
269 void
270 chop_commands (struct commands *cmds)
272 register char *p;
273 unsigned int nlines, idx;
274 char **lines;
276 /* If we don't have any commands,
277 or we already parsed them, never mind. */
279 if (!cmds || cmds->command_lines != 0)
280 return;
282 /* Chop CMDS->commands up into lines in CMDS->command_lines.
283 Also set the corresponding CMDS->lines_flags elements,
284 and the CMDS->any_recurse flag. */
286 nlines = 5;
287 lines = (char **) xmalloc (5 * sizeof (char *));
288 idx = 0;
289 p = cmds->commands;
290 while (*p != '\0')
292 char *end = p;
293 find_end:;
294 end = strchr (end, '\n');
295 if (end == 0)
296 end = p + strlen (p);
297 else if (end > p && end[-1] == '\\')
299 int backslash = 1;
300 register char *b;
301 for (b = end - 2; b >= p && *b == '\\'; --b)
302 backslash = !backslash;
303 if (backslash)
305 ++end;
306 goto find_end;
310 if (idx == nlines)
312 nlines += 2;
313 lines = (char **) xrealloc ((char *) lines,
314 nlines * sizeof (char *));
316 lines[idx++] = savestring (p, end - p);
317 p = end;
318 if (*p != '\0')
319 ++p;
322 if (idx != nlines)
324 nlines = idx;
325 lines = (char **) xrealloc ((char *) lines,
326 nlines * sizeof (char *));
329 cmds->ncommand_lines = nlines;
330 cmds->command_lines = lines;
332 cmds->any_recurse = 0;
333 cmds->lines_flags = (char *) xmalloc (nlines);
334 for (idx = 0; idx < nlines; ++idx)
336 int flags = 0;
338 for (p = lines[idx];
339 isblank ((unsigned char)*p) || *p == '-' || *p == '@' || *p == '+';
340 ++p)
341 switch (*p)
343 case '+':
344 flags |= COMMANDS_RECURSE;
345 break;
346 case '@':
347 flags |= COMMANDS_SILENT;
348 break;
349 case '-':
350 flags |= COMMANDS_NOERROR;
351 break;
354 /* If no explicit '+' was given, look for MAKE variable references. */
355 if (!(flags & COMMANDS_RECURSE)
356 && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
357 flags |= COMMANDS_RECURSE;
359 cmds->lines_flags[idx] = flags;
360 cmds->any_recurse |= flags & COMMANDS_RECURSE;
364 /* Execute the commands to remake FILE. If they are currently executing,
365 return or have already finished executing, just return. Otherwise,
366 fork off a child process to run the first command line in the sequence. */
368 void
369 execute_file_commands (struct file *file)
371 register char *p;
373 /* Don't go through all the preparations if
374 the commands are nothing but whitespace. */
376 for (p = file->cmds->commands; *p != '\0'; ++p)
377 if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
378 break;
379 if (*p == '\0')
381 /* If there are no commands, assume everything worked. */
382 set_command_state (file, cs_running);
383 file->update_status = 0;
384 notice_finished_file (file);
385 return;
388 /* First set the automatic variables according to this file. */
390 initialize_file_variables (file, 0);
392 set_file_variables (file);
394 /* Start the commands running. */
395 new_job (file);
398 /* This is set while we are inside fatal_error_signal,
399 so things can avoid nonreentrant operations. */
401 int handling_fatal_signal = 0;
403 /* Handle fatal signals. */
405 RETSIGTYPE
406 fatal_error_signal (int sig)
408 #ifdef __MSDOS__
409 extern int dos_status, dos_command_running;
411 if (dos_command_running)
413 /* That was the child who got the signal, not us. */
414 dos_status |= (sig << 8);
415 return;
417 remove_intermediates (1);
418 exit (EXIT_FAILURE);
419 #else /* not __MSDOS__ */
420 #ifdef _AMIGA
421 remove_intermediates (1);
422 if (sig == SIGINT)
423 fputs (_("*** Break.\n"), stderr);
425 exit (10);
426 #else /* not Amiga */
427 #ifdef WINDOWS32
428 extern HANDLE main_thread;
430 /* Windows creates a sperate thread for handling Ctrl+C, so we need
431 to suspend the main thread, or else we will have race conditions
432 when both threads call reap_children. */
433 if (main_thread)
435 DWORD susp_count = SuspendThread (main_thread);
437 if (susp_count != 0)
438 fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
439 else if (susp_count == (DWORD)-1)
441 DWORD ierr = GetLastError ();
443 fprintf (stderr, "SuspendThread: error %ld: %s\n",
444 ierr, map_windows32_error_to_string (ierr));
447 #endif
448 handling_fatal_signal = 1;
450 /* Set the handling for this signal to the default.
451 It is blocked now while we run this handler. */
452 signal (sig, SIG_DFL);
454 /* A termination signal won't be sent to the entire
455 process group, but it means we want to kill the children. */
457 if (sig == SIGTERM)
459 register struct child *c;
460 for (c = children; c != 0; c = c->next)
461 if (!c->remote)
462 (void) kill (c->pid, SIGTERM);
465 /* If we got a signal that means the user
466 wanted to kill make, remove pending targets. */
468 if (sig == SIGTERM || sig == SIGINT
469 #ifdef SIGHUP
470 || sig == SIGHUP
471 #endif
472 #ifdef SIGQUIT
473 || sig == SIGQUIT
474 #endif
477 register struct child *c;
479 /* Remote children won't automatically get signals sent
480 to the process group, so we must send them. */
481 for (c = children; c != 0; c = c->next)
482 if (c->remote)
483 (void) remote_kill (c->pid, sig);
485 for (c = children; c != 0; c = c->next)
486 delete_child_targets (c);
488 /* Clean up the children. We don't just use the call below because
489 we don't want to print the "Waiting for children" message. */
490 while (job_slots_used > 0)
491 reap_children (1, 0);
493 else
494 /* Wait for our children to die. */
495 while (job_slots_used > 0)
496 reap_children (1, 1);
498 /* Delete any non-precious intermediate files that were made. */
500 remove_intermediates (1);
502 #ifdef SIGQUIT
503 if (sig == SIGQUIT)
504 /* We don't want to send ourselves SIGQUIT, because it will
505 cause a core dump. Just exit instead. */
506 exit (EXIT_FAILURE);
507 #endif
509 #ifdef WINDOWS32
510 if (main_thread)
511 CloseHandle (main_thread);
512 /* Cannot call W32_kill with a pid (it needs a handle). The exit
513 status of 130 emulates what happens in Bash. */
514 exit (130);
515 #else
516 /* Signal the same code; this time it will really be fatal. The signal
517 will be unblocked when we return and arrive then to kill us. */
518 if (kill (getpid (), sig) < 0)
519 pfatal_with_name ("kill");
520 #endif /* not WINDOWS32 */
521 #endif /* not Amiga */
522 #endif /* not __MSDOS__ */
525 /* Delete FILE unless it's precious or not actually a file (phony),
526 and it has changed on disk since we last stat'd it. */
528 static void
529 delete_target (struct file *file, char *on_behalf_of)
531 struct stat st;
532 int e;
534 if (file->precious || file->phony)
535 return;
537 #ifndef NO_ARCHIVES
538 if (ar_name (file->name))
540 time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
541 ? (time_t) -1
542 : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
543 if (ar_member_date (file->name) != file_date)
545 if (on_behalf_of)
546 error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
547 on_behalf_of, file->name);
548 else
549 error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
550 file->name);
552 return;
554 #endif
556 EINTRLOOP (e, stat (file->name, &st));
557 if (e == 0
558 && S_ISREG (st.st_mode)
559 && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
561 if (on_behalf_of)
562 error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
563 else
564 error (NILF, _("*** Deleting file `%s'"), file->name);
565 if (unlink (file->name) < 0
566 && errno != ENOENT) /* It disappeared; so what. */
567 perror_with_name ("unlink: ", file->name);
572 /* Delete all non-precious targets of CHILD unless they were already deleted.
573 Set the flag in CHILD to say they've been deleted. */
575 void
576 delete_child_targets (struct child *child)
578 struct dep *d;
580 if (child->deleted)
581 return;
583 /* Delete the target file if it changed. */
584 delete_target (child->file, (char *) 0);
586 /* Also remove any non-precious targets listed in the `also_make' member. */
587 for (d = child->file->also_make; d != 0; d = d->next)
588 delete_target (d->file, child->file->name);
590 child->deleted = 1;
593 /* Print out the commands in CMDS. */
595 void
596 print_commands (struct commands *cmds)
598 register char *s;
600 fputs (_("# commands to execute"), stdout);
602 if (cmds->fileinfo.filenm == 0)
603 puts (_(" (built-in):"));
604 else
605 printf (_(" (from `%s', line %lu):\n"),
606 cmds->fileinfo.filenm, cmds->fileinfo.lineno);
608 s = cmds->commands;
609 while (*s != '\0')
611 char *end;
613 while (isspace ((unsigned char)*s))
614 ++s;
616 end = strchr (s, '\n');
617 if (end == 0)
618 end = s + strlen (s);
620 printf ("\t%.*s\n", (int) (end - s), s);
622 s = end;