From 35473d5d8dbf4e1337c7e6af26a3e05992b98247 Mon Sep 17 00:00:00 2001
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Date: Sat, 13 Jun 2020 20:13:29 +0200
Subject: [PATCH] sql: don't build sql as a separate library

SQL heavily depends on box, and box on SQL. So they can't be
separate libraries. The build started failing with undefined box
symbols in SQL, when code of the latter has slightly changed in
one of the recent commits.

The build failed only with UB sanitizer enabled, but
'VERBOSE=1 make' showed that both with UB and without UB the build
command was the same (not counting -fsanitize flags). So the
sanitizer has nothing to do with it.

The patch makes SQL sources being built as a part of box library.

Closes #5067
---
 src/CMakeLists.txt         |   2 +-
 src/box/CMakeLists.txt     | 101 ++++++++++++++++++++++++++++++++--
 src/box/sql/CMakeLists.txt | 109 -------------------------------------
 3 files changed, 98 insertions(+), 114 deletions(-)
 delete mode 100644 src/box/sql/CMakeLists.txt

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7099e9bef7..68d69eded3 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -266,7 +266,7 @@ add_executable(
     ${LIBUTIL_FREEBSD_SRC}/flopen.c
     ${LIBUTIL_FREEBSD_SRC}/pidfile.c)
 
-add_dependencies(tarantool build_bundled_libs sql)
+add_dependencies(tarantool build_bundled_libs)
 target_link_libraries(tarantool box ${common_libraries})
 
 if (TARGET_OS_FREEBSD AND NOT TARGET_OS_DEBIAN_FREEBSD)
diff --git a/src/box/CMakeLists.txt b/src/box/CMakeLists.txt
index 230e7427d9..63f98f6c89 100644
--- a/src/box/CMakeLists.txt
+++ b/src/box/CMakeLists.txt
@@ -1,12 +1,11 @@
 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/src/box/lua)
+file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/src/box/sql)
 
 # Sometimes 'spying' code is not acceptable even if it would be
 # disabled by default. That option allows to remove the feedback
 # daemon from the build completely.
 option(ENABLE_FEEDBACK_DAEMON "Feedback daemon which reports debug data to the Tarantool team" ON)
 
-add_subdirectory(sql)
-
 set(lua_sources)
 lua_source(lua_sources lua/load_cfg.lua)
 lua_source(lua_sources lua/schema.lua)
@@ -25,6 +24,52 @@ lua_source(lua_sources lua/merger.lua)
 set(bin_sources)
 bin_source(bin_sources bootstrap.snap bootstrap.h)
 
+set(sql_sources
+    sql/opcodes.c
+    sql/parse.c
+    sql/alter.c
+    sql/analyze.c
+    sql/cursor.c
+    sql/build.c
+    sql/callback.c
+    sql/date.c
+    sql/delete.c
+    sql/expr.c
+    sql/fk_constraint.c
+    sql/func.c
+    sql/global.c
+    sql/hash.c
+    sql/insert.c
+    sql/legacy.c
+    sql/main.c
+    sql/malloc.c
+    sql/os.c
+    sql/os_unix.c
+    sql/parse_def.c
+    sql/pragma.c
+    sql/prepare.c
+    sql/printf.c
+    sql/random.c
+    sql/resolve.c
+    sql/select.c
+    sql/tokenize.c
+    sql/treeview.c
+    sql/trigger.c
+    sql/utf.c
+    sql/update.c
+    sql/util.c
+    sql/vdbe.c
+    sql/vdbeapi.c
+    sql/vdbeaux.c
+    sql/vdbemem.c
+    sql/vdbesort.c
+    sql/vdbetrace.c
+    sql/walker.c
+    sql/where.c
+    sql/wherecode.c
+    sql/whereexpr.c
+)
+
 add_custom_target(box_generate_lua_sources
     WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src/box
     DEPENDS ${lua_sources})
@@ -143,6 +188,7 @@ add_library(box STATIC
     wal.c
     call.c
     merger.c
+    ${sql_sources}
     ${lua_sources}
     lua/init.c
     lua/call.c
@@ -167,6 +213,53 @@ add_library(box STATIC
     lua/merger.c
     ${bin_sources})
 
+if(CMAKE_BUILD_TYPE STREQUAL "Debug")
+  add_definitions(-DSQL_DEBUG=1)
+endif()
+add_definitions(-DSQL_OMIT_AUTOMATIC_INDEX=1 -DSQL_TEST=1)
+
+set(EXT_SRC_DIR ${CMAKE_SOURCE_DIR}/extra)
+set(EXT_BIN_DIR ${CMAKE_BINARY_DIR}/extra)
+set(SQL_SRC_DIR ${CMAKE_SOURCE_DIR}/src/box/sql)
+set(SQL_BIN_DIR ${CMAKE_BINARY_DIR}/src/box/sql)
+
+include_directories(${SQL_SRC_DIR})
+include_directories(${SQL_BIN_DIR})
+
+add_custom_target(generate_sql_files DEPENDS
+    sql/parse.h
+    sql/keywordhash.h
+    sql/parse.y
+    sql/parse.c
+    sql/opcodes.c)
+
+add_custom_command(OUTPUT ${SQL_BIN_DIR}/keywordhash.h
+    COMMAND ${EXT_BIN_DIR}/mkkeywordhash > keywordhash.h.tmp
+    COMMAND ${CMAKE_COMMAND} -E copy_if_different keywordhash.h.tmp keywordhash.h
+    COMMAND ${CMAKE_COMMAND} -E remove keywordhash.h.tmp
+    WORKING_DIRECTORY "${SQL_BIN_DIR}"
+    DEPENDS mkkeywordhash)
+
+add_custom_command(OUTPUT ${SQL_BIN_DIR}/parse.h ${SQL_BIN_DIR}/parse.c
+    COMMAND ${EXT_BIN_DIR}/lemon -T${EXT_SRC_DIR}/lempar.c -o${SQL_BIN_DIR}/parse.c ${SQL_SRC_DIR}/parse.y
+    COMMAND ${CMAKE_COMMAND} -E copy parse.h parse.h.tmp
+    COMMAND ${EXT_SRC_DIR}/addopcodes.sh parse.h.tmp > parse.h
+    COMMAND ${CMAKE_COMMAND} -E remove parse.h.tmp parse.out
+    WORKING_DIRECTORY "${SQL_BIN_DIR}"
+    DEPENDS lemon ${SQL_SRC_DIR}/parse.y)
+
+add_custom_command(OUTPUT ${SQL_BIN_DIR}/opcodes.h
+    COMMAND cat parse.h ${SQL_SRC_DIR}/vdbe.c | ${EXT_SRC_DIR}/mkopcodeh.sh > opcodes.h
+    WORKING_DIRECTORY "${SQL_BIN_DIR}"
+    DEPENDS ${SQL_SRC_DIR}/vdbe.c ${EXT_SRC_DIR}/mkopcodeh.sh ${SQL_BIN_DIR}/parse.h)
+
+add_custom_command(OUTPUT ${SQL_BIN_DIR}/opcodes.c
+    COMMAND ${EXT_SRC_DIR}/mkopcodec.sh opcodes.h > opcodes.c
+    WORKING_DIRECTORY "${SQL_BIN_DIR}"
+    DEPENDS ${SQL_SRC_DIR}/vdbe.c ${EXT_SRC_DIR}/mkopcodec.sh ${SQL_BIN_DIR}/parse.h
+        ${SQL_BIN_DIR}/opcodes.h)
+
 target_link_libraries(box box_error tuple stat xrow xlog vclock crc32 scramble
-                      sql ${common_libraries})
-add_dependencies(box build_bundled_libs)
+                      ${common_libraries})
+
+add_dependencies(box build_bundled_libs generate_sql_files)
diff --git a/src/box/sql/CMakeLists.txt b/src/box/sql/CMakeLists.txt
deleted file mode 100644
index 1f2a6640fa..0000000000
--- a/src/box/sql/CMakeLists.txt
+++ /dev/null
@@ -1,109 +0,0 @@
-if(CMAKE_BUILD_TYPE STREQUAL "Debug")
-  add_definitions(-DSQL_DEBUG=1)
-endif()
-
-set(EXT_SRC_DIR ${CMAKE_SOURCE_DIR}/extra)
-set(EXT_BIN_DIR ${CMAKE_BINARY_DIR}/extra)
-set(SQL_SRC_DIR ${CMAKE_SOURCE_DIR}/src/box/sql)
-set(SQL_BIN_DIR ${CMAKE_BINARY_DIR}/src/box/sql)
-
-include_directories(${SQL_SRC_DIR})
-include_directories(${SQL_BIN_DIR})
-
-add_definitions(-DSQL_OMIT_AUTOMATIC_INDEX)
-
-set(TEST_DEFINITIONS
-    SQL_NO_SYNC=1
-    SQL_TEST=1
-    SQL_PRIVATE=
-    SQL_CORE=1
-)
-
-add_library(sql STATIC
-    # Generated files
-    opcodes.c
-    parse.c
-    alter.c
-    analyze.c
-    cursor.c
-    build.c
-    callback.c
-    date.c
-    delete.c
-    expr.c
-    fk_constraint.c
-    func.c
-    global.c
-    hash.c
-    insert.c
-    legacy.c
-    main.c
-    malloc.c
-    os.c
-    os_unix.c
-    parse_def.c
-    pragma.c
-    prepare.c
-    printf.c
-    random.c
-    resolve.c
-    select.c
-    tokenize.c
-    treeview.c
-    trigger.c
-    utf.c
-    update.c
-    util.c
-    vdbe.c
-    vdbeapi.c
-    vdbeaux.c
-    vdbemem.c
-    vdbesort.c
-    vdbetrace.c
-    walker.c
-    where.c
-    wherecode.c
-    whereexpr.c
-)
-set_target_properties(sql PROPERTIES COMPILE_DEFINITIONS
-    "${TEST_DEFINITIONS}")
-target_link_libraries(sql ${ICU_LIBRARIES})
-
-add_custom_target(generate_sql_files DEPENDS
-    parse.h
-    keywordhash.h
-    parse.y
-    parse.c
-    opcodes.c)
-
-add_custom_command(OUTPUT ${SQL_BIN_DIR}/keywordhash.h
-    COMMAND ${EXT_BIN_DIR}/mkkeywordhash > keywordhash.h.tmp
-    COMMAND ${CMAKE_COMMAND} -E copy_if_different keywordhash.h.tmp keywordhash.h
-    COMMAND ${CMAKE_COMMAND} -E remove keywordhash.h.tmp
-    WORKING_DIRECTORY "${SQL_BIN_DIR}"
-    DEPENDS mkkeywordhash)
-
-add_custom_command(OUTPUT ${SQL_BIN_DIR}/parse.h ${SQL_BIN_DIR}/parse.c
-    COMMAND ${EXT_BIN_DIR}/lemon -T${EXT_SRC_DIR}/lempar.c ${SQL_SRC_DIR}/parse.y
-    COMMAND ${CMAKE_COMMAND} -E copy parse.h parse.h.tmp
-    COMMAND ${EXT_SRC_DIR}/addopcodes.sh parse.h.tmp > parse.h
-    COMMAND ${CMAKE_COMMAND} -E remove parse.h.tmp parse.out
-    WORKING_DIRECTORY "${SQL_BIN_DIR}"
-    DEPENDS lemon ${SQL_SRC_DIR}/parse.y)
-
-add_custom_command(OUTPUT ${SQL_BIN_DIR}/opcodes.h
-    COMMAND cat parse.h ${SQL_SRC_DIR}/vdbe.c | ${EXT_SRC_DIR}/mkopcodeh.sh > opcodes.h
-    WORKING_DIRECTORY "${SQL_BIN_DIR}"
-    DEPENDS ${SQL_SRC_DIR}/vdbe.c ${EXT_SRC_DIR}/mkopcodeh.sh ${SQL_BIN_DIR}/parse.h)
-
-add_custom_command(OUTPUT ${SQL_BIN_DIR}/opcodes.c
-    COMMAND ${EXT_SRC_DIR}/mkopcodec.sh opcodes.h > opcodes.c
-    WORKING_DIRECTORY "${SQL_BIN_DIR}"
-    DEPENDS ${SQL_SRC_DIR}/vdbe.c ${EXT_SRC_DIR}/mkopcodec.sh ${SQL_BIN_DIR}/parse.h
-        ${SQL_BIN_DIR}/opcodes.h)
-
-add_dependencies(sql generate_sql_files)
-
-if (APPLE)
-    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined suppress -flat_namespace")
-endif(APPLE)
-- 
GitLab