From 95199c754a1d267f456e0ec826abfa7c451a679b Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Fri, 26 Jun 2020 02:57:46 +0300 Subject: [PATCH] mdbx-cmake: fixes for old C/C++ compilers and/or CMake. --- CMakeLists.txt | 26 +++++++++++------ cmake/compiler.cmake | 69 ++++++++++++++++++++++++++++++++------------ test/CMakeLists.txt | 32 ++++++++++++++++++-- 3 files changed, 97 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 40355c27..e00f3403 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -299,7 +299,9 @@ if(NOT HAS_C11 LESS 0) else() set(MDBX_C_STANDARD 99) endif() -message(STATUS "Use C${MDBX_C_STANDARD} for libmdbx") +if(MDBX_C_STANDARD) + message(STATUS "Use C${MDBX_C_STANDARD} for libmdbx") +endif() if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows" AND EXISTS "${MDBX_SOURCE_DIR}/ntdll.def") if(MSVC) @@ -485,9 +487,11 @@ if(MDBX_INSTALL_STATIC) else() add_library(mdbx-static STATIC EXCLUDE_FROM_ALL ${LIBMDBX_SOURCES}) endif() -set_target_properties(mdbx-static PROPERTIES - C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON - PUBLIC_HEADER mdbx.h) +set_target_properties(mdbx-static PROPERTIES PUBLIC_HEADER mdbx.h) +if(MDBX_C_STANDARD) + set_target_properties(mdbx-static PROPERTIES + C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON) +endif() target_compile_definitions(mdbx-static PRIVATE MDBX_BUILD_SHARED_LIBRARY=0) target_setup_options(mdbx-static) libmdbx_setup_libs(mdbx-static INTERFACE) @@ -502,9 +506,11 @@ endif() # build shared library if(MDBX_BUILD_SHARED_LIBRARY) add_library(mdbx SHARED ${LIBMDBX_SOURCES}) - set_target_properties(mdbx PROPERTIES - C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON - PUBLIC_HEADER mdbx.h) + set_target_properties(mdbx PROPERTIES PUBLIC_HEADER mdbx.h) + if(MDBX_C_STANDARD) + set_target_properties(mdbx PROPERTIES + C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON) + endif() target_compile_definitions(mdbx PRIVATE LIBMDBX_EXPORTS MDBX_BUILD_SHARED_LIBRARY=1 INTERFACE LIBMDBX_IMPORTS) target_setup_options(mdbx) libmdbx_setup_libs(mdbx PRIVATE) @@ -557,8 +563,10 @@ if(MDBX_BUILD_TOOLS) foreach(TOOL mdbx_chk mdbx_copy mdbx_stat mdbx_dump mdbx_load) add_executable(${TOOL} mdbx.h ${MDBX_SOURCE_DIR}/${TOOL}.c ${WINGETOPT_SRC}) - set_target_properties(${TOOL} PROPERTIES - C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON) + if(MDBX_C_STANDARD) + set_target_properties(${TOOL} PROPERTIES + C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON) + endif() target_setup_options(${TOOL}) target_link_libraries(${TOOL} ${TOOL_MDBX_LIB}) endforeach() diff --git a/cmake/compiler.cmake b/cmake/compiler.cmake index 61d7b3f0..fca9886f 100644 --- a/cmake/compiler.cmake +++ b/cmake/compiler.cmake @@ -254,6 +254,17 @@ else() set(CMAKE_REQUIRED_FLAGS "") endif() +# Crutch for old C++ compilers and/or CMake to enabling C++11 +if(CMAKE_CXX_COMPILER_LOADED) + list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_11 HAS_CXX11) + if(HAS_CXX11 LESS 0) + check_compiler_flag("-std=gnu++11" CXX_FALLBACK_STDGNU11) + if(NOT CXX_FALLBACK_STDGNU11) + check_compiler_flag("-std=c++11" CXX_FALLBACK_STD11) + endif() + endif() +endif() + # Check for LTO support by GCC if(CMAKE_COMPILER_IS_GNU${CMAKE_PRIMARY_LANG}) unset(gcc_collect) @@ -487,8 +498,12 @@ endif() macro(setup_compile_flags) # save initial C/CXX flags if(NOT INITIAL_CMAKE_FLAGS_SAVED) - set(INITIAL_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} CACHE STRING "Initial CMake's flags" FORCE) - set(INITIAL_CMAKE_C_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "Initial CMake's flags" FORCE) + if(CMAKE_CXX_COMPILER_LOADED) + set(INITIAL_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} CACHE STRING "Initial CMake's flags" FORCE) + endif() + if(CMAKE_C_COMPILER_LOADED) + set(INITIAL_CMAKE_C_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "Initial CMake's flags" FORCE) + endif() set(INITIAL_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} CACHE STRING "Initial CMake's flags" FORCE) set(INITIAL_CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS} CACHE STRING "Initial CMake's flags" FORCE) set(INITIAL_CMAKE_STATIC_LINKER_FLAGS ${CMAKE_STATIC_LINKER_FLAGS} CACHE STRING "Initial CMake's flags" FORCE) @@ -497,8 +512,18 @@ macro(setup_compile_flags) endif() # reset C/CXX flags - set(CXX_FLAGS ${INITIAL_CMAKE_CXX_FLAGS}) - set(C_FLAGS ${INITIAL_CMAKE_C_FLAGS}) + if(CMAKE_CXX_COMPILER_LOADED) + set(CXX_FLAGS ${INITIAL_CMAKE_CXX_FLAGS}) + # Crutch for old C++ compilers and/or CMake to enabling C++11 + if(CXX_FALLBACK_STDGNU11) + add_compile_flags("CXX" "-std=gnu++11") + elseif(CXX_FALLBACK_STD11) + add_compile_flags("CXX" "-std=c++11") + endif() + endif() + if(CMAKE_C_COMPILER_LOADED) + set(C_FLAGS ${INITIAL_CMAKE_C_FLAGS}) + endif() set(EXE_LINKER_FLAGS ${INITIAL_CMAKE_EXE_LINKER_FLAGS}) set(SHARED_LINKER_FLAGS ${INITIAL_CMAKE_SHARED_LINKER_FLAGS}) set(STATIC_LINKER_FLAGS ${INITIAL_CMAKE_STATIC_LINKER_FLAGS}) @@ -721,31 +746,37 @@ macro(setup_compile_flags) endif() # push C/CXX flags into the cache - set(CMAKE_CXX_FLAGS ${CXX_FLAGS} CACHE STRING "Flags used by the C++ compiler during all build types" FORCE) - set(CMAKE_C_FLAGS ${C_FLAGS} CACHE STRING "Flags used by the C compiler during all build types" FORCE) + if(CMAKE_CXX_COMPILER_LOADED) + set(CMAKE_CXX_FLAGS ${CXX_FLAGS} CACHE STRING "Flags used by the C++ compiler during all build types" FORCE) + unset(CXX_FLAGS) + endif() + if(CMAKE_C_COMPILER_LOADED) + set(CMAKE_C_FLAGS ${C_FLAGS} CACHE STRING "Flags used by the C compiler during all build types" FORCE) + unset(C_FLAGS) + endif() set(CMAKE_EXE_LINKER_FLAGS ${EXE_LINKER_FLAGS} CACHE STRING "Flags used by the linker" FORCE) set(CMAKE_SHARED_LINKER_FLAGS ${SHARED_LINKER_FLAGS} CACHE STRING "Flags used by the linker during the creation of dll's" FORCE) set(CMAKE_STATIC_LINKER_FLAGS ${STATIC_LINKER_FLAGS} CACHE STRING "Flags used by the linker during the creation of static libraries" FORCE) set(CMAKE_MODULE_LINKER_FLAGS ${MODULE_LINKER_FLAGS} CACHE STRING "Flags used by the linker during the creation of modules" FORCE) - unset(CXX_FLAGS) - unset(C_FLAGS) unset(EXE_LINKER_FLAGS) unset(SHARED_LINKER_FLAGS) unset(STATIC_LINKER_FLAGS) unset(MODULE_LINKER_FLAGS) endmacro(setup_compile_flags) -# determine library for for std::filesystem -set(LIBCXX_FILESYSTEM "") -if(CMAKE_COMPILER_IS_GNUCXX) - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0) - set(LIBCXX_FILESYSTEM "stdc++fs") - endif() -elseif(CMAKE_COMPILER_IS_CLANG) - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) - set(LIBCXX_FILESYSTEM "c++experimental") - else() - set(LIBCXX_FILESYSTEM "stdc++fs") +if(CMAKE_CXX_COMPILER_LOADED) + # determine library for for std::filesystem + set(LIBCXX_FILESYSTEM "") + if(CMAKE_COMPILER_IS_GNUCXX) + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0) + set(LIBCXX_FILESYSTEM "stdc++fs") + endif() + elseif(CMAKE_COMPILER_IS_CLANG) + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) + set(LIBCXX_FILESYSTEM "c++experimental") + else() + set(LIBCXX_FILESYSTEM "stdc++fs") + endif() endif() endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2014cb57..3e0a929b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -27,9 +27,37 @@ add_executable(mdbx_test nested.cc ) +list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_20 HAS_CXX20) +list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_17 HAS_CXX17) +list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_14 HAS_CXX14) +list(FIND CMAKE_CXX_COMPILE_FEATURES cxx_std_11 HAS_CXX11) +if(NOT DEFINED MDBX_CXX_STANDARD) + if(DEFINED CMAKE_CXX_STANDARD) + set(MDBX_CXX_STANDARD ${CMAKE_CXX_STANDARD}) + elseif(NOT HAS_CXX20 LESS 0) + set(MDBX_CXX_STANDARD 20) + elseif(NOT HAS_CXX17 LESS 0) + set(MDBX_CXX_STANDARD 17) + elseif(NOT HAS_CXX14 LESS 0) + set(MDBX_CXX_STANDARD 14) + elseif(NOT HAS_CXX11 LESS 0) + set(MDBX_CXX_STANDARD 11) + endif() +endif() +if(MDBX_CXX_STANDARD) + message(STATUS "Use C++${MDBX_CXX_STANDARD} for libmdbx") + if(NOT SUBPROJECT OR NOT DEFINED CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD ${MDBX_CXX_STANDARD}) + endif() +endif() + +if(MDBX_CXX_STANDARD) + set_target_properties(mdbx_test PROPERTIES + CXX_STANDARD ${MDBX_CXX_STANDARD} CXX_STANDARD_REQUIRED ON) +endif() + set_target_properties(mdbx_test PROPERTIES - INTERPROCEDURAL_OPTIMIZATION $ - CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON) + INTERPROCEDURAL_OPTIMIZATION $) target_setup_options(mdbx_test) target_link_libraries(mdbx_test ${TOOL_MDBX_LIB} ${LIB_MATH} ${CMAKE_THREAD_LIBS_INIT})