aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/makefile_escaping
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2020-05-30 21:08:38 +0000
committerEgor Tensin <Egor.Tensin@gmail.com>2020-05-30 21:09:26 +0000
commit85b9c46a218e62f161e4c961e27bfab8de7bca53 (patch)
treea07e9041416904c059d5b365c942354086e12476 /makefile_escaping
parentmakefile_escaping: add comments to *.mk files (diff)
downloadblog-85b9c46a218e62f161e4c961e27bfab8de7bca53.tar.gz
blog-85b9c46a218e62f161e4c961e27bfab8de7bca53.zip
makefile_escaping: fix variable overrides
Diffstat (limited to 'makefile_escaping')
-rw-r--r--makefile_escaping/escaping_env_vars.mk36
1 files changed, 26 insertions, 10 deletions
diff --git a/makefile_escaping/escaping_env_vars.mk b/makefile_escaping/escaping_env_vars.mk
index 2b8690b..421c4b6 100644
--- a/makefile_escaping/escaping_env_vars.mk
+++ b/makefile_escaping/escaping_env_vars.mk
@@ -7,22 +7,38 @@ SHELL := bash
escape = $(subst ','\'',$(1))
-test_var ?= This is safe.
-test_var := $(value test_var)
+define escape_arg
+ifeq ($$(origin $(1)),environment)
+$(1) := $$(value $(1))
+endif
+ifeq ($$(origin $(1)),environment override)
+$(1) := $$(value $(1))
+endif
+ifeq ($$(origin $(1)),command line)
+override $(1) := $$(value $(1))
+endif
+endef
+
+# Simple variable.
+simple_var := Simple value
+
+test_var ?= $(simple_var)
export test_var
+$(eval $(call escape_arg,test_var))
+
+simple_var := New simple value
+
# printf $test_var
echo_test_var := printf '%s\n' '$(call escape,$(test_var))'
# bash -c 'printf $test_var'
bash_test_var := bash -c '$(call escape,$(echo_test_var))'
-# Simple variable.
-inner_var := Inner variable
-# Composite variable, includes both $inner_var and $test_var.
-outer_var := Outer variable - $(inner_var) - $(test_var)
+# Composite variable, includes both $simple_var and $test_var.
+composite_var := Composite value - $(simple_var) - $(test_var)
-# printf $outer_var
-echo_outer_var := printf '%s\n' '$(call escape,$(outer_var))'
+# printf $composite_var
+echo_composite_var := printf '%s\n' '$(call escape,$(composite_var))'
.PHONY: test
test:
@@ -30,5 +46,5 @@ test:
@printf '%s\n' "$$test_var"
@bash -c '$(call escape,$(echo_test_var))'
@bash -c '$(call escape,$(bash_test_var))'
- @printf '%s\n' '$(call escape,$(outer_var))'
- @bash -c '$(call escape,$(echo_outer_var))'
+ @printf '%s\n' '$(call escape,$(composite_var))'
+ @bash -c '$(call escape,$(echo_composite_var))'