Subject: Re: arithmetic in make
To: None <tech-toolchain@NetBSD.org>
From: Manuel Bouyer <bouyer@antioche.eu.org>
List: tech-toolchain
Date: 02/25/2007 18:25:57
On Sun, Feb 25, 2007 at 06:01:44PM +0100, Manuel Bouyer wrote:
> On Sun, Feb 25, 2007 at 11:12:29AM -0500, der Mouse wrote:
> > > .for file in ${MYFILES}
> > > 	cp ${file} dir/file{$n}
> > > .endfor
> > 
> > n := 1
> > .for file in $(MYFILES)
> > 	cp $(file) dir/file$(n)
> > 	n != echo $(($(n)+1))   # or != expr $(n) + 1
> > .endfor
> > 
> > I don't know of any way to do the arithmetic itself within make.  For
> > general arithmetic, it's probably possible, but I suspect only with the
> > help of an include file which implements recursion through re-including
> > itself.  However, incrementing is a special case:
> > 
> > n := 1
> > .for file in $(MYFILES)
> > 	cp $(file) dir/file$(n)
> > 	n1 := $(n:C/\(.*\)\(.\)\(9*\)/\1/)
> > 	n2 := $(n:C/\(.*\)\(.\)\(9*\)/\2/)
> > 	n3 := $(n:C/\(.*\)\(.\)\(9*\)/\3/)
> > 	n := $(n1)$(n2:S/9/10/:S/8/9/:S/7/8/:S/6/7/:S/5/6/:S/4/5/:S/3/4/:S/2/3:S/1/2/:S/0/1/)$(n3:S/9/0/g)
> > .endfor
> > 
> > The three regex lines can probably be done with only one regex line,
> > producing a string containing delimiters, picking apart the result
> > separately.
> 
> The help of shell is probably the easiest for what I want to do,
> but is echo $(($(n)+1)) portable enough ? This is for
> distrib/common/Makefile.bootcd, so it has to be cross-compile friendly ...

Thanks, I got this working (at last on NetBSD) using expr.
I had to split it in 2 parts tough, as I couldn't mix the index compute with
commands. I used a intermediate variable:
n := 1
.for kernel in ${CDEXTRAKERNELS}
targetname.${kernel} := netbsd${n}
n != expr $(n) + 1
.endfor

prepare:
	[...]
.for kernel in ${CDEXTRAKERNELS}
        ${CP} ${_INSTKERELNOBJDIR}/${kernel} cdrom/${targetname.${kernel}}
.endfor

-- 
Manuel Bouyer <bouyer@antioche.eu.org>
     NetBSD: 26 ans d'experience feront toujours la difference
--