Here's a cmake-multi-platform.yml GitHub Actions workflow that builds and tests a CMake project on multiple platforms (Linux, macOS, Windows) with different compilers:
name: CMake Multi-Platform Build
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
# Linux configurations
- os: ubuntu-22.04
compiler: gcc
cc: gcc
cxx: g++
generator: Ninja
- os: ubuntu-22.04
compiler: clang
cc: clang
cxx: clang++
generator: Ninja
# macOS configurations
- os: macos-13
compiler: appleclang
cc: clang
cxx: clang++
generator: Ninja
# Windows configurations
- os: windows-latest
compiler: msvc
generator: Visual Studio 17 2022
architecture: x64
- os: windows-latest
compiler: mingw
cc: gcc
cxx: g++
generator: MinGW Makefiles
steps:
- name: Checkout code
uses: actions/checkout@v4
# Install dependencies
- name: Install Linux dependencies
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y ninja-build
- name: Install macOS dependencies
if: runner.os == 'macOS'
run: brew install ninja
- name: Install Windows dependencies (MinGW)
if: matrix.os == 'windows-latest' && matrix.compiler == 'mingw'
uses: msys2/setup-msys2@v2
with:
update: true
install: mingw-w64-x86_64-gcc mingw-w64-x86_64-ninja
# Setup compilers
- name: Set up GCC (Linux)
if: matrix.os == 'ubuntu-22.04' && matrix.compiler == 'gcc'
run: |
sudo apt-get update
sudo apt-get install -y gcc g++
- name: Set up Clang (Linux)
if: matrix.os == 'ubuntu-22.04' && matrix.compiler == 'clang'
run: |
sudo apt-get update
sudo apt-get install -y clang
# Configure CMake
- name: Configure CMake
shell: bash
run: |
cmake -B build \
-G "${{ matrix.generator }}" \
${{ matrix.architecture && '-A ' + matrix.architecture || '' }} \
${{ matrix.cc && '-DCMAKE_C_COMPILER=' + matrix.cc || '' }} \
${{ matrix.cxx && '-DCMAKE_CXX_COMPILER=' + matrix.cxx || '' }} \
-DCMAKE_BUILD_TYPE=Release
# Build project
- name: Build
shell: bash
run: cmake --build build --config Release --parallel
# Run tests
- name: Run tests
shell: bash
run: ctest --test-dir build --output-on-failure -C Release
Key Features:
Multi-Platform Support:
- Ubuntu Linux (GCC, Clang)
- macOS (Apple Clang)
- Windows (MSVC, MinGW)
Compiler Matrix:
- GCC (Linux)
- Clang (Linux)
- Apple Clang (macOS)
- MSVC (Windows)
- MinGW (Windows)
Build System:
- Uses Ninja for Linux/macOS
- Uses Visual Studio generator for MSVC
- Uses MinGW Makefiles for MinGW
Configuration Options:
- Automatic generator selection
- Compiler detection
- Architecture specification for Windows
- Release builds
Testing:
- Runs tests using CTest
- Shows failure outputs
Customization Notes:
CMake Options: Add additional CMake flags in the "Configure CMake" step:
yaml -DCMAKE_CXX_STANDARD=17 -DOPTION_NAME=ONBuild Types: To add Debug builds, duplicate matrix entries and change
CMAKE_BUILD_TYPE.Windows Architecture: For ARM builds, change
architecture: ARM64.macOS Version: Adjust
macos-13to other versions likemacos-12ormacos-latest.Dependencies: Add system dependencies in the appropriate "Install dependencies" section.
This workflow provides a solid foundation for CI/CD of CMake projects across different platforms and compilers. The matrix strategy ensures all configurations are tested independently, and the setup optimizes build times through parallelization.
<!-- *** Please remove the following help text before submitting: *** Pull requests without a rationale and clear improvement may be closed immediately. GUI-related pull requests should be opened against https://github.com/bitcoin-core/gui first. See CONTRIBUTING.md -->
<!-- Please provide clear motivation for your patch and explain how it improves Bitcoin Core user experience or Bitcoin Core developer experience significantly: * Any test improvements or new tests that improve coverage are always welcome. * All other changes should have accompanying unit tests (see `src/test/`) or functional tests (see `test/`). Contributors should note which tests cover modified code. If no tests exist for a region of modified code, new tests should accompany the change. * Bug fixes are most welcome when they come with steps to reproduce or an explanation of the potential issue as well as reasoning for the way the bug was fixed. * Features are welcome, but might be rejected due to design or scope issues. If a feature is based on a lot of dependencies, contributors should first consider building the system outside of Bitcoin Core, if possible. * Refactoring changes are only accepted if they are required for a feature or bug fix or otherwise improve developer experience significantly. For example, most "code style" refactoring changes require a thorough explanation why they are useful, what downsides they have and why they *significantly* improve developer experience or avoid serious programming bugs. Note that code style is often a subjective matter. Unless they are explicitly mentioned to be preferred in the [developer notes](/doc/developer-notes.md), stylistic code changes are usually rejected. -->
<!-- Bitcoin Core has a thorough review process and even the most trivial change needs to pass a lot of eyes and requires non-zero or even substantial time effort to review. There is a huge lack of active reviewers on the project, so patches often sit for a long time. -->