| 1 |
#!/bin/sh |
| 2 |
# Copyright 2017 gRPC authors. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
set -x |
| 17 |
|
| 18 |
cd ${IWYU_ROOT} |
| 19 |
|
| 20 |
export PATH=${PATH}:${IWYU_ROOT}/iwyu_build/bin |
| 21 |
|
| 22 |
# number of CPUs available |
| 23 |
CPU_COUNT=`nproc` |
| 24 |
|
| 25 |
rm -rf iwyu || true |
| 26 |
git clone https://github.com/include-what-you-use/include-what-you-use.git iwyu |
| 27 |
# latest commit on the clang 15 branch |
| 28 |
cd ${IWYU_ROOT}/iwyu |
| 29 |
git checkout 7f0b6c304acf69c42bb7f6e03c63f836924cb7e0 |
| 30 |
mkdir -p ${IWYU_ROOT}/iwyu_build |
| 31 |
cd ${IWYU_ROOT}/iwyu_build |
| 32 |
cmake -G "Unix Makefiles" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLLVM_ROOT_DIR=/usr/lib/llvm-15 ${IWYU_ROOT}/iwyu |
| 33 |
make -j $CPU_COUNT |
| 34 |
cd ${IWYU_ROOT} |
| 35 |
|
| 36 |
# patch python shebang for our environment (we need python3, not python) |
| 37 |
sed -i 's,^#!/usr/bin/env python,#!/usr/bin/env python3,g' ${IWYU_ROOT}/iwyu/iwyu_tool.py |
| 38 |
sed -i 's,^#!/usr/bin/env python,#!/usr/bin/env python3,g' ${IWYU_ROOT}/iwyu/fix_includes.py |
| 39 |
|
| 40 |
cat compile_commands.json \ |
| 41 |
| sed "s/ -DNDEBUG//g" \ |
| 42 |
| sed "s,\"file\": \",\"file\": \"${IWYU_ROOT}/,g" \ |
| 43 |
> compile_commands_for_iwyu.json |
| 44 |
|
| 45 |
export ENABLED_MODULES=' |
| 46 |
src/core/ext |
| 47 |
src/core/lib |
| 48 |
src/cpp |
| 49 |
test/core |
| 50 |
' |
| 51 |
|
| 52 |
export DISABLED_MODULES=' |
| 53 |
src/core/lib/gpr |
| 54 |
src/core/lib/iomgr |
| 55 |
src/core/ext/transport/binder |
| 56 |
test/core/alts |
| 57 |
test/core/iomgr |
| 58 |
test/core/security |
| 59 |
test/core/tsi |
| 60 |
test/core/transport/binder |
| 61 |
' |
| 62 |
|
| 63 |
export INCLUSION_REGEX=`echo $ENABLED_MODULES | sed 's/ /|/g' | sed 's,\\(.*\\),^(\\1)/,g'` |
| 64 |
export EXCLUSION_REGEX=`echo $DISABLED_MODULES | sed 's/ /|/g' | sed 's,\\(.*\\),^(\\1)/,g'` |
| 65 |
|
| 66 |
# figure out which files to include |
| 67 |
cat compile_commands.json | jq -r '.[].file' \ |
| 68 |
| grep -E $INCLUSION_REGEX \ |
| 69 |
| grep -v -E "/upb-generated/|/upbdefs-generated/" \ |
| 70 |
| grep -v -E $EXCLUSION_REGEX \ |
| 71 |
| grep -v src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h \ |
| 72 |
| grep -v test/core/end2end/end2end_tests.cc \ |
| 73 |
| grep -v test/core/surface/public_headers_must_be_c89.c \ |
| 74 |
| sort \ |
| 75 |
> iwyu_files0.txt |
| 76 |
|
| 77 |
cat iwyu_files0.txt \ |
| 78 |
| xargs -d '\n' ls -1df 2> /dev/null \ |
| 79 |
> iwyu_files.txt \ |
| 80 |
|| true |
| 81 |
|
| 82 |
echo '#!/bin/sh |
| 83 |
${IWYU_ROOT}/iwyu/iwyu_tool.py -p compile_commands_for_iwyu.json $1 \ |
| 84 |
-- -Xiwyu --no_fwd_decls \ |
| 85 |
-Xiwyu --update_comments \ |
| 86 |
-Xiwyu --mapping_file=${IWYU_ROOT}/tools/distrib/iwyu_mappings.imp \ |
| 87 |
| grep -v -E "port_platform.h" \ |
| 88 |
| grep -v -E "repeated_ptr_field.h" \ |
| 89 |
| grep -v -E "repeated_field.h" \ |
| 90 |
| grep -v -E "^(- )?namespace " \ |
| 91 |
> iwyu/iwyu.`echo $1 | sha1sum`.out |
| 92 |
' > iwyu/run_iwyu_on.sh |
| 93 |
chmod +x iwyu/run_iwyu_on.sh |
| 94 |
|
| 95 |
# run iwyu, filtering out changes to port_platform.h |
| 96 |
xargs -n 1 -P $CPU_COUNT -a iwyu_files.txt ${IWYU_ROOT}/iwyu/run_iwyu_on.sh |
| 97 |
|
| 98 |
cat iwyu/iwyu.*.out > iwyu.out |
| 99 |
|
| 100 |
# apply the suggested changes |
| 101 |
${IWYU_ROOT}/iwyu/fix_includes.py \ |
| 102 |
--nocomments \ |
| 103 |
--nosafe_headers \ |
| 104 |
--ignore_re='^(include/.*|src/core/lib/security/credentials/tls/grpc_tls_credentials_options\.h)' \ |
| 105 |
< iwyu.out |
| 106 |
|
| 107 |
if [ $? -ne 0 ] |
| 108 |
then |
| 109 |
echo "Iwyu edited some files. Here is the diff of files edited by iwyu:" |
| 110 |
git --no-pager diff |
| 111 |
# Exit with a non zero error code to ensure sanity checks fail accordingly. |
| 112 |
exit 1 |
| 113 |
fi |