From ee33b228f90a70cd751b9b31ad4d6ec5004bd13f Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Mon, 11 Apr 2022 10:22:36 -0500 Subject: [PATCH] Update Makefile and add wrapper script for managing header includes This adds a `bin/fix-header-includes` script that uses `iwyu.py` and `format-code.py` to manage header includes in Kaleidoscope source files. In addition to the `src` and `plugins` trees, it is now also capable of handling files in `testing` for the test simulator, which introduces some particular complications due to Arduino's ill-advised `min` and `max` preprocessor macros. The new `fix-header-includes` script can be run on a repository, and runs IWYU and clang-format on code that differs between the current worktree and a specified commit (default: `origin/master`), hopefully ensuring compliance with the code style guide. Also added: a new `check-all-includes` makefile target meant to be useful for running as a git workflows check. Signed-off-by: Michael Richters --- Makefile | 13 ++++++++++--- bin/fix-header-includes | 43 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 02062372..50bed699 100644 --- a/Makefile +++ b/Makefile @@ -113,21 +113,28 @@ check-code-style: bin/format-code.py \ --exclude-dir 'testing/googletest' \ --exclude-file 'generated-testcase.cpp' \ - --force \ --check \ --verbose \ src plugins examples testing .PHONY: check-includes check-includes: + bin/fix-header-includes + +.PHONY: check-all-includes +check-all-includes: bin/iwyu.py -v src plugins - bin/format-code.py -f -v --check src plugins + bin/iwyu.py -v \ + -I=$(KALEIDOSCOPE_DIR) \ + -I=$(KALEIDOSCOPE_DIR)/testing/googletest/googlemock/include \ + -I=$(KALEIDOSCOPE_DIR)/testing/googletest/googletest/include \ + testing + bin/format-code.py -f -v --check src plugins testing .PHONY: cpplint-noisy cpplint-noisy: -bin/cpplint.py --config=.cpplint-noisy --recursive src plugins examples - .PHONY: cpplint cpplint: bin/cpplint.py --config=.cpplint --quiet --recursive src plugins examples diff --git a/bin/fix-header-includes b/bin/fix-header-includes index 13c105cb..e44af80e 100755 --- a/bin/fix-header-includes +++ b/bin/fix-header-includes @@ -1,8 +1,43 @@ #!/usr/bin/env bash +# In case it hasn't been set, assume we're being run from the root of the local +# Kaleidoscope repository. : "${KALEIDOSCOPE_DIR:=$(pwd)}" -cd "${KALEIDOSCOPE_DIR}" || exit 1 -# Process sources first, then headers to minimize errors from removed includes -git ls-files -m | grep -E '\.(cpp|ino)$' | xargs "${KALEIDOSCOPE_DIR}"/bin/iwyu.py -git ls-files -m | grep -E '\.h$' | xargs "${KALEIDOSCOPE_DIR}"/bin/iwyu.py +# This variable is a git ref that should point to the current master branch of +# the primary Kaleidoscope repository. In most cases, this would be +# `origin/master`. +: "${KALEIDOSCOPE_MERGE_BASE:=origin/master}" + +# Don't do anything if the working tree has unstaged changes, to avoid +# unintentional combining of contentful changes with formatting changes. +if ! git diff -z --exit-code --quiet; then + echo "Working tree has unstaged changes; aborting." + exit 1 +fi + +# Run git-diff so we only run IWYU on files that differ from `master`. This +# isn't necessarily what we want, but if the current branch has been rebased, it +# shouldn't touch any extra files. +git diff -z --name-only "${KALEIDOSCOPE_MERGE_BASE}" -- src plugins \ + | "${KALEIDOSCOPE_DIR}/bin/iwyu.py" -z -v + +# After running it on Kaleidoscope source files, run it on the test simulator, +# which requires some additional include dirs. +git diff -z --name-only "${KALEIDOSCOPE_MERGE_BASE}" -- testing \ + | "${KALEIDOSCOPE_DIR}/bin/iwyu.py" \ + -z -v \ + -I="${KALEIDOSCOPE_DIR}" \ + -I="${KALEIDOSCOPE_DIR}/testing/googletest/googlemock/include" \ + -I="${KALEIDOSCOPE_DIR}/testing/googletest/googletest/include" + +# Always run clang-format after IWYU, because they have different indentation +# rules for comments added by IWYU. +git diff -z --name-only "${KALEIDOSCOPE_MERGE_BASE}" -- src plugins testing \ + | "${KALEIDOSCOPE_DIR}/bin/format-code.py" \ + -z -v \ + --exclude-dir='testing/googletest' \ + --exclude-file='generated-testcase.cpp' \ + --force \ + --check \ + --verbose