From 70350bad81bcebb2a28bce2c6b1293ed652fa496 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Tue, 22 Oct 2019 12:14:26 +0300 Subject: [PATCH] mdbx-cmake: require C99 or use C11 when available. Change-Id: I241e8c38232750efb537a0b581f822540f207cdc --- CMakeLists.dist-minimal | 25 ++++++++++++++++++++++--- CMakeLists.txt | 9 ++++++++- src/CMakeLists.txt | 10 +++++----- src/tools/CMakeLists.txt | 1 + test/CMakeLists.txt | 4 +--- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CMakeLists.dist-minimal b/CMakeLists.dist-minimal index 6a217506..1f138af6 100644 --- a/CMakeLists.dist-minimal +++ b/CMakeLists.dist-minimal @@ -75,6 +75,14 @@ if(NOT CMAKE_BUILD_TYPE) FORCE) endif() +list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 HAS_C11) +if(NOT HAS_C11 LESS 0) + set(MDBX_C_STANDARD 11) +else() + set(MDBX_C_STANDARD 99) +endif() +message(STATUS "Use C${MDBX_C_STANDARD} for libmdbx") + # not supported by this (minimal) script add_definitions(-DMDBX_AVOID_CRT=0) @@ -141,9 +149,13 @@ if(NOT DEFINED MDBX_BUILD_SHARED_LIBRARY) endif() if(MDBX_BUILD_SHARED_LIBRARY) add_library(mdbx SHARED mdbx.c mdbx.h) + set_target_properties(mdbx PROPERTIES + C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON + PUBLIC_HEADER mdbx.h) target_compile_definitions(mdbx PRIVATE LIBMDBX_EXPORTS INTERFACE LIBMDBX_IMPORTS) if(DEFINED INTERPROCEDURAL_OPTIMIZATION) - set_target_properties(mdbx PROPERTIES INTERPROCEDURAL_OPTIMIZATION $) + set_target_properties(mdbx PROPERTIES + INTERPROCEDURAL_OPTIMIZATION $) endif() target_link_libraries(mdbx PRIVATE ${CMAKE_THREAD_LIBS_INIT}) if(WIN32) @@ -153,9 +165,13 @@ endif() # static library used for tools, to avoid rpath/dll-path troubles add_library(mdbx-static STATIC EXCLUDE_FROM_ALL mdbx.c mdbx.h) +set_target_properties(mdbx-static PROPERTIES + C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON + PUBLIC_HEADER mdbx.h) target_link_libraries(mdbx-static INTERFACE ${CMAKE_THREAD_LIBS_INIT}) if(DEFINED INTERPROCEDURAL_OPTIMIZATION) - set_target_properties(mdbx-static PROPERTIES INTERPROCEDURAL_OPTIMIZATION $) + set_target_properties(mdbx-static PROPERTIES + INTERPROCEDURAL_OPTIMIZATION $) endif() if(WIN32) target_link_libraries(mdbx-static INTERFACE ntdll.lib) @@ -164,8 +180,11 @@ endif() # mdbx-tools foreach(TOOL mdbx_chk mdbx_copy mdbx_stat mdbx_dump mdbx_load) add_executable(${TOOL} ${TOOL}.c) + set_target_properties(${TOOL} PROPERTIES + C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON) if(DEFINED INTERPROCEDURAL_OPTIMIZATION) - set_target_properties(${TOOL} PROPERTIES INTERPROCEDURAL_OPTIMIZATION $) + set_target_properties(${TOOL} PROPERTIES + INTERPROCEDURAL_OPTIMIZATION $) endif() target_link_libraries(${TOOL} mdbx-static) endforeach() diff --git a/CMakeLists.txt b/CMakeLists.txt index e7976ac7..1cd76100 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -272,7 +272,13 @@ else() setup_compile_flags() endif(SUBPROJECT) -option(MDBX_ENABLE_TESTS "Build MDBX tests." ${BUILD_TESTING}) +list(FIND CMAKE_C_COMPILE_FEATURES c_std_11 HAS_C11) +if(NOT HAS_C11 LESS 0) + set(MDBX_C_STANDARD 11) +else() + set(MDBX_C_STANDARD 99) +endif() +message(STATUS "Use C${MDBX_C_STANDARD} for libmdbx") ############################################################################## ############################################################################## @@ -312,6 +318,7 @@ else() add_mdbx_option(MDBX_USE_OFDLOCKS "Use Open file description locks (aka OFD locks, non-POSIX)" AUTO) mark_as_advanced(MDBX_USE_OFDLOCKS) endif() +option(MDBX_ENABLE_TESTS "Build MDBX tests." ${BUILD_TESTING}) ################################################################################ ################################################################################ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7a12cdfe..5fa99a3e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,15 +35,16 @@ list(APPEND LIBMDBX_SOURCES ../mdbx.h if(MDBX_BUILD_SHARED_LIBRARY) add_library(mdbx SHARED ${LIBMDBX_SOURCES}) target_compile_definitions(mdbx PRIVATE LIBMDBX_EXPORTS INTERFACE LIBMDBX_IMPORTS) - if(CC_HAS_VISIBILITY AND (LTO_ENABLED OR INTERPROCEDURAL_OPTIMIZATION)) - set_target_properties(mdbx PROPERTIES LINK_FLAGS "-fvisibility=hidden") - endif() set(MDBX_LIBDEP_MODE PRIVATE) else() add_library(mdbx STATIC ${LIBMDBX_SOURCES}) set(MDBX_LIBDEP_MODE PUBLIC) endif() +if(CC_HAS_VISIBILITY AND (LTO_ENABLED OR INTERPROCEDURAL_OPTIMIZATION)) + set_target_properties(mdbx PROPERTIES LINK_FLAGS "-fvisibility=hidden") +endif() + if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") if(MSVC) if(NOT MSVC_LIB_EXE) @@ -102,8 +103,7 @@ endif() set_target_properties(mdbx PROPERTIES INTERPROCEDURAL_OPTIMIZATION $ - C_STANDARD 11 - C_STANDARD_REQUIRED OFF + C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON PUBLIC_HEADER "../mdbx.h") if(CC_HAS_FASTMATH) diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index 5cb796d0..01ecd7dd 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -29,6 +29,7 @@ foreach(TOOL ${MDBX_TOOLS}) target_link_libraries(${TOOL} mdbx ${CMAKE_THREAD_LIBS_INIT}) set_target_properties(${TOOL} PROPERTIES + C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON INTERPROCEDURAL_OPTIMIZATION $) install(TARGETS ${TOOL} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin COMPONENT mdbx) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 75db969f..0cc22182 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -34,9 +34,7 @@ add_executable(mdbx_test set_target_properties(mdbx_test PROPERTIES INTERPROCEDURAL_OPTIMIZATION $ - CXX_STANDARD 17 - CXX_STANDARD_REQUIRED ON - ) + CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON) if(CC_HAS_FASTMATH) target_compile_options(mdbx_test PRIVATE "-ffast-math")