For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > November 2004 > Makefile question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Makefile question
Travis Emmitt

2004-11-16, 3:56 am

THE QUESTION:

How can I avoid hardcoding the album names (aaa, bbb, ccc) in this:

- albums/aaa/index.html : songs/aaa/*.xml
- albums/bbb/index.html : songs/bbb/*.xml
- albums/ccc/index.html : songs/ccc/*.xml

I tried:

- albums/%/index.html : songs/%/*.xml
- albums/*/index.html : songs/${subst albums/,,$$(@D)}/*.xml

Neither of these worked. The 2nd one's subst function return nothing.
It seems like the $$(@D) cannot be used within a function in the
prerequisites list; is that right?

Any ideas?

Thanks!

Travis

p.s. In case it isn't apparent, what I'm trying to say is that an
album's index.html depends upon all the song xml files for that album.
If any song in album "aaa" has its xml updated, I want to regenerate
albums/aaa/index.html
Trent Buck

2004-11-16, 8:56 am

Quoth Travis Emmitt on or about 2004-11-15:
> - albums/%/index.html : songs/%/*.xml
> - albums/*/index.html : songs/${subst albums/,,$$(@D)}/*.xml


albums/%/index.html: songs/$*/*.xml

-trent
Travis Emmitt

2004-11-16, 3:57 pm

Trent wrote:

> albums/%/index.html: songs/$*/*.xml


Sorry, this didn't work for me. I didn't get a syntax error, but
neither did the rule get triggered.

I'm using make for windows (mingw). Would that make a difference?

Travis
Trent Buck

2004-11-16, 3:57 pm

Quoth Travis Emmitt on or about 2004-11-16:
>
> Sorry, this didn't work for me. I didn't get a syntax error, but
> neither did the rule get triggered.
>
> I'm using make for windows (mingw). Would that make a difference?


Sorry, I was assuming GNU Make. What version and flavor of make are you
using?

-t
Travis Emmitt

2004-11-16, 6:56 pm

I'm using GNU Make 3.80

I downloaded the windows binary from www.mingw.org

I posted this question in the UNIX group b/c there don't seem many
people using make for windows. I think I feel more at home with
the UNIX crowd too. :)

Travis


Trent Buck

2004-11-17, 8:57 am

Quoth Travis Emmitt on or about 2004-11-16:
> I'm using GNU Make 3.80
> I downloaded the windows binary from www.mingw.org


That's good. I'm using GNU Make 3.80 on Debian.

OK, I did some more testing. It turns out that $* only works in the
rule body (sorry about that). Similarly these rules won't work:

ALL = $(addsuffix /y,$(wildcard x/*))
all: $(ALL)
$(ALL): x/%/y: a/%/*.b
@echo "'$+' -> '$@'" '(a/$*/*.b)'
$(ALL): x/%/y: $(wildcard a/%/*.b)
@echo "'$+' -> '$@'" '(a/$*/*.b)'

....the former won't glob; the latter won't perform % substitution.

I came up with the slightly inventive solution of building a second
Makefile, the making with that:

ALBUMS = $(patsubst songs/%,%,$(wildcard songs/*))
DIRECTORIES = $(patsubst %,albums/%,$(ALBUMS))
INDICES = $(patsubst %,albums/%/index.html,$(ALBUMS))
XML = $(patsubst %,songs/%/*.xml,$(ALBUMS))

all: deps.mk
$(MAKE) -f $<

deps.mk: $(XML)
( \
echo '#-*-Makefile-*-' ;\
echo 'default all: $(INDICES)' ;\
echo '$(DIRECTORIES):' ;\
echo ' mkdir -p $@' ;\
for ALBUM in $(ALBUMS); do \
echo -n "album/$$ALBUM/index.html: " ;\
echo -n "album/$$ALBUM " ;\
echo "\$$(wildcard songs/$$ALBUM/*.xml)" ;\
echo " cat \$$+ > \$$@" ;\
done \
) > $@


This appears to work on my system, although it may build targets more
often that strictly necessary.

You should replace the 'cat $+ > $@' line with whatever rule is
appropriate; you can also rename deps.mk to whatever you want the
subsidiary makefile to be.

Be warned that I have used a lot of GNU-only make extensions, so don't
expect this to work with e.g. Solaris make.

-trent
Travis Emmitt

2004-11-17, 3:59 pm

Trent - Thanks again for the info!

So, is it impossible to specify these sort of dependencies without
dynamically generating a separate makefile? If so, it's not like
the end of the world or anything, but I was hoping make would be
flexible enough to smoothly handle intra-directory dependencies,
once I learn the details of the syntax.

Travis



Trent Buck

2004-11-17, 3:59 pm

Quoth Travis Emmitt on or about 2004-11-17:
> So, is it impossible to specify these sort of dependencies without
> dynamically generating a separate makefile? If so, it's not like
> the end of the world or anything, but I was hoping make would be
> flexible enough to smoothly handle intra-directory dependencies,
> once I learn the details of the syntax.


Make can handle separate target and dependency directories and it can
handle globbing dependencies. But AFAICT it can't elegantly handle both
at once. The solution I gave is the best I can think of. Do tell me if
you find a better one :-)

-trent
PS: The technique is conceptually similar to the way autoconf works.
Travis Emmitt

2004-11-19, 3:58 pm

Trent -

Hi! I finally got it to work. I took your suggestion and created
temporary makefiles. I created one per album, instead of one biggie.

I just have to get used to the idea of using make to create makefiles
(and find out how to do it cleanly)!

Thanks a ton for your help!

Travis


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com