tags:

views:

261

answers:

4

Let's say you have a variable in a makefile fragment like the following:

MY_LIST=a b c d

How do I then reverse the order of that list? I need:

$(warning MY_LIST=${MY_LIST})

to show

MY_LIST=d c b a

Edit: the real problem is that

ld -r some_object.o ${MY_LIST}

produces an a.out with undefined symbols because the items in MY_LIST are actually archives, but in the wrong order. If the order of MY_LIST is reversed, it will link correctly (I think). If you know a smarter way to get the link order right, clue me in.

A: 

If it's GNU Makefile, you could may be run shell with $(shell echo $MY_LIST | rev).

Your dependency issue looks weird. You could make bigger archives first, like ((c d) a b), but the compiler usually cope with that.

elmarco
Heh. Nice try :-)rev reverses all the characters, too, not just the strings. echo 'lib1.a lib2.a lib3.a' | rev results in "a.3bil a.2bil a.1bil"
Ben Collins
oups :) it works with your example though!
elmarco
I went ahead and accepted your answer, cuz it made me think of the real answer.
Ben Collins
thanks :)Awk is good at that too: echo one two three | awk '{ for (i=NF; i>0; i--) { printf "%s ", $i; } printf "\n" }'
elmarco
A: 

Doh! I could have just used a shell script-let:

(for d in ${MY_LIST}; do echo $$d; done) | tac

Ben Collins
$ for d in one two three; do echo $d; done | tacthreetwooneSuperflous \n. But it's cool to learn about tac.
elmarco
+2  A: 

You can also define search groups with ld:

ld -r foo.o -( a.a b.a c.a -)

Will iterate through a.a, b.a, and c.a until no new unresolved symbols can be satisfied by any object in the group.

If you're using gnu ld, you can also do:

ld -r -o foo.o --whole-archive bar.a

Which is slightly stronger, in that it will include every object from bar.a regardless of whether it satisfies an unresolved symbol from foo.o.

ajax
+1  A: 

A solution in pure GNU make:

default: all

foo = please reverse me

reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1))

all : @echo $(call reverse,$(foo))

Gives:

$ make

me reverse please