简体   繁体   English

从搜索目录创建目标

[英]Creating target from searched directories

I want to create separate executables for each directory named "test" in my project.我想为我的项目中名为“test”的每个目录创建单独的可执行文件。 Test directory has structure like this: test.mk测试目录的结构如下: test.mk

test (dir)
|__test.mk
|__testRun.c
|__testRun.h

test.mk contains dependencies for specific test, testRun.c is the test itself. test.mk包含特定测试的依赖项, testRun.c是测试本身。

So hierarchy looks like this:所以层次结构看起来像这样:

ROOT
|__Makefile
|__Module1 (dir)
  |__some files
  |__test (dir)
|__Module2 (dir)
   |__some files
   |__test (dir)
|__Module3 (dir)
   |__some files
   |__test (dir)
|__outdir  (dir)
   |__test1 (exec)
   |__test2 (exec)
   |__test3 (exec)

I want to run single Makefile in root parent directory.我想在根父目录中运行单个Makefile I want it to search for all test directories and create test executables in outdirectory .我希望它搜索所有test目录并在outdirectory创建测试可执行文件。 So far I have this:到目前为止,我有这个:

Makefile生成文件

CFLAGS =    -I$(DIRTEST) -I$(DIRTEST)/..
OUTDIR=     ./outdirectory
DIRTEST =   $(shell find -type d -not -path "$(OUTDIR)/*" -name "test" -prune )

I have no idea how to create rule that creates target executable from sources and headers from separate directories.我不知道如何创建从不同目录的源和头文件创建目标可执行文件的规则。 I was looking for solution for similar problem, but no luck so far.我正在寻找类似问题的解决方案,但到目前为止没有运气。

Note: I do not want to use recursive make.注意:我不想使用递归 make。 test.mk looks for example like this: test.mk看起来像这样:

SRCTEST = module1/test/testRun.c \
          module1/module1.c
CFLAGS += -Imodule1

You can use bash scripts files for building variables, in case if you are avoiding a recursive build.您可以使用 bash 脚本文件来构建变量,以防您避免递归构建。

cat module1/test/test.sh 
export SRCTEST="module1/test/testRun.c 
    module1/module1.c"
export CFLAGS="$CFLAGS -Imodule1"

cat module2/test/test.sh 
export SRCTEST="module2/test/testRun.c
          module2/module2.c"
export CFLAGS="$CFLAGS -Imodule2"

Then your Makefile could looks like:那么你的Makefile可能看起来像:

test1:
    source module1/test/test.sh && gcc -o $@ $$CFLAGS $$SRCTEST
test2:
    source module2/test/test.sh && gcc -o $@ $$CFLAGS $$SRCTEST

Also you can do something with include instruction.你也可以用include指令做一些事情。 Redefinition could be avoided by checking the build target, but this is not a good way.通过检查构建目标可以避免重新定义,但这不是一个好方法。

Your examples are not clear enough for us to answer.你的例子不够清楚,我们无法回答。

For one thing, tt's not clear how the name of the test executable is related to the name of the module.一方面,tt 不清楚测试可执行文件的名称与模块名称的关系。 Is it really the case that the modules end with a number and the generated test program should also end with that same number ( Module1 -> test1 )?模块是否真的以数字结尾并且生成的测试程序也应该以相同的数字结尾( Module1 -> test1 )? Or is there some other relationship?还是有其他什么关系? There doesn't appear to be any variable containing a test name in the test.mk file so how is the test name computed?. test.mk文件中似乎没有任何包含测试名称的变量,那么测试名称是如何计算的?。

Second, it will make your life very difficult if you redefine the same variables in every test.mk file.其次,如果您在每个test.mk文件中重新定义相同的变量,这将使您的生活变得非常困难。 There is no "scoping" in make that will allow you to say "this instance of CFLAGS is used only for this included makefile". make 中没有“范围”允许您说“此CFLAGS实例仅用于此包含的 makefile”。 Each time you include a different test.mk file it will overwrite the previous settings.每次包含不同的test.mk文件时,它都会覆盖以前的设置。

It CAN be done: you'll need to use a combination of define variables to hold rule definitions, then eval to evaluate the rules.这是可以做到:你需要使用的组合define变量来保存规则定义,然后eval评估规则。

Let's suppose that you added a new variable to test.mk which defined the test name, and you qualified your variables with this name as well;假设您向test.mk添加了一个定义测试名称的新变量,并且您也使用此名称限定了变量; then your life is much easier:那么你的生活就容易多了:

Module1/test/test.mk would contain: Module1/test/test.mk将包含:

TESTNAME = test1
test1_SRCTEST = module1/test/testRun.c \
                module1/module1.c
test1_CFLAGS = -Imodule1

Now in your main makefile you would create a variable holding the rule you wanted to define:现在在你的主 makefile 中,你将创建一个变量来保存你想要定义的规则:

define MAKETEST
include $T
$$(OUTDIR)/$$(TESTNAME): $$(OUTDIR)/% : $$($$(TESTNAME)_SRCTEST)
        $$(CC) $$(CPPFLAGS) $$(CFLAGS) $$($$*_CFLAGS) -o $$@ $$^
endef

$(foreach T,$(DIRTEST),$(eval $(MAKETEST)))

Note, this is untested.请注意,这是未经测试的。

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

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