Skip to content

Commit 2e55f9c

Browse files
authored
kokoro: Streamline check-format (KhronosGroup#5973)
Use a special Docker image that has only the necessary tools. Avoid downloading clang. Also, utils/check_code_format.sh can find clang-format and clang-format-diff.py from CLANG_FORMAT and CLANG_FORMAT_DIFF environment variables. The Docker image sets those. BUG=392135092, 397439901
1 parent 9fa14b9 commit 2e55f9c

File tree

3 files changed

+59
-35
lines changed

3 files changed

+59
-35
lines changed

kokoro/check-format/build-docker.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
# Copyright (c) 2025 Google LLC
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+
# Fail on any error.
17+
set -e
18+
19+
# This is required to run any git command in the docker since owner will
20+
# have changed between the clone environment, and the docker container.
21+
# Marking the root of the repo as safe for ownership changes.
22+
git config --global --add safe.directory "$PWD"
23+
24+
echo $(date): Check formatting...
25+
./utils/check_code_format.sh ${1:-main}
26+
echo $(date): check completed.

kokoro/check-format/build.sh

100644100755
Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Copyright (c) 2018 Google LLC.
2+
# Copyright (c) 2025 Google LLC
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -12,37 +12,16 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
#
16-
# Android Build Script.
1715

1816
# Fail on any error.
1917
set -e
20-
# Display commands being run.
21-
set -x
22-
23-
BUILD_ROOT=$PWD
24-
SRC=$PWD/github/SPIRV-Tools
25-
26-
# This is required to run any git command in the docker since owner will
27-
# have changed between the clone environment, and the docker container.
28-
# Marking the root of the repo as safe for ownership changes.
29-
git config --global --add safe.directory $SRC
30-
31-
# Get clang-format-5.0.0.
32-
# Once kokoro upgrades the Ubuntu VMs, we can use 'apt-get install clang-format'
33-
curl -L http://releases.llvm.org/5.0.0/clang+llvm-5.0.0-linux-x86_64-ubuntu14.04.tar.xz -o clang-llvm.tar.xz
34-
tar xf clang-llvm.tar.xz
35-
export PATH=$PWD/clang+llvm-5.0.0-linux-x86_64-ubuntu14.04/bin:$PATH
3618

37-
cd $SRC
38-
git clone --depth=1 https://github.com/KhronosGroup/SPIRV-Headers external/spirv-headers
39-
git clone https://github.com/google/googletest external/googletest
40-
cd external && cd googletest && git reset --hard 1fb1bb23bb8418dc73a5a9a82bbed31dc610fec7 && cd .. && cd ..
41-
git clone --depth=1 https://github.com/google/effcee external/effcee
42-
git clone --depth=1 https://github.com/google/re2 external/re2
43-
# The --fail flag causes the command to fail on HTTP error response codes, like 404.
44-
curl -L --fail https://raw.githubusercontent.com/llvm/llvm-project/main/clang/tools/clang-format/clang-format-diff.py -o utils/clang-format-diff.py
19+
SCRIPT_DIR="$( cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
20+
SRC_ROOT="$( cd "${SCRIPT_DIR}/../.." >/dev/null 2>&1 && pwd )"
21+
TARGET_BRANCH="${KOKORO_GITHUB_PULL_REQUEST_TARGET_BRANCH-main}"
4522

46-
echo $(date): Check formatting...
47-
./utils/check_code_format.sh;
48-
echo $(date): check completed.
23+
docker run --rm -i \
24+
--volume "${SRC_ROOT}:${SRC_ROOT}" \
25+
--workdir "${SRC_ROOT}" \
26+
"us-east4-docker.pkg.dev/shaderc-build/radial-docker/ubuntu-24.04-amd64/formatter" \
27+
"${SCRIPT_DIR}/build-docker.sh" "${TARGET_BRANCH}"

utils/check_code_format.sh

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,41 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
#
16-
# Script to determine if source code in Pull Request is properly formatted.
15+
16+
# This script determines if the source code in a Pull Request is properly formatted.
1717
# Exits with non 0 exit code if formatting is needed.
18-
#
19-
# This script assumes to be invoked at the project root directory.
18+
# Assumptions:
19+
# - git and python3 are on the path
20+
# - Runs from the project root diretory.
21+
# - 'clang-format' is on the path, or env var CLANG_FORMAT points to it.
22+
# - 'clang-format-diff.py' is in the utils directory, or env var
23+
# points to it.CLANG_FORMAT_DIFF
2024

2125
BASE_BRANCH=${1:-main}
2226

27+
CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
28+
if [ ! -f "$CLANG_FORMAT" ]; then
29+
echo missing clang-format: set CLANG_FORMAT or put clang-format in the PATH
30+
exit 1
31+
fi
32+
33+
# Find clang-format-diff.py from an environment variable, or use a default
34+
CLANG_FORMAT_DIFF=${CLANG_FORMAT_DIFF:-./utils/clang-format-diff.py}
35+
if [ ! -f "$CLANG_FORMAT_DIFF" ]; then
36+
echo missing clang-format-diffy.py: set CLANG_FORMAT_DIFF or put it in ./utils/clang-format-diff.py
37+
exit 1
38+
fi
39+
40+
echo "Comparing "$(git rev-parse HEAD)" against $BASE_BRANCH"
41+
2342
FILES_TO_CHECK=$(git diff --name-only ${BASE_BRANCH} | grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp)$")
2443

2544
if [ -z "${FILES_TO_CHECK}" ]; then
2645
echo "No source code to check for formatting."
2746
exit 0
2847
fi
2948

30-
FORMAT_DIFF=$(git diff -U0 ${BASE_BRANCH} -- ${FILES_TO_CHECK} | python ./utils/clang-format-diff.py -p1 -style=file)
49+
FORMAT_DIFF=$(git diff -U0 ${BASE_BRANCH} -- ${FILES_TO_CHECK} | python3 "${CLANG_FORMAT_DIFF}" -p1 -style=file -binary "$CLANG_FORMAT")
3150

3251
if [ -z "${FORMAT_DIFF}" ]; then
3352
echo "All source code in PR properly formatted."

0 commit comments

Comments
 (0)