Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 22 additions & 0 deletions cmake/iwyu-wrapper.sh
Original file line number Diff line number Diff line change
@@ -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"
Loading