@@ -34,41 +34,22 @@
set(CMAKE_C_FLAGS "-fprofile-arcs -ftest-coverage -ggdb -O0")
endif()
# Find the correct libgcov library path matching the gcc compiler version
# This ensures the test executable links against the same libgcov version used during compilation
# Link the libgcov that belongs to the selected compiler. Asking GCC directly
# avoids selecting another installed GCC version through platform-specific
# library directory names (for example, the GCC required by CUDA on Arch Linux).
if(UNIX AND NOT APPLE AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
if(UNIX AND NOT APPLE)
# Get the gcc compiler version
execute_process(
COMMAND ${CMAKE_C_COMPILER} -print-file-name=libgcov.a
COMMAND ${CMAKE_C_COMPILER} -dumpversion
OUTPUT_VARIABLE GCC_VERSION
OUTPUT_VARIABLE LIBGCOV_LIBRARY
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE LIBGCOV_RESULT
)
# Extract major version
if(LIBGCOV_RESULT EQUAL 0 AND EXISTS "${LIBGCOV_LIBRARY}")
message(STATUS "Found libgcov: ${LIBGCOV_LIBRARY}")
set(GCOV_LINK_LIBRARY "${LIBGCOV_LIBRARY}")
string(REGEX MATCH "^[0-9]+" GCC_MAJOR_VERSION "${GCC_VERSION}")
# Search for the gcc library directory matching this version
file(GLOB GCC_LIB_DIRS "/usr/lib/gcc/*/*${GCC_MAJOR_VERSION}.*")
if(GCC_LIB_DIRS)
list(GET GCC_LIB_DIRS 0 GCC_LIB_DIR)
message(STATUS "Found GCC library directory: ${GCC_LIB_DIR}")
# Look for libgcov.a in the gcc library directory
find_library(LIBGCOV_LIBRARY
NAMES gcov
PATHS ${GCC_LIB_DIR}
NO_DEFAULT_PATH
)
if(LIBGCOV_LIBRARY)
message(STATUS "Found libgcov: ${LIBGCOV_LIBRARY}")
# Store this to link against later
set(GCOV_LINK_LIBRARY ${LIBGCOV_LIBRARY})
else()
message(WARNING "Could not find libgcov in ${GCC_LIB_DIR}")
endif()
else()
message(WARNING "Could not find libgcov for ${CMAKE_C_COMPILER}")
message(WARNING "Could not find GCC library directory for version ${GCC_VERSION}")
unset(GCOV_LINK_LIBRARY)
endif()
endif()