From 6d99efe6852923e3abdbdbc9ae797d24b77787a6 Mon Sep 17 00:00:00 2001
From: Magomed Kostoev <m.kostoev@tarantool.org>
Date: Thu, 9 Nov 2023 17:08:09 +0300
Subject: [PATCH] cmake: include config-specific and LTO flags into
 TARANTOOL_C_FLAGS

Prior to this patch only the flags given by the CMAKE_C_FLAGS were
shown in the tarantool -v and Lua's tarantool.build.flags. Make
it also show the flags from CMAKE_C_FLAGS_<CONFIG> and CFLAGS_LTO
variables. The first one is config-specific set of flags (different
for Debug, Release, RelWithDebInfo and MinSizeRel configs), the
former is set in the cmake/lto.cmake if LTO is enabled.

Closes #8022

NO_DOC=build
---
 .../gh-8022-tarantool-build-flags.md          |  4 ++++
 src/CMakeLists.txt                            | 20 +++++++++++++++++--
 .../gh_8022_tarantool_build_flags_test.lua    | 10 ++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 changelogs/unreleased/gh-8022-tarantool-build-flags.md
 create mode 100644 test/app-luatest/gh_8022_tarantool_build_flags_test.lua

diff --git a/changelogs/unreleased/gh-8022-tarantool-build-flags.md b/changelogs/unreleased/gh-8022-tarantool-build-flags.md
new file mode 100644
index 0000000000..cb03df4b57
--- /dev/null
+++ b/changelogs/unreleased/gh-8022-tarantool-build-flags.md
@@ -0,0 +1,4 @@
+## bugfix/core
+
+* The `tarantool -v` output and `tarantool.build.flags` string now include
+  build type-specific and LTO flags if any (gh-8022).
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index de2a2e8d69..998d244cb0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -339,8 +339,24 @@ add_subdirectory(box)
 include_directories(${EXTRA_BOX_INCLUDE_DIRS})
 
 # Save CMAKE_XXX_FLAGS from this directory for config.h (used in --version)
-set(TARANTOOL_C_FLAGS ${CMAKE_C_FLAGS} PARENT_SCOPE)
-set(TARANTOOL_CXX_FLAGS ${CMAKE_CXX_FLAGS} PARENT_SCOPE)
+string(REPLACE ";" " " TARANTOOL_CFLAGS_LTO "${CFLAGS_LTO}")
+
+set(TARANTOOL_BUILD_TYPES Debug Release RelWithDebInfo MinSizeRel)
+list(FIND TARANTOOL_BUILD_TYPES "${CMAKE_BUILD_TYPE}" TARANTOOL_BUILD_TYPE)
+
+if(${TARANTOOL_BUILD_TYPE} EQUAL -1)
+    message(FATAL_ERROR "Unknown config passed: ${CMAKE_BUILD_TYPE}")
+endif()
+
+string(TOUPPER "${CMAKE_BUILD_TYPE}" TARANTOOL_BUILD_TYPE)
+set(TARANTOOL_C_FLAGS_CONFIG "${CMAKE_C_FLAGS_${TARANTOOL_BUILD_TYPE}}")
+set(TARANTOOL_CXX_FLAGS_CONFIG "${CMAKE_CXX_FLAGS_${TARANTOOL_BUILD_TYPE}}")
+
+set(TARANTOOL_C_FLAGS "${CMAKE_C_FLAGS} ${TARANTOOL_C_FLAGS_CONFIG} ${TARANTOOL_CFLAGS_LTO}")
+set(TARANTOOL_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TARANTOOL_CXX_FLAGS_CONFIG} ${TARANTOOL_CFLAGS_LTO}")
+
+set(TARANTOOL_C_FLAGS ${TARANTOOL_C_FLAGS} PARENT_SCOPE)
+set(TARANTOOL_CXX_FLAGS ${TARANTOOL_CXX_FLAGS} PARENT_SCOPE)
 
 set(exports_file_sources
     ${PROJECT_SOURCE_DIR}/extra/exports
diff --git a/test/app-luatest/gh_8022_tarantool_build_flags_test.lua b/test/app-luatest/gh_8022_tarantool_build_flags_test.lua
new file mode 100644
index 0000000000..ad533802b0
--- /dev/null
+++ b/test/app-luatest/gh_8022_tarantool_build_flags_test.lua
@@ -0,0 +1,10 @@
+local t = require('luatest')
+
+local g = t.group('gh-8022')
+
+-- Check that `tarantool.build.flags` contains an optimization level setting.
+-- It means that config-specific flags are included into the variable.
+g.test_build_target = function()
+    local tarantool = require('tarantool')
+    t.assert_str_contains(tarantool.build.flags, '-O')
+end
-- 
GitLab