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, 2008, 2009 Free
4 Software Foundation, Inc.
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
);
44 dep_hash_1 (const void *key
)
46 const struct dep
*d
= key
;
47 return_STRING_HASH_1 (dep_name (d
));
51 dep_hash_2 (const void *key
)
53 const struct dep
*d
= key
;
54 return_STRING_HASH_2 (dep_name (d
));
58 dep_hash_cmp (const void *x
, const void *y
)
60 const struct dep
*dx
= x
;
61 const struct dep
*dy
= y
;
62 return strcmp (dep_name (dx
), dep_name (dy
));
65 /* Set FILE's automatic variables up. */
68 set_file_variables (struct file
*file
)
71 const char *at
, *percent
, *star
, *less
;
74 /* If the target is an archive member `lib(member)',
75 then $@ is `lib' and $% is `member'. */
77 if (ar_name (file
->name
))
83 cp
= strchr (file
->name
, '(');
84 p
= alloca (cp
- file
->name
+ 1);
85 memcpy (p
, file
->name
, cp
- file
->name
);
86 p
[cp
- file
->name
] = '\0';
88 len
= strlen (cp
+ 1);
90 memcpy (p
, cp
+ 1, len
- 1);
95 #endif /* NO_ARCHIVES. */
101 /* $* is the stem from an implicit or static pattern rule. */
104 /* In Unix make, $* is set to the target name with
105 any suffix in the .SUFFIXES list stripped off for
106 explicit rules. We store this in the `stem' member. */
111 if (ar_name (file
->name
))
113 name
= strchr (file
->name
, '(') + 1;
114 len
= strlen (name
) - 1;
123 for (d
= enter_file (strcache_add (".SUFFIXES"))->deps
; d
; d
= d
->next
)
125 unsigned int slen
= strlen (dep_name (d
));
126 if (len
> slen
&& strneq (dep_name (d
), name
+ (len
- slen
), slen
))
128 file
->stem
= strcache_add_len (name
, len
- slen
);
137 /* $< is the first not order-only dependency. */
139 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
140 if (!d
->ignore_mtime
)
142 if (!d
->need_2nd_expansion
)
147 if (file
->cmds
== default_file
->cmds
)
148 /* This file got its commands from .DEFAULT.
149 In this case $< is the same as $@. */
152 #define DEFINE_VARIABLE(name, len, value) \
153 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
155 /* Define the variables. */
157 DEFINE_VARIABLE ("<", 1, less
);
158 DEFINE_VARIABLE ("*", 1, star
);
159 DEFINE_VARIABLE ("@", 1, at
);
160 DEFINE_VARIABLE ("%", 1, percent
);
162 /* Compute the values for $^, $+, $?, and $|. */
165 static char *plus_value
=0, *bar_value
=0, *qmark_value
=0;
166 static unsigned int plus_max
=0, bar_max
=0, qmark_max
=0;
168 unsigned int qmark_len
, plus_len
, bar_len
;
175 struct hash_table dep_hash
;
178 /* Compute first the value for $+, which is supposed to contain
179 duplicate dependencies as they were listed in the makefile. */
183 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
185 if (!d
->need_2nd_expansion
)
188 bar_len
+= strlen (dep_name (d
)) + 1;
190 plus_len
+= strlen (dep_name (d
)) + 1;
200 if (plus_len
> plus_max
)
201 plus_value
= xrealloc (plus_value
, plus_max
= plus_len
);
205 qmark_len
= plus_len
+ 1; /* Will be this or less. */
206 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
207 if (! d
->ignore_mtime
&& ! d
->need_2nd_expansion
)
209 const char *c
= dep_name (d
);
214 c
= strchr (c
, '(') + 1;
215 len
= strlen (c
) - 1;
223 *cp
++ = FILE_LIST_SEPARATOR
;
224 if (! (d
->changed
|| always_make_flag
))
225 qmark_len
-= len
+ 1; /* Don't space in $? for this one. */
228 /* Kill the last space and define the variable. */
230 cp
[cp
> plus_value
? -1 : 0] = '\0';
231 DEFINE_VARIABLE ("+", 1, plus_value
);
233 /* Compute the values for $^, $?, and $|. */
235 cp
= caret_value
= plus_value
; /* Reuse the buffer; it's big enough. */
237 if (qmark_len
> qmark_max
)
238 qmark_value
= xrealloc (qmark_value
, qmark_max
= qmark_len
);
241 if (bar_len
> bar_max
)
242 bar_value
= xrealloc (bar_value
, bar_max
= bar_len
);
245 /* Make sure that no dependencies are repeated in $^, $?, and $|. It
246 would be natural to combine the next two loops but we can't do it
247 because of a situation where we have two dep entries, the first
248 is order-only and the second is normal (see below). */
250 hash_init (&dep_hash
, 500, dep_hash_1
, dep_hash_2
, dep_hash_cmp
);
252 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
254 if (d
->need_2nd_expansion
)
257 slot
= hash_find_slot (&dep_hash
, d
);
258 if (HASH_VACANT (*slot
))
259 hash_insert_at (&dep_hash
, d
, slot
);
262 /* Check if the two prerequisites have different ignore_mtime.
263 If so then we need to "upgrade" one that is order-only. */
265 struct dep
* hd
= (struct dep
*) *slot
;
267 if (d
->ignore_mtime
!= hd
->ignore_mtime
)
268 d
->ignore_mtime
= hd
->ignore_mtime
= 0;
272 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
276 if (d
->need_2nd_expansion
|| hash_find_item (&dep_hash
, d
) != d
)
283 c
= strchr (c
, '(') + 1;
284 len
= strlen (c
) - 1;
294 *bp
++ = FILE_LIST_SEPARATOR
;
300 *cp
++ = FILE_LIST_SEPARATOR
;
301 if (d
->changed
|| always_make_flag
)
305 *qp
++ = FILE_LIST_SEPARATOR
;
310 hash_free (&dep_hash
, 0);
312 /* Kill the last spaces and define the variables. */
314 cp
[cp
> caret_value
? -1 : 0] = '\0';
315 DEFINE_VARIABLE ("^", 1, caret_value
);
317 qp
[qp
> qmark_value
? -1 : 0] = '\0';
318 DEFINE_VARIABLE ("?", 1, qmark_value
);
320 bp
[bp
> bar_value
? -1 : 0] = '\0';
321 DEFINE_VARIABLE ("|", 1, bar_value
);
324 #undef DEFINE_VARIABLE
327 /* Chop CMDS up into individual command lines if necessary.
328 Also set the `lines_flags' and `any_recurse' members. */
331 chop_commands (struct commands
*cmds
)
334 unsigned int nlines
, idx
;
337 /* If we don't have any commands,
338 or we already parsed them, never mind. */
340 if (!cmds
|| cmds
->command_lines
!= 0)
343 /* Chop CMDS->commands up into lines in CMDS->command_lines.
344 Also set the corresponding CMDS->lines_flags elements,
345 and the CMDS->any_recurse flag. */
348 lines
= xmalloc (5 * sizeof (char *));
355 end
= strchr (end
, '\n');
357 end
= p
+ strlen (p
);
358 else if (end
> p
&& end
[-1] == '\\')
362 for (b
= end
- 2; b
>= p
&& *b
== '\\'; --b
)
363 backslash
= !backslash
;
374 lines
= xrealloc (lines
, nlines
* sizeof (char *));
376 lines
[idx
++] = xstrndup (p
, end
- p
);
385 lines
= xrealloc (lines
, nlines
* sizeof (char *));
388 cmds
->ncommand_lines
= nlines
;
389 cmds
->command_lines
= lines
;
391 cmds
->any_recurse
= 0;
392 cmds
->lines_flags
= xmalloc (nlines
);
393 for (idx
= 0; idx
< nlines
; ++idx
)
398 isblank ((unsigned char)*p
) || *p
== '-' || *p
== '@' || *p
== '+';
403 flags
|= COMMANDS_RECURSE
;
406 flags
|= COMMANDS_SILENT
;
409 flags
|= COMMANDS_NOERROR
;
413 /* If no explicit '+' was given, look for MAKE variable references. */
414 if (!(flags
& COMMANDS_RECURSE
)
415 && (strstr (p
, "$(MAKE)") != 0 || strstr (p
, "${MAKE}") != 0))
416 flags
|= COMMANDS_RECURSE
;
418 cmds
->lines_flags
[idx
] = flags
;
419 cmds
->any_recurse
|= flags
& COMMANDS_RECURSE
;
423 /* Execute the commands to remake FILE. If they are currently executing,
424 return or have already finished executing, just return. Otherwise,
425 fork off a child process to run the first command line in the sequence. */
428 execute_file_commands (struct file
*file
)
432 /* Don't go through all the preparations if
433 the commands are nothing but whitespace. */
435 for (p
= file
->cmds
->commands
; *p
!= '\0'; ++p
)
436 if (!isspace ((unsigned char)*p
) && *p
!= '-' && *p
!= '@')
440 /* If there are no commands, assume everything worked. */
441 set_command_state (file
, cs_running
);
442 file
->update_status
= 0;
443 notice_finished_file (file
);
447 /* First set the automatic variables according to this file. */
449 initialize_file_variables (file
, 0);
451 set_file_variables (file
);
453 /* Start the commands running. */
457 /* This is set while we are inside fatal_error_signal,
458 so things can avoid nonreentrant operations. */
460 int handling_fatal_signal
= 0;
462 /* Handle fatal signals. */
465 fatal_error_signal (int sig
)
468 extern int dos_status
, dos_command_running
;
470 if (dos_command_running
)
472 /* That was the child who got the signal, not us. */
473 dos_status
|= (sig
<< 8);
476 remove_intermediates (1);
478 #else /* not __MSDOS__ */
480 remove_intermediates (1);
482 fputs (_("*** Break.\n"), stderr
);
485 #else /* not Amiga */
487 extern HANDLE main_thread
;
489 /* Windows creates a sperate thread for handling Ctrl+C, so we need
490 to suspend the main thread, or else we will have race conditions
491 when both threads call reap_children. */
494 DWORD susp_count
= SuspendThread (main_thread
);
497 fprintf (stderr
, "SuspendThread: suspend count = %ld\n", susp_count
);
498 else if (susp_count
== (DWORD
)-1)
500 DWORD ierr
= GetLastError ();
502 fprintf (stderr
, "SuspendThread: error %ld: %s\n",
503 ierr
, map_windows32_error_to_string (ierr
));
507 handling_fatal_signal
= 1;
509 /* Set the handling for this signal to the default.
510 It is blocked now while we run this handler. */
511 signal (sig
, SIG_DFL
);
513 /* A termination signal won't be sent to the entire
514 process group, but it means we want to kill the children. */
519 for (c
= children
; c
!= 0; c
= c
->next
)
521 (void) kill (c
->pid
, SIGTERM
);
524 /* If we got a signal that means the user
525 wanted to kill make, remove pending targets. */
527 if (sig
== SIGTERM
|| sig
== SIGINT
538 /* Remote children won't automatically get signals sent
539 to the process group, so we must send them. */
540 for (c
= children
; c
!= 0; c
= c
->next
)
542 (void) remote_kill (c
->pid
, sig
);
544 for (c
= children
; c
!= 0; c
= c
->next
)
545 delete_child_targets (c
);
547 /* Clean up the children. We don't just use the call below because
548 we don't want to print the "Waiting for children" message. */
549 while (job_slots_used
> 0)
550 reap_children (1, 0);
553 /* Wait for our children to die. */
554 while (job_slots_used
> 0)
555 reap_children (1, 1);
557 /* Delete any non-precious intermediate files that were made. */
559 remove_intermediates (1);
563 /* We don't want to send ourselves SIGQUIT, because it will
564 cause a core dump. Just exit instead. */
570 CloseHandle (main_thread
);
571 /* Cannot call W32_kill with a pid (it needs a handle). The exit
572 status of 130 emulates what happens in Bash. */
575 /* Signal the same code; this time it will really be fatal. The signal
576 will be unblocked when we return and arrive then to kill us. */
577 if (kill (getpid (), sig
) < 0)
578 pfatal_with_name ("kill");
579 #endif /* not WINDOWS32 */
580 #endif /* not Amiga */
581 #endif /* not __MSDOS__ */
584 /* Delete FILE unless it's precious or not actually a file (phony),
585 and it has changed on disk since we last stat'd it. */
588 delete_target (struct file
*file
, const char *on_behalf_of
)
593 if (file
->precious
|| file
->phony
)
597 if (ar_name (file
->name
))
599 time_t file_date
= (file
->last_mtime
== NONEXISTENT_MTIME
601 : (time_t) FILE_TIMESTAMP_S (file
->last_mtime
));
602 if (ar_member_date (file
->name
) != file_date
)
605 error (NILF
, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
606 on_behalf_of
, file
->name
);
608 error (NILF
, _("*** Archive member `%s' may be bogus; not deleted"),
615 EINTRLOOP (e
, stat (file
->name
, &st
));
617 && S_ISREG (st
.st_mode
)
618 && FILE_TIMESTAMP_STAT_MODTIME (file
->name
, st
) != file
->last_mtime
)
621 error (NILF
, _("*** [%s] Deleting file `%s'"), on_behalf_of
, file
->name
);
623 error (NILF
, _("*** Deleting file `%s'"), file
->name
);
624 if (unlink (file
->name
) < 0
625 && errno
!= ENOENT
) /* It disappeared; so what. */
626 perror_with_name ("unlink: ", file
->name
);
631 /* Delete all non-precious targets of CHILD unless they were already deleted.
632 Set the flag in CHILD to say they've been deleted. */
635 delete_child_targets (struct child
*child
)
642 /* Delete the target file if it changed. */
643 delete_target (child
->file
, NULL
);
645 /* Also remove any non-precious targets listed in the `also_make' member. */
646 for (d
= child
->file
->also_make
; d
!= 0; d
= d
->next
)
647 delete_target (d
->file
, child
->file
->name
);
652 /* Print out the commands in CMDS. */
655 print_commands (const struct commands
*cmds
)
659 fputs (_("# recipe to execute"), stdout
);
661 if (cmds
->fileinfo
.filenm
== 0)
662 puts (_(" (built-in):"));
664 printf (_(" (from `%s', line %lu):\n"),
665 cmds
->fileinfo
.filenm
, cmds
->fileinfo
.lineno
);
672 end
= strchr (s
, '\n');
674 end
= s
+ strlen (s
);
676 printf ("%c%.*s\n", cmd_prefix
, (int) (end
- s
), s
);
678 s
= end
+ (end
[0] == '\n');