bilke / cmake-modules

Additional CMake functionality. Most of the modules are from Ryan Pavlik (https://github.com/rpavlik/cmake-modules)
Boost Software License 1.0
548 stars 213 forks source link

Is there a way to specify environment variables when running LCOV_EXEC_TESTS_CMD? #57

Closed ahtsan closed 3 years ago

ahtsan commented 3 years ago

In add_custom_target,

    add_custom_target(${Coverage_NAME}
        COMMAND ${LCOV_CLEAN_CMD}
        COMMAND ${LCOV_BASELINE_CMD} 
        COMMAND ${LCOV_EXEC_TESTS_CMD}
        COMMAND ${LCOV_CAPTURE_CMD}
        COMMAND ${LCOV_BASELINE_COUNT_CMD}
        COMMAND ${LCOV_FILTER_CMD} 
        COMMAND ${LCOV_GEN_HTML_CMD}

${LCOV_EXEC_TESTS_CMD} is resolved to the full path of the executable. However, when I changed the COMMAND to something like

COMMAND ${Coverage_ENV_VAR} ${LCOV_EXEC_TESTS_CMD}

Then ${LCOV_EXEC_TESTS_CMD} does not get resolved to the full path.

bilke commented 3 years ago

See this StackOverflow answer.

ahtsan commented 3 years ago

Thanks for the pointer, but I still can't get it working.

I modified add_custom_target to something like

    add_custom_target(${Coverage_NAME}
        COMMAND ${LCOV_CLEAN_CMD}
        COMMAND ${LCOV_BASELINE_CMD} 
        COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=/some/path ${LCOV_EXEC_TESTS_CMD}
        COMMAND ${LCOV_CAPTURE_CMD}
        COMMAND ${LCOV_BASELINE_COUNT_CMD}
        COMMAND ${LCOV_FILTER_CMD} 
        COMMAND ${LCOV_GEN_HTML_CMD}

and then I run make code_coverage and got

Capturing coverage data from .
Found gcov version: 7.5.0
Scanning . for .gcno files ...
Found 1 graph files in .
Processing sim_runtime_gazebo9_plugin_UNIT_TEST.cc.gcno
Finished .info-file creation
/usr/bin/cmake -E env LD_LIBRARY_PATH=/some/path/ my_test
No such file or directory

where my_test is the executable from my tests

ahtsan commented 3 years ago

In case someone also has this issue, I came up with a workaround.

The issue above is that CMake couldn't find my executable. According to the add_custom_target documentation,

If neither of the above conditions are met, it is assumed that the command name is a program to be found on the PATH at build time.

so I add the path to the executable to the PATH environment variable as well, like

    add_custom_target(${Coverage_NAME}
        COMMAND ${LCOV_CLEAN_CMD}
        COMMAND ${LCOV_BASELINE_CMD} 
        COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=/some/path PATH=/path/to/the/executable ${LCOV_EXEC_TESTS_CMD}
        COMMAND ${LCOV_CAPTURE_CMD}
        COMMAND ${LCOV_BASELINE_COUNT_CMD}
        COMMAND ${LCOV_FILTER_CMD} 
        COMMAND ${LCOV_GEN_HTML_CMD}