Skip to content

Commit 5dc3ee5

Browse files
authored
Separate header file dependencies for test and src. (#364)
* Separate header file dependencies for test and src. * fix cmake. * fix asan in mac. * fix some issue. * add ci workflow. * fix asan name. * fix compile. * fix compile on win. * fix some issue. * delete file. * fix comments. * add example to cmakelists.
1 parent c852f8d commit 5dc3ee5

39 files changed

+429
-553
lines changed

.github/workflows/unit-test-cpp.yml

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# This workflow will build a Java project with Maven
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3+
4+
name: Unit-Test-Cpp
5+
6+
on:
7+
push:
8+
branches:
9+
- develop
10+
- iotdb
11+
- rc/*
12+
paths-ignore:
13+
- 'docs/**'
14+
- 'java/**'
15+
pull_request:
16+
branches:
17+
- develop
18+
- dev/*
19+
- iotdb
20+
- rc/*
21+
paths-ignore:
22+
- 'docs/**'
23+
- 'java/**'
24+
# Enable manually starting builds, and allow forcing updating of SNAPSHOT dependencies.
25+
workflow_dispatch:
26+
inputs:
27+
forceUpdates:
28+
description: "Forces a snapshot update"
29+
required: false
30+
default: 'false'
31+
32+
concurrency:
33+
group: ${{ github.workflow }}-${{ github.ref }}
34+
cancel-in-progress: true
35+
36+
env:
37+
MAVEN_OPTS: -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.http.retryHandler.class=standard -Dmaven.wagon.http.retryHandler.count=3
38+
DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
39+
40+
jobs:
41+
unit-test:
42+
strategy:
43+
fail-fast: false
44+
max-parallel: 15
45+
matrix:
46+
include:
47+
# Linux all
48+
- os: ubuntu-latest
49+
build_type: Debug
50+
enable_asan: Asan
51+
- os: ubuntu-latest
52+
build_type: Debug
53+
enable_asan: NoAsan
54+
- os: ubuntu-latest
55+
build_type: Release
56+
enable_asan: Asan
57+
- os: ubuntu-latest
58+
build_type: Release
59+
enable_asan: NoAsan
60+
61+
# macOS exclude Release+ASan
62+
- os: macos-latest
63+
build_type: Debug
64+
enable_asan: Asan
65+
- os: macos-latest
66+
build_type: Debug
67+
enable_asan: NoAsan
68+
- os: macos-latest
69+
build_type: Release
70+
enable_asan: NoAsan
71+
72+
# Windows just Release
73+
- os: windows-latest
74+
build_type: Release
75+
enable_asan: NoAsan
76+
runs-on: ${{ matrix.os }}
77+
78+
steps:
79+
80+
- name: Checkout repository
81+
uses: actions/checkout@v4
82+
83+
# Setup caching of the artifacts in the .m2 directory, so they don't have to
84+
# all be downloaded again for every build.
85+
- name: Cache Maven packages
86+
uses: actions/cache@v4
87+
with:
88+
path: ~/.m2
89+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
90+
restore-keys: ${{ runner.os }}-m2-
91+
92+
# On Windows systems the 'mvnw' script needs an additional ".cmd" appended.
93+
- name: Calculate platform suffix
94+
id: platform_suffix
95+
uses: actions/[email protected]
96+
env:
97+
OS: ${{ matrix.os }}
98+
with:
99+
script: |
100+
const { OS } = process.env
101+
if (OS.includes("windows")) {
102+
core.setOutput('platform_suffix', `.cmd`)
103+
} else {
104+
core.setOutput('platform_suffix', ``)
105+
}
106+
107+
# Run the actual maven build including all tests.
108+
- name: Build and test with Maven
109+
shell: bash
110+
run: |
111+
if [ "${{ matrix.enable_asan }}" = "Asan" ]; then
112+
ASAN_VALUE="ON"
113+
else
114+
ASAN_VALUE="OFF"
115+
fi
116+
./mvnw${{ steps.platform_suffix.outputs.platform_suffix }} -P with-cpp \
117+
-Denable.asan=$ASAN_VALUE -Dbuild.type=${{ matrix.build_type }} clean verify
118+

.github/workflows/unit-test-cpp-py.yml renamed to .github/workflows/unit-test-python.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will build a Java project with Maven
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
33

4-
name: Unit-Test-Cpp-Py
4+
name: Unit-Test-Py
55

66
on:
77
push:
@@ -15,6 +15,7 @@ on:
1515
pull_request:
1616
branches:
1717
- develop
18+
- dev/*
1819
- iotdb
1920
- rc/*
2021
paths-ignore:
@@ -78,7 +79,7 @@ jobs:
7879
- name: Build and test with Maven
7980
shell: bash
8081
run: |
81-
./mvnw${{ steps.platform_suffix.outputs.platform_suffix }} -P with-cpp,with-python clean verify
82+
./mvnw${{ steps.platform_suffix.outputs.platform_suffix }} -P with-python -Denable.asan=OFF -Dbuild.type=Release clean verify
8283
8384
- name: Upload whl Artifact
8485
uses: actions/upload-artifact@v4

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ python/data
3535
python/venv/*
3636
python/tests/__pycache__/*
3737
python/tests/*.tsfile
38+
3839
cpp/cmake-build-debug-mingw/
40+
cpp/third_party/googletest-release-1.12.1.zip
3941

4042
.vscode/
4143

cpp/CMakeLists.txt

+43-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[[
1+
#[[
22
Licensed to the Apache Software Foundation (ASF) under one
33
or more contributor license agreements. See the NOTICE file
44
distributed with this work for additional information
@@ -23,10 +23,10 @@ cmake_policy(SET CMP0079 NEW)
2323
set(TsFile_CPP_VERSION 2.1.0.dev)
2424
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -Wall")
2525
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
26-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=maybe-uninitialized")
26+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=maybe-uninitialized -D__STDC_FORMAT_MACROS")
2727
endif()
28-
message("cmake using: USE_CPP11=${USE_CPP11}")
2928

29+
message("cmake using: USE_CPP11=${USE_CPP11}")
3030
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
3131

3232
if(DEFINED ENV{CXX})
@@ -50,42 +50,59 @@ if (${COV_ENABLED})
5050
message("add_definitions -DCOV_ENABLED=1")
5151
endif()
5252

53-
message("build type: ${BUILD_TYPE}")
53+
54+
if (NOT CMAKE_BUILD_TYPE)
55+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
56+
endif ()
57+
58+
message("CMAKE BUILD TYPE " ${CMAKE_BUILD_TYPE})
5459
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
55-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g")
56-
set(CMAKE_CXX_FLAGS_DEBUG" ${CMAKE_CXX_FLAGS} -O0 -g")
60+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")
5761
elseif (CMAKE_BUILD_TYPE STREQUAL "Release")
58-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g") # disable -g
59-
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS}")
62+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
6063
elseif (CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
61-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g")
62-
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS}")
64+
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -O2 -g")
6365
elseif (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
64-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections -Os")
65-
set(LDFLAGS "-Wl,--gc-sections")
66-
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS}")
67-
endif()
66+
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -ffunction-sections -fdata-sections -Os")
67+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
68+
endif ()
6869
message("CMAKE DEBUG: CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}")
6970

71+
# disable asan by default.
72+
option(ENABLE_ASAN "Enable Address Sanitizer" OFF)
73+
74+
if (NOT WIN32)
75+
if (ENABLE_ASAN)
76+
message("Address Sanitizer is enabled.")
77+
78+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
79+
80+
if (NOT APPLE)
81+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libasan")
82+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -static-libasan")
83+
else ()
84+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
85+
endif ()
86+
else ()
87+
message("Address Sanitizer is disabled.")
88+
endif ()
89+
endif ()
90+
91+
92+
# All libs will be stored here, including libtsfile, compress-encoding lib.
7093
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
71-
set(PROJECT_INCLUDE_DIR ${PROJECT_INCLUDE_DIR}
72-
${PROJECT_SOURCE_DIR}/src
73-
${PROJECT_SOURCE_DIR}/third_party/lz4
74-
${PROJECT_SOURCE_DIR}/third_party/lzokay
75-
${PROJECT_SOURCE_DIR}/third_party/zlib-1.2.13
76-
${PROJECT_BINARY_DIR}/third_party/zlib-1.2.13
77-
)
7894

79-
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
80-
include_directories(${PROJECT_INCLUDE_DIR})
95+
# TsFile code will be stored here.
96+
set(PROJECT_SRC_DIR ${PROJECT_SOURCE_DIR}/src)
8197

82-
include_directories(${PROJECT_SOURCE_DIR}/third_party/antlr4-cpp-runtime-4/runtime/src)
98+
# All include files will be installed here.
99+
set(LIBRARY_INCLUDE_DIR ${PROJECT_BINARY_DIR}/include)
100+
set(THIRD_PARTY_INCLUDE ${PROJECT_BINARY_DIR}/third_party)
83101

84102
add_subdirectory(third_party)
85-
86103
add_subdirectory(src)
87-
88104
add_subdirectory(test)
105+
add_subdirectory(examples)
89106
if(TESTS_ENABLED)
90107
add_dependencies(TsFile_Test tsfile)
91108
endif()

cpp/cmake/CopyToDir.cmake

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#[[
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
]]
19+
20+
# CopyToDir.cmake
21+
22+
# This function is used to copy files to a directory and it will handle relative paths automatically.
23+
function(copy_to_dir)
24+
set(INCLUDE_EXPORT_DR ${LIBRARY_INCLUDE_DIR} CACHE INTERNAL "Include export directory")
25+
foreach(file ${ARGN})
26+
get_filename_component(file_name ${file} NAME)
27+
get_filename_component(file_path ${file} PATH)
28+
string(REPLACE "${CMAKE_SOURCE_DIR}/src" "" relative_path "${file_path}")
29+
add_custom_target(
30+
copy_${file_name} ALL
31+
COMMAND ${CMAKE_COMMAND} -E make_directory ${INCLUDE_EXPORT_DR}/${relative_path}
32+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${file} ${INCLUDE_EXPORT_DR}/${relative_path}/${file_name}
33+
COMMENT "Copying ${file_name} to ${INCLUDE_EXPORT_DR}/${relative_path}"
34+
)
35+
endforeach()
36+
endfunction()
37+
38+

cpp/examples/CMakeLists.txt

+2-8
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,9 @@ include_directories(${SDK_INCLUDE_DIR})
4040
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g")
4141
set(CMAKE_CXX_FLAGS_DEBUG" ${CMAKE_CXX_FLAGS} -O0 -g")
4242

43-
# Find libtsfile in SDK_LIB_RIR
44-
if (BUILD_TYPE STREQUAL "Release")
45-
find_library(my_tsfile_lib NAMES tsfile PATHS ${SDK_LIB_DIR_RELEASE} NO_DEFAULT_PATH REQUIRED)
46-
elseif(BUILD_TYPE STREQUAL "Debug")
47-
find_library(my_tsfile_lib NAMES tsfile PATHS ${SDK_LIB_DIR_DEBUG} NO_DEFAULT_PATH REQUIRED)
48-
endif ()
49-
5043
add_subdirectory(cpp_examples)
5144
add_subdirectory(c_examples)
5245

5346
add_executable(examples examples.cc)
54-
target_link_libraries(examples ${my_tsfile_lib} cpp_examples_obj c_examples_obj)
47+
target_link_libraries(examples cpp_examples_obj c_examples_obj)
48+
target_link_libraries(examples tsfile)

cpp/examples/c_examples/c_examples.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
#include "cwrapper/errno_define.h"
20+
#include "cwrapper/errno_define_c.h"
2121
#include "cwrapper/tsfile_cwrapper.h"
2222

2323
#ifdef __cplusplus

cpp/examples/c_examples/demo_write.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
#include <malloc.h>
20+
#include <stdlib.h>
2121
#include <stdint.h>
2222
#include <stdio.h>
2323
#include <string.h>

cpp/pom.xml

+6-8
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
<!-- Tell Sonar where to find the sources -->
3232
<sonar.sources>common,examples,tsfile</sonar.sources>
3333
<sonar.cfamily.build-wrapper-output>${project.build.directory}/build-wrapper-output</sonar.cfamily.build-wrapper-output>
34-
<cmake.addition.option>-DCOV_ENABLED=OFF</cmake.addition.option>
34+
<coverage.enabled>OFF</coverage.enabled>
35+
<build.type>Release</build.type>
36+
<enable.asan>OFF</enable.asan>
3537
</properties>
3638
<build>
3739
<sourceDirectory>${project.basedir}</sourceDirectory>
@@ -69,7 +71,9 @@
6971
<configuration>
7072
<options>
7173
<option>-DBUILD_PHASE=test-compile</option>
72-
<option>${cmake.addition.option}</option>
74+
<option>-DCOV_ENABLED=${coverage.enabled}</option>
75+
<option>-DCMAKE_BUILD_TYPE=${build.type}</option>
76+
<option>-DENABLE_ASAN=${enable.asan}</option>
7377
</options>
7478
<sourcePath/>
7579
<targetPath/>
@@ -180,12 +184,6 @@
180184
</plugins>
181185
</build>
182186
</profile>
183-
<profile>
184-
<id>with-code-coverage</id>
185-
<properties>
186-
<cmake.addition.option>-DCOV_ENABLED=ON</cmake.addition.option>
187-
</properties>
188-
</profile>
189187
<!-- When running on jenkins, download the sonar build-wrapper, so we can do a code analysis -->
190188
<profile>
191189
<id>jenkins-build</id>

0 commit comments

Comments
 (0)