cburstedde/libsc

CMake for valgrind with tests

cburstedde opened this issue · 3 comments

Description

With autoconf, we provide the --evable-valgrind option to do two things:

  1. Use valgrind on make check if given and valgrind is found.
  2. #define SC_ENABLE_VALGRIND if given and valgrind is found.

Furthermore, p4est will #define P4EST_ENABLE_VALGRIND under the same conditions.
This functionality is, so far, not in the CMake build. How hard would it be to establish it?

This is not hard to do,

# cmake/options.cmake
option(ENABLE_VALGRIND "Use valgrind with ctest to run tests" OFF)

# 
if(BUILD_TESTING AND ENABLE_VALGRIND)
    find_program(VALGRIND "valgrind")
    if(VALGRIND)
        set(VALGRIND_FOUND TRUE)
        set(VALGRIND_COMMAND ${VALGRIND})
        set(VALGRIND_COMMAND_OPTIONS --error-exitcode=1)
        add_compile_definitions(SC_ENABLE_VALGRIND=1)
    else()
        message(FATAL_ERROR "ENABLE_VALGRIND was set, but valgrind was not found")
    endif()
endif()

This would then be enabled via cmake -DENABLE_VALGRIND=ON ...

Then in the test cases we can mirror what the autoconf does by using something like

# test/CMakeLists.txt
set(VALGRIND_WRAPPER "")
if(VALGRIND_FOUND AND ENABLE_VALGRIND)
    set(VALGRIND_WRAPPER ${VALGRIND_COMMAND} ${VALGRIND_COMMAND_OPTIONS})
endif()

set(MPI_WRAPPER "")
if(MPIEXEC_EXACUTABLE)
    set(MPIEXEC_MAX_NUMPROCS 2)
    set(MPI_WRAPPER ${MPIEXEC_EXACUTABLE} ${MPIEXEC_NUMPROC_FLAG} ${MPIEXEC_MAX_NUMPROCS})
endif()

foreach(t IN LISTS sc_tests)

  add_executable(sc_test_${t} test_${t}.c)
  target_link_libraries(sc_test_${t} PRIVATE SC::SC)

  add_test(
      NAME ${t}
      COMMAND ${MPI_WRAPPER} ${VALGRIND_WRAPPER} $<TARGET_FILE:sc_test_${t}>
  )

endforeach()

Cool, if you'd like to create a PR to mimic the autoconf behaviour as closely as possible?

This issue was fixed by #139