Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile find in array

Tags:

makefile

If I have something like this:

PROJECTS += path/to/first
PROJECTS += path/to/second
PROJECTS += path/to/third

and

LIBS += lib_output/first.lib
LIBS += lib_output/second.lib
LIBS += lib_output/third.lib

How could I associate the project from PROJECTS += path/to/first with LIBS += lib_output/first.lib? Is there something like a hashmap available in a makefile? Or possibility to search in an array?

like image 588
Geo Avatar asked Sep 02 '11 10:09

Geo


People also ask

What is Findstring in makefile?

The findstring function is what your heart desires: $(findstring find,in) Searches in for an occurrence of find. If it occurs, the value is find; otherwise, the value is empty. You can use this function in a conditional to test for the presence of a specific substring in a given string.

How do I use IFEQ in makefile?

The ifeq directive begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared.

What is target in makefile?

A simple makefile consists of “rules” with the following shape: target … : prerequisites … recipe … … A target is usually the name of a file that is generated by a program; examples of targets are executable or object files. A target can also be the name of an action to carry out, such as ' clean ' (see Phony Targets).


2 Answers

You can simulate lookup tables using computed variable names and the fact that make variable names can include some special characters like dot and forward slash:

PROJECTS += path/to/first
PROJECTS += path/to/second
PROJECTS += path/to/third

LIBS += lib_output/first.lib
LIBS += lib_output/second.lib
LIBS += lib_output/third.lib

lookup.path/to/first  := lib_output/first.lib
lookup.path/to/second := lib_output/second.lib
lookup.path/to/third  := lib_output/third.lib

path := path/to/first
$(info ${path} -> ${lookup.${path}})
path := path/to/second
$(info ${path} -> ${lookup.${path}})
path := path/to/third
$(info ${path} -> ${lookup.${path}})

Outputs:

$ make
path/to/first -> lib_output/first.lib
path/to/second -> lib_output/second.lib
path/to/third -> lib_output/third.lib
like image 140
Maxim Egorushkin Avatar answered Oct 21 '22 11:10

Maxim Egorushkin


I'm not sure if I completely understand your question, but I think the word function might be what you need (it may be a GNU make extension):

$(word 2, $(PROJECTS)) returns path/to/second,
$(word 2, $(LIBS)) returns lib_output/second.lib.

like image 43
eriktous Avatar answered Oct 21 '22 09:10

eriktous