From 85b9c46a218e62f161e4c961e27bfab8de7bca53 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 30 May 2020 21:08:38 +0000 Subject: makefile_escaping: fix variable overrides --- .github/workflows/makefile-escaping.yml | 4 ++-- makefile_escaping/escaping_env_vars.mk | 36 ++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/.github/workflows/makefile-escaping.yml b/.github/workflows/makefile-escaping.yml index 0a3c120..d6cc01d 100644 --- a/.github/workflows/makefile-escaping.yml +++ b/.github/workflows/makefile-escaping.yml @@ -93,5 +93,5 @@ jobs: Quote ' and variable ${reference} Quote ' and variable ${reference} Quote ' and variable ${reference} - Outer variable - Inner variable - Quote ' and variable ${reference} - Outer variable - Inner variable - Quote ' and variable ${reference} + Composite value - New simple value - Quote ' and variable ${reference} + Composite value - New simple value - Quote ' and variable ${reference} 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))' -- cgit v1.2.3