[Techtalk] Loop/wildcard-like syntax for GNU make?

Conor Daly conor.daly-linuxchix at cod.homelinux.org
Wed Apr 18 08:28:51 UTC 2007

On Tue, Apr 17, 2007 at 05:51:10PM -0700 or so it is rumoured hereabouts, 
Kelly Jones thought:
> Here's a Makefile that converts 3 GIFs to JPGs in a given directory:
> 1.jpg: 1.gif
>       /usr/local/bin/convert 1.gif 1.jpg
> 2.jpg: 2.gif
>       /usr/local/bin/convert 2.gif 2.jpg
> 3.jpg: 3.gif
>       /usr/local/bin/convert 3.gif 3.jpg
> How do I generalize this to apply to ALL the GIFs in a given
> directory? I tried:
> *.jpg: *.gif
>       /usr/bin/local/bin/convert $1.gif $1.jpg
> but this obviously doesn't work (I didn't really expect it to). Neither does
> for $i (*.jpg) {
> $i.jpg: $i.gif
>        /usr/bin/local/bin/convert $i.gif $i.jpg
> }
> (again, didn't really expect it to, since you can't put shell looping
> into a Makefile).

I have managed to put loops in my Makefiles.  It seems to be bash loop
syntax rather than C style syntax.  I'm not doing conversion like this but
just handling multiple man pages.  The install just copies all the man
pages to the target directory.

install:	$(Libname) $(LibSOname)
		cp ../man3/*.3 $(MANDIR)/man3/; 

But the uninstall must operate on a subset of the files in $(MANDIR) so I
have to use a list of files in my source tree to determine which files to

		for FILE in ../man3/*.3; do \
			rm -f $(MANDIR)/man3/$$(basename "$$FILE"); \

This works for a straight cp and should work with convert also:

	for FILE in *.png; do \
		cp -f $$FILE $$(basename "$$FILE" png)jpg; \

Note the use of $$FILE rather than $FILE and the use of the 'png' arg to
basename which strips that bit off the end of $$FILE.

Conor Daly <conor.daly at cod.homelinux.org>
Version: 3.1
GCS/G/S/O d+(-) s:+ a+ C++(+) UL++++ US++ P>++ L+++>++++ E--- W++ !N
PS+ PE Y+ PGP? tv(-) b+++(+) G e+++(*) h-- r+++ z++++ 
http://www.geekcode.com/ http://www.ebb.org/ungeek/

More information about the Techtalk mailing list