|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +cmake_minimum_required(VERSION 3.30) |
| 4 | + |
| 5 | +if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) |
| 6 | + set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "") |
| 7 | +endif() |
| 8 | + |
| 9 | +project(iris VERSION 0.0.1 LANGUAGES CXX) |
| 10 | + |
| 11 | + |
| 12 | +# ----------------------------------------------------------------- |
| 13 | +# Global settings |
| 14 | +# Handle with care, keep these to the ones that can't be |
| 15 | +# accomplished by target-specific settings. |
| 16 | + |
| 17 | +set(CMAKE_COLOR_DIAGNOSTICS ON) |
| 18 | +set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 19 | + |
| 20 | +option(IRIS_REMOVE_MINSIZEREL_CONFIG "Remove rarely used MinSizeRel config" ON) |
| 21 | + |
| 22 | +if(MSVC) |
| 23 | + if(IRIS_REMOVE_MINSIZEREL_CONFIG OR PROJECT_IS_TOP_LEVEL) |
| 24 | + list(REMOVE_ITEM CMAKE_CONFIGURATION_TYPES MinSizeRel) |
| 25 | + endif() |
| 26 | +endif() |
| 27 | + |
| 28 | + |
| 29 | +# ----------------------------------------------------------------- |
| 30 | +# Create common base targets |
| 31 | + |
| 32 | +# Iris-specific common target |
| 33 | +add_library(_iris_cxx_common INTERFACE) |
| 34 | +set_target_properties(_iris_cxx_common PROPERTIES CXX_EXTENSIONS OFF) |
| 35 | + |
| 36 | + |
| 37 | +# ----------------------------------------------------------------- |
| 38 | +# Create the main Iris target |
| 39 | + |
| 40 | +if(MSVC) |
| 41 | + # This needs to be `OBJECT` target to set correct `/std:` flags on IDE |
| 42 | + add_library(iris OBJECT EXCLUDE_FROM_ALL) |
| 43 | + set_target_properties(iris PROPERTIES LINKER_LANGUAGE CXX) |
| 44 | + |
| 45 | + target_sources( |
| 46 | + iris |
| 47 | + PRIVATE |
| 48 | + # "${PROJECT_SOURCE_DIR}/cpp.hint" # TODO |
| 49 | + # "${PROJECT_SOURCE_DIR}/iris.natvis" # TODO |
| 50 | + ) |
| 51 | + |
| 52 | + target_link_libraries(iris PUBLIC _iris_cxx_common) |
| 53 | + |
| 54 | +else() |
| 55 | + add_library(iris INTERFACE) |
| 56 | + target_link_libraries(iris INTERFACE _iris_cxx_common) |
| 57 | +endif() |
| 58 | + |
| 59 | +add_library(Iris::Iris ALIAS iris) |
| 60 | +set_target_properties(iris PROPERTIES CXX_EXTENSIONS OFF) |
| 61 | + |
| 62 | + |
| 63 | +# ----------------------------------------------------------------- |
| 64 | +# Configure C++ version |
| 65 | +# |
| 66 | +# Set minimal C++ version to `/std:c++XXpreview` and avoid `/std:c++latest` |
| 67 | +# if the user explicitly specifies `-DCMAKE_CXX_STANDARD=XX`. |
| 68 | + |
| 69 | +set(IRIS_CXX_VERSION_BEFORE_LATEST 23) |
| 70 | +set(IRIS_CXX_VERSION_LATEST 26) |
| 71 | + |
| 72 | +if(DEFINED CMAKE_CXX_STANDARD AND CMAKE_CXX_STANDARD LESS ${IRIS_CXX_VERSION_BEFORE_LATEST}) |
| 73 | + message(FATAL_ERROR "Too old C++ version `${CMAKE_CXX_STANDARD}`; the minimal version supported by Iris is `${IRIS_CXX_VERSION_BEFORE_LATEST}`.") |
| 74 | +endif() |
| 75 | + |
| 76 | +if(MSVC) |
| 77 | + if(CMAKE_CXX_STANDARD EQUAL ${IRIS_CXX_VERSION_BEFORE_LATEST}) |
| 78 | + set(CMAKE_CXX${IRIS_CXX_VERSION_BEFORE_LATEST}_STANDARD_COMPILE_OPTION /std:c++${IRIS_CXX_VERSION_BEFORE_LATEST}preview) |
| 79 | + set(CMAKE_CXX${IRIS_CXX_VERSION_BEFORE_LATEST}_EXTENSION_COMPILE_OPTION /std:c++${IRIS_CXX_VERSION_BEFORE_LATEST}preview) |
| 80 | + target_compile_options(_iris_cxx_common INTERFACE /std:c++${IRIS_CXX_VERSION_BEFORE_LATEST}preview) |
| 81 | + |
| 82 | + else() |
| 83 | + # MSVC's CMake support does not provide the latest `cxx_std_XX` |
| 84 | + # feature until the very last stage of the implementation. Instead, |
| 85 | + # the feature number that is one version behind the latest usually |
| 86 | + # resolves to `/std:c++latest`. |
| 87 | + target_compile_features(_iris_cxx_common INTERFACE cxx_std_${IRIS_CXX_VERSION_BEFORE_LATEST}) |
| 88 | + endif() |
| 89 | + |
| 90 | +else() # Non-MSVC |
| 91 | + if(DEFINED CMAKE_CXX_STANDARD) |
| 92 | + target_compile_features(_iris_cxx_common INTERFACE cxx_std_${CMAKE_CXX_STANDARD}) |
| 93 | + else() |
| 94 | + target_compile_features(_iris_cxx_common INTERFACE cxx_std_${IRIS_CXX_VERSION_LATEST}) |
| 95 | + endif() |
| 96 | +endif() |
| 97 | + |
| 98 | +unset(IRIS_CXX_VERSION_BEFORE_LATEST) |
| 99 | +unset(IRIS_CXX_FEATURE_BEFORE_LATEST) |
| 100 | + |
| 101 | + |
| 102 | +# ----------------------------------------------------------------- |
| 103 | +# Advanced compile/link settings |
| 104 | + |
| 105 | +if(MSVC) |
| 106 | + # Don't set too strict flags for testing! They must go to `iris_cxx_test`. |
| 107 | + # ABI-dependent configurations MUST be set here. |
| 108 | + target_compile_definitions( |
| 109 | + _iris_cxx_common |
| 110 | + INTERFACE UNICODE _UNICODE |
| 111 | + ) |
| 112 | + target_compile_options( |
| 113 | + _iris_cxx_common |
| 114 | + INTERFACE |
| 115 | + /EHsc /MP /utf-8 /Zc:preprocessor /permissive- |
| 116 | + # $<$<CONFIG:Debug,RelWithDebInfo>:/fsanitize=address> # TODO |
| 117 | + ) |
| 118 | + target_link_options( |
| 119 | + _iris_cxx_common |
| 120 | + INTERFACE |
| 121 | + # $<$<CONFIG:Debug,RelWithDebInfo>:/INCREMENTAL:NO> # TODO |
| 122 | + ) |
| 123 | + |
| 124 | +else() |
| 125 | + target_compile_options( |
| 126 | + _iris_cxx_common |
| 127 | + INTERFACE |
| 128 | + # $<$<CONFIG:Debug>:-fsanitize=undefined,address> # TODO |
| 129 | + ) |
| 130 | + target_link_options( |
| 131 | + _iris_cxx_common |
| 132 | + INTERFACE |
| 133 | + # $<$<CONFIG:Debug>:-fsanitize=undefined,address> # TODO |
| 134 | + ) |
| 135 | +endif() |
| 136 | + |
| 137 | + |
| 138 | +# ----------------------------------------------------------------- |
| 139 | +# Configure Iris main target |
| 140 | + |
| 141 | +file( |
| 142 | + GLOB_RECURSE IRIS_HEADERS |
| 143 | + ${PROJECT_SOURCE_DIR}/include/iris/*.hpp |
| 144 | + ${PROJECT_SOURCE_DIR}/include/iris/*.ipp |
| 145 | +) |
| 146 | + |
| 147 | +target_sources(iris PRIVATE FILE_SET HEADERS TYPE HEADERS FILES ${IRIS_HEADERS}) |
| 148 | +source_group(TREE ${PROJECT_SOURCE_DIR}/include/iris PREFIX iris FILES ${IRIS_HEADERS}) |
| 149 | +target_include_directories(iris INTERFACE ${PROJECT_SOURCE_DIR}/include) |
| 150 | + |
| 151 | + |
| 152 | +# ----------------------------------------------------------------- |
| 153 | +# Test |
| 154 | + |
| 155 | +if(PROJECT_IS_TOP_LEVEL) |
| 156 | + include(CTest) |
| 157 | +endif() |
| 158 | + |
| 159 | +if(BUILD_TESTING) |
| 160 | + add_subdirectory(test) |
| 161 | + |
| 162 | + if(MSVC AND PROJECT_IS_TOP_LEVEL) |
| 163 | + set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT Iris::Iris) |
| 164 | + endif() |
| 165 | +endif() |
0 commit comments