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)
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. */
32 # define FILE_LIST_SEPARATOR ','
34 # define FILE_LIST_SEPARATOR ' '
37 extern int remote_kill
PARAMS ((int id
, int sig
));
43 /* Set FILE's automatic variables up. */
46 set_file_variables (struct file
*file
)
49 char *at
, *percent
, *star
, *less
;
52 /* If the target is an archive member `lib(member)',
53 then $@ is `lib' and $% is `member'. */
55 if (ar_name (file
->name
))
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';
65 percent
= (char *) alloca (len
);
66 bcopy (p
+ 1, percent
, len
- 1);
67 percent
[len
- 1] = '\0';
70 #endif /* NO_ARCHIVES. */
76 /* $* is the stem from an implicit or static pattern rule. */
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
;
87 if (ar_name (file
->name
))
89 name
= strchr (file
->name
, '(') + 1;
90 len
= strlen (name
) - 1;
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
);
113 /* $< is the first not order-only dependency. */
115 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
116 if (!d
->ignore_mtime
)
122 if (file
->cmds
== default_file
->cmds
)
123 /* This file got its commands from .DEFAULT.
124 In this case $< is the same as $@. */
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
;
150 /* Compute first the value for $+, which is supposed to contain
151 duplicate dependencies as they were listed in the makefile. */
154 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
155 if (! d
->ignore_mtime
)
156 plus_len
+= strlen (dep_name (d
)) + 1;
160 if (plus_len
> plus_max
)
161 plus_value
= (char *) xmalloc (plus_max
= plus_len
);
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
);
173 c
= strchr (c
, '(') + 1;
174 len
= strlen (c
) - 1;
182 *cp
++ = FILE_LIST_SEPARATOR
;
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
);
199 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
201 bar_len
+= strlen (dep_name (d
)) + 1;
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
);
213 if (bar_len
> bar_max
)
214 bar_value
= (char *) xmalloc (bar_max
= bar_len
);
217 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
219 char *c
= dep_name (d
);
224 c
= strchr (c
, '(') + 1;
225 len
= strlen (c
) - 1;
235 *bp
++ = FILE_LIST_SEPARATOR
;
241 *cp
++ = FILE_LIST_SEPARATOR
;
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. */
270 chop_commands (struct commands
*cmds
)
273 unsigned int nlines
, idx
;
276 /* If we don't have any commands,
277 or we already parsed them, never mind. */
279 if (!cmds
|| cmds
->command_lines
!= 0)
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. */
287 lines
= (char **) xmalloc (5 * sizeof (char *));
294 end
= strchr (end
, '\n');
296 end
= p
+ strlen (p
);
297 else if (end
> p
&& end
[-1] == '\\')
301 for (b
= end
- 2; b
>= p
&& *b
== '\\'; --b
)
302 backslash
= !backslash
;
313 lines
= (char **) xrealloc ((char *) lines
,
314 nlines
* sizeof (char *));
316 lines
[idx
++] = savestring (p
, end
- p
);
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
)
339 isblank ((unsigned char)*p
) || *p
== '-' || *p
== '@' || *p
== '+';
344 flags
|= COMMANDS_RECURSE
;
347 flags
|= COMMANDS_SILENT
;
350 flags
|= COMMANDS_NOERROR
;
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. */
369 execute_file_commands (struct file
*file
)
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
!= '@')
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
);
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. */
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. */
406 fatal_error_signal (int sig
)
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);
417 remove_intermediates (1);
419 #else /* not __MSDOS__ */
421 remove_intermediates (1);
423 fputs (_("*** Break.\n"), stderr
);
426 #else /* not Amiga */
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. */
435 DWORD susp_count
= SuspendThread (main_thread
);
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
));
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. */
459 register struct child
*c
;
460 for (c
= children
; c
!= 0; c
= c
->next
)
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
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
)
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);
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);
504 /* We don't want to send ourselves SIGQUIT, because it will
505 cause a core dump. Just exit instead. */
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. */
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. */
529 delete_target (struct file
*file
, char *on_behalf_of
)
534 if (file
->precious
|| file
->phony
)
538 if (ar_name (file
->name
))
540 time_t file_date
= (file
->last_mtime
== NONEXISTENT_MTIME
542 : (time_t) FILE_TIMESTAMP_S (file
->last_mtime
));
543 if (ar_member_date (file
->name
) != file_date
)
546 error (NILF
, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
547 on_behalf_of
, file
->name
);
549 error (NILF
, _("*** Archive member `%s' may be bogus; not deleted"),
556 EINTRLOOP (e
, stat (file
->name
, &st
));
558 && S_ISREG (st
.st_mode
)
559 && FILE_TIMESTAMP_STAT_MODTIME (file
->name
, st
) != file
->last_mtime
)
562 error (NILF
, _("*** [%s] Deleting file `%s'"), on_behalf_of
, file
->name
);
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. */
576 delete_child_targets (struct child
*child
)
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
);
593 /* Print out the commands in CMDS. */
596 print_commands (struct commands
*cmds
)
600 fputs (_("# commands to execute"), stdout
);
602 if (cmds
->fileinfo
.filenm
== 0)
603 puts (_(" (built-in):"));
605 printf (_(" (from `%s', line %lu):\n"),
606 cmds
->fileinfo
.filenm
, cmds
->fileinfo
.lineno
);
613 while (isspace ((unsigned char)*s
))
616 end
= strchr (s
, '\n');
618 end
= s
+ strlen (s
);
620 printf ("\t%.*s\n", (int) (end
- s
), s
);