mdbx-cmake: fixes for old C/C++ compilers and/or CMake.

This commit is contained in:
Leonid Yuriev 2020-06-26 02:57:46 +03:00
parent 505ee31635
commit 95199c754a
3 changed files with 97 additions and 30 deletions

View File

@ -299,7 +299,9 @@ if(NOT HAS_C11 LESS 0)
else() else()
set(MDBX_C_STANDARD 99) set(MDBX_C_STANDARD 99)
endif() 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(${CMAKE_SYSTEM_NAME} STREQUAL "Windows" AND EXISTS "${MDBX_SOURCE_DIR}/ntdll.def")
if(MSVC) if(MSVC)
@ -485,9 +487,11 @@ if(MDBX_INSTALL_STATIC)
else() else()
add_library(mdbx-static STATIC EXCLUDE_FROM_ALL ${LIBMDBX_SOURCES}) add_library(mdbx-static STATIC EXCLUDE_FROM_ALL ${LIBMDBX_SOURCES})
endif() endif()
set_target_properties(mdbx-static PROPERTIES set_target_properties(mdbx-static PROPERTIES PUBLIC_HEADER mdbx.h)
C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON if(MDBX_C_STANDARD)
PUBLIC_HEADER mdbx.h) 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_compile_definitions(mdbx-static PRIVATE MDBX_BUILD_SHARED_LIBRARY=0)
target_setup_options(mdbx-static) target_setup_options(mdbx-static)
libmdbx_setup_libs(mdbx-static INTERFACE) libmdbx_setup_libs(mdbx-static INTERFACE)
@ -502,9 +506,11 @@ endif()
# build shared library # build shared library
if(MDBX_BUILD_SHARED_LIBRARY) if(MDBX_BUILD_SHARED_LIBRARY)
add_library(mdbx SHARED ${LIBMDBX_SOURCES}) add_library(mdbx SHARED ${LIBMDBX_SOURCES})
set_target_properties(mdbx PROPERTIES set_target_properties(mdbx PROPERTIES PUBLIC_HEADER mdbx.h)
C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON if(MDBX_C_STANDARD)
PUBLIC_HEADER mdbx.h) 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_compile_definitions(mdbx PRIVATE LIBMDBX_EXPORTS MDBX_BUILD_SHARED_LIBRARY=1 INTERFACE LIBMDBX_IMPORTS)
target_setup_options(mdbx) target_setup_options(mdbx)
libmdbx_setup_libs(mdbx PRIVATE) 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) foreach(TOOL mdbx_chk mdbx_copy mdbx_stat mdbx_dump mdbx_load)
add_executable(${TOOL} mdbx.h ${MDBX_SOURCE_DIR}/${TOOL}.c ${WINGETOPT_SRC}) add_executable(${TOOL} mdbx.h ${MDBX_SOURCE_DIR}/${TOOL}.c ${WINGETOPT_SRC})
set_target_properties(${TOOL} PROPERTIES if(MDBX_C_STANDARD)
C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON) set_target_properties(${TOOL} PROPERTIES
C_STANDARD ${MDBX_C_STANDARD} C_STANDARD_REQUIRED ON)
endif()
target_setup_options(${TOOL}) target_setup_options(${TOOL})
target_link_libraries(${TOOL} ${TOOL_MDBX_LIB}) target_link_libraries(${TOOL} ${TOOL_MDBX_LIB})
endforeach() endforeach()

View File

@ -254,6 +254,17 @@ else()
set(CMAKE_REQUIRED_FLAGS "") set(CMAKE_REQUIRED_FLAGS "")
endif() 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 # Check for LTO support by GCC
if(CMAKE_COMPILER_IS_GNU${CMAKE_PRIMARY_LANG}) if(CMAKE_COMPILER_IS_GNU${CMAKE_PRIMARY_LANG})
unset(gcc_collect) unset(gcc_collect)
@ -487,8 +498,12 @@ endif()
macro(setup_compile_flags) macro(setup_compile_flags)
# save initial C/CXX flags # save initial C/CXX flags
if(NOT INITIAL_CMAKE_FLAGS_SAVED) if(NOT INITIAL_CMAKE_FLAGS_SAVED)
set(INITIAL_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} CACHE STRING "Initial CMake's flags" FORCE) if(CMAKE_CXX_COMPILER_LOADED)
set(INITIAL_CMAKE_C_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "Initial CMake's flags" FORCE) 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_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_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) 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() endif()
# reset C/CXX flags # reset C/CXX flags
set(CXX_FLAGS ${INITIAL_CMAKE_CXX_FLAGS}) if(CMAKE_CXX_COMPILER_LOADED)
set(C_FLAGS ${INITIAL_CMAKE_C_FLAGS}) 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(EXE_LINKER_FLAGS ${INITIAL_CMAKE_EXE_LINKER_FLAGS})
set(SHARED_LINKER_FLAGS ${INITIAL_CMAKE_SHARED_LINKER_FLAGS}) set(SHARED_LINKER_FLAGS ${INITIAL_CMAKE_SHARED_LINKER_FLAGS})
set(STATIC_LINKER_FLAGS ${INITIAL_CMAKE_STATIC_LINKER_FLAGS}) set(STATIC_LINKER_FLAGS ${INITIAL_CMAKE_STATIC_LINKER_FLAGS})
@ -721,31 +746,37 @@ macro(setup_compile_flags)
endif() endif()
# push C/CXX flags into the cache # 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) if(CMAKE_CXX_COMPILER_LOADED)
set(CMAKE_C_FLAGS ${C_FLAGS} CACHE STRING "Flags used by the C compiler during all build types" FORCE) 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_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_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_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) 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(EXE_LINKER_FLAGS)
unset(SHARED_LINKER_FLAGS) unset(SHARED_LINKER_FLAGS)
unset(STATIC_LINKER_FLAGS) unset(STATIC_LINKER_FLAGS)
unset(MODULE_LINKER_FLAGS) unset(MODULE_LINKER_FLAGS)
endmacro(setup_compile_flags) endmacro(setup_compile_flags)
# determine library for for std::filesystem if(CMAKE_CXX_COMPILER_LOADED)
set(LIBCXX_FILESYSTEM "") # determine library for for std::filesystem
if(CMAKE_COMPILER_IS_GNUCXX) set(LIBCXX_FILESYSTEM "")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0) if(CMAKE_COMPILER_IS_GNUCXX)
set(LIBCXX_FILESYSTEM "stdc++fs") if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
endif() set(LIBCXX_FILESYSTEM "stdc++fs")
elseif(CMAKE_COMPILER_IS_CLANG) endif()
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) elseif(CMAKE_COMPILER_IS_CLANG)
set(LIBCXX_FILESYSTEM "c++experimental") if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
else() set(LIBCXX_FILESYSTEM "c++experimental")
set(LIBCXX_FILESYSTEM "stdc++fs") else()
set(LIBCXX_FILESYSTEM "stdc++fs")
endif()
endif() endif()
endif() endif()

View File

@ -27,9 +27,37 @@ add_executable(mdbx_test
nested.cc 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 set_target_properties(mdbx_test PROPERTIES
INTERPROCEDURAL_OPTIMIZATION $<BOOL:${INTERPROCEDURAL_OPTIMIZATION}> INTERPROCEDURAL_OPTIMIZATION $<BOOL:${INTERPROCEDURAL_OPTIMIZATION}>)
CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)
target_setup_options(mdbx_test) target_setup_options(mdbx_test)
target_link_libraries(mdbx_test ${TOOL_MDBX_LIB} ${LIB_MATH} ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(mdbx_test ${TOOL_MDBX_LIB} ${LIB_MATH} ${CMAKE_THREAD_LIBS_INIT})