Skip to content

Instantly share code, notes, and snippets.

@NickNaso
Created June 3, 2020 23:55
Show Gist options
  • Star 56 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save NickNaso/0d478f1481686d5bcc868cac06620a60 to your computer and use it in GitHub Desktop.
Save NickNaso/0d478f1481686d5bcc868cac06620a60 to your computer and use it in GitHub Desktop.
Example of Github action for C++ rpoject
# This is a basic workflow to help you get started with Actions
# workflow - цепочка действий
# Имя процесса Билдится на всех типах 📦 🐍
name: CMake Build Matrix
# Controls when the action will run. Triggers the workflow on push
on:
push:
pull_request:
release:
# tags:
# - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
name: ${{ matrix.config.name }}
runs-on: ${{ matrix.config.os }} # будет запускаться по очереди на всех типах машин
strategy:
fail-fast: false
matrix:
config:
- {
name: "Windows Latest MSVC",
os: windows-latest,
artifact: "windows_msvc.7z",
build_type: "Release",
cc: "cl",
cxx: "cl",
environment_script: "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Auxiliary/Build/vcvars64.bat",
archiver: "7z a",
generators: "Visual Studio 16 2019"
}
- {
name: "Windows Latest MinGW",
os: windows-latest,
artifact: "windows_mingw.7z",
build_type: "Release",
cc: "gcc",
cxx: "g++",
archiver: "7z a",
generators: "Ninja"
}
- {
name: "Ubuntu_Latest_GCC",
os: ubuntu-latest,
artifact: "ubuntu_gcc.7z",
build_type: "Release",
cc: "gcc",
cxx: "g++",
archiver: "7z a",
generators: "Ninja"
}
- {
name: "Ubuntu_GCC_9",
os: ubuntu-latest,
artifact: "ubuntu_gcc9.7z",
build_type: "Release",
cc: "gcc",
cxx: "g++",
archiver: "7z a",
generators: "Ninja"
}
- {
name: "macOS Latest Clang",
os: macos-latest,
artifact: "macos_clang.7z",
build_type: "Release",
cc: "clang",
cxx: "clang++",
archiver: "7za a",
generators: "Ninja"
}
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Print env
run: |
echo github.event.action: ${{ github.event.action }}
echo github.event_name: ${{ github.event_name }}
- name: Install dependencies on windows
if: startsWith(matrix.config.os, 'windows')
run: |
choco install ninja cmake
ninja --version
cmake --version
# cmd "${{ matrix.config.environment_script }}"
- name: Install dependencies on ubuntu
if: startsWith(matrix.config.name, 'Ubuntu_Latest_GCC')
run: |
sudo apt-get update
sudo apt-get install ninja-build cmake
ninja --version
cmake --version
gcc --version
- name: Install dependencies on ubuntu9
if: startsWith(matrix.config.name, 'Ubuntu_GCC_9')
run: |
echo Update gcc-9 =======================================================================
echo gcc version before
gcc --version
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install ninja-build cmake gcc-9 g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9
echo gcc version after
gcc --version
echo Update ninja =======================================================================
echo ninja version before
ninja --version
# wget https://github.com/ninja-build/ninja/releases/download/v1.10.0/ninja-linux.zip
wget https://github.com/ninja-build/ninja/releases/latest/download/ninja-linux.zip
sudo unzip ninja-linux.zip -d /usr/local/bin/
sudo update-alternatives --install /usr/bin/ninja ninja /usr/local/bin/ninja 1 --force
echo ninja version after
ninja --version
echo Update cmake =======================================================================
echo cmake version before
cmake --version
# curl --silent "https://api.github.com/repos/Kitware/CMake/releases/latest" | sed -n 's/.*tag_name":\s"\(.*\)".*/\1/p' | head -2
# wget https://github.com/Kitware/CMake/releases/latest/download/cmake-3.16.5-Linux-x86_64.sh
cmake_version=$(curl --silent "https://api.github.com/repos/Kitware/CMake/releases/latest" | sed -n 's/.*tag_name":\s"\(.*\)".*/\1/p' | head -2 | cut -c 2-)
echo cmake download latest v$cmake_version version
wget https://github.com/Kitware/CMake/releases/download/v$cmake_version/cmake-$cmake_version-Linux-x86_64.sh
chmod +x cmake-$cmake_version-Linux-x86_64.sh
sudo mkdir /opt/cmake
sudo ./cmake-$cmake_version-Linux-x86_64.sh --prefix=/opt/cmake --skip-license
sudo update-alternatives --install /usr/bin/cmake cmake /opt/cmake/bin/cmake 1 --force
echo cmake version after
cmake --version
- name: Install dependencies on macos
if: startsWith(matrix.config.os, 'macos')
run: |
brew install p7zip cmake ninja
ninja --version
cmake --version
- name: Configure
shell: bash
run: |
mkdir build
mkdir instdir
cmake \
-S . \
-B . \
-DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \
-G "${{ matrix.config.generators }}" \
-DCMAKE_INSTALL_PREFIX:PATH=instdir
- name: Build
shell: bash
run: cmake --build . --config ${{ matrix.config.build_type }}
- name: Install Strip
shell: bash
run: cmake --install . --strip
- name: Pack
shell: bash
working-directory: instdir
run: |
ls -laR
${{ matrix.config.archiver }} ../${{ matrix.config.artifact }} .
- name: Upload
uses: actions/upload-artifact@v1
with:
path: ./${{ matrix.config.artifact }}
name: ${{ matrix.config.artifact }}
- name: Upload release asset
if: github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'created')
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./${{ matrix.config.artifact }}
asset_name: ${{ matrix.config.artifact }}.zip
asset_content_type: application/zip
@jkalias
Copy link

jkalias commented Jul 26, 2021

Hi, this looks exactly what I need for https://github.com/jkalias/functional_vector. Would you mind if I copy-pasted it for my project?

@NickNaso
Copy link
Author

Hi @jkalias,
yes sure use this for what you need.

@jkalias
Copy link

jkalias commented Jul 26, 2021

Wow, that worked like a charm. No hiccups or whatsoever. Perfect, thank you sooooo much!
I added a link to your gist as the least token of attribution, I hope you don't mind
https://github.com/jkalias/functional_vector/blob/main/.github/workflows/cmake.yml

@rishid
Copy link

rishid commented Aug 30, 2021

Thanks for this -- have you ever tried to build with Clang on Windows? It seems like -T ClangCL is needed but I can't get it working.

@jkalias
Copy link

jkalias commented Aug 31, 2021

I don’t have a Windows machine, so I trusted what the GitHub action result was

@addy1997
Copy link

Hey @NickNaso,
If you don't mind can I use it for my Cpp project? I will put a link to your gist as an attribution/reference.

@NickNaso
Copy link
Author

Hey @addy1997,
this gist is only an example that I use. You don't need to add the link to it.

@addy1997
Copy link

@NickNaso, okay, thanks

@camielverdult
Copy link

Any idea how to define the CXX standard? cmake seems to ignore it even though it's defined in CMakeLists.txt. Take a look at this: https://github.com/camielverdult/APC-Library/runs/4843141711?check_suite_focus=true It fails due to a missing operator!=, even though that operator is automatically generated if operator== is defined in C++20 (see image below).
image

@camielverdult
Copy link

This does not happen on the mac build for some reason?

@NickNaso
Copy link
Author

Hi @camielverdult,
what I know is that C++20 is not yet the default option for the compiler to I think that you should set the flag -std=HERE-SOME-VALUE. The value is different across compiler version for example for GCC:

  • GCC 8: -std=c++20
  • GCC 9: -std=c++2a

Sometimes if you set the standard in cmake it uses the default library that is not the C++20 library and you get errors. I'm not sure if this is the real problem.

@camielverdult
Copy link

Thanks, I let github generate the workflow for cmake. I appreciate the gist!

@LogeshG5
Copy link

Jobs using the windows-latest runner label migrated from Windows Server 2019 to Windows Server 2022. See article.

Use updated environment_script and generators like shown below,

            name: "Windows Latest MSVC",
            environment_script: "C:/Program Files (x86)/Microsoft Visual Studio/2022/Enterprise/VC/Auxiliary/Build/vcvars64.bat",
            generators: "Visual Studio 17 2022"

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