On 2023-09-03 14:38, Paul Smith wrote:
> On Sun, 2023-09-03 at 13:14 +0200, Alejandro Colomar wrote:
>>> You need to put the ifdef outside the recipe:
>>
>> That would hurt readability a little bit.
> 
> IMO a make preprocessor statement inside a recipe is a code smell and I
> would find the "outside the recipe" version simpler to understand, but
> of course this is a personal taste thing.
> 

The rule is something like this:

target: source
        sed 's/%date%/$(shell date)/' <$< |
ifneq ($(VERSION),)
        sed 's/%version%/$(VERSION)/' |
endif
        cp -T /dev/stdin $@


I could first copy and then edit in-place, to avoid needing .ONESHELL:

target: source
        cp -T $< $@
        sed -i 's/%date%/$(shell date)/' $@
ifneq ($(VERSION),)
        sed -i 's/%version%/$(VERSION)/' $@
endif


But I don't like this as much; it's probably quite inefficient.

Then I could split this rule into intermediate targets, and make the
dependencies variable depending on $(VERSION).  Something like

ifeq ($(VERSION),)
target: target-version
target-version: target-date
else
target: target-date
endif

target-date: source
        sed 's/%date%/$(shell date)/' <$< >$@

target-version:
        sed 's/%version%/$(VERSION)/' <$< >$@

target:
        cp -T $< $@


It's more work, but would keep it at 1 canned recipe per rule, which
should be the normal thing.  I might do this.  It felt a bit overkill,
but I don't know.

Cheers,
Alex


-- 
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to