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, 2007 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 3 of the License, or (at your option) any later
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
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 const 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
))
60 cp
= strchr (file
->name
, '(');
61 p
= alloca (cp
- file
->name
+ 1);
62 memcpy (p
, file
->name
, cp
- file
->name
);
63 p
[cp
- file
->name
] = '\0';
65 len
= strlen (cp
+ 1);
67 memcpy (p
, cp
+ 1, len
- 1);
72 #endif /* NO_ARCHIVES. */
78 /* $* is the stem from an implicit or static pattern rule. */
81 /* In Unix make, $* is set to the target name with
82 any suffix in the .SUFFIXES list stripped off for
83 explicit rules. We store this in the `stem' member. */
88 if (ar_name (file
->name
))
90 name
= strchr (file
->name
, '(') + 1;
91 len
= strlen (name
) - 1;
100 for (d
= enter_file (strcache_add (".SUFFIXES"))->deps
; d
; d
= d
->next
)
102 unsigned int slen
= strlen (dep_name (d
));
103 if (len
> slen
&& strneq (dep_name (d
), name
+ (len
- slen
), slen
))
105 file
->stem
= strcache_add_len (name
, len
- slen
);
114 /* $< is the first not order-only dependency. */
116 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
117 if (!d
->ignore_mtime
)
123 if (file
->cmds
== default_file
->cmds
)
124 /* This file got its commands from .DEFAULT.
125 In this case $< is the same as $@. */
128 #define DEFINE_VARIABLE(name, len, value) \
129 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
131 /* Define the variables. */
133 DEFINE_VARIABLE ("<", 1, less
);
134 DEFINE_VARIABLE ("*", 1, star
);
135 DEFINE_VARIABLE ("@", 1, at
);
136 DEFINE_VARIABLE ("%", 1, percent
);
138 /* Compute the values for $^, $+, $?, and $|. */
141 static char *plus_value
=0, *bar_value
=0, *qmark_value
=0;
142 static unsigned int plus_max
=0, bar_max
=0, qmark_max
=0;
144 unsigned int qmark_len
, plus_len
, bar_len
;
151 /* Compute first the value for $+, which is supposed to contain
152 duplicate dependencies as they were listed in the makefile. */
155 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
156 if (! d
->ignore_mtime
)
157 plus_len
+= strlen (dep_name (d
)) + 1;
161 if (plus_len
> plus_max
)
162 plus_value
= xrealloc (plus_value
, plus_max
= plus_len
);
165 qmark_len
= plus_len
+ 1; /* Will be this or less. */
166 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
167 if (! d
->ignore_mtime
)
169 const char *c
= dep_name (d
);
174 c
= strchr (c
, '(') + 1;
175 len
= strlen (c
) - 1;
183 *cp
++ = FILE_LIST_SEPARATOR
;
185 qmark_len
-= len
+ 1; /* Don't space in $? for this one. */
188 /* Kill the last space and define the variable. */
190 cp
[cp
> plus_value
? -1 : 0] = '\0';
191 DEFINE_VARIABLE ("+", 1, plus_value
);
193 /* Make sure that no dependencies are repeated. This does not
194 really matter for the purpose of updating targets, but it
195 might make some names be listed twice for $^ and $?. */
197 uniquize_deps (file
->deps
);
200 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
202 bar_len
+= strlen (dep_name (d
)) + 1;
206 /* Compute the values for $^, $?, and $|. */
208 cp
= caret_value
= plus_value
; /* Reuse the buffer; it's big enough. */
210 if (qmark_len
> qmark_max
)
211 qmark_value
= xrealloc (qmark_value
, qmark_max
= qmark_len
);
214 if (bar_len
> bar_max
)
215 bar_value
= xrealloc (bar_value
, bar_max
= bar_len
);
218 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
220 const char *c
= dep_name (d
);
225 c
= strchr (c
, '(') + 1;
226 len
= strlen (c
) - 1;
236 *bp
++ = FILE_LIST_SEPARATOR
;
242 *cp
++ = FILE_LIST_SEPARATOR
;
247 *qp
++ = FILE_LIST_SEPARATOR
;
252 /* Kill the last spaces and define the variables. */
254 cp
[cp
> caret_value
? -1 : 0] = '\0';
255 DEFINE_VARIABLE ("^", 1, caret_value
);
257 qp
[qp
> qmark_value
? -1 : 0] = '\0';
258 DEFINE_VARIABLE ("?", 1, qmark_value
);
260 bp
[bp
> bar_value
? -1 : 0] = '\0';
261 DEFINE_VARIABLE ("|", 1, bar_value
);
264 #undef DEFINE_VARIABLE
267 /* Chop CMDS up into individual command lines if necessary.
268 Also set the `lines_flags' and `any_recurse' members. */
271 chop_commands (struct commands
*cmds
)
274 unsigned int nlines
, idx
;
277 /* If we don't have any commands,
278 or we already parsed them, never mind. */
280 if (!cmds
|| cmds
->command_lines
!= 0)
283 /* Chop CMDS->commands up into lines in CMDS->command_lines.
284 Also set the corresponding CMDS->lines_flags elements,
285 and the CMDS->any_recurse flag. */
288 lines
= xmalloc (5 * sizeof (char *));
295 end
= strchr (end
, '\n');
297 end
= p
+ strlen (p
);
298 else if (end
> p
&& end
[-1] == '\\')
302 for (b
= end
- 2; b
>= p
&& *b
== '\\'; --b
)
303 backslash
= !backslash
;
314 lines
= xrealloc (lines
, nlines
* sizeof (char *));
316 lines
[idx
++] = xstrndup (p
, end
- p
);
325 lines
= xrealloc (lines
, nlines
* sizeof (char *));
328 cmds
->ncommand_lines
= nlines
;
329 cmds
->command_lines
= lines
;
331 cmds
->any_recurse
= 0;
332 cmds
->lines_flags
= xmalloc (nlines
);
333 for (idx
= 0; idx
< nlines
; ++idx
)
338 isblank ((unsigned char)*p
) || *p
== '-' || *p
== '@' || *p
== '+';
343 flags
|= COMMANDS_RECURSE
;
346 flags
|= COMMANDS_SILENT
;
349 flags
|= COMMANDS_NOERROR
;
353 /* If no explicit '+' was given, look for MAKE variable references. */
354 if (!(flags
& COMMANDS_RECURSE
)
355 && (strstr (p
, "$(MAKE)") != 0 || strstr (p
, "${MAKE}") != 0))
356 flags
|= COMMANDS_RECURSE
;
358 cmds
->lines_flags
[idx
] = flags
;
359 cmds
->any_recurse
|= flags
& COMMANDS_RECURSE
;
363 /* Execute the commands to remake FILE. If they are currently executing,
364 return or have already finished executing, just return. Otherwise,
365 fork off a child process to run the first command line in the sequence. */
368 execute_file_commands (struct file
*file
)
372 /* Don't go through all the preparations if
373 the commands are nothing but whitespace. */
375 for (p
= file
->cmds
->commands
; *p
!= '\0'; ++p
)
376 if (!isspace ((unsigned char)*p
) && *p
!= '-' && *p
!= '@')
380 /* If there are no commands, assume everything worked. */
381 set_command_state (file
, cs_running
);
382 file
->update_status
= 0;
383 notice_finished_file (file
);
387 /* First set the automatic variables according to this file. */
389 initialize_file_variables (file
, 0);
391 set_file_variables (file
);
393 /* Start the commands running. */
397 /* This is set while we are inside fatal_error_signal,
398 so things can avoid nonreentrant operations. */
400 int handling_fatal_signal
= 0;
402 /* Handle fatal signals. */
405 fatal_error_signal (int sig
)
408 extern int dos_status
, dos_command_running
;
410 if (dos_command_running
)
412 /* That was the child who got the signal, not us. */
413 dos_status
|= (sig
<< 8);
416 remove_intermediates (1);
418 #else /* not __MSDOS__ */
420 remove_intermediates (1);
422 fputs (_("*** Break.\n"), stderr
);
425 #else /* not Amiga */
427 extern HANDLE main_thread
;
429 /* Windows creates a sperate thread for handling Ctrl+C, so we need
430 to suspend the main thread, or else we will have race conditions
431 when both threads call reap_children. */
434 DWORD susp_count
= SuspendThread (main_thread
);
437 fprintf (stderr
, "SuspendThread: suspend count = %ld\n", susp_count
);
438 else if (susp_count
== (DWORD
)-1)
440 DWORD ierr
= GetLastError ();
442 fprintf (stderr
, "SuspendThread: error %ld: %s\n",
443 ierr
, map_windows32_error_to_string (ierr
));
447 handling_fatal_signal
= 1;
449 /* Set the handling for this signal to the default.
450 It is blocked now while we run this handler. */
451 signal (sig
, SIG_DFL
);
453 /* A termination signal won't be sent to the entire
454 process group, but it means we want to kill the children. */
459 for (c
= children
; c
!= 0; c
= c
->next
)
461 (void) kill (c
->pid
, SIGTERM
);
464 /* If we got a signal that means the user
465 wanted to kill make, remove pending targets. */
467 if (sig
== SIGTERM
|| sig
== SIGINT
478 /* Remote children won't automatically get signals sent
479 to the process group, so we must send them. */
480 for (c
= children
; c
!= 0; c
= c
->next
)
482 (void) remote_kill (c
->pid
, sig
);
484 for (c
= children
; c
!= 0; c
= c
->next
)
485 delete_child_targets (c
);
487 /* Clean up the children. We don't just use the call below because
488 we don't want to print the "Waiting for children" message. */
489 while (job_slots_used
> 0)
490 reap_children (1, 0);
493 /* Wait for our children to die. */
494 while (job_slots_used
> 0)
495 reap_children (1, 1);
497 /* Delete any non-precious intermediate files that were made. */
499 remove_intermediates (1);
503 /* We don't want to send ourselves SIGQUIT, because it will
504 cause a core dump. Just exit instead. */
510 CloseHandle (main_thread
);
511 /* Cannot call W32_kill with a pid (it needs a handle). The exit
512 status of 130 emulates what happens in Bash. */
515 /* Signal the same code; this time it will really be fatal. The signal
516 will be unblocked when we return and arrive then to kill us. */
517 if (kill (getpid (), sig
) < 0)
518 pfatal_with_name ("kill");
519 #endif /* not WINDOWS32 */
520 #endif /* not Amiga */
521 #endif /* not __MSDOS__ */
524 /* Delete FILE unless it's precious or not actually a file (phony),
525 and it has changed on disk since we last stat'd it. */
528 delete_target (struct file
*file
, const char *on_behalf_of
)
533 if (file
->precious
|| file
->phony
)
537 if (ar_name (file
->name
))
539 time_t file_date
= (file
->last_mtime
== NONEXISTENT_MTIME
541 : (time_t) FILE_TIMESTAMP_S (file
->last_mtime
));
542 if (ar_member_date (file
->name
) != file_date
)
545 error (NILF
, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
546 on_behalf_of
, file
->name
);
548 error (NILF
, _("*** Archive member `%s' may be bogus; not deleted"),
555 EINTRLOOP (e
, stat (file
->name
, &st
));
557 && S_ISREG (st
.st_mode
)
558 && FILE_TIMESTAMP_STAT_MODTIME (file
->name
, st
) != file
->last_mtime
)
561 error (NILF
, _("*** [%s] Deleting file `%s'"), on_behalf_of
, file
->name
);
563 error (NILF
, _("*** Deleting file `%s'"), file
->name
);
564 if (unlink (file
->name
) < 0
565 && errno
!= ENOENT
) /* It disappeared; so what. */
566 perror_with_name ("unlink: ", file
->name
);
571 /* Delete all non-precious targets of CHILD unless they were already deleted.
572 Set the flag in CHILD to say they've been deleted. */
575 delete_child_targets (struct child
*child
)
582 /* Delete the target file if it changed. */
583 delete_target (child
->file
, NULL
);
585 /* Also remove any non-precious targets listed in the `also_make' member. */
586 for (d
= child
->file
->also_make
; d
!= 0; d
= d
->next
)
587 delete_target (d
->file
, child
->file
->name
);
592 /* Print out the commands in CMDS. */
595 print_commands (const struct commands
*cmds
)
599 fputs (_("# recipe to execute"), stdout
);
601 if (cmds
->fileinfo
.filenm
== 0)
602 puts (_(" (built-in):"));
604 printf (_(" (from `%s', line %lu):\n"),
605 cmds
->fileinfo
.filenm
, cmds
->fileinfo
.lineno
);
612 end
= strchr (s
, '\n');
614 end
= s
+ strlen (s
);
616 printf ("%c%.*s\n", cmd_prefix
, (int) (end
- s
), s
);
618 s
= end
+ (end
[0] == '\n');