-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMakeLists.txt
160 lines (131 loc) · 4.84 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
# cannot have a cmake project name the same as a target or
# library name (use pp for ++ to indicate C++)
project(peakingduck LANGUAGES CXX)
set(HEADER_LIB_NAME ${PROJECT_NAME})
include(GNUInstallDirs)
# cmake options
option(BUILD_TESTS "Build unit tests" ON)
option(BUILD_PY_BINDINGS "Build python bindings" ON)
option(USE_SYSTEM_PYBIND "Use a pre-installed version of PyBind" OFF)
option(USE_SYSTEM_EIGEN "Use a pre-installed version of Eigen" OFF)
option(USE_SYSTEM_UNITS "Use a pre-installed version of Units" OFF)
option(USE_SYSTEM_CATCH2 "Use a pre-installed version of Catch2" OFF)
# set various source directories in this project
set(PROJECT_DIR_BASE ${CMAKE_CURRENT_LIST_DIR}/include)
set(PYTHON_PROJECT_DIR_BASE ${CMAKE_CURRENT_LIST_DIR}/py)
set(TESTS_DIR_BASE ${CMAKE_CURRENT_LIST_DIR}/tests)
set(THIRD_PARTY_DIR ${CMAKE_CURRENT_LIST_DIR}/thirdparty)
# load dependencies
if(USE_SYSTEM_EIGEN)
find_package(Eigen3 REQUIRED NO_MODULE)
else()
set(BUILD_TESTING OFF CACHE STRING "Enable creation of Eigen tests.")
add_subdirectory(${THIRD_PARTY_DIR}/eigen)
endif()
if(USE_SYSTEM_UNITS)
find_package(units REQUIRED)
else()
set(BUILD_TESTS_TMP ${BUILD_TESTS})
set(BUILD_TESTS OFF CACHE STRING
"Temporarily turn of building unit tests while importing units" FORCE)
add_subdirectory(${THIRD_PARTY_DIR}/units)
set(BUILD_TESTS ${BUILD_TESTS_TMP} CACHE STRING "Build unit tests" FORCE)
add_library(units::units ALIAS units)
endif()
if(BUILD_TESTS)
if(USE_SYSTEM_CATCH2)
find_package(Catch2 REQUIRED)
else()
list(APPEND CMAKE_MODULE_PATH ${THIRD_PARTY_DIR}/catch2/cmake)
add_subdirectory(${THIRD_PARTY_DIR}/Catch2)
endif()
endif()
if(BUILD_PY_BINDINGS)
if(USE_SYSTEM_PYBIND)
find_package(pybind11 REQUIRED)
else()
add_subdirectory(${THIRD_PARTY_DIR}/pybind11)
endif()
endif(BUILD_PY_BINDINGS)
# set the output directories
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(PY_OUTPUT_DIR ${CMAKE_BINARY_DIR}/py)
# Get the version, set in .VERSION file
file(STRINGS "${CMAKE_SOURCE_DIR}/.VERSION" VERSION)
string(REPLACE "." ";" VERSION_LIST ${VERSION})
list(GET VERSION_LIST 0 VERSION_MAJOR)
list(GET VERSION_LIST 1 VERSION_MINOR)
list(GET VERSION_LIST 2 VERSION_PATCH)
set(PEAKINGDUCK_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
message(STATUS "CMake build configuration for PEAKING DUCK ${PEAKINGDUCK_VERSION}")
# C++ flags
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_FLAGS "-Wall -Wextra" CACHE STRING "Compiler flags")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 --coverage -fprofile-arcs -ftest-coverage -fno-inline -fno-inline-small-functions -fno-default-inline")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
if (CMAKE_BUILD_TYPE EQUAL "DEBUG")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif (CMAKE_BUILD_TYPE EQUAL "DEBUG")
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# Increase warning levels
add_compile_options(-Wall -Wextra -pedantic)
endif()
# header-only library target. To use this project as a subdirectory,
# add the following to your code:
#
# add_subdirectory(peakingduck) # or whatever you named the directory
# target_link_libraries(${PROJECT_NAME} peakingduck)
add_library(${HEADER_LIB_NAME} INTERFACE)
set(HEADERLIB_DEPENDENCIES "Eigen3::Eigen units::units")
target_link_libraries(${HEADER_LIB_NAME}
INTERFACE
Eigen3::Eigen
units::units
)
target_include_directories(${HEADER_LIB_NAME}
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>
)
# python bindings
if(BUILD_PY_BINDINGS)
add_subdirectory(${PYTHON_PROJECT_DIR_BASE})
endif(BUILD_PY_BINDINGS)
# unit tests
if(BUILD_TESTS)
add_subdirectory(${TESTS_DIR_BASE}/cpp)
endif(BUILD_TESTS)
# install the header-only library
include(CMakePackageConfigHelpers)
set(CONFIG_INSTALL_DIR ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME})
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PEAKINGDUCK_VERSION}
COMPATIBILITY SameMajorVersion
)
configure_package_config_file(${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${CONFIG_INSTALL_DIR}
PATH_VARS CONFIG_INSTALL_DIR
)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION ${CONFIG_INSTALL_DIR}
)
install(TARGETS ${HEADER_LIB_NAME}
EXPORT ${PROJECT_NAME}
)
install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)
install(EXPORT ${PROJECT_NAME}
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CONFIG_INSTALL_DIR}
)