Skip to content

Commit

Permalink
Enable installation of the library via cmake.
Browse files Browse the repository at this point in the history
See #8.
  • Loading branch information
Sedeniono committed Dec 12, 2024
1 parent 165eb47 commit c714ae9
Showing 3 changed files with 107 additions and 1 deletion.
59 changes: 59 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
cmake_minimum_required(VERSION 3.14)

project(
tiny-optional
VERSION 1.3.1
DESCRIPTION "Replacement for std::optional that does not waste memory unnecessarily."
HOMEPAGE_URL "https://github.com/Sedeniono/tiny-optional"
LANGUAGES CXX
)

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

add_library(tiny-optional INTERFACE)
add_library(tiny-optional::tiny-optional ALIAS tiny-optional)

if (NOT PROJECT_IS_TOP_LEVEL)
set(warning_guard SYSTEM)
endif()

target_include_directories(
tiny-optional ${warning_guard}
INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

target_compile_features(tiny-optional INTERFACE cxx_std_17)

install(
DIRECTORY include/
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")

install(
TARGETS tiny-optional
EXPORT tiny-optionalTargets
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")

write_basic_package_version_file(
"tiny-optionalConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
# Different versions of tiny::optional cannot be mixed. It is
# actively prevented by the use of inline namespaces.
COMPATIBILITY ExactVersion)

configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/tiny-optionalConfig.cmake.in"
"${PROJECT_BINARY_DIR}/tiny-optionalConfig.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/tiny-optional")

install(
EXPORT tiny-optionalTargets
FILE tiny-optionalTargets.cmake
NAMESPACE tiny-optional::
DESTINATION "${CMAKE_INSTALL_DATADIR}/tiny-optional")

install(
FILES "${PROJECT_BINARY_DIR}/tiny-optionalConfig.cmake"
"${PROJECT_BINARY_DIR}/tiny-optionalConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_DATADIR}/tiny-optional")
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -18,6 +18,9 @@
- [Compatibility with `std::optional`](#compatibility-with-stdoptional)
- [Usage](#usage)
- [Installation](#installation)
- [cmake](#cmake)
- [Manually](#manually)
- [Preprocessor flags](#preprocessor-flags)
- [Using `tiny::optional` as `std::optional` replacement](#using-tinyoptional-as-stdoptional-replacement)
- [Using a sentinel value](#using-a-sentinel-value)
- [Storing the empty state in a member variable](#storing-the-empty-state-in-a-member-variable)
@@ -160,9 +163,49 @@ Moreover, the monadic operation `transform()` always returns a `tiny::optional<T
# Usage
## Installation
### cmake
Via [`find_package`](https://cmake.org/cmake/help/latest/command/find_package.html) (recommended):
1. Clone or download the `tiny-optional` repository.
1. In its root directory, execute
```sh
cmake -B build
sudo cmake --install build
```
to generate the files in the folder `build` and then install them to the default location (e.g. `/usr/local/` on linux, `C:\Program Files` on Windows).
Alternatively, to install into some custom directory:
```sh
cmake -B build -DCMAKE_INSTALL_PREFIX="/path/to/custom/tiny/optional/install/dir"
cmake --install build
```
1. In your own project's CMakeLists.txt, add:
```cmake
find_package(tiny-optional CONFIG REQUIRED)
target_link_libraries(<target> PRIVATE tiny-optional::tiny-optional)
```
where `<target>` is the cmake target that uses the library.
If you had installed the library in a custom location, you need tell cmake about it, e.g. via [`CMAKE_PREFIX_PATH`](https://cmake.org/cmake/help/latest/variable/CMAKE_PREFIX_PATH.html) (for example, run cmake as `cmake <path> -DCMAKE_PREFIX_PATH="/path/to/custom/tiny/optional/install/dir"`).


Alternative via [`add_subdirectory`](https://cmake.org/cmake/help/latest/command/add_subdirectory.html):
1. Put all files of the `tiny-optional` library in a subdirectory of your own project, so that you end up with `own_project/tiny-optional/CMakeLists.txt` and `own_project/tiny-optional/include/`.
1. In your own project's `own_project/CMakeLists.txt`, add:
```cmake
add_subdirectory(tiny-optional)
target_link_libraries(<target> PRIVATE tiny-optional::tiny-optional)
```


### Manually
This is a header-only library. Just copy the folder from the include directory containing the header to your project. Include it via `#include <tiny/optional>`.

The library uses the standard [`assert()` macro](https://en.cppreference.com/w/cpp/error/assert) in a few places, which can be disabled as usual by defining `NDEBUG` for release builds.

### Preprocessor flags
* The library uses the standard [`assert()` macro](https://en.cppreference.com/w/cpp/error/assert) in a few places, which can be disabled as usual by defining `NDEBUG` for release builds.
* If you like to disable the use of platform specific tricks at the cost of most of the features, see the chapter "[Disabling platform specific tricks (`TINY_OPTIONAL_USE_SEPARATE_BOOL_INSTEAD_OF_UB_TRICKS`)](#disabling-platform-specific-tricks-tiny_optional_use_separate_bool_instead_of_ub_tricks)".




## Using `tiny::optional` as `std::optional` replacement
4 changes: 4 additions & 0 deletions cmake/tiny-optionalConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components("@PROJECT_NAME@")

0 comments on commit c714ae9

Please sign in to comment.