Skip to content

Commit a6a5b61

Browse files
authored
Merge pull request mpusz#724 from alex-tee/cmake_install_flag
build: add INSTALL option to cmake
2 parents c71fef5 + 1688d27 commit a6a5b61

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

docs/getting_started/installation_and_usage.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ dependencies by other means, some modifications to the library's CMake files mig
231231

232232
[cmake natural units]: https://github.com/mpusz/mp-units/releases/tag/v2.5.0
233233

234+
[`MP_UNITS_INSTALL`](#MP_UNITS_INSTALL){ #MP_UNITS_INSTALL }
235+
236+
: [:octicons-tag-24: 2.5.0][cmake install] · :octicons-milestone-24: `ON`/`OFF` (Default: `ON`)
237+
238+
Creates an installable target. Users may want to turn this off for example when consuming the
239+
library via CMake's `add_subdirectory` or similar mechanisms.
240+
241+
[cmake install]: https://github.com/mpusz/mp-units/releases/tag/v2.5.0
242+
234243

235244
## Installation and reuse
236245

@@ -493,6 +502,34 @@ with the following differences:
493502
[FAQ](faq.md#why-dont-we-have-cmake-options-to-disable-the-building-of-tests-and-examples).
494503
495504
505+
### CPM or FetchContent (CMake)
506+
507+
**mp-units** can be consumed via [CPM](https://github.com/cpm-cmake/CPM.cmake) or CMake's built-in
508+
[FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) mechanism. An example
509+
CPM configuration is shown below.
510+
511+
```cmake
512+
CPMFindPackage(
513+
NAME mp-units
514+
VERSION 2.4.0
515+
GIT_TAG 6e06eddf205deaf6c2f2f63500c8c70ec220a99f
516+
GITHUB_REPOSITORY mpusz/mp-units
517+
SOURCE_SUBDIR src
518+
SYSTEM YES
519+
EXCLUDE_FROM_ALL YES
520+
OPTIONS
521+
"MP_UNITS_BUILD_AS_SYSTEM_HEADERS ON"
522+
"MP_UNITS_INSTALL OFF"
523+
)
524+
# ...
525+
target_link_libraries(<your_target> <PUBLIC|PRIVATE|INTERFACE> mp-units::mp-units)
526+
```
527+
528+
!!! note
529+
530+
This is essentially a shortcut way to do what [Copy + CMake](#copy-cmake) mentions above, so the same
531+
prerequisites apply.
532+
496533
### Install
497534
498535
If you don't want to use Conan in your project and just want to install the **mp-units**

src/CMakeLists.txt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ check_libcxx_in_use(${projectPrefix}LIBCXX)
3939
# project build options
4040
option(${projectPrefix}BUILD_AS_SYSTEM_HEADERS "Export library as system headers" OFF)
4141
option(${projectPrefix}BUILD_CXX_MODULES "Add C++ modules to the list of default targets" OFF)
42+
option(${projectPrefix}INSTALL "Install the library" ON)
4243

4344
message(STATUS "${projectPrefix}BUILD_AS_SYSTEM_HEADERS: ${${projectPrefix}BUILD_AS_SYSTEM_HEADERS}")
4445
message(STATUS "${projectPrefix}BUILD_CXX_MODULES: ${${projectPrefix}BUILD_CXX_MODULES}")
46+
message(STATUS "${projectPrefix}INSTALL: ${${projectPrefix}INSTALL}")
4547

4648
if(${projectPrefix}BUILD_AS_SYSTEM_HEADERS)
4749
set(${projectPrefix}_AS_SYSTEM SYSTEM)
@@ -135,15 +137,17 @@ add_subdirectory(systems)
135137
# project-wide wrapper
136138
add_mp_units_module(mp-units mp-units DEPENDENCIES mp-units::core mp-units::systems MODULE_INTERFACE_UNIT mp-units.cpp)
137139

138-
# local build
139-
export(EXPORT mp-unitsTargets NAMESPACE mp-units::)
140-
configure_file("mp-unitsConfig.cmake" "." COPYONLY)
141-
include(CMakePackageConfigHelpers)
142-
write_basic_package_version_file(mp-unitsConfigVersion.cmake COMPATIBILITY SameMajorVersion)
140+
if(${projectPrefix}INSTALL)
141+
# local build
142+
export(EXPORT mp-unitsTargets NAMESPACE mp-units::)
143+
configure_file("mp-unitsConfig.cmake" "." COPYONLY)
144+
include(CMakePackageConfigHelpers)
145+
write_basic_package_version_file(mp-unitsConfigVersion.cmake COMPATIBILITY SameMajorVersion)
143146

144-
# installation
145-
install(EXPORT mp-unitsTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mp-units NAMESPACE mp-units::)
147+
# installation
148+
install(EXPORT mp-unitsTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mp-units NAMESPACE mp-units::)
146149

147-
install(FILES mp-unitsConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/mp-unitsConfigVersion.cmake
148-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mp-units
149-
)
150+
install(FILES mp-unitsConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/mp-unitsConfigVersion.cmake
151+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/mp-units
152+
)
153+
endif()

0 commit comments

Comments
 (0)