CMake: how to add Boost.Test cases with relative directories?

后端 未结 2 824
旧时难觅i
旧时难觅i 2020-12-10 07:05

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
         


        
2条回答
  •  萌比男神i
    2020-12-10 07:47

    A possible solution to disambiguate names in a directory structure like the one you have using a FOREACH() over ${test_cases} may be:

    # Set Cmake version and policy
    CMAKE_MINIMUM_REQUIRED( VERSION 2.8.7 )
    CMAKE_POLICY( VERSION 2.8.7 )
    
    PROJECT( DUMMY CXX )
    
    FILE( GLOB_RECURSE test_cases FOLLOW_SYMLINKS "test/*.[h,c]pp" )
    
    FOREACH( case ${test_cases} )
      ## Get filename without extension
      GET_FILENAME_COMPONENT(case_name_we ${case} NAME_WE)
      ## Get innermost directory name
      GET_FILENAME_COMPONENT(case_directory ${case} PATH)
      GET_FILENAME_COMPONENT(case_innermost ${case_directory} NAME_WE)
      ## Construct executable name
      SET( exe_name "${case_innermost}_${case_name_we}")
      ## Construct test name
      SET( test_name "${exe_name}_test")
      ## Add executable and test
      ADD_EXECUTABLE( ${exe_name} ${case} )
      ADD_TEST( ${test_name} ${exe_name} )
    ENDFOREACH()
    

    As you can see this CMakeLists.txt creates 4 distinct test/executable couples.

提交回复
热议问题