help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Rule to compile file[0-9].lf + file.file => file[0-9].view


From: Lane Schwartz
Subject: Re: Rule to compile file[0-9].lf + file.file => file[0-9].view
Date: Sat, 9 Jul 2011 10:26:03 -0400

On Fri, Jul 8, 2011 at 6:15 AM, bofrost
<address@hidden> wrote:
>
> Hi,
>
> I have some source files called file0.lf file1.lf file2.lf .... they should
> compiled to file0.view file1.view file2.view ...
> For compilation they need the file bla.file (whithout the number).
>
> My question is how i can write a pattern Rule that my makefile atomatically
> compiles my *.view files when ther is a chang in *.lf or *.file.
>
> I've tried many things like
>
> %.view : ./src/dss/%.file $(%:%[0-9]=%)
>      echo "compiling"
>
> but nothing works :(
>
> Has anybody an idea how to write such a rule?

Complex pattern matching in rules, like what you want to do, is one of
the main weaknesses of make, in my opinion. There are ways of doing
what you want, but they require somewhat advanced use of make's
built-in call, eval, and foreach functions.

The simplest solution is something like this:

%.view: %.lf bla.file
     echo "compiling"

This should do the trick, given the requirements that you described.
Now, the problem is that this easy solution overgeneralizes. It will
match *any* file with suffix .lf and generate the corresponding .view
file with the same name. Maybe this is what you want. But if you
*only* want this rule to fire for specific files (like file[0-9].lf)
then this solution isn't specific enough for you.

The workaround is to use rule templates in conjunction with eval.

# Define a rule template
define COMPILE

file$1.view: file$1.lf bla.file
     echo "compiling"

endef


# Dynamically generate new makefile rules
$(foreach n,1 2 3 4 5,\
     $(eval $(call COMPILE,$n)))


This is similar to the example described in Section 8.8 Eval function
of the GNU Make manual.

As I recently learned
(http://lists.gnu.org/archive/html/help-make/2011-06/msg00012.html)
there is another alternative which allows you to used named parameters
instead of the automatic $1, $2, ... variables within a rule template:

# Define a rule template
define COMPILE

file$n.view: file$n.lf bla.file
     echo "compiling"

endef


# Dynamically generate new makefile rules
$(foreach n,1 2 3 4 5,\
     $(eval COMPILE))


Hope this helps.

Cheers,
Lane



reply via email to

[Prev in Thread] Current Thread [Next in Thread]