<p>Let's say you have a variable in a makefile fragment like the following:</p>

<pre><code>MY_LIST=a b c d
</code></pre>

<p>How do I then reverse the order of that list?  I need:</p>

<pre><code>$(warning MY_LIST=${MY_LIST}) 
</code></pre>

<p>to show</p>

<pre><code>MY_LIST=d c b a
</code></pre>

<p>Edit:  the real problem is that </p>

<pre><code>ld -r some_object.o ${MY_LIST}
</code></pre>

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

[{'answer_id': 52722, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 3, 'selected': False, 'text': '<p>Doh!  I could have just used a shell script-let:</p>\n\n<p><code>(for d in ${MY_LIST}; do echo $$d; done) | tac</code></p>\n'}, {'answer_id': 52903, 'author': 'ajax', 'author_id': 5250, 'author_profile': 'https://Stackoverflow.com/users/5250', 'pm_score': 3, 'selected': False, 'text': "<p>You can also define search groups with ld:</p>\n\n<pre><code>ld -r foo.o -( a.a b.a c.a -)\n</code></pre>\n\n<p>Will iterate through a.a, b.a, and c.a until no new unresolved symbols can be satisfied by any object in the group.</p>\n\n<p>If you're using gnu ld, you can also do:</p>\n\n<pre><code>ld -r -o foo.o --whole-archive bar.a\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 786530, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': True, 'text': '<p>A solution in pure GNU make:</p>\n\n<blockquote>\n  <p>default: all</p>\n  \n  <p>foo = please reverse me</p>\n  \n  <p>reverse = $(if $(1),$(call\n  reverse,$(wordlist 2,$(words\n  $(1)),$(1)))) $(firstword $(1))</p>\n  \n  <p>all :     @echo $(call reverse,$(foo))</p>\n</blockquote>\n\n<p>Gives:</p>\n\n<blockquote>\n  <p>$ make</p>\n  \n  <p>me reverse please</p>\n</blockquote>\n'}, {'answer_id': 14260762, 'author': 'studog', 'author_id': 1352761, 'author_profile': 'https://Stackoverflow.com/users/1352761', 'pm_score': 3, 'selected': False, 'text': "<p>An improvement to the GNU make solution:</p>\n\n<blockquote>\n  <p>reverse = $(if $(wordlist 2,2,$(1)),$(call reverse,$(wordlist 2,$(words $(1)),$(1))) $(firstword $(1)),$(1))</p>\n</blockquote>\n\n<ul>\n<li>better stopping condition, original uses the empty string wasting a function call</li>\n<li>doesn't add a leading space to the reversed list, unlike the original</li>\n</ul>\n"}, {'answer_id': 16795504, 'author': 'Tripp Lilley', 'author_id': 309233, 'author_profile': 'https://Stackoverflow.com/users/309233', 'pm_score': 2, 'selected': False, 'text': '<p>Playing off of both <a href="https://stackoverflow.com/a/52722/309233">Ben Collins\'</a> and <a href="https://stackoverflow.com/a/52697/309233">elmarco\'s</a> answers, here\'s a punt to bash which handles whitespace "properly"<sup>1</sup></p>\n\n<pre><code>reverse = $(shell printf "%s\\n" $(strip $1) | tac)\n</code></pre>\n\n<p>which does the right thing, thanks to <code>$(shell)</code> automatically cleaning whitespace and <code>printf</code> automatically formatting each word in its arg list:</p>\n\n<pre><code>$(info [ $(call reverse,  one two   three four  ) ] )\n</code></pre>\n\n<p>yields:</p>\n\n<pre><code>[ four three two one ]\n</code></pre>\n\n<p><sup>1</sup>...according to my limited test case (i.e., the <code>$(info ...)</code> line, above).</p>\n'}]
['https://Stackoverflow.com/questions/52674', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3279/']