Skip to content

Commit

Permalink
Update clang-format binary with a workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
aarcangeli committed Feb 22, 2025
1 parent b5a2501 commit ab75a46
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 20 deletions.
144 changes: 144 additions & 0 deletions .github/workflows/update-clang.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
name: Update Clang
on:
workflow_dispatch:
schedule:
- cron: '6 6 * * *'

# Cancel outdated jobs
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}

permissions:
pull-requests: write
contents: write

jobs:

# Prepare environment and build the plugin
update-clang:
name: Update Clang
runs-on: ubuntu-latest
outputs:
version: ${{ steps.properties.outputs.version }}
changelog: ${{ steps.properties.outputs.changelog }}
pluginVerifierHomeDir: ${{ steps.properties.outputs.pluginVerifierHomeDir }}
steps:

- name: Fetch Sources
uses: actions/checkout@v4

- name: Check for updates
id: check-for-updates
run: |
latest_tag=$(gh release view --repo llvm/llvm-project --json tagName --jq .tagName)
echo "Latest tag: $latest_tag"
branch_name="autoupdate-clang-$latest_tag"
current_version=$(cat src/main/resources/clang-format-tag.txt)
found_branch=$(gh api repos/llvm/llvm-project/branches/$branch_name --jq .name 2> /dev/null || true)
echo "Branch found: $found_branch"
# We need to update if: 1. The current branch is outdated and 2. There is no other branch with the same name
if [ "$latest_tag" != "$current_version" ] && [ "$found_branch" != "$branch_name" ]; then
echo -e "\033[31mUpdate to $latest_tag is needed\033[0m"
echo "update_needed=true" >> $GITHUB_OUTPUT
echo "version=$latest_tag" >> $GITHUB_OUTPUT
else
echo -e "\033[32mUpdate to $latest_tag is not needed\033[0m"
echo "update_needed=false" >> $GITHUB_OUTPUT
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Python
uses: actions/setup-python@v5
if: steps.check-for-updates.outputs.update_needed == 'true'
with:
python-version: '3.13'

- name: Generate schema
if: steps.check-for-updates.outputs.update_needed == 'true'
run: |
latest_tag=${{ steps.check-for-updates.outputs.version }}
cd schemaGenerator
python generate_schema.py --download --clang-tag $latest_tag
- name: Update clang-format Binary
if: steps.check-for-updates.outputs.update_needed == 'true'
run: |
latest_tag=${{ steps.check-for-updates.outputs.version }}
echo "Downloading windows binary"
echo "::group::Download Details"
gh release download $latest_tag --repo llvm/llvm-project --pattern "clang+llvm-*windows-msvc.tar.xz" --dir clang
tar -xvf clang/clang+llvm-*windows-msvc.tar.xz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format.exe'
mkdir -p src/main/resources/clang-format-win
mv clang/clang-format.exe src/main/resources/clang-format-win/clang-format.exe
rm clang/clang+llvm-*
echo "::endgroup::"
echo "Downloading linux aarch64 binary"
echo "::group::Download Details"
gh release download $latest_tag --repo llvm/llvm-project --pattern "clang+llvm-*-aarch64-linux-gnu.tar.xz" --dir clang
tar -xvf clang/clang+llvm-*-aarch64-linux-gnu.tar.xz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
mkdir -p src/main/resources/clang-format-linux-aarch64
mv clang/clang-format src/main/resources/clang-format-linux-aarch64/clang-format
chmod +x src/main/resources/clang-format-linux-aarch64/clang-format
rm clang/clang+llvm-*
echo "::endgroup::"
echo "Downloading linux armv7a binary"
echo "::group::Download Details"
gh release download $latest_tag --repo llvm/llvm-project --pattern "clang+llvm-*-armv7a-linux-*.tar.gz" --dir clang
tar -xvf clang/clang+llvm-*-armv7a-linux-*.tar.gz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
mkdir -p src/main/resources/clang-format-linux-armv7a
mv clang/clang-format src/main/resources/clang-format-linux-armv7a/clang-format
chmod +x src/main/resources/clang-format-linux-armv7a/clang-format
rm clang/clang+llvm-*
echo "::endgroup::"
echo "Downloading macos x64 binary"
echo "::group::Download Details"
gh release download $latest_tag --repo llvm/llvm-project --pattern "LLVM-*-macOS-X64.tar.xz" --dir clang
tar -xvf clang/LLVM-*-macOS-X64.tar.xz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
mkdir -p src/main/resources/clang-format-macos-x64
mv clang/clang-format src/main/resources/clang-format-macos-x64/clang-format
chmod +x src/main/resources/clang-format-macos-x64/clang-format
rm clang/LLVM-*
echo "::endgroup::"
echo "Downloading macos arm64 binary"
echo "::group::Download Details"
gh release download $latest_tag --repo llvm/llvm-project --pattern "LLVM-*-macOS-ARM64.tar.xz" --dir clang
tar -xvf clang/LLVM-*-macOS-ARM64.tar.xz --strip-components=2 -C clang --wildcards --no-anchored 'clang-format'
mkdir -p src/main/resources/clang-format-macos-arm64
mv clang/clang-format src/main/resources/clang-format-macos-arm64/clang-format
chmod +x src/main/resources/clang-format-macos-arm64/clang-format
rm clang/LLVM-*
echo "::endgroup::"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create branch and commit
if: steps.check-for-updates.outputs.update_needed == 'true'
run: |
latest_tag=${{ steps.check-for-updates.outputs.version }}
echo $latest_tag > src/main/resources/clang-format-tag.txt
git config --global user.email "[email protected]"
git config --global user.name "Alessandro Arcangeli (bot)"
branch_name="autoupdate-clang-$latest_tag"
git checkout -b $branch_name
git add src/*
git add schemaGenerator/*
git status
git commit -m "Update clang-format to $latest_tag"
- name: Create Pull Request
if: steps.check-for-updates.outputs.update_needed == 'true'
run: |
latest_tag=${{ steps.check-for-updates.outputs.version }}
branch_name="autoupdate-clang-$latest_tag"
echo "Pushing branch $branch_name"
git push origin $branch_name
echo "Creating pull request"
gh pr create --title "Update clang-format to $latest_tag" --body "Update clang-format to $latest_tag" --base main --head $branch_name
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.intellijPlatform
.qodana
build
__pycache__
15 changes: 11 additions & 4 deletions schemaGenerator/generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
# Based on dump_format_style.py

# Update the CLANG_BRANCH to the latest version
CLANG_BRANCH = "release/19.x"
CLANG_ROOT = f"https://raw.githubusercontent.com/llvm/llvm-project/{CLANG_BRANCH}/clang"
CLANG_ROOT = f"https://raw.githubusercontent.com/llvm/llvm-project/__branch__/clang"

PROJECT_ROOT = Path(__file__).parent.parent
OUTPUT_FILE = str(PROJECT_ROOT / "src/main/resources/schemas/clangFormat-options.json")
Expand Down Expand Up @@ -231,13 +230,21 @@ def main():
help="Download the latest version of the schema from the GitHub",
)

parser.add_argument(
"--clang-tag",
type=str,
default="main",
help="The tag of the clang-format repository",
)

args = parser.parse_args()

# Download the latest version of the schema from the GitHub
if args.download:
download_file(f"{CLANG_ROOT}/include/clang/Format/Format.h", "clang/Format.h")
root = CLANG_ROOT.replace("__branch__", args.clang_tag)
download_file(f"{root}/include/clang/Format/Format.h", "clang/Format.h")
download_file(
f"{CLANG_ROOT}/include/clang/Tooling/Inclusions/IncludeStyle.h",
f"{root}/include/clang/Tooling/Inclusions/IncludeStyle.h",
"clang/IncludeStyle.h",
)

Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/clang-format-linux-aarch64/clang-format

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/resources/clang-format-linux-armv7a/clang-format

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/resources/clang-format-macos-arm64/clang-format

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/resources/clang-format-macos-x64/clang-format

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/clang-format-tag.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
llvmorg-19.1.7
FORCE_REBUILD
3 changes: 0 additions & 3 deletions src/main/resources/clang-format-win/clang-format.exe

This file was deleted.

0 comments on commit ab75a46

Please sign in to comment.