Trouble setting environment variables for CTest tests

前端 未结 2 760
粉色の甜心
粉色の甜心 2020-12-11 16:03

I\'m tasked with building python bindings for a c++-based project (using swig). The project uses cmake to build and ctest to test and the build and test of the bindings are

相关标签:
2条回答
  • 2020-12-11 16:39

    Craig Scott already provided a great solution via ENVIRONMENT test properties. I just want to add how this works on Windows with the Visual Studio generator and generator expressions:

    set_tests_properties(
        name_of_test_one
        name_of_test_two
    PROPERTIES
        # Make sure DLL is found by adding its directory to PATH
        ENVIRONMENT "PATH=$<TARGET_FILE_DIR:library_target_name>\;$ENV{PATH}"
    )
    

    Note 1: Instead of a colon, use an escaped semi-colon Note 2: I'm using the more flexible $ generator expression

    0 讨论(0)
  • 2020-12-11 16:52

    You can set environment variables as part of invoking the test by using the cmake -E env command. You can modify the add_test() call to something like the following:

    ADD_TEST(NAME testPyMyproj
        COMMAND ${CMAKE_COMMAND} -E env
            LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH}
            ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_scripts/test_pyMyproj.py
    )
    

    The above assumes a Unix-based host environment, but you could generalise this to support all platforms/generator types with a bit of work if you needed to.

    Another alternative is to use the ENVIRONMENT test property which should achieve essentially the same thing:

    set_tests_properties(testPyMyproj PROPERTIES
        ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH})
    
    0 讨论(0)
提交回复
热议问题