help-make
[Top][All Lists]
Advanced

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

RE: Expanding variables as part of function arguments


From: Anthony Penniston
Subject: RE: Expanding variables as part of function arguments
Date: Thu, 10 Jun 2010 12:41:02 -0400

Thanks for the ideas Philip, your example is an improvement over what I mention 
here:
>> Then join the list as before, but using subst to split on 
commas and pass $(firstword, $(lastword as arguments $(1) and $(2) to 
$(call.

However in both examples (splitting on commas and splitting on periods), I 
realized that they may fail in cases like:
SRCEXT := .c .bz2    .a
OBJEXT := .o .tar.gz .b,c

So I combined your example with my previous one to produce this:

SRCEXT  = .c .p .def .java
OBJEXT  = .o .o .dll .class
SRCDIRS = src
OUTDIR  = bin
empty :=
space := $(empty) $(empty)
separator := |
sources = $(strip $(foreach pair,$(join $(addsuffix 
$(separator),$(SRCEXT)),$(OBJEXT))\
,$(addprefix $(OUTDIR)/,$(addsuffix $(lastword $(subst 
$(separator),$(space),$(pair)))\
,$(basename $(notdir $(wildcard $(addsuffix /*$(firstword $(subst 
$(separator),$(space),$(pair))),$(SRCDIRS)))))))))

It joins the extensions with a separator (guaranteed not to be part of a valid 
filename) then for each pair it splits on the separator, adds all sources from 
the source dirs matching the first part and replaces the extension with the 
last part while prepending the output directory. The result is that "src/foo.c 
src/bar.java" etc. becomes "bin/foo.o bin/bar.class" etc.

But going back to my original message, none of these solutions feel 'natural' 
to what ought to be a common task for make. I've re-read the sections on 
'Writing Rules' but still can't think of a better solution for automation; I 
guess I'll continue to use the above while it works.

Thanks for your help,
Anthony

----------------------------------------
> Date: Wed, 9 Jun 2010 17:37:34 -0700
> Subject: Re: Expanding variables as part of function arguments
> From: address@hidden
> To: address@hidden
> CC: address@hidden; address@hidden
>
> On Wed, Jun 9, 2010 at 1:29 AM, Anthony Penniston
>  wrote:
>> I see; similar to the limitations preventing function calls like "$($(func) 
>> $(args))" (although it would be a nice feature to have).
>
> GNU make does have $(call), so this might be what you want: $(call
> ${func},arg1,arg2,...)
>
> If, to use lisp terminology, you're looking for 'apply' instead of
> 'funcall', then you have to use $(eval) and a temp variable, but
> that's getting weird.
>
>
>>Indeed, most suffixes will be .o, though I have a few others and the goal was 
>>to be able to add sources/objects with minimal updating of the makefile, i.e.:
>>
>> SRCEXT := .c .p .def .java
>> OBJEXT := .o .o .dll .class
>>
>> From this, the dependency list for the main target could be updated easily 
>> from files that match OBJEXT (i.e. "foo.o bar.o baz.dll fred.class").
>>
>> The only way I can think to achieve this now is to define OBJ as:
>> OBJ = $(patsubst
>> $(1),$(2),$(wildcard $(addprefix $(SRC)/*,$(SRCEXT))))
>> Then join the list as before, but using subst to split on commas and pass 
>> $(firstword, $(lastword as arguments $(1) and $(2) to $(call.
>> But this seems overly obtuse, and I can't help but feel I'm forcing an 
>> unnatural solution. Basically, I have certain source files in /src that I 
>> want to pull into the build with minimal editing of the makefile. These 
>> mostly depend on extension type and most have generic rules for building. Is 
>> there a better way to go about this than the above approach?
>
>
> $ ls -l
> total 10
> drwxrwxr-x 4 guenther wheel 512 Jun 9 17:26 .
> drwxrwxrwt 8 root wheel 512 Jun 9 17:26 ..
> -rw-rw-r-- 1 guenther wheel 271 Jun 9 17:35 Makefile
> drwxrwxr-x 2 guenther wheel 512 Jun 9 17:26 a
> drwxrwxr-x 2 guenther wheel 512 Jun 9 17:26 b
> $ ls -l *
> -rw-rw-r-- 1 guenther wheel 271 Jun 9 17:35 Makefile
>
> a:
> total 4
> drwxrwxr-x 2 guenther wheel 512 Jun 9 17:26 .
> drwxrwxr-x 4 guenther wheel 512 Jun 9 17:26 ..
> -rw-rw-r-- 1 guenther wheel 0 Jun 9 17:26 foo.c
> -rw-rw-r-- 1 guenther wheel 0 Jun 9 17:26 foo.def
>
> b:
> total 4
> drwxrwxr-x 2 guenther wheel 512 Jun 9 17:26 .
> drwxrwxr-x 4 guenther wheel 512 Jun 9 17:26 ..
> -rw-rw-r-- 1 guenther wheel 0 Jun 9 17:26 bar.java
> -rw-rw-r-- 1 guenther wheel 0 Jun 9 17:26 bar.p
> $ make
> a/foo.o b/bar.o a/foo.dll b/bar.class
> $ cat Makefile
>
> SRCEXT = .c .p .def .java
> OBJEXT = .o .o .dll .class
>
> SRCDIRS = a b
>
> OBJ = $(foreach pair,$(join ${OBJEXT},${SRCEXT}),$(patsubst %$(suffix
> ${pair}),%$(basename ${pair}),$(wildcard $(addsuffix /*$(suffix
> ${pair}),${SRCDIRS}))))
>
> all:
> @echo ${OBJ}
>
> .SUFFIXES:
> Makefile:;
> $
>
> I.e., join the matched suffixes so you can iterate across them
> together, then use $(basename) and $(suffix) to extract the two pieces
> in the loop.
>
>
> Philip Guenther
                                          
_________________________________________________________________
Look 'em in the eye: FREE Messenger video chat
http://go.microsoft.com/?linkid=9734386


reply via email to

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