-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
130 lines (117 loc) · 4.56 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
cmake_minimum_required(VERSION 3.10)
project(lib_math
VERSION 1.4.0
DESCRIPTION "A extended library for matrix mathematics"
LANGUAGES CXX)
message(STATUS "Project: ${PROJECT_NAME}")
set(CMAKE_CXX_STANDARD 17)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
# Set the install path and prefix
option(SPECIFY_INSTALL_PATH "Specify the install path" OFF)
if(SPECIFY_INSTALL_PATH)
set(MODULE_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
message(STATUS "Module root: ${MODULE_ROOT}")
set(REPO_ROOT ${MODULE_ROOT}/../)
message(STATUS "Repo root: ${REPO_ROOT}")
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX
${REPO_ROOT}/buildtarget/ CACHE PATH "repo root" FORCE
)
endif()
endif()
message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
option(LIB_MATH_USE_DOUBLE "Use double precision in kinematics" OFF)
if(LIB_MATH_USE_DOUBLE)
add_definitions(-DLIB_MATH_USE_DOUBLE)
message(STATUS "Use double precision in kinematics")
endif()
# make cache variables for install destinations
include(GNUInstallDirs)
# --------------------------------------------------------------------
# Create target and set properties
# --------------------------------------------------------------------
# Specifies include directories to use when compiling a given target.
# The target name must have been created by a command such as
# add_excutable() or add_library().
find_package(Eigen3 REQUIRED CONFIG)
if(TARGET Eigen3::Eigen)
message(STATUS "Eigen3 v${EIGEN3_VERSION_STRING} found in ${EIGEN3_INCLUDE_DIR}")
else()
message(ERROR "Cannot find Eigen3")
endif()
file(GLOB_RECURSE SRCS src/*.cpp)
add_library(${PROJECT_NAME} STATIC
${SRCS}
)
# We need to tell CMake that we want to use different include
# directories depending on if we are building the library or using it
# from an installed location. If we don't do this, when CMake creates
# the export information it will export a path that is specific to the
# current build directory and will not be valid for other projects.
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${EIGEN3_INCLUDE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
Eigen3::Eigen
)
# --------------------------------------------------------------------
# Installation
# --------------------------------------------------------------------
# The install(TARGETS) and install(EXPORT) commands work together to
# install both targets and a CMake file designed to make it easy to
# import the targets into another CMake project.
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
message(STATUS "INSTALL_CONFIGDIR: ${INSTALL_CONFIGDIR}")
# 1. Install library
# Set installation path
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}-targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)
# 2. Install header files
file(GLOB EXPORT_H export/*.h)
install(FILES ${EXPORT_H}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)
install(DIRECTORY export/${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/
)
# 3. Explicitly install the ${PROJECT_NAME}Targets export details.
install(EXPORT ${PROJECT_NAME}-targets
# NAMESPACE mylib::
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION ${INSTALL_CONFIGDIR}
)
# --------------------------------------------------------------------
# Creating Packages
# --------------------------------------------------------------------
# Create a library that can be found by find_package()
# i.e., creat a ConfigVersion.cmake file
# Install the Config, ConfigVersion and Custom find moduls
include(CMakePackageConfigHelpers)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
# Install the package infomation
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${INSTALL_CONFIGDIR}
)