From 3dbf19b61bbe897041f76f544b19148640229422 Mon Sep 17 00:00:00 2001
From: Sergey Bronnikov <sergeyb@tarantool.org>
Date: Thu, 7 Dec 2023 14:37:15 +0300
Subject: [PATCH] cmake: propagate debug mode to third party components

The patch propagates debug mode to building of third party components:
c-ares, libcurl, libeio, nghttp2, zstd. Other components enables debug
mode automatically once it is enabled in Tarantool build.

Curl has two similar options that enables debug mode, however they are
different: `ENABLE_CURLDEBUG` enable memory debugging and `ENABLE_DEBUG`
restricts code which is only compiled for debug enabled builds [1].

1. https://everything.curl.dev/internals/memory-debugging

NO_CHANGELOG=build
NO_DOC=build
NO_TEST=build
---
 CMakeLists.txt           | 4 ++++
 cmake/BuildAres.cmake    | 1 +
 cmake/BuildLibCURL.cmake | 7 +++++--
 cmake/BuildLibEIO.cmake  | 4 ++++
 cmake/BuildLibEV.cmake   | 4 ++++
 cmake/BuildZSTD.cmake    | 5 +++++
 6 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 099bf3c59c..f67bdb6aad 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -382,6 +382,10 @@ find_package_message(MODULE_LUAPATH "Lua package.path: ${MODULE_LUAPATH}"
 find_package_message(MODULE_LIBPATH "Lua package.cpath: ${MODULE_LIBPATH}"
     "${MODULE_LIBPATH}")
 
+set(TARANTOOL_DEBUG OFF)
+if (CMAKE_BUILD_TYPE STREQUAL "Debug")
+    set(TARANTOOL_DEBUG ON)
+endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
 
 ##
 ## Third-Party libraries
diff --git a/cmake/BuildAres.cmake b/cmake/BuildAres.cmake
index b86d793d14..603b1ea987 100644
--- a/cmake/BuildAres.cmake
+++ b/cmake/BuildAres.cmake
@@ -11,6 +11,7 @@ macro(ares_build)
     endif()
 
     set(ARES_CMAKE_FLAGS "-DCARES_STATIC=ON")
+    list(APPEND ARES_CMAKE_FLAGS "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
     list(APPEND ARES_CMAKE_FLAGS "-DCARES_SHARED=OFF")
     list(APPEND ARES_CMAKE_FLAGS "-DCARES_BUILD_TOOLS=OFF")
     # We build both static and shared versions of curl, so ares
diff --git a/cmake/BuildLibCURL.cmake b/cmake/BuildLibCURL.cmake
index 4104884031..6a29674246 100644
--- a/cmake/BuildLibCURL.cmake
+++ b/cmake/BuildLibCURL.cmake
@@ -185,11 +185,14 @@ macro(curl_build)
     # catch the symbols may require extra work.
     list(APPEND LIBCURL_CMAKE_FLAGS "-DCURL_ZSTD=OFF")
     list(APPEND LIBCURL_CMAKE_FLAGS "-DLIBCURL_OUTPUT_NAME=libcurl")
-    list(APPEND LIBCURL_CMAKE_FLAGS "-DENABLE_CURLDEBUG=OFF")
-    list(APPEND LIBCURL_CMAKE_FLAGS "-DENABLE_DEBUG=OFF")
+    list(APPEND LIBCURL_CMAKE_FLAGS "-DENABLE_CURLDEBUG=${TARANTOOL_DEBUG}")
+    list(APPEND LIBCURL_CMAKE_FLAGS "-DENABLE_DEBUG=${TARANTOOL_DEBUG}")
     list(APPEND LIBCURL_CMAKE_FLAGS "-DUSE_MSH3=OFF")
     list(APPEND LIBCURL_CMAKE_FLAGS "-DENABLE_WEBSOCKETS=OFF")
     list(APPEND LIBCURL_CMAKE_FLAGS "-DCMAKE_UNITY_BUILD=OFF")
+    # Note that CMake build does not allow build curl and libcurl debug
+    # enabled, see https://github.com/curl/curl/blob/master/docs/INSTALL.cmake
+    list(APPEND LIBCURL_CMAKE_FLAGS "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
 
     # We need PIC at least to enable build for Fedora on
     # ARM64 CPU. Without it configuration with Fedora
diff --git a/cmake/BuildLibEIO.cmake b/cmake/BuildLibEIO.cmake
index a542a3deaf..b493b23aa0 100644
--- a/cmake/BuildLibEIO.cmake
+++ b/cmake/BuildLibEIO.cmake
@@ -11,6 +11,10 @@ macro(libeio_build)
         set(eio_compile_flags
             "${eio_compile_flags} -DHAVE_SYS_PRCTL_H -DHAVE_PRCTL_SET_NAME")
     endif ()
+    if (TARANTOOL_DEBUG)
+        set(eio_compile_flags
+            "${eio_compile_flags} ${CMAKE_C_FLAGS_DEBUG}")
+    endif (TARANTOOL_DEBUG)
 
     set(eio_src
         ${PROJECT_SOURCE_DIR}/third_party/tarantool_eio.c
diff --git a/cmake/BuildLibEV.cmake b/cmake/BuildLibEV.cmake
index a8fef0a8d0..39738713f4 100644
--- a/cmake/BuildLibEV.cmake
+++ b/cmake/BuildLibEV.cmake
@@ -25,6 +25,10 @@ macro(libev_build)
         set(ev_compile_flags "${ev_compile_flags} -DEV_USE_KQUEUE")
     endif()
 
+    if (TARANTOOL_DEBUG)
+        set(ev_compile_flags "${ev_compile_flags} ${CMAKE_C_FLAGS_DEBUG}")
+    endif(TARANTOOL_DEBUG)
+
     list(APPEND ev_link_libraries "m")
     if (TARGET_OS_DEBIAN_FREEBSD)
         # libev depends on librt under kFreeBSD
diff --git a/cmake/BuildZSTD.cmake b/cmake/BuildZSTD.cmake
index 995a5245de..0dceb759c4 100644
--- a/cmake/BuildZSTD.cmake
+++ b/cmake/BuildZSTD.cmake
@@ -30,6 +30,11 @@ macro(zstd_build)
     if (CC_HAS_WNO_IMPLICIT_FALLTHROUGH)
         set(zstd_cflags "${zstd_cflags} -Wno-implicit-fallthrough")
     endif()
+    if (TARANTOOL_DEBUG)
+        # See lib/common/debug.h,
+        # https://github.com/facebook/zstd/blob/7567769a7e8e8236a2015769a5083d0f090a654b/lib/common/debug.h#L21-L29
+        set(zstd_cflags "${zstd_cflags} -DDEBUG_LEVEL=2")
+    endif()
     set_source_files_properties(${zstd_src}
         PROPERTIES COMPILE_FLAGS ${zstd_cflags})
 
-- 
GitLab