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