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:
0name: CMake Multi-Platform Build
1
2on:
3 push:
4 branches: [ main, master ]
5 pull_request:
6 branches: [ main, master ]
7
8jobs:
9 build:
10 runs-on: ${{ matrix.os }}
11 strategy:
12 matrix:
13 include:
14 # Linux configurations
15 - os: ubuntu-22.04
16 compiler: gcc
17 cc: gcc
18 cxx: g++
19 generator: Ninja
20 - os: ubuntu-22.04
21 compiler: clang
22 cc: clang
23 cxx: clang++
24 generator: Ninja
25
26 # macOS configurations
27 - os: macos-13
28 compiler: appleclang
29 cc: clang
30 cxx: clang++
31 generator: Ninja
32
33 # Windows configurations
34 - os: windows-latest
35 compiler: msvc
36 generator: Visual Studio 17 2022
37 architecture: x64
38 - os: windows-latest
39 compiler: mingw
40 cc: gcc
41 cxx: g++
42 generator: MinGW Makefiles
43
44 steps:
45 - name: Checkout code
46 uses: actions/checkout@v4
47
48 # Install dependencies
49 - name: Install Linux dependencies
50 if: runner.os == 'Linux'
51 run: |
52 sudo apt-get update
53 sudo apt-get install -y ninja-build
54
55 - name: Install macOS dependencies
56 if: runner.os == 'macOS'
57 run: brew install ninja
58
59 - name: Install Windows dependencies (MinGW)
60 if: matrix.os == 'windows-latest' && matrix.compiler == 'mingw'
61 uses: msys2/setup-msys2@v2
62 with:
63 update: true
64 install: mingw-w64-x86_64-gcc mingw-w64-x86_64-ninja
65
66 # Setup compilers
67 - name: Set up GCC (Linux)
68 if: matrix.os == 'ubuntu-22.04' && matrix.compiler == 'gcc'
69 run: |
70 sudo apt-get update
71 sudo apt-get install -y gcc g++
72
73 - name: Set up Clang (Linux)
74 if: matrix.os == 'ubuntu-22.04' && matrix.compiler == 'clang'
75 run: |
76 sudo apt-get update
77 sudo apt-get install -y clang
78
79 # Configure CMake
80 - name: Configure CMake
81 shell: bash
82 run: |
83 cmake -B build \
84 -G "${{ matrix.generator }}" \
85 ${{ matrix.architecture && '-A ' + matrix.architecture || '' }} \
86 ${{ matrix.cc && '-DCMAKE_C_COMPILER=' + matrix.cc || '' }} \
87 ${{ matrix.cxx && '-DCMAKE_CXX_COMPILER=' + matrix.cxx || '' }} \
88 -DCMAKE_BUILD_TYPE=Release
89
90 # Build project
91 - name: Build
92 shell: bash
93 run: cmake --build build --config Release --parallel
94
95 # Run tests
96 - name: Run tests
97 shell: bash
98 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=ON
-
Build 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-13
to other versions likemacos-12
ormacos-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.