Skip to content

Commit

Permalink
Merge pull request #18 from FlyingOE/release200
Browse files Browse the repository at this point in the history
revamp kdb+ plugin's build with vcpkg on Windows
  • Loading branch information
hzwale authored Aug 22, 2023
2 parents 6bbadf9 + 860e22f commit b1f086d
Show file tree
Hide file tree
Showing 7 changed files with 897 additions and 580 deletions.
4 changes: 3 additions & 1 deletion include/WideInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace wide_integer {

#ifndef __SIZEOF_INT128__
#error "Current compiler does not support __int128"
# error "Current compiler does not support __int128"
#elif !defined(_GLIBCXX_USE_INT128)
# define _GLIBCXX_USE_INT128 1 // This macro was removed since r12-435
#endif

using int128 = __int128;
Expand Down
146 changes: 103 additions & 43 deletions kdb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,56 +1,116 @@
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)

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 ()
#=== 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 ("${CMAKE_BUILD_TYPE}" MATCHES Debug)
set(optimize_opts -g -O0)
set(dep_debug_tag "d")
elseif("${CMAKE_BUILD_TYPE}" MATCHES Release)
set(optimize_opts -O3 )
set(dep_debug_tag "")
elseif("${CMAKE_BUILD_TYPE}" MATCHES RelWithDebInfo)
set(optimize_opts -g -O2)
set(dep_debug_tag "")
else()
message(FATAL_ERROR "Unknown build type: ${CMAKE_BUILD_TYPE}")
endif ()
endif()

target_compile_options(PluginKDB PRIVATE ${optimize_opts})
if(WIN32)
set(platform_macro WINDOWS)
set(install_type win)
set(kdb_lib "${PROJECT_SOURCE_DIR}/lib/w64/c.lib")
configure_file(
"${PROJECT_SOURCE_DIR}/PluginKDB_WIN32.txt"
"${PROJECT_BINARY_DIR}/PluginKDB.txt" COPYONLY)
#FIXME: CMake 3.21 offers better way to do this: install(IMPORTED_RUNTIME_ARTIFACTS ...)
configure_file(
"${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin/zlib1.dll"
"${PROJECT_BINARY_DIR}/zlib${dep_debug_tag}1.dll" COPYONLY)
set(extra_bin
"${PROJECT_SOURCE_DIR}/lib/w64/c.dll"
"${PROJECT_BINARY_DIR}/zlib${dep_debug_tag}1.dll"
)
elseif(UNIX)
set(platform_macro LINUX)
set(install_type linux)
set(kdb_lib "${PROJECT_SOURCE_DIR}/lib/c.o")
configure_file(
"${PROJECT_SOURCE_DIR}/PluginKDB.txt"
"${PROJECT_BINARY_DIR}/PluginKDB.txt" COPYONLY)
set(extra_bin )
else()
message(FATAL_ERROR "FIXME: Add build support for this platform!")
endif()
target_compile_definitions(PluginKDB PUBLIC ${platform_macro})

#=== 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)
#=== 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}"
)
62 changes: 62 additions & 0 deletions kdb/CMake_mingw+vcpkg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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=<path_to_DolphinDB>
```

### 2. Build using CMake

```batch
del /S /Q build
:: Release mode
cmake -S . -B build -G Ninja
:: Debug mode
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug
:: RelWithDebInfo mode
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake --build build -j 4
cmake --install build
```
43 changes: 24 additions & 19 deletions kdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@ The plugin requires zlib for data decompression.

- Ubuntu

```
```bash
sudo apt install zlib1g
```

- CentOS

```
```bash
yum install -y zlib zlib-devel
```

### 1.2 Load Plugin

Download the precompiled plugin and library files at [DolphinDBPlugin/kdb/](https://github.com/dolphindb/DolphinDBPlugin/tree/release200/kdb). Enter the following command in DolphinDB to load the plugin:

```
```DolphinDB
loadPlugin("/path/to/plugin/PluginKDB.txt")
```

## 2. (Optional) Manually Compile Plugin

### Linux

```
```bash
cd /path/to/plugins/kdb
mkdir build
cd build
Expand All @@ -76,7 +76,9 @@ After compilation, refer to the description in [1.2 Load Plugin](#12-load-plugin

**Syntax**

```DolphinDB
connect(host, port, usernamePassword)
```

**Arguments**

Expand All @@ -98,24 +100,27 @@ If the connection fails, an exception is thrown. Possible causes are:

Suppose the username and password ("admin:123456") are stored in *../passwordfiles/usrs*, and the kdb+ server and DolphinDB server are both on the same machine:

kdb+ shell:
```bash
q -p 5000 -U ../passwordfiles/usrs # note that "-U" must be capitalized
```
kdb shell: q -p 5000 -U ../passwordfiles/usrs // note that "-U" must be capitalized
DolphinDB shell: handle = kdb::connect("127.0.0.1", 5000, "admin:123456")

DolphinDB shell:
```DolphinDB
handle = kdb::connect("127.0.0.1", 5000, "admin:123456")
```

If the kdb+ database you're connecting to does not require authentication:

```
```DolphinDB
handle = kdb::connect("127.0.0.1", 5000)
```



### 3.2 kdb::loadTable

**Syntax**

```
```DolphinDB
loadTable(handle, tablePath, symPath)
```

Expand All @@ -135,7 +140,7 @@ Load data from a connected kdb+ database as an in-memory table in DolphinDB.
**Examples**

```
```DolphinDB
// the table contains an enumerated symbol column
DATA_DIR="/path/to/data/kdb_sample"
Txns = kdb::loadTable(handle, DATA_DIR + "/2022.06.17/Txns", DATA_DIR + "/sym")
Expand All @@ -149,7 +154,7 @@ Txns = kdb::loadTable(handle, DATA_DIR + "/2022.06.17/Txns", DATA_DIR)

**Syntax**

```
```DolphinDB
loadFile(tablePath, symPath)
```

Expand All @@ -168,7 +173,7 @@ Directly read the specified kdb+ data files on disk and load the file data to Do
**Examples**

```
```DolphinDB
//the table contains an enumerated symbol column
DATA_DIR="/path/to/data/kdb_sample"
Txns = kdb::loadFile(handle, DATA_DIR + "/2022.06.17/Txns", DATA_DIR + "/sym")
Expand All @@ -183,7 +188,7 @@ Txns = kdb::loadFile(handle, DATA_DIR + "/2022.06.17/Txns", DATA_DIR)

**Syntax**

```
```DolphinDB
close(handle)
```

Expand All @@ -197,13 +202,13 @@ Close the connection to the kdb+ server.

**Examples**

```
```DolphinDB
kdb::close(handle)
```

### 3.5 A Complete Example

```
```DolphinDB
loadPlugin("/home/DolphinDBPlugin/kdb/build/PluginKDB.txt")
go
// connect to the kdb+ database
Expand Down Expand Up @@ -267,7 +272,7 @@ path/to/data
└── table_name
```

```
```DolphinDB
handle = kdb::connect("127.0.0.1", 5000, "username:password");
table = kdb::loadTable(handle, "path/to/data/table_name", "path/to/data/sym");
```
Expand All @@ -290,7 +295,7 @@ path/to/data
└── ti
```

```
```DolphinDB
handle = kdb::connect("127.0.0.1", 5000, "username:password");
table1 = kdb::loadTable(handle, "path/to/data/table_name/", "path/to/data/sym");
table2 = kdb::loadTable("path/to/data/table_name/", "path/to/data/sym");
Expand Down Expand Up @@ -320,7 +325,7 @@ path/to/data
└── ti
```

```
```DolphinDB
// get the information on all files under the directory
fileRes=files("path/to/data");
Expand Down
Loading

0 comments on commit b1f086d

Please sign in to comment.