|
28 | 28 | contents: read |
29 | 29 | pull-requests: read |
30 | 30 | runs-on: ${{ (inputs && inputs.runner && fromJson(inputs.runner)) || 'ubuntu-latest' }} |
31 | | - env: |
32 | | - CLANG_FORMAT_VERSION: 20 |
33 | 31 | steps: |
34 | 32 | - name: Checkout |
35 | 33 | uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
@@ -80,12 +78,62 @@ jobs: |
80 | 78 | with: |
81 | 79 | python-version: '3.14' |
82 | 80 |
|
| 81 | + - name: Resolve clang-format requirement |
| 82 | + shell: bash |
| 83 | + run: | |
| 84 | + requirement="clang-format" |
| 85 | + submodule="third-party/lizardbyte-common" |
| 86 | +
|
| 87 | + if git config -f .gitmodules --get-regexp path 2>/dev/null | grep -Fq " ${submodule}"; then |
| 88 | + if git submodule update --init --depth 1 -- "${submodule}"; then |
| 89 | + pinned_requirement=$(SUBMODULE="${submodule}" python - <<'PY' |
| 90 | + import os |
| 91 | + import re |
| 92 | + import tomllib |
| 93 | + from pathlib import Path |
| 94 | +
|
| 95 | + pyproject = Path(os.environ["SUBMODULE"]) / "pyproject.toml" |
| 96 | + try: |
| 97 | + config = tomllib.loads(pyproject.read_text(encoding="utf-8")) |
| 98 | + except (OSError, tomllib.TOMLDecodeError): |
| 99 | + config = {} |
| 100 | +
|
| 101 | + dependencies = config.get("dependency-groups", {}).get("lint-c", []) |
| 102 | + if not dependencies: |
| 103 | + dependencies = config.get("project", {}).get("optional-dependencies", {}).get("lint-c", []) |
| 104 | + for dependency in dependencies: |
| 105 | + if not isinstance(dependency, str): |
| 106 | + continue |
| 107 | + if re.fullmatch(r"clang-format==[0-9]+(?:\.[0-9]+)*(?:\.\*)?", dependency): |
| 108 | + print(dependency) |
| 109 | + break |
| 110 | + PY |
| 111 | + ) |
| 112 | +
|
| 113 | + if [ -n "${pinned_requirement}" ]; then |
| 114 | + requirement="${pinned_requirement}" |
| 115 | + else |
| 116 | + echo "::warning::Unable to find the clang-format pin in ${submodule}; using the latest version." |
| 117 | + fi |
| 118 | + else |
| 119 | + echo "::warning::Unable to check out ${submodule}; using the latest clang-format version." |
| 120 | + fi |
| 121 | +
|
| 122 | + # Keep the targeted submodule checkout out of every lint file search below. |
| 123 | + git submodule deinit --force -- "${submodule}" || true |
| 124 | + else |
| 125 | + echo "${submodule} is not configured; using the latest clang-format version." |
| 126 | + fi |
| 127 | +
|
| 128 | + echo "Installing ${requirement}" |
| 129 | + echo "CLANG_FORMAT_REQUIREMENT=${requirement}" >> "${GITHUB_ENV}" |
| 130 | +
|
83 | 131 | - name: Install Python dependencies |
84 | 132 | shell: bash |
85 | 133 | run: | |
86 | 134 | # shellcheck disable=SC2102 # this is triggered by the [toolchain] extra |
87 | 135 | python -m pip install --upgrade \ |
88 | | - "clang-format==${CLANG_FORMAT_VERSION}.*" \ |
| 136 | + "${CLANG_FORMAT_REQUIREMENT}" \ |
89 | 137 | pip \ |
90 | 138 | setuptools \ |
91 | 139 | wheel \ |
@@ -196,15 +244,32 @@ jobs: |
196 | 244 | - name: C++ - Clang format (diff) |
197 | 245 | id: clang_format_diff |
198 | 246 | if: always() && steps.cpp_files.outputs.found_files |
199 | | - uses: DoozyX/clang-format-lint-action@bcb4eb2cb0d707ee4f3e5cc3b456eb075f12cf73 # v0.20 |
200 | | - with: |
201 | | - source: ${{ steps.cpp_files.outputs.found_files }} |
202 | | - clangFormatVersion: '${{ env.CLANG_FORMAT_VERSION }}' |
203 | | - extensions: 'c,cpp,h,hpp,m,mm' |
204 | | - style: file |
205 | | - inplace: false |
| 247 | + shell: bash |
| 248 | + env: |
| 249 | + CPP_FILES: ${{ steps.cpp_files.outputs.found_files }} |
| 250 | + run: | |
| 251 | + set +e |
| 252 | + error=0 |
| 253 | + # shellcheck disable=SC2086 # split the space-delimited file list |
| 254 | + for file in ${CPP_FILES}; do |
| 255 | + clang-format --style=file "${file}" | |
| 256 | + diff \ |
| 257 | + --unified=3 \ |
| 258 | + --label "${file} (original)" \ |
| 259 | + --label "${file} (reformatted)" \ |
| 260 | + "${file}" \ |
| 261 | + - |
| 262 | + statuses=("${PIPESTATUS[@]}") |
| 263 | + if [ "${statuses[0]}" -ne 0 ]; then |
| 264 | + error=2 |
| 265 | + elif [ "${statuses[1]}" -gt "${error}" ]; then |
| 266 | + error="${statuses[1]}" |
| 267 | + fi |
| 268 | + done |
| 269 | + set -e |
| 270 | + exit "${error}" |
206 | 271 |
|
207 | | - - name: C++ - Clang format (simple) |
| 272 | + - name: C++ - Clang format (annotations) |
208 | 273 | if: always() && steps.clang_format_diff.outcome == 'failure' |
209 | 274 | shell: bash |
210 | 275 | run: | |
|
0 commit comments