compat.c revision 1.214 1 1.214 rillig /* $NetBSD: compat.c,v 1.214 2020/12/13 18:57:44 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.156 rillig * Compat_Run Initialize things for this module and recreate
82 1.156 rillig * thems as need creatin'
83 1.1 cgd */
84 1.1 cgd
85 1.165 rillig #include <sys/types.h>
86 1.165 rillig #include <sys/stat.h>
87 1.165 rillig #include <sys/wait.h>
88 1.165 rillig
89 1.165 rillig #include <errno.h>
90 1.165 rillig #include <signal.h>
91 1.165 rillig
92 1.165 rillig #include "make.h"
93 1.165 rillig #include "dir.h"
94 1.165 rillig #include "job.h"
95 1.165 rillig #include "metachar.h"
96 1.165 rillig #include "pathnames.h"
97 1.1 cgd
98 1.145 rillig /* "@(#)compat.c 8.2 (Berkeley) 3/19/94" */
99 1.214 rillig MAKE_RCSID("$NetBSD: compat.c,v 1.214 2020/12/13 18:57:44 rillig Exp $");
100 1.1 cgd
101 1.165 rillig static GNode *curTarg = NULL;
102 1.107 sjg static pid_t compatChild;
103 1.107 sjg static int compatSigno;
104 1.1 cgd
105 1.106 dholland /*
106 1.174 rillig * CompatDeleteTarget -- delete the file of a failed, interrupted, or
107 1.174 rillig * otherwise duffed target if not inhibited by .PRECIOUS.
108 1.106 dholland */
109 1.106 dholland static void
110 1.106 dholland CompatDeleteTarget(GNode *gn)
111 1.106 dholland {
112 1.186 rillig if (gn != NULL && !Targ_Precious(gn)) {
113 1.186 rillig const char *file = GNode_VarTarget(gn);
114 1.106 dholland
115 1.186 rillig if (!opts.noExecute && eunlink(file) != -1) {
116 1.186 rillig Error("*** %s removed", file);
117 1.186 rillig }
118 1.106 dholland }
119 1.106 dholland }
120 1.106 dholland
121 1.134 rillig /* Interrupt the creation of the current target and remove it if it ain't
122 1.134 rillig * precious. Then exit.
123 1.1 cgd *
124 1.134 rillig * If .INTERRUPT exists, its commands are run first WITH INTERRUPTS IGNORED.
125 1.1 cgd *
126 1.106 dholland * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
127 1.106 dholland * left the logic alone for now. - dholland 20160826
128 1.1 cgd */
129 1.1 cgd static void
130 1.44 wiz CompatInterrupt(int signo)
131 1.1 cgd {
132 1.186 rillig CompatDeleteTarget(curTarg);
133 1.186 rillig
134 1.186 rillig if (curTarg != NULL && !Targ_Precious(curTarg)) {
135 1.186 rillig /*
136 1.186 rillig * Run .INTERRUPT only if hit with interrupt signal
137 1.186 rillig */
138 1.186 rillig if (signo == SIGINT) {
139 1.186 rillig GNode *gn = Targ_FindNode(".INTERRUPT");
140 1.186 rillig if (gn != NULL) {
141 1.186 rillig Compat_Make(gn, gn);
142 1.186 rillig }
143 1.186 rillig }
144 1.186 rillig }
145 1.186 rillig
146 1.186 rillig if (signo == SIGQUIT)
147 1.186 rillig _exit(signo);
148 1.106 dholland
149 1.1 cgd /*
150 1.186 rillig * If there is a child running, pass the signal on.
151 1.186 rillig * We will exist after it has exited.
152 1.1 cgd */
153 1.186 rillig compatSigno = signo;
154 1.186 rillig if (compatChild > 0) {
155 1.186 rillig KILLPG(compatChild, signo);
156 1.186 rillig } else {
157 1.186 rillig bmake_signal(signo, SIG_DFL);
158 1.186 rillig kill(myPid, signo);
159 1.1 cgd }
160 1.1 cgd }
161 1.120 rillig
162 1.184 rillig static void
163 1.184 rillig DebugFailedTarget(const char *cmd, GNode *gn)
164 1.184 rillig {
165 1.184 rillig const char *p = cmd;
166 1.184 rillig debug_printf("\n*** Failed target: %s\n*** Failed command: ",
167 1.184 rillig gn->name);
168 1.184 rillig
169 1.184 rillig /* Replace runs of whitespace with a single space, to reduce
170 1.184 rillig * the amount of whitespace for multi-line command lines. */
171 1.184 rillig while (*p != '\0') {
172 1.184 rillig if (ch_isspace(*p)) {
173 1.184 rillig debug_printf(" ");
174 1.184 rillig cpp_skip_whitespace(&p);
175 1.184 rillig } else {
176 1.184 rillig debug_printf("%c", *p);
177 1.184 rillig p++;
178 1.184 rillig }
179 1.184 rillig }
180 1.184 rillig debug_printf("\n");
181 1.184 rillig }
182 1.184 rillig
183 1.209 rillig static Boolean
184 1.209 rillig UseShell(const char *cmd MAKE_ATTR_UNUSED)
185 1.209 rillig {
186 1.209 rillig #if !defined(MAKE_NATIVE)
187 1.209 rillig /*
188 1.209 rillig * In a non-native build, the host environment might be weird enough
189 1.209 rillig * that it's necessary to go through a shell to get the correct
190 1.209 rillig * behaviour. Or perhaps the shell has been replaced with something
191 1.209 rillig * that does extra logging, and that should not be bypassed.
192 1.209 rillig */
193 1.209 rillig return TRUE;
194 1.209 rillig #else
195 1.209 rillig /*
196 1.209 rillig * Search for meta characters in the command. If there are no meta
197 1.209 rillig * characters, there's no need to execute a shell to execute the
198 1.209 rillig * command.
199 1.209 rillig *
200 1.209 rillig * Additionally variable assignments and empty commands
201 1.209 rillig * go to the shell. Therefore treat '=' and ':' like shell
202 1.209 rillig * meta characters as documented in make(1).
203 1.209 rillig */
204 1.209 rillig
205 1.209 rillig return needshell(cmd);
206 1.209 rillig #endif
207 1.209 rillig }
208 1.209 rillig
209 1.165 rillig /* Execute the next command for a target. If the command returns an error,
210 1.165 rillig * the node's made field is set to ERROR and creation stops.
211 1.1 cgd *
212 1.44 wiz * Input:
213 1.44 wiz * cmdp Command to execute
214 1.44 wiz * gnp Node from which the command came
215 1.44 wiz *
216 1.1 cgd * Results:
217 1.1 cgd * 0 if the command succeeded, 1 if an error occurred.
218 1.1 cgd */
219 1.54 sjg int
220 1.168 rillig Compat_RunCommand(const char *cmdp, GNode *gn)
221 1.1 cgd {
222 1.186 rillig char *cmdStart; /* Start of expanded command */
223 1.186 rillig char *bp;
224 1.186 rillig Boolean silent; /* Don't print command */
225 1.186 rillig Boolean doIt; /* Execute even if -n */
226 1.186 rillig volatile Boolean errCheck; /* Check errors */
227 1.186 rillig int reason; /* Reason for child's death */
228 1.186 rillig int status; /* Description of child's death */
229 1.186 rillig pid_t cpid; /* Child actually found */
230 1.186 rillig pid_t retstat; /* Result of wait */
231 1.186 rillig StringListNode *cmdNode; /* Node where current command is located */
232 1.186 rillig const char **volatile av; /* Argument vector for thing to exec */
233 1.186 rillig char **volatile mav; /* Copy of the argument vector for freeing */
234 1.186 rillig Boolean useShell; /* TRUE if command should be executed
235 1.62 apb * using a shell */
236 1.186 rillig const char *volatile cmd = cmdp;
237 1.186 rillig
238 1.186 rillig silent = (gn->type & OP_SILENT) != 0;
239 1.186 rillig errCheck = !(gn->type & OP_IGNORE);
240 1.186 rillig doIt = FALSE;
241 1.186 rillig
242 1.186 rillig /* Luckily the commands don't end up in a string pool, otherwise
243 1.186 rillig * this comparison could match too early, in a dependency using "..."
244 1.186 rillig * for delayed commands, run in parallel mode, using the same shell
245 1.186 rillig * command line more than once; see JobPrintCommand.
246 1.186 rillig * TODO: write a unit-test to protect against this potential bug. */
247 1.193 rillig cmdNode = Lst_FindDatum(&gn->commands, cmd);
248 1.186 rillig (void)Var_Subst(cmd, gn, VARE_WANTRES, &cmdStart);
249 1.186 rillig /* TODO: handle errors */
250 1.186 rillig
251 1.186 rillig if (cmdStart[0] == '\0') {
252 1.186 rillig free(cmdStart);
253 1.186 rillig return 0;
254 1.186 rillig }
255 1.186 rillig cmd = cmdStart;
256 1.186 rillig LstNode_Set(cmdNode, cmdStart);
257 1.186 rillig
258 1.186 rillig if (gn->type & OP_SAVE_CMDS) {
259 1.186 rillig GNode *endNode = Targ_GetEndNode();
260 1.186 rillig if (gn != endNode) {
261 1.214 rillig /*
262 1.214 rillig * Append the expanded command, to prevent the
263 1.214 rillig * local variables from being interpreted in the
264 1.214 rillig * context of the .END node.
265 1.214 rillig *
266 1.214 rillig * A probably unintended side effect of this is that
267 1.214 rillig * the expanded command will be expanded again in the
268 1.214 rillig * .END node. Therefore, a literal '$' in these
269 1.214 rillig * commands must be written as '$$$$' instead of the
270 1.214 rillig * usual '$$'.
271 1.214 rillig */
272 1.193 rillig Lst_Append(&endNode->commands, cmdStart);
273 1.186 rillig return 0;
274 1.186 rillig }
275 1.186 rillig }
276 1.186 rillig if (strcmp(cmdStart, "...") == 0) {
277 1.186 rillig gn->type |= OP_SAVE_CMDS;
278 1.186 rillig return 0;
279 1.186 rillig }
280 1.186 rillig
281 1.186 rillig for (;;) {
282 1.186 rillig if (*cmd == '@')
283 1.186 rillig silent = !DEBUG(LOUD);
284 1.186 rillig else if (*cmd == '-')
285 1.186 rillig errCheck = FALSE;
286 1.186 rillig else if (*cmd == '+') {
287 1.186 rillig doIt = TRUE;
288 1.186 rillig if (!shellName) /* we came here from jobs */
289 1.186 rillig Shell_Init();
290 1.186 rillig } else
291 1.186 rillig break;
292 1.186 rillig cmd++;
293 1.186 rillig }
294 1.1 cgd
295 1.186 rillig while (ch_isspace(*cmd))
296 1.186 rillig cmd++;
297 1.1 cgd
298 1.186 rillig /*
299 1.186 rillig * If we did not end up with a command, just skip it.
300 1.186 rillig */
301 1.186 rillig if (cmd[0] == '\0')
302 1.186 rillig return 0;
303 1.86 sjg
304 1.209 rillig useShell = UseShell(cmd);
305 1.186 rillig /*
306 1.186 rillig * Print the command before echoing if we're not supposed to be quiet
307 1.186 rillig * for this one. We also print the command if -n given.
308 1.186 rillig */
309 1.186 rillig if (!silent || !GNode_ShouldExecute(gn)) {
310 1.186 rillig printf("%s\n", cmd);
311 1.186 rillig fflush(stdout);
312 1.186 rillig }
313 1.186 rillig
314 1.186 rillig /*
315 1.186 rillig * If we're not supposed to execute any commands, this is as far as
316 1.186 rillig * we go...
317 1.186 rillig */
318 1.186 rillig if (!doIt && !GNode_ShouldExecute(gn))
319 1.186 rillig return 0;
320 1.186 rillig
321 1.186 rillig DEBUG1(JOB, "Execute: '%s'\n", cmd);
322 1.14 christos
323 1.186 rillig if (useShell) {
324 1.186 rillig /*
325 1.186 rillig * We need to pass the command off to the shell, typically
326 1.186 rillig * because the command contains a "meta" character.
327 1.186 rillig */
328 1.186 rillig static const char *shargv[5];
329 1.186 rillig
330 1.186 rillig /* The following work for any of the builtin shell specs. */
331 1.186 rillig int shargc = 0;
332 1.186 rillig shargv[shargc++] = shellPath;
333 1.186 rillig if (errCheck && shellErrFlag)
334 1.186 rillig shargv[shargc++] = shellErrFlag;
335 1.186 rillig shargv[shargc++] = DEBUG(SHELL) ? "-xc" : "-c";
336 1.186 rillig shargv[shargc++] = cmd;
337 1.186 rillig shargv[shargc] = NULL;
338 1.186 rillig av = shargv;
339 1.186 rillig bp = NULL;
340 1.186 rillig mav = NULL;
341 1.186 rillig } else {
342 1.186 rillig /*
343 1.186 rillig * No meta-characters, so no need to exec a shell. Break the
344 1.186 rillig * command into words to form an argument vector we can
345 1.186 rillig * execute.
346 1.186 rillig */
347 1.186 rillig Words words = Str_Words(cmd, FALSE);
348 1.186 rillig mav = words.words;
349 1.186 rillig bp = words.freeIt;
350 1.186 rillig av = (void *)mav;
351 1.186 rillig }
352 1.113 rillig
353 1.81 sjg #ifdef USE_META
354 1.81 sjg if (useMeta) {
355 1.186 rillig meta_compat_start();
356 1.81 sjg }
357 1.81 sjg #endif
358 1.14 christos
359 1.186 rillig /*
360 1.186 rillig * Fork and execute the single command. If the fork fails, we abort.
361 1.186 rillig */
362 1.186 rillig compatChild = cpid = vFork();
363 1.186 rillig if (cpid < 0) {
364 1.186 rillig Fatal("Could not fork");
365 1.186 rillig }
366 1.186 rillig if (cpid == 0) {
367 1.208 rillig Var_ReexportVars();
368 1.81 sjg #ifdef USE_META
369 1.186 rillig if (useMeta) {
370 1.186 rillig meta_compat_child();
371 1.186 rillig }
372 1.81 sjg #endif
373 1.186 rillig (void)execvp(av[0], (char *const *)UNCONST(av));
374 1.186 rillig execDie("exec", av[0]);
375 1.186 rillig }
376 1.81 sjg
377 1.186 rillig free(mav);
378 1.186 rillig free(bp);
379 1.186 rillig
380 1.186 rillig /* XXX: Memory management looks suspicious here. */
381 1.186 rillig /* XXX: Setting a list item to NULL is unexpected. */
382 1.186 rillig LstNode_SetNull(cmdNode);
383 1.186 rillig
384 1.186 rillig #ifdef USE_META
385 1.144 rillig if (useMeta) {
386 1.186 rillig meta_compat_parent(cpid);
387 1.186 rillig }
388 1.186 rillig #endif
389 1.186 rillig
390 1.186 rillig /*
391 1.186 rillig * The child is off and running. Now all we can do is wait...
392 1.186 rillig */
393 1.186 rillig while ((retstat = wait(&reason)) != cpid) {
394 1.186 rillig if (retstat > 0)
395 1.186 rillig JobReapChild(retstat, reason, FALSE); /* not ours? */
396 1.186 rillig if (retstat == -1 && errno != EINTR) {
397 1.186 rillig break;
398 1.186 rillig }
399 1.144 rillig }
400 1.186 rillig
401 1.186 rillig if (retstat < 0)
402 1.186 rillig Fatal("error in wait: %d: %s", retstat, strerror(errno));
403 1.186 rillig
404 1.186 rillig if (WIFSTOPPED(reason)) {
405 1.186 rillig status = WSTOPSIG(reason); /* stopped */
406 1.186 rillig } else if (WIFEXITED(reason)) {
407 1.186 rillig status = WEXITSTATUS(reason); /* exited */
408 1.186 rillig #if defined(USE_META) && defined(USE_FILEMON_ONCE)
409 1.186 rillig if (useMeta) {
410 1.186 rillig meta_cmd_finish(NULL);
411 1.186 rillig }
412 1.109 maxv #endif
413 1.186 rillig if (status != 0) {
414 1.186 rillig if (DEBUG(ERROR))
415 1.186 rillig DebugFailedTarget(cmd, gn);
416 1.186 rillig printf("*** Error code %d", status);
417 1.186 rillig }
418 1.186 rillig } else {
419 1.186 rillig status = WTERMSIG(reason); /* signaled */
420 1.186 rillig printf("*** Signal %d", status);
421 1.186 rillig }
422 1.14 christos
423 1.1 cgd
424 1.186 rillig if (!WIFEXITED(reason) || status != 0) {
425 1.186 rillig if (errCheck) {
426 1.81 sjg #ifdef USE_META
427 1.186 rillig if (useMeta) {
428 1.205 rillig meta_job_error(NULL, gn, FALSE, status);
429 1.186 rillig }
430 1.81 sjg #endif
431 1.186 rillig gn->made = ERROR;
432 1.186 rillig if (opts.keepgoing) {
433 1.186 rillig /*
434 1.186 rillig * Abort the current target,
435 1.186 rillig * but let others continue.
436 1.186 rillig */
437 1.186 rillig printf(" (continuing)\n");
438 1.186 rillig } else {
439 1.186 rillig printf("\n");
440 1.186 rillig }
441 1.186 rillig if (deleteOnError)
442 1.186 rillig CompatDeleteTarget(gn);
443 1.186 rillig } else {
444 1.186 rillig /*
445 1.186 rillig * Continue executing commands for this target.
446 1.186 rillig * If we return 0, this will happen...
447 1.186 rillig */
448 1.186 rillig printf(" (ignored)\n");
449 1.186 rillig status = 0;
450 1.186 rillig }
451 1.186 rillig }
452 1.186 rillig
453 1.186 rillig free(cmdStart);
454 1.186 rillig compatChild = 0;
455 1.187 rillig if (compatSigno != 0) {
456 1.186 rillig bmake_signal(compatSigno, SIG_DFL);
457 1.186 rillig kill(myPid, compatSigno);
458 1.186 rillig }
459 1.113 rillig
460 1.186 rillig return status;
461 1.1 cgd }
462 1.120 rillig
463 1.155 rillig static void
464 1.155 rillig RunCommands(GNode *gn)
465 1.142 rillig {
466 1.186 rillig StringListNode *ln;
467 1.186 rillig
468 1.193 rillig for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
469 1.186 rillig const char *cmd = ln->datum;
470 1.186 rillig if (Compat_RunCommand(cmd, gn) != 0)
471 1.186 rillig break;
472 1.186 rillig }
473 1.142 rillig }
474 1.142 rillig
475 1.152 rillig static void
476 1.152 rillig MakeNodes(GNodeList *gnodes, GNode *pgn)
477 1.143 rillig {
478 1.186 rillig GNodeListNode *ln;
479 1.186 rillig
480 1.186 rillig for (ln = gnodes->first; ln != NULL; ln = ln->next) {
481 1.186 rillig GNode *cohort = ln->datum;
482 1.186 rillig Compat_Make(cohort, pgn);
483 1.186 rillig }
484 1.143 rillig }
485 1.143 rillig
486 1.185 rillig static Boolean
487 1.207 rillig MakeUnmade(GNode *gn, GNode *pgn)
488 1.1 cgd {
489 1.190 rillig
490 1.190 rillig assert(gn->made == UNMADE);
491 1.190 rillig
492 1.1 cgd /*
493 1.1 cgd * First mark ourselves to be made, then apply whatever transformations
494 1.1 cgd * the suffix module thinks are necessary. Once that's done, we can
495 1.1 cgd * descend and make all our children. If any of them has an error
496 1.179 rillig * but the -k flag was given, our 'make' field will be set to FALSE
497 1.179 rillig * again. This is our signal to not attempt to do anything but abort
498 1.179 rillig * our parent as well.
499 1.1 cgd */
500 1.26 christos gn->flags |= REMAKE;
501 1.1 cgd gn->made = BEINGMADE;
502 1.190 rillig
503 1.165 rillig if (!(gn->type & OP_MADE))
504 1.185 rillig Suff_FindDeps(gn);
505 1.190 rillig
506 1.194 rillig MakeNodes(&gn->children, gn);
507 1.190 rillig
508 1.165 rillig if (!(gn->flags & REMAKE)) {
509 1.185 rillig gn->made = ABORTED;
510 1.185 rillig pgn->flags &= ~(unsigned)REMAKE;
511 1.185 rillig return FALSE;
512 1.1 cgd }
513 1.1 cgd
514 1.196 rillig if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL)
515 1.185 rillig Var_Set(IMPSRC, GNode_VarTarget(gn), pgn);
516 1.14 christos
517 1.1 cgd /*
518 1.167 rillig * All the children were made ok. Now youngestChild->mtime contains the
519 1.82 christos * modification time of the newest child, we need to find out if we
520 1.82 christos * exist and when we were modified last. The criteria for datedness
521 1.181 rillig * are defined by GNode_IsOODate.
522 1.1 cgd */
523 1.157 rillig DEBUG1(MAKE, "Examining %s...", gn->name);
524 1.181 rillig if (!GNode_IsOODate(gn)) {
525 1.185 rillig gn->made = UPTODATE;
526 1.185 rillig DEBUG0(MAKE, "up-to-date.\n");
527 1.185 rillig return FALSE;
528 1.190 rillig }
529 1.1 cgd
530 1.1 cgd /*
531 1.1 cgd * If the user is just seeing if something is out-of-date, exit now
532 1.1 cgd * to tell him/her "yes".
533 1.1 cgd */
534 1.190 rillig DEBUG0(MAKE, "out-of-date.\n");
535 1.183 rillig if (opts.queryFlag)
536 1.185 rillig exit(1);
537 1.1 cgd
538 1.1 cgd /*
539 1.190 rillig * We need to be re-made.
540 1.190 rillig * Ensure that $? (.OODATE) and $> (.ALLSRC) are both set.
541 1.1 cgd */
542 1.1 cgd Make_DoAllVar(gn);
543 1.14 christos
544 1.1 cgd /*
545 1.1 cgd * Alter our type to tell if errors should be ignored or things
546 1.213 christos * should not be printed so Compat_RunCommand knows what to do.
547 1.1 cgd */
548 1.206 rillig if (opts.ignoreErrors)
549 1.185 rillig gn->type |= OP_IGNORE;
550 1.206 rillig if (opts.beSilent)
551 1.185 rillig gn->type |= OP_SILENT;
552 1.1 cgd
553 1.56 christos if (Job_CheckCommands(gn, Fatal)) {
554 1.185 rillig /*
555 1.185 rillig * Our commands are ok, but we still have to worry about
556 1.185 rillig * the -t flag.
557 1.185 rillig */
558 1.185 rillig if (!opts.touchFlag || (gn->type & OP_MAKE)) {
559 1.185 rillig curTarg = gn;
560 1.81 sjg #ifdef USE_META
561 1.185 rillig if (useMeta && GNode_ShouldExecute(gn)) {
562 1.185 rillig meta_job_start(NULL, gn);
563 1.185 rillig }
564 1.185 rillig #endif
565 1.185 rillig RunCommands(gn);
566 1.185 rillig curTarg = NULL;
567 1.185 rillig } else {
568 1.185 rillig Job_Touch(gn, (gn->type & OP_SILENT) != 0);
569 1.81 sjg }
570 1.1 cgd } else {
571 1.185 rillig gn->made = ERROR;
572 1.1 cgd }
573 1.81 sjg #ifdef USE_META
574 1.173 rillig if (useMeta && GNode_ShouldExecute(gn)) {
575 1.185 rillig if (meta_job_finish(NULL) != 0)
576 1.185 rillig gn->made = ERROR;
577 1.81 sjg }
578 1.81 sjg #endif
579 1.1 cgd
580 1.1 cgd if (gn->made != ERROR) {
581 1.185 rillig /*
582 1.185 rillig * If the node was made successfully, mark it so, update
583 1.185 rillig * its modification time and timestamp all its parents.
584 1.185 rillig * This is to keep its state from affecting that of its parent.
585 1.185 rillig */
586 1.185 rillig gn->made = MADE;
587 1.185 rillig if (Make_Recheck(gn) == 0)
588 1.185 rillig pgn->flags |= FORCE;
589 1.185 rillig if (!(gn->type & OP_EXEC)) {
590 1.185 rillig pgn->flags |= CHILDMADE;
591 1.185 rillig GNode_UpdateYoungestChild(pgn, gn);
592 1.185 rillig }
593 1.169 rillig } else if (opts.keepgoing) {
594 1.185 rillig pgn->flags &= ~(unsigned)REMAKE;
595 1.1 cgd } else {
596 1.185 rillig PrintOnError(gn, "\nStop.");
597 1.185 rillig exit(1);
598 1.1 cgd }
599 1.185 rillig return TRUE;
600 1.185 rillig }
601 1.185 rillig
602 1.185 rillig static void
603 1.185 rillig MakeOther(GNode *gn, GNode *pgn)
604 1.185 rillig {
605 1.185 rillig
606 1.196 rillig if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL) {
607 1.185 rillig const char *target = GNode_VarTarget(gn);
608 1.185 rillig Var_Set(IMPSRC, target != NULL ? target : "", pgn);
609 1.96 joerg }
610 1.185 rillig
611 1.186 rillig switch (gn->made) {
612 1.185 rillig case BEINGMADE:
613 1.42 christos Error("Graph cycles through %s", gn->name);
614 1.1 cgd gn->made = ERROR;
615 1.129 rillig pgn->flags &= ~(unsigned)REMAKE;
616 1.1 cgd break;
617 1.185 rillig case MADE:
618 1.175 rillig if (!(gn->type & OP_EXEC)) {
619 1.185 rillig pgn->flags |= CHILDMADE;
620 1.185 rillig GNode_UpdateYoungestChild(pgn, gn);
621 1.1 cgd }
622 1.1 cgd break;
623 1.185 rillig case UPTODATE:
624 1.175 rillig if (!(gn->type & OP_EXEC))
625 1.185 rillig GNode_UpdateYoungestChild(pgn, gn);
626 1.1 cgd break;
627 1.185 rillig default:
628 1.5 cgd break;
629 1.1 cgd }
630 1.185 rillig }
631 1.185 rillig
632 1.185 rillig /* Make a target.
633 1.185 rillig *
634 1.185 rillig * If an error is detected and not being ignored, the process exits.
635 1.185 rillig *
636 1.185 rillig * Input:
637 1.185 rillig * gn The node to make
638 1.185 rillig * pgn Parent to abort if necessary
639 1.189 rillig *
640 1.189 rillig * Output:
641 1.189 rillig * gn->made
642 1.189 rillig * UPTODATE gn was already up-to-date.
643 1.189 rillig * MADE gn was recreated successfully.
644 1.189 rillig * ERROR An error occurred while gn was being created,
645 1.189 rillig * either due to missing commands or in -k mode.
646 1.189 rillig * ABORTED gn was not remade because one of its
647 1.189 rillig * dependencies could not be made due to errors.
648 1.185 rillig */
649 1.185 rillig void
650 1.185 rillig Compat_Make(GNode *gn, GNode *pgn)
651 1.185 rillig {
652 1.186 rillig if (shellName == NULL) /* we came here from jobs */
653 1.186 rillig Shell_Init();
654 1.185 rillig
655 1.186 rillig if (gn->made == UNMADE && (gn == pgn || !(pgn->type & OP_MADE))) {
656 1.186 rillig if (!MakeUnmade(gn, pgn))
657 1.186 rillig goto cohorts;
658 1.191 rillig
659 1.191 rillig /* XXX: Replace with GNode_IsError(gn) */
660 1.186 rillig } else if (gn->made == ERROR) {
661 1.186 rillig /*
662 1.186 rillig * Already had an error when making this.
663 1.186 rillig * Tell the parent to abort.
664 1.186 rillig */
665 1.186 rillig pgn->flags &= ~(unsigned)REMAKE;
666 1.186 rillig } else {
667 1.186 rillig MakeOther(gn, pgn);
668 1.186 rillig }
669 1.1 cgd
670 1.28 mycroft cohorts:
671 1.195 rillig MakeNodes(&gn->cohorts, pgn);
672 1.1 cgd }
673 1.120 rillig
674 1.210 rillig static void
675 1.210 rillig MakeBeginNode(void)
676 1.210 rillig {
677 1.210 rillig GNode *gn = Targ_FindNode(".BEGIN");
678 1.210 rillig if (gn == NULL)
679 1.210 rillig return;
680 1.210 rillig
681 1.210 rillig Compat_Make(gn, gn);
682 1.210 rillig if (GNode_IsError(gn)) {
683 1.210 rillig PrintOnError(gn, "\nStop.");
684 1.210 rillig exit(1);
685 1.210 rillig }
686 1.210 rillig }
687 1.210 rillig
688 1.211 rillig static void
689 1.211 rillig InitSignals(void)
690 1.211 rillig {
691 1.211 rillig if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN)
692 1.211 rillig bmake_signal(SIGINT, CompatInterrupt);
693 1.211 rillig if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN)
694 1.211 rillig bmake_signal(SIGTERM, CompatInterrupt);
695 1.211 rillig if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN)
696 1.211 rillig bmake_signal(SIGHUP, CompatInterrupt);
697 1.211 rillig if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
698 1.211 rillig bmake_signal(SIGQUIT, CompatInterrupt);
699 1.211 rillig }
700 1.211 rillig
701 1.134 rillig /* Initialize this module and start making.
702 1.1 cgd *
703 1.44 wiz * Input:
704 1.134 rillig * targs The target nodes to re-create
705 1.1 cgd */
706 1.1 cgd void
707 1.147 rillig Compat_Run(GNodeList *targs)
708 1.1 cgd {
709 1.186 rillig GNode *gn = NULL; /* Current root target */
710 1.202 rillig Boolean seenError;
711 1.186 rillig
712 1.186 rillig if (!shellName)
713 1.186 rillig Shell_Init();
714 1.186 rillig
715 1.211 rillig InitSignals();
716 1.186 rillig
717 1.186 rillig /* Create the .END node now, to keep the (debug) output of the
718 1.186 rillig * counter.mk test the same as before 2020-09-23. This implementation
719 1.186 rillig * detail probably doesn't matter though. */
720 1.186 rillig (void)Targ_GetEndNode();
721 1.186 rillig
722 1.210 rillig if (!opts.queryFlag)
723 1.210 rillig MakeBeginNode();
724 1.186 rillig
725 1.186 rillig /*
726 1.186 rillig * Expand .USE nodes right now, because they can modify the structure
727 1.186 rillig * of the tree.
728 1.186 rillig */
729 1.186 rillig Make_ExpandUse(targs);
730 1.46 sjg
731 1.202 rillig seenError = FALSE;
732 1.186 rillig while (!Lst_IsEmpty(targs)) {
733 1.186 rillig gn = Lst_Dequeue(targs);
734 1.186 rillig Compat_Make(gn, gn);
735 1.1 cgd
736 1.186 rillig if (gn->made == UPTODATE) {
737 1.186 rillig printf("`%s' is up to date.\n", gn->name);
738 1.186 rillig } else if (gn->made == ABORTED) {
739 1.186 rillig printf("`%s' not remade because of errors.\n",
740 1.186 rillig gn->name);
741 1.204 rillig }
742 1.212 rillig if (GNode_IsError(gn)) {
743 1.202 rillig seenError = TRUE;
744 1.212 rillig /* XXX: In case of error, set the error node. */
745 1.212 rillig }
746 1.1 cgd }
747 1.15 christos
748 1.204 rillig /* If the user has defined a .END target, run its commands. */
749 1.202 rillig if (!seenError) {
750 1.186 rillig GNode *endNode = Targ_GetEndNode();
751 1.186 rillig Compat_Make(endNode, endNode);
752 1.202 rillig seenError = GNode_IsError(endNode);
753 1.212 rillig /* XXX: In case of error, set the error node. */
754 1.202 rillig }
755 1.198 rillig
756 1.202 rillig if (seenError) {
757 1.212 rillig /*
758 1.212 rillig * XXX: Instead of gn, it makes more sense to report the
759 1.212 rillig * first error node.
760 1.212 rillig */
761 1.198 rillig PrintOnError(gn, "\nStop.");
762 1.198 rillig exit(1);
763 1.36 sjg }
764 1.1 cgd }
765