From 0db5226e5aee41281eb21ba368f8a8cfd8a63451 Mon Sep 17 00:00:00 2001 From: Freddie Wu Date: Wed, 16 Aug 2023 00:51:46 +0800 Subject: [PATCH] implement #15; restructured CMakeLists.txt to 3.x-style rules --- include/WideInteger.h | 4 +- kdb/CMakeLists.txt | 135 ++++++++++++++++++++++++++------------- kdb/CMake_mingw+vcpkg.md | 55 ++++++++++++++++ 3 files changed, 150 insertions(+), 44 deletions(-) create mode 100644 kdb/CMake_mingw+vcpkg.md diff --git a/include/WideInteger.h b/include/WideInteger.h index 0499d1d4..c53c0b4c 100644 --- a/include/WideInteger.h +++ b/include/WideInteger.h @@ -12,7 +12,9 @@ namespace wide_integer { #ifndef __SIZEOF_INT128__ - #error "Current compiler does not support __int128" +# error "Current compiler does not support __int128" +#else +# define _GLIBCXX_USE_INT128 // This macro was removed since r12-435 #endif using int128 = __int128; diff --git a/kdb/CMakeLists.txt b/kdb/CMakeLists.txt index e490d961..9a13a6c6 100644 --- a/kdb/CMakeLists.txt +++ b/kdb/CMakeLists.txt @@ -1,56 +1,105 @@ -cmake_minimum_required(VERSION 3.0) -project(PluginKDB) -add_definitions(-DLOCKFREE_SYMBASE) +cmake_minimum_required(VERSION 3.1) -set(CMAKE_CXX_STANDARD 11) +#======================================== +if("${CMAKE_BUILD_TYPE}" MATCHES "") + set(CMAKE_BUILD_TYPE "Release" CACHE STRING "") +endif () +message(STATUS "CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE}) -if (WIN32) - add_definitions("-DWINDOWS -D_WIN32_WINNT=0x0600 -DWINVER=0x0600") - set(plugin_file "${CMAKE_CURRENT_LIST_DIR}/PluginKDB_WIN32.txt") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${platform_macro} -DKXVER=3 -Wall") - file(COPY ${PROJECT_SOURCE_DIR}/lib/w64/c.dll DESTINATION ${CMAKE_BINARY_DIR}/) -elseif(UNIX) - set(platform_macro "-DLINUX") - set(plugin_file "${CMAKE_CURRENT_LIST_DIR}/PluginKDB.txt") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${platform_macro} -DKXVER=3 -Wall -fPIC") +#=== vcpkg +if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) + set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "") endif() -if("${CMAKE_BUILD_TYPE}" STREQUAL "") - set(CMAKE_BUILD_TYPE "RELEASE") -endif () +if(NOT DEFINED VCPKG_TARGET_TRIPLET) + set(VCPKG_TARGET_TRIPLET x64-windows) # Use x64-windows by default +endif() +message(STATUS "VCPKG_TARGET_TRIPLET: " ${VCPKG_TARGET_TRIPLET}) -string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE) -message("CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE}) +#======================================== +project(PluginKDB LANGUAGES CXX) + +#=== Source Code +file(GLOB PluginKDB_HEADER CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/src/*.h ) +file(GLOB PluginKDB_SOURCE CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/src/*.cpp) + +#=== Build Target +add_library(PluginKDB SHARED) +set_property(TARGET PluginKDB PROPERTY CXX_STANDARD 11) +target_sources(PluginKDB PUBLIC ${PluginKDB_HEADER} PRIVATE ${PluginKDB_SOURCE}) +target_compile_options(PluginKDB PRIVATE -Wall) + +#=== Build Types +if(WIN32) + set(platform_macro WINDOWS) + set(install_type win) + set(kdb_lib "${PROJECT_SOURCE_DIR}/lib/w64/c.lib") + set(extra_bin + "${PROJECT_SOURCE_DIR}/lib/w64/c.dll" + "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin/zlib1.dll" + ) #FIXME: CMake 3.21 offers better way to do this! + configure_file("${PROJECT_SOURCE_DIR}/PluginKDB_WIN32.txt" "${PROJECT_BINARY_DIR}/PluginKDB.txt" COPYONLY) +elseif(UNIX) + set(platform_macro LINUX) + set(install_type linux) + set(kdb_lib "${PROJECT_SOURCE_DIR}/lib/c.o") + set(extra_bin ) + configure_file("${PROJECT_SOURCE_DIR}/PluginKDB.txt" "${PROJECT_BINARY_DIR}/PluginKDB.txt" COPYONLY) +else() + message(FATAL_ERROR "FIXME: Add build support for this platform!") +endif() +target_compile_definitions(PluginKDB PUBLIC ${platform_macro}) -if("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0") -elseif("${CMAKE_BUILD_TYPE}" STREQUAL "RELEASE") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") -elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RELWITHDEBINFO") # release with debug info - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2") -else () +if ("${CMAKE_BUILD_TYPE}" MATCHES Debug) + set(optimize_opts -g -O0) +elseif("${CMAKE_BUILD_TYPE}" MATCHES Release) + set(optimize_opts -O3 ) +elseif("${CMAKE_BUILD_TYPE}" MATCHES RelWithDebInfo) + set(optimize_opts -g -O2) +else() message(FATAL_ERROR "Unknown build type: ${CMAKE_BUILD_TYPE}") -endif () +endif() +target_compile_options(PluginKDB PRIVATE ${optimize_opts}) +#=== ASAN or not? if (${DDB_USE_ASAN}) - add_compile_options( - "-fsanitize=address" # Enable ASAN. - "-fno-omit-frame-pointer" # Nicer stack traces in error messages. - "-fno-optimize-sibling-calls" # Disable tail call elimination (perfect stack traces if inlining off). - ) - set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-fsanitize=address") -endif () + target_compile_options(PluginKDB PRIVATE + -fsanitize=address # Enable ASAN. + -fno-omit-frame-pointer # Nicer stack traces in error messages. + -fno-optimize-sibling-calls # Disable tail call elimination (perfect stack traces if inlining off). + ) + target_link_options(PluginKDB PRIVATE + -fsanitize=address + ) +endif() -include_directories(${CMAKE_SOURCE_DIR}/../include) -link_directories("${CMAKE_BINARY_DIR}") +#=== Dependencies +if(NOT DEFINED DOLPHINDB_ROOT) + if(NOT DEFINED ENV{DOLPHINDB_ROOT}) + message(FATAL_ERROR "DOLPHINDB_ROOT should be defined to provide path to DolphinDB installation!") + else() + set(DOLPHINDB_ROOT "$ENV{DOLPHINDB_ROOT}") + endif() +endif() +target_compile_definitions(PluginKDB PUBLIC LOCKFREE_SYMBASE) +target_include_directories(PluginKDB PUBLIC "${PROJECT_SOURCE_DIR}/../include") +target_link_directories (PluginKDB PRIVATE "${DOLPHINDB_ROOT}") +target_link_libraries (PluginKDB PRIVATE DolphinDB) -aux_source_directory(${PROJECT_SOURCE_DIR}/src DIR_SRCS) -add_library(PluginKDB SHARED ${DIR_SRCS}) +#== zlib +find_package(ZLIB REQUIRED) +target_link_libraries (PluginKDB PRIVATE ZLIB::ZLIB) -if (WIN32) -target_link_libraries(PluginKDB DolphinDB ${PROJECT_SOURCE_DIR}/lib/w64/c.lib ${PROJECT_SOURCE_DIR}/lib/w64/c.dll z) -elseif(UNIX) -target_link_libraries(PluginKDB DolphinDB ${PROJECT_SOURCE_DIR}/lib/c.o z) -endif() +#== kdb+ +target_compile_definitions(PluginKDB PUBLIC KXVER=3) +target_link_directories (PluginKDB PUBLIC "${CMAKE_BINARY_DIR}") +target_link_libraries (PluginKDB PRIVATE ${kdb_lib}) -configure_file(${plugin_file} ${CMAKE_BINARY_DIR}/PluginKDB.txt COPYONLY) \ No newline at end of file +#=== Post-build Actions +install(TARGETS PluginKDB + RUNTIME DESTINATION "${PROJECT_SOURCE_DIR}/bin/${install_type}" +) +install(FILES + "${PROJECT_BINARY_DIR}/PluginKDB.txt" ${extra_bin} + DESTINATION "${PROJECT_SOURCE_DIR}/bin/${install_type}" +) diff --git a/kdb/CMake_mingw+vcpkg.md b/kdb/CMake_mingw+vcpkg.md new file mode 100644 index 00000000..a2485007 --- /dev/null +++ b/kdb/CMake_mingw+vcpkg.md @@ -0,0 +1,55 @@ +# kdb+ Plugin for DolphinDB using CMake & MinGW + +## Requirements + +### 1. MinGW-w64 + +https://www.mingw-w64.org/downloads/#mingw-builds + +- In order to be consistent with DolphinDB, it is recommended to use the `x86_64`/`win32`/`seh` build of MinGW-w64. + +- The following version of MinGW-w64 was considered: + + - The original kdb+ plugin on Windows was built with `x86_64-5.3.0-win32-seh-rt_v4-rev0.zip` + +### 2. CMake + +https://cmake.org/download/ + +Alternatively, the build-in CMake within Visual Studio can also be used. + +### 3. vcpkg + +https://vcpkg.io/en/getting-started + +A short version of how to install vcpkg: + +```batch +git clone https://github.com/Microsoft/vcpkg.git +.\vcpkg\bootstrap-vcpkg.bat +SET VCPKG_ROOT=%CD%\vcpkg +%VCPKG_ROOT%\vcpkg integrate install +``` + +## Build Steps + +The follow commands assume that you are under the `DolphinDBPlugin\kdb\` directory. + +### 1. Install dependencies + +```batch +%VCPKG_ROOT%\vcpkg install zlib:x64-windows +``` + +```batch +SET DOLPHINDB_ROOT= +``` + +### 2. Build using CMake + +```batch +del /S /Q build +cmake -S . -B build -G Ninja +cmake --build build +cmake --install build +```