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. */
28 # define FILE_LIST_SEPARATOR ','
30 # define FILE_LIST_SEPARATOR ' '
33 extern int remote_kill
PARAMS ((int id
, int sig
));
39 /* Set FILE's automatic variables up. */
42 set_file_variables (file
)
43 register struct file
*file
;
45 char *at
, *percent
, *star
, *less
;
48 /* If the target is an archive member `lib(member)',
49 then $@ is `lib' and $% is `member'. */
51 if (ar_name (file
->name
))
56 p
= strchr (file
->name
, '(');
57 at
= (char *) alloca (p
- file
->name
+ 1);
58 bcopy (file
->name
, at
, p
- file
->name
);
59 at
[p
- file
->name
] = '\0';
61 percent
= (char *) alloca (len
);
62 bcopy (p
+ 1, percent
, len
- 1);
63 percent
[len
- 1] = '\0';
66 #endif /* NO_ARCHIVES. */
72 /* $* is the stem from an implicit or static pattern rule. */
75 /* In Unix make, $* is set to the target name with
76 any suffix in the .SUFFIXES list stripped off for
77 explicit rules. We store this in the `stem' member. */
78 register struct dep
*d
;
83 if (ar_name (file
->name
))
85 name
= strchr (file
->name
, '(') + 1;
86 len
= strlen (name
) - 1;
95 for (d
= enter_file (".SUFFIXES")->deps
; d
!= 0; d
= d
->next
)
97 unsigned int slen
= strlen (dep_name (d
));
98 if (len
> slen
&& strneq (dep_name (d
), name
+ (len
- slen
), slen
))
100 file
->stem
= savestring (name
, len
- slen
);
109 /* $< is the first dependency. */
110 less
= file
->deps
!= 0 ? dep_name (file
->deps
) : "";
112 if (file
->cmds
== default_file
->cmds
)
113 /* This file got its commands from .DEFAULT.
114 In this case $< is the same as $@. */
117 #define DEFINE_VARIABLE(name, len, value) \
118 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
120 /* Define the variables. */
122 DEFINE_VARIABLE ("<", 1, less
);
123 DEFINE_VARIABLE ("*", 1, star
);
124 DEFINE_VARIABLE ("@", 1, at
);
125 DEFINE_VARIABLE ("%", 1, percent
);
127 /* Compute the values for $^, $+, $?, and $|. */
130 unsigned int qmark_len
, plus_len
, bar_len
;
131 char *caret_value
, *plus_value
;
140 /* Compute first the value for $+, which is supposed to contain
141 duplicate dependencies as they were listed in the makefile. */
144 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
145 if (! d
->ignore_mtime
)
146 plus_len
+= strlen (dep_name (d
)) + 1;
150 cp
= plus_value
= (char *) alloca (plus_len
);
152 qmark_len
= plus_len
+ 1; /* Will be this or less. */
153 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
154 if (! d
->ignore_mtime
)
156 char *c
= dep_name (d
);
161 c
= strchr (c
, '(') + 1;
162 len
= strlen (c
) - 1;
170 *cp
++ = FILE_LIST_SEPARATOR
;
172 qmark_len
-= len
+ 1; /* Don't space in $? for this one. */
175 /* Kill the last space and define the variable. */
177 cp
[cp
> plus_value
? -1 : 0] = '\0';
178 DEFINE_VARIABLE ("+", 1, plus_value
);
180 /* Make sure that no dependencies are repeated. This does not
181 really matter for the purpose of updating targets, but it
182 might make some names be listed twice for $^ and $?. */
184 uniquize_deps (file
->deps
);
187 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
189 bar_len
+= strlen (dep_name (d
)) + 1;
193 /* Compute the values for $^, $?, and $|. */
195 cp
= caret_value
= plus_value
; /* Reuse the buffer; it's big enough. */
196 qp
= qmark_value
= (char *) alloca (qmark_len
);
197 bp
= bar_value
= (char *) alloca (bar_len
);
199 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
201 char *c
= dep_name (d
);
206 c
= strchr (c
, '(') + 1;
207 len
= strlen (c
) - 1;
217 *bp
++ = FILE_LIST_SEPARATOR
;
223 *cp
++ = FILE_LIST_SEPARATOR
;
228 *qp
++ = FILE_LIST_SEPARATOR
;
233 /* Kill the last spaces and define the variables. */
235 cp
[cp
> caret_value
? -1 : 0] = '\0';
236 DEFINE_VARIABLE ("^", 1, caret_value
);
238 qp
[qp
> qmark_value
? -1 : 0] = '\0';
239 DEFINE_VARIABLE ("?", 1, qmark_value
);
241 bp
[bp
> bar_value
? -1 : 0] = '\0';
242 DEFINE_VARIABLE ("|", 1, bar_value
);
245 #undef DEFINE_VARIABLE
248 /* Chop CMDS up into individual command lines if necessary.
249 Also set the `lines_flags' and `any_recurse' members. */
253 register struct commands
*cmds
;
256 unsigned int nlines
, idx
;
259 /* If we don't have any commands,
260 or we already parsed them, never mind. */
262 if (!cmds
|| cmds
->command_lines
!= 0)
265 /* Chop CMDS->commands up into lines in CMDS->command_lines.
266 Also set the corresponding CMDS->lines_flags elements,
267 and the CMDS->any_recurse flag. */
270 lines
= (char **) xmalloc (5 * sizeof (char *));
277 end
= strchr (end
, '\n');
279 end
= p
+ strlen (p
);
280 else if (end
> p
&& end
[-1] == '\\')
284 for (b
= end
- 2; b
>= p
&& *b
== '\\'; --b
)
285 backslash
= !backslash
;
296 lines
= (char **) xrealloc ((char *) lines
,
297 nlines
* sizeof (char *));
299 lines
[idx
++] = savestring (p
, end
- p
);
308 lines
= (char **) xrealloc ((char *) lines
,
309 nlines
* sizeof (char *));
312 cmds
->ncommand_lines
= nlines
;
313 cmds
->command_lines
= lines
;
315 cmds
->any_recurse
= 0;
316 cmds
->lines_flags
= (char *) xmalloc (nlines
);
317 for (idx
= 0; idx
< nlines
; ++idx
)
322 isblank ((unsigned char)*p
) || *p
== '-' || *p
== '@' || *p
== '+';
327 flags
|= COMMANDS_RECURSE
;
330 flags
|= COMMANDS_SILENT
;
333 flags
|= COMMANDS_NOERROR
;
336 if (!(flags
& COMMANDS_RECURSE
))
338 unsigned int len
= strlen (p
);
339 if (sindex (p
, len
, "$(MAKE)", 7) != 0
340 || sindex (p
, len
, "${MAKE}", 7) != 0)
341 flags
|= COMMANDS_RECURSE
;
344 cmds
->lines_flags
[idx
] = flags
;
345 cmds
->any_recurse
|= flags
& COMMANDS_RECURSE
;
349 /* Execute the commands to remake FILE. If they are currently executing,
350 return or have already finished executing, just return. Otherwise,
351 fork off a child process to run the first command line in the sequence. */
354 execute_file_commands (file
)
359 /* Don't go through all the preparations if
360 the commands are nothing but whitespace. */
362 for (p
= file
->cmds
->commands
; *p
!= '\0'; ++p
)
363 if (!isspace ((unsigned char)*p
) && *p
!= '-' && *p
!= '@')
367 /* If there are no commands, assume everything worked. */
368 set_command_state (file
, cs_running
);
369 file
->update_status
= 0;
370 notice_finished_file (file
);
374 /* First set the automatic variables according to this file. */
376 initialize_file_variables (file
, 0);
378 set_file_variables (file
);
380 /* Start the commands running. */
384 /* This is set while we are inside fatal_error_signal,
385 so things can avoid nonreentrant operations. */
387 int handling_fatal_signal
= 0;
389 /* Handle fatal signals. */
392 fatal_error_signal (sig
)
396 extern int dos_status
, dos_command_running
;
398 if (dos_command_running
)
400 /* That was the child who got the signal, not us. */
401 dos_status
|= (sig
<< 8);
404 remove_intermediates (1);
406 #else /* not __MSDOS__ */
408 remove_intermediates (1);
410 fputs (_("*** Break.\n"), stderr
);
413 #else /* not Amiga */
414 handling_fatal_signal
= 1;
416 /* Set the handling for this signal to the default.
417 It is blocked now while we run this handler. */
418 signal (sig
, SIG_DFL
);
420 /* A termination signal won't be sent to the entire
421 process group, but it means we want to kill the children. */
425 register struct child
*c
;
426 for (c
= children
; c
!= 0; c
= c
->next
)
428 (void) kill (c
->pid
, SIGTERM
);
431 /* If we got a signal that means the user
432 wanted to kill make, remove pending targets. */
434 if (sig
== SIGTERM
|| sig
== SIGINT
443 register struct child
*c
;
445 /* Remote children won't automatically get signals sent
446 to the process group, so we must send them. */
447 for (c
= children
; c
!= 0; c
= c
->next
)
449 (void) remote_kill (c
->pid
, sig
);
451 for (c
= children
; c
!= 0; c
= c
->next
)
452 delete_child_targets (c
);
454 /* Clean up the children. We don't just use the call below because
455 we don't want to print the "Waiting for children" message. */
456 while (job_slots_used
> 0)
457 reap_children (1, 0);
460 /* Wait for our children to die. */
461 while (job_slots_used
> 0)
462 reap_children (1, 1);
464 /* Delete any non-precious intermediate files that were made. */
466 remove_intermediates (1);
470 /* We don't want to send ourselves SIGQUIT, because it will
471 cause a core dump. Just exit instead. */
475 /* Signal the same code; this time it will really be fatal. The signal
476 will be unblocked when we return and arrive then to kill us. */
477 if (kill (getpid (), sig
) < 0)
478 pfatal_with_name ("kill");
479 #endif /* not Amiga */
480 #endif /* not __MSDOS__ */
483 /* Delete FILE unless it's precious or not actually a file (phony),
484 and it has changed on disk since we last stat'd it. */
487 delete_target (file
, on_behalf_of
)
493 if (file
->precious
|| file
->phony
)
497 if (ar_name (file
->name
))
499 time_t file_date
= (file
->last_mtime
== NONEXISTENT_MTIME
501 : (time_t) FILE_TIMESTAMP_S (file
->last_mtime
));
502 if (ar_member_date (file
->name
) != file_date
)
505 error (NILF
, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
506 on_behalf_of
, file
->name
);
508 error (NILF
, _("*** Archive member `%s' may be bogus; not deleted"),
515 if (stat (file
->name
, &st
) == 0
516 && S_ISREG (st
.st_mode
)
517 && FILE_TIMESTAMP_STAT_MODTIME (file
->name
, st
) != file
->last_mtime
)
520 error (NILF
, _("*** [%s] Deleting file `%s'"), on_behalf_of
, file
->name
);
522 error (NILF
, _("*** Deleting file `%s'"), file
->name
);
523 if (unlink (file
->name
) < 0
524 && errno
!= ENOENT
) /* It disappeared; so what. */
525 perror_with_name ("unlink: ", file
->name
);
530 /* Delete all non-precious targets of CHILD unless they were already deleted.
531 Set the flag in CHILD to say they've been deleted. */
534 delete_child_targets (child
)
542 /* Delete the target file if it changed. */
543 delete_target (child
->file
, (char *) 0);
545 /* Also remove any non-precious targets listed in the `also_make' member. */
546 for (d
= child
->file
->also_make
; d
!= 0; d
= d
->next
)
547 delete_target (d
->file
, child
->file
->name
);
552 /* Print out the commands in CMDS. */
555 print_commands (cmds
)
556 register struct commands
*cmds
;
560 fputs (_("# commands to execute"), stdout
);
562 if (cmds
->fileinfo
.filenm
== 0)
563 puts (_(" (built-in):"));
565 printf (_(" (from `%s', line %lu):\n"),
566 cmds
->fileinfo
.filenm
, cmds
->fileinfo
.lineno
);
573 while (isspace ((unsigned char)*s
))
576 end
= strchr (s
, '\n');
578 end
= s
+ strlen (s
);
580 printf ("\t%.*s\n", (int) (end
- s
), s
);