Re: Hot to write pattern rules for suffix deletion

Renaud Pacalet <[email protected]> Fri, 5 Jan 2024 08:20:50 +0100
Newsgroups gmane.comp.gnu.make.general
Message-ID <[email protected]>
On 05/01/2024 01:08, Stephen Touset wrote:
> Hello all,
> 
> I have a set of files in a project that need to be preprocessed through
> `envsubst` to render their output. The targets of these templated files
> are a mix of different file types, so they aren't rendered to files with
> a consistent extension. Further, not all files with those target
> extensions need to be rendered from templates. For example:
> 
>      a.yaml.envsubst    // a.yaml should be rendered from this
>      b.json.envsubst    // b.json should be rendered from this
>      c.json             // should not pass through a rendering step
> 
> I would like to express this in a make rule. Instinctually, I want to
> reach for something like
> 
>      %: %.envsubst
>          envsubst < $< > $@
> 
> Obviously this rule isn't correct. But I'm a bit at a loss for how to
> express what I want through make rules. One approach that I considered
> was using `$(shell find ...)` to get a list of these templates and
> generate rules for each one. Unfortunately in my case, that would be
> quite difficult because one file might go through *multiple*
> preprocessing steps:
> 
>      d.json.envsubst.gz // d.json should be decompressed then rendered
> 
> So now I'd need to have some overcomplicated logic to write out the
> resulting rules:
> 
>      d.json: d.json.envsubst
>          ...
> 
>      d.json.envsubst: d.json.envsubst.gz
>          ...
> 
> I'm happy to change the naming scheme of these files for a reasonable
> solution, but I'm feeling a bit stuck by the combination that a) files
> which should be rendered through `gzip -d` and `envsubst` don't have
> consistent target suffixes, b) a final target might go through either or
> both of these steps, and c) not every target requires rendering.
> 
> Any ideas?

What about (not tested):

ALLES   := $(shell find -type f \( -name '*.envsubst' -o -name 
'*.envsubst. gz \))
ES      := $(filter %.envsubst,$(ALLES))
GZES    := $(filter %.envsubst.gz,$(ALLES))
DGZES   := $(patsubst %.gz,%,$(GZES))
TARGETS := $(patsubst %.envsubst,%,$(ES) $(DGZES))

.PHONY: all
all: $(TARGETS)

$(DGZES): %.envsubst: %.envsubst.gz
     gunzip -dc $< > $@

$(TARGETS): %: %.envsubst
     envsubst < $< > $@

> 
> --
> Stephen Touset <[email protected]>
> 

-- 
Renaud Pacalet
Télécom Paris
Campus SophiaTech
450 Route des Chappes, CS 50193
06904 Biot Sophia Antipolis cedex, FRANCE
Tel : +33 (0) 4 9300 8402
Web : http://www.telecom-paris.fr/