Gnu Makefile的多行变量展开问题

torthtyo 2015-04-17 05:23:10
最近在玩Openjdk9,其中有些makefile的宏(多行变量)看不懂,不知到makefile的问题哪里发帖比较好,暂时发到这里,希望熟悉makefile的人能够多多指教。

Openjdk9 forest url:http://hg.openjdk.java.net/jdk9/jdk9

在root repo 下面的:make/Main。gmk 有一段代码调用eval展开多行变量:

################################################################################
# Targets for compiling native executables
$(eval $(call DeclareRecipesForPhase, LAUNCHER, \
TARGET_SUFFIX := launchers, \
FILE_PREFIX := Launcher, \
MAKE_SUBDIR := launcher, \
CHECK_MODULES := $(ALL_MODULES), \
USE_WRAPPER := true))

ALL_TARGETS += $(LAUNCHER_TARGETS)

################################################################################


多行变量的定义在:make/MakeHelpers.gmk

# Helper macro for DeclareRecipesForPhase
# Declare a recipe for calling the module and phase specific makefile.
# If there are multiple makefiles to call, create a rule for each topdir
# that contains a makefile with the target $module-$suffix-$repodir,
# (i.e: java.base-gensrc-jdk)
# Normally there is only one makefile, and the target will just be
# $module-$suffix
# Param 1: Name of list to add targets to
# Param 2: Module name
# Param 3: Topdir
define DeclareRecipeForModuleMakefile
ifeq ($$($1_MULTIPLE_MAKEFILES), true)
$2-$$($1_TARGET_SUFFIX): $2-$$($1_TARGET_SUFFIX)-$$(notdir $3)
$1 += $2-$$($1_TARGET_SUFFIX)-$$(notdir $3)

$2-$$($1_TARGET_SUFFIX)-$$(notdir $3):
else
$2-$$($1_TARGET_SUFFIX):
endif
$(ECHO) $(LOG_INFO) "Building $$@"
ifeq ($$($1_USE_WRAPPER), true)
+($(CD) $(SRC_ROOT)/make && $(MAKE) $(MAKE_ARGS) \
-f ModuleWrapper.gmk \
$$(addprefix -I, $$(wildcard $$(addprefix $3/, $(MAKE_MAKEDIR_LIST)) \
$$(addsuffix /$$($1_MAKE_SUBDIR), $$(addprefix $3/, $(MAKE_MAKEDIR_LIST))))) \
MODULE=$2 MAKEFILE_PREFIX=$$($1_FILE_PREFIX))
else
+($(CD) $$(dir $$(firstword $$(wildcard $$(patsubst %, \
$3/%/$$($1_MAKE_SUBDIR)/$$($1_FILE_PREFIX)-$2.gmk, $(MAKE_MAKEDIR_LIST))))) \
&& $(MAKE) $(MAKE_ARGS) \
-f $$($1_FILE_PREFIX)-$2.gmk \
$$(addprefix -I, $$(wildcard $$(addprefix $3/, $(MAKE_MAKEDIR_LIST)) \
$$(addsuffix /$3, $$(addprefix $3/, $(MAKE_MAKEDIR_LIST))))) \
MODULE=$2)
endif

endef

# Helper macro for DeclareRecipesForPhase
# Param 1: Name of list to add targets to
# Param 2: Module name
define DeclareRecipesForPhaseAndModule
$1_$2_TOPDIRS := $$(strip $$(sort $$(foreach d, $(MAKE_TOPDIR_LIST), \
$$(patsubst $$d/%, $$d, $$(filter $$d/%, \
$$(wildcard $$(patsubst %, %/$$($1_MAKE_SUBDIR)/$$($1_FILE_PREFIX)-$2.gmk, \
$$(foreach s, $(MAKE_MAKEDIR_LIST), \
$$(addsuffix /$$s, $(MAKE_TOPDIR_LIST))))))))))

# Only declare recipes if there are makefiles to call
ifneq ($$($1_$2_TOPDIRS), )
$$(foreach d, $$($1_$2_TOPDIRS), \
$$(eval $$(call DeclareRecipeForModuleMakefile,$1,$2,$$d)))
$1 += $2-$$($1_TARGET_SUFFIX)
$1_MODULES += $2
endif
endef

# Declare recipes for a specific module and build phase if there are makefiles
# present for the specific combination.
# Param 1: Name of list to add targets to
# Named params:
# TARGET_SUFFIX : Suffix of target to create for recipe
# MAKE_SUBDIR : Subdir for this build phase
# FILE_PREFIX : File prefix for this build phase
# USE_WRAPPER : Set to true to use ModuleWrapper.gmk
# CHECK_MODULES : List of modules to try
# MULTIPLE_MAKEFILES : Set to true to handle makefils for the same module in
# phase in multiple repos
# Exported variables:
# $1_MODULES : All modules that had rules generated
# $1_TARGETS : All targets generated
define DeclareRecipesForPhase
$(foreach i,2 3 4 5 6 7, $(if $($i),$(strip $1)_$(strip $($i)))$(NEWLINE))
$(if $(8),$(error Internal makefile error: Too many arguments to \
DeclareRecipesForPhase, please update MakeHelper.gmk))

$$(foreach m, $$($(strip $1)_CHECK_MODULES), \
$$(eval $$(call DeclareRecipesForPhaseAndModule,$(strip $1),$$m)))

$(strip $1)_TARGETS := $$($(strip $1))
endef

################################################################################


里面$(foreach i,2 3 4 5 6 7, $(if $($i),$(strip $1)_$(strip $($i)))$(NEWLINE))这句就没看懂。后面貌似很多变量有用到,比如 $$(foreach m, $$($(strip $1)_CHECK_MODULES) 前面 $(strip $1)_$(strip $($i)))$(NEWLINE) 定义的变量在后面用到了,但是我理解前面只是打印出$(strip $1)_$(strip $($i)))$(NEWLINE) 并没有赋值,这样算定义了一个变量吗?

我看到后面很多代码引用到了第一个foreach 定义的变量,如:$$($(strip $1)_CHECK_MODULES), $$($1_MAKE_SUBDIR)/$$($1_FILE_PREFIX)。有没有熟悉named param的解释一下?
...全文
194 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-04-17
  • 打赏
  • 举报
回复
搜“跟我一起写makefile”
mLee79 2015-04-17
  • 打赏
  • 举报
回复
$(foreach i,2 3 4 5 6 7, $(if $($i),$(strip $1)_$(strip $($i)))$(NEWLINE)) ------------------------------------------------------------------------------------------- $(eval LAUNCHER_TARGET_SUFFIX := launchers LAUNCHER_FILE_PREFIX := Launcher ..... LAUNCHER_USE_WRAPPER := true ) 其他自己带进去跟. 在 Makefile 里多用 $(info .... ) $(error ....) 打 trace, 看不懂的地方就加个 $(error ..... ) 上去看看展开是什么..
mLee79 2015-04-17
  • 打赏
  • 举报
回复
$(foreach i,2 3 4 5 6 7, $(if $($i),$(strip $1)_$(strip $($i)))$(NEWLINE)) ------------------------------------------------------------------------------------------- $(eval LAUNCHER_TARGET_SUFFIX := launchers LAUNCHER_FILE_PREFIX := Launcher ..... LAUNCHER_USE_WRAPPER := true ) 其他自己带进去跟. 在 Makefile 里多用 $(info .... ) $(error ....) 打 trace, 看不懂的地方就加个 $(error ..... ) 上去看看展开是什么..

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧