Search code examples
makefile

Simplest way to reverse the order of strings in a make variable


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.


Solution

  • 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