Skip to content

Commit 929779b

Browse files
authored
Merge pull request #10 from soblin/feature/cmake-installer
Feature/cmake installer
2 parents 811479c + 9f14078 commit 929779b

File tree

6 files changed

+133
-19
lines changed

6 files changed

+133
-19
lines changed

CMakeLists.txt

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1-
project(matplotlibcpp17)
21
cmake_minimum_required(VERSION 3.12)
32

4-
find_package(Python3 COMPONENTS Interpreter Development NumPy)
3+
project(
4+
"matplotlibcpp17"
5+
VERSION 0.0.0
6+
DESCRIPTION "A C++ header-only plotting library based on pybind11 and matplotlib, featuring more flexibility than matplotlibcpp"
7+
HOMEPAGE_URL "https://soblin.github.io/matplotlibcpp17/"
8+
)
9+
10+
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
511
find_package(pybind11 REQUIRED)
612

7-
if(NOT DEFINED ADD_DEMO)
8-
set(ADD_DEMO ON)
9-
endif()
13+
set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
1014

15+
# gallery
1116
if(NOT DEFINED USE_GUI)
12-
set(USE_GUI 0)
17+
set(USE_GUI 1)
18+
endif()
19+
if(NOT DEFINED ADD_DEMO)
20+
set(ADD_DEMO 1)
1321
endif()
14-
15-
set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
1622

1723
function(add_demo name path)
1824
add_executable(${name} ${path})
1925
target_include_directories(${name} PUBLIC
2026
${Python3_INCLUDE_DIRS}
21-
${Python3_NumPy_INCLUDE_DIRS}
22-
${pybind11_INCLUDE_DIR}
2327
${matplotlibcpp17_INCLUDE_DIRS}
2428
${xtensor_INCLUDE_DIRS}
2529
)
2630
target_link_libraries(${name} ${Python3_LIBRARIES} pybind11::embed)
2731
endfunction()
2832

2933
if(${ADD_DEMO})
34+
find_package(Python3 COMPONENTS NumPy REQUIRED)
3035
find_package(xtensor REQUIRED)
3136
set(CMAKE_CXX_STANDARD 17)
3237
set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI}")
@@ -39,10 +44,67 @@ if(${ADD_DEMO})
3944
add_subdirectory(gallery/mplot3d)
4045
endif()
4146

47+
48+
# test
49+
enable_testing()
4250
if(NOT DEFINED DO_TEST)
4351
set(DO_TEST ON)
4452
endif()
45-
4653
if(${DO_TEST})
4754
add_subdirectory(tests)
4855
endif()
56+
57+
58+
# install
59+
## https://dominikberner.ch/cmake-interface-lib/
60+
include(GNUInstallDirs)
61+
add_library(${PROJECT_NAME} INTERFACE)
62+
target_include_directories(${PROJECT_NAME}
63+
INTERFACE
64+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
65+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
66+
)
67+
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
68+
install(TARGETS ${PROJECT_NAME}
69+
EXPORT ${PROJECT_NAME}_Targets
70+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
71+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
72+
)
73+
include(CMakePackageConfigHelpers)
74+
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
75+
VERSION ${PROJECT_VERSION}
76+
COMPATIBILITY SameMajorVersion
77+
)
78+
configure_package_config_file(
79+
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
80+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
81+
INSTALL_DESTINATION
82+
${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}
83+
)
84+
install(EXPORT ${PROJECT_NAME}_Targets
85+
FILE ${PROJECT_NAME}Targets.cmake
86+
NAMESPACE ${PROJECT_NAME}::
87+
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}
88+
)
89+
install(FILES
90+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
91+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
92+
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}
93+
)
94+
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}
95+
DESTINATION include
96+
)
97+
98+
# uninstall target
99+
## actually it's just `xargs rm < install_manifest.txt`
100+
## https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#can-i-do-make-uninstall-with-cmake
101+
if(NOT TARGET uninstall)
102+
configure_file(
103+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/uninstall.cmake.in"
104+
"${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake"
105+
IMMEDIATE @ONLY
106+
)
107+
add_custom_target(uninstall
108+
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake
109+
)
110+
endif()

README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,25 @@ It is supposed to provide the user with almost full access to matplotlib feature
1111
## Dependencies
1212

1313
- [pybind11](https://github.com/pybind/pybind11) >= 2.4.3
14-
- `sudo apt install pybind11-dev`
14+
- `sudo apt install pybind11-dev` (on Ubuntu20.04)
15+
- or manual install
1516
- compatible with [matplotlib](https://matplotlib.org/stable/index.html) == 3.5.1
1617
- numpy for `mplot3d`
17-
- [xtensor](https://github.com/xtensor-stack/xtensor) == 0.24.0 (+ [xtl](https://github.com/xtensor-stack/xtl), for `gallery` demos)
18+
- ([xtensor](https://github.com/xtensor-stack/xtensor) == 0.24.0 + [xtl](https://github.com/xtensor-stack/xtl), only for `gallery` demos)
1819

19-
## Usage
20+
## Installation
2021

21-
Just add include path to `include` directory of this project.
22+
```bash
23+
$ mdkir build; cd build;
24+
$ cmake .. -DADD_DEMO=0 (-DCMAKE_INSTALL_PREFIX=<custom path>)
25+
$ make -j
26+
$ make install
27+
$ (make uninstall)
28+
```
29+
30+
For using matplotlibcpp17 from CMakeLists.txt, see [hello_world](https://github.com/soblin/matplotlibcpp17/tree/master/hello_world) example.
31+
32+
Or you could just add include path to `include` directory and compile your codes as descibed in [minimal example](#minimal-example).
2233

2334
## Syntax
2435

@@ -31,7 +42,7 @@ The user will need to capsulate *arguments* in `Args(arg1, arg2, ...) == pybind1
3142
### minimal example
3243

3344
```cpp
34-
g++ hello_world.cpp -std=c++17 -I./include -I/usr/include/python3.x -I<path to pybind11> -lpython3.x
45+
g++ ./hello_world/hello_world.cpp -std=c++17 -I./include -I/usr/include/python3.x -I<path to pybind11> -lpython3.x
3546
./a.out
3647
```
3748

@@ -148,13 +159,17 @@ From [gallery/artist_animation/random_walk.cpp](https://github.com/soblin/matplo
148159
149160
### build
150161
162+
If you do not need to build the demos, use `-DADD_DEMO=0` (by default it is `1`).
163+
151164
```bash
152-
mkdir build; cd build; cmake .. ; make -j
165+
$ mkdir build; cd build
166+
$ cmake .. -DADD_DEMO={0, 1} -DUSE_GUI={0, 1}
167+
$ make -j
153168
```
154169

155-
If you want to see the demo with `plt.show()`, add `-DUSE_GUI=1` (by default it is `0`). Otherwise the executables will `plt.savefig()` to `gallery/images` directory.
170+
If you do not need to see the demo with `plt.show()`, use `-DUSE_GUI=0` (by default it is `1`). Otherwise the executables will `plt.savefig()` to `gallery/images` directory.
156171

157-
`make <gallery directory name>` runs all executables under that directory.
172+
`make <gallery directory name>` runs all the executables under that directory.
158173

159174
```bash
160175
make lines_bars_and_markers

cmake/matplotlibcpp17Config.cmake.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
4+
check_required_components("@PROJECT_NAME@")

cmake/uninstall.cmake.in

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
if(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt")
2+
message(FATAL_ERROR "Cannot find install manifest: @CMAKE_BINARY_DIR@/install_manifest.txt")
3+
endif()
4+
5+
file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files)
6+
string(REGEX REPLACE "\n" ";" files "${files}")
7+
foreach(file ${files})
8+
message(STATUS "Uninstalling $ENV{DESTDIR}${file}")
9+
if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
10+
exec_program(
11+
"@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
12+
OUTPUT_VARIABLE rm_out
13+
RETURN_VALUE rm_retval
14+
)
15+
if(NOT "${rm_retval}" STREQUAL 0)
16+
message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}")
17+
endif()
18+
else(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
19+
message(STATUS "File $ENV{DESTDIR}${file} does not exist.")
20+
endif()
21+
endforeach()

hello_world/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
# example of CMakeLists.txt with installed matplotlibcpp17
4+
project(matplotlibcpp17_hello_world)
5+
6+
find_package(Python3 COMPONENTS Interpreter Development)
7+
find_package(pybind11 REQUIRED)
8+
find_package(matplotlibcpp17 REQUIRED)
9+
10+
add_executable(hello_world hello_world.cpp)
11+
target_include_directories(hello_world PUBLIC ${Python3_INCLUDE_DIRS})
12+
target_link_libraries(hello_world ${Python3_LIBRARIES} pybind11::embed matplotlibcpp17::matplotlibcpp17)
File renamed without changes.

0 commit comments

Comments
 (0)