我试图在makefile中构建一个特性,它允许我指定一个库列表,这个列表是依赖于的特定库
这将允许库的受抚养人在重新构建库的依赖项时自动重建,并将依赖项添加到链接行中。
我在SO here上问了一个相关的问题,并给出了一个答案,我想出了下面的测试
uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
expand-deps = $1 $(foreach _,$1, $(call expand-deps,$($__deps)))
make-dep-list = $(call uniq,$(call expand-deps,$1))
define make-lib
$(warning $1_deps: $2)
# capture the list of libraries this library depends on
$1_deps = $2
endef
define make-bin
# show the fully expanded list of libraries this binary depends on
$(warning $1 dep-list: [$(call make-dep-list,$2)])
endef
#$(eval $(call make-lib, thread, log utils)) circular-dependency log->thread; thread->log
$(eval $(call make-lib, thread, utils))
$(eval $(call make-lib, log, thread))
$(eval $(call make-lib, order, log))
$(eval $(call make-lib, price, log))
$(eval $(call make-bin, test, order price))运行上述makefile将得到以下结果:
$ make
makefile:15: thread_deps: utils
makefile:16: log_deps: thread
makefile:17: order_deps: log
makefile:18: price_deps: log
makefile:19: test dep-list: [order price log thread utils ]
make: *** No targets. Stop.库有可能具有循环依赖关系。
例如:日志库是多线程的,因此需要线程库。线程库可以发出日志语句,因此需要日志库。
如果取消对具有循环依赖项的行的注释
$(eval $(call make-lib, thread, log utils))
$(eval $(call make-lib, log, thread))makefile将被困在一个无限循环中。
如何允许用户指定循环依赖项,并打破无限循环?
发布于 2015-06-15 17:04:27
因此,您的问题是,您正在递归地展开lib_deps (例如)。在这样做的同时,您又开始扩展lib_deps。无限循环(呃,堆栈崩溃)。为了阻止自己,你需要保留一张清单,列出你已经扩展过的东西。退出功能风格,将答案保留在全球expansion (呜!)中,如下所示:
expand-deps = \
$(foreach _,$1, \
$(if $(filter $_,${expansion}),, \
$(eval expansion += $_)$(call expand-deps,${$__deps})))
make-dep-list = $(eval expansion :=)$(call expand-deps,$1)${expansion}
define make-lib
$(warning $1_deps: $2)
# capture the list of libraries this library depends on
$1_deps := $2
endef
define make-bin
# show the fully expanded list of libraries this binary depends on
$(warning $1 dep-list: [$(call make-dep-list,$2)])
endef
$(eval $(call make-lib,thread,log utils))#circular-dependency log->thread; thread->log
#$(eval $(call make-lib,thread,utils))
$(eval $(call make-lib,log,thread))
$(eval $(call make-lib,order,log))
$(eval $(call make-lib,price,log))
$(eval $(call make-bin,test,order price))(作为练习,您可能希望用函数式重写它,即去掉全局$expansion,代之以传递给它的参数。)
https://stackoverflow.com/questions/30837891
复制相似问题