compat.c revision 1.132 1 1.132 rillig /* $NetBSD: compat.c,v 1.132 2020/08/28 04:48:56 rillig Exp $ */
2 1.10 christos
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 1.47 agc * All rights reserved.
6 1.47 agc *
7 1.47 agc * This code is derived from software contributed to Berkeley by
8 1.47 agc * Adam de Boor.
9 1.47 agc *
10 1.47 agc * Redistribution and use in source and binary forms, with or without
11 1.47 agc * modification, are permitted provided that the following conditions
12 1.47 agc * are met:
13 1.47 agc * 1. Redistributions of source code must retain the above copyright
14 1.47 agc * notice, this list of conditions and the following disclaimer.
15 1.47 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.47 agc * notice, this list of conditions and the following disclaimer in the
17 1.47 agc * documentation and/or other materials provided with the distribution.
18 1.47 agc * 3. Neither the name of the University nor the names of its contributors
19 1.47 agc * may be used to endorse or promote products derived from this software
20 1.47 agc * without specific prior written permission.
21 1.47 agc *
22 1.47 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.47 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.47 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.47 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.47 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.47 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.47 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.47 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.47 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.47 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.47 agc * SUCH DAMAGE.
33 1.47 agc */
34 1.47 agc
35 1.47 agc /*
36 1.1 cgd * Copyright (c) 1988, 1989 by Adam de Boor
37 1.1 cgd * Copyright (c) 1989 by Berkeley Softworks
38 1.1 cgd * All rights reserved.
39 1.1 cgd *
40 1.1 cgd * This code is derived from software contributed to Berkeley by
41 1.1 cgd * Adam de Boor.
42 1.1 cgd *
43 1.1 cgd * Redistribution and use in source and binary forms, with or without
44 1.1 cgd * modification, are permitted provided that the following conditions
45 1.1 cgd * are met:
46 1.1 cgd * 1. Redistributions of source code must retain the above copyright
47 1.1 cgd * notice, this list of conditions and the following disclaimer.
48 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 cgd * notice, this list of conditions and the following disclaimer in the
50 1.1 cgd * documentation and/or other materials provided with the distribution.
51 1.1 cgd * 3. All advertising materials mentioning features or use of this software
52 1.1 cgd * must display the following acknowledgement:
53 1.1 cgd * This product includes software developed by the University of
54 1.1 cgd * California, Berkeley and its contributors.
55 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
56 1.1 cgd * may be used to endorse or promote products derived from this software
57 1.1 cgd * without specific prior written permission.
58 1.1 cgd *
59 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.1 cgd * SUCH DAMAGE.
70 1.1 cgd */
71 1.1 cgd
72 1.53 ross #ifndef MAKE_NATIVE
73 1.132 rillig static char rcsid[] = "$NetBSD: compat.c,v 1.132 2020/08/28 04:48:56 rillig Exp $";
74 1.22 lukem #else
75 1.21 christos #include <sys/cdefs.h>
76 1.1 cgd #ifndef lint
77 1.10 christos #if 0
78 1.14 christos static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
79 1.10 christos #else
80 1.132 rillig __RCSID("$NetBSD: compat.c,v 1.132 2020/08/28 04:48:56 rillig Exp $");
81 1.10 christos #endif
82 1.1 cgd #endif /* not lint */
83 1.22 lukem #endif
84 1.1 cgd
85 1.1 cgd /*-
86 1.1 cgd * compat.c --
87 1.1 cgd * The routines in this file implement the full-compatibility
88 1.1 cgd * mode of PMake. Most of the special functionality of PMake
89 1.1 cgd * is available in this mode. Things not supported:
90 1.1 cgd * - different shells.
91 1.1 cgd * - friendly variable substitution.
92 1.1 cgd *
93 1.1 cgd * Interface:
94 1.1 cgd * Compat_Run Initialize things for this module and recreate
95 1.1 cgd * thems as need creatin'
96 1.1 cgd */
97 1.1 cgd
98 1.1 cgd #include <sys/types.h>
99 1.11 jtc #include <sys/stat.h>
100 1.1 cgd #include <sys/wait.h>
101 1.44 wiz
102 1.1 cgd #include <ctype.h>
103 1.11 jtc #include <errno.h>
104 1.11 jtc #include <signal.h>
105 1.44 wiz #include <stdio.h>
106 1.44 wiz
107 1.1 cgd #include "make.h"
108 1.5 cgd #include "hash.h"
109 1.5 cgd #include "dir.h"
110 1.5 cgd #include "job.h"
111 1.97 christos #include "metachar.h"
112 1.43 bjh21 #include "pathnames.h"
113 1.1 cgd
114 1.1 cgd
115 1.72 dsl static GNode *curTarg = NULL;
116 1.1 cgd static GNode *ENDNode;
117 1.89 wiz static void CompatInterrupt(int);
118 1.107 sjg static pid_t compatChild;
119 1.107 sjg static int compatSigno;
120 1.1 cgd
121 1.106 dholland /*
122 1.106 dholland * CompatDeleteTarget -- delete a failed, interrupted, or otherwise
123 1.106 dholland * duffed target if not inhibited by .PRECIOUS.
124 1.106 dholland */
125 1.106 dholland static void
126 1.106 dholland CompatDeleteTarget(GNode *gn)
127 1.106 dholland {
128 1.106 dholland if ((gn != NULL) && !Targ_Precious (gn)) {
129 1.116 rillig char *p1;
130 1.116 rillig const char *file = Var_Value(TARGET, gn, &p1);
131 1.106 dholland
132 1.106 dholland if (!noExecute && eunlink(file) != -1) {
133 1.106 dholland Error("*** %s removed", file);
134 1.106 dholland }
135 1.106 dholland
136 1.117 rillig bmake_free(p1);
137 1.106 dholland }
138 1.106 dholland }
139 1.106 dholland
140 1.1 cgd /*-
141 1.1 cgd *-----------------------------------------------------------------------
142 1.1 cgd * CompatInterrupt --
143 1.1 cgd * Interrupt the creation of the current target and remove it if
144 1.1 cgd * it ain't precious.
145 1.1 cgd *
146 1.1 cgd * Results:
147 1.1 cgd * None.
148 1.1 cgd *
149 1.1 cgd * Side Effects:
150 1.1 cgd * The target is removed and the process exits. If .INTERRUPT exists,
151 1.1 cgd * its commands are run first WITH INTERRUPTS IGNORED..
152 1.1 cgd *
153 1.106 dholland * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
154 1.106 dholland * left the logic alone for now. - dholland 20160826
155 1.106 dholland *
156 1.1 cgd *-----------------------------------------------------------------------
157 1.1 cgd */
158 1.1 cgd static void
159 1.44 wiz CompatInterrupt(int signo)
160 1.1 cgd {
161 1.1 cgd GNode *gn;
162 1.14 christos
163 1.106 dholland CompatDeleteTarget(curTarg);
164 1.106 dholland
165 1.72 dsl if ((curTarg != NULL) && !Targ_Precious (curTarg)) {
166 1.1 cgd /*
167 1.1 cgd * Run .INTERRUPT only if hit with interrupt signal
168 1.1 cgd */
169 1.1 cgd if (signo == SIGINT) {
170 1.1 cgd gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
171 1.72 dsl if (gn != NULL) {
172 1.57 christos Compat_Make(gn, gn);
173 1.1 cgd }
174 1.1 cgd }
175 1.1 cgd }
176 1.87 sjg if (signo == SIGQUIT)
177 1.87 sjg _exit(signo);
178 1.107 sjg /*
179 1.107 sjg * If there is a child running, pass the signal on
180 1.107 sjg * we will exist after it has exited.
181 1.107 sjg */
182 1.107 sjg compatSigno = signo;
183 1.107 sjg if (compatChild > 0) {
184 1.107 sjg KILLPG(compatChild, signo);
185 1.107 sjg } else {
186 1.107 sjg bmake_signal(signo, SIG_DFL);
187 1.107 sjg kill(myPid, signo);
188 1.107 sjg }
189 1.1 cgd }
190 1.120 rillig
191 1.1 cgd /*-
192 1.1 cgd *-----------------------------------------------------------------------
193 1.1 cgd * CompatRunCommand --
194 1.1 cgd * Execute the next command for a target. If the command returns an
195 1.1 cgd * error, the node's made field is set to ERROR and creation stops.
196 1.1 cgd *
197 1.44 wiz * Input:
198 1.44 wiz * cmdp Command to execute
199 1.44 wiz * gnp Node from which the command came
200 1.44 wiz *
201 1.1 cgd * Results:
202 1.1 cgd * 0 if the command succeeded, 1 if an error occurred.
203 1.1 cgd *
204 1.1 cgd * Side Effects:
205 1.1 cgd * The node's 'made' field may be set to ERROR.
206 1.1 cgd *
207 1.1 cgd *-----------------------------------------------------------------------
208 1.1 cgd */
209 1.54 sjg int
210 1.75 dsl CompatRunCommand(void *cmdp, void *gnp)
211 1.1 cgd {
212 1.1 cgd char *cmdStart; /* Start of expanded command */
213 1.24 christos char *cp, *bp;
214 1.1 cgd Boolean silent, /* Don't print command */
215 1.118 rillig doIt; /* Execute even if -n */
216 1.64 christos volatile Boolean errCheck; /* Check errors */
217 1.13 christos int reason; /* Reason for child's death */
218 1.1 cgd int status; /* Description of child's death */
219 1.76 dholland pid_t cpid; /* Child actually found */
220 1.76 dholland pid_t retstat; /* Result of wait */
221 1.1 cgd LstNode cmdNode; /* Node where current command is located */
222 1.64 christos const char ** volatile av; /* Argument vector for thing to exec */
223 1.64 christos char ** volatile mav;/* Copy of the argument vector for freeing */
224 1.62 apb Boolean useShell; /* TRUE if command should be executed
225 1.62 apb * using a shell */
226 1.64 christos char * volatile cmd = (char *)cmdp;
227 1.60 christos GNode *gn = (GNode *)gnp;
228 1.1 cgd
229 1.1 cgd silent = gn->type & OP_SILENT;
230 1.1 cgd errCheck = !(gn->type & OP_IGNORE);
231 1.54 sjg doIt = FALSE;
232 1.113 rillig
233 1.132 rillig cmdNode = Lst_Member(gn->commands, cmd);
234 1.115 rillig cmdStart = Var_Subst(cmd, gn, VARE_WANTRES);
235 1.1 cgd
236 1.1 cgd /*
237 1.24 christos * brk_string will return an argv with a NULL in av[0], thus causing
238 1.1 cgd * execvp to choke and die horribly. Besides, how can we execute a null
239 1.1 cgd * command? In any case, we warn the user that the command expanded to
240 1.1 cgd * nothing (is this the right thing to do?).
241 1.1 cgd */
242 1.14 christos
243 1.1 cgd if (*cmdStart == '\0') {
244 1.7 jtc free(cmdStart);
245 1.112 rillig return 0;
246 1.1 cgd }
247 1.74 dsl cmd = cmdStart;
248 1.132 rillig LstNode_Set(cmdNode, cmdStart);
249 1.1 cgd
250 1.1 cgd if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
251 1.125 rillig assert(ENDNode != NULL);
252 1.132 rillig Lst_Append(ENDNode->commands, cmdStart);
253 1.112 rillig return 0;
254 1.74 dsl }
255 1.74 dsl if (strcmp(cmdStart, "...") == 0) {
256 1.1 cgd gn->type |= OP_SAVE_CMDS;
257 1.112 rillig return 0;
258 1.1 cgd }
259 1.1 cgd
260 1.54 sjg while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
261 1.54 sjg switch (*cmd) {
262 1.54 sjg case '@':
263 1.69 sjg silent = DEBUG(LOUD) ? FALSE : TRUE;
264 1.54 sjg break;
265 1.54 sjg case '-':
266 1.1 cgd errCheck = FALSE;
267 1.54 sjg break;
268 1.54 sjg case '+':
269 1.54 sjg doIt = TRUE;
270 1.97 christos if (!shellName) /* we came here from jobs */
271 1.97 christos Shell_Init();
272 1.54 sjg break;
273 1.1 cgd }
274 1.1 cgd cmd++;
275 1.1 cgd }
276 1.1 cgd
277 1.5 cgd while (isspace((unsigned char)*cmd))
278 1.5 cgd cmd++;
279 1.14 christos
280 1.86 sjg /*
281 1.86 sjg * If we did not end up with a command, just skip it.
282 1.86 sjg */
283 1.86 sjg if (!*cmd)
284 1.112 rillig return 0;
285 1.86 sjg
286 1.62 apb #if !defined(MAKE_NATIVE)
287 1.62 apb /*
288 1.62 apb * In a non-native build, the host environment might be weird enough
289 1.62 apb * that it's necessary to go through a shell to get the correct
290 1.62 apb * behaviour. Or perhaps the shell has been replaced with something
291 1.62 apb * that does extra logging, and that should not be bypassed.
292 1.62 apb */
293 1.62 apb useShell = TRUE;
294 1.62 apb #else
295 1.1 cgd /*
296 1.1 cgd * Search for meta characters in the command. If there are no meta
297 1.1 cgd * characters, there's no need to execute a shell to execute the
298 1.1 cgd * command.
299 1.98 mlelstv *
300 1.98 mlelstv * Additionally variable assignments and empty commands
301 1.98 mlelstv * go to the shell. Therefore treat '=' and ':' like shell
302 1.98 mlelstv * meta characters as documented in make(1).
303 1.1 cgd */
304 1.113 rillig
305 1.100 christos useShell = needshell(cmd, FALSE);
306 1.62 apb #endif
307 1.1 cgd
308 1.1 cgd /*
309 1.1 cgd * Print the command before echoing if we're not supposed to be quiet for
310 1.1 cgd * this one. We also print the command if -n given.
311 1.1 cgd */
312 1.31 sommerfe if (!silent || NoExecute(gn)) {
313 1.59 christos printf("%s\n", cmd);
314 1.1 cgd fflush(stdout);
315 1.1 cgd }
316 1.1 cgd
317 1.1 cgd /*
318 1.1 cgd * If we're not supposed to execute any commands, this is as far as
319 1.1 cgd * we go...
320 1.1 cgd */
321 1.54 sjg if (!doIt && NoExecute(gn)) {
322 1.112 rillig return 0;
323 1.1 cgd }
324 1.74 dsl if (DEBUG(JOB))
325 1.74 dsl fprintf(debug_file, "Execute: '%s'\n", cmd);
326 1.14 christos
327 1.73 christos again:
328 1.62 apb if (useShell) {
329 1.1 cgd /*
330 1.62 apb * We need to pass the command off to the shell, typically
331 1.62 apb * because the command contains a "meta" character.
332 1.1 cgd */
333 1.92 sjg static const char *shargv[5];
334 1.92 sjg int shargc;
335 1.1 cgd
336 1.92 sjg shargc = 0;
337 1.92 sjg shargv[shargc++] = shellPath;
338 1.46 sjg /*
339 1.46 sjg * The following work for any of the builtin shell specs.
340 1.46 sjg */
341 1.93 sjg if (errCheck && shellErrFlag) {
342 1.92 sjg shargv[shargc++] = shellErrFlag;
343 1.92 sjg }
344 1.34 sjg if (DEBUG(SHELL))
345 1.92 sjg shargv[shargc++] = "-xc";
346 1.34 sjg else
347 1.92 sjg shargv[shargc++] = "-c";
348 1.92 sjg shargv[shargc++] = cmd;
349 1.92 sjg shargv[shargc++] = NULL;
350 1.1 cgd av = shargv;
351 1.24 christos bp = NULL;
352 1.61 christos mav = NULL;
353 1.1 cgd } else {
354 1.128 rillig size_t argc;
355 1.1 cgd /*
356 1.1 cgd * No meta-characters, so no need to exec a shell. Break the command
357 1.1 cgd * into words to form an argument vector we can execute.
358 1.1 cgd */
359 1.128 rillig mav = brk_string(cmd, TRUE, &argc, &bp);
360 1.73 christos if (mav == NULL) {
361 1.73 christos useShell = 1;
362 1.73 christos goto again;
363 1.73 christos }
364 1.83 christos av = (void *)mav;
365 1.1 cgd }
366 1.14 christos
367 1.81 sjg #ifdef USE_META
368 1.81 sjg if (useMeta) {
369 1.81 sjg meta_compat_start();
370 1.81 sjg }
371 1.81 sjg #endif
372 1.113 rillig
373 1.1 cgd /*
374 1.1 cgd * Fork and execute the single command. If the fork fails, we abort.
375 1.1 cgd */
376 1.107 sjg compatChild = cpid = vFork();
377 1.1 cgd if (cpid < 0) {
378 1.1 cgd Fatal("Could not fork");
379 1.1 cgd }
380 1.1 cgd if (cpid == 0) {
381 1.68 sjg Var_ExportVars();
382 1.81 sjg #ifdef USE_META
383 1.81 sjg if (useMeta) {
384 1.81 sjg meta_compat_child();
385 1.81 sjg }
386 1.81 sjg #endif
387 1.121 rillig (void)execvp(av[0], (char *const *)UNCONST(av));
388 1.41 pk execError("exec", av[0]);
389 1.23 thorpej _exit(1);
390 1.1 cgd }
391 1.103 christos
392 1.103 christos free(mav);
393 1.103 christos free(bp);
394 1.103 christos
395 1.127 rillig /* XXX: Memory management looks suspicious here. */
396 1.127 rillig /* XXX: Setting a list item to NULL is unexpected. */
397 1.132 rillig LstNode_SetNull(cmdNode);
398 1.14 christos
399 1.81 sjg #ifdef USE_META
400 1.81 sjg if (useMeta) {
401 1.110 riastrad meta_compat_parent(cpid);
402 1.81 sjg }
403 1.81 sjg #endif
404 1.81 sjg
405 1.1 cgd /*
406 1.1 cgd * The child is off and running. Now all we can do is wait...
407 1.1 cgd */
408 1.71 christos while (1) {
409 1.1 cgd
410 1.37 reinoud while ((retstat = wait(&reason)) != cpid) {
411 1.80 sjg if (retstat > 0)
412 1.80 sjg JobReapChild(retstat, reason, FALSE); /* not ours? */
413 1.37 reinoud if (retstat == -1 && errno != EINTR) {
414 1.1 cgd break;
415 1.1 cgd }
416 1.1 cgd }
417 1.14 christos
418 1.37 reinoud if (retstat > -1) {
419 1.1 cgd if (WIFSTOPPED(reason)) {
420 1.13 christos status = WSTOPSIG(reason); /* stopped */
421 1.1 cgd } else if (WIFEXITED(reason)) {
422 1.13 christos status = WEXITSTATUS(reason); /* exited */
423 1.109 maxv #if defined(USE_META) && defined(USE_FILEMON_ONCE)
424 1.109 maxv if (useMeta) {
425 1.109 maxv meta_cmd_finish(NULL);
426 1.109 maxv }
427 1.109 maxv #endif
428 1.1 cgd if (status != 0) {
429 1.52 jmmv if (DEBUG(ERROR)) {
430 1.118 rillig fprintf(debug_file, "\n*** Failed target: %s\n*** Failed command: ",
431 1.52 jmmv gn->name);
432 1.118 rillig for (cp = cmd; *cp; ) {
433 1.118 rillig if (isspace((unsigned char)*cp)) {
434 1.63 dsl fprintf(debug_file, " ");
435 1.118 rillig while (isspace((unsigned char)*cp))
436 1.52 jmmv cp++;
437 1.52 jmmv } else {
438 1.63 dsl fprintf(debug_file, "%c", *cp);
439 1.118 rillig cp++;
440 1.52 jmmv }
441 1.118 rillig }
442 1.63 dsl fprintf(debug_file, "\n");
443 1.51 lukem }
444 1.77 sjg printf("*** Error code %d", status);
445 1.1 cgd }
446 1.1 cgd } else {
447 1.13 christos status = WTERMSIG(reason); /* signaled */
448 1.59 christos printf("*** Signal %d", status);
449 1.14 christos }
450 1.14 christos
451 1.1 cgd
452 1.1 cgd if (!WIFEXITED(reason) || (status != 0)) {
453 1.1 cgd if (errCheck) {
454 1.81 sjg #ifdef USE_META
455 1.81 sjg if (useMeta) {
456 1.81 sjg meta_job_error(NULL, gn, 0, status);
457 1.81 sjg }
458 1.81 sjg #endif
459 1.1 cgd gn->made = ERROR;
460 1.1 cgd if (keepgoing) {
461 1.1 cgd /*
462 1.1 cgd * Abort the current target, but let others
463 1.1 cgd * continue.
464 1.1 cgd */
465 1.59 christos printf(" (continuing)\n");
466 1.106 dholland } else {
467 1.106 dholland printf("\n");
468 1.106 dholland }
469 1.106 dholland if (deleteOnError) {
470 1.106 dholland CompatDeleteTarget(gn);
471 1.1 cgd }
472 1.1 cgd } else {
473 1.1 cgd /*
474 1.1 cgd * Continue executing commands for this target.
475 1.1 cgd * If we return 0, this will happen...
476 1.1 cgd */
477 1.59 christos printf(" (ignored)\n");
478 1.1 cgd status = 0;
479 1.1 cgd }
480 1.1 cgd }
481 1.1 cgd break;
482 1.1 cgd } else {
483 1.56 christos Fatal("error in wait: %d: %s", retstat, strerror(errno));
484 1.1 cgd /*NOTREACHED*/
485 1.1 cgd }
486 1.1 cgd }
487 1.50 lukem free(cmdStart);
488 1.107 sjg compatChild = 0;
489 1.107 sjg if (compatSigno) {
490 1.107 sjg bmake_signal(compatSigno, SIG_DFL);
491 1.107 sjg kill(myPid, compatSigno);
492 1.107 sjg }
493 1.113 rillig
494 1.112 rillig return status;
495 1.1 cgd }
496 1.120 rillig
497 1.1 cgd /*-
498 1.1 cgd *-----------------------------------------------------------------------
499 1.57 christos * Compat_Make --
500 1.1 cgd * Make a target.
501 1.1 cgd *
502 1.44 wiz * Input:
503 1.44 wiz * gnp The node to make
504 1.44 wiz * pgnp Parent to abort if necessary
505 1.44 wiz *
506 1.1 cgd * Results:
507 1.1 cgd * 0
508 1.1 cgd *
509 1.1 cgd * Side Effects:
510 1.1 cgd * If an error is detected and not being ignored, the process exits.
511 1.1 cgd *
512 1.1 cgd *-----------------------------------------------------------------------
513 1.1 cgd */
514 1.57 christos int
515 1.75 dsl Compat_Make(void *gnp, void *pgnp)
516 1.1 cgd {
517 1.60 christos GNode *gn = (GNode *)gnp;
518 1.60 christos GNode *pgn = (GNode *)pgnp;
519 1.16 christos
520 1.97 christos if (!shellName) /* we came here from jobs */
521 1.97 christos Shell_Init();
522 1.40 pk if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) {
523 1.1 cgd /*
524 1.1 cgd * First mark ourselves to be made, then apply whatever transformations
525 1.1 cgd * the suffix module thinks are necessary. Once that's done, we can
526 1.1 cgd * descend and make all our children. If any of them has an error
527 1.1 cgd * but the -k flag was given, our 'make' field will be set FALSE again.
528 1.1 cgd * This is our signal to not attempt to do anything but abort our
529 1.1 cgd * parent as well.
530 1.1 cgd */
531 1.26 christos gn->flags |= REMAKE;
532 1.1 cgd gn->made = BEINGMADE;
533 1.38 pk if ((gn->type & OP_MADE) == 0)
534 1.56 christos Suff_FindDeps(gn);
535 1.132 rillig Lst_ForEach(gn->children, Compat_Make, gn);
536 1.26 christos if ((gn->flags & REMAKE) == 0) {
537 1.1 cgd gn->made = ABORTED;
538 1.129 rillig pgn->flags &= ~(unsigned)REMAKE;
539 1.28 mycroft goto cohorts;
540 1.1 cgd }
541 1.1 cgd
542 1.132 rillig if (Lst_Member(gn->iParents, pgn) != NULL) {
543 1.96 joerg char *p1;
544 1.111 rillig Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
545 1.117 rillig bmake_free(p1);
546 1.96 joerg }
547 1.14 christos
548 1.1 cgd /*
549 1.82 christos * All the children were made ok. Now cmgn->mtime contains the
550 1.82 christos * modification time of the newest child, we need to find out if we
551 1.82 christos * exist and when we were modified last. The criteria for datedness
552 1.82 christos * are defined by the Make_OODate function.
553 1.1 cgd */
554 1.1 cgd if (DEBUG(MAKE)) {
555 1.63 dsl fprintf(debug_file, "Examining %s...", gn->name);
556 1.1 cgd }
557 1.1 cgd if (! Make_OODate(gn)) {
558 1.1 cgd gn->made = UPTODATE;
559 1.1 cgd if (DEBUG(MAKE)) {
560 1.63 dsl fprintf(debug_file, "up-to-date.\n");
561 1.1 cgd }
562 1.28 mycroft goto cohorts;
563 1.1 cgd } else if (DEBUG(MAKE)) {
564 1.63 dsl fprintf(debug_file, "out-of-date.\n");
565 1.1 cgd }
566 1.1 cgd
567 1.1 cgd /*
568 1.1 cgd * If the user is just seeing if something is out-of-date, exit now
569 1.1 cgd * to tell him/her "yes".
570 1.1 cgd */
571 1.1 cgd if (queryFlag) {
572 1.59 christos exit(1);
573 1.1 cgd }
574 1.1 cgd
575 1.1 cgd /*
576 1.1 cgd * We need to be re-made. We also have to make sure we've got a $?
577 1.1 cgd * variable. To be nice, we also define the $> variable using
578 1.1 cgd * Make_DoAllVar().
579 1.1 cgd */
580 1.1 cgd Make_DoAllVar(gn);
581 1.14 christos
582 1.1 cgd /*
583 1.1 cgd * Alter our type to tell if errors should be ignored or things
584 1.1 cgd * should not be printed so CompatRunCommand knows what to do.
585 1.1 cgd */
586 1.56 christos if (Targ_Ignore(gn)) {
587 1.1 cgd gn->type |= OP_IGNORE;
588 1.1 cgd }
589 1.56 christos if (Targ_Silent(gn)) {
590 1.1 cgd gn->type |= OP_SILENT;
591 1.1 cgd }
592 1.1 cgd
593 1.56 christos if (Job_CheckCommands(gn, Fatal)) {
594 1.1 cgd /*
595 1.1 cgd * Our commands are ok, but we still have to worry about the -t
596 1.1 cgd * flag...
597 1.1 cgd */
598 1.20 mycroft if (!touchFlag || (gn->type & OP_MAKE)) {
599 1.1 cgd curTarg = gn;
600 1.81 sjg #ifdef USE_META
601 1.81 sjg if (useMeta && !NoExecute(gn)) {
602 1.81 sjg meta_job_start(NULL, gn);
603 1.81 sjg }
604 1.81 sjg #endif
605 1.132 rillig Lst_ForEach(gn->commands, CompatRunCommand, gn);
606 1.72 dsl curTarg = NULL;
607 1.1 cgd } else {
608 1.71 christos Job_Touch(gn, gn->type & OP_SILENT);
609 1.1 cgd }
610 1.1 cgd } else {
611 1.1 cgd gn->made = ERROR;
612 1.1 cgd }
613 1.81 sjg #ifdef USE_META
614 1.81 sjg if (useMeta && !NoExecute(gn)) {
615 1.105 sjg if (meta_job_finish(NULL) != 0)
616 1.105 sjg gn->made = ERROR;
617 1.81 sjg }
618 1.81 sjg #endif
619 1.1 cgd
620 1.1 cgd if (gn->made != ERROR) {
621 1.1 cgd /*
622 1.1 cgd * If the node was made successfully, mark it so, update
623 1.1 cgd * its modification time and timestamp all its parents. Note
624 1.1 cgd * that for .ZEROTIME targets, the timestamping isn't done.
625 1.1 cgd * This is to keep its state from affecting that of its parent.
626 1.1 cgd */
627 1.1 cgd gn->made = MADE;
628 1.26 christos pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
629 1.1 cgd if (!(gn->type & OP_EXEC)) {
630 1.26 christos pgn->flags |= CHILDMADE;
631 1.1 cgd Make_TimeStamp(pgn, gn);
632 1.1 cgd }
633 1.1 cgd } else if (keepgoing) {
634 1.129 rillig pgn->flags &= ~(unsigned)REMAKE;
635 1.1 cgd } else {
636 1.106 dholland PrintOnError(gn, "\nStop.");
637 1.59 christos exit(1);
638 1.1 cgd }
639 1.1 cgd } else if (gn->made == ERROR) {
640 1.1 cgd /*
641 1.1 cgd * Already had an error when making this beastie. Tell the parent
642 1.1 cgd * to abort.
643 1.1 cgd */
644 1.129 rillig pgn->flags &= ~(unsigned)REMAKE;
645 1.1 cgd } else {
646 1.132 rillig if (Lst_Member(gn->iParents, pgn) != NULL) {
647 1.96 joerg char *p1;
648 1.111 rillig Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
649 1.117 rillig bmake_free(p1);
650 1.96 joerg }
651 1.1 cgd switch(gn->made) {
652 1.1 cgd case BEINGMADE:
653 1.42 christos Error("Graph cycles through %s", gn->name);
654 1.1 cgd gn->made = ERROR;
655 1.129 rillig pgn->flags &= ~(unsigned)REMAKE;
656 1.1 cgd break;
657 1.1 cgd case MADE:
658 1.1 cgd if ((gn->type & OP_EXEC) == 0) {
659 1.26 christos pgn->flags |= CHILDMADE;
660 1.1 cgd Make_TimeStamp(pgn, gn);
661 1.1 cgd }
662 1.1 cgd break;
663 1.1 cgd case UPTODATE:
664 1.1 cgd if ((gn->type & OP_EXEC) == 0) {
665 1.1 cgd Make_TimeStamp(pgn, gn);
666 1.1 cgd }
667 1.1 cgd break;
668 1.5 cgd default:
669 1.5 cgd break;
670 1.1 cgd }
671 1.1 cgd }
672 1.1 cgd
673 1.28 mycroft cohorts:
674 1.132 rillig Lst_ForEach(gn->cohorts, Compat_Make, pgnp);
675 1.112 rillig return 0;
676 1.1 cgd }
677 1.120 rillig
678 1.1 cgd /*-
679 1.1 cgd *-----------------------------------------------------------------------
680 1.1 cgd * Compat_Run --
681 1.1 cgd * Initialize this mode and start making.
682 1.1 cgd *
683 1.44 wiz * Input:
684 1.44 wiz * targs List of target nodes to re-create
685 1.44 wiz *
686 1.1 cgd * Results:
687 1.1 cgd * None.
688 1.1 cgd *
689 1.1 cgd * Side Effects:
690 1.1 cgd * Guess what?
691 1.1 cgd *
692 1.1 cgd *-----------------------------------------------------------------------
693 1.1 cgd */
694 1.1 cgd void
695 1.44 wiz Compat_Run(Lst targs)
696 1.1 cgd {
697 1.5 cgd GNode *gn = NULL;/* Current root target */
698 1.1 cgd int errors; /* Number of targets not remade due to errors */
699 1.46 sjg
700 1.97 christos if (!shellName)
701 1.97 christos Shell_Init();
702 1.1 cgd
703 1.79 sjg if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN) {
704 1.79 sjg bmake_signal(SIGINT, CompatInterrupt);
705 1.1 cgd }
706 1.79 sjg if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN) {
707 1.79 sjg bmake_signal(SIGTERM, CompatInterrupt);
708 1.1 cgd }
709 1.79 sjg if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN) {
710 1.79 sjg bmake_signal(SIGHUP, CompatInterrupt);
711 1.1 cgd }
712 1.79 sjg if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
713 1.79 sjg bmake_signal(SIGQUIT, CompatInterrupt);
714 1.1 cgd }
715 1.1 cgd
716 1.1 cgd ENDNode = Targ_FindNode(".END", TARG_CREATE);
717 1.58 christos ENDNode->type = OP_SPECIAL;
718 1.1 cgd /*
719 1.1 cgd * If the user has defined a .BEGIN target, execute the commands attached
720 1.1 cgd * to it.
721 1.1 cgd */
722 1.1 cgd if (!queryFlag) {
723 1.1 cgd gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
724 1.72 dsl if (gn != NULL) {
725 1.57 christos Compat_Make(gn, gn);
726 1.118 rillig if (gn->made == ERROR) {
727 1.118 rillig PrintOnError(gn, "\nStop.");
728 1.118 rillig exit(1);
729 1.118 rillig }
730 1.1 cgd }
731 1.1 cgd }
732 1.15 christos
733 1.15 christos /*
734 1.15 christos * Expand .USE nodes right now, because they can modify the structure
735 1.15 christos * of the tree.
736 1.15 christos */
737 1.66 dsl Make_ExpandUse(targs);
738 1.1 cgd
739 1.1 cgd /*
740 1.57 christos * For each entry in the list of targets to create, call Compat_Make on
741 1.57 christos * it to create the thing. Compat_Make will leave the 'made' field of gn
742 1.1 cgd * in one of several states:
743 1.1 cgd * UPTODATE gn was already up-to-date
744 1.1 cgd * MADE gn was recreated successfully
745 1.1 cgd * ERROR An error occurred while gn was being created
746 1.1 cgd * ABORTED gn was not remade because one of its inferiors
747 1.1 cgd * could not be made due to errors.
748 1.1 cgd */
749 1.1 cgd errors = 0;
750 1.132 rillig while (!Lst_IsEmpty(targs)) {
751 1.132 rillig gn = Lst_Dequeue(targs);
752 1.57 christos Compat_Make(gn, gn);
753 1.1 cgd
754 1.1 cgd if (gn->made == UPTODATE) {
755 1.59 christos printf("`%s' is up to date.\n", gn->name);
756 1.1 cgd } else if (gn->made == ABORTED) {
757 1.59 christos printf("`%s' not remade because of errors.\n", gn->name);
758 1.1 cgd errors += 1;
759 1.1 cgd }
760 1.1 cgd }
761 1.1 cgd
762 1.1 cgd /*
763 1.1 cgd * If the user has defined a .END target, run its commands.
764 1.1 cgd */
765 1.1 cgd if (errors == 0) {
766 1.57 christos Compat_Make(ENDNode, ENDNode);
767 1.36 sjg if (gn->made == ERROR) {
768 1.106 dholland PrintOnError(gn, "\nStop.");
769 1.36 sjg exit(1);
770 1.36 sjg }
771 1.1 cgd }
772 1.1 cgd }
773