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
)
119 if (!d
->need_2nd_expansion
)
124 if (file
->cmds
== default_file
->cmds
)
125 /* This file got its commands from .DEFAULT.
126 In this case $< is the same as $@. */
129 #define DEFINE_VARIABLE(name, len, value) \
130 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
132 /* Define the variables. */
134 DEFINE_VARIABLE ("<", 1, less
);
135 DEFINE_VARIABLE ("*", 1, star
);
136 DEFINE_VARIABLE ("@", 1, at
);
137 DEFINE_VARIABLE ("%", 1, percent
);
139 /* Compute the values for $^, $+, $?, and $|. */
142 static char *plus_value
=0, *bar_value
=0, *qmark_value
=0;
143 static unsigned int plus_max
=0, bar_max
=0, qmark_max
=0;
145 unsigned int qmark_len
, plus_len
, bar_len
;
152 /* Compute first the value for $+, which is supposed to contain
153 duplicate dependencies as they were listed in the makefile. */
156 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
157 if (! d
->ignore_mtime
&& ! d
->need_2nd_expansion
)
158 plus_len
+= strlen (dep_name (d
)) + 1;
162 if (plus_len
> plus_max
)
163 plus_value
= xrealloc (plus_value
, plus_max
= plus_len
);
166 qmark_len
= plus_len
+ 1; /* Will be this or less. */
167 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
168 if (! d
->ignore_mtime
&& ! d
->need_2nd_expansion
)
170 const char *c
= dep_name (d
);
175 c
= strchr (c
, '(') + 1;
176 len
= strlen (c
) - 1;
184 *cp
++ = FILE_LIST_SEPARATOR
;
185 if (! (d
->changed
|| always_make_flag
))
186 qmark_len
-= len
+ 1; /* Don't space in $? for this one. */
189 /* Kill the last space and define the variable. */
191 cp
[cp
> plus_value
? -1 : 0] = '\0';
192 DEFINE_VARIABLE ("+", 1, plus_value
);
194 /* Make sure that no dependencies are repeated. This does not
195 really matter for the purpose of updating targets, but it
196 might make some names be listed twice for $^ and $?. */
198 uniquize_deps (file
->deps
);
201 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
202 if (d
->ignore_mtime
&& ! d
->need_2nd_expansion
)
203 bar_len
+= strlen (dep_name (d
)) + 1;
207 /* Compute the values for $^, $?, and $|. */
209 cp
= caret_value
= plus_value
; /* Reuse the buffer; it's big enough. */
211 if (qmark_len
> qmark_max
)
212 qmark_value
= xrealloc (qmark_value
, qmark_max
= qmark_len
);
215 if (bar_len
> bar_max
)
216 bar_value
= xrealloc (bar_value
, bar_max
= bar_len
);
219 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
223 if (d
->need_2nd_expansion
)
230 c
= strchr (c
, '(') + 1;
231 len
= strlen (c
) - 1;
241 *bp
++ = FILE_LIST_SEPARATOR
;
247 *cp
++ = FILE_LIST_SEPARATOR
;
248 if (d
->changed
|| always_make_flag
)
252 *qp
++ = FILE_LIST_SEPARATOR
;
257 /* Kill the last spaces and define the variables. */
259 cp
[cp
> caret_value
? -1 : 0] = '\0';
260 DEFINE_VARIABLE ("^", 1, caret_value
);
262 qp
[qp
> qmark_value
? -1 : 0] = '\0';
263 DEFINE_VARIABLE ("?", 1, qmark_value
);
265 bp
[bp
> bar_value
? -1 : 0] = '\0';
266 DEFINE_VARIABLE ("|", 1, bar_value
);
269 #undef DEFINE_VARIABLE
272 /* Chop CMDS up into individual command lines if necessary.
273 Also set the `lines_flags' and `any_recurse' members. */
276 chop_commands (struct commands
*cmds
)
279 unsigned int nlines
, idx
;
282 /* If we don't have any commands,
283 or we already parsed them, never mind. */
285 if (!cmds
|| cmds
->command_lines
!= 0)
288 /* Chop CMDS->commands up into lines in CMDS->command_lines.
289 Also set the corresponding CMDS->lines_flags elements,
290 and the CMDS->any_recurse flag. */
293 lines
= xmalloc (5 * sizeof (char *));
300 end
= strchr (end
, '\n');
302 end
= p
+ strlen (p
);
303 else if (end
> p
&& end
[-1] == '\\')
307 for (b
= end
- 2; b
>= p
&& *b
== '\\'; --b
)
308 backslash
= !backslash
;
319 lines
= xrealloc (lines
, nlines
* sizeof (char *));
321 lines
[idx
++] = xstrndup (p
, end
- p
);
330 lines
= xrealloc (lines
, nlines
* sizeof (char *));
333 cmds
->ncommand_lines
= nlines
;
334 cmds
->command_lines
= lines
;
336 cmds
->any_recurse
= 0;
337 cmds
->lines_flags
= xmalloc (nlines
);
338 for (idx
= 0; idx
< nlines
; ++idx
)
343 isblank ((unsigned char)*p
) || *p
== '-' || *p
== '@' || *p
== '+';
348 flags
|= COMMANDS_RECURSE
;
351 flags
|= COMMANDS_SILENT
;
354 flags
|= COMMANDS_NOERROR
;
358 /* If no explicit '+' was given, look for MAKE variable references. */
359 if (!(flags
& COMMANDS_RECURSE
)
360 && (strstr (p
, "$(MAKE)") != 0 || strstr (p
, "${MAKE}") != 0))
361 flags
|= COMMANDS_RECURSE
;
363 cmds
->lines_flags
[idx
] = flags
;
364 cmds
->any_recurse
|= flags
& COMMANDS_RECURSE
;
368 /* Execute the commands to remake FILE. If they are currently executing,
369 return or have already finished executing, just return. Otherwise,
370 fork off a child process to run the first command line in the sequence. */
373 execute_file_commands (struct file
*file
)
377 /* Don't go through all the preparations if
378 the commands are nothing but whitespace. */
380 for (p
= file
->cmds
->commands
; *p
!= '\0'; ++p
)
381 if (!isspace ((unsigned char)*p
) && *p
!= '-' && *p
!= '@')
385 /* If there are no commands, assume everything worked. */
386 set_command_state (file
, cs_running
);
387 file
->update_status
= 0;
388 notice_finished_file (file
);
392 /* First set the automatic variables according to this file. */
394 initialize_file_variables (file
, 0);
396 set_file_variables (file
);
398 /* Start the commands running. */
402 /* This is set while we are inside fatal_error_signal,
403 so things can avoid nonreentrant operations. */
405 int handling_fatal_signal
= 0;
407 /* Handle fatal signals. */
410 fatal_error_signal (int sig
)
413 extern int dos_status
, dos_command_running
;
415 if (dos_command_running
)
417 /* That was the child who got the signal, not us. */
418 dos_status
|= (sig
<< 8);
421 remove_intermediates (1);
423 #else /* not __MSDOS__ */
425 remove_intermediates (1);
427 fputs (_("*** Break.\n"), stderr
);
430 #else /* not Amiga */
432 extern HANDLE main_thread
;
434 /* Windows creates a sperate thread for handling Ctrl+C, so we need
435 to suspend the main thread, or else we will have race conditions
436 when both threads call reap_children. */
439 DWORD susp_count
= SuspendThread (main_thread
);
442 fprintf (stderr
, "SuspendThread: suspend count = %ld\n", susp_count
);
443 else if (susp_count
== (DWORD
)-1)
445 DWORD ierr
= GetLastError ();
447 fprintf (stderr
, "SuspendThread: error %ld: %s\n",
448 ierr
, map_windows32_error_to_string (ierr
));
452 handling_fatal_signal
= 1;
454 /* Set the handling for this signal to the default.
455 It is blocked now while we run this handler. */
456 signal (sig
, SIG_DFL
);
458 /* A termination signal won't be sent to the entire
459 process group, but it means we want to kill the children. */
464 for (c
= children
; c
!= 0; c
= c
->next
)
466 (void) kill (c
->pid
, SIGTERM
);
469 /* If we got a signal that means the user
470 wanted to kill make, remove pending targets. */
472 if (sig
== SIGTERM
|| sig
== SIGINT
483 /* Remote children won't automatically get signals sent
484 to the process group, so we must send them. */
485 for (c
= children
; c
!= 0; c
= c
->next
)
487 (void) remote_kill (c
->pid
, sig
);
489 for (c
= children
; c
!= 0; c
= c
->next
)
490 delete_child_targets (c
);
492 /* Clean up the children. We don't just use the call below because
493 we don't want to print the "Waiting for children" message. */
494 while (job_slots_used
> 0)
495 reap_children (1, 0);
498 /* Wait for our children to die. */
499 while (job_slots_used
> 0)
500 reap_children (1, 1);
502 /* Delete any non-precious intermediate files that were made. */
504 remove_intermediates (1);
508 /* We don't want to send ourselves SIGQUIT, because it will
509 cause a core dump. Just exit instead. */
515 CloseHandle (main_thread
);
516 /* Cannot call W32_kill with a pid (it needs a handle). The exit
517 status of 130 emulates what happens in Bash. */
520 /* Signal the same code; this time it will really be fatal. The signal
521 will be unblocked when we return and arrive then to kill us. */
522 if (kill (getpid (), sig
) < 0)
523 pfatal_with_name ("kill");
524 #endif /* not WINDOWS32 */
525 #endif /* not Amiga */
526 #endif /* not __MSDOS__ */
529 /* Delete FILE unless it's precious or not actually a file (phony),
530 and it has changed on disk since we last stat'd it. */
533 delete_target (struct file
*file
, const char *on_behalf_of
)
538 if (file
->precious
|| file
->phony
)
542 if (ar_name (file
->name
))
544 time_t file_date
= (file
->last_mtime
== NONEXISTENT_MTIME
546 : (time_t) FILE_TIMESTAMP_S (file
->last_mtime
));
547 if (ar_member_date (file
->name
) != file_date
)
550 error (NILF
, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
551 on_behalf_of
, file
->name
);
553 error (NILF
, _("*** Archive member `%s' may be bogus; not deleted"),
560 EINTRLOOP (e
, stat (file
->name
, &st
));
562 && S_ISREG (st
.st_mode
)
563 && FILE_TIMESTAMP_STAT_MODTIME (file
->name
, st
) != file
->last_mtime
)
566 error (NILF
, _("*** [%s] Deleting file `%s'"), on_behalf_of
, file
->name
);
568 error (NILF
, _("*** Deleting file `%s'"), file
->name
);
569 if (unlink (file
->name
) < 0
570 && errno
!= ENOENT
) /* It disappeared; so what. */
571 perror_with_name ("unlink: ", file
->name
);
576 /* Delete all non-precious targets of CHILD unless they were already deleted.
577 Set the flag in CHILD to say they've been deleted. */
580 delete_child_targets (struct child
*child
)
587 /* Delete the target file if it changed. */
588 delete_target (child
->file
, NULL
);
590 /* Also remove any non-precious targets listed in the `also_make' member. */
591 for (d
= child
->file
->also_make
; d
!= 0; d
= d
->next
)
592 delete_target (d
->file
, child
->file
->name
);
597 /* Print out the commands in CMDS. */
600 print_commands (const struct commands
*cmds
)
604 fputs (_("# recipe to execute"), stdout
);
606 if (cmds
->fileinfo
.filenm
== 0)
607 puts (_(" (built-in):"));
609 printf (_(" (from `%s', line %lu):\n"),
610 cmds
->fileinfo
.filenm
, cmds
->fileinfo
.lineno
);
617 end
= strchr (s
, '\n');
619 end
= s
+ strlen (s
);
621 printf ("%c%.*s\n", cmd_prefix
, (int) (end
- s
), s
);
623 s
= end
+ (end
[0] == '\n');