-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CI: Streamline the autoformat process and format versions
After much iteration, this is the simplest solution I could come up with that fulfills all the requirements: * A single clang-format version is used for all platforms * The version can be pinned in one central location * Whichever version is currently used will be tracked by git * Doesn't consume resources excessively thanks to caching * No reliance on third parties, other than the LLVM organization * Uses the same script for both CI runs and local development * No excessive branching/other accidental complexity * Bonus: Approach can also be used to install clang-tidy (later) Downloading a huge release tarball just for the tiny formatter feels like overkill, but it's reasonably fast in the CI.
- Loading branch information
1 parent
5583e79
commit 3665816
Showing
5 changed files
with
108 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
EVO_CLANGFORMAT_VERSION=19.1.2 | ||
EVO_STYLUA_VERSION=v0.20.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,41 +19,66 @@ jobs: | |
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
include: | ||
- os: ubuntu-latest | ||
stylua: stylua-linux.zip | ||
exe: "" | ||
- os: macos-latest | ||
stylua: stylua-macos.zip | ||
exe: "" | ||
- os: windows-latest | ||
stylua: stylua-win64.zip | ||
exe: ".exe" | ||
defaults: | ||
run: | ||
shell: bash | ||
|
||
steps: | ||
# MSYS needs to be available before running any git commands | ||
- name: Install clang-format (via MSYS) | ||
if: runner.os == 'Windows' | ||
uses: msys2/setup-msys2@v2 | ||
with: | ||
install: git mingw-w64-x86_64-clang | ||
|
||
- name: Disable autocrlf # Messes up everything on Windows since the formatter applies \n | ||
run: | | ||
git config --global core.autocrlf false | ||
git config --global core.eol lf | ||
- name: Check out Git repository | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 1 | ||
submodules: false | ||
|
||
- name: Set up environment | ||
run: | | ||
set -a | ||
source .github/autoformat.env | ||
cat .github/autoformat.env >> $GITHUB_ENV | ||
echo $(pwd) >> $GITHUB_PATH | ||
- name: Set up StyLua | ||
uses: JohnnyMorganz/[email protected] | ||
- name: Cache clang-format binary | ||
id: cache-clang-format | ||
uses: actions/cache@v4 | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
args: --check . --verbose | ||
version: v0.20.0 | ||
|
||
# The preinstalled version is positively antique; replace with the latest and greatest (to match MSYS) | ||
- name: Install clang-format (via APT) | ||
if: runner.os == 'Linux' | ||
run: ./deps/install-clang-format.sh | ||
|
||
- name: Install clang-format (via Homebrew) | ||
if: runner.os == 'macOS' | ||
run: brew install clang-format | ||
|
||
path: clang-format${{ matrix.exe }} | ||
key: clang-format-${{ runner.os }}-${{ env.EVO_CLANGFORMAT_VERSION }} | ||
restore-keys: | | ||
clang-format-${{ runner.os }}-${{ env.EVO_CLANGFORMAT_VERSION }} | ||
- name: Install clang-format | ||
if: steps.cache-clang-format.outputs.cache-hit != 'true' | ||
run: deps/get-clang-format.sh | ||
|
||
- name: Verify clang-format version | ||
run: which clang-format && clang-format --version | ||
|
||
- name: Install stylua | ||
run: | | ||
curl --location --output ${{ matrix.stylua }} https://github.com/JohnnyMorganz/StyLua/releases/download/${{ env.EVO_STYLUA_VERSION }}/${{ matrix.stylua }} | ||
unzip ${{ matrix.stylua }} | ||
chmod +x stylua | ||
- name: Verify stylua version | ||
run: which stylua && stylua --version | ||
|
||
- name: Run autoformat | ||
run: ./autoformat.sh | ||
|
||
- name: Check for inconsistent formatting | ||
run: git --no-pager diff --exit-code -b . #The -b is for inconsistent newlines, which we ignore (for now) | ||
run: git --no-pager diff --exit-code -b . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/bin/bash | ||
set -aeuo pipefail | ||
source .github/autoformat.env | ||
|
||
GITHUB_ORGANIZATION="llvm" | ||
GITHUB_REPOSITORY="llvm-project" | ||
REQUIRED_LLVM_VERSION=$EVO_CLANGFORMAT_VERSION # Pinned via ENV file to avoid headaches in CI runs | ||
LLVMORG_SUFFIX="llvmorg-$REQUIRED_LLVM_VERSION" | ||
GITHUB_BASE_URL="https://github.com/$GITHUB_ORGANIZATION/$GITHUB_REPOSITORY/releases/download/$LLVMORG_SUFFIX" | ||
|
||
echo "Downloading clang-format release for version $REQUIRED_LLVM_VERSION" | ||
|
||
PLATFORM=$(uname) | ||
echo "Detected platform: $PLATFORM" | ||
|
||
case $PLATFORM in | ||
MINGW64_NT*|CYGWIN_NT*|MSYS_NT*) | ||
LLVM_ASSET_NAME="LLVM-$REQUIRED_LLVM_VERSION-Windows-X64" | ||
CLANG_FORMAT_EXECUTABLE="clang-format.exe" | ||
;; | ||
Linux) | ||
LLVM_ASSET_NAME="LLVM-$REQUIRED_LLVM_VERSION-Linux-X64" | ||
CLANG_FORMAT_EXECUTABLE="clang-format" | ||
;; | ||
Darwin) | ||
LLVM_ASSET_NAME="LLVM-$REQUIRED_LLVM_VERSION-macOS-ARM64" | ||
CLANG_FORMAT_EXECUTABLE="clang-format" | ||
;; | ||
*) | ||
echo "Unsupported platform: $PLATFORM" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
LLVM_RELEASE_ASSET="$LLVM_ASSET_NAME.tar.xz" | ||
TARBALL_DOWNLOAD_DIR=$(pwd)/deps | ||
TARBALL_DOWNLOAD_FILE="$TARBALL_DOWNLOAD_DIR/$LLVM_RELEASE_ASSET" | ||
|
||
LLVM_RELEASE_URL="$GITHUB_BASE_URL/$LLVM_RELEASE_ASSET" | ||
echo "Fetching $LLVM_RELEASE_URL ..." | ||
curl --location --output "$TARBALL_DOWNLOAD_FILE" "$LLVM_RELEASE_URL" | ||
|
||
CLANG_FORMAT_PATH="$LLVM_ASSET_NAME/bin/$CLANG_FORMAT_EXECUTABLE" | ||
echo "Unpacking $CLANG_FORMAT_PATH ..." | ||
tar --strip-components=2 -xvf "$TARBALL_DOWNLOAD_FILE" "$CLANG_FORMAT_PATH" | ||
|
||
chmod +x "./$CLANG_FORMAT_EXECUTABLE" | ||
|
||
echo "Cleanup: Removing $TARBALL_DOWNLOAD_FILE ..." | ||
rm $TARBALL_DOWNLOAD_FILE |
This file was deleted.
Oops, something went wrong.