mdbx-build: refine CMake scripts, add build options.

Change-Id: Ie3db2f6439d5cc7ef3cd9ebb5b8e5d5d34f743dd
This commit is contained in:
Leonid Yuriev 2019-09-03 02:24:19 +03:00
parent 874418a301
commit 263837094e
10 changed files with 182 additions and 88 deletions

View File

@ -61,6 +61,13 @@ else()
endif()
endif()
macro(add_mdbx_option NAME DESCPTION DEFAULT)
list(APPEND MDBX_BUILD_OPTIONS ${NAME})
if(NOT ${DEFAULT} STREQUAL "AUTO")
option(${NAME} "${DESCPTION}" ${DEFAULT})
endif()
endmacro()
# only for compatibility testing
# set(CMAKE_CXX_STANDARD 14)
@ -150,7 +157,11 @@ endif()
find_package(Threads REQUIRED)
if(NOT SUBPROJECT)
if(SUBPROJECT)
if(NOT DEFINED BUILD_SHARED_LIBS)
option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
endif()
else()
option(CMAKE_POSITION_INDEPENDENT_CODE "Generate position independed (PIC)." ON)
option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." ON)
if (CC_HAS_ARCH_NATIVE)
@ -249,10 +260,47 @@ if(NOT SUBPROJECT)
endif()
setup_compile_flags()
endif(NOT SUBPROJECT)
endif(SUBPROJECT)
option(MDBX_ENABLE_TESTS "Build MDBX tests." ${BUILD_TESTING})
##############################################################################
##############################################################################
#
# #### ##### ##### # #### # # ####
# # # # # # # # # ## # #
# # # # # # # # # # # # ####
# # # ##### # # # # # # # #
# # # # # # # # # ## # #
# #### # # # #### # # ####
#
set(MDBX_BUILD_OPTIONS ENABLE_ASAN ENABLE_VALGRIND ENABLE_GPROF ENABLE_GCOV)
add_mdbx_option(MDBX_ALLOY_BUILD "Build MDBX library as single object file" ON)
add_mdbx_option(MDBX_TXN_CHECKOWNER "Checking transaction matches the calling thread inside libmdbx's API" ON)
add_mdbx_option(MDBX_TXN_CHECKPID "Paranoid checking PID inside libmdbx's API" OFF)
mark_as_advanced(MDBX_TXN_CHECKPID)
if(APPLE)
add_mdbx_option(MDBX_OSX_SPEED_INSTEADOF_DURABILITY "Disable use fcntl(F_FULLFSYNC) in favor of speed" OFF)
mark_as_advanced(MDBX_OSX_SPEED_INSTEADOF_DURABILITY)
endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
add_mdbx_option(MDBX_AVOID_CRT "Avoid dependence from MSVC CRT" ${BUILD_SHARED_LIBS})
if(NOT BUILD_SHARED_LIBS)
add_mdbx_option(MDBX_CONFIG_MANUAL_TLS_CALLBACK
"Provide mdbx_dll_callback() for manual initialization" OFF)
mark_as_advanced(MDBX_CONFIG_MANUAL_TLS_CALLBACK)
endif()
else()
add_mdbx_option(MDBX_USE_ROBUST "Use POSIX.1-2008 robust mutexes" AUTO)
mark_as_advanced(MDBX_USE_ROBUST)
add_mdbx_option(MDBX_USE_OFDLOCKS "Use Open file description locks (aka OFD locks, non-POSIX)" AUTO)
mark_as_advanced(MDBX_USE_OFDLOCKS)
endif()
################################################################################
################################################################################
################################################################################
if(PROVIDE_VERSIONINFO)
@ -261,9 +309,6 @@ else()
set(HAVE_MDBX_VERSIONINFO FALSE)
endif()
set(LIBMDBX_CONFIG_H "${CMAKE_CURRENT_BINARY_DIR}/mdbx-build-config.h")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/elements/config.h.in ${LIBMDBX_CONFIG_H})
add_subdirectory(src)
if(MDBX_ENABLE_TESTS)
add_subdirectory(test)

View File

@ -132,10 +132,10 @@ version.c: src/elements/version.c.in $(lastword $(MAKEFILE_LIST)) .git/HEAD .git
-e "s|\$${MDBX_VERSION_MINOR}|$(shell echo '$(MDBX_VERSION_GIT)' | cut -d . -f 2)|" \
-e "s|\$${MDBX_VERSION_RELEASE}|$(shell echo '$(MDBX_VERSION_GIT)' | cut -d . -f 3)|" \
-e "s|\$${MDBX_VERSION_REVISION}|$(shell git rev-list --count --no-merges HEAD || echo 'Please fetch tags and/or install latest git version')|" \
-e "s|@MDBX_OPTIONS@|$(MDBX_OPTIONS)|" \
-e "s|\$${MDBX_COMPILE_FLAGS}|\"$(CFLAGS) $(LDFLAGS)\"|" \
-e "s|@MDBX_OPTIONS_STRING@|$(MDBX_OPTIONS)|" \
-e "s|\$${MDBX_COMPILE_FLAGS}|$(CFLAGS) $(LDFLAGS)|" \
-e "s|@MDBX_BUILD_COMPILER@|$(shell set -o pipefail; $(CC) --version | head -1 || echo 'Please use GCC or CLANG compatible compiler')|" \
-e "s|\$${MDBX_BUILD_TARGET}|\"$(shell set -o pipefail; LC_ALL=C $(CC) -v 2>&1 | grep -i '^Target:' | cut -d ' ' -f 2- || echo 'Please use GCC or CLANG compatible compiler')\"|" \
-e "s|\$${MDBX_BUILD_TARGET}|$(shell set -o pipefail; LC_ALL=C $(CC) -v 2>&1 | grep -i '^Target:' | cut -d ' ' -f 2- || echo 'Please use GCC or CLANG compatible compiler')|" \
src/elements/version.c.in > $@
version.o: version.c $(lastword $(MAKEFILE_LIST))

View File

@ -9,6 +9,7 @@ TODO.md
mdbx.h
src/CMakeLists.txt
src/alloy.c
src/elements/config.h.in
src/elements/data.c
src/elements/internals.h
src/elements/defs.h

View File

@ -12,9 +12,6 @@
## <http://www.OpenLDAP.org/license.html>.
##
option(MDBX_ALLOY_MODE "Build MDBX library as single object file." ON)
#
# Get version
if(HAVE_MDBX_VERSIONINFO)
fetch_version(MDBX "${CMAKE_CURRENT_SOURCE_DIR}/../VERSION")
@ -35,48 +32,49 @@ else()
elements/defs.h elements/internals.h elements/osal.h
elements/core.c elements/osal.c elements/lck-${LIBMDBX_OSAL}.c)
endif()
add_library(mdbx_objects OBJECT ../mdbx.h ${CMAKE_CURRENT_BINARY_DIR}/version.c ${LIBMDBX_SOURCES})
set_target_properties(mdbx_objects PROPERTIES
INTERPROCEDURAL_OPTIMIZATION $<BOOL:${INTERPROCEDURAL_OPTIMIZATION}>
C_STANDARD 11
C_STANDARD_REQUIRED OFF
PUBLIC_HEADER "../mdbx.h"
)
target_compile_definitions(mdbx_objects PRIVATE "LIBMDBX_EXPORTS")
target_include_directories(mdbx_objects PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")
if(CC_HAS_FASTMATH)
target_compile_options(mdbx_objects PRIVATE "-ffast-math")
endif()
if(BUILD_FOR_NATIVE_CPU AND CC_HAS_ARCH_NATIVE)
target_compile_options(mdbx_objects PUBLIC "-march=native")
endif()
if(CC_HAS_VISIBILITY)
target_compile_options(mdbx_objects PRIVATE "-fvisibility=hidden")
endif()
list(APPEND LIBMDBX_SOURCES ../mdbx.h
"${CMAKE_CURRENT_BINARY_DIR}/version.c"
"${CMAKE_CURRENT_BINARY_DIR}/mdbx-config.h")
if(BUILD_SHARED_LIBS)
set(LIBMDBX_TYPE SHARED)
else()
set(LIBMDBX_TYPE STATIC)
endif()
add_library(mdbx ${LIBMDBX_TYPE} $<TARGET_OBJECTS:mdbx_objects>)
target_link_libraries(mdbx INTERFACE ${CMAKE_THREAD_LIBS_INIT})
if(LIBFPTU_TYPE STREQUAL "SHARED")
target_compile_definitions(mdbx INTERFACE "LIBMDBX_IMPORTS")
add_library(mdbx SHARED ${LIBMDBX_SOURCES})
target_compile_definitions(mdbx PRIVATE LIBMDBX_EXPORTS INTERFACE LIBMDBX_IMPORTS)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_compile_definitions(mdbx PRIVATE MDBX_BUILD_DLL)
endif()
if(CC_HAS_VISIBILITY AND (LTO_ENABLED OR INTERPROCEDURAL_OPTIMIZATION))
set_target_properties(mdbx PROPERTIES LINK_FLAGS "-fvisibility=hidden")
endif()
target_link_libraries(mdbx PRIVATE ${CMAKE_THREAD_LIBS_INIT})
else()
add_library(mdbx STATIC ${LIBMDBX_SOURCES})
target_link_libraries(mdbx INTERFACE ${CMAKE_THREAD_LIBS_INIT})
endif()
set_target_properties(mdbx PROPERTIES
INTERPROCEDURAL_OPTIMIZATION $<BOOL:${INTERPROCEDURAL_OPTIMIZATION}>
C_STANDARD 11
C_STANDARD_REQUIRED OFF
PUBLIC_HEADER "../mdbx.h")
target_include_directories(mdbx PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")
if(CC_HAS_FASTMATH)
target_compile_options(mdbx PRIVATE "-ffast-math")
endif()
if(BUILD_FOR_NATIVE_CPU AND CC_HAS_ARCH_NATIVE)
target_compile_options(mdbx PUBLIC "-march=native")
endif()
if(CC_HAS_VISIBILITY)
target_compile_options(mdbx PRIVATE "-fvisibility=hidden")
endif()
install(TARGETS mdbx
LIBRARY DESTINATION lib COMPONENT runtime
RUNTIME DESTINATION bin COMPONENT runtime
ARCHIVE DESTINATION lib/static COMPONENT devel
PUBLIC_HEADER DESTINATION include/libmdbx INCLUDES DESTINATION include COMPONENT devel
)
PUBLIC_HEADER DESTINATION include/libmdbx
INCLUDES DESTINATION include COMPONENT devel)
foreach(file mdbx.h LICENSE README.md AUTHORS)
install(FILES "../${file}" DESTINATION "include/libmdbx")
@ -88,7 +86,7 @@ endforeach()
#
# get definitions as a string of "-Dxyz=124 ..."
get_target_property(MDBX_DEFINITIONS mdbx_objects COMPILE_DEFINITIONS)
get_target_property(MDBX_DEFINITIONS mdbx COMPILE_DEFINITIONS)
if(NOT MDBX_DEFINITIONS)
set(MDBX_DEFINITIONS "")
endif()
@ -97,7 +95,7 @@ string(REGEX REPLACE "([^;]+)" " -D\\1" MDBX_DEFINITIONS "${MDBX_DEFINITIONS}")
string(STRIP MDBX_DEFINITIONS "${MDBX_DEFINITIONS}")
# get target compile options as a list
get_target_property(mdbx_compile_options mdbx_objects COMPILE_OPTIONS)
get_target_property(mdbx_compile_options mdbx COMPILE_OPTIONS)
if(NOT mdbx_compile_options)
set(mdbx_compile_options "")
endif()
@ -109,7 +107,9 @@ unset(cmake_c_options)
# append cmake's build-type flags and defines
if(NOT CMAKE_CONFIGURATION_TYPES)
string(REPLACE " " ";" cmake_cxx_options "${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPERCASE}}" "${CMAKE_C_DEFINES_${CMAKE_BUILD_TYPE_UPPERCASE}}")
string(REPLACE " " ";" cmake_cxx_options
"${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPERCASE}}"
"${CMAKE_C_DEFINES_${CMAKE_BUILD_TYPE_UPPERCASE}}")
list(APPEND mdbx_compile_options "${cmake_c_options}")
unset(cmake_c_options)
endif()
@ -131,49 +131,73 @@ string(STRIP "${MDBX_COMPILE_FLAGS}${MDBX_DEFINITIONS}" MDBX_COMPILE_FLAGS)
if(CMAKE_CONFIGURATION_TYPES)
# add dynamic part via per-configuration define
message(STATUS "MDBX Compile Flags: ${MDBX_COMPILE_FLAGS} <AND CONFIGURATION DEPENDENT>")
set(MDBX_COMPILE_FLAGS "MDBX_COMPILE_FLAGS \"${MDBX_COMPILE_FLAGS}\"")
set(MDBX_COMPILE_FLAGS "${MDBX_COMPILE_FLAGS}")
add_definitions(
-DMDBX_COMPILE_FLAGS="$<$<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}>"
)
else()
message(STATUS "MDBX Compile Flags: ${MDBX_COMPILE_FLAGS}")
set(MDBX_COMPILE_FLAGS "\"${MDBX_COMPILE_FLAGS}\"")
set(MDBX_COMPILE_FLAGS "${MDBX_COMPILE_FLAGS}")
endif()
# get compiler info
execute_process(COMMAND sh -c "${CMAKE_C_COMPILER} --version | head -1"
OUTPUT_VARIABLE MDBX_BUILD_COMPILER
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
RESULT_VARIABLE rc)
if(rc OR NOT MDBX_BUILD_COMPILER)
set(MDBX_BUILD_COMPILER "")
else()
set(MDBX_BUILD_COMPILER " | ${MDBX_BUILD_COMPILER}" )
endif()
string(STRIP "${CMAKE_C_COMPILER_ID}-${CMAKE_C_COMPILER_VERSION}${MDBX_BUILD_COMPILER}" MDBX_BUILD_COMPILER)
# make a build-target triplet
if(CMAKE_CONFIGURATION_TYPES)
# via per-configuration define
add_definitions(-DMDBX_BUILD_TARGET="${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-$<CONFIG>")
add_definitions(-DMDBX_BUILD_TARGET="${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_SYSTEM_NAME}-$<CONFIG>")
set(MDBX_BUILD_TARGET "MDBX_BUILD_TARGET")
else()
string(STRIP "\"${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_BUILD_TYPE}\"" MDBX_BUILD_TARGET)
string(STRIP "${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_SYSTEM_NAME}-${CMAKE_BUILD_TYPE}" MDBX_BUILD_TARGET)
endif()
# generate version file
# options
string(TIMESTAMP MDBX_BUILD_TIMESTAMP UTC)
string(REPLACE " " " " MDBX_OPTIONS "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}"
" -DENABLE_ASAN=${ENABLE_ASAN}"
" -DENABLE_VALGRIND=${ENABLE_VALGRIND}"
" -DENABLE_GPROF=${ENABLE_GPROF}"
" -DENABLE_GCOV=${ENABLE_GCOV}"
" -DENABLE_BACKTRACE=${ENABLE_BACKTRACE}"
)
string(STRIP "${CMAKE_C_COMPILER_ID}-${CMAKE_C_COMPILER_VERSION}" MDBX_BUILD_COMPILER)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/elements/version.c.in ${CMAKE_CURRENT_BINARY_DIR}/version.c)
set(options VERSION C_COMPILER C_COMPILER DEFINITIONS
ENABLE_GCOV ENABLE_GPROF ENABLE_VALGRIND ENABLE_BACKTRACE BUILD_TARGET
HAVE_BFD ENABLE_ASAN)
foreach(option IN LISTS options)
if(DEFINED MDBX_${option})
set(value ${MDBX_${option}})
elseif(DEFINED ${option})
set(value "${${option}}")
set(MDBX_OPTIONS_STRING -DCMAKE_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}")
foreach(item IN LISTS MDBX_BUILD_OPTIONS)
if(DEFINED ${item} AND NOT "${${item}}" STREQUAL "AUTO")
string(APPEND MDBX_OPTIONS_STRING " -D${item}=${${item}}")
message(STATUS "${item}: ${${item}}")
else()
set(value "${CMAKE_${option}}")
unset(${item} CACHE)
set(${item}_AUTO ON)
string(APPEND MDBX_OPTIONS_STRING " -D${item}=AUTO")
message(STATUS "${item}: AUTO")
endif()
message(STATUS "MDBX_${option}: ${value}")
endforeach(option)
endforeach()
set(options VERSION C_COMPILER CXX_COMPILER DEFINITIONS)
foreach(item IN LISTS options)
if(DEFINED ${item})
set(value "${${item}}")
elseif(DEFINED MDBX_${item})
set(item MDBX_${item})
set(value "${${item}}")
elseif(DEFINED CMAKE_${item})
set(item CMAKE_${item})
set(value "${${item}}")
else()
set(value "undefined")
endif()
message(STATUS "${item}: ${value}")
endforeach(item)
# generate version file
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/elements/version.c.in"
"${CMAKE_CURRENT_BINARY_DIR}/version.c" ESCAPE_QUOTES)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/elements/config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/mdbx-config.h")
target_compile_definitions(mdbx PRIVATE MDBX_CONFIG_H="${CMAKE_CURRENT_BINARY_DIR}/mdbx-config.h")
add_subdirectory(tools)

View File

@ -1,10 +1,4 @@
#cmakedefine HAVE_UNISTD_H
#cmakedefine HAVE_SYS_UIO_H
#cmakedefine HAVE_SYS_STAT_H
#cmakedefine HAVE_TIMEVAL_TV_USEC
#cmakedefine HAVE_TIMESPEC_TV_NSEC
#cmakedefine CMAKE_HAVE_PTHREAD_H
#cmakedefine HAVE_VALGRIND_MEMCHECK_H
#cmakedefine HAVE_VALGRIND_MEMCHECK_H
#cmakedefine HAS_RELAXED_CONSTEXPR
#cmakedefine LTO_ENABLED
@ -14,3 +8,29 @@
#cmakedefine ENABLE_ASAN
#cmakedefine01 HAVE_MDBX_VERSIONINFO
/* Common */
#cmakedefine01 MDBX_TXN_CHECKPID
#cmakedefine01 MDBX_TXN_CHECKOWNER
/* Windows */
#cmakedefine MDBX_BUILD_DLL
#cmakedefine01 MDBX_CONFIG_MANUAL_TLS_CALLBACK
#cmakedefine MDBX_AVOID_CRT
/* MacOS */
#cmakedefine01 MDBX_OSX_SPEED_INSTEADOF_DURABILITY
/* POSIX */
#cmakedefine01 MDBX_USE_ROBUST
#cmakedefine01 MDBX_USE_OFDLOCKS
/* Simulate "AUTO" values of tristate options */
#cmakedefine MDBX_USE_ROBUST_AUTO
#ifdef MDBX_USE_ROBUST_AUTO
#undef MDBX_USE_ROBUST
#endif
#cmakedefine MDBX_USE_OFDLOCKS_AUTO
#ifdef MDBX_USE_OFDLOCKS_AUTO
#undef MDBX_USE_OFDLOCKS
#endif

View File

@ -15,6 +15,10 @@
/* *INDENT-OFF* */
/* clang-format off */
#ifdef MDBX_CONFIG_H
#include MDBX_CONFIG_H
#endif
/* In case the MDBX_DEBUG is undefined set it corresponding to NDEBUG */
#ifndef MDBX_DEBUG
#ifdef NDEBUG

View File

@ -719,14 +719,14 @@ MDBX_INTERNAL_FUNC void mdbx_rdt_unlock(MDBX_env *env);
/// \brief Захватывает блокировку для изменения БД (при старте пишущей
/// транзакции). Транзакции чтения при этом никак не блокируются.
/// Объявлена без MDBX_INTERNAL_FUNC так как используется в mdbx_chk.
/// Объявлена LIBMDBX_API так как используется в mdbx_chk.
/// \return Код ошибки или 0 в случае успеха.
int mdbx_txn_lock(MDBX_env *env, bool dontwait);
LIBMDBX_API int mdbx_txn_lock(MDBX_env *env, bool dontwait);
/// \brief Освобождает блокировку по окончанию изменения БД (после завершения
/// пишущей транзакции).
/// Объявлена без MDBX_INTERNAL_FUNC так как используется в mdbx_chk.
void mdbx_txn_unlock(MDBX_env *env);
/// Объявлена LIBMDBX_API так как используется в mdbx_chk.
LIBMDBX_API void mdbx_txn_unlock(MDBX_env *env);
/// \brief Устанавливает alive-флажок присутствия (индицирующую блокировку)
/// читателя для pid текущего процесса. Функции может выполнить не более

View File

@ -36,5 +36,5 @@
"@MDBX_GIT_DESCRIBE@"}};
/*LIBMDBX_API*/ const mdbx_build_info mdbx_build = {
"@MDBX_BUILD_TIMESTAMP@", ${MDBX_BUILD_TARGET}, "@MDBX_OPTIONS@",
"@MDBX_BUILD_COMPILER@", ${MDBX_COMPILE_FLAGS}};
"@MDBX_BUILD_TIMESTAMP@", "${MDBX_BUILD_TARGET}", "@MDBX_OPTIONS_STRING@",
"@MDBX_BUILD_COMPILER@", "${MDBX_COMPILE_FLAGS}"};

View File

@ -1,4 +1,4 @@
set(MDBX_TOOLS
set(MDBX_TOOLS
mdbx_chk
mdbx_copy
mdbx_dump
@ -13,7 +13,7 @@ foreach (TOOL ${MDBX_TOOLS})
add_executable(${TOOL} ${TOOL}.c)
endif()
target_link_libraries(${TOOL} mdbx_objects ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(${TOOL} mdbx)
set_target_properties(${TOOL} PROPERTIES
INTERPROCEDURAL_OPTIMIZATION $<BOOL:${INTERPROCEDURAL_OPTIMIZATION}>)

View File

@ -63,7 +63,7 @@ if(CC_HAS_VISIBILITY AND (LTO_ENABLED OR INTERPROCEDURAL_OPTIMIZATION))
set_target_properties(mdbx_test PROPERTIES LINK_FLAGS "-fvisibility=hidden")
endif()
target_link_libraries(mdbx_test mdbx ${LIB_MATH})
target_link_libraries(mdbx_test mdbx ${LIB_MATH} ${CMAKE_THREAD_LIBS_INIT})
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_link_libraries(mdbx_test winmm.lib)
endif()