[CMake] How to specify LD_LIBRARY_PATH for a test?

Marcus D. Hanwell marcus.hanwell at kitware.com
Tue Feb 26 14:29:49 EST 2013


On Tue, Feb 26, 2013 at 2:15 PM, Orion Poplawski <orion at cora.nwra.com> wrote:
> On 02/26/2013 12:06 PM, Alexander Neundorf wrote:
>>
>> On Tuesday 26 February 2013, Orion Poplawski wrote:
>>>
>>> We have some tests that load libraries at run time.  How can we specify
>>> that LD_LIBRARY_PATH needs to be set?
>>>
>>> file(WRITE ${CMAKE_SOURCE_DIR}/testsuite/launchtest.c "${LAUNCH}")
>>> add_executable(launchtest EXCLUDE_FROM_ALL
>>> ${CMAKE_SOURCE_DIR}/testsuite/launchtest.c)
>>> add_library(test_ce SHARED EXCLUDE_FROM_ALL libtest_ce.cpp)
>>> add_custom_target(check COMMAND ${CMAKE_BUILD_TOOL} test DEPENDS
>>> launchtest
>>> test_ce)
>>> get_target_property(LAUNCHTESTLOCATION launchtest LOCATION)
>>> execute_process(
>>>     COMMAND "fgrep" ".pro" "${BASE_SOURCE}/testsuite/Makefile.am"
>>>     COMMAND "awk" "{printf(\"%s;\",$1)}"
>>>     OUTPUT_VARIABLE TESTS)
>>> foreach(TEST ${TESTS})
>>>           add_test(${TEST} ${LAUNCHTESTLOCATION} ${TEST})
>>> endforeach(TEST TESTS)
>>>
>>> The test needs to be able to load the test_ce library at run-time.
>>
>>
>> If you didn't explicitely disable it, cmake builds the executables by
>> default
>> with the complete RPATH to all used libraries (the "build RPATH").
>> This is replaced during make install by the "install RPATH", which is by
>> default empty.
>> So it should actually just work.
>>
>> Alex
>>
>
> The tests are not being linked to the library.  The tests are dlopening them
> at run time.
>
You can set pretty much anything in the environment for a test. For example,

set_tests_properties(avogadro-${name} PROPERTIES
    ENVIRONMENT "AVOGADRO_PLUGIN_DIR=${AvogadroLibs_INSTALL_PREFIX}")

would set AVOGADRO_PLUGIN_DIR for a test. To see a full example,

https://github.com/OpenChemistry/avogadroapp/blob/master/tests/CMakeLists.txt

I think you could use that approach for LD_LIBRARY_PATH, or to tell
the dlopen call where to look on disk (as we do there).

Marcus


More information about the CMake mailing list