我正在将boost-build构建系统转换为cmake。
boost-build的一个特性是,您可以指定一个Jamfile的路径(相当于一个CMakeLists.txt文件),其中指定的所有目标都将被生成。
例如,具有以下项目结构:
root
|
+--- foo
| |
| +--- test
|
+--- bar
| |
| +--- test
|
+--- app如果输入以下命令:
$ b2 foo将执行root/foo下的root/foo,生成foo库,构建并运行test测试。
boost构建示例
下面是一个使用boost-build的简单构建配置
Jamroot**:**
using gcc ;
project proj : requirements
<link>static
<include>.
;
build-project foo ;foo/Jamfile**:**
lib foo : [ glob *.cpp ] ;
build-project test ;foo/test/Jamfile**:**
import testing ;
unit-test foo-tests
: [ glob *.cpp ]
..//foo
;您会注意到,在foo's Jamfile中有一个指令build-project test
这意味着,如果我键入b2 foo,那么lib/Jamfile中的所有内容都将被执行,从而生成foo 和 foo/test。
而且,在Jamroot中有一个指令build-project foo
这意味着,如果我只键入b2,那么Jamroot中的所有内容都将被执行,从而生成foo和foo/test。
因此,构建整个项目并获得所有源代码和构建的所有测试都很容易。
构建也很容易,只需一个子目录,只需要--它是源代码和测试生成的.
这就是我想复制的行为。
cmake实例
root/CMakeLists.txt**:**
cmake_minimum_required(VERSION 3.2.2)
project(proj CXX)
add_subdirectory(foo)foo/CMakeLists.txt**:**
file(GLOB src "*.cpp")
add_library(foo STATIC ${src})
add_subdirectory(test)foo/test/CMakeLists.txt**:**
file(GLOB src "*.cpp")
add_executable(foo_test ${src})
add_test(foo_test foo_test foo)
# run tests if foo_test.passed is missing or outdated
add_custom_command(
OUTPUT foo_test.passed
COMMAND foo_test
COMMAND ${CMAKE_COMMAND} -E touch foo_test.passed
DEPENDS foo_test
)
# make tests run as part of ALL target
add_custom_target(run_foo_test
ALL
DEPENDS foo_test.passed)上面的CMakeLists.txt结构允许我使用make并同时构建foo和foo_test。
但是,如果我指定make foo,只会构建 foo,但是foo_test不会构建,测试也不会运行。
问题:
foo/CMakeLists.txt时,如何构建make foo中的所有内容foo_test.passed作为更新目标foo 和构建的一部分作为ALL目标的一部分?发布于 2016-04-18 19:37:33
下面是一个实现需求的实现。
这有点复杂,因为它需要几个虚假的目标和依赖链。
步骤1:
ALL中,以便将其作为全局构建过程的一部分构建。foo/CMakeLists.txt
# create a phony target which all 'foo' related items will be added to
add_custom_target(foo
ALL
)步骤2:
libfoo**:**
foo重命名为libfoo,因为我们现在已经将foo用于早期的虚假目标。liblibfoo.a,这有点难看)foo/CMakeLists.txt
add_library(libfoo STATIC ${src})
# add libfoo as a dependency of foo, so 'make foo' will build libfoo
add_dependencies(foo
libfoo)foo_test**:**
让测试作为构建的一部分自动运行有点费解。
你必须:
custom_command,并在测试通过时生成一个哨兵文件(foo_test.passed)custom_target (foo_test.run),它依赖于哨兵(foo_test.passed)foo和foo_test.run之间添加一个依赖项foo_test/CMakeLists.txt
add_executable (foo_test main.cpp)
target_link_libraries(foo_test libfoo ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
# create foo_test.passed command which generates the sentinel file when the tests pass
add_custom_command(
OUTPUT foo_test.passed
COMMAND foo_test
COMMAND ${CMAKE_COMMAND} -E touch foo_test.passed
DEPENDS foo_test
)
# create foo_test.run target which depends on foo_test.passed
add_custom_target(foo_test.run
DEPENDS foo_test.passed
)
# add foo_test.run as a dependency of foo, so 'make foo' will build foo_test.run
add_dependencies(foo
foo_test.run
)因此,目标foo将libfoo和foo_test.run作为依赖项。
因此,make和make foo都构建了libfoo,构建并运行了foo_test (通过foo_test.run和foo_test.passed)
感知的笨重:
foo -> foo_test.run -> foo_test.passed -> foo_test依赖链。boost-build中,您可以命名库foo,而不会导致foo虚假目标和foo库之间的冲突( b2 foo构建foo库及其测试)然而,它的工作,在没有一个更优雅的解决方案,将给我我想要的。
https://stackoverflow.com/questions/36701286
复制相似问题