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 (struct file
*file
)
44 char *at
, *percent
, *star
, *less
;
47 /* If the target is an archive member `lib(member)',
48 then $@ is `lib' and $% is `member'. */
50 if (ar_name (file
->name
))
55 p
= strchr (file
->name
, '(');
56 at
= (char *) alloca (p
- file
->name
+ 1);
57 bcopy (file
->name
, at
, p
- file
->name
);
58 at
[p
- file
->name
] = '\0';
60 percent
= (char *) alloca (len
);
61 bcopy (p
+ 1, percent
, len
- 1);
62 percent
[len
- 1] = '\0';
65 #endif /* NO_ARCHIVES. */
71 /* $* is the stem from an implicit or static pattern rule. */
74 /* In Unix make, $* is set to the target name with
75 any suffix in the .SUFFIXES list stripped off for
76 explicit rules. We store this in the `stem' member. */
77 register struct dep
*d
;
82 if (ar_name (file
->name
))
84 name
= strchr (file
->name
, '(') + 1;
85 len
= strlen (name
) - 1;
94 for (d
= enter_file (".SUFFIXES")->deps
; d
!= 0; d
= d
->next
)
96 unsigned int slen
= strlen (dep_name (d
));
97 if (len
> slen
&& strneq (dep_name (d
), name
+ (len
- slen
), slen
))
99 file
->stem
= savestring (name
, len
- slen
);
108 /* $< is the first dependency. */
109 less
= file
->deps
!= 0 ? dep_name (file
->deps
) : "";
111 if (file
->cmds
== default_file
->cmds
)
112 /* This file got its commands from .DEFAULT.
113 In this case $< is the same as $@. */
116 #define DEFINE_VARIABLE(name, len, value) \
117 (void) define_variable_for_file (name,len,value,o_automatic,0,file)
119 /* Define the variables. */
121 DEFINE_VARIABLE ("<", 1, less
);
122 DEFINE_VARIABLE ("*", 1, star
);
123 DEFINE_VARIABLE ("@", 1, at
);
124 DEFINE_VARIABLE ("%", 1, percent
);
126 /* Compute the values for $^, $+, $?, and $|. */
129 static char *plus_value
=0, *bar_value
=0, *qmark_value
=0;
130 static int qmark_max
=0, plus_max
=0, bar_max
=0;
132 unsigned int qmark_len
, plus_len
, bar_len
;
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 if (plus_len
> plus_max
)
151 plus_value
= (char *) xmalloc (plus_max
= plus_len
);
154 qmark_len
= plus_len
+ 1; /* Will be this or less. */
155 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
156 if (! d
->ignore_mtime
)
158 char *c
= dep_name (d
);
163 c
= strchr (c
, '(') + 1;
164 len
= strlen (c
) - 1;
172 *cp
++ = FILE_LIST_SEPARATOR
;
174 qmark_len
-= len
+ 1; /* Don't space in $? for this one. */
177 /* Kill the last space and define the variable. */
179 cp
[cp
> plus_value
? -1 : 0] = '\0';
180 DEFINE_VARIABLE ("+", 1, plus_value
);
182 /* Make sure that no dependencies are repeated. This does not
183 really matter for the purpose of updating targets, but it
184 might make some names be listed twice for $^ and $?. */
186 uniquize_deps (file
->deps
);
189 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
191 bar_len
+= strlen (dep_name (d
)) + 1;
195 /* Compute the values for $^, $?, and $|. */
197 cp
= caret_value
= plus_value
; /* Reuse the buffer; it's big enough. */
199 if (qmark_len
> qmark_max
)
200 qmark_value
= (char *) xmalloc (qmark_max
= qmark_len
);
203 if (bar_len
> bar_max
)
204 bar_value
= (char *) xmalloc (bar_max
= bar_len
);
207 for (d
= file
->deps
; d
!= 0; d
= d
->next
)
209 char *c
= dep_name (d
);
214 c
= strchr (c
, '(') + 1;
215 len
= strlen (c
) - 1;
225 *bp
++ = FILE_LIST_SEPARATOR
;
231 *cp
++ = FILE_LIST_SEPARATOR
;
236 *qp
++ = FILE_LIST_SEPARATOR
;
241 /* Kill the last spaces and define the variables. */
243 cp
[cp
> caret_value
? -1 : 0] = '\0';
244 DEFINE_VARIABLE ("^", 1, caret_value
);
246 qp
[qp
> qmark_value
? -1 : 0] = '\0';
247 DEFINE_VARIABLE ("?", 1, qmark_value
);
249 bp
[bp
> bar_value
? -1 : 0] = '\0';
250 DEFINE_VARIABLE ("|", 1, bar_value
);
253 #undef DEFINE_VARIABLE
256 /* Chop CMDS up into individual command lines if necessary.
257 Also set the `lines_flags' and `any_recurse' members. */
260 chop_commands (struct commands
*cmds
)
263 unsigned int nlines
, idx
;
266 /* If we don't have any commands,
267 or we already parsed them, never mind. */
269 if (!cmds
|| cmds
->command_lines
!= 0)
272 /* Chop CMDS->commands up into lines in CMDS->command_lines.
273 Also set the corresponding CMDS->lines_flags elements,
274 and the CMDS->any_recurse flag. */
277 lines
= (char **) xmalloc (5 * sizeof (char *));
284 end
= strchr (end
, '\n');
286 end
= p
+ strlen (p
);
287 else if (end
> p
&& end
[-1] == '\\')
291 for (b
= end
- 2; b
>= p
&& *b
== '\\'; --b
)
292 backslash
= !backslash
;
303 lines
= (char **) xrealloc ((char *) lines
,
304 nlines
* sizeof (char *));
306 lines
[idx
++] = savestring (p
, end
- p
);
315 lines
= (char **) xrealloc ((char *) lines
,
316 nlines
* sizeof (char *));
319 cmds
->ncommand_lines
= nlines
;
320 cmds
->command_lines
= lines
;
322 cmds
->any_recurse
= 0;
323 cmds
->lines_flags
= (char *) xmalloc (nlines
);
324 for (idx
= 0; idx
< nlines
; ++idx
)
329 isblank ((unsigned char)*p
) || *p
== '-' || *p
== '@' || *p
== '+';
334 flags
|= COMMANDS_RECURSE
;
337 flags
|= COMMANDS_SILENT
;
340 flags
|= COMMANDS_NOERROR
;
343 if (!(flags
& COMMANDS_RECURSE
))
345 unsigned int len
= strlen (p
);
346 if (sindex (p
, len
, "$(MAKE)", 7) != 0
347 || sindex (p
, len
, "${MAKE}", 7) != 0)
348 flags
|= COMMANDS_RECURSE
;
351 cmds
->lines_flags
[idx
] = flags
;
352 cmds
->any_recurse
|= flags
& COMMANDS_RECURSE
;
356 /* Execute the commands to remake FILE. If they are currently executing,
357 return or have already finished executing, just return. Otherwise,
358 fork off a child process to run the first command line in the sequence. */
361 execute_file_commands (struct file
*file
)
365 /* Don't go through all the preparations if
366 the commands are nothing but whitespace. */
368 for (p
= file
->cmds
->commands
; *p
!= '\0'; ++p
)
369 if (!isspace ((unsigned char)*p
) && *p
!= '-' && *p
!= '@')
373 /* If there are no commands, assume everything worked. */
374 set_command_state (file
, cs_running
);
375 file
->update_status
= 0;
376 notice_finished_file (file
);
380 /* First set the automatic variables according to this file. */
382 initialize_file_variables (file
, 0);
384 set_file_variables (file
);
386 /* Start the commands running. */
390 /* This is set while we are inside fatal_error_signal,
391 so things can avoid nonreentrant operations. */
393 int handling_fatal_signal
= 0;
395 /* Handle fatal signals. */
398 fatal_error_signal (int sig
)
401 extern int dos_status
, dos_command_running
;
403 if (dos_command_running
)
405 /* That was the child who got the signal, not us. */
406 dos_status
|= (sig
<< 8);
409 remove_intermediates (1);
411 #else /* not __MSDOS__ */
413 remove_intermediates (1);
415 fputs (_("*** Break.\n"), stderr
);
418 #else /* not Amiga */
419 handling_fatal_signal
= 1;
421 /* Set the handling for this signal to the default.
422 It is blocked now while we run this handler. */
423 signal (sig
, SIG_DFL
);
425 /* A termination signal won't be sent to the entire
426 process group, but it means we want to kill the children. */
430 register struct child
*c
;
431 for (c
= children
; c
!= 0; c
= c
->next
)
433 (void) kill (c
->pid
, SIGTERM
);
436 /* If we got a signal that means the user
437 wanted to kill make, remove pending targets. */
439 if (sig
== SIGTERM
|| sig
== SIGINT
448 register struct child
*c
;
450 /* Remote children won't automatically get signals sent
451 to the process group, so we must send them. */
452 for (c
= children
; c
!= 0; c
= c
->next
)
454 (void) remote_kill (c
->pid
, sig
);
456 for (c
= children
; c
!= 0; c
= c
->next
)
457 delete_child_targets (c
);
459 /* Clean up the children. We don't just use the call below because
460 we don't want to print the "Waiting for children" message. */
461 while (job_slots_used
> 0)
462 reap_children (1, 0);
465 /* Wait for our children to die. */
466 while (job_slots_used
> 0)
467 reap_children (1, 1);
469 /* Delete any non-precious intermediate files that were made. */
471 remove_intermediates (1);
475 /* We don't want to send ourselves SIGQUIT, because it will
476 cause a core dump. Just exit instead. */
480 /* Signal the same code; this time it will really be fatal. The signal
481 will be unblocked when we return and arrive then to kill us. */
482 if (kill (getpid (), sig
) < 0)
483 pfatal_with_name ("kill");
484 #endif /* not Amiga */
485 #endif /* not __MSDOS__ */
488 /* Delete FILE unless it's precious or not actually a file (phony),
489 and it has changed on disk since we last stat'd it. */
492 delete_target (struct file
*file
, char *on_behalf_of
)
497 if (file
->precious
|| file
->phony
)
501 if (ar_name (file
->name
))
503 time_t file_date
= (file
->last_mtime
== NONEXISTENT_MTIME
505 : (time_t) FILE_TIMESTAMP_S (file
->last_mtime
));
506 if (ar_member_date (file
->name
) != file_date
)
509 error (NILF
, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
510 on_behalf_of
, file
->name
);
512 error (NILF
, _("*** Archive member `%s' may be bogus; not deleted"),
519 EINTRLOOP (e
, stat (file
->name
, &st
));
521 && S_ISREG (st
.st_mode
)
522 && FILE_TIMESTAMP_STAT_MODTIME (file
->name
, st
) != file
->last_mtime
)
525 error (NILF
, _("*** [%s] Deleting file `%s'"), on_behalf_of
, file
->name
);
527 error (NILF
, _("*** Deleting file `%s'"), file
->name
);
528 if (unlink (file
->name
) < 0
529 && errno
!= ENOENT
) /* It disappeared; so what. */
530 perror_with_name ("unlink: ", file
->name
);
535 /* Delete all non-precious targets of CHILD unless they were already deleted.
536 Set the flag in CHILD to say they've been deleted. */
539 delete_child_targets (struct child
*child
)
546 /* Delete the target file if it changed. */
547 delete_target (child
->file
, (char *) 0);
549 /* Also remove any non-precious targets listed in the `also_make' member. */
550 for (d
= child
->file
->also_make
; d
!= 0; d
= d
->next
)
551 delete_target (d
->file
, child
->file
->name
);
556 /* Print out the commands in CMDS. */
559 print_commands (struct commands
*cmds
)
563 fputs (_("# commands to execute"), stdout
);
565 if (cmds
->fileinfo
.filenm
== 0)
566 puts (_(" (built-in):"));
568 printf (_(" (from `%s', line %lu):\n"),
569 cmds
->fileinfo
.filenm
, cmds
->fileinfo
.lineno
);
576 while (isspace ((unsigned char)*s
))
579 end
= strchr (s
, '\n');
581 end
= s
+ strlen (s
);
583 printf ("\t%.*s\n", (int) (end
- s
), s
);