如何将文件名与Makefile中的用户定义函数相关联

时间:2019-06-13 09:05:40

标签: shell makefile gnu-make

我有一个makefile,它很大而且很复杂。所以我想将其拆分。拆分时,我遇到了一个有趣的问题。

假设我在某些生成文件中有一些用户定义的函数,即tools.mkcompile.mktest.mk

tools.mk看起来

define clean
    rm -rf some_folder
endef

compile.mk看起来

define generate_binaries
    ...
    creates some executables
    ...
endef

test.mk看起来

define run_test
    /bin/sh runtest.sh
endef

现在,我的Makefile接受了所有这些用户定义的功能并完成了一些工作。看起来像

include tools.mk
include compile.mk
include test.mk

.PHONY: build
build:
    $(call clean)
    $(call run_test)
    $(call generate_binaries)

问题来了。现在,我不知道clean函数是在哪个文件中定义的。因此,如果复杂性和规模增加,调试将是一个痛苦。我想要的是这样的东西

include tools.mk as tools
include compile.mk as compile
include test.mk as test

.PHONY: build
build:
    $(call tools.clean)
    $(call test.run_test)
    $(call compile.generate_binaries)

谁能告诉我如何实现这一目标?在此先感谢:)

2 个答案:

答案 0 :(得分:2)

您甚至无法像这样远程执行任何操作:

include tools.mk as tools

但是您可以运行make -p,它将准确显示所有变量的定义位置。

答案 1 :(得分:0)

C中也存在此问题,在C中不存在名称空间,并且使用#include导入模块。

C中的常规解决方案(我一直在使用它来解决makefile中的确切问题)是在makefile中定义的所有名称前加上表示该模块名称的前缀。

示例test.mk

define test_run_test
    /bin/sh runtest.sh
endef

和相应的Makefile

build:
    $(call tools_clean)
    $(call test_run_test)
    $(call compile_generate_binaries)

一旦习惯了,您还会想到更适合该约定的名称。

相关问题