|
| 1 | +# Detects whether this is a top-level project |
| 2 | +get_directory_property(HAS_PARENT PARENT_DIRECTORY) |
| 3 | +if(HAS_PARENT) |
| 4 | + set(SJV_TOPLEVEL_PROJECT OFF) |
| 5 | +else() |
| 6 | + set(SJV_TOPLEVEL_PROJECT ON) |
| 7 | +endif() |
| 8 | + |
| 9 | +# Check required CMake version |
| 10 | +set(REQUIRED_CMAKE_VERSION "3.14.0") |
| 11 | +if(SJV_TOPLEVEL_PROJECT) |
| 12 | + cmake_minimum_required(VERSION ${REQUIRED_CMAKE_VERSION}) |
| 13 | +else() |
| 14 | + # Don't use cmake_minimum_required here to avoid implicitly overriding parent policies |
| 15 | + if(${CMAKE_VERSION} VERSION_LESS ${REQUIRED_CMAKE_VERSION}) |
| 16 | + message(FATAL_ERROR "CMake required version to build SJV is ${REQUIRED_CMAKE_VERSION}") |
| 17 | + endif() |
| 18 | +endif() |
| 19 | + |
| 20 | +# Include user-provided default options if available. We do that before the main |
| 21 | +# `project()` so that we can define the C/C++ compilers from the option file. |
| 22 | +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/SJVOptions.cmake) |
| 23 | + message(STATUS "Using local options file: ${CMAKE_CURRENT_SOURCE_DIR}/SJVOptions.cmake") |
| 24 | + include(${CMAKE_CURRENT_SOURCE_DIR}/SJVOptions.cmake) |
| 25 | +endif() |
| 26 | + |
| 27 | +################################################################################ |
| 28 | + |
| 29 | +project(SJV |
| 30 | + DESCRIPTION "Easy-to-use wrapper for linear solver" |
| 31 | + LANGUAGES CXX) |
| 32 | + |
| 33 | +# SJV options |
| 34 | +option(SJV_WITH_SANITIZERS "Enable sanitizers in compilation targets" OFF) |
| 35 | +# Sanitizer options |
| 36 | +option(SJV_SANITIZE_ADDRESS "Sanitize Address" OFF) |
| 37 | +option(SJV_SANITIZE_MEMORY "Sanitize Memory" OFF) |
| 38 | +option(SJV_SANITIZE_THREAD "Sanitize Thread" OFF) |
| 39 | +option(SJV_SANITIZE_UNDEFINED "Sanitize Undefined" OFF) |
| 40 | +# Misc. |
| 41 | +option(SJV_WITH_TESTS "Build unit-tests" ${SJV_TOPLEVEL_PROJECT}) |
| 42 | + |
| 43 | +include(CMakeDependentOption) |
| 44 | + |
| 45 | +# Set default minimum C++ standard |
| 46 | +if(SJV_TOPLEVEL_PROJECT) |
| 47 | + set(CMAKE_CXX_STANDARD 14) |
| 48 | + set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 49 | + set(CMAKE_CXX_EXTENSIONS OFF) |
| 50 | +endif() |
| 51 | + |
| 52 | +if (MSVC) |
| 53 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj") |
| 54 | +endif() |
| 55 | + |
| 56 | +### Configuration |
| 57 | +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/sjv/") |
| 58 | +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/recipes/") |
| 59 | +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/find/") |
| 60 | + |
| 61 | +# Color output |
| 62 | +include(sjv_use_colors) |
| 63 | + |
| 64 | +# IPC Toolkit utils |
| 65 | +include(sjv_prepend_current_path) |
| 66 | +include(sjv_set_source_group) |
| 67 | + |
| 68 | +# Sort projects inside the solution |
| 69 | +set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 70 | + |
| 71 | +# Generate position independent code by default |
| 72 | +set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 73 | + |
| 74 | +################################################################################ |
| 75 | +# SJV Library |
| 76 | +################################################################################ |
| 77 | + |
| 78 | +# Add an empty library and fill in the list of sources in `src/CMakeLists.txt`. |
| 79 | +add_library(sjv) |
| 80 | +add_library(sjv::sjv ALIAS sjv) |
| 81 | + |
| 82 | +add_subdirectory(src) |
| 83 | + |
| 84 | +# Public include directory for SJV |
| 85 | +target_include_directories(sjv PUBLIC ${PROJECT_SOURCE_DIR}/src) |
| 86 | + |
| 87 | +################################################################################ |
| 88 | +# Definitions |
| 89 | +################################################################################ |
| 90 | + |
| 91 | +if(SJV_LARGE_INDEX) |
| 92 | + target_compile_definitions(sjv PUBLIC -DSJV_LARGE_INDEX) |
| 93 | +endif() |
| 94 | + |
| 95 | +# No limit yay |
| 96 | +target_compile_definitions(sjv PUBLIC -DEIGEN_STACK_ALLOCATION_LIMIT=0) |
| 97 | + |
| 98 | +# 8MB |
| 99 | +# target_compile_definitions(sjv PUBLIC -DEIGEN_STACK_ALLOCATION_LIMIT=8388608) |
| 100 | + |
| 101 | +################################################################################ |
| 102 | +# Dependencies |
| 103 | +################################################################################ |
| 104 | + |
| 105 | +# Extra warnings |
| 106 | +include(sjv_warnings) |
| 107 | +target_link_libraries(sjv PRIVATE sjv::warnings) |
| 108 | + |
| 109 | +# Sanitizers |
| 110 | +if(SJV_WITH_SANITIZERS) |
| 111 | + include(sanitizers) |
| 112 | + add_sanitizers(sjv) |
| 113 | +endif() |
| 114 | + |
| 115 | +# include(eigen) |
| 116 | +# target_link_libraries(SJV PUBLIC Eigen3::Eigen) |
| 117 | + |
| 118 | +# Json (MIT) |
| 119 | +include(json) |
| 120 | +target_link_libraries(sjv PUBLIC nlohmann::json) |
| 121 | + |
| 122 | +################################################################################ |
| 123 | +# Compiler options |
| 124 | +################################################################################ |
| 125 | + |
| 126 | +# Use C++14 |
| 127 | +target_compile_features(sjv PUBLIC cxx_std_14) |
| 128 | + |
| 129 | +################################################################################ |
| 130 | +# Tests |
| 131 | +################################################################################ |
| 132 | + |
| 133 | +# Compile extras only if this is a top-level project |
| 134 | +if(SJV_WITH_TESTS) |
| 135 | + # Unit tests |
| 136 | + include(CTest) |
| 137 | + enable_testing() |
| 138 | + |
| 139 | + # Include Catch2 and provide function `catch_discover_tests` to register tests. |
| 140 | + include(catch2) |
| 141 | + FetchContent_GetProperties(catch2) |
| 142 | + include("${catch2_SOURCE_DIR}/contrib/Catch.cmake") |
| 143 | + |
| 144 | + add_subdirectory(tests) |
| 145 | +endif() |
0 commit comments