From 0880e5c86ab7ba92c3ba0f83bab1a230906ee657 Mon Sep 17 00:00:00 2001 From: Dmitry Goncharov Date: Sun, 30 Apr 2023 09:26:29 -0400 Subject: [SV 64107] Disable builtins immediately on -R or -r Disable builtin variables and rules immediately, when -R or -r is added to MAKEFLAGS inside the makefile. * src/main.c (disable_builtins): Add new function disable_builtins(). (main): Call disable_builtins(). (reset_makeflags): Call disable_builtins(). * tests/scripts/options/dash-r: Add tests. * tests/scripts/variables/MAKEFLAGS: Update tests. --- src/main.c | 58 ++++++++++++++++++++++------------- tests/scripts/options/dash-r | 63 +++++++++++++++++++++++++++++++++++++-- tests/scripts/variables/MAKEFLAGS | 2 +- 3 files changed, 100 insertions(+), 23 deletions(-) diff --git a/src/main.c b/src/main.c index 6a94cfdb..8215ed78 100644 --- a/src/main.c +++ b/src/main.c @@ -101,6 +101,7 @@ static void decode_switches (int argc, const char **argv, enum variable_origin origin); static void decode_env_switches (const char *envar, size_t len, enum variable_origin origin); +static void disable_builtins (); static char *quote_for_env (char *out, const char *in); static void initialize_global_hash_tables (void); @@ -180,6 +181,8 @@ int question_flag = 0; int no_builtin_rules_flag = 0; int no_builtin_variables_flag = 0; +static int old_builtin_rules_flag; +static int old_builtin_variables_flag; /* Nonzero means all variables are automatically exported. */ @@ -2052,9 +2055,9 @@ main (int argc, char **argv, char **envp) } { - int old_builtin_rules_flag = no_builtin_rules_flag; - int old_builtin_variables_flag = no_builtin_variables_flag; int old_arg_job_slots = arg_job_slots; + old_builtin_rules_flag = no_builtin_rules_flag; + old_builtin_variables_flag = no_builtin_variables_flag; /* Read all the makefiles. */ read_files = read_all_makefiles (makefiles == 0 ? 0 : makefiles->list); @@ -2100,24 +2103,7 @@ main (int argc, char **argv, char **envp) make_sync.syncout = syncing; OUTPUT_SET (&make_sync); - /* If -R was given, set -r too (doesn't make sense otherwise!) */ - if (no_builtin_variables_flag) - no_builtin_rules_flag = 1; - - /* If we've disabled builtin rules, get rid of them. */ - if (no_builtin_rules_flag && ! old_builtin_rules_flag) - { - if (suffix_file->builtin) - { - free_dep_chain (suffix_file->deps); - suffix_file->deps = 0; - } - define_variable_cname ("SUFFIXES", "", o_default, 0); - } - - /* If we've disabled builtin variables, get rid of them. */ - if (no_builtin_variables_flag && ! old_builtin_variables_flag) - undefine_default_variables (); + disable_builtins (); } #if MK_OS_W32 @@ -3094,6 +3080,7 @@ reset_makeflags (enum variable_origin origin) { decode_env_switches (STRING_SIZE_TUPLE(MAKEFLAGS_NAME), origin); construct_include_path (include_dirs ? include_dirs->list : NULL); + disable_builtins (); define_makeflags (rebuilding_makefiles); } @@ -3451,6 +3438,37 @@ quote_for_env (char *out, const char *in) return out; } +/* Disable builtin variables and rules, if -R or -r is specified. + * This function is called at parse time whenever MAKEFLAGS is modified and + * also when the parsing phase is over. */ + +static +void disable_builtins () +{ + /* If -R was given, set -r too (doesn't make sense otherwise!) */ + if (no_builtin_variables_flag) + no_builtin_rules_flag = 1; + + /* If we've disabled builtin rules, get rid of them. */ + if (no_builtin_rules_flag && ! old_builtin_rules_flag) + { + old_builtin_rules_flag = 1; + if (suffix_file->builtin) + { + free_dep_chain (suffix_file->deps); + suffix_file->deps = 0; + } + define_variable_cname ("SUFFIXES", "", o_default, 0); + } + + /* If we've disabled builtin variables, get rid of them. */ + if (no_builtin_variables_flag && ! old_builtin_variables_flag) + { + old_builtin_variables_flag = 1; + undefine_default_variables (); + } +} + /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the command switches. Always include options with args. Don't include options with the 'no_makefile' flag set if MAKEFILE. */ diff --git a/tests/scripts/options/dash-r b/tests/scripts/options/dash-r index 7157f715..16eda104 100644 --- a/tests/scripts/options/dash-r +++ b/tests/scripts/options/dash-r @@ -2,8 +2,6 @@ $description = "Test removing default rules and variables"; -$details = "DETAILS"; - touch('xxx.c'); # Simple check @@ -41,4 +39,65 @@ MAKEFLAGS := -R all: hello.o !, '', "#MAKE#: *** No rule to make target 'hello.o', needed by 'all'. Stop.", 512); +my @flavors = ('=', ':=', ':::=', '+='); + +# sv 64107. + +# Use $answer to test that -R in the makefile has the same effect as -R on the +# command line. + +my $answer = "at parse time TEX=\nat build time TEX=\n#MAKE#: 'all' is up to date.\n"; + +# Subtest 1. +# First run with -R command line switch. + +for my $fl (@flavors) { +run_make_test(" +\$(info at parse time TEX=\$(TEX)) +all:; \$(info at build time TEX=\$(TEX)) +", '-R', "$answer"); +} + +# Subtest 2. +# Set -R in the makefile. +# Also, test that setting -R in MAKEFLAGS takes effect immediately. + +for my $fl (@flavors) { +run_make_test(" +\$(info at start time TEX=\$(TEX)) +MAKEFLAGS ${fl} -R +\$(info at parse time TEX=\$(TEX)) +all:; \$(info at build time TEX=\$(TEX)) +", '', "at start time TEX=tex\n$answer"); +} + +# Same as above, but also set TEX conditionally. + +$answer = "at parse time TEX=hello\nat build time TEX=hello\n#MAKE#: 'all' is up to date.\n"; + +# Subtest 3. +# -R on the command line. + +for my $fl (@flavors) { +run_make_test(" +TEX ?= hello +\$(info at parse time TEX=\$(TEX)) +all:; \$(info at build time TEX=\$(TEX)) +", '-R', "$answer"); +} + +# Subtest 4. +# -R in the makefile. + +for my $fl (@flavors) { +run_make_test(" +\$(info at start time TEX=\$(TEX)) +MAKEFLAGS ${fl} -R +TEX ?= hello +\$(info at parse time TEX=\$(TEX)) +all:; \$(info at build time TEX=\$(TEX)) +", '', "at start time TEX=tex\n$answer"); +} + + 1; diff --git a/tests/scripts/variables/MAKEFLAGS b/tests/scripts/variables/MAKEFLAGS index fc18d650..aab4a0e1 100644 --- a/tests/scripts/variables/MAKEFLAGS +++ b/tests/scripts/variables/MAKEFLAGS @@ -297,7 +297,7 @@ MAKEFLAGS${fl}R all:; \$(info \$(MAKEFLAGS)) ", 'bye=moon', "$answer -R$answer +rR$answer rR$answer #MAKE#: 'all' is up to date.\n"); } -- cgit v1.2.1