Home | History | Annotate | Line # | Download | only in make
compat.c revision 1.155
      1  1.155    rillig /*	$NetBSD: compat.c,v 1.155 2020/09/27 11:43:46 rillig Exp $	*/
      2   1.10  christos 
      3    1.1       cgd /*
      4    1.1       cgd  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5   1.47       agc  * All rights reserved.
      6   1.47       agc  *
      7   1.47       agc  * This code is derived from software contributed to Berkeley by
      8   1.47       agc  * Adam de Boor.
      9   1.47       agc  *
     10   1.47       agc  * Redistribution and use in source and binary forms, with or without
     11   1.47       agc  * modification, are permitted provided that the following conditions
     12   1.47       agc  * are met:
     13   1.47       agc  * 1. Redistributions of source code must retain the above copyright
     14   1.47       agc  *    notice, this list of conditions and the following disclaimer.
     15   1.47       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.47       agc  *    notice, this list of conditions and the following disclaimer in the
     17   1.47       agc  *    documentation and/or other materials provided with the distribution.
     18   1.47       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.47       agc  *    may be used to endorse or promote products derived from this software
     20   1.47       agc  *    without specific prior written permission.
     21   1.47       agc  *
     22   1.47       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.47       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.47       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.47       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.47       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.47       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.47       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.47       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.47       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.47       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.47       agc  * SUCH DAMAGE.
     33   1.47       agc  */
     34   1.47       agc 
     35   1.47       agc /*
     36    1.1       cgd  * Copyright (c) 1988, 1989 by Adam de Boor
     37    1.1       cgd  * Copyright (c) 1989 by Berkeley Softworks
     38    1.1       cgd  * All rights reserved.
     39    1.1       cgd  *
     40    1.1       cgd  * This code is derived from software contributed to Berkeley by
     41    1.1       cgd  * Adam de Boor.
     42    1.1       cgd  *
     43    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44    1.1       cgd  * modification, are permitted provided that the following conditions
     45    1.1       cgd  * are met:
     46    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     52    1.1       cgd  *    must display the following acknowledgement:
     53    1.1       cgd  *	This product includes software developed by the University of
     54    1.1       cgd  *	California, Berkeley and its contributors.
     55    1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     56    1.1       cgd  *    may be used to endorse or promote products derived from this software
     57    1.1       cgd  *    without specific prior written permission.
     58    1.1       cgd  *
     59    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69    1.1       cgd  * SUCH DAMAGE.
     70    1.1       cgd  */
     71    1.1       cgd 
     72    1.1       cgd /*-
     73    1.1       cgd  * compat.c --
     74    1.1       cgd  *	The routines in this file implement the full-compatibility
     75    1.1       cgd  *	mode of PMake. Most of the special functionality of PMake
     76    1.1       cgd  *	is available in this mode. Things not supported:
     77    1.1       cgd  *	    - different shells.
     78    1.1       cgd  *	    - friendly variable substitution.
     79    1.1       cgd  *
     80    1.1       cgd  * Interface:
     81    1.1       cgd  *	Compat_Run	    Initialize things for this module and recreate
     82    1.1       cgd  *	    	  	    thems as need creatin'
     83    1.1       cgd  */
     84    1.1       cgd 
     85    1.1       cgd #include    <sys/types.h>
     86   1.11       jtc #include    <sys/stat.h>
     87    1.1       cgd #include    <sys/wait.h>
     88   1.44       wiz 
     89    1.1       cgd #include    <ctype.h>
     90   1.11       jtc #include    <errno.h>
     91   1.11       jtc #include    <signal.h>
     92   1.44       wiz #include    <stdio.h>
     93   1.44       wiz 
     94    1.1       cgd #include    "make.h"
     95    1.5       cgd #include    "hash.h"
     96    1.5       cgd #include    "dir.h"
     97    1.5       cgd #include    "job.h"
     98   1.97  christos #include    "metachar.h"
     99   1.43     bjh21 #include    "pathnames.h"
    100    1.1       cgd 
    101  1.145    rillig /*	"@(#)compat.c	8.2 (Berkeley) 3/19/94"	*/
    102  1.155    rillig MAKE_RCSID("$NetBSD: compat.c,v 1.155 2020/09/27 11:43:46 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.1       cgd     char    	  *cmdStart;	/* Start of expanded command */
    191  1.153    rillig     char 	  *bp;
    192    1.1       cgd     Boolean 	  silent,   	/* Don't print command */
    193  1.118    rillig 		  doIt;		/* Execute even if -n */
    194   1.64  christos     volatile Boolean errCheck; 	/* Check errors */
    195   1.13  christos     int 	  reason;   	/* Reason for child's death */
    196    1.1       cgd     int	    	  status;   	/* Description of child's death */
    197   1.76  dholland     pid_t	  cpid;	    	/* Child actually found */
    198   1.76  dholland     pid_t	  retstat;    	/* Result of wait */
    199  1.146    rillig     StringListNode *cmdNode;  	/* Node where current command is located */
    200   1.64  christos     const char  ** volatile av;	/* Argument vector for thing to exec */
    201   1.64  christos     char	** volatile mav;/* Copy of the argument vector for freeing */
    202   1.62       apb     Boolean 	  useShell;    	/* TRUE if command should be executed
    203   1.62       apb 				 * using a shell */
    204  1.153    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.74       dsl     if (DEBUG(JOB))
    305   1.74       dsl 	fprintf(debug_file, "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.92       sjg 	int shargc;
    314    1.1       cgd 
    315   1.92       sjg 	shargc = 0;
    316   1.92       sjg 	shargv[shargc++] = shellPath;
    317   1.46       sjg 	/*
    318   1.46       sjg 	 * The following work for any of the builtin shell specs.
    319   1.46       sjg 	 */
    320   1.93       sjg 	if (errCheck && shellErrFlag) {
    321   1.92       sjg 	    shargv[shargc++] = shellErrFlag;
    322   1.92       sjg 	}
    323   1.34       sjg 	if (DEBUG(SHELL))
    324   1.92       sjg 		shargv[shargc++] = "-xc";
    325   1.34       sjg 	else
    326   1.92       sjg 		shargv[shargc++] = "-c";
    327   1.92       sjg 	shargv[shargc++] = cmd;
    328  1.138    rillig 	shargv[shargc] = NULL;
    329    1.1       cgd 	av = shargv;
    330   1.24  christos 	bp = NULL;
    331   1.61  christos 	mav = NULL;
    332    1.1       cgd     } else {
    333    1.1       cgd 	/*
    334    1.1       cgd 	 * No meta-characters, so no need to exec a shell. Break the command
    335    1.1       cgd 	 * into words to form an argument vector we can execute.
    336    1.1       cgd 	 */
    337  1.139    rillig 	Words words = Str_Words(cmd, FALSE);
    338  1.138    rillig 	mav = words.words;
    339  1.138    rillig 	bp = words.freeIt;
    340   1.83  christos 	av = (void *)mav;
    341    1.1       cgd     }
    342   1.14  christos 
    343   1.81       sjg #ifdef USE_META
    344   1.81       sjg     if (useMeta) {
    345   1.81       sjg 	meta_compat_start();
    346   1.81       sjg     }
    347   1.81       sjg #endif
    348  1.113    rillig 
    349    1.1       cgd     /*
    350    1.1       cgd      * Fork and execute the single command. If the fork fails, we abort.
    351    1.1       cgd      */
    352  1.107       sjg     compatChild = cpid = vFork();
    353    1.1       cgd     if (cpid < 0) {
    354    1.1       cgd 	Fatal("Could not fork");
    355    1.1       cgd     }
    356    1.1       cgd     if (cpid == 0) {
    357   1.68       sjg 	Var_ExportVars();
    358   1.81       sjg #ifdef USE_META
    359   1.81       sjg 	if (useMeta) {
    360   1.81       sjg 	    meta_compat_child();
    361   1.81       sjg 	}
    362   1.81       sjg #endif
    363  1.121    rillig 	(void)execvp(av[0], (char *const *)UNCONST(av));
    364   1.41        pk 	execError("exec", av[0]);
    365   1.23   thorpej 	_exit(1);
    366    1.1       cgd     }
    367  1.103  christos 
    368  1.103  christos     free(mav);
    369  1.103  christos     free(bp);
    370  1.103  christos 
    371  1.127    rillig     /* XXX: Memory management looks suspicious here. */
    372  1.127    rillig     /* XXX: Setting a list item to NULL is unexpected. */
    373  1.132    rillig     LstNode_SetNull(cmdNode);
    374   1.14  christos 
    375   1.81       sjg #ifdef USE_META
    376   1.81       sjg     if (useMeta) {
    377  1.110  riastrad 	meta_compat_parent(cpid);
    378   1.81       sjg     }
    379   1.81       sjg #endif
    380   1.81       sjg 
    381    1.1       cgd     /*
    382    1.1       cgd      * The child is off and running. Now all we can do is wait...
    383    1.1       cgd      */
    384  1.144    rillig     while ((retstat = wait(&reason)) != cpid) {
    385  1.144    rillig 	if (retstat > 0)
    386  1.144    rillig 	    JobReapChild(retstat, reason, FALSE); /* not ours? */
    387  1.144    rillig 	if (retstat == -1 && errno != EINTR) {
    388  1.144    rillig 	    break;
    389  1.144    rillig 	}
    390  1.144    rillig     }
    391    1.1       cgd 
    392  1.144    rillig     if (retstat < 0)
    393  1.144    rillig 	Fatal("error in wait: %d: %s", retstat, strerror(errno));
    394   1.14  christos 
    395  1.144    rillig     if (WIFSTOPPED(reason)) {
    396  1.144    rillig 	status = WSTOPSIG(reason);		/* stopped */
    397  1.144    rillig     } else if (WIFEXITED(reason)) {
    398  1.144    rillig 	status = WEXITSTATUS(reason);		/* exited */
    399  1.109      maxv #if defined(USE_META) && defined(USE_FILEMON_ONCE)
    400  1.144    rillig 	if (useMeta) {
    401  1.144    rillig 	    meta_cmd_finish(NULL);
    402  1.144    rillig 	}
    403  1.109      maxv #endif
    404  1.144    rillig 	if (status != 0) {
    405  1.144    rillig 	    if (DEBUG(ERROR)) {
    406  1.153    rillig 		const char *cp;
    407  1.144    rillig 		fprintf(debug_file, "\n*** Failed target:  %s\n*** Failed command: ",
    408  1.144    rillig 		    gn->name);
    409  1.144    rillig 		for (cp = cmd; *cp; ) {
    410  1.144    rillig 		    if (ch_isspace(*cp)) {
    411  1.144    rillig 			fprintf(debug_file, " ");
    412  1.144    rillig 			while (ch_isspace(*cp))
    413  1.144    rillig 			    cp++;
    414  1.144    rillig 		    } else {
    415  1.144    rillig 			fprintf(debug_file, "%c", *cp);
    416  1.144    rillig 			cp++;
    417   1.51     lukem 		    }
    418    1.1       cgd 		}
    419  1.144    rillig 		fprintf(debug_file, "\n");
    420   1.14  christos 	    }
    421  1.144    rillig 	    printf("*** Error code %d", status);
    422  1.144    rillig 	}
    423  1.144    rillig     } else {
    424  1.144    rillig 	status = WTERMSIG(reason);		/* signaled */
    425  1.144    rillig 	printf("*** Signal %d", status);
    426  1.144    rillig     }
    427   1.14  christos 
    428    1.1       cgd 
    429  1.144    rillig     if (!WIFEXITED(reason) || (status != 0)) {
    430  1.144    rillig 	if (errCheck) {
    431   1.81       sjg #ifdef USE_META
    432  1.144    rillig 	    if (useMeta) {
    433  1.144    rillig 		meta_job_error(NULL, gn, 0, status);
    434  1.144    rillig 	    }
    435   1.81       sjg #endif
    436  1.144    rillig 	    gn->made = ERROR;
    437  1.144    rillig 	    if (keepgoing) {
    438  1.144    rillig 		/*
    439  1.144    rillig 		 * Abort the current target, but let others
    440  1.144    rillig 		 * continue.
    441  1.144    rillig 		 */
    442  1.144    rillig 		printf(" (continuing)\n");
    443  1.144    rillig 	    } else {
    444  1.144    rillig 		printf("\n");
    445  1.144    rillig 	    }
    446  1.144    rillig 	    if (deleteOnError) {
    447  1.144    rillig 		    CompatDeleteTarget(gn);
    448    1.1       cgd 	    }
    449    1.1       cgd 	} else {
    450  1.144    rillig 	    /*
    451  1.144    rillig 	     * Continue executing commands for this target.
    452  1.144    rillig 	     * If we return 0, this will happen...
    453  1.144    rillig 	     */
    454  1.144    rillig 	    printf(" (ignored)\n");
    455  1.144    rillig 	    status = 0;
    456    1.1       cgd 	}
    457    1.1       cgd     }
    458  1.144    rillig 
    459   1.50     lukem     free(cmdStart);
    460  1.107       sjg     compatChild = 0;
    461  1.107       sjg     if (compatSigno) {
    462  1.107       sjg 	bmake_signal(compatSigno, SIG_DFL);
    463  1.107       sjg 	kill(myPid, compatSigno);
    464  1.107       sjg     }
    465  1.113    rillig 
    466  1.112    rillig     return status;
    467    1.1       cgd }
    468  1.120    rillig 
    469  1.155    rillig static void
    470  1.155    rillig RunCommands(GNode *gn)
    471  1.142    rillig {
    472  1.155    rillig     StringListNode *ln;
    473  1.155    rillig     for (ln = gn->commands->first; ln != NULL; ln = ln->next) {
    474  1.155    rillig 	const char *cmd = ln->datum;
    475  1.155    rillig 	if (Compat_RunCommand(cmd, gn) != 0)
    476  1.155    rillig 	    break;
    477  1.155    rillig     }
    478  1.142    rillig }
    479  1.142    rillig 
    480  1.152    rillig static void
    481  1.152    rillig MakeNodes(GNodeList *gnodes, GNode *pgn)
    482  1.143    rillig {
    483  1.154    rillig     GNodeListNode *ln;
    484  1.154    rillig     for (ln = gnodes->first; ln != NULL; ln = ln->next) {
    485  1.154    rillig 	GNode *cohort = ln->datum;
    486  1.152    rillig 	Compat_Make(cohort, pgn);
    487  1.152    rillig     }
    488  1.143    rillig }
    489  1.143    rillig 
    490  1.152    rillig /* Make a target.
    491  1.152    rillig  *
    492  1.152    rillig  * If an error is detected and not being ignored, the process exits.
    493    1.1       cgd  *
    494   1.44       wiz  * Input:
    495  1.152    rillig  *	gn		The node to make
    496  1.152    rillig  *	pgn		Parent to abort if necessary
    497    1.1       cgd  */
    498  1.152    rillig void
    499  1.143    rillig Compat_Make(GNode *gn, GNode *pgn)
    500    1.1       cgd {
    501   1.97  christos     if (!shellName)		/* we came here from jobs */
    502   1.97  christos 	Shell_Init();
    503   1.40        pk     if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) {
    504    1.1       cgd 	/*
    505    1.1       cgd 	 * First mark ourselves to be made, then apply whatever transformations
    506    1.1       cgd 	 * the suffix module thinks are necessary. Once that's done, we can
    507    1.1       cgd 	 * descend and make all our children. If any of them has an error
    508    1.1       cgd 	 * but the -k flag was given, our 'make' field will be set FALSE again.
    509    1.1       cgd 	 * This is our signal to not attempt to do anything but abort our
    510    1.1       cgd 	 * parent as well.
    511    1.1       cgd 	 */
    512   1.26  christos 	gn->flags |= REMAKE;
    513    1.1       cgd 	gn->made = BEINGMADE;
    514   1.38        pk 	if ((gn->type & OP_MADE) == 0)
    515   1.56  christos 	    Suff_FindDeps(gn);
    516  1.152    rillig 	MakeNodes(gn->children, gn);
    517   1.26  christos 	if ((gn->flags & REMAKE) == 0) {
    518    1.1       cgd 	    gn->made = ABORTED;
    519  1.129    rillig 	    pgn->flags &= ~(unsigned)REMAKE;
    520   1.28   mycroft 	    goto cohorts;
    521    1.1       cgd 	}
    522    1.1       cgd 
    523  1.137    rillig 	if (Lst_FindDatum(gn->implicitParents, pgn) != NULL) {
    524   1.96     joerg 	    char *p1;
    525  1.111    rillig 	    Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
    526  1.117    rillig 	    bmake_free(p1);
    527   1.96     joerg 	}
    528   1.14  christos 
    529    1.1       cgd 	/*
    530   1.82  christos 	 * All the children were made ok. Now cmgn->mtime contains the
    531   1.82  christos 	 * modification time of the newest child, we need to find out if we
    532   1.82  christos 	 * exist and when we were modified last. The criteria for datedness
    533   1.82  christos 	 * are defined by the Make_OODate function.
    534    1.1       cgd 	 */
    535    1.1       cgd 	if (DEBUG(MAKE)) {
    536   1.63       dsl 	    fprintf(debug_file, "Examining %s...", gn->name);
    537    1.1       cgd 	}
    538    1.1       cgd 	if (! Make_OODate(gn)) {
    539    1.1       cgd 	    gn->made = UPTODATE;
    540    1.1       cgd 	    if (DEBUG(MAKE)) {
    541   1.63       dsl 		fprintf(debug_file, "up-to-date.\n");
    542    1.1       cgd 	    }
    543   1.28   mycroft 	    goto cohorts;
    544    1.1       cgd 	} else if (DEBUG(MAKE)) {
    545   1.63       dsl 	    fprintf(debug_file, "out-of-date.\n");
    546    1.1       cgd 	}
    547    1.1       cgd 
    548    1.1       cgd 	/*
    549    1.1       cgd 	 * If the user is just seeing if something is out-of-date, exit now
    550    1.1       cgd 	 * to tell him/her "yes".
    551    1.1       cgd 	 */
    552    1.1       cgd 	if (queryFlag) {
    553   1.59  christos 	    exit(1);
    554    1.1       cgd 	}
    555    1.1       cgd 
    556    1.1       cgd 	/*
    557    1.1       cgd 	 * We need to be re-made. We also have to make sure we've got a $?
    558    1.1       cgd 	 * variable. To be nice, we also define the $> variable using
    559    1.1       cgd 	 * Make_DoAllVar().
    560    1.1       cgd 	 */
    561    1.1       cgd 	Make_DoAllVar(gn);
    562   1.14  christos 
    563    1.1       cgd 	/*
    564    1.1       cgd 	 * Alter our type to tell if errors should be ignored or things
    565    1.1       cgd 	 * should not be printed so CompatRunCommand knows what to do.
    566    1.1       cgd 	 */
    567   1.56  christos 	if (Targ_Ignore(gn)) {
    568    1.1       cgd 	    gn->type |= OP_IGNORE;
    569    1.1       cgd 	}
    570   1.56  christos 	if (Targ_Silent(gn)) {
    571    1.1       cgd 	    gn->type |= OP_SILENT;
    572    1.1       cgd 	}
    573    1.1       cgd 
    574   1.56  christos 	if (Job_CheckCommands(gn, Fatal)) {
    575    1.1       cgd 	    /*
    576    1.1       cgd 	     * Our commands are ok, but we still have to worry about the -t
    577    1.1       cgd 	     * flag...
    578    1.1       cgd 	     */
    579   1.20   mycroft 	    if (!touchFlag || (gn->type & OP_MAKE)) {
    580    1.1       cgd 		curTarg = gn;
    581   1.81       sjg #ifdef USE_META
    582   1.81       sjg 		if (useMeta && !NoExecute(gn)) {
    583   1.81       sjg 		    meta_job_start(NULL, gn);
    584   1.81       sjg 		}
    585   1.81       sjg #endif
    586  1.155    rillig 		RunCommands(gn);
    587   1.72       dsl 		curTarg = NULL;
    588    1.1       cgd 	    } else {
    589  1.133    rillig 		Job_Touch(gn, (gn->type & OP_SILENT) != 0);
    590    1.1       cgd 	    }
    591    1.1       cgd 	} else {
    592    1.1       cgd 	    gn->made = ERROR;
    593    1.1       cgd 	}
    594   1.81       sjg #ifdef USE_META
    595   1.81       sjg 	if (useMeta && !NoExecute(gn)) {
    596  1.105       sjg 	    if (meta_job_finish(NULL) != 0)
    597  1.105       sjg 		gn->made = ERROR;
    598   1.81       sjg 	}
    599   1.81       sjg #endif
    600    1.1       cgd 
    601    1.1       cgd 	if (gn->made != ERROR) {
    602    1.1       cgd 	    /*
    603    1.1       cgd 	     * If the node was made successfully, mark it so, update
    604    1.1       cgd 	     * its modification time and timestamp all its parents. Note
    605    1.1       cgd 	     * that for .ZEROTIME targets, the timestamping isn't done.
    606    1.1       cgd 	     * This is to keep its state from affecting that of its parent.
    607    1.1       cgd 	     */
    608    1.1       cgd 	    gn->made = MADE;
    609   1.26  christos 	    pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
    610    1.1       cgd 	    if (!(gn->type & OP_EXEC)) {
    611   1.26  christos 		pgn->flags |= CHILDMADE;
    612    1.1       cgd 		Make_TimeStamp(pgn, gn);
    613    1.1       cgd 	    }
    614    1.1       cgd 	} else if (keepgoing) {
    615  1.129    rillig 	    pgn->flags &= ~(unsigned)REMAKE;
    616    1.1       cgd 	} else {
    617  1.106  dholland 	    PrintOnError(gn, "\nStop.");
    618   1.59  christos 	    exit(1);
    619    1.1       cgd 	}
    620    1.1       cgd     } else if (gn->made == ERROR) {
    621    1.1       cgd 	/*
    622    1.1       cgd 	 * Already had an error when making this beastie. Tell the parent
    623    1.1       cgd 	 * to abort.
    624    1.1       cgd 	 */
    625  1.129    rillig 	pgn->flags &= ~(unsigned)REMAKE;
    626    1.1       cgd     } else {
    627  1.137    rillig 	if (Lst_FindDatum(gn->implicitParents, pgn) != NULL) {
    628   1.96     joerg 	    char *p1;
    629  1.135    rillig 	    const char *target = Var_Value(TARGET, gn, &p1);
    630  1.135    rillig 	    Var_Set(IMPSRC, target != NULL ? target : "", pgn);
    631  1.117    rillig 	    bmake_free(p1);
    632   1.96     joerg 	}
    633    1.1       cgd 	switch(gn->made) {
    634    1.1       cgd 	    case BEINGMADE:
    635   1.42  christos 		Error("Graph cycles through %s", gn->name);
    636    1.1       cgd 		gn->made = ERROR;
    637  1.129    rillig 		pgn->flags &= ~(unsigned)REMAKE;
    638    1.1       cgd 		break;
    639    1.1       cgd 	    case MADE:
    640    1.1       cgd 		if ((gn->type & OP_EXEC) == 0) {
    641   1.26  christos 		    pgn->flags |= CHILDMADE;
    642    1.1       cgd 		    Make_TimeStamp(pgn, gn);
    643    1.1       cgd 		}
    644    1.1       cgd 		break;
    645    1.1       cgd 	    case UPTODATE:
    646    1.1       cgd 		if ((gn->type & OP_EXEC) == 0) {
    647    1.1       cgd 		    Make_TimeStamp(pgn, gn);
    648    1.1       cgd 		}
    649    1.1       cgd 		break;
    650    1.5       cgd 	    default:
    651    1.5       cgd 		break;
    652    1.1       cgd 	}
    653    1.1       cgd     }
    654    1.1       cgd 
    655   1.28   mycroft cohorts:
    656  1.152    rillig     MakeNodes(gn->cohorts, pgn);
    657    1.1       cgd }
    658  1.120    rillig 
    659  1.134    rillig /* Initialize this module and start making.
    660    1.1       cgd  *
    661   1.44       wiz  * Input:
    662  1.134    rillig  *	targs		The target nodes to re-create
    663    1.1       cgd  */
    664    1.1       cgd void
    665  1.147    rillig Compat_Run(GNodeList *targs)
    666    1.1       cgd {
    667    1.5       cgd     GNode   	  *gn = NULL;/* Current root target */
    668    1.1       cgd     int	    	  errors;   /* Number of targets not remade due to errors */
    669   1.46       sjg 
    670   1.97  christos     if (!shellName)
    671   1.97  christos 	Shell_Init();
    672    1.1       cgd 
    673   1.79       sjg     if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN) {
    674   1.79       sjg 	bmake_signal(SIGINT, CompatInterrupt);
    675    1.1       cgd     }
    676   1.79       sjg     if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN) {
    677   1.79       sjg 	bmake_signal(SIGTERM, CompatInterrupt);
    678    1.1       cgd     }
    679   1.79       sjg     if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN) {
    680   1.79       sjg 	bmake_signal(SIGHUP, CompatInterrupt);
    681    1.1       cgd     }
    682   1.79       sjg     if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
    683   1.79       sjg 	bmake_signal(SIGQUIT, CompatInterrupt);
    684    1.1       cgd     }
    685    1.1       cgd 
    686  1.149    rillig     /* Create the .END node now, to keep the (debug) output of the
    687  1.149    rillig      * counter.mk test the same as before 2020-09-23.  This implementation
    688  1.149    rillig      * detail probably doesn't matter though. */
    689  1.149    rillig     (void)Targ_GetEndNode();
    690    1.1       cgd     /*
    691    1.1       cgd      * If the user has defined a .BEGIN target, execute the commands attached
    692    1.1       cgd      * to it.
    693    1.1       cgd      */
    694    1.1       cgd     if (!queryFlag) {
    695  1.151    rillig 	gn = Targ_FindNode(".BEGIN");
    696   1.72       dsl 	if (gn != NULL) {
    697   1.57  christos 	    Compat_Make(gn, gn);
    698  1.118    rillig 	    if (gn->made == ERROR) {
    699  1.118    rillig 		PrintOnError(gn, "\nStop.");
    700  1.118    rillig 		exit(1);
    701  1.118    rillig 	    }
    702    1.1       cgd 	}
    703    1.1       cgd     }
    704   1.15  christos 
    705   1.15  christos     /*
    706   1.15  christos      * Expand .USE nodes right now, because they can modify the structure
    707   1.15  christos      * of the tree.
    708   1.15  christos      */
    709   1.66       dsl     Make_ExpandUse(targs);
    710    1.1       cgd 
    711    1.1       cgd     /*
    712   1.57  christos      * For each entry in the list of targets to create, call Compat_Make on
    713   1.57  christos      * it to create the thing. Compat_Make will leave the 'made' field of gn
    714    1.1       cgd      * in one of several states:
    715    1.1       cgd      *	    UPTODATE	    gn was already up-to-date
    716    1.1       cgd      *	    MADE  	    gn was recreated successfully
    717    1.1       cgd      *	    ERROR 	    An error occurred while gn was being created
    718    1.1       cgd      *	    ABORTED	    gn was not remade because one of its inferiors
    719    1.1       cgd      *	    	  	    could not be made due to errors.
    720    1.1       cgd      */
    721    1.1       cgd     errors = 0;
    722  1.132    rillig     while (!Lst_IsEmpty(targs)) {
    723  1.132    rillig 	gn = Lst_Dequeue(targs);
    724   1.57  christos 	Compat_Make(gn, gn);
    725    1.1       cgd 
    726    1.1       cgd 	if (gn->made == UPTODATE) {
    727   1.59  christos 	    printf("`%s' is up to date.\n", gn->name);
    728    1.1       cgd 	} else if (gn->made == ABORTED) {
    729   1.59  christos 	    printf("`%s' not remade because of errors.\n", gn->name);
    730    1.1       cgd 	    errors += 1;
    731    1.1       cgd 	}
    732    1.1       cgd     }
    733    1.1       cgd 
    734    1.1       cgd     /*
    735    1.1       cgd      * If the user has defined a .END target, run its commands.
    736    1.1       cgd      */
    737    1.1       cgd     if (errors == 0) {
    738  1.155    rillig 	GNode *endNode = Targ_GetEndNode();
    739  1.149    rillig 	Compat_Make(endNode, endNode);
    740  1.149    rillig 	/* XXX: Did you mean endNode->made instead of gn->made? */
    741   1.36       sjg 	if (gn->made == ERROR) {
    742  1.106  dholland 	    PrintOnError(gn, "\nStop.");
    743   1.36       sjg 	    exit(1);
    744   1.36       sjg 	}
    745    1.1       cgd     }
    746    1.1       cgd }
    747