Search code examples
visual-c++cmakectest

Setting CTest (cmake) environment variables configuration specific


In a cmake script I have a suite of tests that depend of a set plugins (whose output varies for configuration in Windows Debug/Release/RelWithDebInfo etc).

And I want and env variable with the right path, like

if(WIN32)
    set_tests_properties(${my_test} PROPERTIES ENVIRONMENT "MYVAR=\\dir_for_plugins\\debug")
endif()

which obviously only works for Debug builds.

I have tried either

if(WIN32)
  foreach(cfg_ ${CMAKE_CONFIGURATION_TYPES})
    set_tests_properties(${test_name} PROPERTIES
      ENVIRONMENT_${cfg__} "MYVAR=\\dir_for_plugins\\${cfg__}")
  endforeach()   
endif

if(WIN32)
    set_tests_properties(${my_test} PROPERTIES ENVIRONMENT "MYVAR=\\dir_for_plugins\\env{MY_CONFIG_TYPE}")
endif

if(WIN32)
    set_tests_properties(${my_test} PROPERTIES ENVIRONMENT "MYVAR=\\dir_for_plugins\\%MY_CONFIG_TYPE%")
endif

But none seems to work for all configurations, because the env is resolved at cmake-time. Is there a way to do lazy evaluation of these expressions until the proper suite test for a given config is running?


Solution

  • Meh, I found an option, with "Generator Expressions" https://cmake.org/cmake/help/v3.3/manual/cmake-generator-expressions.7.html

    so using "$<CONFIG>":

    if(WIN32)
        set_tests_properties(${my_test} PROPERTIES ENVIRONMENT "MYVAR=\\dir_for_plugins\\$<CONFIG>")
    endif()