Skip to content

Commit c77917f

Browse files
committed
Enable install in non-top-level projects via NAMESPACE_INSTALL
Installation is now enabled if either building as a top-level project or if the `${NAMESPACE}_INSTALL` variable is set to ON. Updated documentation to explain how to enable installation in non-top-level projects, improving support for package managers and advanced workflows.
1 parent e95b1b3 commit c77917f

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ cmake --install build/install --prefix /opt/mylib
171171
```
172172
The `install` preset enables `CPM_USE_LOCAL_PACKAGES`, which verifies your generated Config.cmake works correctly. See the [CPM.cmake documentation](https://github.com/cpm-cmake/CPM.cmake#cpm_use_local_packages) for more about using installed packages.
173173

174+
By default, installation is enabled only when building as a top-level project. To enable installation in non-top-level projects (e.g., when using CPM with package managers), set `${NAMESPACE}_INSTALL=ON`:
175+
176+
```bash
177+
cmake -DSTLAB_INSTALL=ON -B build # For a library with NAMESPACE stlab
178+
```
179+
174180
**Re-exporting CPM dependencies:** When re-exporting dependencies from `CPMAddPackage`, wrap them in `BUILD_INTERFACE` to avoid export errors (CPM creates non-IMPORTED targets that can't be exported):
175181

176182
```cmake
@@ -333,6 +339,7 @@ cpp_library_setup(
333339
- The project name is automatically taken from `PROJECT_NAME` (set by the `project()` command). You must call `project(your-library)` before `cpp_library_setup()`.
334340
- **If you specify `TESTS` or `EXAMPLES`**, call `include(CTest)` after `project()` and before `cpp_library_setup()`.
335341
- Version is automatically detected from git tags (see [Version Management](#version-management) for overrides).
342+
- Installation is enabled by default when `PROJECT_IS_TOP_LEVEL=TRUE`. Set `${NAMESPACE}_INSTALL=ON` to enable installation in non-top-level projects.
336343

337344
### Target Naming
338345

cmake/cpp-library-setup.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ function(_cpp_library_setup_core)
119119
endif()
120120
endif()
121121

122-
# Setup installation when building as top-level project
123-
if(ARG_TOP_LEVEL)
122+
# Setup installation when building as top-level project OR explicitly requested
123+
# This allows package managers (vcpkg, CPM with install intent) to enable installation
124+
# even when PROJECT_IS_TOP_LEVEL is FALSE
125+
if(ARG_TOP_LEVEL OR ${ARG_NAMESPACE}_INSTALL)
124126
_cpp_library_setup_install(
125127
NAME "${ARG_NAME}"
126128
PACKAGE_NAME "${ARG_PACKAGE_NAME}"

0 commit comments

Comments
 (0)