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 Free Software
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 2, or (at your option) any later version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 GNU Make; see the file COPYING. If not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
31 # define FILE_LIST_SEPARATOR ','
33 # define FILE_LIST_SEPARATOR ' '
36 int remote_kill (int id
, int sig
);
42 /* Set FILE's automatic variables up. */
45 set_file_variables (struct file
*file
)
48 char *at
, *percent
, *star
, *less
;
51 /* If the target is an archive member `lib(member)',
52 then $@ is `lib' and $% is `member'. */
54 if (ar_name (file
->name
))
59 p
= strchr (file
->name
, '(');
60 at
= alloca (p
- file
->name
+ 1);
61 memcpy (at
, file
->name
, p
- file
->name
);
62 at
[p
- file
->name
] = '\0';
64 percent
= alloca (len
);
65 memcpy (percent
, p
+ 1, len
- 1);
66 percent
[len
- 1] = '\0';
69 #endif /* NO_ARCHIVES. */
75 /* $* is the stem from an implicit or static pattern rule. */
78 /* In Unix make, $* is set to the target name with
79 any suffix in the .SUFFIXES list stripped off for
80 explicit rules. We store this in the `stem' member. */
85 if (ar_name (file
->name
))
87 name
= strchr (file
->name
, '(') + 1;
88 len
= strlen (name
) - 1;
97 for (d
= enter_file (".SUFFIXES")->deps
; d
!= 0; d
= d
->next
)
99 unsigned int slen
= strlen (dep_name (d
));
100 if (len
> slen
&& strneq (dep_name (d
), name
+ (len
- slen
), slen
))
102 file
->stem
= savestring (name
, len
- slen
);
111 /* $< is the first not order-only dependency. */
113 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
114 if (!d
->ignore_mtime
)
120 if (file
->cmds
== default_file
->cmds
)
121 /* This file got its commands from .DEFAULT.
122 In this case $< is the same as $@. */
125 #define DEFINE_VARIABLE(name, len, value) \
126 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
128 /* Define the variables. */
130 DEFINE_VARIABLE ("<", 1, less
);
131 DEFINE_VARIABLE ("*", 1, star
);
132 DEFINE_VARIABLE ("@", 1, at
);
133 DEFINE_VARIABLE ("%", 1, percent
);
135 /* Compute the values for $^, $+, $?, and $|. */
138 static char *plus_value
=0, *bar_value
=0, *qmark_value
=0;
139 static unsigned int qmark_max
=0, plus_max
=0, bar_max
=0;
141 unsigned int qmark_len
, plus_len
, bar_len
;
148 /* Compute first the value for $+, which is supposed to contain
149 duplicate dependencies as they were listed in the makefile. */
152 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
153 if (! d
->ignore_mtime
)
154 plus_len
+= strlen (dep_name (d
)) + 1;
158 if (plus_len
> plus_max
)
159 plus_value
= xrealloc (plus_value
, plus_max
= plus_len
);
162 qmark_len
= plus_len
+ 1; /* Will be this or less. */
163 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
164 if (! d
->ignore_mtime
)
166 char *c
= dep_name (d
);
171 c
= strchr (c
, '(') + 1;
172 len
= strlen (c
) - 1;
180 *cp
++ = FILE_LIST_SEPARATOR
;
182 qmark_len
-= len
+ 1; /* Don't space in $? for this one. */
185 /* Kill the last space and define the variable. */
187 cp
[cp
> plus_value
? -1 : 0] = '\0';
188 DEFINE_VARIABLE ("+", 1, plus_value
);
190 /* Make sure that no dependencies are repeated. This does not
191 really matter for the purpose of updating targets, but it
192 might make some names be listed twice for $^ and $?. */
194 uniquize_deps (file
->deps
);
197 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
199 bar_len
+= strlen (dep_name (d
)) + 1;
203 /* Compute the values for $^, $?, and $|. */
205 cp
= caret_value
= plus_value
; /* Reuse the buffer; it's big enough. */
207 if (qmark_len
> qmark_max
)
208 qmark_value
= xrealloc (qmark_value
, qmark_max
= qmark_len
);
211 if (bar_len
> bar_max
)
212 bar_value
= xrealloc (bar_value
, bar_max
= bar_len
);
215 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
217 char *c
= dep_name (d
);
222 c
= strchr (c
, '(') + 1;
223 len
= strlen (c
) - 1;
233 *bp
++ = FILE_LIST_SEPARATOR
;
239 *cp
++ = FILE_LIST_SEPARATOR
;
244 *qp
++ = FILE_LIST_SEPARATOR
;
249 /* Kill the last spaces and define the variables. */
251 cp
[cp
> caret_value
? -1 : 0] = '\0';
252 DEFINE_VARIABLE ("^", 1, caret_value
);
254 qp
[qp
> qmark_value
? -1 : 0] = '\0';
255 DEFINE_VARIABLE ("?", 1, qmark_value
);
257 bp
[bp
> bar_value
? -1 : 0] = '\0';
258 DEFINE_VARIABLE ("|", 1, bar_value
);
261 #undef DEFINE_VARIABLE
264 /* Chop CMDS up into individual command lines if necessary.
265 Also set the `lines_flags' and `any_recurse' members. */
268 chop_commands (struct commands
*cmds
)
271 unsigned int nlines
, idx
;
274 /* If we don't have any commands,
275 or we already parsed them, never mind. */
277 if (!cmds
|| cmds
->command_lines
!= 0)
280 /* Chop CMDS->commands up into lines in CMDS->command_lines.
281 Also set the corresponding CMDS->lines_flags elements,
282 and the CMDS->any_recurse flag. */
285 lines
= xmalloc (5 * sizeof (char *));
292 end
= strchr (end
, '\n');
294 end
= p
+ strlen (p
);
295 else if (end
> p
&& end
[-1] == '\\')
299 for (b
= end
- 2; b
>= p
&& *b
== '\\'; --b
)
300 backslash
= !backslash
;
311 lines
= xrealloc (lines
, nlines
* sizeof (char *));
313 lines
[idx
++] = savestring (p
, end
- p
);
322 lines
= xrealloc (lines
, nlines
* sizeof (char *));
325 cmds
->ncommand_lines
= nlines
;
326 cmds
->command_lines
= lines
;
328 cmds
->any_recurse
= 0;
329 cmds
->lines_flags
= xmalloc (nlines
);
330 for (idx
= 0; idx
< nlines
; ++idx
)
335 isblank ((unsigned char)*p
) || *p
== '-' || *p
== '@' || *p
== '+';
340 flags
|= COMMANDS_RECURSE
;
343 flags
|= COMMANDS_SILENT
;
346 flags
|= COMMANDS_NOERROR
;
350 /* If no explicit '+' was given, look for MAKE variable references. */
351 if (!(flags
& COMMANDS_RECURSE
)
352 && (strstr (p
, "$(MAKE)") != 0 || strstr (p
, "${MAKE}") != 0))
353 flags
|= COMMANDS_RECURSE
;
355 cmds
->lines_flags
[idx
] = flags
;
356 cmds
->any_recurse
|= flags
& COMMANDS_RECURSE
;
360 /* Execute the commands to remake FILE. If they are currently executing,
361 return or have already finished executing, just return. Otherwise,
362 fork off a child process to run the first command line in the sequence. */
365 execute_file_commands (struct file
*file
)
369 /* Don't go through all the preparations if
370 the commands are nothing but whitespace. */
372 for (p
= file
->cmds
->commands
; *p
!= '\0'; ++p
)
373 if (!isspace ((unsigned char)*p
) && *p
!= '-' && *p
!= '@')
377 /* If there are no commands, assume everything worked. */
378 set_command_state (file
, cs_running
);
379 file
->update_status
= 0;
380 notice_finished_file (file
);
384 /* First set the automatic variables according to this file. */
386 initialize_file_variables (file
, 0);
388 set_file_variables (file
);
390 /* Start the commands running. */
394 /* This is set while we are inside fatal_error_signal,
395 so things can avoid nonreentrant operations. */
397 int handling_fatal_signal
= 0;
399 /* Handle fatal signals. */
402 fatal_error_signal (int sig
)
405 extern int dos_status
, dos_command_running
;
407 if (dos_command_running
)
409 /* That was the child who got the signal, not us. */
410 dos_status
|= (sig
<< 8);
413 remove_intermediates (1);
415 #else /* not __MSDOS__ */
417 remove_intermediates (1);
419 fputs (_("*** Break.\n"), stderr
);
422 #else /* not Amiga */
424 extern HANDLE main_thread
;
426 /* Windows creates a sperate thread for handling Ctrl+C, so we need
427 to suspend the main thread, or else we will have race conditions
428 when both threads call reap_children. */
431 DWORD susp_count
= SuspendThread (main_thread
);
434 fprintf (stderr
, "SuspendThread: suspend count = %ld\n", susp_count
);
435 else if (susp_count
== (DWORD
)-1)
437 DWORD ierr
= GetLastError ();
439 fprintf (stderr
, "SuspendThread: error %ld: %s\n",
440 ierr
, map_windows32_error_to_string (ierr
));
444 handling_fatal_signal
= 1;
446 /* Set the handling for this signal to the default.
447 It is blocked now while we run this handler. */
448 signal (sig
, SIG_DFL
);
450 /* A termination signal won't be sent to the entire
451 process group, but it means we want to kill the children. */
455 register struct child
*c
;
456 for (c
= children
; c
!= 0; c
= c
->next
)
458 (void) kill (c
->pid
, SIGTERM
);
461 /* If we got a signal that means the user
462 wanted to kill make, remove pending targets. */
464 if (sig
== SIGTERM
|| sig
== SIGINT
473 register struct child
*c
;
475 /* Remote children won't automatically get signals sent
476 to the process group, so we must send them. */
477 for (c
= children
; c
!= 0; c
= c
->next
)
479 (void) remote_kill (c
->pid
, sig
);
481 for (c
= children
; c
!= 0; c
= c
->next
)
482 delete_child_targets (c
);
484 /* Clean up the children. We don't just use the call below because
485 we don't want to print the "Waiting for children" message. */
486 while (job_slots_used
> 0)
487 reap_children (1, 0);
490 /* Wait for our children to die. */
491 while (job_slots_used
> 0)
492 reap_children (1, 1);
494 /* Delete any non-precious intermediate files that were made. */
496 remove_intermediates (1);
500 /* We don't want to send ourselves SIGQUIT, because it will
501 cause a core dump. Just exit instead. */
507 CloseHandle (main_thread
);
508 /* Cannot call W32_kill with a pid (it needs a handle). The exit
509 status of 130 emulates what happens in Bash. */
512 /* Signal the same code; this time it will really be fatal. The signal
513 will be unblocked when we return and arrive then to kill us. */
514 if (kill (getpid (), sig
) < 0)
515 pfatal_with_name ("kill");
516 #endif /* not WINDOWS32 */
517 #endif /* not Amiga */
518 #endif /* not __MSDOS__ */
521 /* Delete FILE unless it's precious or not actually a file (phony),
522 and it has changed on disk since we last stat'd it. */
525 delete_target (struct file
*file
, char *on_behalf_of
)
530 if (file
->precious
|| file
->phony
)
534 if (ar_name (file
->name
))
536 time_t file_date
= (file
->last_mtime
== NONEXISTENT_MTIME
538 : (time_t) FILE_TIMESTAMP_S (file
->last_mtime
));
539 if (ar_member_date (file
->name
) != file_date
)
542 error (NILF
, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
543 on_behalf_of
, file
->name
);
545 error (NILF
, _("*** Archive member `%s' may be bogus; not deleted"),
552 EINTRLOOP (e
, stat (file
->name
, &st
));
554 && S_ISREG (st
.st_mode
)
555 && FILE_TIMESTAMP_STAT_MODTIME (file
->name
, st
) != file
->last_mtime
)
558 error (NILF
, _("*** [%s] Deleting file `%s'"), on_behalf_of
, file
->name
);
560 error (NILF
, _("*** Deleting file `%s'"), file
->name
);
561 if (unlink (file
->name
) < 0
562 && errno
!= ENOENT
) /* It disappeared; so what. */
563 perror_with_name ("unlink: ", file
->name
);
568 /* Delete all non-precious targets of CHILD unless they were already deleted.
569 Set the flag in CHILD to say they've been deleted. */
572 delete_child_targets (struct child
*child
)
579 /* Delete the target file if it changed. */
580 delete_target (child
->file
, 0);
582 /* Also remove any non-precious targets listed in the `also_make' member. */
583 for (d
= child
->file
->also_make
; d
!= 0; d
= d
->next
)
584 delete_target (d
->file
, child
->file
->name
);
589 /* Print out the commands in CMDS. */
592 print_commands (struct commands
*cmds
)
596 fputs (_("# commands to execute"), stdout
);
598 if (cmds
->fileinfo
.filenm
== 0)
599 puts (_(" (built-in):"));
601 printf (_(" (from `%s', line %lu):\n"),
602 cmds
->fileinfo
.filenm
, cmds
->fileinfo
.lineno
);
609 while (isspace ((unsigned char)*s
))
612 end
= strchr (s
, '\n');
614 end
= s
+ strlen (s
);
616 printf ("\t%.*s\n", (int) (end
- s
), s
);