Skip to content
Draft
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ if(CLANG_TIDY)
)
message(STATUS "Using clang-tidy from: ${CLANG_TIDY_EXE}")
endif()
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/iwyu.cmake)

if(SAN OR TSAN)
find_program(LLVM_SYMBOLIZER NAMES "llvm-symbolizer")
Expand Down
50 changes: 50 additions & 0 deletions cmake/iwyu.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.

option(INCLUDE_WHAT_YOU_USE "Run include-what-you-use on the codebase" OFF)
set(
IWYU_MAPPING_DIR
""
CACHE PATH
"Directory containing include-what-you-use mapping files"
)

if(NOT INCLUDE_WHAT_YOU_USE)
return()
endif()

find_program(IWYU_EXE NAMES "include-what-you-use" "iwyu")
if(NOT IWYU_EXE)
message(FATAL_ERROR "include-what-you-use requested but not found")
endif()

# -w suppresses compiler warnings so CI logs focus on IWYU findings.
# --error=1 selects IWYU's standard non-zero failure code, so any suggestion
# fails the build and enforces direct includes.
set(IWYU_COMMAND "${IWYU_EXE}" "-w" "-Xiwyu" "--error=1")
get_filename_component(IWYU_BIN_DIR "${IWYU_EXE}" DIRECTORY)
set(
IWYU_MAPPING_DIRS
"${IWYU_MAPPING_DIR}"
"${IWYU_BIN_DIR}/../share/include-what-you-use"
"/usr/local/share/include-what-you-use"
"/usr/share/include-what-you-use"
)
set(IWYU_LIBCXX_MAPPING_FOUND OFF)
foreach(IWYU_MAPPING_CANDIDATE_DIR ${IWYU_MAPPING_DIRS})
if(EXISTS "${IWYU_MAPPING_CANDIDATE_DIR}/libcxx.imp")
list(
APPEND IWYU_COMMAND
"-Xiwyu"
"--mapping_file=${IWYU_MAPPING_CANDIDATE_DIR}/libcxx.imp"
)
set(IWYU_LIBCXX_MAPPING_FOUND ON)
break()
endif()
endforeach()
if(NOT IWYU_LIBCXX_MAPPING_FOUND)
message(WARNING "include-what-you-use libcxx.imp mapping file not found")
endif()

set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${IWYU_COMMAND})
message(STATUS "Using include-what-you-use from: ${IWYU_EXE}")
1 change: 1 addition & 0 deletions scripts/ci-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ CHECKS=(
"Shell scripts:shellcheck-checks.sh"
"TODOs:todo-checks.sh"
"Includes:includes-checks.sh"
"Include what you use:iwyu-checks.sh"
"Release notes:release-notes-checks.sh"
"Non-ASCII characters:ascii-checks.sh"
"C/C++ format:cpp-format-checks.sh"
Expand Down
44 changes: 44 additions & 0 deletions scripts/iwyu-checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.

# Checks C/C++ include hygiene with include-what-you-use.
# Pass -f for interface consistency, but no auto-fix is available.

set -e
set -u
set -o pipefail

FIX=0
if [ "${1:-}" = "-f" ]; then
# ci-checks.sh passes -f to every check; IWYU has no safe auto-fix mode, so
# this script treats it as a request to run the check normally.
FIX=1
shift
fi

if [ "$#" -ne 0 ]; then
echo "Usage: $0 [-f]"
exit 1
fi

if [ "$FIX" -ne 0 ]; then
echo "include-what-you-use checks do not support auto-fix; running checks only"
fi

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ROOT_DIR=$( dirname "$SCRIPT_DIR" )
BUILD_DIR=${CCF_IWYU_BUILD_DIR:-"$ROOT_DIR/build-iwyu"}
NPROC=$(nproc 2>/dev/null || echo 4)

cmake \
-S "$ROOT_DIR" \
-B "$BUILD_DIR" \
-GNinja \
-DCMAKE_BUILD_TYPE=Debug \
-DBUILD_END_TO_END_TESTS=OFF \
-DCOMPILE_TARGET=virtual \
-DINCLUDE_WHAT_YOU_USE=ON

cmake --build "$BUILD_DIR" --parallel "$NPROC"
echo "include-what-you-use checks passed"
1 change: 1 addition & 0 deletions scripts/setup-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ install_build_dependencies() {
tdnf --snapshottime=$SOURCE_DATE_EPOCH -y install \
build-essential \
clang \
include-what-you-use \
cmake \
ninja-build \
which \
Expand Down
1 change: 1 addition & 0 deletions scripts/setup-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ retry() {
install_dev_dependencies() {
tdnf -y install \
clang-tools-extra \
include-what-you-use \
python-pip \
jq \
tar \
Expand Down
Loading