Barry
Barry

Reputation: 303017

makefile how to turn this into a macro

In trying to write a non-recursive make, I have the following block:

dir := $(ROOT)/libs/libA/
include $(dir)/rules.mk

dir := $(ROOT)/libs/libB/
include $(dir)/rules.mk

dir := $(ROOT)/libs/libC/
include $(dir)/rules.mk

This is obviously unnecessary repetitive. How can I write a macro to collapse that all so that I just have to provide libA, libB, and libC? I need to also set dir before each include.

Upvotes: 0

Views: 183

Answers (3)

Mark Galeck
Mark Galeck

Reputation: 6385

Here is how to include a rules.mk makefile inside each of the list of subdirectories, in order, and make the variable DIR to be equal to the subdirectory, inside each rules.mk. I use the variable name DIR, not dir as you wanted, because I believe "global" variables names in makefiles should be uppercase.

For other implementation details of non-recursive makefiles, please consult

evbergen.home.xs4all.nl/nonrecursive-make.html

# include the makefile $2/rules.mk
# and make the variable $1 be equal to the directory $2, inside that makefile
define INCLUDE_DIR
$1 := $2
include $$($1)/rules.mk
endef

# do INCLUDE_DIR for directory variable name $1 and directories $2
define INCLUDE_DIRS
$(foreach dir, $2, $(eval $(call INCLUDE_DIR,$1, $(dir))))
endef


DIRS := \
    $(ROOT)/libs/libA \
    $(ROOT)/libs/libB \


$(call INCLUDE_DIRS,DIR, $(DIRS))

Upvotes: 1

R Sahu
R Sahu

Reputation: 206577

If you are able to use GNU make, the following should work:

get_mk := $(ROOT)/libs/$(1)/rules.mk

include $(call get_mk,libA)
include $(call get_mk,libB)
include $(call get_mk,libC)

You can get more info on call at http://www.gnu.org/software/make/manual/make.html#Call-Function.

Upvotes: 0

Beta
Beta

Reputation: 99094

LIBS := libA libB libC

include $(patsubst %, $(ROOT)/libs/%/rules.mk, $(LIBS))

Upvotes: 1

Related Questions