tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

make cleaning the environment for children



The patch below implements the ability to clean the environment.
Coupled with a test for
.if ${.MAKE.LEVEL} == 0
this can allow sys.mk some serious control over the build environment.

My main question (apart from does anyone else see the value ;-)
is whether using a 1st arg of '=' as in:

.if ${.MAKE.LEVEL} == 0
# clean the environment
PATH = /bin:/usr/bin:/sbin:/usr/sbin
.undef .MAKE.EXPORTED
.export = PATH
.endif

is too subtle?

--sjg

Index: make.1
===================================================================
RCS file: /cvsroot/src/usr.bin/make/make.1,v
retrieving revision 1.163
diff -u -p -r1.163 make.1
--- make.1      2 Oct 2009 07:43:15 -0000       1.163
+++ make.1      15 Nov 2009 00:40:02 -0000
@@ -29,7 +29,7 @@
 .\"
 .\"    from: @(#)make.1        8.4 (Berkeley) 3/19/94
 .\"
-.Dd October 1, 2009
+.Dd November 14, 2009
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -1284,12 +1284,36 @@ The possible conditionals are as follows
 .Bl -tag -width Ds
 .It Ic .export Ar variable
 Export the specified global variable.
+.Pp
+If the first arg is
+.Ql \=
+then clear the environment.
+Take care to first preserve any variables that might have originated
+from the environment, that you care about.
+For example:
+.Bd -literal -offset indent
+PATH := ${PATH}
+.Li .undef .MAKE.EXPORTED
+.Li .export = PATH
+.Pp
+.Ed
+Would result in an environment containing only 
+.Ql Ev PATH ,
+which is the minimal useful environment.
+Without the 
+.Ql .undef
+of
+.Va .MAKE.EXPORTED
+variables previously flagged for export would continue to do so,
+but the environment would otherwise be clean.
+.Pp
 If no variable is provided, all globals are exported
 except for internal variables (those that start with
 .Ql \&. ) .
 This is not affected by the
 .Fl X
 flag, so should be used with caution.
+.Pp
 Appending a variable name to
 .Va .MAKE.EXPORTED
 is equivalent to exporting a variable.
Index: var.c
===================================================================
RCS file: /cvsroot/src/usr.bin/make/var.c,v
retrieving revision 1.154
diff -u -p -r1.154 var.c
--- var.c       8 Sep 2009 17:29:20 -0000       1.154
+++ var.c       15 Nov 2009 00:40:03 -0000
@@ -527,6 +527,9 @@ Var_Delete(const char *name, GNode *ctxt
        if ((v->flags & VAR_EXPORTED)) {
            unsetenv(v->name);
        }
+       if (strcmp(MAKE_EXPORTED, v->name) == 0) {
+           var_exportedVars = VAR_EXPORTED_NONE;
+       }
        if (v->name != ln->name)
                free(v->name);
        Hash_DeleteEntry(&ctxt->context, ln);
@@ -691,6 +694,39 @@ Var_Export(char *str, int isExport)
             * into a loop.
             */
            switch (name[0]) {
+           case '=':
+               /*
+                * We want to redefine the environment!
+                * This requires some care - any variables which
+                * might have come from the environment, should be
+                * save first.  As in:
+                * PATH := ${PATH}
+                * .export = PATH
+                * As an extra precaution ignore = unless it is
+                * the 1st arg.
+                */
+               if (i == 0) {
+                   extern char **environ;
+                   static char **savenv;
+                   char **newenv;
+
+                   if (environ == savenv) {
+                       /* we have been here before! */
+                       newenv = bmake_realloc(environ, 2 * sizeof(char *));
+                   } else {
+                       if (savenv) {
+                           free(savenv);
+                           savenv = NULL;
+                       }
+                       newenv = bmake_malloc(2 * sizeof(char *));
+                   }
+                   if (!newenv)
+                       continue;
+                   environ = savenv = newenv;
+                   newenv[0] = NULL;
+                   newenv[1] = NULL;
+               }
+               continue;
            case '@':
            case '%':
            case '*':
Index: unit-tests/Makefile
===================================================================
RCS file: /cvsroot/src/usr.bin/make/unit-tests/Makefile,v
retrieving revision 1.24
diff -u -p -r1.24 Makefile
--- unit-tests/Makefile 7 Oct 2009 16:40:30 -0000       1.24
+++ unit-tests/Makefile 15 Nov 2009 00:40:03 -0000
@@ -23,6 +23,7 @@ SUBFILES= \
        cond1 \
        export \
        export-all \
+       environ \
        dotwait \
        forsubst \
        moderrs \
Index: unit-tests/test.exp
===================================================================
RCS file: /cvsroot/src/usr.bin/make/unit-tests/test.exp,v
retrieving revision 1.29
diff -u -p -r1.29 test.exp
--- unit-tests/test.exp 7 Oct 2009 16:40:30 -0000       1.29
+++ unit-tests/test.exp 15 Nov 2009 00:40:03 -0000
@@ -38,6 +38,8 @@ UT_NO=all
 UT_OK=good
 UT_TEST=export-all
 UT_ZOO=hoopie
+UT_PATH=/bin:/usr/bin:/sbin:/usr/sbin
+UT_TEST=environ
 simple.1
 simple.1
 simple.2


Home | Main Index | Thread Index | Old Index