diff --git a/projects/amdsmi/.github/workflows/cmake_format.yml b/projects/amdsmi/.github/workflows/cmake_format.yml new file mode 100644 index 0000000000..accb6088f3 --- /dev/null +++ b/projects/amdsmi/.github/workflows/cmake_format.yml @@ -0,0 +1,98 @@ +# caution: most of this file was written using Claude 3.7 Sonnet +name: CMake Format Check + +on: + push: + branches: [ amd-staging ] + paths: + - '**/*.cmake' + - '**/CMakeLists.txt' + - '**/*.cmake.in' + pull_request: + branches: [ amd-staging ] + paths: + - '**/*.cmake' + - '**/CMakeLists.txt' + - '**/*.cmake.in' + workflow_dispatch: # Allows manual triggering + +defaults: + run: + shell: bash + +jobs: + check-cmake-format: + name: Check CMake files formatting + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Full history for better diff context + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + cache: 'pip' + + - name: Install cmake-format + run: | + python -m pip install --upgrade pip + pip install cmake-format==0.6.13 + + - name: Check CMake formatting + id: check-format + run: | + echo "::group::Finding CMake files" + FILES=$(find . -type f \( -name "CMakeLists.txt" -o -name "*.cmake" -o -name "*.cmake.in" \) \ + -not -path "*/esmi_ib_library/*" \ + -not -path "*/\.*" \ + -not -path "*/build/*") + echo "Found $(echo "$FILES" | wc -l) CMake files to check" + echo "::endgroup::" + + # Create an array to store failed files + declare -a failed_files + + # Check if files are formatted correctly + for file in $FILES; do + echo "Checking $file..." + if ! cmake-format --check "$file"; then + failed_files+=("$file") + echo "::error file=$file::File needs formatting" + fi + done + + # Generate report and exit with error if any files failed + if [ ${#failed_files[@]} -ne 0 ]; then + echo "Failed files: ${failed_files[*]}" + echo "FAILED_FILES=${failed_files[*]}" >> $GITHUB_ENV + exit 1 + else + echo "All CMake files are formatted correctly!" + fi + + - name: Generate diff for failed files + if: failure() && env.FAILED_FILES != '' + run: | + echo "## CMake Format Check Failed" >> $GITHUB_STEP_SUMMARY + echo "The following files need formatting:" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + + for file in ${FAILED_FILES}; do + echo "### $file" >> $GITHUB_STEP_SUMMARY + done + + cat << 'EOF' >> $GITHUB_STEP_SUMMARY + ### How to fix + Run this command locally to fix formatting issues: + ```bash + # Install cmake-format + pip install cmake-format==0.6.13 + + # Format files + cmake-format -i + ``` + EOF diff --git a/projects/amdsmi/tools/cmake_format.sh b/projects/amdsmi/tools/cmake_format.sh new file mode 100755 index 0000000000..0be3160fda --- /dev/null +++ b/projects/amdsmi/tools/cmake_format.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +set -e +set -u +set -o pipefail + +FILES=$(find . -type f \( -name "CMakeLists.txt" -o -name "*.cmake" -o -name "*.cmake.in" \) \ + -not -path "*/esmi_ib_library/*" \ + -not -path "*/\.*" \ + -not -path "*/build/*") + +declare -a failed_files + +# Check if files are formatted correctly +for file in $FILES; do + echo "Checking $file..." + if ! cmake-format --check "$file"; then + failed_files+=("$file") + echo "::error file=$file::File needs formatting" + fi +done + +if [ ${#failed_files[@]} -ne 0 ]; then + cmake-format -i "${failed_files[@]}" +fi