|
| 1 | +macro(setup_repo REPO_OPTION_PREFIX) |
| 2 | + if (NOT DEFINED PROJECT_SOURCE_DIR) |
| 3 | + message(FATAL_ERROR "Must be run inside a project()") |
| 4 | + endif() |
| 5 | + |
| 6 | + # Additional build options |
| 7 | + option(${REPO_OPTION_PREFIX}_DISABLE_DEBUG_POSTFIX "Disable debug ('-debug') postfix" OFF) |
| 8 | + option(${REPO_OPTION_PREFIX}_DEBUG_WARNINGS_AS_ERRORS "Treat debug warnings as errors" OFF) |
| 9 | + option(${REPO_OPTION_PREFIX}_ENABLE_TESTS "Enable unit-tests for ${REPO_OPTION_PREFIX}" ON) |
| 10 | + |
| 11 | + get_filename_component(ROOT_DIR ${CMAKE_SOURCE_DIR} REALPATH) |
| 12 | + |
| 13 | + if (NOT ${PROJECT_SOURCE_DIR} STREQUAL ${ROOT_DIR}) |
| 14 | + set(BUILDING_AS_SUBMODULE ON PARENT_SCOPE) |
| 15 | + message(STATUS "Building as submodule") |
| 16 | + else() |
| 17 | + message(STATUS "Building standalone") |
| 18 | + set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER ".CMakePredefinedTargets") |
| 19 | + set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 20 | + |
| 21 | + get_property(IS_MULTICONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) |
| 22 | + |
| 23 | + message(STATUS "Platform: ${CMAKE_SYSTEM_PROCESSOR} | ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION}") |
| 24 | + message(STATUS "Generator: ${CMAKE_GENERATOR} | ${CMAKE_GENERATOR_PLATFORM}") |
| 25 | + |
| 26 | + if (IS_MULTICONFIG) |
| 27 | + message(STATUS "Configuration types:") |
| 28 | + |
| 29 | + block() |
| 30 | + list(APPEND CMAKE_MESSAGE_INDENT "\t") |
| 31 | + |
| 32 | + foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES}) |
| 33 | + message(STATUS ${CONFIG_TYPE}) |
| 34 | + endforeach() |
| 35 | + endblock() |
| 36 | + else() |
| 37 | + message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") |
| 38 | + endif() |
| 39 | + |
| 40 | + string(TIMESTAMP CONFIGURE_DATE) |
| 41 | + string(TIMESTAMP CURRENT_YEAR "%Y") |
| 42 | + endif() |
| 43 | + |
| 44 | + set(CMAKE_CXX_STANDARD 17) |
| 45 | + if (WIN32) |
| 46 | + add_compile_definitions(NOMINMAX |
| 47 | + _WIN32_WINNT=0x0601 # Windows 7 Compat |
| 48 | + ) |
| 49 | + |
| 50 | + add_compile_definitions(UNICODE _UNICODE) |
| 51 | + endif() |
| 52 | + |
| 53 | + if(NOT CMAKE_DEBUG_POSTFIX AND NOT ${REPO_OPTION_PREFIX}_DISABLE_DEBUG_POSTFIX) |
| 54 | + set(CMAKE_DEBUG_POSTFIX -debug) |
| 55 | + endif() |
| 56 | + |
| 57 | + if (MSVC) |
| 58 | + # As above CMAKE_CXX_STANDARD but for VS |
| 59 | + add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/std:c++17>) |
| 60 | + |
| 61 | + foreach (flag IN ITEMS |
| 62 | + # Set source and execution character sets to UTF-8 |
| 63 | + # https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8 |
| 64 | + /utf-8 |
| 65 | + # Display level 1, level 2, and level 3 warnings, and all level 4 (informational) warnings that aren't off by default. |
| 66 | + # https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level |
| 67 | + /W4 |
| 68 | + # data member 'member1' will be initialized after data member 'member2' |
| 69 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/c5038 |
| 70 | + #/w15038 |
| 71 | + # Supress warnings |
| 72 | + # https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level |
| 73 | + # |
| 74 | + # 'class1' : inherits 'class2::member' via dominance |
| 75 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4250 |
| 76 | + #/wd4250 |
| 77 | + # Your code uses a function, class member, variable, or typedef that's marked deprecated. |
| 78 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996 |
| 79 | + /wd4996 |
| 80 | + # declaration of 'identifier' hides class member |
| 81 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4458 |
| 82 | + /wd4458 |
| 83 | + # nonstandard extension used : nameless struct/union |
| 84 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4201 |
| 85 | + #/wd4201 |
| 86 | + # unreachable code |
| 87 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4702 |
| 88 | + #/wd4702 |
| 89 | + # declaration of 'identifier' hides global declaration |
| 90 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4459 |
| 91 | + #/wd4459 |
| 92 | + # 'function' : unreferenced local function has been removed |
| 93 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4505 |
| 94 | + #/wd4505 |
| 95 | + # conditional expression is constant |
| 96 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4127 |
| 97 | + #/wd4127 |
| 98 | + # assignment within conditional expression |
| 99 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4706 |
| 100 | + #/wd4706 |
| 101 | + # loss of data / precision, unsigned <--> signed |
| 102 | + # |
| 103 | + # 'argument' : conversion from 'type1' to 'type2', possible loss of data |
| 104 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4244 |
| 105 | + /wd4244 |
| 106 | + # 'var' : conversion from 'size_t' to 'type', possible loss of data |
| 107 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4267 |
| 108 | + #/wd4267 |
| 109 | + # 'identifier' : unreferenced formal parameter |
| 110 | + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4100 |
| 111 | + /wd4100 |
| 112 | + ) |
| 113 | + add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:${flag}>) |
| 114 | + endforeach() |
| 115 | + |
| 116 | + if (NOT OPENDAQ_MSVC_SINGLE_PROCESS_BUILD) |
| 117 | + # Build with multiple processes |
| 118 | + # https://learn.microsoft.com/en-us/cpp/build/reference/mp-build-with-multiple-processes |
| 119 | + add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/MP>) |
| 120 | + endif() |
| 121 | + |
| 122 | + # Treat warnings as errors if not Debug or OPENDAQ_DEBUG_WARNINGS_AS_ERRORS is ON |
| 123 | + add_compile_options($<$<OR:$<NOT:$<CONFIG:Debug>>,$<BOOL:${${REPO_OPTION_PREFIX}_DEBUG_WARNINGS_AS_ERRORS}>>:/WX>) |
| 124 | + |
| 125 | + add_compile_definitions($<$<CONFIG:Debug>:_DEBUG>) |
| 126 | + |
| 127 | + if (MSVC_VERSION GREATER_EQUAL 1910) |
| 128 | + # /Zc:__cplusplus forces MSVC to use the correct value of __cplusplus macro (otherwise always C++98) |
| 129 | + add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>) |
| 130 | + if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") |
| 131 | + # /Zf (Faster PDB generation) is not supported by ClangCL |
| 132 | + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Zf") |
| 133 | + endif() |
| 134 | + |
| 135 | + # Produce diagnostic messages with exact location |
| 136 | + add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/diagnostics:caret>) |
| 137 | + endif() |
| 138 | + |
| 139 | + # set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /ignore:4221") |
| 140 | + # set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /ignore:4221") |
| 141 | + # set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221") |
| 142 | + endif() |
| 143 | + |
| 144 | + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 145 | + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 146 | + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 147 | + |
| 148 | + if (${REPO_OPTION_PREFIX}_ENABLE_TESTS) |
| 149 | + set(OPENDAQ_ENABLE_TEST_UTILS ON CACHE BOOL "Enable testing utils library") |
| 150 | + endif() |
| 151 | +endmacro() |
0 commit comments