@@ -19,46 +19,20 @@ Modern CMake template for C++ header-only libraries with common infrastructure.
1919
2020## Usage
2121
22- Include in your project similar to how CPM.cmake is included:
23-
24- ### Option 1: Download and Cache (Recommended)
25-
26- Create ` cmake/cpp-library.cmake ` in your project:
27-
28- ``` cmake
29- # SPDX-License-Identifier: BSL-1.0
30-
31- set(CPP_LIBRARY_VERSION 1.0.0)
32- set(CPP_LIBRARY_HASH_SUM "...") # SHA256 of the release
33-
34- if(CPP_LIBRARY_CACHE)
35- set(CPP_LIBRARY_LOCATION "${CPP_LIBRARY_CACHE}/cpp-library/cpp-library_${CPP_LIBRARY_VERSION}.cmake")
36- elseif(DEFINED ENV{CPP_LIBRARY_CACHE})
37- set(CPP_LIBRARY_LOCATION "$ENV{CPP_LIBRARY_CACHE}/cpp-library/cpp-library_${CPP_LIBRARY_VERSION}.cmake")
38- else()
39- set(CPP_LIBRARY_LOCATION "${CMAKE_BINARY_DIR}/cmake/cpp-library_${CPP_LIBRARY_VERSION}.cmake")
40- endif()
41-
42- get_filename_component(CPP_LIBRARY_LOCATION ${CPP_LIBRARY_LOCATION} ABSOLUTE)
43-
44- file(DOWNLOAD
45- https://github.com/stlab/cpp-library/releases/download/v${CPP_LIBRARY_VERSION}/cpp-library.cmake
46- ${CPP_LIBRARY_LOCATION} EXPECTED_HASH SHA256=${CPP_LIBRARY_HASH_SUM}
47- )
48-
49- include(${CPP_LIBRARY_LOCATION})
50- ```
51-
52- ### Then in your CMakeLists.txt:
22+ Use CPMAddPackage to fetch cpp-library directly in your CMakeLists.txt:
5323
5424``` cmake
5525cmake_minimum_required(VERSION 3.20)
5626project(your-library VERSION 1.0.0 DESCRIPTION "Your library description" LANGUAGES CXX)
5727
5828# Only setup full infrastructure when building as top-level project
5929if(PROJECT_IS_TOP_LEVEL)
60- set(CPP_LIBRARY_CACHE ${CMAKE_SOURCE_DIR}/.cpp-library-cache CACHE PATH "Directory to cache cpp-library packages" FORCE)
61- include(cmake/cpp-library.cmake)
30+ set(CPM_SOURCE_CACHE ${CMAKE_SOURCE_DIR}/.cpm-cache CACHE PATH "CPM cache")
31+ include(cmake/CPM.cmake)
32+
33+ # Fetch cpp-library via CPM
34+ CPMAddPackage("gh:stlab/[email protected] ") 35+ include(${cpp-library_SOURCE_DIR}/cpp-library.cmake)
6236
6337 cpp_library_setup(
6438 NAME your-library
@@ -81,6 +55,12 @@ else()
8155endif()
8256```
8357
58+ ### Prerequisites
59+
60+ - ** CPM.cmake** : Must be included before using cpp-library
61+ - ** CMake 3.20+** : Required for modern CMake features
62+ - ** C++17+** : Default requirement (configurable)
63+
8464## API Reference
8565
8666### ` cpp_library_setup `
@@ -117,27 +97,65 @@ cpp_library_setup(
11797- ** Testing** : doctest integration with CTest
11898- ** Documentation** : Doxygen with doxygen-awesome-css theme
11999- ** Development** : clangd compile_commands.json symlink
100+ - ** Compile-fail tests** : Automatic detection for examples with ` _fail ` suffix
120101
121102### Smart Defaults
122103
123104- ** C++17** standard requirement (configurable)
124105- ** Ninja** generator in presets
125106- ** Debug** builds for testing, ** Release** for default
126107- ** Build isolation** with separate build directories
108+ - ** Two-mode operation** : Full infrastructure when top-level, lightweight when consumed
127109
128110### Dependency Management
129111
130- - ** CPM.cmake** for dependency management
131- - ** Caching ** to avoid re-downloading dependencies
112+ - ** CPM.cmake** integration for seamless fetching
113+ - ** Automatic caching ** via CPM's built-in mechanisms
132114- ** [email protected] ** for testing
133115- ** [email protected] ** for documentation
116+ - ** Git tag versioning** for reliable updates
134117
135118## Example Projects
136119
137120This template is used by:
138121
139- - [ stlab/enum-ops] ( https://github.com/stlab/enum-ops )
140- - [ stlab/copy-on-write] ( https://github.com/stlab/copy-on-write )
122+ - [ stlab/enum-ops] ( https://github.com/stlab/enum-ops ) - Type-safe operators for enums
123+ - [ stlab/copy-on-write] ( https://github.com/stlab/copy-on-write ) - Copy-on-write wrapper
124+
125+ ### Real Usage Example (enum-ops)
126+
127+ ``` cmake
128+ cmake_minimum_required(VERSION 3.20)
129+ project(stlab-enum-ops VERSION 1.0.0 DESCRIPTION "Type-safe operators for enums" LANGUAGES CXX)
130+
131+ if(PROJECT_IS_TOP_LEVEL)
132+ set(CPM_SOURCE_CACHE ${CMAKE_SOURCE_DIR}/.cpm-cache CACHE PATH "CPM cache")
133+ include(cmake/CPM.cmake)
134+
135+ CPMAddPackage("gh:stlab/[email protected] ") 136+ include(${cpp-library_SOURCE_DIR}/cpp-library.cmake)
137+
138+ cpp_library_setup(
139+ NAME stlab-enum-ops
140+ VERSION ${PROJECT_VERSION}
141+ DESCRIPTION "${PROJECT_DESCRIPTION}"
142+ NAMESPACE stlab
143+ HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/stlab/enum_ops.hpp
144+ EXAMPLES enum_ops_example enum_ops_example_fail
145+ TESTS enum_ops_all_tests
146+ DOCS_EXCLUDE_SYMBOLS "stlab::implementation"
147+ )
148+ else()
149+ add_library(stlab-enum-ops INTERFACE)
150+ add_library(stlab::enum-ops ALIAS stlab-enum-ops)
151+ target_include_directories(stlab-enum-ops INTERFACE
152+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
153+ $<INSTALL_INTERFACE:include>)
154+ target_compile_features(stlab-enum-ops INTERFACE cxx_std_17)
155+ endif()
156+ ```
157+
158+ ** Result** : 76 lines of CMake boilerplate reduced to 24 lines (68% reduction)!
141159
142160## License
143161
0 commit comments