diff --git a/.github/workflows/hosted-ninja-vcpkg_submod.yml b/.github/workflows/hosted-ninja-vcpkg_submod.yml index fa62b7f..a3edbf4 100644 --- a/.github/workflows/hosted-ninja-vcpkg_submod.yml +++ b/.github/workflows/hosted-ninja-vcpkg_submod.yml @@ -1,21 +1,24 @@ -# Copyright (c) 2021, 2022 Luca Cappa +# Copyright (c) 2021-2022-2023 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 x86 and ARM. +# The workflow runs on x64 and ARM platforms. # Workflow steps: -# - Restores vcpkg artifacts from cache, or setup vcpkg once and cache it on the GitHub cloud based cache. +# - 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. It will be a no-op if those are restored from cache. +# 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: workflow_dispatch: schedule: - - cron: '0 1 * * FRI' + - cron: '0 1 * * *' jobs: job: @@ -25,14 +28,14 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest, buildjet-2vcpu-ubuntu-2204-arm] - - # env: + #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 }} + # VCPKG_DEFAULT_TRIPLET: ${{ matrix.triplet }} steps: - uses: actions/checkout@v3 @@ -41,20 +44,16 @@ jobs: - uses: lukka/get-cmake@latest - - name: Restore artifacts, or setup vcpkg for building artifacts - uses: lukka/run-vcpkg@v10 - id: runvcpkg + - name: Restore from cache and setup vcpkg executable and data files. + uses: lukka/run-vcpkg@v11 with: vcpkgJsonGlob: 'vcpkg.json' - - name: Run CMake+vcpkg+Ninja+CTest to generate/build/test. + - name: Run CMake+vcpkg+Ninja+CTest to build packages and generate/build/test the code. uses: lukka/run-cmake@v10 - id: runcmake with: configurePreset: 'ninja-multiconfiguration-vcpkg' buildPreset: 'ninja-multiconfiguration-vcpkg' buildPresetAdditionalArgs: "[`--config`, `Release`]" testPreset: 'ninja-multiconfiguration-vcpkg' testPresetAdditionalArgs: "[`--config`, `Release`]" - env: - VCPKG_FORCE_SYSTEM_BINARIES: 1 diff --git a/.github/workflows/hosted-pure-workflow.yml b/.github/workflows/hosted-pure-workflow.yml index 3a1dd23..f84f0ce 100644 --- a/.github/workflows/hosted-pure-workflow.yml +++ b/.github/workflows/hosted-pure-workflow.yml @@ -1,20 +1,20 @@ -# Copyright (c) 2021, 2022 Luca Cappa +# Copyright (c) 2021-2022-2023 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 to have consistent build locallly -# and on continuous integration servers (aka build agents). +# 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 +# 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 build locally (on your development machine) and remotely (on build agents). +# and reproducible local build (on your development machine) and a remote build on +# build agents. name: hosted-pure-workflow on: push: workflow_dispatch: schedule: - - cron: '0 1 * * FRI' + - cron: '0 1 * * *' jobs: job: @@ -36,8 +36,18 @@ jobs: VCPKG_ROOT: ${{ 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@v6 + 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@v3 with: submodules: true @@ -48,8 +58,9 @@ jobs: # 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 both vcpkg and its artifacts from the GitHub cache service. - - name: Restore vcpkg and its artifacts. + # 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@v3 with: # The first path is the location of vcpkg: it contains the vcpkg executable and data files, as long as the @@ -61,18 +72,20 @@ jobs: !${{ env.VCPKG_ROOT }}/packages !${{ env.VCPKG_ROOT }}/downloads !${{ env.VCPKG_ROOT }}/installed - # The key is composed in a way that it gets properly invalidated: this must happen whenever vcpkg's Git commit id changes, or the list of packages changes. In this case a cache miss must happen and a new entry with a new key with be pushed to GitHub the cache service. - # The key includes: hash of the vcpkg.json file, the hash of the vcpkg Git commit id, and the used vcpkg's triplet. The vcpkg's commit id would suffice, but computing an hash out it does not harm. - # Note: given a key, the cache content is immutable. If a cache entry has been created improperly, in order the recreate the right content the key must be changed as well, and it must be brand new (i.e. not existing already). + # The key is composed in a way that it gets properly invalidated whenever a different version of vcpkg is being used. key: | - ${{ hashFiles( 'vcpkg.json' ) }}-${{ hashFiles( '.git/modules/vcpkg/HEAD' )}}-${{ matrix.triplet }} + ${{ hashFiles( '.git/modules/vcpkg/HEAD' )}} - # On Windows runners, let's ensure to have the Developer Command Prompt environment setup correctly. + # 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. - - name: Install dependencies and generate project files + # 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-multiconfiguration-vcpkg diff --git a/.gitignore b/.gitignore index 2d4060f..1e810c3 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ /vcpkg /builds .DS_Store +/build \ No newline at end of file diff --git a/LICENSE b/LICENSE index 137c59b..a3bcd2a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021, 2022 Luca Cappa +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 diff --git a/README.md b/README.md index 4182038..cfc983d 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,25 @@ [![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 +# 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 the source code. +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`. +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). + - `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 unneded 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 content while adding some goodies like inline error annotations, automatic caching, cache miss fallback strategy and other benefits. + - [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 @@ -29,4 +31,12 @@ The major `C++` IDEs should already support `CMakePresets.json` and require no p 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-multiconfiguration-vcpkg`), as show in the following image: -![CMake's preset selection in the status bar of Visual Studio Code](./img/vscode_cmakepresets_selection.png) \ No newline at end of file +![CMake's preset selection in the status bar of Visual Studio Code](./img/vscode_cmakepresets_selection.png) + +
+ +# License + +All the content in this repository is licensed under the [MIT License](LICENSE.txt). + +Copyright © 2021-2022-2023 Luca Cappa diff --git a/vcpkg b/vcpkg index 6f7ffeb..c9f9065 160000 --- a/vcpkg +++ b/vcpkg @@ -1 +1 @@ -Subproject commit 6f7ffeb18f99796233b958aaaf14ec7bd4fb64b2 +Subproject commit c9f906558f9bb12ee9811d6edc98ec9255c6cda5