Skip to content

Commit 7ca2e79

Browse files
authored
Support static libtsfile builds (#894)
1 parent ea09459 commit 7ca2e79

7 files changed

Lines changed: 74 additions & 21 deletions

File tree

cpp/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ if (POLICY CMP0079)
3131
endif ()
3232
set(TsFile_CPP_VERSION 2.3.2.dev)
3333

34+
option(TSFILE_BUILD_SHARED "Build libtsfile as a shared library" ON)
35+
message("cmake using: TSFILE_BUILD_SHARED=${TSFILE_BUILD_SHARED}")
36+
3437
if (MSVC)
3538
# MSVC does not provide a /std:c++11 flag; C++11 is its implicit baseline.
3639
# The lowest explicitly settable standard is /std:c++14. Without this flag,
@@ -39,9 +42,11 @@ if (MSVC)
3942
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} /W3 /utf-8 /EHsc /bigobj /Zc:__cplusplus /std:c++14")
4043
add_definitions(-DNOMINMAX -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS
4144
-D_SCL_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS)
42-
# Export all symbols of the tsfile shared library automatically so that
43-
# consumers do not need __declspec(dllexport) annotations.
44-
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
45+
if (TSFILE_BUILD_SHARED)
46+
# Export all symbols of the tsfile shared library automatically so that
47+
# consumers do not need __declspec(dllexport) annotations.
48+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
49+
endif ()
4550
else ()
4651
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -Wall")
4752
endif ()

cpp/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,29 @@ mvn clean verify -P with-cpp -Dcpp.toolchain=mingw
132132
mvn clean verify -P with-cpp -Dcpp.toolchain=msvc
133133
```
134134

135-
Then you can find the shared library at `./cpp/target/build/lib`.
135+
By default, the shared library is written to `./cpp/target/build/lib`.
136+
137+
To build `libtsfile` as a static library instead, disable
138+
`TSFILE_BUILD_SHARED` through Maven:
139+
140+
```bash
141+
mvn clean verify -P with-cpp -Dtsfile.build.shared=OFF
142+
```
143+
144+
The static library is written to the same directory (`libtsfile.a` on
145+
Linux/macOS and `tsfile.lib` on Windows). When consuming the installed archive
146+
directly on MSVC rather than linking the CMake `tsfile` target, define
147+
`TSFILE_STATIC` for the consumer so public headers do not use DLL import
148+
decorations.
149+
150+
For a direct CMake build, use:
151+
152+
```bash
153+
cmake -S cpp -B cpp/build/static \
154+
-DTSFILE_BUILD_SHARED=OFF \
155+
-DBUILD_TEST=OFF
156+
cmake --build cpp/build/static --target tsfile
157+
```
136158

137159
Before you submit your code to GitHub, please ensure that the compilation is correct.
138160

cpp/build.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# build_type=MinSizeRel
2121
build_type=Release
2222
build_test=0
23+
build_shared=ON
2324
build_bench=0
2425
do_install=0
2526
use_cpp11=1
@@ -52,6 +53,9 @@ Options:
5253
-t=<type>, -t <type> Build type: Debug, Release, RelWithDebInfo, MinSizeRel.
5354
-a=<ON|OFF> Enable or disable AddressSanitizer.
5455
-c=<ON|OFF> Enable or disable code coverage.
56+
--build-shared=<ON|OFF>
57+
Build libtsfile as a shared library (default: ON).
58+
--build-static Build libtsfile as a static library.
5559
--enable-antlr4=<ON|OFF>
5660
--disable-antlr4
5761
--enable-snappy=<ON|OFF>
@@ -70,6 +74,7 @@ function print_config()
7074
{
7175
echo "build_type=$build_type"
7276
echo "build_test=$build_test"
77+
echo "build_shared=$build_shared"
7378
echo "do_install=$do_install"
7479
echo "use_cpp11=$use_cpp11"
7580
echo "enable_cov=$enable_cov"
@@ -113,6 +118,10 @@ parse_options()
113118
-c)
114119
shift
115120
enable_cov=$(get_key_value "$1");;
121+
--build-shared=*)
122+
build_shared=$(get_key_value "$1");;
123+
--build-static)
124+
build_shared=OFF;;
116125
--enable-antlr4=*)
117126
enable_antlr4=$(get_key_value "$1");;
118127
--enable-snappy=*)
@@ -190,6 +199,7 @@ cmake ../../ \
190199
-DZLIB=$zlib_project_dir/install \
191200
-DLZ4LIB=$lz4lib_project_dir \
192201
-DBUILD_TEST=$build_test \
202+
-DTSFILE_BUILD_SHARED=$build_shared \
193203
-DCMAKE_BUILD_TYPE=$build_type \
194204
-DUSE_CPP11=$use_cpp11 \
195205
-DENABLE_COV=$enable_cov \

cpp/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<build.type>Release</build.type>
3636
<enable.asan>OFF</enable.asan>
3737
<build.test>ON</build.test>
38+
<tsfile.build.shared>ON</tsfile.build.shared>
3839
<enable.snappy>ON</enable.snappy>
3940
<enable.lz4>ON</enable.lz4>
4041
<enable.lzokay>ON</enable.lzokay>
@@ -82,6 +83,7 @@
8283
<option>-DCMAKE_BUILD_TYPE=${build.type}</option>
8384
<option>-DENABLE_ASAN=${enable.asan}</option>
8485
<option>-DBUILD_TEST=${build.test}</option>
86+
<option>-DTSFILE_BUILD_SHARED=${tsfile.build.shared}</option>
8587
<option>-DENABLE_ANTLR4=${enable.antlr4}</option>
8688
<option>-DENABLE_SNAPPY=${enable.snappy}</option>
8789
<option>-DENABLE_LZ4=${enable.lz4}</option>

cpp/src/CMakeLists.txt

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ endif()
101101

102102
include_directories(${PROJECT_INCLUDE_DIR})
103103

104-
# Mark every translation unit that is compiled into the tsfile library so that
105-
# TSFILE_API (see utils/util_define.h) resolves to an export-side (empty)
106-
# decoration here, and to __declspec(dllimport) for external consumers.
107-
add_definitions(-DTSFILE_BUILDING)
104+
# Configure TSFILE_API (see utils/util_define.h) for every translation unit
105+
# compiled into the tsfile library. Shared builds export data symbols, while
106+
# static builds do not use DLL import/export decorations.
107+
if (TSFILE_BUILD_SHARED)
108+
add_definitions(-DTSFILE_BUILDING)
109+
else()
110+
add_definitions(-DTSFILE_STATIC)
111+
endif()
108112

109113
if (ENABLE_ANTLR4)
110114
add_subdirectory(parser)
@@ -150,7 +154,14 @@ target_link_libraries(common_obj ${COMPRESSION_LIBS})
150154
target_link_libraries(read_obj ${COMPRESSION_LIBS})
151155
target_link_libraries(write_obj ${COMPRESSION_LIBS})
152156

153-
add_library(tsfile SHARED)
157+
if (TSFILE_BUILD_SHARED)
158+
add_library(tsfile SHARED)
159+
else()
160+
add_library(tsfile STATIC)
161+
# Consumers of the CMake target must see TSFILE_API without DLL import
162+
# decoration when linking the static library on MSVC.
163+
target_compile_definitions(tsfile INTERFACE TSFILE_STATIC)
164+
endif()
154165

155166
if (${COV_ENABLED})
156167
message("Enable code cov...")
@@ -177,14 +188,16 @@ endif()
177188

178189
add_dependencies(tsfile utils_obj encoding_obj)
179190

180-
set(LIBTSFILE_PROJECT_VERSION ${TsFile_CPP_VERSION})
181-
set(LIBTSFILE_SO_VERSION ${TsFile_CPP_VERSION})
182-
set_target_properties(tsfile PROPERTIES VERSION ${LIBTSFILE_PROJECT_VERSION})
183-
set_target_properties(tsfile PROPERTIES SOVERSION ${LIBTSFILE_SO_VERSION})
191+
if (TSFILE_BUILD_SHARED)
192+
set(LIBTSFILE_PROJECT_VERSION ${TsFile_CPP_VERSION})
193+
set(LIBTSFILE_SO_VERSION ${TsFile_CPP_VERSION})
194+
set_target_properties(tsfile PROPERTIES VERSION ${LIBTSFILE_PROJECT_VERSION})
195+
set_target_properties(tsfile PROPERTIES SOVERSION ${LIBTSFILE_SO_VERSION})
196+
endif()
184197

185-
# On Windows a SHARED library produces a .dll (RUNTIME) plus an import .lib
186-
# (ARCHIVE); on Unix it produces a .so (LIBRARY). Cover all three so the
187-
# install step works for every platform.
198+
# A shared library is a RUNTIME plus an import ARCHIVE on Windows and a LIBRARY
199+
# on Unix. A static library is an ARCHIVE on every platform. Cover all three so
200+
# the install step works for either library type.
188201
install(TARGETS tsfile
189202
RUNTIME DESTINATION ${LIBRARY_OUTPUT_PATH}
190203
LIBRARY DESTINATION ${LIBRARY_OUTPUT_PATH}

cpp/src/utils/util_define.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,18 @@ typedef int mode_t;
6464
#endif
6565
#endif // _WIN32
6666

67-
/* ======== shared-library symbol visibility ========
67+
/* ======== library symbol visibility ========
6868
*
6969
* Functions are exported from tsfile.dll automatically via
7070
* CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS, but global DATA symbols (plain variables,
7171
* static class members) are not reliably auto-exported, and a consumer must
7272
* see __declspec(dllimport) to reference them across the DLL boundary. Mark
7373
* such symbols with TSFILE_API: it expands to dllexport while building the
74-
* library (TSFILE_BUILDING is defined for its own translation units),
75-
* dllimport for external consumers, and nothing on non-MSVC toolchains.
74+
* shared library (TSFILE_BUILDING is defined for its own translation units),
75+
* dllimport for shared-library consumers, and nothing for static builds or
76+
* non-MSVC toolchains.
7677
*/
77-
#if defined(_MSC_VER)
78+
#if defined(_MSC_VER) && !defined(TSFILE_STATIC)
7879
#if defined(TSFILE_BUILDING)
7980
#define TSFILE_API __declspec(dllexport)
8081
#else

cpp/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ set_target_properties(TsFile_Test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${LIB_TSFI
258258
# On Windows, copy tsfile DLL next to the test exe so it can load at runtime
259259
# (and when gtest_discover_tests runs the exe). Use TARGET_FILE so the path
260260
# is correct for the current build config (e.g. Release).
261-
if (WIN32)
261+
if (WIN32 AND TSFILE_BUILD_SHARED)
262262
add_custom_command(TARGET TsFile_Test POST_BUILD
263263
COMMAND ${CMAKE_COMMAND} -E copy_if_different
264264
$<TARGET_FILE:tsfile>

0 commit comments

Comments
 (0)