blob: 2ba11b7c75d8c4ebd2e1f91691641ad45017d2a1 [file] [log] [blame]
#!/usr/bin/env bash
#
# Run the include-cleaner tool (iwyu replacement) on a file in the webrtc source
# directory.
#
#
# In order to handle include paths correctly, you need to provide
# a compile DB (aka compile_commands.json).
# You can create it in one of the following ways:
# "gn gen --export-compile-commands path/to/out"
# "tools/clang/scripts/generate_compdb.py -p path/to/out > compile_commands.json"
# If "out/Default" exists, the script will attempt to generate it for you.
#
# To get include-cleaner:
# - in google3: define an alias and/or point to it below
# - in debian distros: sudo apt install clang-tools-17 (or later, and fix below)
# Ignore errors of the type "this argument doesn't exist".
CLEANER=/usr/bin/clang-include-cleaner-17
# Debug level, also controlled by the "-d" argument.
# Set this to 1 to get more debug information.
# Set this to 2 to also get a dump of the iwyu tool output.
DEBUG=0
set -e
if [ $DEBUG -gt 0 ]; then
set -x
fi
error() {
echo "$*" >&2
exit 1
}
WORKDIR=out/Default
usage() {
echo "Usage: $0 [ -c compile-commands-file.json ] [-r] file.cc"
echo "Runs the include-cleaner tool on a file"
echo "Arguments:"
echo " -n : just print changes, don't do them"
echo " -r : Remove non-required includes from .h file"
echo " -d <n> : Set debug level to <n>"
echo " -h : Print this help message"
}
COMMAND=" --edit"
INCLUDE_ARGS=""
while getopts 'd:rnh' opts; do
case "${opts}" in
n) COMMAND=" --print=changes" ;;
r) INCLUDE_ARGS=" --remove" ;;
d) DEBUG=${OPTARG};if [ $DEBUG -gt 0 ]; then set -x; fi ;;
h) usage; exit 1 ;;
*) error "Unexpected option ${opts}" ;;
esac
done
shift $(expr $OPTIND - 1 )
if [[ -z "$COMPILE_COMMANDS" ]]; then
if [ -d "$WORKDIR" ]; then
if [ ! -f "$WORKDIR/compile_commands.json" ]; then
echo "Generating compile commands file"
gn gen --export-compile-commands $WORKDIR
fi
COMPILE_COMMANDS="$WORKDIR/compile_commands.json"
else
error "Could not generate $WORKDIR/compile_commands.json."
fi
fi
FILE="$1"
if [ ! -f $FILE ]; then
error "File $FILE is not found"
fi
cd $WORKDIR
$CLEANER $INCLUDE_ARGS $COMMAND ../../$FILE
echo "Finished. Check diff, compile, gn gen --check and git cl format"
echo "before uploading."