简体   繁体   English

makefile 库依赖 - 解决循环依赖

[英]makefile library dependencies - resolve circular dependency

I am trying to build a feature into my makefile which allows me to specify a list of libraries a particular library depends on我正在尝试在我的 makefile 中构建一个功能,它允许我指定特定库所依赖的库列表

This will allow dependants of a library to automatically be rebuilt if that library's dependencies are rebuilt, and also have the dependencies added to the link line.如果库的依赖项被重建,这将允许库的依赖项自动重建,并且还将依赖项添加到链接行。

I asked a related question on SO here , and working through a given answer, I came up with the following test我在这里问了一个关于 SO 的相关问题,并通过了一个给定的答案,我想出了以下测试

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))

running the above makefile yields the following results:运行上面的 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.

It is possible for libraries to have circular dependencies.库可能具有循环依赖关系。

As an example: The logging library is multi-threaded, so requires the thread library.例如:日志库是多线程的,因此需要线程库。 The thread library can issue log statements, so requires the log library.线程库可以发出日志语句,所以需要日志库。

If I uncomment out the line which has a circular dependency如果我取消注释掉循环依赖的行

$(eval $(call make-lib, thread, log utils))
$(eval $(call make-lib, log, thread))

The makefile will get stuck in an infinite loop. makefile 将陷入无限循环。

How can I allow users to specify circular dependencies, and break out of the infinite loop?如何让用户指定循环依赖,跳出无限循环?

So, your problem is that you are recursively expanding lib_deps (say).所以,你的问题是你递归地扩展lib_deps (比如说)。 While doing that you start expanding lib_deps again.在这样做的同时,您再次开始扩展lib_deps Infinite loop (er, stack crash).无限循环(呃,堆栈崩溃)。 To stop yourself you need to keep a list of things that you have already expanded.为了阻止自己,你需要保留一份你已经扩展过的东西的清单。 Falling out of functional style and keeping the answer in the global expansion (ugh!), something like:脱离函数式风格并在全球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))

(As an exercise you might like to rewrite this in functional style, ie, get rid of the global $expansion replacing it with a parameter that gets passed around.) (作为练习,您可能想以函数式风格重写它,即,摆脱全局$expansion将其替换为传递的参数。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM