On 2005-10-20 16:06 UTC, Dario Teixeira wrote:
> 
> I need to use variables inside an implicit rule.  The idea
> is that all files named $(PREFIX)_%_$(SUFFIX) should
> be processed by the rule.  The exact values for $(PREFIX)
> and $(SUFFIX) are set previously in the Makefile, and the
> % character is of course the wildcard for implicit rules.
> 
> Anyway, here's an example that works fine:
> (It is of course a stripped down version of the full Makefile.
> Also, suppose that "generate" is a command which reads from
> first argument and writes to the second).
> 
> ==================================================
> all: draft
> PREFIX=before
> SUFFIX=after
> draft: before_foobar_after.out
> 
> $(PREFIX)_%_$(SUFFIX).out : $(PREFIX)_%_$(SUFFIX).in
>         generate $< $@
> ==================================================
> 
> 
> Now, the problem happens when I use target-specific
> variable values.  If I understood the make manual,
> all prerequisites should inherit those target-specific
> variables, but this does not seem to happen with
> the implicit rule.
> 
> This example fails:
> 
> ==================================================
> all: draft
> draft: PREFIX=before
> draft: SUFFIX=after
> draft: prefix_foobar_suffix.out
> 
> $(PREFIX)_%_$(SUFFIX).out : $(PREFIX)_%_$(SUFFIX).in
>         generate $< $@
> ==================================================
> 
> 
> Is this a bug, a feature, or am I missing something?

Target-specific variables work only in scripts: they're like
$@, $*, etc. in that respect.

> Also, is there any other way to achieve what I want?

It's been said that you can solve any programming problem by
adding an extra level of indirection. Here's a way to transform
target-specific variables into vanilla variables.

# GNUmakefile begins #
draft: PREFIX=before
draft: SUFFIX=after

PREFIX ?= (PREFIX not set)
SUFFIX ?= (SUFFIX not set)

all: $(PREFIX)_$(MAKECMDGOALS)_$(SUFFIX).out

%:
        @$(MAKE) --file=submakefile --no-print-directory \
          PREFIX='$(PREFIX)' SUFFIX='$(SUFFIX)' \
          $(PREFIX)[EMAIL PROTECTED](SUFFIX).out
# GNUmakefile ends #

# submakefile begins #
%.out:
        @echo PREFIX='$(PREFIX)'
        @echo SUFFIX='$(SUFFIX)'
        @echo Invoke rule to make $@
# submakefile ends #

Test:

$make draft
PREFIX=before
SUFFIX=after
Invoke rule to make before_draft_after.out

$make xyzzy
PREFIX=(PREFIX not set)
SUFFIX=(SUFFIX not set)
Invoke rule to make (PREFIX not set)_xyzzy_(SUFFIX not set).out


_______________________________________________
Help-make mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-make

Reply via email to