CMakeLists.txt 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. cmake_minimum_required(VERSION 3.9)
  2. project(CTestConfig)
  3. include(CTest)
  4. # We expect this configure to occur through a 'ctest -D Experimental' or a
  5. # 'ctest -S script.cmake' call.
  6. #
  7. # In either case, we expect CMAKE_BUILD_TYPE to be defined for single-configuration
  8. # build trees and not defined for multi-configuration build trees. The value of
  9. # CMAKE_CONFIGURATION_TYPES should not be relied upon to determine whether we
  10. # are using a multi-config generator or not, the GENERATOR_IS_MULTI_CONFIG
  11. # global property is the canonical way to do that as of CMake 3.9.
  12. #
  13. get_property(_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  14. if(_isMultiConfig)
  15. if(NOT DEFINED CMAKE_CONFIGURATION_TYPES OR CMAKE_CONFIGURATION_TYPES STREQUAL "")
  16. message(FATAL_ERROR "CMAKE_CONFIGURATION_TYPES is not defined or is empty "
  17. "(but must be defined and non-empty for a multi-configuration generator)")
  18. endif()
  19. if(DEFINED CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE STREQUAL "")
  20. message(FATAL_ERROR "CMAKE_BUILD_TYPE='${CMAKE_BUILD_TYPE}' is defined and non-empty "
  21. "(but should not be for a multi-configuration generator)")
  22. endif()
  23. set(_configs ${CMAKE_CONFIGURATION_TYPES})
  24. else()
  25. # Populating CMAKE_CONFIGURATION_TYPES even for single config generators is
  26. # common enough for user projects that we don't want to consider it an error.
  27. # We just need CMAKE_BUILD_TYPE to be set and ignore CMAKE_CONFIGURATION_TYPES.
  28. if(NOT DEFINED CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
  29. message(FATAL_ERROR "CMAKE_BUILD_TYPE is not defined or is empty "
  30. "(but should be defined and non-empty for a single-configuration generator)")
  31. endif()
  32. add_definitions(-DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}")
  33. set(_configs ${CMAKE_BUILD_TYPE})
  34. endif()
  35. add_executable(ctc CTestConfig.cxx)
  36. foreach(cfg ${_configs})
  37. add_test(NAME ctc-${cfg} CONFIGURATIONS ${cfg} COMMAND ctc --config $<CONFIGURATION>)
  38. if(_isMultiConfig)
  39. set_property(TEST ctc-${cfg}
  40. PROPERTY PASS_REGULAR_EXPRESSION "CMAKE_INTDIR is ${cfg}")
  41. set_property(TEST ctc-${cfg}
  42. PROPERTY FAIL_REGULAR_EXPRESSION "CMAKE_BUILD_TYPE is")
  43. else()
  44. set_property(TEST ctc-${cfg}
  45. PROPERTY PASS_REGULAR_EXPRESSION "CMAKE_BUILD_TYPE is ${cfg}")
  46. set_property(TEST ctc-${cfg}
  47. PROPERTY FAIL_REGULAR_EXPRESSION "CMAKE_INTDIR is")
  48. endif()
  49. endforeach()