|
| 1 | +cmake_minimum_required (VERSION 3.13) |
| 2 | + |
| 3 | +option (BUILD_PYTHON "Build the python module for the library" ON) |
| 4 | +option (USE_SPHINX "Use sphinx for documentation" OFF) |
| 5 | +option (BUILD_SHARED_LIBS "Build shared libraries" ON) |
| 6 | + |
| 7 | +# By default, build in Release mode. Must appear before project() command |
| 8 | +set( CMAKE_BUILD_TYPE Release CACHE STRING "Build type") |
| 9 | + |
| 10 | +project (otvine) |
| 11 | + |
| 12 | +string (TOLOWER ${CMAKE_PROJECT_NAME} PACKAGE_NAME) |
| 13 | + |
| 14 | +# set module dir to find custom scripts |
| 15 | +list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) |
| 16 | + |
| 17 | +if (POLICY CMP0042) |
| 18 | + # MACOSX_RPATH is enabled by default |
| 19 | + cmake_policy(SET CMP0042 NEW) |
| 20 | +endif() |
| 21 | +if (POLICY CMP0078) |
| 22 | + # UseSWIG generates standard target names |
| 23 | + cmake_policy(SET CMP0078 NEW) |
| 24 | +endif() |
| 25 | +if (POLICY CMP0086) |
| 26 | + # UseSWIG honors SWIG_MODULE_NAME via -module flag |
| 27 | + cmake_policy(SET CMP0086 NEW) |
| 28 | +endif() |
| 29 | + |
| 30 | +find_package (OpenTURNS CONFIG REQUIRED) |
| 31 | +message (STATUS "Found OpenTURNS: ${OPENTURNS_ROOT_DIR} (found version \"${OPENTURNS_VERSION_STRING}\")") |
| 32 | + |
| 33 | +find_package (vinecopulib CONFIG REQUIRED) |
| 34 | + |
| 35 | +find_package(Eigen3 CONFIG REQUIRED) |
| 36 | + |
| 37 | +if (NOT BUILD_SHARED_LIBS) |
| 38 | + list ( APPEND OTVINE_DEFINITIONS "-DOTVINE_STATIC" ) |
| 39 | +endif () |
| 40 | +add_definitions ( ${OTVINE_DEFINITIONS} ) |
| 41 | + |
| 42 | +if (MSVC) |
| 43 | + # Disable some warnings |
| 44 | + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4197 /wd4244 /wd4251 /wd4267 /wd4996") |
| 45 | +endif () |
| 46 | + |
| 47 | +include (GNUInstallDirs) |
| 48 | + |
| 49 | +set (OTVINE_LIBRARY_PATH ${CMAKE_INSTALL_LIBDIR}) |
| 50 | +set (OTVINE_INCLUDE_PATH ${CMAKE_INSTALL_INCLUDEDIR}) |
| 51 | +set (OTVINE_SWIG_INCLUDE_DIRS ${OTVINE_INCLUDE_PATH}/${PACKAGE_NAME}/swig) |
| 52 | +set (OTVINE_DATA_PATH ${CMAKE_INSTALL_DATAROOTDIR}) |
| 53 | +set (OTVINE_CONFIG_CMAKE_PATH ${CMAKE_INSTALL_LIBDIR}/cmake/otvine) |
| 54 | +set (OTVINE_DOC_PATH ${CMAKE_INSTALL_DOCDIR}) |
| 55 | + |
| 56 | +if (BUILD_PYTHON) |
| 57 | + find_package (SWIG 3) |
| 58 | + include (${SWIG_USE_FILE}) |
| 59 | + |
| 60 | + if (CMAKE_VERSION VERSION_LESS 3.28) |
| 61 | + find_package (Python 3.6 COMPONENTS Interpreter Development) |
| 62 | + else () |
| 63 | + find_package (Python 3.6 COMPONENTS Interpreter Development.Module Development.SABIModule) |
| 64 | + endif () |
| 65 | + |
| 66 | + if (Python_FOUND) |
| 67 | + include (FindPythonModule) |
| 68 | + find_python_module (matplotlib) |
| 69 | + if (NOT TARGET Python::Module) |
| 70 | + include (TargetLinkLibrariesWithDynamicLookup) |
| 71 | + endif () |
| 72 | + |
| 73 | + if (USE_SPHINX) |
| 74 | + find_program (SPHINX_EXECUTABLE NAMES sphinx-build DOC "Sphinx Documentation Builder (sphinx-doc.org)") |
| 75 | + find_python_module (numpydoc) |
| 76 | + find_python_module (sphinx_gallery) |
| 77 | + find_python_module (sphinx_copybutton) |
| 78 | + if (SPHINX_EXECUTABLE AND NUMPYDOC_FOUND AND SPHINX_GALLERY_FOUND AND SPHINX_COPYBUTTON_FOUND AND MATPLOTLIB_FOUND) |
| 79 | + find_python_module (sphinx) |
| 80 | + endif () |
| 81 | + if (NOT SPHINX_FOUND) |
| 82 | + message (STATUS "Disabling documentation (need sphinx/pandoc/numpydoc/sphinx-gallery/matplotlib)") |
| 83 | + endif () |
| 84 | + endif () |
| 85 | + |
| 86 | + if (WIN32) |
| 87 | + set (OTVINE_PYTHON_MODULE_PATH Lib/site-packages CACHE STRING "site-packages dir") |
| 88 | + else () |
| 89 | + set (OTVINE_PYTHON_MODULE_PATH ${CMAKE_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages CACHE STRING "site-packages dir") |
| 90 | + endif () |
| 91 | + endif () |
| 92 | +endif () |
| 93 | + |
| 94 | + |
| 95 | +# Define variables without OTVINE_ prefix with absolute paths |
| 96 | +foreach (_var PYTHON_MODULE_PATH LIBRARY_PATH INCLUDE_PATH CONFIG_CMAKE_PATH) |
| 97 | + if (IS_ABSOLUTE "${OTVINE_${_var}}") |
| 98 | + set (${_var} ${OTVINE_${_var}}) |
| 99 | + else () |
| 100 | + set (${_var} ${CMAKE_INSTALL_PREFIX}/${OTVINE_${_var}}) |
| 101 | + endif () |
| 102 | + get_filename_component (${_var} "${${_var}}" ABSOLUTE) |
| 103 | + file (TO_NATIVE_PATH "${${_var}}" NATIVE_${_var}) |
| 104 | + string (REPLACE "\\" "\\\\" NATIVE_${_var} ${NATIVE_${_var}}) |
| 105 | +endforeach () |
| 106 | + |
| 107 | +# RPATH settings |
| 108 | +set (CMAKE_INSTALL_RPATH ${LIBRARY_PATH}) |
| 109 | +set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) |
| 110 | + |
| 111 | +# Some useful macros to ease CMakeLists.txt file writing |
| 112 | +set (SOURCEFILES "" CACHE INTERNAL "List of source files to compile") |
| 113 | +macro (ot_add_source_file FILENAME) |
| 114 | + set (sf ${SOURCEFILES} ${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}) |
| 115 | + set (SOURCEFILES ${sf} CACHE INTERNAL "List of source files to compile") |
| 116 | +endmacro () |
| 117 | + |
| 118 | +macro (ot_install_header_file FILENAME) |
| 119 | + install (FILES ${PACKAGE_NAME}/${FILENAME} DESTINATION ${OTVINE_INCLUDE_PATH}/${PACKAGE_NAME}) |
| 120 | +endmacro () |
| 121 | + |
| 122 | +macro (ot_install_swig_file FILENAME) |
| 123 | + install (FILES ${FILENAME} DESTINATION ${OTVINE_SWIG_INCLUDE_DIRS}) |
| 124 | +endmacro () |
| 125 | + |
| 126 | +set (INTERNAL_INCLUDE_DIRS "" CACHE INTERNAL "List of directories with header files needed for build") |
| 127 | +macro (ot_add_current_dir_to_include_dirs) |
| 128 | + set (inc_dirs ${INTERNAL_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}) |
| 129 | + set (INTERNAL_INCLUDE_DIRS ${inc_dirs} CACHE INTERNAL "List of directories with header files needed for build") |
| 130 | +endmacro () |
| 131 | + |
| 132 | + |
| 133 | +set ( CPACK_PACKAGE_NAME ${PACKAGE_NAME} ) |
| 134 | +set ( CPACK_PACKAGE_VERSION_MAJOR 0 ) |
| 135 | +set ( CPACK_PACKAGE_VERSION_MINOR 0 ) |
| 136 | +set ( CPACK_PACKAGE_VERSION_PATCH ) |
| 137 | +set ( CPACK_SOURCE_GENERATOR "TGZ;TBZ2" ) |
| 138 | +set (CPACK_BINARY_STGZ OFF CACHE BOOL "STGZ") |
| 139 | +set (CPACK_BINARY_TBZ2 ON CACHE BOOL "TBZ2") |
| 140 | +set (CPACK_BINARY_TGZ ON CACHE BOOL "TGZ" ) |
| 141 | +set (CPACK_BINARY_TZ OFF CACHE BOOL "TZ" ) |
| 142 | +set ( CPACK_SOURCE_IGNORE_FILES "/.git;/build;.*~;${CPACK_SOURCE_IGNORE_FILES}" ) |
| 143 | +set ( PACKAGE_NAME ${CPACK_PACKAGE_NAME} ) |
| 144 | +set ( PACKAGE_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR} ) |
| 145 | +if ( CPACK_PACKAGE_VERSION_PATCH ) |
| 146 | + set ( PACKAGE_VERSION ${PACKAGE_VERSION}.${CPACK_PACKAGE_VERSION_PATCH} ) |
| 147 | +endif () |
| 148 | +set ( CPACK_SOURCE_PACKAGE_FILE_NAME ${PACKAGE_NAME}-${PACKAGE_VERSION} ) |
| 149 | + |
| 150 | + |
| 151 | +# The tests can't be run if this function is absent |
| 152 | +enable_testing () |
| 153 | +add_custom_target (tests COMMENT "Build tests") |
| 154 | +add_custom_target (check COMMENT "Run pre-installation tests") |
| 155 | +add_custom_target (installcheck COMMENT "Run post-installation tests") |
| 156 | + |
| 157 | +add_subdirectory (lib) |
| 158 | +add_dependencies (check cppcheck) |
| 159 | + |
| 160 | +if (Python_FOUND AND SWIG_FOUND) |
| 161 | + add_subdirectory (python) |
| 162 | + add_dependencies (installcheck pyinstallcheck) |
| 163 | +endif () |
| 164 | + |
| 165 | + |
| 166 | +# uninstall target |
| 167 | +configure_file ( |
| 168 | + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in |
| 169 | + ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake |
| 170 | + IMMEDIATE @ONLY |
| 171 | +) |
| 172 | + |
| 173 | +add_custom_target ( uninstall |
| 174 | + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake |
| 175 | +) |
| 176 | + |
| 177 | +set ( OTVINE_LIBRARY otvine ) |
| 178 | +set ( OTVINE_LIBRARIES ${OPENTURNS_LIBRARIES} ) |
| 179 | +list ( APPEND OTVINE_LIBRARIES ${OTVINE_LIBRARY} ) |
| 180 | +set ( OTVINE_LIBRARY_DIRS ${OPENTURNS_LIBRARY_DIRS} ) |
| 181 | +list ( APPEND OTVINE_LIBRARY_DIRS ${LIBRARY_PATH} ) |
| 182 | +set ( OTVINE_INCLUDE_DIR ${INCLUDE_PATH} ) |
| 183 | +set ( OTVINE_INCLUDE_DIRS ${OTVINE_INCLUDE_DIR} ) |
| 184 | +set ( OTVINE_ROOT_DIR ${CMAKE_INSTALL_PREFIX} ) |
| 185 | +list ( APPEND OTVINE_INCLUDE_DIRS ${OPENTURNS_INCLUDE_DIRS} ) |
| 186 | +set ( OTVINE_VERSION_STRING ${PACKAGE_VERSION} ) |
| 187 | +set ( OTVINE_VERSION_MAJOR ${CPACK_PACKAGE_VERSION_MAJOR} ) |
| 188 | +set ( OTVINE_VERSION_MINOR ${CPACK_PACKAGE_VERSION_MINOR} ) |
| 189 | +set ( OTVINE_VERSION_PATCH ${CPACK_PACKAGE_VERSION_PATCH} ) |
| 190 | + |
| 191 | +include(CMakePackageConfigHelpers) |
| 192 | +configure_package_config_file (cmake/otvineConfig.cmake.in |
| 193 | + ${CMAKE_CURRENT_BINARY_DIR}/otvineConfig.cmake |
| 194 | + INSTALL_DESTINATION ${OTVINE_CONFIG_CMAKE_PATH} |
| 195 | + PATH_VARS |
| 196 | + OTVINE_ROOT_DIR |
| 197 | + OTVINE_INCLUDE_DIR |
| 198 | + OTVINE_INCLUDE_DIRS |
| 199 | + OTVINE_LIBRARY_DIRS |
| 200 | + PYTHON_MODULE_PATH |
| 201 | + NO_CHECK_REQUIRED_COMPONENTS_MACRO) |
| 202 | + |
| 203 | +write_basic_package_version_file (otvineConfigVersion.cmake VERSION ${OTVINE_VERSION_STRING} COMPATIBILITY AnyNewerVersion) |
| 204 | + |
| 205 | +install (FILES ${CMAKE_CURRENT_BINARY_DIR}/otvineConfig.cmake |
| 206 | + ${CMAKE_CURRENT_BINARY_DIR}/otvineConfigVersion.cmake |
| 207 | + DESTINATION ${OTVINE_CONFIG_CMAKE_PATH}) |
| 208 | + |
| 209 | +include (CPack) |
0 commit comments