Skip to content

Commit 3647374

Browse files
codeinredalandefreitas
authored andcommitted
Add Presets for Installation
This commit adds presets for common use cases, including local and system installation. It also updates the README to explain the use of these presets and how they can be used for installation.
1 parent 8e1ca55 commit 3647374

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

CMakePresets.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 19,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "dev",
11+
"displayName": "Development Mode",
12+
"description": "Build with warnings and debug symbols",
13+
"binaryDir": "build",
14+
"generator": "Unix Makefiles",
15+
"cacheVariables": {
16+
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
17+
}
18+
},
19+
{
20+
"name": "system",
21+
"displayName": "Build for system installation",
22+
"description": "Bulid for system installation (BUILD_EXAMPLES and BUILD_TESTS are OFF)",
23+
"binaryDir": "${sourceDir}/build/system",
24+
"generator": "Unix Makefiles",
25+
"cacheVariables": {
26+
"BUILD_EXAMPLES": "OFF",
27+
"BUILD_TESTS": "OFF",
28+
"BUILD_SHARED_LIBS": "ON",
29+
"CMAKE_BUILD_TYPE": "Release",
30+
"CMAKE_INTERPROCEDURAL_OPTIMIZATION": "ON"
31+
}
32+
},
33+
{
34+
"name": "local",
35+
"displayName":"Build for installation in $HOME/.local",
36+
"description": "Build for installaton in $HOME/.local",
37+
"inherits": "system",
38+
"binaryDir": "${sourceDir}/build/local",
39+
"installDir": "$env{HOME}/.local"
40+
}
41+
],
42+
"buildPresets": [
43+
{
44+
"name": "dev",
45+
"configurePreset": "dev",
46+
"configuration": "RelWithDebInfo",
47+
"jobs": 8
48+
},
49+
{
50+
"name": "system",
51+
"configurePreset": "system",
52+
"configuration": "Release",
53+
"jobs": 8
54+
},
55+
{
56+
"name": "local",
57+
"configurePreset": "local",
58+
"configuration": "Release",
59+
"jobs": 8
60+
}
61+
]
62+
}

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,71 @@ Add this header to your source files:
210210

211211
However, in larger projects, it's always recommended to look for Matplot++ with `find_package` before including it as a subdirectory to avoid [ODR errors](https://en.wikipedia.org/wiki/One_Definition_Rule).
212212

213+
#### Install as a Package via CMake
214+
215+
If you have CMake 3.21 or greater, you can use the `system` build preset to
216+
build the package system-wide:
217+
218+
```bash
219+
cmake --preset=system
220+
cmake --build --preset=system
221+
sudo cmake --install build/system
222+
```
223+
224+
Alternatively, if the `CMAKE_PREFIX_PATH` environment variable is set to
225+
`$HOME/.local`, then you can install it locally. This can be set in `/etc/profile`
226+
or your shell config. This will not affect discovery of packages installed
227+
system-wide.
228+
229+
```bash
230+
export CMAKE_PREFIX_PATH="$HOME/.local"
231+
```
232+
233+
This has the advantage of not
234+
requiring sudo, and matplotplusplus will be installed in `$HOME/.local`.
235+
236+
```bash
237+
cmake --preset=local
238+
cmake --build --preset=local
239+
cmake --install build/local
240+
```
241+
242+
You can now use it from CMake with `find_package`:
243+
244+
```cmake
245+
find_package(Matplot++ REQUIRED)
246+
247+
target_link_libraries(<your target> Matplot++::matplot)
248+
```
249+
250+
If you're using a version of CMake too old to support presets, then building with
251+
the system preset is equivilant to:
252+
253+
```bash
254+
cmake -B build/system \
255+
-DBUILD_EXAMPLES=OFF \
256+
-DBUILD_SHARED_LIBS=ON \
257+
-DBUILD_TESTS=OFF \
258+
-CMAKE_BUILD_TYPE=Release \
259+
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON
260+
261+
cmake --build build/system
262+
```
263+
264+
While building with the local preset is equivilant to:
265+
266+
```bash
267+
cmake -B build/local \
268+
-DBUILD_EXAMPLES=OFF \
269+
-DBUILD_SHARED_LIBS=ON \
270+
-DBUILD_TESTS=OFF \
271+
-DCMAKE_BUILD_TYPE=Release \
272+
-DCMAKE_INSTALL_PREFIX="$HOME/.local" \
273+
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON
274+
275+
cmake --build build/local
276+
```
277+
213278
#### Embed with automatic download
214279

215280
`FetchContent` is a CMake command that can automatically download the Matplot++ repository. Check if you have [Cmake](http://cmake.org) 3.14+ installed:

0 commit comments

Comments
 (0)