make/tests/scripts/variables/conditional
Paul Smith 1eff20f6f6 Support conditional modifiers on all assignment operators
Rework the single "?=" operator to instead allow a "?" modifier to be
prepended to ANY assignment operator.  If "?" is given then the
variable is assigned (using whatever operator comes next) if and only
if the variable is not already defined.  If it is defined then no
action is taken (the right-hand side is not expanded, etc.)

* NEWS: Announce this new feature.
* doc/make.texi: Modify the documentation around assignment operators.
* src/variable.h: Remove the f_conditional variable flavor.
(do_variable_definition): Add an argument specifying conditional.
* src/variable.c (parse_variable_definition): Use the existing flag
"conditional" to remember if we saw "?" rather than the flavor.
When we see "?" skip it and continue trying to parse an assignment.
(try_variable_definition): Pass condition to do_variable_definition().
(initialize_file_variables): Ditto.
(do_variable_definition): Check for conditional up-front: quit if set.
Remove handling of obsolete f_conditional flavor.
* src/read.c (eval_makefile): MAKEFILE_LIST is not conditional.
(do_define): Unset conditional for define with no operator.  Pass the
conditional flag to do_variable_definition().
(construct_include_path): .INCLUDE_DIRS is not conditional.
* src/load.c (load_file): .LOADED is not conditional.
* tests/scripts/variables/conditional: Add new tests.
2024-01-28 14:20:47 -05:00

137 lines
1.9 KiB
Perl

# -*-perl-*-
$description = "Test various flavors of conditional variable setting.";
$details = "";
# Test ?=
run_make_test(q!
x = bar
y = baz
foo ?= $(x)
biz?=$(y)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
!,
'', "foo=10 biz=20");
run_make_test(q!
foo=1
biz=2
x = bar
y = baz
foo ?= $(x)
biz?=$(y)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
!,
'', "foo=1 biz=2");
# Test ?:=
run_make_test(q!
x = bar
y = baz
foo ?:= $(x)
biz?:=$(y)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
!,
'', "foo=bar biz=baz");
run_make_test(q!
foo=1
biz=2
x = bar
y = baz
foo ?:= $(x)$(info expanded)
biz?:=$(y)$(info expanded)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
!,
'', "foo=1 biz=2");
# Test ?::=
run_make_test(q!
x = bar
y = baz
foo ?::= $(x)
biz?::=$(y)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
!,
'', "foo=bar biz=baz");
run_make_test(q!
foo=1
biz=2
x = bar
y = baz
foo ?::= $(x)$(info expanded)
biz?::=$(y)$(info expanded)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
!,
'', "foo=1 biz=2");
# Test ?:::=
run_make_test(q!
x = bar
y = baz
foo ?:::= $(x)
biz?:::=$(y)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
!,
'', "foo=bar biz=baz");
run_make_test(q!
foo=1
biz=2
x = bar
y = baz
foo ?:::= $(x)$(info expanded)
biz?:::=$(y)$(info expanded)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
!,
'', "foo=1 biz=2");
# Test ?!=
run_make_test(q/
x = bar
y = baz
foo ?!= echo $(x)
biz?!=echo $(y)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
/,
'', "foo=bar biz=baz");
run_make_test(q/
foo=1
biz=2
x = bar
y = baz
foo ?!= echo $(x)$(info expanded)
biz?!=echo $(y)$(info expanded)
x = 10
y = 20
all:;@: $(info foo=$(foo) biz=$(biz))
/,
'', "foo=1 biz=2");
1;