1 /* Command processing for GNU Make.
2 Copyright (C) 1988, 1989, 1991, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 extern int remote_kill ();
32 /* Set FILE's automatic variables up. */
35 set_file_variables (file
)
36 register struct file
*file
;
39 char *at
, *percent
, *star
, *less
;
42 /* If the target is an archive member `lib(member)',
43 then $@ is `lib' and $% is `member'. */
45 if (ar_name (file
->name
))
48 p
= index (file
->name
, '(');
49 at
= (char *) alloca (p
- file
->name
+ 1);
50 bcopy (file
->name
, at
, p
- file
->name
);
51 at
[p
- file
->name
] = '\0';
53 percent
= (char *) alloca (len
);
54 bcopy (p
+ 1, percent
, len
- 1);
55 percent
[len
- 1] = '\0';
58 #endif /* NO_ARCHIVES. */
64 /* $* is the stem from an implicit or static pattern rule. */
67 /* In Unix make, $* is set to the target name with
68 any suffix in the .SUFFIXES list stripped off for
69 explicit rules. We store this in the `stem' member. */
70 register struct dep
*d
;
75 if (ar_name (file
->name
))
77 name
= index (file
->name
, '(') + 1;
78 len
= strlen (name
) - 1;
87 for (d
= enter_file (".SUFFIXES")->deps
; d
!= 0; d
= d
->next
)
89 unsigned int slen
= strlen (dep_name (d
));
90 if (len
> slen
&& !strncmp (dep_name (d
), name
+ len
- slen
, slen
))
92 file
->stem
= savestring (name
, len
- slen
);
101 /* $< is the first dependency. */
102 less
= file
->deps
!= 0 ? dep_name (file
->deps
) : "";
104 if (file
->cmds
== default_file
->cmds
)
105 /* This file got its commands from .DEFAULT.
106 In this case $< is the same as $@. */
109 #define DEFINE_VARIABLE(name, len, value) \
110 (void) define_variable_for_file (name, len, value, o_automatic, 0, file)
112 /* Define the variables. */
114 DEFINE_VARIABLE ("<", 1, less
);
115 DEFINE_VARIABLE ("*", 1, star
);
116 DEFINE_VARIABLE ("@", 1, at
);
117 DEFINE_VARIABLE ("%", 1, percent
);
119 /* Make sure that no dependencies are repeated. This does not
120 really matter for the purpose of updating targets, but it
121 might make some names be listed twice for $^ and $?. */
123 uniquize_deps (file
->deps
);
125 /* Compute the values for $^ and $?. */
128 register unsigned int caret_len
, qmark_len
;
133 register struct dep
*d
;
136 caret_len
= qmark_len
= 0;
137 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
139 register unsigned int i
= strlen (dep_name (d
)) + 1;
145 len
= caret_len
== 0 ? 1 : caret_len
;
146 cp
= caret_value
= (char *) alloca (len
);
147 len
= qmark_len
== 0 ? 1 : qmark_len
;
148 qp
= qmark_value
= (char *) alloca (len
);
150 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
152 char *c
= dep_name (d
);
157 c
= index (c
, '(') + 1;
158 len
= strlen (c
) - 1;
176 /* Kill the last spaces and define the variables. */
178 cp
[cp
> caret_value
? -1 : 0] = '\0';
179 DEFINE_VARIABLE ("^", 1, caret_value
);
181 qp
[qp
> qmark_value
? -1 : 0] = '\0';
182 DEFINE_VARIABLE ("?", 1, qmark_value
);
185 #undef DEFINE_VARIABLE
188 /* Chop CMDS up into individual command lines if necessary.
189 Also set the `lines_flag' and `any_recurse' members. */
193 register struct commands
*cmds
;
195 if (cmds
!= 0 && cmds
->command_lines
== 0)
197 /* Chop CMDS->commands up into lines in CMDS->command_lines.
198 Also set the corresponding CMDS->lines_flags elements,
199 and the CMDS->any_recurse flag. */
201 unsigned int nlines
, idx
;
205 lines
= (char **) xmalloc (5 * sizeof (char *));
212 end
= index (end
, '\n');
214 end
= p
+ strlen (p
);
215 else if (end
> p
&& end
[-1] == '\\')
219 for (b
= end
- 2; b
>= p
&& *b
== '\\'; --b
)
220 backslash
= !backslash
;
231 lines
= (char **) xrealloc ((char *) lines
,
232 nlines
* sizeof (char *));
234 lines
[idx
++] = savestring (p
, end
- p
);
243 lines
= (char **) xrealloc ((char *) lines
,
244 nlines
* sizeof (char *));
247 cmds
->ncommand_lines
= nlines
;
248 cmds
->command_lines
= lines
;
250 cmds
->any_recurse
= 0;
251 cmds
->lines_flags
= (char *) xmalloc (nlines
);
252 for (idx
= 0; idx
< nlines
; ++idx
)
257 isblank (*p
) || *p
== '-' || *p
== '@' || *p
== '+';
262 flags
|= COMMANDS_RECURSE
;
265 flags
|= COMMANDS_SILENT
;
268 flags
|= COMMANDS_NOERROR
;
271 if (!(flags
& COMMANDS_RECURSE
))
273 unsigned int len
= strlen (p
);
274 if (sindex (p
, len
, "$(MAKE)", 7) != 0
275 || sindex (p
, len
, "${MAKE}", 7) != 0)
276 flags
|= COMMANDS_RECURSE
;
279 cmds
->lines_flags
[idx
] = flags
;
280 cmds
->any_recurse
|= flags
& COMMANDS_RECURSE
;
285 /* Execute the commands to remake FILE. If they are currently executing,
286 return or have already finished executing, just return. Otherwise,
287 fork off a child process to run the first command line in the sequence. */
290 execute_file_commands (file
)
295 /* Don't go through all the preparations if
296 the commands are nothing but whitespace. */
298 for (p
= file
->cmds
->commands
; *p
!= '\0'; ++p
)
299 if (!isspace (*p
) && *p
!= '-' && *p
!= '@')
303 /* We are all out of commands.
304 If we have gotten this far, all the previous commands
305 have run successfully, so we have winning update status. */
306 file
->update_status
= 0;
307 notice_finished_file (file
);
311 /* First set the automatic variables according to this file. */
313 initialize_file_variables (file
);
315 set_file_variables (file
);
317 /* Start the commands running. */
321 /* This is set while we are inside fatal_error_signal,
322 so things can avoid nonreentrant operations. */
324 int handling_fatal_signal
= 0;
326 /* Handle fatal signals. */
329 fatal_error_signal (sig
)
332 handling_fatal_signal
= 1;
334 signal (sig
, SIG_DFL
);
339 (void) sigprocmask (SIG_SETMASK
, &set
, (sigset_t
*) 0);
342 #ifdef HAVE_SIGSETMASK
343 (void) sigsetmask (0);
347 /* A termination signal won't be sent to the entire
348 process group, but it means we want to kill the children. */
352 register struct child
*c
;
353 for (c
= children
; c
!= 0; c
= c
->next
)
355 (void) kill (c
->pid
, SIGTERM
);
358 /* If we got a signal that means the user
359 wanted to kill make, remove pending targets. */
361 if (sig
== SIGTERM
|| sig
== SIGINT
|| sig
== SIGHUP
|| sig
== SIGQUIT
)
363 register struct child
*c
;
365 /* Remote children won't automatically get signals sent
366 to the process group, so we must send them. */
367 for (c
= children
; c
!= 0; c
= c
->next
)
369 (void) remote_kill (c
->pid
, sig
);
371 for (c
= children
; c
!= 0; c
= c
->next
)
372 delete_child_targets (c
);
374 /* Clean up the children. We don't just use the call below because
375 we don't want to print the "Waiting for children" message. */
376 while (job_slots_used
> 0)
377 reap_children (1, 0);
380 /* Wait for our children to die. */
381 while (job_slots_used
> 0)
382 reap_children (1, 1);
384 /* Delete any non-precious intermediate files that were made. */
386 remove_intermediates (1);
389 /* We don't want to send ourselves SIGQUIT, because it will
390 cause a core dump. Just exit instead. */
393 /* Signal the same code; this time it will really be fatal. */
394 if (kill (getpid (), sig
) < 0)
395 /* It shouldn't return, but if it does, die anyway. */
396 pfatal_with_name ("kill");
399 /* Delete all non-precious targets of CHILD unless they were already deleted.
400 Set the flag in CHILD to say they've been deleted. */
403 delete_child_targets (child
)
412 /* Delete the file unless it's precious. */
413 if (!child
->file
->precious
414 && stat (child
->file
->name
, &st
) == 0
415 && S_ISREG (st
.st_mode
)
416 && (time_t) st
.st_mtime
!= child
->file
->last_mtime
)
418 error ("*** Deleting file `%s'", child
->file
->name
);
419 if (unlink (child
->file
->name
) < 0)
420 perror_with_name ("unlink: ", child
->file
->name
);
423 /* Also remove any non-precious targets listed
424 in the `also_make' member. */
425 for (d
= child
->file
->also_make
; d
!= 0; d
= d
->next
)
426 if (!d
->file
->precious
)
427 if (stat (d
->file
->name
, &st
) == 0
428 && S_ISREG (st
.st_mode
)
429 && (time_t) st
.st_mtime
!= d
->file
->last_mtime
)
431 error ("*** [%s] Deleting file `%s'", child
->file
->name
,
433 if (unlink (d
->file
->name
) < 0)
434 perror_with_name ("unlink: ", d
->file
->name
);
440 /* Print out the commands in CMDS. */
443 print_commands (cmds
)
444 register struct commands
*cmds
;
448 fputs ("# commands to execute", stdout
);
450 if (cmds
->filename
== 0)
451 puts (" (built-in):");
453 printf (" (from `%s', line %u):\n", cmds
->filename
, cmds
->lineno
);
463 end
= index (s
, '\n');
465 end
= s
+ strlen (s
);
467 printf ("\t%.*s\n", (int) (end
- s
), s
);