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