Skip to content

Allow packaging the C++ client in Debug mode (-Dcmake.build.type=Debug)#17856

Open
JackieTien97 wants to merge 1 commit into
masterfrom
client-cpp-debug-build-type
Open

Allow packaging the C++ client in Debug mode (-Dcmake.build.type=Debug)#17856
JackieTien97 wants to merge 1 commit into
masterfrom
client-cpp-debug-build-type

Conversation

@JackieTien97
Copy link
Copy Markdown
Contributor

What

Make -Dcmake.build.type=Debug actually produce a debug build of the C++ client library (libiotdb_session).

Why

On non-MSVC platforms, iotdb-client/client-cpp/src/main/CMakeLists.txt hardcoded:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -O2")

and the cmake-generate step in pom.xml never passed -DCMAKE_BUILD_TYPE. Single-config generators (e.g. Unix Makefiles) ignore cmake --build --config, so on Linux/macOS the existing cmake.build.type property was effectively a no-op — the packaged library was always compiled at -O2. Even -DCMAKE_BUILD_TYPE=Debug stayed at -O2, because Debug only appends -g and never removes the hardcoded -O2.

How

  • pom.xml: pass -DCMAKE_BUILD_TYPE=${cmake.build.type} to the main-library cmake-generate execution.
  • src/main/CMakeLists.txt: stop hardcoding the optimization level. Keep -Wall -g for all build types (Release still ships symbols, as before), set CMAKE_CXX_FLAGS_RELEASE to -O2 and CMAKE_CXX_FLAGS_DEBUG to -O0, and default to Release when no type is given.

Release output is intentionally unchanged (-g -O2, no -DNDEBUG, asserts still active). MSVC is unaffected — it already selects the configuration via cmake --build --config ${cmake.build.type}.

Usage

# Release (default, unchanged)
mvn clean package -P with-cpp -pl iotdb-client/client-cpp -am -DskipTests

# Debug package
mvn clean package -P with-cpp -pl iotdb-client/client-cpp -am -DskipTests -Dcmake.build.type=Debug

Verification

Actual CXX_FLAGS produced by the configure step (macOS, Unix Makefiles) with these changes:

build type resulting flags opt level
default / Release -Wall -g -O2 ... -O2 (unchanged)
Debug -Wall -g -O0 ... -O0

A full Debug build of libiotdb_session compiles and links; the resulting dylib is ~6.7 MB vs ~3.2 MB for Release, consistent with -O0 + debug info.

🤖 Generated with Claude Code

On non-MSVC platforms src/main/CMakeLists.txt hardcoded "-Wall -g -O2" and the cmake generate step never received -DCMAKE_BUILD_TYPE. Single-config generators (Unix Makefiles) ignore "cmake --build --config", so the cmake.build.type property had no effect on Linux/macOS and the packaged library was always built at -O2.

Pass -DCMAKE_BUILD_TYPE=${cmake.build.type} to the main-library generate step and let the build type drive optimization: keep -Wall -g for every build type, set Release to -O2 (exactly the previous flags, no -DNDEBUG) and Debug to -O0, defaulting to Release when unset. Release output is unchanged; -Dcmake.build.type=Debug now yields an unoptimized, debuggable library. MSVC is unaffected.
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 6, 2026

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
# Release keeps the previous flags exactly (-O2, no -DNDEBUG); Debug is unoptimized
# (-O0) so the packaged library can be stepped through in a debugger.
SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should preserve existing per-config flags instead of replacing them completely. Once the POM passes -DCMAKE_BUILD_TYPE, CMAKE_CXX_FLAGS_RELEASE / CMAKE_CXX_FLAGS_DEBUG are actually used, so these unconditional assignments can discard flags provided by a toolchain file or by the caller, e.g. hardening, sanitizer, ABI, or architecture flags. Could we keep the caller/toolchain-provided value and only adjust the optimization level needed here (-O2 for Release, -O0 for Debug)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants