Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ The consumer lifecycle is order-sensitive:
1. Load CPM and this toolkit, then call
`cpp_library_enable_dependency_tracking()` **before `project()`**. It registers
the provider through `CMAKE_PROJECT_TOP_LEVEL_INCLUDES`; CMake 3.24+ is required.
2. After `project()`, declare dependencies through `find_package()` or CPM.
Call `include(CTest)` at directory scope before `cpp_library_setup()` when
2. After `project()`, call `include(CTest)` at directory scope before `cpp_library_setup()` when
supplying `TESTS` or `EXAMPLES`. Setup requires `DESCRIPTION`, `NAMESPACE`,
and `HEADERS`.
and `HEADERS`. Declare dependencies through `find_package()` or CPM after
`cpp_library_setup()`.
3. Setup creates the library target and optional installation rules. Only
top-level projects proceed to template generation, tests, examples, and docs.
`BUILD_TESTING` gates both tests and examples; `BUILD_DOCS` gates documentation.
Expand Down Expand Up @@ -104,7 +104,8 @@ cmake --build --preset docs
```

The `clang-tidy` preset requires clang-tidy and runs analysis during compilation.
These presets come from `templates/CMakePresets.json` and apply to consumers.
These presets come from `templates/CMakePresets.json.in` and apply to consumers.
Source-build presets disable the resolved installation option; `install` enables it.
`cmake --preset init` enables `CPP_LIBRARY_FORCE_INIT` and overwrites generated
consumer templates.

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ jobs:
- name: Validate template files
run: |
# Check that all template files exist
test -f templates/CMakePresets.json
test -f templates/CMakePresets.json.in
test -f templates/Config.cmake.in
test -f templates/Doxyfile.in
test -f templates/custom.css
Expand Down
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Modern CMake template for C++ libraries with comprehensive infrastructure.
- **Testing**: Integrated [doctest](https://github.com/doctest/doctest) with CTest and compile-fail test support
- **Documentation**: [Doxygen](https://www.doxygen.nl/) with [doxygen-awesome-css](https://github.com/jothepro/doxygen-awesome-css) theme
- **Development Tools**: [clangd](https://clangd.llvm.org/) integration, CMakePresets.json, [clang-tidy](https://clang.llvm.org/extra/clang-tidy/) support
- **CI/CD**: [GitHub Actions](https://docs.github.com/en/actions) workflows with multi-platform testing and installation verification
- **CI/CD**: [GitHub Actions](https://docs.github.com/en/actions) workflows with multi-platform source builds and testing
- **Dependency Management**: [CPM.cmake](https://github.com/cpm-cmake/CPM.cmake) integration

## Quick reference
Expand All @@ -31,9 +31,12 @@ Use this order in your top-level `CMakeLists.txt`:
| --- | --- |
| 1 | Load CPM and cpp-library, as shown in [Manual Setup](#manual-setup). |
| 2 | Call [`cpp_library_enable_dependency_tracking()`](#cpp_library_enable_dependency_tracking) **before `project()`**. |
| 3 | Call `project(... LANGUAGES CXX)`, then declare dependencies with `CPMAddPackage()` or `find_package()`. |
| 3 | Call `project(... LANGUAGES CXX)`. |
| 4 | Call `include(CTest)` at directory scope to define `BUILD_TESTING`. |
| 5 | Call [`cpp_library_setup()`](#cpp_library_setup), using the [file-resolution conventions](#path-conventions). |
| 6 | Declare dependencies with `CPMAddPackage()` or `find_package()`, then link them to your library. |

Declare dependencies after `cpp_library_setup()`.

For custom target setup, see [`cpp_library_set_version()`](#cpp_library_set_version).
For non-namespaced dependency targets, see [dependency mapping](#non-namespaced-target-error).
Expand Down Expand Up @@ -223,7 +226,11 @@ against dependencies fetched from source in the same build is not supported.
Source consumption requires no special build/install-interface wrappers.
CMake checks installation exports during configuration/generation, before
`cmake --install` runs. For source-only builds without installed dependencies,
disable the component's installation option.
disable the component's installation option. The generated `default`, `test`,
`docs`, `clang-tidy`, and `init` presets do this automatically; the `install`
preset enables it. Presets use your package's installation-option name, including
a custom `INSTALL_OPTION`. Configuring without a preset preserves the
`PROJECT_IS_TOP_LEVEL` default.

If system-wide deployment or a package manager requires installation, use:

Expand All @@ -244,10 +251,18 @@ rather than falling back to source. It does not verify your project's installed
package config or dependency closure. See the
[CPM.cmake documentation](https://github.com/cpm-cmake/CPM.cmake#cpm_use_local_packages).

The generated CI package check verifies only that the root package's
`Config.cmake`, `ConfigVersion.cmake`, and `Targets.cmake` files exist and appear in
the install manifest. It does not load the package or check that its dependencies
are installed. Report installation failures if you encounter them.
Generated CI builds and tests from source; it does not test installation.
If you want installation testing, provide a separate workflow that prepares
dependencies and installs your package. No installation-specific changes to your
`CMakeLists.txt` are required. See [Test installation in your own workflow](docs/installation-testing.md)
for a cel-euclid example with GLM and the CEL packages.

cpp-library generates `cpp-library-test-package.cmake` in your build directory
for optional artifact checking. It checks that the root package's `Config.cmake`,
`ConfigVersion.cmake`, and `Targets.cmake` files exist and appear in the install
manifest. It does not load the package or check its dependency closure.
To update existing generated CI and presets, use the
[template regeneration procedure](#updating-cpp-library).

**Controlling installation**: The uppercase `${NAMESPACE}_INSTALL` option controls whether installation is enabled (defaults to `PROJECT_IS_TOP_LEVEL`). Use `-D${NAMESPACE}_INSTALL=ON/OFF` to override:

Expand Down
25 changes: 25 additions & 0 deletions cmake/cpp-library-install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,29 @@ function(_cpp_library_deferred_generate_config TARGET_NAME)
message(STATUS "cpp-library: Generated ${ARG_PACKAGE_NAME}Config.cmake with dependencies")
endfunction()

# Rejects setup after a descendant project using the same installation switch.
# Repeated setup in one project is allowed; ancestor marking takes O(directory depth).
function(_cpp_library_check_install_order INSTALL_OPTION)
set(property "_CPP_LIBRARY_INSTALL_ORDER_${INSTALL_OPTION}")
get_property(already_setup DIRECTORY "${PROJECT_SOURCE_DIR}" PROPERTY "${property}_SETUP")
if(already_setup)
return()
endif()
get_property(descendant DIRECTORY "${PROJECT_SOURCE_DIR}" PROPERTY "${property}_DESCENDANT")
if(NOT "${descendant}" STREQUAL "")
message(FATAL_ERROR
"cpp-library: ${descendant} called cpp_library_setup() before ${PROJECT_NAME}; "
"both use ${INSTALL_OPTION}. "
"Call cpp_library_setup() before loading dependencies in ${PROJECT_NAME}.")
endif()
set_property(DIRECTORY "${PROJECT_SOURCE_DIR}" PROPERTY "${property}_SETUP" TRUE)
get_directory_property(parent DIRECTORY "${PROJECT_SOURCE_DIR}" PARENT_DIRECTORY)
while(parent)
set_property(DIRECTORY "${parent}" PROPERTY "${property}_DESCENDANT" "${PROJECT_NAME}")
get_directory_property(parent DIRECTORY "${parent}" PARENT_DIRECTORY)
endwhile()
endfunction()

# Configures CMake install rules for library target and package config files.
# - Precondition: NAME, PACKAGE_NAME, VERSION, and NAMESPACE specified; target NAME exists
# - Postcondition: install rules created for target, config files, and export with NAMESPACE:: prefix
Expand Down Expand Up @@ -489,6 +512,8 @@ function(_cpp_library_setup_install)
string(TOUPPER "${ARG_NAMESPACE}" NAMESPACE_UPPER)
set(install_option "${NAMESPACE_UPPER}_INSTALL")
endif()
_cpp_library_check_install_order("${install_option}")
set_property(TARGET "${ARG_NAME}" PROPERTY _CPP_LIBRARY_INSTALL_OPTION "${install_option}")
option(${install_option} "Enable installation of ${ARG_PACKAGE_NAME}" ${PROJECT_IS_TOP_LEVEL})

# Check if installation is enabled
Expand Down
18 changes: 14 additions & 4 deletions cmake/cpp-library-setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,16 @@ function(_cpp_library_setup_core)

endfunction()

# Copies template files (.clang-format, .gitignore, etc.) to project root if not present.
# - Precondition: PACKAGE_NAME must be passed as first parameter
# Copies static templates and configures project-specific presets and CI if missing.
# - Precondition: PACKAGE_NAME and INSTALL_OPTION identify the current project's package and switch
# - Postcondition: missing template files copied to project, CI workflow configured with PACKAGE_NAME substitution
# - With FORCE_INIT: overwrites existing files
function(_cpp_library_copy_templates PACKAGE_NAME)
set(options FORCE_INIT)
cmake_parse_arguments(ARG "${options}" "" "" ${ARGN})
cmake_parse_arguments(ARG "${options}" "INSTALL_OPTION" "" ${ARGN})
if(NOT ARG_INSTALL_OPTION)
message(FATAL_ERROR "_cpp_library_copy_templates: INSTALL_OPTION is required")
endif()

# List of static template files to copy
set(TEMPLATE_FILES
Expand All @@ -165,7 +168,6 @@ function(_cpp_library_copy_templates PACKAGE_NAME)
".vscode/settings.json"
".vscode/tasks.json"
"docs/index.html"
"CMakePresets.json"
)

foreach(template_file IN LISTS TEMPLATE_FILES)
Expand All @@ -181,6 +183,14 @@ function(_cpp_library_copy_templates PACKAGE_NAME)
message(WARNING "Template file not found: ${source_file}")
endif()
endforeach()

set(preset_dest "${CMAKE_CURRENT_SOURCE_DIR}/CMakePresets.json")
if(NOT EXISTS "${preset_dest}" OR ARG_FORCE_INIT)
set(CPP_LIBRARY_INSTALL_OPTION "${ARG_INSTALL_OPTION}")
configure_file("${CPP_LIBRARY_ROOT}/templates/CMakePresets.json.in"
"${preset_dest}" @ONLY)
message(STATUS "Configured template file: CMakePresets.json")
endif()

# Setup CI workflow with PACKAGE_NAME substitution
_cpp_library_setup_ci("${PACKAGE_NAME}" ${ARG_FORCE_INIT})
Expand Down
7 changes: 4 additions & 3 deletions cpp-library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,12 @@ function(cpp_library_setup)

_cpp_library_setup_package_test("${PACKAGE_NAME}")

# Copy static template files (like .clang-format, .gitignore, CMakePresets.json, etc.)
# Copy static templates and configure project-specific presets and CI.
get_target_property(install_option "${ARG_NAME}" _CPP_LIBRARY_INSTALL_OPTION)
if(DEFINED CPP_LIBRARY_FORCE_INIT AND CPP_LIBRARY_FORCE_INIT)
_cpp_library_copy_templates("${PACKAGE_NAME}" FORCE_INIT)
_cpp_library_copy_templates("${PACKAGE_NAME}" INSTALL_OPTION "${install_option}" FORCE_INIT)
else()
_cpp_library_copy_templates("${PACKAGE_NAME}")
_cpp_library_copy_templates("${PACKAGE_NAME}" INSTALL_OPTION "${install_option}")
endif()

# Download doctest if we'll need it for tests or examples
Expand Down
83 changes: 83 additions & 0 deletions docs/examples/cel-euclid-install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copy into cel-euclid's .github/workflows/install.yml.
name: Test installation

on:
workflow_dispatch:

permissions:
contents: read

jobs:
install:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
env:
INSTALL_PREFIX: ${{ runner.temp }}/cel-install
CMAKE_BUILD_PARALLEL_LEVEL: 2

steps:
# [DEPENDENCY] https://github.com/actions/checkout/releases
- uses: actions/checkout@v7
with:
fetch-depth: 0

- name: Check out GLM 1.0.3
uses: actions/checkout@v7
with:
repository: g-truc/glm
ref: 8d1fd52e5ab5590e2c81768ace50c72bae28f2ed
path: .install-deps/glm

- name: Check out cel-runtime 1.1.2
uses: actions/checkout@v7
with:
repository: stlab/cel-runtime
ref: a819042cfa3fab8ede82052518f2b7f5175af5ad
path: .install-deps/cel-runtime
fetch-depth: 0

- name: Check out cel-parser 1.1.1
uses: actions/checkout@v7
with:
repository: stlab/cel-parser
ref: 3ba7e1990551817427872b9dc88d7ebed32b9cf8
path: .install-deps/cel-parser
fetch-depth: 0

- name: Install GLM
run: |
cmake -S .install-deps/glm -B "$RUNNER_TEMP/build-glm" -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DGLM_BUILD_TESTS=OFF -DGLM_BUILD_INSTALL=ON \
"-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX"
cmake --build "$RUNNER_TEMP/build-glm"
cmake --install "$RUNNER_TEMP/build-glm"

- name: Install CEL dependencies
run: |
for package in cel-runtime cel-parser; do
cmake -S ".install-deps/$package" \
-B "$RUNNER_TEMP/build-$package" -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_TESTING=OFF -DCEL_INSTALL=ON \
-DCPM_USE_LOCAL_PACKAGES=ON \
"-DCMAKE_PREFIX_PATH=$INSTALL_PREFIX" \
"-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX"
cmake --build "$RUNNER_TEMP/build-$package"
cmake --install "$RUNNER_TEMP/build-$package"
done

- name: Build and install cel-euclid
run: |
cmake --preset install -DCEL_INSTALL=ON \
"-DCMAKE_PREFIX_PATH=$INSTALL_PREFIX" \
"-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX"
cmake --build --preset install
cmake --install build/install

- name: Check installed package artifacts
run: |
cmake "-DPACKAGE_TEST_PREFIX=$INSTALL_PREFIX" \
-P build/install/cpp-library-test-package.cmake
59 changes: 59 additions & 0 deletions docs/installation-testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Test installation in your own workflow

If you maintain a library that needs installation testing, provide a separate
GitHub Actions workflow. Generated cpp-library CI tests source builds, not
installation. You do not need installation-specific dependency declarations
in your library's `CMakeLists.txt`.

## Before you begin

Declare dependencies after `cpp_library_setup()`. Generate your consumer
presets and CI with cpp-library, and use ordinary `target_link_libraries()`
calls for dependencies.

Identify every required dependency, its version, and how to install it
independently. Source locations alone do not describe installation recipes.
Dependencies must be available as installed packages before you configure
your library for installation.

## Add a workflow

Copy the [cel-euclid installation workflow](examples/cel-euclid-install.yml) to
`.github/workflows/install.yml` in cel-euclid. For another library, replace the
dependency checkouts, their installation commands, and the installation-option
name with those for your project.

The example runs on manual dispatch and performs these steps:

1. Check out the client and pinned dependency sources. Fetch tags for CEL
packages so cpp-library discovers their release versions.
2. Build and install GLM 1.0.3 in its own build with `GLM_BUILD_INSTALL=ON`.
3. Build and install cel-runtime 1.1.2, then cel-parser 1.1.1, in separate builds.
4. Configure cel-euclid with its `install` preset and the installation prefix
in `CMAKE_PREFIX_PATH`, then build and install it.
5. Run the existing cpp-library package-artifact check.

The GLM-specific setting belongs in GLM's installation command, not the
client's `CPMAddPackage()` declaration. The workflow accounts for cel-runtime
and cel-parser as well as GLM: all required packages must be installed even
when only GLM is maintained outside the CEL project.

Run the workflow from your repository's **Actions** tab. During configuration,
confirm that CPM reports local packages for the installed dependencies rather
than fetching them into the client's build. If it fetches one, check the
installed version and `CMAKE_PREFIX_PATH`.

`CPM_USE_LOCAL_PACKAGES=ON` prefers installed packages; it does not prohibit
source fallback. The workflow still fetches cpp-library itself through CPM.
Do not set `CPM_LOCAL_PACKAGES_ONLY` globally unless you also arrange for that
toolkit dependency to be satisfied.

## Check the result

The final step prints `Installed cel-euclid artifacts found` with the package
directory. A missing package artifact or an artifact absent from the install
manifest fails the check.

This verifies the root package's installation artifacts only. It does not
compile an installed consumer or guarantee that downstream dependency
resolution succeeds.
21 changes: 0 additions & 21 deletions templates/.github/workflows/ci.yml.in
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,6 @@ jobs:
- name: Test
run: ctest --preset=test

- name: Build and Install
run: |
cmake --preset=default
cmake --build --preset=default
cmake --install build/default --prefix "${{ runner.temp }}/install"
env:
CC: ${{ matrix.cc }}
CXX: ${{ matrix.cxx }}
if: ${{ matrix.cc }}

- name: Build and Install
run: |
cmake --preset=default
cmake --build --preset=default
cmake --install build/default --prefix "${{ runner.temp }}/install"
if: ${{ !matrix.cc }}

- name: Check installed package artifacts
run: |
cmake "-DPACKAGE_TEST_PREFIX=${{ runner.temp }}/install" -P build/default/cpp-library-test-package.cmake

clang-tidy:
runs-on: ubuntu-latest

Expand Down
Loading
Loading