generated from lukka/CppCMakeVcpkgTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 097c750
Showing
14 changed files
with
408 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright (c) 2021-2022-2023-2024 Luca Cappa | ||
# Released under the term specified in file LICENSE.txt | ||
# SPDX short identifier: MIT | ||
# | ||
# The peculiarity of this workflow is that assumes vcpkg stored as a submodule of this repository. | ||
# The workflow runs on x64 and ARM platforms. | ||
# Workflow steps: | ||
# - Setup vcpkg and cache it on the GitHub Action cloud based cache. | ||
# - Runs CMake with CMakePreset.json using a presest configuration | ||
# that leverages the vcpkg's toolchain file. This will automatically run vcpkg | ||
# to install dependencies described by the vcpkg.json manifest file. | ||
# This stage also runs vcpkg with Binary Caching leveraging GitHub Action cache to | ||
# store the built packages artifacts, hence it will be a no-op if those are restored | ||
# from cache (e.g., already previously built). | ||
# - Finally builds the sources with Ninja, and tests as well. | ||
name: hosted-ninja-vcpkg_submod-autocache | ||
on: | ||
push: | ||
pull_request: | ||
branches: | ||
- v11 | ||
- main | ||
workflow_dispatch: | ||
schedule: | ||
- cron: '0 1 * * *' | ||
|
||
jobs: | ||
job: | ||
name: ${{ matrix.os }}-${{ github.workflow }} | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
#env: | ||
# | ||
# [OPTIONAL] Define the vcpkg's triplet | ||
# you want to enforce, otherwise the default one | ||
# for the hosting system will be automatically | ||
# choosen (x64 is the default on all platforms, | ||
# e.g. x64-osx). | ||
# VCPKG_DEFAULT_TRIPLET: ${{ matrix.triplet }} | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
submodules: true | ||
|
||
- uses: lukka/get-cmake@latest | ||
|
||
- name: Restore from cache and setup vcpkg executable and data files. | ||
uses: lukka/run-vcpkg@v11 | ||
with: | ||
vcpkgJsonGlob: 'vcpkg.json' | ||
|
||
# Note: if the preset misses the "configuration", it is possible to explicitly select the | ||
# configuration with the additional `--config` flag, e.g.: | ||
# buildPreset: 'ninja-vcpkg' | ||
# buildPresetAdditionalArgs: "[`--config`, `Release`]" | ||
# testPreset: 'ninja-vcpkg' | ||
# testPresetAdditionalArgs: "[`--config`, `Release`]" | ||
- name: Run CMake+vcpkg+Ninja+CTest to build packages and generate/build/test the code. | ||
uses: lukka/run-cmake@v10 | ||
with: | ||
configurePreset: 'ninja-multi-vcpkg' | ||
buildPreset: 'ninja-vcpkg-release' | ||
testPreset: 'test-release' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Copyright (c) 2021-2022-2023-2024 Luca Cappa | ||
# Released under the term specified in file LICENSE.txt | ||
# SPDX short identifier: MIT | ||
|
||
# A "pure" GitHub workflow using CMake, Ninja and vcpkg to build a C/C++ codebase. | ||
# It leverages both CMakePresets.json and vcpkg.json. | ||
# It is called "pure workflow" because it is an example which minimizes the usage of | ||
# custom GitHub Actions, but leverages directly the tools that could be easily run on | ||
# your development machines (i.e. CMake, vcpkg, Ninja) to ensure a perfectly identical | ||
# and reproducible local build (on your development machine) and a remote build on | ||
# build agents. | ||
name: hosted-pure-workflow | ||
on: | ||
push: | ||
pull_request: | ||
branches: | ||
- v11 | ||
- main | ||
workflow_dispatch: | ||
schedule: | ||
- cron: '0 1 * * *' | ||
|
||
jobs: | ||
job: | ||
name: ${{ matrix.os }}-${{ github.workflow }} | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
include: | ||
- os: windows-latest | ||
triplet: x64-windows | ||
- os: ubuntu-latest | ||
triplet: x64-linux | ||
- os: macos-latest | ||
triplet: x64-osx | ||
env: | ||
# Indicates the location of the vcpkg as a Git submodule of the project repository. | ||
# Not using "VCPKG_ROOT" because a variable with the same name is defined in the VS's | ||
# Developer Command Prompt environment in VS 2022 17.6, which would override this one | ||
# if it had the same name. | ||
_VCPKG_: ${{ github.workspace }}/vcpkg | ||
# Tells vcpkg where binary packages are stored. | ||
VCPKG_DEFAULT_BINARY_CACHE: ${{ github.workspace }}/vcpkg/bincache | ||
# Let's use GitHub Action cache as storage for the vcpkg Binary Caching feature. | ||
VCPKG_BINARY_SOURCES: 'clear;x-gha,readwrite' | ||
|
||
steps: | ||
# Set env vars needed for vcpkg to leverage the GitHub Action cache as a storage | ||
# for Binary Caching. | ||
- uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || ''); | ||
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || ''); | ||
- uses: actions/checkout@v4 | ||
with: | ||
submodules: true | ||
- name: "Create directory '${{ env.VCPKG_DEFAULT_BINARY_CACHE }}'" | ||
run: mkdir -p $VCPKG_DEFAULT_BINARY_CACHE | ||
shell: bash | ||
|
||
# Setup the build machine with the most recent versions of CMake and Ninja. Both are cached if not already: on subsequent runs both will be quickly restored from GitHub cache service. | ||
- uses: lukka/get-cmake@latest | ||
|
||
# Restore vcpkg from the GitHub Action cache service. Note that packages are restored by vcpkg's binary caching | ||
# when it is being run afterwards by CMake. | ||
- name: Restore vcpkg | ||
uses: actions/cache@v4 | ||
with: | ||
# The first path is the location of vcpkg: it contains the vcpkg executable and data files, as long as the | ||
# built package archives (aka binary cache) which are located by VCPKG_DEFAULT_BINARY_CACHE env var. | ||
# The other paths starting with '!' are exclusions: they contain termporary files generated during the build of the installed packages. | ||
path: | | ||
${{ env._VCPKG_ }} | ||
!${{ env._VCPKG_ }}/buildtrees | ||
!${{ env._VCPKG_ }}/packages | ||
!${{ env._VCPKG_ }}/downloads | ||
!${{ env._VCPKG_ }}/installed | ||
# The key is composed in a way that it gets properly invalidated whenever a different version of vcpkg is being used. | ||
key: | | ||
${{ hashFiles( '.git/modules/vcpkg/HEAD' )}} | ||
# On Windows runners, let's ensure to have the Developer Command Prompt environment setup correctly. | ||
# As used here the Developer Command Prompt created is targeting x64 and using the default the Windows SDK. | ||
- uses: ilammy/msvc-dev-cmd@v1 | ||
|
||
# Run CMake to generate Ninja project files, using the vcpkg's toolchain file to resolve and install | ||
# the dependencies as specified in vcpkg.json. Note that the vcpkg's toolchain is specified | ||
# in the CMakePresets.json file. | ||
# This step also runs vcpkg with Binary Caching leveraging GitHub Action cache to | ||
# store the built packages artifacts. | ||
- name: Restore from cache the dependencies and generate project files | ||
run: | | ||
cmake --preset ninja-multi-vcpkg | ||
# Build (Release configuration only) the whole project with Ninja (which is spawn by CMake). | ||
# | ||
# Note: if the preset misses the "configuration", it is possible to explicitly select the | ||
# configuration with the `--config` flag, e.g.: | ||
# run: cmake --build --preset ninja-vcpkg --config Release | ||
- name: Build (Release configuration) | ||
run: | | ||
cmake --build --preset ninja-vcpkg-release | ||
# Test the whole project with CTest, again Release configuration only. | ||
- name: Test (Release configuration) | ||
run: | | ||
ctest --preset test-release |
6 changes: 6 additions & 0 deletions
6
.github/workflows/properties/hosted-ninja-vcpkg_submod.properties.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "C++ template project with CMake, vcpkg, Ninja and with GitHub workflows", | ||
"description": "Build C++ code with CMake, Ninja and vcpkg.", | ||
"iconName": "cmake", | ||
"categories": ["CMake", "cpp", "cplusplus", "vcpkg", "Ninja"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Prerequisites | ||
*.d | ||
|
||
# Compiled Object files | ||
*.slo | ||
*.lo | ||
*.o | ||
*.obj | ||
|
||
# Precompiled Headers | ||
*.gch | ||
*.pch | ||
|
||
# Compiled Dynamic libraries | ||
*.so | ||
*.dylib | ||
*.dll | ||
|
||
# Fortran module files | ||
*.mod | ||
*.smod | ||
|
||
# Compiled Static libraries | ||
*.lai | ||
*.la | ||
*.a | ||
*.lib | ||
|
||
# Executables | ||
*.exe | ||
*.out | ||
*.app | ||
|
||
/vcpkg | ||
/builds | ||
.DS_Store | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "vcpkg"] | ||
path = vcpkg | ||
url = https://github.com/microsoft/vcpkg.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# CMakeLists.txt | ||
cmake_minimum_required(VERSION 3.0) | ||
project(cpp_template) | ||
|
||
# Find dependencies provided by vcpkg (via vcpkg.cmake) | ||
find_package(unofficial-sqlite3 CONFIG REQUIRED) | ||
find_package(fmt CONFIG REQUIRED) | ||
|
||
# main target | ||
add_executable(main) | ||
target_sources(main PRIVATE src/main.cpp) | ||
target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3 fmt::fmt) | ||
set_property(TARGET main PROPERTY CXX_STANDARD 20) | ||
|
||
# tests target | ||
add_executable(tests) | ||
target_sources(tests PRIVATE src/test.cpp) | ||
target_link_libraries(tests PRIVATE unofficial::sqlite3::sqlite3 fmt::fmt) | ||
set_property(TARGET tests PROPERTY CXX_STANDARD 20) | ||
enable_testing() | ||
add_test(tests tests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"$schema": "https://cmake.org/cmake/help/latest/_downloads/3e2d73bff478d88a7de0de736ba5e361/schema.json", | ||
"version": 8, | ||
"cmakeMinimumRequired": { | ||
"major": 3, | ||
"minor": 21, | ||
"patch": 0 | ||
}, | ||
"configurePresets": [ | ||
{ | ||
"name": "ninja-multi-vcpkg", | ||
"displayName": "Ninja Multi-Config", | ||
"description": "Configure with vcpkg toolchain and generate Ninja project files for all configurations", | ||
"binaryDir": "${sourceDir}/builds/${presetName}", | ||
"generator": "Ninja Multi-Config", | ||
"toolchainFile": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake" | ||
} | ||
], | ||
"buildPresets": [ | ||
{ | ||
"name": "ninja-vcpkg-debug", | ||
"configurePreset": "ninja-multi-vcpkg", | ||
"displayName": "Build (Debug)", | ||
"description": "Build with Ninja/vcpkg (Debug)", | ||
"configuration": "Debug" | ||
}, | ||
{ | ||
"name": "ninja-vcpkg-release", | ||
"configurePreset": "ninja-multi-vcpkg", | ||
"displayName": "Build (Release)", | ||
"description": "Build with Ninja/vcpkg (Release)", | ||
"configuration": "Release" | ||
}, | ||
{ | ||
"name": "ninja-vcpkg", | ||
"configurePreset": "ninja-multi-vcpkg", | ||
"displayName": "Build", | ||
"description": "Build with Ninja/vcpkg" | ||
} | ||
], | ||
"testPresets": [ | ||
{ | ||
"name": "test-ninja-vcpkg", | ||
"configurePreset": "ninja-multi-vcpkg", | ||
"hidden": true | ||
}, | ||
{ | ||
"name": "test-debug", | ||
"description": "Test (Debug)", | ||
"displayName": "Test (Debug)", | ||
"configuration": "Debug", | ||
"inherits": [ | ||
"test-ninja-vcpkg" | ||
] | ||
}, | ||
{ | ||
"name": "test-release", | ||
"description": "Test (Release)", | ||
"displayName": "Test (Release)", | ||
"configuration": "Release", | ||
"inherits": [ | ||
"test-ninja-vcpkg" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021,2022,2023 Luca Cappa | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[![hosted-ninja-vcpkg_submod-autocache](https://github.com/lukka/CppCMakeVcpkgTemplate/actions/workflows/hosted-ninja-vcpkg_submod.yml/badge.svg)](https://github.com/lukka/CppCMakeVcpkgTemplate/actions/workflows/hosted-ninja-vcpkg_submod.yml) | ||
[![hosted-pure-workflow](https://github.com/lukka/CppCMakeVcpkgTemplate/actions/workflows/hosted-pure-workflow.yml/badge.svg)](https://github.com/lukka/CppCMakeVcpkgTemplate/actions/workflows/hosted-pure-workflow.yml) | ||
|
||
# A C++ project template based on CMake and vcpkg | ||
|
||
## Content | ||
This repository contains a `C++` based project template that leverages [vcpkg](https://github.com/microsoft/vcpkg) and [CMake](https://www.cmake.org)'s [CMakePresets.json](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html) to build and test `C++` source code. | ||
|
||
Supports `Linux`/`macOS`/`Windows` on `x64` and `arm64` platforms. | ||
|
||
`vcpkg` is driven to use its [binary caching](https://learn.microsoft.com/en-us/vcpkg/users/binarycaching) feature storing the content in the GitHub Action cache, hence speeding up workflows by reusing previously built packages. | ||
|
||
## Key features: | ||
- `CMakePresets.json` allows to run the same build either _locally on your IDE_ and on _GitHub runners_. | ||
- `vcpkg` greatly helps in pulling and building the needed dependencies (e.g. libraries) which are cached for reuse on GitHub Action cache. | ||
|
||
## GitHub Action workflows | ||
|
||
The repository provides also two GitHub workflows to build the project on [GitHub runners](https://github.com/actions/runner). Both builds and tests the project using `vcpkg` and `CMake`, the only key difference is their implementation: | ||
|
||
- [hosted-pure-workflow.yml](.github/workflows/hosted-pure-workflow.yml): it is a __pure__ workflow which does not use unneeded GitHub Actions that cannot run locally on your development machine. On the other hand it is directly using the `CMake`, `Ninja`, `vcpkg` and the `C++ build` tools. | ||
- [hosted-ninja-vcpkg_submod.yml](.github/workflows/hosted-ninja-vcpkg_submod.yml): it is a concise workflow based on the custom GitHub Actions [get-cmake](https://github.com/lukka/get-cmake), [run-vcpkg](https://github.com/lukka/run-vcpkg) and [run-cmake](https://github.com/lukka/run-cmake) which simplify and shorten the workflow verbosity while adding some goodies like vcpkg binary caching stored on GH's cache and inline error annotations. | ||
|
||
## Rationale | ||
|
||
The main idea of this `C++` project template is to show how to obtain a _perfectly reproducible_ software development process that can be run anywhere without any difference and no surprises, either locally using your preferred tools/IDE, either remotely on build agents typically used for continuous integration. | ||
|
||
## Integrated Development Environment (IDE) Support | ||
|
||
The major `C++` IDEs should already support `CMakePresets.json` and require no particular configuration. | ||
|
||
For example [Visual Studio Code](https://code.visualstudio.com/) with the [CMake Tools extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools) let you to open the root folder of this repository, and select in the status bar the CMake preset (e.g. `ninja-multi-vcpkg`), as show in the following image: | ||
|
||
![CMake's preset selection in the status bar of Visual Studio Code](./img/vscode_cmakepresets_selection.png) | ||
|
||
<br> | ||
|
||
# License | ||
|
||
All the content in this repository is licensed under the [MIT License](LICENSE.txt). | ||
|
||
Copyright © 2021-2022-2023 Luca Cappa |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// main.cpp | ||
#include <sqlite3.h> | ||
#include <cstdio> | ||
#include <fmt/core.h> | ||
|
||
// main function! | ||
int main() | ||
{ | ||
fmt::print("Hello, world!\n"); | ||
printf("%s\n", sqlite3_libversion()); | ||
return 0; | ||
} |
Oops, something went wrong.