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