4

My project links to a third-party library that comes with a valgrind suppression file, as well as a CMake script. The script stores the location of the suppression file in a CMake cache variable.

I have written a CTest script that runs continuous tests on my project and submit to a dashboard. I would like to use the suppression file during the memory-checking stage. Unfortunately, the CTest script does not seem to know anything about the CMake cache. How can I access the CMake cache variable from my CTest script?

Arek' Fu
  • 826
  • 8
  • 24

2 Answers2

3

You can't directly access CMake cache variables from the ctest -S script.

However, you could possibly:

  1. include the third party CMake script in the ctest -S script with "include" (after the update step, so the source tree is up to date)
  2. read the CMakeCache.txt file after the configure step to pull out the cache variable of interest
  3. add code to your CMakeLists.txt file to write out a mini-script that contains just the information you're looking for

For (1), the code would be something like:

include(${CTEST_SOURCE_DIRECTORY}/path/to/3rdParty/script.cmake)

This would only be realistically possible if the script does only simple things like set variable values that you can then reference. If it does any CMake-configure-time things like find_library or add_executable, then you shouldn't do this.

For (2):

file(STRINGS ${CTEST_BINARY_DIRECTORY}/CMakeCache.txt result
  REGEX "^CURSES_LIBRARY:FILEPATH=(.*)$")
message("result='${result}'")
string(REGEX REPLACE "^CURSES_LIBRARY:FILEPATH=(.*)$" "\\1"
  filename "${result}")
message("filename='${filename}'")

For (3):

In the CMakeLists.txt file:

file(WRITE "${CMAKE_BINARY_DIR}/mini-script.cmake" "
  set(supp_file \"${supp_file_location}\")
")

In your ctest -S script, after the ctest_configure call:

include("${CTEST_BINARY_DIRECTORY}/mini-script.cmake")
message("supp_file='${supp_file}'")
# use supp_file as needed in the rest of your script
DLRdave
  • 13,876
  • 4
  • 53
  • 70
  • I had thought about solution (1) but it does not look very robust. You have to know what `script.cmake` does, and you have to assume that it will not change in future versions. On the other hand, solution (2) relies on the format of the `CMakeCache.txt` file, which CMake is probably free to change in future releases. I think I will go with the brute-force regex solution, although neither of them looks really satisfactory. Thanks anyway for your answer. – Arek' Fu Aug 24 '12 at 14:46
  • I like solution (3), it looks more robust. Thanks again. – Arek' Fu Aug 24 '12 at 14:57
  • In (2) CTEST_BINARY_DIRECTORY isn't defined, right? The variable I am actually trying to get out of the cache is just CMAKE_BINARY_DIR exactly so I can set CTEST_BINARY_DIRECTORY to it. – David Doria Feb 02 '16 at 01:19
  • In (1), (2), **and** (3), the context is within a ctest -S script. In which context, CTEST_BINARY_DIRECTORY should be set, either directly in the script, or by the caller via -D on the ctest command line. The location of the CMakeCache.txt file you are reading **is** the CTEST_BINARY_DIRECTORY. – DLRdave Feb 02 '16 at 15:19
0

The tests should be configured in your CMakeLists.txt file via the command ADD_TEST(). The CTest script then simply calls ctest_test() to run all the configured tests. By doing it this way, you get access to the cache variables, plus you can just run make test at any point to run the tests. Save the CTest script for the nightly testing and/or Dashboard submissions.

Peter
  • 14,559
  • 35
  • 55
  • Well, that's why I use a script -- I do continuous testing and dashboard submission. I have edited the question to clarify this. – Arek' Fu Aug 24 '12 at 14:13