On Sun, 2011-05-15 at 18:06 +0800, Chen(陈) Jun(军) wrote:
> Please check my makefile:
> 
> ================
> define show
>     echo "[$1]"
> endef
> 
> filelist = 1.c 2.c 3.c
> 
> all:
>     @$(call show,1.c)
>     @$(call show,2.c)
>     @$(call show,3.c)
> 
> # That code below cannot work!
> #all2:
> #    @$(foreach f,$(filelist),$(call show,$p))
> ================
> 
> I'd like to dynamically generate N shell commands under "all" targets when
> filelist has N words. How can I do it? Without the ultimate solution,
> everytime I add a word to filelist, I have to manually add one command line
> under "all".

There are a number of ways to do this, some more portable/easier to
understand than others.

The most portable and simplest way is to use shell loops, not make
loops, like this:

        all:
                @for f in $(filelist); do echo "[$$f]"; done

Alternatively you can use make commands to generate a list of shell
commands, similar to your attempt above, BUT you have to add a semicolon
between them so the shell will interpret them as different commands,
like this:

        all:
                @$(foreach f,$(filelist),$(call show,$p);)

This is relatively straightforward but relies on GNU make features
(foreach, call, etc.)

The final method is to use $(eval ...); this is GNU make-specific AND
complicated to understand.

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <[email protected]>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.mad-scientist.net
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist


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

Reply via email to