]> git.ipfire.org Git - thirdparty/make.git/commitdiff
[SV 46013] Allow recursive variable overrides from Makefiles
authorPaul Smith <psmith@gnu.org>
Sun, 19 May 2019 16:16:14 +0000 (12:16 -0400)
committerPaul Smith <psmith@gnu.org>
Sun, 19 May 2019 23:25:50 +0000 (19:25 -0400)
Ensure that variable overrides are passed to recursive make instances
even if no overrides were provided on the command line.
Fix suggested by Rici Lake <ricilake@gmail.com>

* src/main.c (define_makeflags): Add overrides without respect to the
value of command_variables.
* tests/scripts/features/recursion: Add a test.

src/main.c
tests/scripts/features/recursion

index e9323fae292232ef8a7e52ed53587297766f725e..afc7fd97785bb07dd262cb9eb4ae88afcc3fb4a6 100644 (file)
@@ -3119,8 +3119,8 @@ quote_for_env (char *out, const char *in)
 static struct variable *
 define_makeflags (int all, int makefile)
 {
-  const char ref[] = "$(MAKEOVERRIDES)";
-  const char posixref[] = "$(-*-command-variables-*-)";
+  const char ref[] = "MAKEOVERRIDES";
+  const char posixref[] = "-*-command-variables-*-";
   const char evalref[] = "$(-*-eval-flags-*-)";
   const struct command_switch *cs;
   char *flagstring;
@@ -3247,7 +3247,7 @@ define_makeflags (int all, int makefile)
 #undef  ADD_FLAG
 
   /* Four more for the possible " -- ", plus variable references.  */
-  flagslen += 4 + CSTRLEN (posixref) + 1 + CSTRLEN (evalref) + 1;
+  flagslen += 4 + CSTRLEN (posixref) + 4 + CSTRLEN (evalref) + 4;
 
   /* Construct the value in FLAGSTRING.
      We allocate enough space for a preceding dash and trailing null.  */
@@ -3315,25 +3315,26 @@ define_makeflags (int all, int makefile)
       p += CSTRLEN (evalref);
     }
 
-  if (all && command_variables)
+  if (all)
     {
-      /* Write a reference to $(MAKEOVERRIDES), which contains all the
-         command-line variable definitions.  Separate the variables from the
-         switches with a "--" arg.  */
+      /* If there are any overrides to add, write a reference to
+         $(MAKEOVERRIDES), which contains command-line variable definitions.
+         Separate the variables from the switches with a "--" arg.  */
 
-      strcpy (p, " -- ");
-      p += 4;
+      const char *r = posix_pedantic ? posixref : ref;
+      size_t l = strlen (r);
+      struct variable *v = lookup_variable (r, l);
 
-      /* Copy in the string.  */
-      if (posix_pedantic)
-        {
-          memcpy (p, posixref, CSTRLEN (posixref));
-          p += CSTRLEN (posixref);
-        }
-      else
+      if (v && v->value && v->value[0] != '\0')
         {
-          memcpy (p, ref, CSTRLEN (ref));
-          p += CSTRLEN (ref);
+          strcpy (p, " -- ");
+          p += 4;
+
+          *(p++) = '$';
+          *(p++) = '(';
+          memcpy (p, r, l);
+          p += l;
+          *(p++) = ')';
         }
     }
 
index fd5e3518c98719518aa32055efb332fe9dc1024f..d225c13750c8d328521597131364637b70bad24f 100644 (file)
@@ -5,16 +5,17 @@ $details = "DETAILS";
 
 # Test some basic recursion.
 run_make_test('
+.RECIPEPREFIX := |
 all:
-       $(MAKE) -f #MAKEFILE# foo
+|       $(MAKE) -f #MAKEFILE# foo
 foo:
-       @echo $(MAKE)
-       @echo MAKELEVEL = $(MAKELEVEL)
-       $(MAKE) -f #MAKEFILE# last
+|       @echo $(MAKE)
+|       @echo MAKELEVEL = $(MAKELEVEL)
+|       $(MAKE) -f #MAKEFILE# last
 last:
-       @echo $(MAKE)
-       @echo MAKELEVEL = $(MAKELEVEL)
-       @echo THE END
+|       @echo $(MAKE)
+|       @echo MAKELEVEL = $(MAKELEVEL)
+|       @echo THE END
 ',
               ('CFLAGS=-O -w' . ($parallel_jobs ? ' -j 2' : '')),
               ($vos
@@ -52,4 +53,15 @@ all: ; @echo "MAKEOVERRIDES = $(MAKEOVERRIDES)"
 MAKEOVERRIDES = a=AA
 ');
 
+# SV 46013: Ensure that MAKEOVERRIDES is passed even if set in the makefile
+run_make_test(q!
+ifeq ($(MAKELEVEL),0)
+MAKEOVERRIDES += FOO+=bar
+endif
+.PHONY: M R
+M: ; @$(MAKE) --no-print-directory -f #MAKEFILE# R
+R: ; @echo '$(FOO)'
+!,
+              '', 'bar');
+
 1;