|
| 1 | +macro(get_compiler_id) |
| 2 | + # Get lowercase compiler id |
| 3 | + string(TOLOWER "${CMAKE_CXX_COMPILER_ID}" cmake_cxx_compiler_id) |
| 4 | + if(cmake_cxx_compiler_id STREQUAL "gnu") |
| 5 | + set(cmake_cxx_compiler_id "gcc") |
| 6 | + endif() |
| 7 | +endmacro() |
| 8 | + |
| 9 | +# if set toolchain file, don't build, and add include path and link path |
| 10 | +if(CMAKE_TOOLCHAIN_FILE) |
| 11 | + set(BUILD_DEPENDENCIES |
| 12 | + OFF |
| 13 | + CACHE BOOL "Build Dependencies" FORCE |
| 14 | + ) |
| 15 | + # toolchain file added include path and link path, use it |
| 16 | + target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_INCLUDE_PATH}) |
| 17 | + target_link_directories(${PROJECT_NAME} PRIVATE ${CMAKE_LIBRARY_PATH}) |
| 18 | +endif() |
| 19 | + |
| 20 | +if(BUILD_DEPENDENCIES) |
| 21 | + message(STATUS "Building Dependencies") |
| 22 | + |
| 23 | + # CMake >= 3.14 |
| 24 | + if(${CMAKE_VERSION} VERSION_LESS "3.14") |
| 25 | + message(FATAL_ERROR "CMake >= 3.14 required") |
| 26 | + endif() |
| 27 | + |
| 28 | + include(FetchContent) |
| 29 | + # show progress |
| 30 | + set(FETCHCONTENT_QUIET OFF) |
| 31 | + set(FETCHCONTENT_UPDATES_DISCONNECTED ON) |
| 32 | + |
| 33 | + # disable CMP0135 warning |
| 34 | + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24") |
| 35 | + cmake_policy(SET CMP0135 NEW) |
| 36 | + endif() |
| 37 | + |
| 38 | + # 💡Using URL is faster than git clone |
| 39 | + |
| 40 | + # GTest/GoogleTest |
| 41 | + if(BUILD_TESTING) |
| 42 | + #[[ |
| 43 | + # Can't use name ⚠️GTest/gtest⚠️ in FetchContent_Declare, see: |
| 44 | + # CMake FetchContent not working as expected (https://github.com/google/googletest/issues/2457) |
| 45 | + # FetchContent_* does not work with GTest as dependency name on Windows (https://gitlab.kitware.com/cmake/cmake/-/issues/25294) |
| 46 | + # The name gtest reserved: |
| 47 | + # googletest_SOURCE_DIR: build/_deps/googletest-src |
| 48 | + # gtest_SOURCE_DIR: build/_deps/googletest-src/googletest |
| 49 | + #]] |
| 50 | + FetchContent_Declare(googletest URL "https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz") |
| 51 | + # For Windows: Prevent overriding the parent project's compiler/linker ⚠️MT/MD⚠️ settings |
| 52 | + set(gtest_force_shared_crt |
| 53 | + ON |
| 54 | + CACHE BOOL "" FORCE |
| 55 | + ) |
| 56 | + FetchContent_MakeAvailable(googletest) |
| 57 | + |
| 58 | + # Set build output path, this is mainly for Windows, to solve the test/unittests running 0x000135 problems |
| 59 | + set_standard_build_output(gtest) |
| 60 | + set_standard_build_output(gtest_main) |
| 61 | + set_standard_build_output(gmock) |
| 62 | + set_standard_build_output(gmock_main) |
| 63 | + endif() |
| 64 | + |
| 65 | + # spdlog |
| 66 | + if(TRANTOR_USE_SPDLOG) |
| 67 | + FetchContent_Declare(fmt URL "https://github.com/fmtlib/fmt/archive/refs/tags/10.2.1.tar.gz") |
| 68 | + FetchContent_Declare(spdlog URL "https://github.com/gabime/spdlog/archive/refs/tags/v1.13.0.tar.gz") |
| 69 | + FetchContent_MakeAvailable(fmt spdlog) |
| 70 | + |
| 71 | + # Set build output path, this is mainly for Windows, to solve the test/unittests running 0x000135 problems |
| 72 | + set_standard_build_output(fmt) |
| 73 | + set_standard_build_output(spdlog) |
| 74 | + endif() |
| 75 | + |
| 76 | + # c-ares |
| 77 | + if(TRANTOR_USE_C-ARES) |
| 78 | + FetchContent_Declare(c-ares URL "https://github.com/c-ares/c-ares/archive/refs/tags/cares-1_25_0.tar.gz") |
| 79 | + FetchContent_MakeAvailable(c-ares) |
| 80 | + |
| 81 | + # Set build output path, this is mainly for Windows, to solve the test/unittests running 0x000135 problems |
| 82 | + set_standard_build_output(c-ares) |
| 83 | + endif() |
| 84 | + |
| 85 | + # OpenSSL |
| 86 | + if(TRANTOR_TLS_PROVIDER STREQUAL "openssl") |
| 87 | + # openssl build need perl |
| 88 | + find_package(Perl REQUIRED) |
| 89 | + |
| 90 | + FetchContent_Declare(OpenSSL URL "https://github.com/openssl/openssl/archive/refs/tags/openssl-3.2.0.tar.gz") |
| 91 | + FetchContent_MakeAvailable(OpenSSL) |
| 92 | + |
| 93 | + # Set OPENSSL_ROOT_DIR, to let find_package could search from the build directory |
| 94 | + set(OPENSSL_ROOT_DIR |
| 95 | + ${FETCHCONTENT_BASE_DIR}/openssl |
| 96 | + CACHE PATH "Let find_package use the directory to find" FORCE |
| 97 | + ) |
| 98 | + |
| 99 | + if(NOT EXISTS ${OPENSSL_ROOT_DIR}/include/openssl) |
| 100 | + # ⚠️ remove "no-asm" flags to improve openssl performance |
| 101 | + if(CMAKE_CXX_COMPILER_ID MATCHES MSVC) |
| 102 | + execute_process( |
| 103 | + COMMAND ${PERL_EXECUTABLE} ${openssl_SOURCE_DIR}/Configure VC-WIN64A no-asm no-tests |
| 104 | + --prefix=${OPENSSL_ROOT_DIR} WORKING_DIRECTORY ${openssl_BINARY_DIR} |
| 105 | + ) |
| 106 | + execute_process(COMMAND nmake WORKING_DIRECTORY ${openssl_BINARY_DIR}) |
| 107 | + execute_process(COMMAND nmake install WORKING_DIRECTORY ${openssl_BINARY_DIR}) |
| 108 | + else() |
| 109 | + execute_process( |
| 110 | + COMMAND ${openssl_SOURCE_DIR}/config no-asm no-tests --prefix=${OPENSSL_ROOT_DIR} |
| 111 | + WORKING_DIRECTORY ${openssl_BINARY_DIR} |
| 112 | + ) |
| 113 | + execute_process(COMMAND make -j WORKING_DIRECTORY ${openssl_BINARY_DIR}) |
| 114 | + execute_process(COMMAND make install WORKING_DIRECTORY ${openssl_BINARY_DIR}) |
| 115 | + endif() # compiler id |
| 116 | + endif() |
| 117 | + |
| 118 | + target_include_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_ROOT_DIR}/include) |
| 119 | + # copy dlls, this is mainly for Windows, to solve the test/unittests running 0x000135 problems |
| 120 | + copy_dlls_to_standard_build_output(${OPENSSL_ROOT_DIR}/bin) |
| 121 | + |
| 122 | + endif() |
| 123 | + |
| 124 | + # Botan-3 |
| 125 | + #[[ |
| 126 | + # ⚠️⚠️⚠️Botan3 is now a C++20 codebase; compiler requirements have been increased to GCC 11, Clang 14, or MSVC 2022. (GH #2455 #3086) |
| 127 | + # support ninja build after botan 3.2 |
| 128 | + #]] |
| 129 | + if(TRANTOR_TLS_PROVIDER STREQUAL "botan-3") |
| 130 | + # botan build need python |
| 131 | + find_package(PythonInterp REQUIRED) |
| 132 | + |
| 133 | + FetchContent_Declare(Botan URL "https://github.com/randombit/botan/archive/refs/tags/3.2.0.tar.gz") |
| 134 | + FetchContent_MakeAvailable(Botan) |
| 135 | + |
| 136 | + set(BOTAN_ROOT_DIR |
| 137 | + ${FETCHCONTENT_BASE_DIR}/botan-3 |
| 138 | + CACHE PATH "Let find_package use the directory to find" FORCE |
| 139 | + ) |
| 140 | + |
| 141 | + if(NOT EXISTS ${BOTAN_ROOT_DIR}/include/botan-3/botan) |
| 142 | + # Get lowercase compiler id |
| 143 | + get_compiler_id() |
| 144 | + |
| 145 | + if(CMAKE_CXX_COMPILER_ID MATCHES MSVC) |
| 146 | + # ⚠️botan-3 support ninja build |
| 147 | + execute_process( |
| 148 | + COMMAND ${PYTHON_EXECUTABLE} ${botan_SOURCE_DIR}/configure.py --prefix=${BOTAN_ROOT_DIR} |
| 149 | + --cc=${cmake_cxx_compiler_id} --build-tool=ninja --with-pkg-config --build-target=shared |
| 150 | + WORKING_DIRECTORY ${botan_BINARY_DIR} |
| 151 | + ) |
| 152 | + execute_process(COMMAND ninja -j WORKING_DIRECTORY ${botan_BINARY_DIR}) |
| 153 | + execute_process(COMMAND ninja install WORKING_DIRECTORY ${botan_BINARY_DIR}) |
| 154 | + else() |
| 155 | + execute_process( |
| 156 | + COMMAND ${PYTHON_EXECUTABLE} ${botan_SOURCE_DIR}/configure.py --prefix=${BOTAN_ROOT_DIR} |
| 157 | + --cc=${cmake_cxx_compiler_id} --with-pkg-config --build-target=shared |
| 158 | + WORKING_DIRECTORY ${botan_BINARY_DIR} |
| 159 | + ) |
| 160 | + execute_process(COMMAND make -j WORKING_DIRECTORY ${botan_BINARY_DIR}) |
| 161 | + execute_process(COMMAND make install WORKING_DIRECTORY ${botan_BINARY_DIR}) |
| 162 | + endif() # compiler id |
| 163 | + endif() |
| 164 | + |
| 165 | + target_include_directories(${PROJECT_NAME} PRIVATE ${BOTAN_ROOT_DIR}/include/botan-3) |
| 166 | + # copy dlls, this is mainly for Windows, to solve the test/unittests running 0x000135 problems |
| 167 | + copy_dlls_to_standard_build_output(${BOTAN_ROOT_DIR}/bin) |
| 168 | + |
| 169 | + endif() |
| 170 | + |
| 171 | +endif() |
0 commit comments