diff --git a/CMakeLists.txt b/CMakeLists.txt index 4eb0e694..3462f3e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,7 +96,12 @@ if(MP_ENABLE_IWYU) if(NOT IWYU_EXECUTABLE) message(FATAL_ERROR "MP_ENABLE_IWYU is ON but include-what-you-use was not found.") endif() - set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${IWYU_EXECUTABLE};-Xiwyu;--error") + # Pass -w so CXXFLAGS meant for the compiler (-Werror -Wpedantic, GCC-only -W + # options) do not turn clang warnings into errors in the IWYU pass. Clang + # stops after 19 errors, and IWYU then exits nonzero without checking includes. + # Run IWYU through a wrapper so its diagnostics are shown when it fails + # without suggesting includes (see cmake/iwyu-wrapper.sh). + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/iwyu-wrapper.sh;${IWYU_EXECUTABLE};-Xiwyu;--error;-w") if(DEFINED ENV{IWYU_MAPPING_FILE}) list(APPEND CMAKE_CXX_INCLUDE_WHAT_YOU_USE "-Xiwyu" "--mapping_file=$ENV{IWYU_MAPPING_FILE}") endif() diff --git a/cmake/iwyu-wrapper.sh b/cmake/iwyu-wrapper.sh new file mode 100755 index 00000000..fa2fd1a0 --- /dev/null +++ b/cmake/iwyu-wrapper.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Run include-what-you-use for CMAKE_CXX_INCLUDE_WHAT_YOU_USE and make sure its +# output is shown when it fails. Usage: iwyu-wrapper.sh IWYU [ARGS...] +# +# "cmake -E __run_co_compile" only prints IWYU output that contains "should add +# these lines:" or "should remove these lines:" (HandleIWYU in CMake's +# Source/cmcmd.cxx). If IWYU fails for another reason, such as a clang error, +# the build stops with a bare "Error 1". In that case this script appends a line +# containing the marker text, so CMake prints the clang diagnostics too. +# +# This works around https://gitlab.kitware.com/cmake/cmake/-/issues/28020 and +# can be removed once CMake prints IWYU output whenever IWYU fails. +set -o nounset +iwyu=$1 +shift +out=$("$iwyu" "$@" 2>&1) +ret=$? +printf '%s\n' "$out" >&2 +if [ "$ret" -ne 0 ] && ! grep -q -e 'should add these lines:' -e 'should remove these lines:' <<<"$out"; then + printf 'include-what-you-use failed with exit status %s before suggesting includes (marker for CMake: "should add these lines:"); see the diagnostics above.\n' "$ret" >&2 +fi +exit "$ret"