• <bdo id='Mnewy'></bdo><ul id='Mnewy'></ul>
    1. <tfoot id='Mnewy'></tfoot>

    2. <small id='Mnewy'></small><noframes id='Mnewy'>

    3. <i id='Mnewy'><tr id='Mnewy'><dt id='Mnewy'><q id='Mnewy'><span id='Mnewy'><b id='Mnewy'><form id='Mnewy'><ins id='Mnewy'></ins><ul id='Mnewy'></ul><sub id='Mnewy'></sub></form><legend id='Mnewy'></legend><bdo id='Mnewy'><pre id='Mnewy'><center id='Mnewy'></center></pre></bdo></b><th id='Mnewy'></th></span></q></dt></tr></i><div id='Mnewy'><tfoot id='Mnewy'></tfoot><dl id='Mnewy'><fieldset id='Mnewy'></fieldset></dl></div>
      <legend id='Mnewy'><style id='Mnewy'><dir id='Mnewy'><q id='Mnewy'></q></dir></style></legend>
    4. CMake:如何使用相关目录添加 Boost.Test 用例?

      时间:2023-07-19
      <tfoot id='IdnY4'></tfoot>
      <legend id='IdnY4'><style id='IdnY4'><dir id='IdnY4'><q id='IdnY4'></q></dir></style></legend>
        <tbody id='IdnY4'></tbody>

            <small id='IdnY4'></small><noframes id='IdnY4'>

            • <bdo id='IdnY4'></bdo><ul id='IdnY4'></ul>

                <i id='IdnY4'><tr id='IdnY4'><dt id='IdnY4'><q id='IdnY4'><span id='IdnY4'><b id='IdnY4'><form id='IdnY4'><ins id='IdnY4'></ins><ul id='IdnY4'></ul><sub id='IdnY4'></sub></form><legend id='IdnY4'></legend><bdo id='IdnY4'><pre id='IdnY4'><center id='IdnY4'></center></pre></bdo></b><th id='IdnY4'></th></span></q></dt></tr></i><div id='IdnY4'><tfoot id='IdnY4'></tfoot><dl id='IdnY4'><fieldset id='IdnY4'></fieldset></dl></div>

                本文介绍了CMake:如何使用相关目录添加 Boost.Test 用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个使用 CMake 和 Boost.Test 的工作项目,其目录结构如下(请原谅 ASCII 艺术):

                I have a working project with CMake and Boost.Test with a directory structure like this (pardon the ASCII art):

                +-proj
                |---CMakeLists.txt
                |---build
                |---test
                |----dir1
                |   ----foo.cpp // contains one BOOST_AUTO_TEST_SUITE and several BOOST_AUTO_TEST_CASE
                |    |---bar.cpp // contains one BOOST_AUTO_TEST_SUITE and several BOOST_AUTO_TEST_CASE
                 ----dir2
                    ----foo.cpp // contains one BOOST_AUTO_TEST_SUITE and several BOOST_AUTO_TEST_CASE
                     |---bar.cpp // contains one BOOST_AUTO_TEST_SUITE and several BOOST_AUTO_TEST_CASE
                

                我目前将所有源文件编译成一个可以使用 CTest 运行的大型可执行文件.我的 CMakeLists.txt 看起来像这样:

                I currently compile all source files into one big executable that I can run with CTest. My CMakeLists.txt looks like this:

                file(GLOB_RECURSE test_cases FOLLOW_SYMLINKS "test/*.[h,c]pp")
                add_executable(test_suite ${test_cases})
                include_directories(${PROJECT_SOURCE_DIR} ${Boost_INCLUDE_DIRS})
                target_link_libraries(test_suite ${Boost_LIBRARIES})
                include(CTest)
                add_test(test_runner test_suite)
                

                我想将每个 .cpp 文件编译成一个单独的可执行文件,并将其单独添加为测试,以便我可以使用 CTest 正则表达式机制(尤其是 Boost.Test 似乎没有的测试排除)有选择地运行某些测试.但是,当 CMake 从 dir1/dir2 为 foo/bar 生成构建目标时,我遇到了名称冲突.

                I would like to compile each .cpp file into a separate executable, and add it separately as a test so that I can use the CTest regular expression machinery (especially the test exclusion which Boost.Test doesn't seem to have) to selectively run certain tests. However, I get a name conflict when CMake is generating build targets for foo/bar from dir1/dir2.

                我的问题是:如何将 test 下的整个目录树镜像到 build 下的类似树,以便不再有各种可执行文件之间的名称冲突,以便 CTest 可以运行它们?

                My question is: how can I mirror the entire directory tree under test to a similar tree under build so that there are no more name conflicts between the various executables and so that CTest can run them all?

                注意:不能在源代码树中重命名它们.我想对变量 ${test_cases} 执行 foreach() (如 这个答案),但我无法提取相关目录和文件名,并将它们移植到文件一个文件的 build/ 目录中基础.

                Note: Renaming them in the source tree is not an option. I'd like to do a foreach() over the variable ${test_cases} (as explained in this answer), but I am having trouble to extract the relative directory and the file name, and port those to the build/ directory on a file-by-file basis.

                UPDATE:最后,我拼凑了这个脚本:

                UPDATE: In the end, I pieced together this script:

                # get the test sources
                file(GLOB_RECURSE test_sources RELATIVE ${PROJECT_SOURCE_DIR} *.cpp)
                
                # except any CMake generated sources under build/
                string(REGEX REPLACE "build/[^;]+;?" "" test_sources "${test_sources}")
                
                # get the test headers
                file(GLOB_RECURSE test_headers RELATIVE ${PROJECT_SOURCE_DIR} *.hpp)
                
                # except any CMake generated headers under build/
                string(REGEX REPLACE "build/[^;]+;?" "" test_headers "${test_headers}")
                
                # compile against the test headers, the parent project, and the Boost libraries
                include_directories(${PROJECT_SOURCE_DIR} ${ParentProject_include_dirs} ${Boost_INCLUDE_DIRS})
                
                # calls enable_testing()
                include(CTest)
                
                foreach(t ${test_sources} )
                  # get the relative path in the source tree
                  get_filename_component(test_path ${t} PATH)
                
                  # get the source name without extension
                  get_filename_component(test_name ${t} NAME_WE)
                
                  # concatenate the relative path and name in an underscore separated identifier
                  string(REPLACE "/" "_" test_concat "${test_path}/${test_name}")
                
                  # strip the leading "test_" part from the test ID
                  string(REGEX REPLACE "^test_" "" test_id ${test_concat})
                
                  # depend on the current source file, all the test headers, and the parent project headers
                  add_executable(${test_id} ${t} ${test_headers} ${ParentProject_headers})
                
                  # link against the Boost libraries
                  target_link_libraries(${test_id} ${Boost_LIBRARIES})
                
                  # match the relative path in the build tree with the corresponding one in the source tree 
                  set_target_properties(${test_id} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${test_path})
                
                  # add a test with executable in the relative path of the build tree
                  add_test(${test_id} ${test_path}/${test_id})
                endforeach()
                

                推荐答案

                可以指定 RELATIVE 标志和文件的目录( GLOB ... ) 命令.虽然在文件(GLOB)文档中没有直接提到a>,这也适用于 file( GLOB_RECURSE ... ) .请注意,我在 Windows 设置中对此进行了测试.我不知道 *nix.

                It is possible to specify a RELATIVE flag and a directory to a file( GLOB ... ) command. Although not mentioned directly in the documentation of file( GLOB ), this works for file( GLOB_RECURSE ... ) too. Note, I tested this on my windows setup. I don't known about *nix.

                1. 连同一些 get_filename_component 调用与 <代码>NAME_WE和/或 PATH 标志,现在可以重建名称和cpp 文件相对于 globbing 目录的相对路径.
                2. 提取路径和名称(不带扩展名)大多类似马西米利亚诺的回答.另外,我用过他的建议使用 string( REGEX REPLACE ... ) 生成唯一的测试名称;用下划线替换正斜杠.
                3. 使用唯一的测试名称,可以生成可执行文件,然后可以使用 set_target_properties.
                1. Together with some get_filename_component calls with NAME_WE and/or PATH flags, it is now possible to reconstruct the name and the relative path of the cpp-file with respect to the globbing dir.
                2. Extracting a path and a name (without extension) is mostly similar to the answer by Massimiliano. In addition, I have used his suggestion to generate a unique testname with string( REGEX REPLACE ... ); replacing forward slashes by underscores.
                3. With a unique test-name, the executable can be generated and afterwards its output directory can be modified with set_target_properties.

                检查这个 和 这个问题有关修改输出目录的更多信息.

                Check this and this question for more info on modifying the output directory.

                file( GLOB_RECURSE TEST_CPP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
                
                foreach( test_case ${TEST_CPP_SOURCES} )
                    # Get the name without extension
                    get_filename_component( test_name ${test_case} NAME_WE )
                    # Get the path to the test-case, relative to the ${CMAKE_CURRENT_SOURCE_DIR} 
                    # thanks to the RELATIVE flag in file( GLOB_RECURSE ... )
                    get_filename_component( test_path ${test_case} PATH )
                
                    message( STATUS "  name = " ${test_name} )
                    message( STATUS "  path = " ${test_path} )
                    # I would suggests constructing a 'unique' test-name
                    string( REPLACE "/" "_" full_testcase "${test_name}/${test_path}" )
                
                    # Add an executable using the 'unique' test-name
                    message( STATUS "  added " ${full_testcase} " in " ${test_path} )
                    add_executable( ${full_testcase} ${test_case} )
                    # and modify its output paths. 
                    set_target_properties( ${full_testcase} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${test_path} )
                endforeach( test_case ${TEST_CPP_SOURCES} )
                

                这篇关于CMake:如何使用相关目录添加 Boost.Test 用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 LLVM 将问题与 OSX 上的 boost::program_options 链接起来 下一篇:你能在 boost asio 中设置 SO_RCVTIMEO 和 SO_SNDTIMEO 套接字选项吗?

                相关文章

                <legend id='TWivN'><style id='TWivN'><dir id='TWivN'><q id='TWivN'></q></dir></style></legend>

                <small id='TWivN'></small><noframes id='TWivN'>

                <tfoot id='TWivN'></tfoot>
                    • <bdo id='TWivN'></bdo><ul id='TWivN'></ul>
                    <i id='TWivN'><tr id='TWivN'><dt id='TWivN'><q id='TWivN'><span id='TWivN'><b id='TWivN'><form id='TWivN'><ins id='TWivN'></ins><ul id='TWivN'></ul><sub id='TWivN'></sub></form><legend id='TWivN'></legend><bdo id='TWivN'><pre id='TWivN'><center id='TWivN'></center></pre></bdo></b><th id='TWivN'></th></span></q></dt></tr></i><div id='TWivN'><tfoot id='TWivN'></tfoot><dl id='TWivN'><fieldset id='TWivN'></fieldset></dl></div>