Reputation: 21
In GNU make, I'd like to perform multiple string substitutions on a blob of text containing several "placeholders", e.g.:
MYTEXT:= blabla _FIRST_PLACEHOLDER_ blabla _SECOND_PLACEHOLDER_whateverblabla_THIRD_PLACEHOLDER_blablabla
So I'd like to replace the "placeholders" with values as follows:
_FIRST_PLACEHOLDER_ => FIRST_VAL
_SECOND_PLACEHOLDER_ => SECOND_VAL
_THIRD_PLACEHOLDER_ => THIRD_VAL
...
The following shows a hideous way of obtaining the result I'd like:
$(subst _FIRST_PLACEHOLDER_,FIRST_VAL, $(subst _SECOND_PLACEHOLDER_,SECOND_VAL, $(subst _THIRD_PLACEHOLDER_,THIRD_VAL, $(MYTEXT))))
A solution would be straightforward to find outside the make world, but is there a better way than the above to perform such a recursive substitution while remaining within the confines of make? I tried using $(foreach), but this simply concatenates the result of each substitution applied once to the initial $(MYTEXT)
.
Upvotes: 2
Views: 54
Reputation: 5962
This solution requires overwriting of the variables _p
and _x
.
# -*- gnu-make -*-
ORIGINAL := 123__PLACE_HOLDER__1567__PLACE_HOLDER__2890
REPLACEMENT_LIST := \
__PLACE_HOLDER__1=ABC \
__PLACE_HOLDER__2=DEF \
_replace1 = $(eval _x := $(subst $(word 1,$(1)),$(word 2,$(1)),$(_x)))
replace = $(strip \
$(eval _x := $(strip $(2))) \
$(foreach _p,$(strip $(1)),$(call _replace1,$(subst =, ,$(_p)))) \
$(_x) \
$(eval _x :=) \
)
$(info ORIGINAL: '$(ORIGINAL)')
$(info REPLACEMENT: '$(call replace,$(REPLACEMENT_LIST),$(ORIGINAL))')
.PHONY: all
all:
Example run:
$ make
ORIGINAL: '123__PLACE_HOLDER__1567__PLACE_HOLDER__2890'
REPLACEMENT: '123ABC567DEF890'
make: Nothing to be done for 'all'.
This solution has the advantage of not modifying any variable.
_replace2 = $(subst $(word 1,$(1)),$(word 2,$(1)),$(2))
_replace1 = $(call replace,$(2),$(call _replace2,$(subst =, ,$(1)),$(3)))
replace = $(if $(1),$(call _replace1,$(firstword $(1)),$(wordlist 2,1000000,$(1)),$(2)),$(2))
or
_replace1 = $(subst $(word 1,$(1)),$(word 2,$(1)),$(2))
replace = $(if $(1),$(call replace,$(wordlist 2,1000000,$(1)),$(call _replace1,$(subst =, ,$(firstword $(1))),$(2))),$(2))
Upvotes: 1