Skip to content

Commit

Permalink
CI: Streamline the autoformat process and format versions
Browse files Browse the repository at this point in the history
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 none of the other solutions I explored were a net gain.
  • Loading branch information
rdw-software committed Oct 19, 2024
1 parent 5583e79 commit bbe2d85
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .github/autoformat.env
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
71 changes: 48 additions & 23 deletions .github/workflows/autoformat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
21 changes: 8 additions & 13 deletions autoformat.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# See deps/install-clang-format.sh for the required version (should always match)
REQUIRED_CLANG_FORMAT_VERSION="17"
CLANG_FORMAT="clang-format-$REQUIRED_CLANG_FORMAT_VERSION"

# The MSYS version may lag behind somewhat, so a fallback option is needed
if ! command -v $CLANG_FORMAT &> /dev/null
then
echo "clang-format-$REQUIRED_CLANG_FORMAT_VERSION not found. Using the default clang-format instead"
echo
CLANG_FORMAT="clang-format"
fi
#!/bin/bash
set -aeuo pipefail

STYLUA="stylua"
CLANG_FORMAT="clang-format"
echo "Installed formatters:"
echo

echo "* " $(stylua --version)
echo "* " $($CLANG_FORMAT --version)
echo $(which $STYLUA)
echo $($STYLUA --version)
echo $(which $CLANG_FORMAT)
echo $($CLANG_FORMAT --version)
echo

echo "Formatting Lua sources ..."
Expand Down
50 changes: 50 additions & 0 deletions deps/get-clang-format.sh
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
27 changes: 0 additions & 27 deletions deps/install-clang-format.sh

This file was deleted.

0 comments on commit bbe2d85

Please sign in to comment.