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