Iterate over a list in a Makefile and get both values ​​and indexes

In the GNU Makefile, you have a list of elements:

OBJECTS = foo bar baz 

You want to iterate over the elements, and you are interested in both the index and the value of each element (foo => 1, bar => 2, baz => 3). What is the idiomatic way to do this in a makefile?

+4
source share
1 answer

There are several ways to do this: none of them are very clean (this is a strong hint that you are trying to do something that is not suitable for Make), and your specific case may require special handling, but here goes:

 OBJECTS = foo bar baz NLIST = $(shell for x in {1..$(words $(OBJECTS))}; do echo $$x; done) LIST = $(foreach x,$(NLIST), do_something_with_$(x)_and_$(word $(x),$(OBJECTS))) 
+5
source

Source: https://habr.com/ru/post/1401031/


All Articles