move sanitizer and iwyu configuration to Testing module and update iwyu

this changes the iwyu configuration so that it will check for using
GNU or Clang only as supported compilers, enforces the necessary recording
of compilation commands in a json file and tweaks the "iwyu" target
to work around an issue with the current iwyu implementation by placing
the "native" runtime of the chosen compiler first
This commit is contained in:
Axel Kohlmeyer 2021-04-24 11:09:09 -04:00
parent 93691ca939
commit e4c7c23843
No known key found for this signature in database
GPG Key ID: D9B44E93BF0C375A
2 changed files with 73 additions and 63 deletions

View File

@ -245,69 +245,6 @@ if(BUILD_OMP)
target_link_libraries(lammps PRIVATE OpenMP::OpenMP_CXX)
endif()
# Compiler specific features for testing
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
option(ENABLE_COVERAGE "Enable collecting code coverage data" OFF)
mark_as_advanced(ENABLE_COVERAGE)
if(ENABLE_COVERAGE)
if(CMAKE_VERSION VERSION_LESS 3.13)
if(CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_${CMAKE_BUILD_TYPE}_FLAGS} --coverage")
endif()
else()
target_compile_options(lammps PUBLIC --coverage)
target_link_options(lammps PUBLIC --coverage)
endif()
endif()
endif()
#######################################
# add custom target for IWYU analysis
#######################################
set(ENABLE_IWYU OFF CACHE BOOL "Add 'iwyu' build target to call the include-what-you-use tool")
mark_as_advanced(ENABLE_IWYU)
if(ENABLE_IWYU)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_program(IWYU_EXE NAMES include-what-you-use iwyu)
find_program(IWYU_TOOL NAMES iwyu_tool iwyu-tool iwyu_tool.py)
if (IWYU_EXE AND IWYU_TOOL)
add_custom_target(
iwyu
${IWYU_TOOL} -o clang -p ${CMAKE_CURRENT_BINARY_DIR} -- -Xiwyu --mapping_file=${CMAKE_CURRENT_SOURCE_DIR}/iwyu/iwyu-extra-map.imp
COMMENT "Running IWYU")
add_dependencies(iwyu lammps)
else()
message(FATAL_ERROR "To use IWYU you need the include-what-you-use/iwyu executable"
"and the iwyu-tool/iwyu_tool script installed in your PATH")
endif()
endif()
set(ENABLE_SANITIZER "none" CACHE STRING "Select a code sanitizer option (none (default), address, leak, thread, undefined)")
mark_as_advanced(ENABLE_SANITIZER)
set(ENABLE_SANITIZER_VALUES none address leak thread undefined)
set_property(CACHE ENABLE_SANITIZER PROPERTY STRINGS ${ENABLE_SANITIZER_VALUES})
validate_option(ENABLE_SANITIZER ENABLE_SANITIZER_VALUES)
string(TOLOWER ${ENABLE_SANITIZER} ENABLE_SANITIZER)
if(NOT ENABLE_SANITIZER STREQUAL "none")
if((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") OR (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"))
if(CMAKE_VERSION VERSION_LESS 3.13)
if(CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${ENABLE_SANITIZER}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_${CMAKE_BUILD_TYPE}_FLAGS} -fsanitize=${ENABLE_SANITIZER}")
endif()
else()
target_compile_options(lammps PUBLIC -fsanitize=${ENABLE_SANITIZER})
target_link_options(lammps PUBLIC -fsanitize=${ENABLE_SANITIZER})
endif()
else()
message(WARNING "ENABLE_SANITIZER option not supported by ${CMAKE_CXX_COMPILER_ID} compilers. Ignoring.")
set(ENABLE_SANITIZER "none")
endif()
endif()
if(PKG_MSCG OR PKG_USER-ATC OR PKG_USER-AWPMD OR PKG_USER-QUIP OR PKG_LATTE)
enable_language(C)
find_package(LAPACK)

View File

@ -56,3 +56,76 @@ if(ENABLE_TESTING)
get_filename_component(LAMMPS_UNITTEST_BIN ${CMAKE_BINARY_DIR}/unittest ABSOLUTE)
add_subdirectory(${LAMMPS_UNITTEST_DIR} ${LAMMPS_UNITTEST_BIN})
endif()
# Compiler specific features for testing
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
option(ENABLE_COVERAGE "Enable collecting code coverage data" OFF)
mark_as_advanced(ENABLE_COVERAGE)
if(ENABLE_COVERAGE)
if(CMAKE_VERSION VERSION_LESS 3.13)
if(CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_${CMAKE_BUILD_TYPE}_FLAGS} --coverage")
endif()
else()
target_compile_options(lammps PUBLIC --coverage)
target_link_options(lammps PUBLIC --coverage)
endif()
endif()
endif()
#######################################
# add custom target for IWYU analysis
#######################################
set(ENABLE_IWYU OFF CACHE BOOL "Add 'iwyu' build target to call the include-what-you-use tool")
mark_as_advanced(ENABLE_IWYU)
if(ENABLE_IWYU)
# enforce these settings
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Enable reporting compilation commands to compile_commands.json" FORCE)
if (NOT ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")))
message(FATAL_ERROR "IWYU is only supported with Clang or GNU compilers")
endif()
# detect the "native" header folder so we can include them first
execute_process(COMMAND ${CMAKE_CXX_COMPILER} --print-search-dirs OUTPUT_VARIABLE IWYU_SEARCH_PATHS)
string(REGEX REPLACE ".*libraries: *=([^:]+):.*" "\\1/include" IWYU_EXTRA_INCLUDE_DIR ${IWYU_SEARCH_PATHS})
find_program(IWYU_EXE NAMES include-what-you-use iwyu)
find_program(IWYU_TOOL NAMES iwyu_tool iwyu-tool iwyu_tool.py)
if (IWYU_EXE AND IWYU_TOOL)
add_custom_target(
iwyu
${IWYU_TOOL} -o clang -p ${CMAKE_CURRENT_BINARY_DIR} -- -I${IWYU_EXTRA_INCLUDE_DIR} -Xiwyu --mapping_file=${CMAKE_CURRENT_SOURCE_DIR}/iwyu/iwyu-extra-map.imp
COMMENT "Running IWYU")
add_dependencies(iwyu lammps)
else()
message(FATAL_ERROR "To use IWYU you need the include-what-you-use/iwyu executable"
"and the iwyu-tool/iwyu_tool script installed in your PATH")
endif()
endif()
#######################################
# select code sanitizer options
#######################################
set(ENABLE_SANITIZER "none" CACHE STRING "Select a code sanitizer option (none (default), address, leak, thread, undefined)")
mark_as_advanced(ENABLE_SANITIZER)
set(ENABLE_SANITIZER_VALUES none address leak thread undefined)
set_property(CACHE ENABLE_SANITIZER PROPERTY STRINGS ${ENABLE_SANITIZER_VALUES})
validate_option(ENABLE_SANITIZER ENABLE_SANITIZER_VALUES)
string(TOLOWER ${ENABLE_SANITIZER} ENABLE_SANITIZER)
if(NOT ENABLE_SANITIZER STREQUAL "none")
if((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") OR (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"))
if(CMAKE_VERSION VERSION_LESS 3.13)
if(CMAKE_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${ENABLE_SANITIZER}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_${CMAKE_BUILD_TYPE}_FLAGS} -fsanitize=${ENABLE_SANITIZER}")
endif()
else()
target_compile_options(lammps PUBLIC -fsanitize=${ENABLE_SANITIZER})
target_link_options(lammps PUBLIC -fsanitize=${ENABLE_SANITIZER})
endif()
else()
message(WARNING "ENABLE_SANITIZER option not supported by ${CMAKE_CXX_COMPILER_ID} compilers. Ignoring.")
set(ENABLE_SANITIZER "none")
endif()
endif()