From d7ff21ecd70bbf9dfda7b3a136b22f631cd80d22 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sun, 19 May 2019 12:16:14 -0400 Subject: [PATCH] [SV 46013] Allow recursive variable overrides from Makefiles 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 * 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 | 37 ++++++++++++++++---------------- tests/scripts/features/recursion | 26 ++++++++++++++++------ 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/main.c b/src/main.c index e9323fae..afc7fd97 100644 --- a/src/main.c +++ b/src/main.c @@ -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) + if (v && v->value && v->value[0] != '\0') { - memcpy (p, posixref, CSTRLEN (posixref)); - p += CSTRLEN (posixref); - } - else - { - memcpy (p, ref, CSTRLEN (ref)); - p += CSTRLEN (ref); + strcpy (p, " -- "); + p += 4; + + *(p++) = '$'; + *(p++) = '('; + memcpy (p, r, l); + p += l; + *(p++) = ')'; } } diff --git a/tests/scripts/features/recursion b/tests/scripts/features/recursion index fd5e3518..d225c137 100644 --- a/tests/scripts/features/recursion +++ b/tests/scripts/features/recursion @@ -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;