github.com/moontrade/mdbx-go@v0.4.0/CMakeLists.txt (about) 1 ## 2 ## Copyright 2020-2022 Leonid Yuriev <leo@yuriev.ru> 3 ## and other libmdbx authors: please see AUTHORS file. 4 ## All rights reserved. 5 ## 6 ## Redistribution and use in source and binary forms, with or without 7 ## modification, are permitted only as authorized by the OpenLDAP 8 ## Public License. 9 ## 10 ## A copy of this license is available in the file LICENSE in the 11 ## top-level directory of the distribution or, alternatively, at 12 ## <http://www.OpenLDAP.org/license.html>. 13 ## 14 15 ## 16 ## libmdbx = { Revised and extended descendant of Symas LMDB. } 17 ## Please see README.md at https://gitflic.ru/project/erthink/libmdbx 18 ## 19 ## Libmdbx is superior to LMDB in terms of features and reliability, 20 ## not inferior in performance. libmdbx works on Linux, FreeBSD, MacOS X 21 ## and other systems compliant with POSIX.1-2008, but also support Windows 22 ## as a complementary platform. 23 ## 24 ## The next version is under active non-public development and will be 25 ## released as MithrilDB and libmithrildb for libraries & packages. 26 ## Admittedly mythical Mithril is resembling silver but being stronger and 27 ## lighter than steel. Therefore MithrilDB is rightly relevant name. 28 ## 29 ## MithrilDB will be radically different from libmdbx by the new database 30 ## format and API based on C++17, as well as the Apache 2.0 License. 31 ## The goal of this revolution is to provide a clearer and robust API, 32 ## add more features and new valuable properties of database. 33 ## 34 ## The Future will (be) Positive. Всё будет хорошо. 35 ## 36 37 if(CMAKE_VERSION VERSION_LESS 3.12) 38 cmake_minimum_required(VERSION 3.8.2) 39 else() 40 cmake_minimum_required(VERSION 3.12) 41 endif() 42 43 cmake_policy(PUSH) 44 cmake_policy(VERSION ${CMAKE_MINIMUM_REQUIRED_VERSION}) 45 if(NOT CMAKE_VERSION VERSION_LESS 3.15) 46 cmake_policy(SET CMP0091 NEW) 47 endif() 48 if(NOT CMAKE_VERSION VERSION_LESS 3.13) 49 cmake_policy(SET CMP0077 NEW) 50 endif() 51 if(NOT CMAKE_VERSION VERSION_LESS 3.12) 52 cmake_policy(SET CMP0075 NEW) 53 endif() 54 if(NOT CMAKE_VERSION VERSION_LESS 3.9) 55 cmake_policy(SET CMP0069 NEW) 56 include(CheckIPOSupported) 57 check_ipo_supported(RESULT CMAKE_INTERPROCEDURAL_OPTIMIZATION_AVAILABLE) 58 else() 59 set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_AVAILABLE FALSE) 60 endif() 61 62 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git" AND 63 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt" AND 64 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/core.c" AND 65 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/alloy.c" AND 66 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.in" AND 67 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/version.c.in" AND 68 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/man1" AND 69 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/mdbx_chk.c" AND 70 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/mdbx.c++") 71 set(MDBX_AMALGAMATED_SOURCE FALSE) 72 find_program(GIT git) 73 if(NOT GIT) 74 message(SEND_ERROR "Git command-line tool not found") 75 endif() 76 set(MDBX_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") 77 elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION.txt" AND 78 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/mdbx.c" AND 79 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/mdbx.c++" AND 80 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/config.h.in" AND 81 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/man1" AND 82 EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/mdbx_chk.c") 83 set(MDBX_AMALGAMATED_SOURCE TRUE) 84 set(MDBX_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") 85 else() 86 message(FATAL_ERROR "\n" 87 "Please don't use tarballs nor zips which are automatically provided by Github! " 88 "These archives do not contain version information and thus are unfit to build libmdbx. " 89 "You can vote for ability of disabling auto-creation such unsuitable archives at https://github.community/t/disable-tarball\n" 90 "Instead of above, just clone the git repository, either download a tarball or zip with the properly amalgamated source core. " 91 "For embedding libmdbx use a git-submodule or the amalgamated source code.\n" 92 "Please, avoid using any other techniques.") 93 endif() 94 95 if(DEFINED PROJECT_NAME) 96 option(MDBX_FORCE_BUILD_AS_MAIN_PROJECT "Force libmdbx to full control build options even it added as a subdirectory to your project." OFF) 97 endif() 98 99 if(DEFINED PROJECT_NAME AND NOT MDBX_FORCE_BUILD_AS_MAIN_PROJECT) 100 set(SUBPROJECT ON) 101 set(NOT_SUBPROJECT OFF) 102 if(NOT MDBX_AMALGAMATED_SOURCE AND NOT DEFINED BUILD_TESTING) 103 set(BUILD_TESTING OFF) 104 endif() 105 enable_language(C) 106 else() 107 set(SUBPROJECT OFF) 108 set(NOT_SUBPROJECT ON) 109 project(libmdbx C) 110 if(NOT MDBX_AMALGAMATED_SOURCE AND NOT DEFINED BUILD_TESTING) 111 set(BUILD_TESTING ON) 112 endif() 113 endif() 114 115 if(NOT MDBX_AMALGAMATED_SOURCE) 116 include(CTest) 117 option(MDBX_ENABLE_TESTS "Build libmdbx tests." ${BUILD_TESTING}) 118 elseif(DEFINED MDBX_ENABLE_TESTS AND MDBX_ENABLE_TESTS) 119 message(WARNING "MDBX_ENABLE_TESTS=${MDBX_ENABLE_TESTS}: But amalgamated source code don't includes tests.") 120 set(MDBX_ENABLE_TESTS OFF) 121 endif() 122 123 # Try to find a C++ compiler unless sure that this is unnecessary. 124 if (NOT CMAKE_CXX_COMPILER_LOADED) 125 include(CheckLanguage) 126 if(NOT DEFINED MDBX_BUILD_CXX OR MDBX_BUILD_CXX 127 OR (NOT MDBX_AMALGAMATED_SOURCE AND (NOT DEFINED MDBX_ENABLE_TESTS OR MDBX_ENABLE_TESTS))) 128 check_language(CXX) 129 if(CMAKE_CXX_COMPILER) 130 enable_language(CXX) 131 endif() 132 endif() 133 else() 134 enable_language(CXX) 135 endif() 136 137 # Set default build type to Release. This is to ease a User's life. 138 if(NOT CMAKE_BUILD_TYPE) 139 set(CMAKE_BUILD_TYPE Release CACHE STRING 140 "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." 141 FORCE) 142 endif() 143 string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPERCASE) 144 145 if(NOT_SUBPROJECT AND (CMAKE_CROSSCOMPILING OR IOS)) 146 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) 147 endif() 148 149 if(NOT "$ENV{TEAMCITY_PROCESS_FLOW_ID}" STREQUAL "") 150 set(CI TEAMCITY) 151 message(STATUS "TeamCity CI") 152 elseif(NOT "$ENV{TRAVIS}" STREQUAL "") 153 set(CI TRAVIS) 154 message(STATUS "Travis CI") 155 elseif(NOT "$ENV{CIRCLECI}" STREQUAL "") 156 set(CI CIRCLE) 157 message(STATUS "Circle CI") 158 elseif(NOT "$ENV{APPVEYOR}" STREQUAL "") 159 set(CI APPVEYOR) 160 message(STATUS "AppVeyor CI") 161 elseif(NOT "$ENV{CI}" STREQUAL "") 162 set(CI "$ENV{CI}") 163 message(STATUS "Other CI (${CI})") 164 else() 165 message(STATUS "Assume No any CI environment") 166 unset(CI) 167 endif() 168 169 # output all mdbx-related targets in single directory 170 if(NOT DEFINED MDBX_OUTPUT_DIR) 171 set(MDBX_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) 172 endif() 173 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${MDBX_OUTPUT_DIR}) 174 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MDBX_OUTPUT_DIR}) 175 set(CMAKE_PDB_OUTPUT_DIRECTORY ${MDBX_OUTPUT_DIR}) 176 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${MDBX_OUTPUT_DIR}) 177 178 include(CheckFunctionExists) 179 include(FindPackageMessage) 180 include(GNUInstallDirs) 181 182 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION LESS 1900) 183 message(SEND_ERROR "MSVC compiler ${MSVC_VERSION} is too old for building MDBX." 184 " At least 'Microsoft Visual Studio 2015' is required.") 185 endif() 186 187 set(CMAKE_THREAD_PREFER_PTHREAD TRUE) 188 set(THREADS_PREFER_PTHREAD_FLAG TRUE) 189 find_package(Threads REQUIRED) 190 191 include(cmake/utils.cmake) 192 include(cmake/compiler.cmake) 193 include(cmake/profile.cmake) 194 195 # Workaround for `-pthread` toolchain/cmake bug 196 if(NOT APPLE AND NOT MSVC 197 AND CMAKE_USE_PTHREADS_INIT AND NOT CMAKE_THREAD_LIBS_INIT 198 AND (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)) 199 check_compiler_flag("-pthread" CC_HAS_PTHREAD) 200 if(CC_HAS_PTHREAD AND NOT CMAKE_EXE_LINKER_FLAGS MATCHES "-pthread") 201 message(STATUS "Force add -pthread for linker flags to avoid troubles") 202 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") 203 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pthread") 204 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -pthread") 205 endif() 206 endif() 207 208 CHECK_FUNCTION_EXISTS(pow NOT_NEED_LIBM) 209 if(NOT_NEED_LIBM) 210 set(LIB_MATH "") 211 else() 212 set(CMAKE_REQUIRED_LIBRARIES m) 213 CHECK_FUNCTION_EXISTS(pow HAVE_LIBM) 214 if(HAVE_LIBM) 215 set(LIB_MATH m) 216 else() 217 message(FATAL_ERROR "No libm found for math support") 218 endif() 219 endif() 220 221 if(SUBPROJECT) 222 if(NOT DEFINED BUILD_SHARED_LIBS) 223 option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)" OFF) 224 endif() 225 if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE) 226 option(CMAKE_POSITION_INDEPENDENT_CODE "Generate position independent (PIC)" ON) 227 endif() 228 else() 229 option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)" ON) 230 option(CMAKE_POSITION_INDEPENDENT_CODE "Generate position independent (PIC)" ON) 231 if (CC_HAS_ARCH_NATIVE) 232 option(BUILD_FOR_NATIVE_CPU "Generate code for the compiling machine CPU" OFF) 233 endif() 234 235 if(CMAKE_CONFIGURATION_TYPES OR NOT CMAKE_BUILD_TYPE_UPPERCASE STREQUAL "DEBUG") 236 set(INTERPROCEDURAL_OPTIMIZATION_DEFAULT ON) 237 else() 238 set(INTERPROCEDURAL_OPTIMIZATION_DEFAULT OFF) 239 endif() 240 241 if(CMAKE_INTERPROCEDURAL_OPTIMIZATION_AVAILABLE 242 OR GCC_LTO_AVAILABLE OR MSVC_LTO_AVAILABLE OR 243 (CLANG_LTO_AVAILABLE AND 244 ((DEFINED MDBX_ENABLE_TESTS AND NOT MDBX_ENABLE_TESTS) 245 OR NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.0))) 246 option(INTERPROCEDURAL_OPTIMIZATION "Enable interprocedural/LTO optimization" ${INTERPROCEDURAL_OPTIMIZATION_DEFAULT}) 247 else() 248 set(INTERPROCEDURAL_OPTIMIZATION OFF) 249 endif() 250 251 if(INTERPROCEDURAL_OPTIMIZATION) 252 if(GCC_LTO_AVAILABLE) 253 set(LTO_ENABLED TRUE) 254 set(CMAKE_AR ${CMAKE_GCC_AR} CACHE PATH "Path to ar program with LTO-plugin" FORCE) 255 set(CMAKE_NM ${CMAKE_GCC_NM} CACHE PATH "Path to nm program with LTO-plugin" FORCE) 256 set(CMAKE_RANLIB ${CMAKE_GCC_RANLIB} CACHE PATH "Path to ranlib program with LTO-plugin" FORCE) 257 message(STATUS "MDBX indulge Link-Time Optimization by GCC") 258 elseif(CLANG_LTO_AVAILABLE) 259 set(LTO_ENABLED TRUE) 260 set(CMAKE_AR ${CMAKE_CLANG_AR} CACHE PATH "Path to ar program with LTO-plugin" FORCE) 261 set(CMAKE_NM ${CMAKE_CLANG_NM} CACHE PATH "Path to nm program with LTO-plugin" FORCE) 262 set(CMAKE_RANLIB ${CMAKE_CLANG_RANLIB} CACHE PATH "Path to ranlib program with LTO-plugin" FORCE) 263 message(STATUS "MDBX indulge Link-Time Optimization by CLANG") 264 elseif(MSVC_LTO_AVAILABLE) 265 set(LTO_ENABLED TRUE) 266 message(STATUS "MDBX indulge Link-Time Optimization by MSVC") 267 elseif(CMAKE_INTERPROCEDURAL_OPTIMIZATION_AVAILABLE) 268 message(STATUS "MDBX indulge Interprocedural Optimization by CMake") 269 set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) 270 set(LTO_ENABLED TRUE) 271 else() 272 message(WARNING "Unable to engage interprocedural/LTO optimization.") 273 endif() 274 else() 275 set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE) 276 set(LTO_ENABLED FALSE) 277 endif() 278 279 if(NOT MDBX_AMALGAMATED_SOURCE) 280 find_program(VALGRIND valgrind) 281 if(VALGRIND) 282 # LY: cmake is ugly and nasty. 283 # - therefore memcheck-options should be defined before including ctest; 284 # - otherwise ctest may ignore it. 285 set(MEMORYCHECK_SUPPRESSIONS_FILE 286 "${CMAKE_CURRENT_SOURCE_DIR}/test/valgrind_suppress.txt" 287 CACHE FILEPATH "Suppressions file for Valgrind" FORCE) 288 set(MEMORYCHECK_COMMAND_OPTIONS 289 "--trace-children=yes --leak-check=full --track-origins=yes --error-exitcode=42 --error-markers=@ --errors-for-leak-kinds=definite --fair-sched=yes --suppressions=${MEMORYCHECK_SUPPRESSIONS_FILE}" 290 CACHE STRING "Valgrind options" FORCE) 291 set(VALGRIND_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS}" CACHE STRING "Valgrind options" FORCE) 292 endif() 293 294 # Enable 'make tags' target. 295 find_program(CTAGS ctags) 296 if(CTAGS) 297 add_custom_target(tags COMMAND ${CTAGS} -R -f tags 298 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) 299 add_custom_target(ctags DEPENDS tags) 300 endif(CTAGS) 301 302 if(UNIX) 303 find_program(CLANG_FORMAT 304 NAMES clang-format-13 clang-format) 305 if(CLANG_FORMAT) 306 execute_process(COMMAND ${CLANG_FORMAT} "--version" OUTPUT_VARIABLE clang_format_version_info) 307 string(REGEX MATCH "version ([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)?" clang_format_version_info CLANG_FORMAT_VERSION) 308 if(clang_format_version_info AND NOT CLANG_FORMAT_VERSION VERSION_LESS 13.0) 309 # Enable 'make reformat' target. 310 add_custom_target(reformat 311 VERBATIM 312 COMMAND 313 git ls-files | 314 grep -E \\.\(c|cxx|cc|cpp|h|hxx|hpp\)\(\\.in\)?\$ | 315 xargs ${CLANG_FORMAT} -i --style=file 316 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) 317 endif() 318 endif() 319 endif() 320 321 if(NOT "${PROJECT_BINARY_DIR}" STREQUAL "${PROJECT_SOURCE_DIR}") 322 add_custom_target(distclean) 323 add_custom_command(TARGET distclean 324 COMMAND ${CMAKE_COMMAND} -E remove_directory "${PROJECT_BINARY_DIR}" 325 COMMENT "Removing the build directory and its content") 326 elseif(IS_DIRECTORY .git AND GIT) 327 add_custom_target(distclean) 328 add_custom_command(TARGET distclean 329 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} 330 COMMAND ${GIT} submodule foreach --recursive git clean -f -X -d 331 COMMAND ${GIT} clean -f -X -d 332 COMMENT "Removing all build files from the source directory") 333 endif() 334 endif(NOT MDBX_AMALGAMATED_SOURCE) 335 336 setup_compile_flags() 337 endif(SUBPROJECT) 338 339 list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 HAS_C11) 340 list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_11 HAS_CXX11) 341 list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_14 HAS_CXX14) 342 list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_17 HAS_CXX17) 343 list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_20 HAS_CXX20) 344 list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_23 HAS_CXX23) 345 if(NOT DEFINED MDBX_CXX_STANDARD) 346 if(DEFINED ENV{CMAKE_CXX_STANDARD}) 347 set(CMAKE_CXX_STANDARD $ENV{CMAKE_CXX_STANDARD}) 348 endif() 349 if(DEFINED CMAKE_CXX_STANDARD) 350 set(MDBX_CXX_STANDARD ${CMAKE_CXX_STANDARD}) 351 elseif(NOT HAS_CXX23 LESS 0 352 AND NOT (CMAKE_COMPILER_IS_CLANG AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12)) 353 set(MDBX_CXX_STANDARD 23) 354 elseif(NOT HAS_CXX20 LESS 0 355 AND NOT (CMAKE_COMPILER_IS_CLANG AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10)) 356 set(MDBX_CXX_STANDARD 20) 357 elseif(NOT HAS_CXX17 LESS 0 358 AND NOT (CMAKE_COMPILER_IS_CLANG AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5)) 359 set(MDBX_CXX_STANDARD 17) 360 elseif(NOT HAS_CXX14 LESS 0) 361 set(MDBX_CXX_STANDARD 14) 362 elseif(NOT HAS_CXX11 LESS 0) 363 set(MDBX_CXX_STANDARD 11) 364 else() 365 set(MDBX_CXX_STANDARD 98) 366 endif() 367 endif() 368 if(NOT DEFINED MDBX_C_STANDARD) 369 # MSVC >= 19.28 (Microsoft Visual Studio 16.8) is mad! 370 # It unable process Windows SDK headers in the C11 mode! 371 if(MSVC AND MSVC_VERSION GREATER 1927 AND NOT MSVC_VERSION GREATER 1929) 372 set(MDBX_C_STANDARD 99) 373 set(C_FALLBACK_11 OFF) 374 set(C_FALLBACK_GNU11 OFF) 375 elseif(HAS_C11 LESS 0 AND NOT C_FALLBACK_GNU11 AND NOT C_FALLBACK_11) 376 set(MDBX_C_STANDARD 99) 377 else() 378 set(MDBX_C_STANDARD 11) 379 endif() 380 endif() 381 382 if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows" AND EXISTS "${MDBX_SOURCE_DIR}/ntdll.def") 383 if(MSVC) 384 if(NOT MSVC_LIB_EXE) 385 # Find lib.exe 386 get_filename_component(CL_NAME ${CMAKE_C_COMPILER} NAME) 387 string(REPLACE cl.exe lib.exe MSVC_LIB_EXE ${CL_NAME}) 388 find_program(MSVC_LIB_EXE ${MSVC_LIB_EXE}) 389 endif() 390 if(MSVC_LIB_EXE) 391 message(STATUS "Found MSVC's lib tool: ${MSVC_LIB_EXE}") 392 set(MDBX_NTDLL_EXTRA_IMPLIB "${CMAKE_CURRENT_BINARY_DIR}/mdbx_ntdll_extra.lib") 393 add_custom_command(OUTPUT "${MDBX_NTDLL_EXTRA_IMPLIB}" 394 COMMENT "Create extra-import-library for ntdll.dll" 395 MAIN_DEPENDENCY "${MDBX_SOURCE_DIR}/ntdll.def" 396 COMMAND ${MSVC_LIB_EXE} /def:"${MDBX_SOURCE_DIR}/ntdll.def" /out:"${MDBX_NTDLL_EXTRA_IMPLIB}" ${INITIAL_CMAKE_STATIC_LINKER_FLAGS}) 397 else() 398 message(WARNING "MSVC's lib tool not found") 399 endif() 400 elseif(MINGW OR MINGW64) 401 if(NOT DLLTOOL) 402 # Find dlltool 403 get_filename_component(GCC_NAME ${CMAKE_C_COMPILER} NAME) 404 string(REPLACE gcc dlltool DLLTOOL_NAME ${GCC_NAME}) 405 find_program(DLLTOOL NAMES ${DLLTOOL_NAME}) 406 endif() 407 if(DLLTOOL) 408 message(STATUS "Found dlltool: ${DLLTOOL}") 409 set(MDBX_NTDLL_EXTRA_IMPLIB "${CMAKE_CURRENT_BINARY_DIR}/mdbx_ntdll_extra.a") 410 add_custom_command(OUTPUT "${MDBX_NTDLL_EXTRA_IMPLIB}" 411 COMMENT "Create extra-import-library for ntdll.dll" 412 MAIN_DEPENDENCY "${MDBX_SOURCE_DIR}/ntdll.def" 413 COMMAND ${DLLTOOL} -d "${MDBX_SOURCE_DIR}/ntdll.def" -l "${MDBX_NTDLL_EXTRA_IMPLIB}") 414 else() 415 message(WARNING "dlltool not found") 416 endif() 417 endif() 418 419 if(MDBX_NTDLL_EXTRA_IMPLIB) 420 # LY: Sometimes CMake requires a nightmarish magic for simple things. 421 # 1) create a target out of the library compilation result 422 add_custom_target(ntdll_extra_target DEPENDS "${MDBX_NTDLL_EXTRA_IMPLIB}") 423 # 2) create an library target out of the library compilation result 424 add_library(ntdll_extra STATIC IMPORTED GLOBAL) 425 add_dependencies(ntdll_extra ntdll_extra_target) 426 # 3) specify where the library is (and where to find the headers) 427 set_target_properties(ntdll_extra 428 PROPERTIES 429 IMPORTED_LOCATION "${MDBX_NTDLL_EXTRA_IMPLIB}") 430 endif() 431 endif() 432 433 ################################################################################ 434 ################################################################################ 435 # 436 # #### ##### ##### # #### # # #### 437 # # # # # # # # # ## # # 438 # # # # # # # # # # # # #### 439 # # # ##### # # # # # # # # 440 # # # # # # # # # ## # # 441 # #### # # # #### # # #### 442 # 443 444 set(MDBX_BUILD_OPTIONS ENABLE_UBSAN ENABLE_ASAN MDBX_USE_VALGRIND ENABLE_GPROF ENABLE_GCOV) 445 macro(add_mdbx_option NAME DESCRIPTION DEFAULT) 446 list(APPEND MDBX_BUILD_OPTIONS ${NAME}) 447 if(NOT ${DEFAULT} STREQUAL "AUTO") 448 option(${NAME} "${DESCRIPTION}" ${DEFAULT}) 449 elseif(NOT DEFINED ${NAME}) 450 set(${NAME}_AUTO ON) 451 endif() 452 endmacro() 453 454 if(IOS) 455 set(MDBX_BUILD_TOOLS_DEFAULT OFF) 456 if(NOT_SUBPROJECT) 457 cmake_policy(SET CMP0006 OLD) 458 set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO") 459 endif() 460 else() 461 set(MDBX_BUILD_TOOLS_DEFAULT ON) 462 endif() 463 464 add_mdbx_option(MDBX_INSTALL_STATIC "Build and install libmdbx for static linking" OFF) 465 add_mdbx_option(MDBX_BUILD_SHARED_LIBRARY "Build libmdbx as shared library (DLL)" ${BUILD_SHARED_LIBS}) 466 add_mdbx_option(MDBX_BUILD_TOOLS "Build MDBX tools (mdbx_chk/stat/dump/load/copy)" ${MDBX_BUILD_TOOLS_DEFAULT}) 467 CMAKE_DEPENDENT_OPTION(MDBX_INSTALL_MANPAGES "Install man-pages for MDBX tools (mdbx_chk/stat/dump/load/copy)" ON MDBX_BUILD_TOOLS OFF) 468 add_mdbx_option(MDBX_TXN_CHECKOWNER "Checking transaction matches the calling thread inside libmdbx's API" ON) 469 add_mdbx_option(MDBX_ENV_CHECKPID "Paranoid checking PID inside libmdbx's API" AUTO) 470 mark_as_advanced(MDBX_ENV_CHECKPID) 471 if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") 472 add_mdbx_option(MDBX_DISABLE_GNU_SOURCE "Don't use GNU/Linux libc extensions" OFF) 473 mark_as_advanced(MDBX_DISABLE_GNU_SOURCE) 474 endif() 475 if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR IOS) 476 add_mdbx_option(MDBX_OSX_SPEED_INSTEADOF_DURABILITY "Disable use fcntl(F_FULLFSYNC) in favor of speed" OFF) 477 mark_as_advanced(MDBX_OSX_SPEED_INSTEADOF_DURABILITY) 478 endif() 479 if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") 480 if(MDBX_NTDLL_EXTRA_IMPLIB) 481 add_mdbx_option(MDBX_WITHOUT_MSVC_CRT "Avoid dependence from MSVC CRT and use ntdll.dll instead" OFF) 482 endif() 483 else() 484 add_mdbx_option(MDBX_USE_OFDLOCKS "Use Open file description locks (aka OFD locks, non-POSIX)" AUTO) 485 mark_as_advanced(MDBX_USE_OFDLOCKS) 486 endif() 487 add_mdbx_option(MDBX_LOCKING "Locking method (Win32=-1, SysV=5, POSIX=1988, POSIX=2001, POSIX=2008, Futexes=1995)" AUTO) 488 mark_as_advanced(MDBX_LOCKING) 489 add_mdbx_option(MDBX_TRUST_RTC "Does a system have battery-backed Real-Time Clock or just a fake" AUTO) 490 mark_as_advanced(MDBX_TRUST_RTC) 491 option(MDBX_FORCE_ASSERTIONS "Force enable assertion checking" OFF) 492 option(MDBX_DISABLE_PAGECHECKS "Disable some checks to reduce an overhead and detection probability of database corruption to a values closer to the LMDB" OFF) 493 494 if(NOT MDBX_AMALGAMATED_SOURCE) 495 if(CMAKE_CONFIGURATION_TYPES OR CMAKE_BUILD_TYPE_UPPERCASE STREQUAL "DEBUG") 496 set(MDBX_ALLOY_BUILD_DEFAULT OFF) 497 else() 498 set(MDBX_ALLOY_BUILD_DEFAULT ON) 499 endif() 500 add_mdbx_option(MDBX_ALLOY_BUILD "Build MDBX library through single/alloyed object file" ${MDBX_ALLOY_BUILD_DEFAULT}) 501 endif() 502 503 if((MDBX_BUILD_TOOLS OR MDBX_ENABLE_TESTS) AND MDBX_BUILD_SHARED_LIBRARY) 504 add_mdbx_option(MDBX_LINK_TOOLS_NONSTATIC "Link MDBX tools with non-static libmdbx" OFF) 505 else() 506 unset(MDBX_LINK_TOOLS_NONSTATIC CACHE) 507 endif() 508 509 if(CMAKE_CXX_COMPILER_LOADED AND MDBX_CXX_STANDARD GREATER_EQUAL 11 AND MDBX_CXX_STANDARD LESS 83) 510 if(NOT MDBX_AMALGAMATED_SOURCE) 511 option(MDBX_ENABLE_TESTS "Build MDBX tests" ${BUILD_TESTING}) 512 endif() 513 if(NOT MDBX_WITHOUT_MSVC_CRT 514 AND NOT (CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8) 515 AND NOT (CMAKE_COMPILER_IS_CLANG AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.9) 516 AND NOT (MSVC AND MSVC_VERSION LESS 1900)) 517 option(MDBX_BUILD_CXX "Build C++ portion" ON) 518 else() 519 set(MDBX_BUILD_CXX FALSE) 520 endif() 521 else() 522 set(MDBX_BUILD_CXX FALSE) 523 set(MDBX_ENABLE_TESTS FALSE) 524 endif() 525 526 ################################################################################ 527 ################################################################################ 528 529 if(MDBX_BUILD_CXX AND NOT CMAKE_CXX_COMPILER_LOADED) 530 message(FATAL_ERROR "MDBX_BUILD_CXX=${MDBX_BUILD_CXX}: The C++ compiler is required to build the C++API.") 531 endif() 532 533 if(MDBX_BUILD_CXX) 534 # determine library for C++ std::filesystem 535 probe_libcxx_filesystem() 536 endif() 537 538 # Get version 539 fetch_version(MDBX "${CMAKE_CURRENT_SOURCE_DIR}" FALSE) 540 message(STATUS "libmdbx version is ${MDBX_VERSION}") 541 542 # sources list 543 set(LIBMDBX_PUBLIC_HEADERS mdbx.h) 544 set(LIBMDBX_SOURCES mdbx.h "${CMAKE_CURRENT_BINARY_DIR}/config.h") 545 if(MDBX_AMALGAMATED_SOURCE) 546 list(APPEND LIBMDBX_SOURCES mdbx.c) 547 else() 548 # generate version file 549 configure_file("${MDBX_SOURCE_DIR}/version.c.in" 550 "${CMAKE_CURRENT_BINARY_DIR}/version.c" ESCAPE_QUOTES) 551 file(SHA256 "${CMAKE_CURRENT_BINARY_DIR}/version.c" MDBX_SOURCERY_DIGEST) 552 string(MAKE_C_IDENTIFIER "${MDBX_GIT_DESCRIBE}" MDBX_SOURCERY_SUFFIX) 553 set(MDBX_BUILD_SOURCERY "${MDBX_SOURCERY_DIGEST}_${MDBX_SOURCERY_SUFFIX}") 554 555 if(MDBX_ALLOY_BUILD) 556 list(APPEND LIBMDBX_SOURCES "${MDBX_SOURCE_DIR}/alloy.c") 557 include_directories("${MDBX_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}") 558 else() 559 list(APPEND LIBMDBX_SOURCES 560 "${CMAKE_CURRENT_BINARY_DIR}/version.c" 561 "${MDBX_SOURCE_DIR}/options.h" "${MDBX_SOURCE_DIR}/defs.h" 562 "${MDBX_SOURCE_DIR}/internals.h" "${MDBX_SOURCE_DIR}/osal.h" 563 "${MDBX_SOURCE_DIR}/core.c" "${MDBX_SOURCE_DIR}/osal.c" 564 "${MDBX_SOURCE_DIR}/lck-posix.c" "${MDBX_SOURCE_DIR}/lck-windows.c") 565 include_directories("${MDBX_SOURCE_DIR}") 566 endif() 567 endif(MDBX_AMALGAMATED_SOURCE) 568 if(MDBX_BUILD_CXX) 569 message(STATUS "Use C${MDBX_C_STANDARD} and C++${MDBX_CXX_STANDARD} for libmdbx") 570 list(APPEND LIBMDBX_PUBLIC_HEADERS mdbx.h++) 571 list(APPEND LIBMDBX_SOURCES "${MDBX_SOURCE_DIR}/mdbx.c++" mdbx.h++) 572 else() 573 message(STATUS "Use C${MDBX_C_STANDARD} for libmdbx but C++ portion is disabled") 574 endif() 575 576 if(SUBPROJECT AND MSVC) 577 if(MSVC_VERSION LESS 1900) 578 message(FATAL_ERROR "At least \"Microsoft C/C++ Compiler\" version 19.0.24234.1 (Visual Studio 2015 Update 3) is required.") 579 endif() 580 add_compile_options("/utf-8") 581 endif() 582 583 macro(target_setup_options TARGET) 584 if(DEFINED INTERPROCEDURAL_OPTIMIZATION) 585 set_target_properties(${TARGET} PROPERTIES 586 INTERPROCEDURAL_OPTIMIZATION $<BOOL:${INTERPROCEDURAL_OPTIMIZATION}>) 587 endif() 588 if(NOT C_FALLBACK_GNU11 AND NOT C_FALLBACK_11) 589 set_target_properties(${TARGET} PROPERTIES 590 C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON) 591 endif() 592 if(MDBX_BUILD_CXX) 593 set_target_properties(${TARGET} PROPERTIES 594 CXX_STANDARD ${MDBX_CXX_STANDARD} CXX_STANDARD_REQUIRED ON) 595 if(MSVC AND NOT MSVC_VERSION LESS 1910) 596 target_compile_options(${TARGET} INTERFACE "/Zc:__cplusplus") 597 endif() 598 endif() 599 if(CC_HAS_FASTMATH 600 AND NOT (CMAKE_COMPILER_IS_CLANG AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10)) 601 target_compile_options(${TARGET} PRIVATE "-ffast-math") 602 endif() 603 if(CC_HAS_VISIBILITY) 604 target_compile_options(${TARGET} PRIVATE "-fvisibility=hidden") 605 endif() 606 if(BUILD_FOR_NATIVE_CPU AND CC_HAS_ARCH_NATIVE) 607 target_compile_options(${TARGET} PRIVATE "-march=native") 608 endif() 609 endmacro() 610 611 macro(libmdbx_setup_libs TARGET MODE) 612 target_link_libraries(${TARGET} ${MODE} Threads::Threads) 613 if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") 614 target_link_libraries(${TARGET} ${MODE} ntdll advapi32) 615 if(MDBX_NTDLL_EXTRA_IMPLIB AND MDBX_WITHOUT_MSVC_CRT) 616 target_link_libraries(${TARGET} ${MODE} ntdll_extra) 617 endif() 618 elseif(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Solaris") 619 target_link_libraries(${TARGET} ${MODE} kstat) 620 elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Android") 621 target_link_libraries(${TARGET} ${MODE} log) 622 endif() 623 if(LIBCXX_FILESYSTEM AND MDBX_BUILD_CXX) 624 if(CMAKE_COMPILER_IS_ELBRUSCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 1.25.23 625 AND NOT CMAKE_VERSION VERSION_LESS 3.13) 626 target_link_options(${TARGET} PUBLIC "-Wl,--allow-multiple-definition") 627 endif() 628 target_link_libraries(${TARGET} PUBLIC ${LIBCXX_FILESYSTEM}) 629 endif() 630 endmacro() 631 632 # build static library 633 if(MDBX_INSTALL_STATIC) 634 add_library(mdbx-static STATIC ${LIBMDBX_SOURCES}) 635 else() 636 add_library(mdbx-static STATIC EXCLUDE_FROM_ALL ${LIBMDBX_SOURCES}) 637 endif() 638 set_target_properties(mdbx-static PROPERTIES PUBLIC_HEADER "${LIBMDBX_PUBLIC_HEADERS}") 639 target_compile_definitions(mdbx-static PRIVATE MDBX_BUILD_SHARED_LIBRARY=0) 640 target_setup_options(mdbx-static) 641 libmdbx_setup_libs(mdbx-static INTERFACE) 642 if(MDBX_BUILD_SHARED_LIBRARY) 643 set_target_properties(mdbx-static PROPERTIES OUTPUT_NAME mdbx-static) 644 else() 645 set_target_properties(mdbx-static PROPERTIES OUTPUT_NAME mdbx) 646 endif() 647 target_include_directories(mdbx-static INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}") 648 649 ################################################################################ 650 651 # build shared library 652 if(MDBX_BUILD_SHARED_LIBRARY) 653 add_library(mdbx SHARED ${LIBMDBX_SOURCES}) 654 set_target_properties(mdbx PROPERTIES PUBLIC_HEADER "${LIBMDBX_PUBLIC_HEADERS}") 655 target_compile_definitions(mdbx PRIVATE LIBMDBX_EXPORTS MDBX_BUILD_SHARED_LIBRARY=1 INTERFACE LIBMDBX_IMPORTS) 656 target_setup_options(mdbx) 657 libmdbx_setup_libs(mdbx PRIVATE) 658 if(MSVC) 659 if(MDBX_NTDLL_EXTRA_IMPLIB AND MDBX_WITHOUT_MSVC_CRT) 660 set_property(TARGET mdbx PROPERTY LINKER_FLAGS "/NODEFAULTLIB") 661 else() 662 set_property(TARGET mdbx PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL") 663 endif() 664 endif() 665 if(CC_HAS_VISIBILITY AND (LTO_ENABLED OR INTERPROCEDURAL_OPTIMIZATION)) 666 set_target_properties(mdbx PROPERTIES LINK_FLAGS "-fvisibility=hidden") 667 endif() 668 list(APPEND MDBX_BUILD_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}) 669 target_include_directories(mdbx INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}") 670 endif() 671 672 if(MDBX_BUILD_SHARED_LIBRARY AND MDBX_LINK_TOOLS_NONSTATIC) 673 set(TOOL_MDBX_LIB mdbx) 674 675 # use, i.e. don't skip the full RPATH for the build tree 676 set(CMAKE_SKIP_BUILD_RPATH FALSE) 677 678 # when building, don't use the install RPATH already (but later on when installing) 679 set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) 680 681 # add the automatically determined parts of the RPATH 682 # which point to directories outside the build tree to the install RPATH 683 set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) 684 685 # the RPATH to be used when installing, but only if it's not a system directory 686 list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir) 687 if(isSystemDir EQUAL -1) 688 if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") 689 set(CMAKE_INSTALL_RPATH "@executable_path/../lib") 690 else() 691 set(CMAKE_INSTALL_RPATH "\$ORIGIN/../lib") 692 endif() 693 endif() 694 else() 695 set(TOOL_MDBX_LIB mdbx-static) 696 endif() 697 698 # build mdbx-tools 699 if(MDBX_BUILD_TOOLS) 700 if(NOT MDBX_AMALGAMATED_SOURCE AND ${CMAKE_SYSTEM_NAME} STREQUAL "Windows") 701 set(WINGETOPT_SRC ${MDBX_SOURCE_DIR}/wingetopt.c ${MDBX_SOURCE_DIR}/wingetopt.h) 702 else() 703 set(WINGETOPT_SRC "") 704 endif() 705 706 foreach(TOOL mdbx_chk mdbx_copy mdbx_stat mdbx_dump mdbx_load mdbx_drop) 707 add_executable(${TOOL} mdbx.h ${MDBX_SOURCE_DIR}/${TOOL}.c ${WINGETOPT_SRC}) 708 if(NOT C_FALLBACK_GNU11 AND NOT C_FALLBACK_11) 709 set_target_properties(${TOOL} PROPERTIES 710 C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON) 711 endif() 712 target_setup_options(${TOOL}) 713 target_link_libraries(${TOOL} ${TOOL_MDBX_LIB}) 714 endforeach() 715 if(LIB_MATH) 716 target_link_libraries(mdbx_chk ${LIB_MATH}) 717 target_link_libraries(mdbx_stat ${LIB_MATH}) 718 endif() 719 endif() 720 721 ################################################################################ 722 723 # mdbx-shared-lib installation 724 if(NOT DEFINED MDBX_DLL_INSTALL_DESTINATION) 725 if(WIN32) 726 set(MDBX_DLL_INSTALL_DESTINATION ${CMAKE_INSTALL_BINDIR}) 727 else() 728 set(MDBX_DLL_INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}) 729 endif() 730 endif() 731 if(MDBX_BUILD_SHARED_LIBRARY) 732 if(CMAKE_VERSION VERSION_LESS 3.12) 733 install(TARGETS mdbx EXPORT libmdbx 734 LIBRARY DESTINATION ${MDBX_DLL_INSTALL_DESTINATION} COMPONENT runtime 735 OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 736 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 737 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT devel 738 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT devel) 739 else() 740 install(TARGETS mdbx EXPORT libmdbx 741 LIBRARY DESTINATION ${MDBX_DLL_INSTALL_DESTINATION} COMPONENT runtime 742 NAMELINK_COMPONENT devel 743 OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 744 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 745 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT devel 746 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT devel) 747 endif() 748 endif(MDBX_BUILD_SHARED_LIBRARY) 749 750 # mdbx-tools installation 751 if(MDBX_BUILD_TOOLS) 752 if(NOT DEFINED MDBX_TOOLS_INSTALL_DESTINATION) 753 set(MDBX_TOOLS_INSTALL_DESTINATION ${CMAKE_INSTALL_BINDIR}) 754 endif() 755 install( 756 TARGETS 757 mdbx_chk 758 mdbx_stat 759 mdbx_copy 760 mdbx_dump 761 mdbx_load 762 mdbx_drop 763 RUNTIME 764 DESTINATION ${MDBX_TOOLS_INSTALL_DESTINATION} 765 COMPONENT runtime) 766 if(MDBX_INSTALL_MANPAGES) 767 if(NOT DEFINED MDBX_MAN_INSTALL_DESTINATION) 768 set(MDBX_MAN_INSTALL_DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) 769 endif() 770 install( 771 FILES 772 "${MDBX_SOURCE_DIR}/man1/mdbx_chk.1" 773 "${MDBX_SOURCE_DIR}/man1/mdbx_stat.1" 774 "${MDBX_SOURCE_DIR}/man1/mdbx_copy.1" 775 "${MDBX_SOURCE_DIR}/man1/mdbx_dump.1" 776 "${MDBX_SOURCE_DIR}/man1/mdbx_load.1" 777 "${MDBX_SOURCE_DIR}/man1/mdbx_drop.1" 778 DESTINATION ${MDBX_MAN_INSTALL_DESTINATION} 779 COMPONENT doc) 780 endif() 781 endif(MDBX_BUILD_TOOLS) 782 783 # mdbx-static-lib installation 784 if(MDBX_INSTALL_STATIC) 785 if(CMAKE_VERSION VERSION_LESS 3.12) 786 install(TARGETS mdbx-static EXPORT libmdbx 787 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 788 OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 789 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 790 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT devel 791 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT devel) 792 else() 793 install(TARGETS mdbx-static EXPORT libmdbx 794 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 795 NAMELINK_COMPONENT devel 796 OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 797 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel 798 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT devel 799 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT devel) 800 endif() 801 endif(MDBX_INSTALL_STATIC) 802 803 ################################################################################ 804 805 # collect options & build info 806 if(NOT DEFINED MDBX_BUILD_TIMESTAMP) 807 string(TIMESTAMP MDBX_BUILD_TIMESTAMP UTC) 808 endif() 809 set(MDBX_BUILD_FLAGS ${CMAKE_C_FLAGS}) 810 if(MDBX_BUILD_CXX) 811 set(MDBX_BUILD_FLAGS ${CMAKE_CXX_FLAGS}) 812 endif() 813 814 # append cmake's build-type flags and defines 815 if(NOT CMAKE_CONFIGURATION_TYPES) 816 list(APPEND MDBX_BUILD_FLAGS ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPERCASE}}) 817 if(MDBX_BUILD_CXX) 818 list(APPEND MDBX_BUILD_FLAGS ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPERCASE}}) 819 endif() 820 endif() 821 822 # choice target to fetch definitions and options 823 if(MDBX_BUILD_SHARED_LIBRARY) 824 set(target4fetch mdbx) 825 else() 826 set(target4fetch mdbx-static) 827 endif() 828 829 # get definitions 830 get_target_property(defs_list ${target4fetch} COMPILE_DEFINITIONS) 831 if(defs_list) 832 list(APPEND MDBX_BUILD_FLAGS ${defs_list}) 833 endif() 834 835 # get target compile options 836 get_target_property(options_list ${target4fetch} COMPILE_OPTIONS) 837 if(options_list) 838 list(APPEND MDBX_BUILD_FLAGS ${options_list}) 839 endif() 840 841 list(REMOVE_DUPLICATES MDBX_BUILD_FLAGS) 842 string(REPLACE ";" " " MDBX_BUILD_FLAGS "${MDBX_BUILD_FLAGS}") 843 if(CMAKE_CONFIGURATION_TYPES) 844 # add dynamic part via per-configuration define 845 message(STATUS "MDBX Compile Flags: ${MDBX_BUILD_FLAGS} <AND CONFIGURATION DEPENDENT>") 846 add_definitions(-DMDBX_BUILD_FLAGS_CONFIG="$<$<CONFIG:Debug>:${CMAKE_C_FLAGS_DEBUG} ${CMAKE_C_DEFINES_DEBUG}>$<$<CONFIG:Release>:${CMAKE_C_FLAGS_RELEASE} ${CMAKE_C_DEFINES_RELEASE}>$<$<CONFIG:RelWithDebInfo>:${CMAKE_C_FLAGS_RELWITHDEBINFO} ${CMAKE_C_DEFINES_RELWITHDEBINFO}>$<$<CONFIG:MinSizeRel>:${CMAKE_C_FLAGS_MINSIZEREL} ${CMAKE_C_DEFINES_MINSIZEREL}>") 847 else() 848 message(STATUS "MDBX Compile Flags: ${MDBX_BUILD_FLAGS}") 849 endif() 850 851 # get compiler info 852 execute_process(COMMAND sh -c "${CMAKE_C_COMPILER} --version | head -1" 853 OUTPUT_VARIABLE MDBX_BUILD_COMPILER 854 OUTPUT_STRIP_TRAILING_WHITESPACE 855 ERROR_QUIET 856 RESULT_VARIABLE rc) 857 if(rc OR NOT MDBX_BUILD_COMPILER) 858 string(STRIP "${CMAKE_C_COMPILER_ID}-${CMAKE_C_COMPILER_VERSION}" MDBX_BUILD_COMPILER) 859 endif() 860 861 # make a build-target triplet 862 if(CMAKE_C_COMPILER_TARGET) 863 set(MDBX_BUILD_TARGET "${CMAKE_C_COMPILER_TARGET}") 864 else() 865 if(CMAKE_C_COMPILER_ARCHITECTURE_ID) 866 string(STRIP "${CMAKE_C_COMPILER_ARCHITECTURE_ID}" MDBX_BUILD_TARGET) 867 elseif(CMAKE_GENERATOR_PLATFORM AND NOT CMAKE_GENERATOR_PLATFORM STREQUAL CMAKE_SYSTEM_NAME) 868 string(STRIP "${CMAKE_GENERATOR_PLATFORM}" MDBX_BUILD_TARGET) 869 elseif(CMAKE_SYSTEM_ARCH) 870 string(STRIP "${CMAKE_SYSTEM_ARCH}" MDBX_BUILD_TARGET) 871 elseif(CMAKE_LIBRARY_ARCHITECTURE) 872 string(STRIP "${CMAKE_LIBRARY_ARCHITECTURE}" MDBX_BUILD_TARGET) 873 elseif(CMAKE_SYSTEM_PROCESSOR) 874 string(STRIP "${CMAKE_SYSTEM_PROCESSOR}" MDBX_BUILD_TARGET) 875 else() 876 set(MDBX_BUILD_TARGET "unknown") 877 endif() 878 if(CMAKE_C_COMPILER_ABI 879 AND NOT (CMAKE_C_COMPILER_ABI MATCHES ".*${MDBX_BUILD_TARGET}.*" OR MDBX_BUILD_TARGET MATCHES ".*${CMAKE_C_COMPILER_ABI}.*")) 880 string(APPEND MDBX_BUILD_TARGET "-${CMAKE_C_COMPILER_ABI}") 881 endif() 882 if(CMAKE_C_PLATFORM_ID 883 AND NOT (CMAKE_SYSTEM_NAME 884 AND (CMAKE_C_PLATFORM_ID MATCHES ".*${CMAKE_SYSTEM_NAME}.*" OR CMAKE_SYSTEM_NAME MATCHES ".*${CMAKE_C_PLATFORM_ID}.*")) 885 AND NOT (CMAKE_C_PLATFORM_ID MATCHES ".*${CMAKE_C_PLATFORM_ID}.*" OR MDBX_BUILD_TARGET MATCHES ".*${CMAKE_C_PLATFORM_ID}.*")) 886 string(APPEND MDBX_BUILD_TARGET "-${CMAKE_C_COMPILER_ABI}") 887 endif() 888 if(CMAKE_SYSTEM_NAME) 889 string(APPEND MDBX_BUILD_TARGET "-${CMAKE_SYSTEM_NAME}") 890 endif() 891 endif() 892 893 # provide build-type 894 if(CMAKE_CONFIGURATION_TYPES) 895 # via per-configuration define 896 add_definitions(-DMDBX_BUILD_TYPE="$<CONFIG>") 897 set(MDBX_BUILD_TYPE "<CONFIGURATION DEPENDENT>") 898 else() 899 set(MDBX_BUILD_TYPE ${CMAKE_BUILD_TYPE}) 900 endif() 901 902 # options 903 set(options VERSION C_COMPILER CXX_COMPILER MDBX_BUILD_TARGET MDBX_BUILD_TYPE ${MDBX_BUILD_OPTIONS}) 904 foreach(item IN LISTS options) 905 if(DEFINED ${item}) 906 set(value "${${item}}") 907 elseif(DEFINED MDBX_${item}) 908 set(item MDBX_${item}) 909 set(value "${${item}}") 910 elseif(DEFINED CMAKE_${item}) 911 set(item CMAKE_${item}) 912 set(value "${${item}}") 913 else() 914 set(value "AUTO (not pre-defined explicitly)") 915 endif() 916 message(STATUS "${item}: ${value}") 917 endforeach(item) 918 919 # provide config.h for library build info 920 configure_file("${MDBX_SOURCE_DIR}/config.h.in" 921 "${CMAKE_CURRENT_BINARY_DIR}/config.h" ESCAPE_QUOTES) 922 add_definitions(-DMDBX_CONFIG_H="${CMAKE_CURRENT_BINARY_DIR}/config.h") 923 924 ################################################################################ 925 926 if(NOT MDBX_AMALGAMATED_SOURCE AND MDBX_ENABLE_TESTS) 927 if(NOT CMAKE_CXX_COMPILER_LOADED) 928 message(FATAL_ERROR "MDBX_ENABLE_TESTS=${MDBX_ENABLE_TESTS}: The C++ compiler is required to build the tests.") 929 endif() 930 add_subdirectory(test) 931 endif() 932 933 ################################################################################ 934 935 set(PACKAGE "libmdbx") 936 set(CPACK_PACKAGE_VERSION_MAJOR ${MDBX_VERSION_MAJOR}) 937 set(CPACK_PACKAGE_VERSION_MINOR ${MDBX_VERSION_MINOR}) 938 set(CPACK_PACKAGE_VERSION_PATCH ${MDBX_VERSION_RELEASE}) 939 set(CPACK_PACKAGE_VERSION_COMMIT ${MDBX_VERSION_REVISION}) 940 set(PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}.${CPACK_PACKAGE_VERSION_COMMIT}") 941 message(STATUS "libmdbx package version is ${PACKAGE_VERSION}") 942 943 cmake_policy(POP)