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