makefile如何将其转换为宏

makefile how to turn this into a macro

本文关键字:转换 makefile      更新时间:2023-10-16

在尝试编写非递归make时,我有以下块:

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

这显然是不必要的重复。我怎么能写一个宏来折叠这一切,这样我只需要提供libAlibBlibC?我还需要在每个include之前设置dir

以下是如何按顺序在每个子目录列表中包含一个rules.mk makefile,并使变量DIR等于每个rules.mk中的子目录。我使用变量名DIR,而不是您想要的dir,因为我认为makefile中的"全局"变量名应该是大写的。

有关非递归makefile的其他实现细节,请参阅

evbergen.home.xs4all.nl/non-recurve-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))
LIBS := libA libB libC
include $(patsubst %, $(ROOT)/libs/%/rules.mk, $(LIBS))

如果您能够使用GNU make,那么以下操作应该有效:

get_mk := $(ROOT)/libs/$(1)/rules.mk
include $(call get_mk,libA)
include $(call get_mk,libB)
include $(call get_mk,libC)

有关call的更多信息,请访问http://www.gnu.org/software/make/manual/make.html#Call-函数。