-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMakeLists.txt
205 lines (168 loc) · 7.67 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
cmake_minimum_required (VERSION 3.13)
option (BUILD_PYTHON "Build the python module for the library" ON)
option (USE_SPHINX "Use sphinx for documentation" OFF)
option (BUILD_SHARED_LIBS "Build shared libraries" ON)
# By default, build in Release mode. Must appear before project() command
set( CMAKE_BUILD_TYPE Release CACHE STRING "Build type")
project (ottemplate)
string (TOLOWER ${CMAKE_PROJECT_NAME} PACKAGE_NAME)
# set module dir to find custom scripts
list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
if (POLICY CMP0042)
# MACOSX_RPATH is enabled by default
cmake_policy(SET CMP0042 NEW)
endif()
if (POLICY CMP0078)
# UseSWIG generates standard target names
cmake_policy(SET CMP0078 NEW)
endif()
if (POLICY CMP0086)
# UseSWIG honors SWIG_MODULE_NAME via -module flag
cmake_policy(SET CMP0086 NEW)
endif()
find_package (OpenTURNS CONFIG REQUIRED)
message (STATUS "Found OpenTURNS: ${OPENTURNS_ROOT_DIR} (found version \"${OPENTURNS_VERSION_STRING}\")")
if (NOT BUILD_SHARED_LIBS)
list ( APPEND OTTEMPLATE_DEFINITIONS "-DOTTEMPLATE_STATIC" )
endif ()
add_definitions ( ${OTTEMPLATE_DEFINITIONS} )
if (MSVC)
# Disable some warnings
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4197 /wd4244 /wd4251 /wd4267 /wd4996")
endif ()
include (GNUInstallDirs)
set (OTTEMPLATE_LIBRARY_PATH ${CMAKE_INSTALL_LIBDIR})
set (OTTEMPLATE_INCLUDE_PATH ${CMAKE_INSTALL_INCLUDEDIR})
set (OTTEMPLATE_SWIG_INCLUDE_DIRS ${OTTEMPLATE_INCLUDE_PATH}/${PACKAGE_NAME}/swig)
set (OTTEMPLATE_DATA_PATH ${CMAKE_INSTALL_DATAROOTDIR})
set (OTTEMPLATE_CONFIG_CMAKE_PATH ${CMAKE_INSTALL_LIBDIR}/cmake/ottemplate)
set (OTTEMPLATE_DOC_PATH ${CMAKE_INSTALL_DOCDIR})
if (BUILD_PYTHON)
find_package (SWIG 3)
include (${SWIG_USE_FILE})
if (CMAKE_VERSION VERSION_LESS 3.26)
find_package (Python 3.6 COMPONENTS Interpreter Development)
else ()
find_package (Python 3.6 COMPONENTS Interpreter Development.Module Development.SABIModule)
endif ()
if (Python_FOUND)
include (FindPythonModule)
find_python_module (matplotlib)
if (NOT TARGET Python::Module)
include (TargetLinkLibrariesWithDynamicLookup)
endif ()
if (USE_SPHINX)
find_program (SPHINX_EXECUTABLE NAMES sphinx-build DOC "Sphinx Documentation Builder (sphinx-doc.org)")
find_python_module (numpydoc)
find_python_module (sphinx_gallery)
find_python_module (sphinx_copybutton)
if (SPHINX_EXECUTABLE AND NUMPYDOC_FOUND AND SPHINX_GALLERY_FOUND AND SPHINX_COPYBUTTON_FOUND AND MATPLOTLIB_FOUND)
find_python_module (sphinx)
endif ()
if (NOT SPHINX_FOUND)
message (STATUS "Disabling documentation (need sphinx/pandoc/numpydoc/sphinx-gallery/matplotlib)")
endif ()
endif ()
if (WIN32)
set (OTTEMPLATE_PYTHON_MODULE_PATH Lib/site-packages CACHE STRING "site-packages dir")
else ()
set (OTTEMPLATE_PYTHON_MODULE_PATH ${CMAKE_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages CACHE STRING "site-packages dir")
endif ()
endif ()
endif ()
# Define variables without OTTEMPLATE_ prefix with absolute paths
foreach (_var PYTHON_MODULE_PATH LIBRARY_PATH INCLUDE_PATH CONFIG_CMAKE_PATH)
if (IS_ABSOLUTE "${OTTEMPLATE_${_var}}")
set (${_var} ${OTTEMPLATE_${_var}})
else ()
set (${_var} ${CMAKE_INSTALL_PREFIX}/${OTTEMPLATE_${_var}})
endif ()
get_filename_component (${_var} "${${_var}}" ABSOLUTE)
file (TO_NATIVE_PATH "${${_var}}" NATIVE_${_var})
string (REPLACE "\\" "\\\\" NATIVE_${_var} ${NATIVE_${_var}})
endforeach ()
# RPATH settings
set (CMAKE_INSTALL_RPATH ${LIBRARY_PATH})
set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# Some useful macros to ease CMakeLists.txt file writing
set (SOURCEFILES "" CACHE INTERNAL "List of source files to compile")
macro (ot_add_source_file FILENAME)
set (sf ${SOURCEFILES} ${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME})
set (SOURCEFILES ${sf} CACHE INTERNAL "List of source files to compile")
endmacro ()
macro (ot_install_header_file FILENAME)
install (FILES ${PACKAGE_NAME}/${FILENAME} DESTINATION ${OTTEMPLATE_INCLUDE_PATH}/${PACKAGE_NAME})
endmacro ()
macro (ot_install_swig_file FILENAME)
install (FILES ${FILENAME} DESTINATION ${OTTEMPLATE_SWIG_INCLUDE_DIRS})
endmacro ()
set (INTERNAL_INCLUDE_DIRS "" CACHE INTERNAL "List of directories with header files needed for build")
macro (ot_add_current_dir_to_include_dirs)
set (inc_dirs ${INTERNAL_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})
set (INTERNAL_INCLUDE_DIRS ${inc_dirs} CACHE INTERNAL "List of directories with header files needed for build")
endmacro ()
set ( CPACK_PACKAGE_NAME ${PACKAGE_NAME} )
set ( CPACK_PACKAGE_VERSION_MAJOR 0 )
set ( CPACK_PACKAGE_VERSION_MINOR 0 )
set ( CPACK_PACKAGE_VERSION_PATCH )
set ( CPACK_SOURCE_GENERATOR "TGZ;TBZ2" )
set (CPACK_BINARY_STGZ OFF CACHE BOOL "STGZ")
set (CPACK_BINARY_TBZ2 ON CACHE BOOL "TBZ2")
set (CPACK_BINARY_TGZ ON CACHE BOOL "TGZ" )
set (CPACK_BINARY_TZ OFF CACHE BOOL "TZ" )
set ( CPACK_SOURCE_IGNORE_FILES "/.git;/build;.*~;${CPACK_SOURCE_IGNORE_FILES}" )
set ( PACKAGE_NAME ${CPACK_PACKAGE_NAME} )
set ( PACKAGE_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR} )
if ( CPACK_PACKAGE_VERSION_PATCH )
set ( PACKAGE_VERSION ${PACKAGE_VERSION}.${CPACK_PACKAGE_VERSION_PATCH} )
endif ()
set ( CPACK_SOURCE_PACKAGE_FILE_NAME ${PACKAGE_NAME}-${PACKAGE_VERSION} )
# The tests can't be run if this function is absent
enable_testing ()
add_custom_target (tests COMMENT "Build tests")
add_custom_target (check COMMENT "Run pre-installation tests")
add_custom_target (installcheck COMMENT "Run post-installation tests")
add_subdirectory (lib)
add_dependencies (check cppcheck)
if (Python_FOUND AND SWIG_FOUND)
add_subdirectory (python)
add_dependencies (installcheck pyinstallcheck)
endif ()
# uninstall target
configure_file (
${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
IMMEDIATE @ONLY
)
add_custom_target ( uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
)
set ( OTTEMPLATE_LIBRARY ottemplate )
set ( OTTEMPLATE_LIBRARIES ${OPENTURNS_LIBRARIES} )
list ( APPEND OTTEMPLATE_LIBRARIES ${OTTEMPLATE_LIBRARY} )
set ( OTTEMPLATE_LIBRARY_DIRS ${OPENTURNS_LIBRARY_DIRS} )
list ( APPEND OTTEMPLATE_LIBRARY_DIRS ${LIBRARY_PATH} )
set ( OTTEMPLATE_INCLUDE_DIR ${INCLUDE_PATH} )
set ( OTTEMPLATE_INCLUDE_DIRS ${OTTEMPLATE_INCLUDE_DIR} )
set ( OTTEMPLATE_ROOT_DIR ${CMAKE_INSTALL_PREFIX} )
list ( APPEND OTTEMPLATE_INCLUDE_DIRS ${OPENTURNS_INCLUDE_DIRS} )
set ( OTTEMPLATE_VERSION_STRING ${PACKAGE_VERSION} )
set ( OTTEMPLATE_VERSION_MAJOR ${CPACK_PACKAGE_VERSION_MAJOR} )
set ( OTTEMPLATE_VERSION_MINOR ${CPACK_PACKAGE_VERSION_MINOR} )
set ( OTTEMPLATE_VERSION_PATCH ${CPACK_PACKAGE_VERSION_PATCH} )
include(CMakePackageConfigHelpers)
configure_package_config_file (cmake/OTTemplateConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/OTTemplateConfig.cmake
INSTALL_DESTINATION ${OTTEMPLATE_CONFIG_CMAKE_PATH}
PATH_VARS
OTTEMPLATE_ROOT_DIR
OTTEMPLATE_INCLUDE_DIR
OTTEMPLATE_INCLUDE_DIRS
OTTEMPLATE_LIBRARY_DIRS
PYTHON_MODULE_PATH
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
write_basic_package_version_file (OTTemplateConfigVersion.cmake VERSION ${OTTEMPLATE_VERSION_STRING} COMPATIBILITY AnyNewerVersion)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/OTTemplateConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/OTTemplateConfigVersion.cmake
DESTINATION ${OTTEMPLATE_CONFIG_CMAKE_PATH})
include (CPack)