Module Name:    src
Committed By:   sjg
Date:           Fri Mar  1 16:41:42 UTC 2024

Modified Files:
        src/usr.bin/make: compat.c job.c main.c make.h var.c

Log Message:
make: export target scope values

Pass target scope to Var_ReexportVars so that a target process
will see the correct values in its env.
We must then mark any Global scope variable as unexported
so targets without local value get the Global one.


To generate a diff of this commit:
cvs rdiff -u -r1.252 -r1.253 src/usr.bin/make/compat.c
cvs rdiff -u -r1.465 -r1.466 src/usr.bin/make/job.c
cvs rdiff -u -r1.610 -r1.611 src/usr.bin/make/main.c
cvs rdiff -u -r1.327 -r1.328 src/usr.bin/make/make.h
cvs rdiff -u -r1.1099 -r1.1100 src/usr.bin/make/var.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/compat.c
diff -u src/usr.bin/make/compat.c:1.252 src/usr.bin/make/compat.c:1.253
--- src/usr.bin/make/compat.c:1.252	Fri Jan  5 23:22:06 2024
+++ src/usr.bin/make/compat.c	Fri Mar  1 16:41:42 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: compat.c,v 1.252 2024/01/05 23:22:06 rillig Exp $	*/
+/*	$NetBSD: compat.c,v 1.253 2024/03/01 16:41:42 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -91,7 +91,7 @@
 #include "pathnames.h"
 
 /*	"@(#)compat.c	8.2 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: compat.c,v 1.252 2024/01/05 23:22:06 rillig Exp $");
+MAKE_RCSID("$NetBSD: compat.c,v 1.253 2024/03/01 16:41:42 sjg Exp $");
 
 static GNode *curTarg = NULL;
 static pid_t compatChild;
@@ -326,7 +326,7 @@ Compat_RunCommand(const char *cmdp, GNod
 		meta_compat_start();
 #endif
 
-	Var_ReexportVars();
+	Var_ReexportVars(gn);
 
 	compatChild = cpid = vfork();
 	if (cpid < 0)

Index: src/usr.bin/make/job.c
diff -u src/usr.bin/make/job.c:1.465 src/usr.bin/make/job.c:1.466
--- src/usr.bin/make/job.c:1.465	Sun Jan  7 11:39:04 2024
+++ src/usr.bin/make/job.c	Fri Mar  1 16:41:42 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: job.c,v 1.465 2024/01/07 11:39:04 rillig Exp $	*/
+/*	$NetBSD: job.c,v 1.466 2024/03/01 16:41:42 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -141,7 +141,7 @@
 #include "trace.h"
 
 /*	"@(#)job.c	8.2 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: job.c,v 1.465 2024/01/07 11:39:04 rillig Exp $");
+MAKE_RCSID("$NetBSD: job.c,v 1.466 2024/03/01 16:41:42 sjg Exp $");
 
 /*
  * A shell defines how the commands are run.  All commands for a target are
@@ -1420,7 +1420,7 @@ JobExec(Job *job, char **argv)
 	/* Pre-emptively mark job running, pid still zero though */
 	job->status = JOB_ST_RUNNING;
 
-	Var_ReexportVars();
+	Var_ReexportVars(job->node);
 
 	cpid = vfork();
 	if (cpid == -1)

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.610 src/usr.bin/make/main.c:1.611
--- src/usr.bin/make/main.c:1.610	Wed Feb  7 06:43:02 2024
+++ src/usr.bin/make/main.c	Fri Mar  1 16:41:42 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.610 2024/02/07 06:43:02 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.611 2024/03/01 16:41:42 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.610 2024/02/07 06:43:02 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.611 2024/03/01 16:41:42 sjg Exp $");
 #if defined(MAKE_NATIVE)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -1735,7 +1735,7 @@ Cmd_Exec(const char *cmd, char **error)
 		return bmake_strdup("");
 	}
 
-	Var_ReexportVars();
+	Var_ReexportVars(SCOPE_GLOBAL);
 
 	switch (cpid = vfork()) {
 	case 0:

Index: src/usr.bin/make/make.h
diff -u src/usr.bin/make/make.h:1.327 src/usr.bin/make/make.h:1.328
--- src/usr.bin/make/make.h:1.327	Sun Dec 17 09:02:26 2023
+++ src/usr.bin/make/make.h	Fri Mar  1 16:41:42 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: make.h,v 1.327 2023/12/17 09:02:26 rillig Exp $	*/
+/*	$NetBSD: make.h,v 1.328 2024/03/01 16:41:42 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -1011,7 +1011,7 @@ char *Var_Subst(const char *, GNode *, V
 void Var_Expand(FStr *, GNode *, VarEvalMode);
 void Var_Stats(void);
 void Var_Dump(GNode *);
-void Var_ReexportVars(void);
+void Var_ReexportVars(GNode *);
 void Var_Export(VarExportMode, const char *);
 void Var_ExportVars(const char *);
 void Var_UnExport(bool, const char *);

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.1099 src/usr.bin/make/var.c:1.1100
--- src/usr.bin/make/var.c:1.1099	Wed Feb  7 06:43:02 2024
+++ src/usr.bin/make/var.c	Fri Mar  1 16:41:42 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.1099 2024/02/07 06:43:02 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.1100 2024/03/01 16:41:42 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -137,7 +137,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1099 2024/02/07 06:43:02 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1100 2024/03/01 16:41:42 sjg Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -590,7 +590,7 @@ MayExport(const char *name)
 }
 
 static bool
-ExportVarEnv(Var *v)
+ExportVarEnv(Var *v, GNode *scope)
 {
 	const char *name = v->name.str;
 	char *val = v->val.data;
@@ -610,7 +610,13 @@ ExportVarEnv(Var *v)
 
 	/* XXX: name is injected without escaping it */
 	expr = str_concat3("${", name, "}");
-	val = Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES);
+	val = Var_Subst(expr, scope, VARE_WANTRES);
+	if (scope != SCOPE_GLOBAL) {
+		/* we will need to re-rexport the Global version */
+		v = VarFind(name, SCOPE_GLOBAL, false);
+		if (v)
+			v->exported = false;
+	}
 	/* TODO: handle errors */
 	setenv(name, val, 1);
 	free(val);
@@ -657,19 +663,21 @@ ExportVarLiteral(Var *v)
  * Internal variables are not exported.
  */
 static bool
-ExportVar(const char *name, VarExportMode mode)
+ExportVar(const char *name, GNode *scope, VarExportMode mode)
 {
 	Var *v;
 
 	if (!MayExport(name))
 		return false;
 
-	v = VarFind(name, SCOPE_GLOBAL, false);
+	v = VarFind(name, scope, false);
+	if (v == NULL && scope != SCOPE_GLOBAL)
+		v = VarFind(name, SCOPE_GLOBAL, false);
 	if (v == NULL)
 		return false;
 
 	if (mode == VEM_ENV)
-		return ExportVarEnv(v);
+		return ExportVarEnv(v, scope);
 	else if (mode == VEM_PLAIN)
 		return ExportVarPlain(v);
 	else
@@ -681,7 +689,7 @@ ExportVar(const char *name, VarExportMod
  * re-exported.
  */
 void
-Var_ReexportVars(void)
+Var_ReexportVars(GNode *scope)
 {
 	char *xvarnames;
 
@@ -705,7 +713,7 @@ Var_ReexportVars(void)
 		HashIter_Init(&hi, &SCOPE_GLOBAL->vars);
 		while (HashIter_Next(&hi) != NULL) {
 			Var *var = hi.entry->value;
-			ExportVar(var->name.str, VEM_ENV);
+			ExportVar(var->name.str, scope, VEM_ENV);
 		}
 		return;
 	}
@@ -718,7 +726,7 @@ Var_ReexportVars(void)
 		size_t i;
 
 		for (i = 0; i < varnames.len; i++)
-			ExportVar(varnames.words[i], VEM_ENV);
+			ExportVar(varnames.words[i], scope, VEM_ENV);
 		Words_Free(varnames);
 	}
 	free(xvarnames);
@@ -736,7 +744,7 @@ ExportVars(const char *varnames, bool is
 
 	for (i = 0; i < words.len; i++) {
 		const char *varname = words.words[i];
-		if (!ExportVar(varname, mode))
+		if (!ExportVar(varname, SCOPE_GLOBAL, mode))
 			continue;
 
 		if (var_exportedVars == VAR_EXPORTED_NONE)
@@ -961,7 +969,7 @@ Var_SetWithFlags(GNode *scope, const cha
 		DEBUG4(VAR, "%s: %s = %s%s\n",
 		    scope->name, name, val, ValueDescription(val));
 		if (v->exported)
-			ExportVar(name, VEM_PLAIN);
+			ExportVar(name, scope, VEM_PLAIN);
 	}
 
 	if (scope == SCOPE_CMDLINE) {

Reply via email to