Skip to content

Sazwanismail patch 2#10

Open
Sazwanismail wants to merge 4 commits intoSazwanend:masterfrom
Sazwanismail:Sazwanismail-patch-2
Open

Sazwanismail patch 2#10
Sazwanismail wants to merge 4 commits intoSazwanend:masterfrom
Sazwanismail:Sazwanismail-patch-2

Conversation

@Sazwanismail
Copy link

No description provided.

Sazwanismail and others added 4 commits March 17, 2026 01:18
# Updated CMake Single Platform GitHub Actions Workflow

This repository provides an up-to-date GitHub Actions workflow for building CMake projects on a single platform (Linux, macOS, or Windows). It includes:

- CMake configuration and build
- Caching for dependencies (vcpkg, ccache)
- Multiple build types (Debug, Release)
- Running tests with CTest
- Uploading build artifacts
- Code formatting and linting (optional)

## 📄 Workflow File: `.github/workflows/cmake-single-platform.yml`

```yaml
name: CMake Single Platform Build

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:  # Allow manual trigger

env:
  BUILD_TYPE: Release
  # Customize CMake build type if needed

jobs:
  build:
    # Choose the runner: ubuntu-latest, windows-latest, or macos-latest
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    # ===== SETUP DEPENDENCIES =====
    - name: Install Linux dependencies (if any)
      if: runner.os == 'Linux'
      run: |
        sudo apt-get update
        sudo apt-get install -y ninja-build ccache
        # Add other packages as needed

    - name: Install macOS dependencies
      if: runner.os == 'macOS'
      run: |
        brew install ninja ccache
        # Add other packages

    - name: Install Windows dependencies (using Chocolatey)
      if: runner.os == 'Windows'
      run: |
        choco install ninja ccache
        # Or use vcpkg (see below)

    # ===== CACHE MANAGEMENT =====
    - name: Cache ccache
      uses: actions/cache@v4
      with:
        path: ~/.ccache
        key: ${{ runner.os }}-ccache-${{ github.sha }}
        restore-keys: ${{ runner.os }}-ccache-

    - name: Cache vcpkg (if used)
      if: false  # Enable if you use vcpkg
      uses: actions/cache@v4
      with:
        path: |
          ~/.cache/vcpkg
          build/vcpkg_installed
        key: ${{ runner.os }}-vcpkg-${{ hashFiles('**/vcpkg.json') }}
        restore-keys: ${{ runner.os }}-vcpkg-

    # ===== CONFIGURE & BUILD =====
    - name: Configure CMake
      run: |
        cmake -B build -S . \
          -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
          -G Ninja \
          -DCMAKE_C_COMPILER_LAUNCHER=ccache \
          -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
      env:
        CC: clang   # Override compiler if needed (gcc, clang, cl)
        CXX: clang++

    - name: Build
      run: cmake --build build --config ${{ env.BUILD_TYPE }} --parallel

    # ===== TEST =====
    - name: Test
      working-directory: build
      run: ctest -C ${{ env.BUILD_TYPE }} --output-on-failure --parallel

    # ===== CODE QUALITY (optional) =====
    - name: Run clang-format lint
      if: false  # Enable if you want formatting checks
      uses: jidicula/clang-format-action@v4.11.0
      with:
        clang-format-version: '16'
        check-path: 'src'

    # ===== ARTIFACTS =====
    - name: Upload build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: ${{ runner.os }}-${{ env.BUILD_TYPE }}-binaries
        path: |
          build/bin
          build/lib
          build/*.exe
          build/*.dll
          build/*.so
          build/*.dylib
        if-no-files-found: ignore
```

## 🔧 Customization Tips

1. **Runner OS**: Change `runs-on` to `windows-latest` or `macos-latest` as needed.
2. **Dependencies**: Adjust package installation steps for your specific libraries.
3. **vcpkg**: If your project uses vcpkg, enable the vcpkg cache step and install vcpkg in a setup step.
4. **Compiler**: Override `CC` and `CXX` environment variables to use different compilers (e.g., `gcc`, `clang`, `msvc`).
5. **Build Types**: You can matrix over `BUILD_TYPE` to build both Debug and Release, or add a strategy matrix.
6. **Artifacts**: Customize the artifact paths to match your output locations.

## 📦 Example with vcpkg and Matrix

If you need multiple build types or configurations, extend with a matrix:

```yaml
jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        build_type: [Debug, Release]
    env:
      BUILD_TYPE: ${{ matrix.build_type }}
    steps:
      # ... steps (use matrix.os and matrix.build_type)
```

## ✅ Best Practices Included

- **Caching** with ccache and vcpkg speeds up rebuilds.
- **Ninja** generator for faster builds.
- **Parallel** builds and tests.
- **Artifact** upload for easy access to binaries.
- **Manual trigger** (`workflow_dispatch`) for ad-hoc runs.

## 🔗 References

- [GitHub Actions Documentation](https://docs.github.com/actions)
- [CMake Documentation](https://cmake.org/cmake/help/latest/)
- [vcpkg with GitHub Actions](https://vcpkg.io/en/getting-started.html)

---

**Maintainer:** Your Team  
**License:** MIT
# Updated Advanced CMake Single/Multi-Platform Workflow

This workflow provides a robust CI pipeline for CMake projects, supporting multiple operating systems and build configurations. It includes caching, testing, code coverage, static analysis, and artifact upload.

## 📄 `.github/workflows/cmake-advanced.yml`

```yaml
name: CMake Advanced CI

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:        # Allow manual trigger

env:
  # Global build type; can be overridden per matrix
  BUILD_TYPE: Release

jobs:
  build:
    name: ${{ matrix.os }} / ${{ matrix.build_type }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false      # Continue other jobs if one fails
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        build_type: [Debug, Release]
        # Optionally exclude some combinations
        # exclude:
        #   - os: windows-latest
        #     build_type: Debug

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    # ===== DEPENDENCY INSTALLATION =====
    - name: Install Linux dependencies
      if: runner.os == 'Linux'
      run: |
        sudo apt-get update
        sudo apt-get install -y \
          ninja-build \
          ccache \
          lcov \
          clang-tidy \
          curl \
          zip
        # Add project-specific packages here

    - name: Install macOS dependencies
      if: runner.os == 'macOS'
      run: |
        brew install \
          ninja \
          ccache \
          llvm      # provides clang-tidy, lcov
        # Ensure llvm binaries are in PATH
        echo "$(brew --prefix llvm)/bin" >> $GITHUB_PATH

    - name: Install Windows dependencies
      if: runner.os == 'Windows'
      run: |
        choco install ninja ccache
        # vcpkg is usually installed on GitHub runners; if needed, bootstrap:
        # git clone https://github.com/Microsoft/vcpkg.git
        # .\vcpkg\bootstrap-vcpkg.bat
        # echo "${{ github.workspace }}/vcpkg" >> $GITHUB_PATH

    # ===== CACHE SETUP =====
    - name: Cache ccache
      uses: actions/cache@v4
      with:
        path: ~/.ccache
        key: ${{ runner.os }}-ccache-${{ matrix.build_type }}-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-ccache-${{ matrix.build_type }}-
          ${{ runner.os }}-ccache-

    - name: Cache vcpkg (if used)
      if: false   # Enable if you use vcpkg.json manifest mode
      uses: actions/cache@v4
      with:
        path: |
          ~/.cache/vcpkg
          ${{ github.workspace }}/build/vcpkg_installed
        key: ${{ runner.os }}-vcpkg-${{ hashFiles('**/vcpkg.json') }}
        restore-keys: ${{ runner.os }}-vcpkg-

    # ===== CONFIGURE CMAKE =====
    - name: Configure CMake
      shell: bash
      run: |
        cmake -B build -S . \
          -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
          -G Ninja \
          -DCMAKE_C_COMPILER_LAUNCHER=ccache \
          -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
          -DCMAKE_EXPORT_COMPILE_COMMANDS=ON   # for clang-tidy

    # ===== BUILD =====
    - name: Build
      run: cmake --build build --config ${{ matrix.build_type }} --parallel

    # ===== RUN TESTS =====
    - name: Test
      working-directory: build
      run: ctest -C ${{ matrix.build_type }} --output-on-failure --parallel

    # ===== STATIC ANALYSIS (clang-tidy) =====
    - name: Run clang-tidy
      if: runner.os == 'Linux' && matrix.build_type == 'Debug'   # Run once to avoid duplication
      working-directory: build
      run: |
        # Adjust source directory and checks as needed
        run-clang-tidy -p . -extra-arg=-Wno-unknown-warning-option -quiet

    # ===== CODE COVERAGE (Linux only) =====
    - name: Generate coverage report
      if: runner.os == 'Linux' && matrix.build_type == 'Debug' && github.event_name == 'push'
      run: |
        # Ensure you have built with coverage flags: -fprofile-arcs -ftest-coverage
        lcov --directory . --capture --output-file coverage.info
        lcov --remove coverage.info '/usr/*' --output-file coverage.info
        lcov --list coverage.info
      working-directory: build

    - name: Upload coverage to Codecov
      if: runner.os == 'Linux' && matrix.build_type == 'Debug' && github.event_name == 'push'
      uses: codecov/codecov-action@v4
      with:
        files: build/coverage.info
        flags: unittests
        name: codecov-umbrella
        fail_ci_if_error: false
        token: ${{ secrets.CODECOV_TOKEN }}

    # ===== UPLOAD ARTIFACTS =====
    - name: Prepare artifacts
      shell: bash
      run: |
        mkdir -p artifacts
        # Copy binaries, libraries, etc.
        if [ -d build/bin ]; then cp -r build/bin artifacts/; fi
        if [ -d build/lib ]; then cp -r build/lib artifacts/; fi
        # Include compile_commands.json for debugging
        cp build/compile_commands.json artifacts/ || true

    - name: Upload build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: ${{ runner.os }}-${{ matrix.build_type }}-artifacts
        path: artifacts/
```

## 🚀 Key Features

- **Matrix Build**: Builds on Ubuntu, Windows, and macOS with both Debug and Release configurations.
- **Caching**: Uses `ccache` to speed up rebuilds; vcpkg cache ready.
- **Dependencies**: Installs `ninja`, `ccache`, and platform-specific tools.
- **Static Analysis**: Runs `clang-tidy` (on Linux/Debug once).
- **Code Coverage**: Generates and uploads coverage reports to Codecov (Linux/Debug only).
- **Artifacts**: Uploads binaries, libraries, and `compile_commands.json`.

## 🔧 Customization

- **Add more packages**: Modify the dependency installation steps.
- **Adjust coverage**: Ensure your CMake project enables coverage flags when `CMAKE_BUILD_TYPE` is Debug.
- **vcpkg**: Uncomment the cache step and add a vcpkg installation step if needed.
- **Compiler**: Override `CC`/`CXX` in the configure step to use specific compilers.
- **Artifacts**: Modify the `Prepare artifacts` step to capture your desired output.

## ✅ Best Practices

- **fail-fast: false** allows all matrix jobs to run even if one fails.
- **Conditional steps** avoid redundant work (e.g., coverage only once).
- **Caching keys** use both OS and build type to avoid mixing caches.
- **Parallel** builds and tests reduce CI time.

## 📚 References

- [GitHub Actions Documentation](https://docs.github.com/actions)
- [CMake Documentation](https://cmake.org/documentation)
- [Codecov Action](https://github.com/codecov/codecov-action)
- [clang-tidy Integration](https://clang.llvm.org/extra/clang-tidy/)

---

**Maintainer:** Your Team  
**License:** MIT
@gemini-code-assist
Copy link

Note

Gemini is unable to generate a summary for this pull request due to the file types involved not being currently supported.

@google-cla
Copy link

google-cla bot commented Mar 23, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants