From 140fd68188bf634c2bc998b628535bacb57f5516 Mon Sep 17 00:00:00 2001
From: Vladimir Davydov <vdavydov@tarantool.org>
Date: Tue, 7 Nov 2023 11:22:21 +0300
Subject: [PATCH] cmake: allow to use bundled zzip

There's EMBED_LUZIP cmake option that embeds the Lua zip module in
Tarantool binary. Since the Lua zip module depends on the zzip library,
it also links the zzip library. The option is used only by Tarantool EE
so there's no config for bundling the zzip library in the CE repository.
Now, that we bundle all static build dependencies in the main project
cmake config, let's add zzip bundling code here as well. The code of
/cmake/BuildZZIP.make is copied from /static-build/CMakeLists.txt of
the EE repository.

Follow-up #9242

NO_DOC=build
NO_TEST=build
NO_CHANGELOG=build
---
 CMakeLists.txt        | 12 ++++++++--
 cmake/BuildZZIP.cmake | 54 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 2 deletions(-)
 create mode 100644 cmake/BuildZZIP.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5c86eb7c81..c5f1f63326 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -433,9 +433,17 @@ endif()
 #
 # ZZIP
 #
+
+option(ENABLE_BUNDLED_ZZIP "Enable building of the bundled zzip"
+    ${BUILD_STATIC_WITH_BUNDLED_LIBS})
 if(EMBED_LUAZIP)
-    set(ZZIP_FIND_REQUIRED ON)
-    find_package(ZZIP)
+    if(ENABLE_BUNDLED_ZZIP)
+        include(BuildZZIP)
+        add_dependencies(build_bundled_libs bundled-zzip)
+    else()
+        set(ZZIP_FIND_REQUIRED ON)
+        find_package(ZZIP)
+    endif()
 endif()
 
 #
diff --git a/cmake/BuildZZIP.cmake b/cmake/BuildZZIP.cmake
new file mode 100644
index 0000000000..4a9005f8b6
--- /dev/null
+++ b/cmake/BuildZZIP.cmake
@@ -0,0 +1,54 @@
+set(ZZIP_VERSION v0.13.71)
+set(ZZIP_HASH 1aa094186cf2222e4cda1b91b8fb8f60)
+set(ZZIP_INSTALL_DIR ${PROJECT_BINARY_DIR}/build/zzip)
+set(ZZIP_INCLUDE_DIR ${ZZIP_INSTALL_DIR}/include)
+set(ZZIP_LIBRARY ${ZZIP_INSTALL_DIR}/lib/libzzip-0.a)
+set(ZZIP_CFLAGS "${DEPENDENCY_CFLAGS} -O2")
+
+if(APPLE)
+    set(ZZIP_CFLAGS "${ZZIP_CFLAGS} ${CMAKE_C_SYSROOT_FLAG} ${CMAKE_OSX_SYSROOT}")
+endif()
+
+set(ZZIP_CMAKE_FLAGS
+    "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
+    "-DCMAKE_C_FLAGS=${ZZIP_CFLAGS}"
+    "-DCMAKE_INSTALL_PREFIX=${ZZIP_INSTALL_DIR}"
+    "-DCMAKE_BUILD_TYPE=Release"
+    "-DBUILD_STATIC_LIBS=TRUE"
+    "-DBUILD_SHARED_LIBS=FALSE"
+    "-DZZIPDOCS=FALSE"
+    "-DZZIPBINS=FALSE"
+    "-DZZIPWRAP=FALSE"
+    "-DZZIPTEST=FALSE"
+    "-DZZIPSDL=FALSE"
+)
+
+if(ENABLED_BUNDLED_ZLIB)
+    list(APPEND ZZIP_CMAKE_FLAGS
+        "-DCMAKE_PREFIX_PATH=${ZLIB_INSTALL_DIR}"
+        "-DCMAKE_FIND_USE_CMAKE_SYSTEM_PATH=FALSE"
+    )
+endif()
+
+ExternalProject_Add(bundled-zzip-project
+    PREFIX ${ZZIP_INSTALL_DIR}
+    URL https://github.com/gdraheim/zziplib/archive/${ZZIP_VERSION}.tar.gz
+    URL_MD5 ${ZZIP_HASH}
+    CMAKE_ARGS ${ZZIP_CMAKE_FLAGS}
+    BUILD_BYPRODUCTS ${ZZIP_LIBRARY}
+)
+
+if(ENABLE_BUNDLED_ZLIB)
+    add_dependencies(bundled-zzip-project bundled-zlib)
+endif()
+
+add_library(bundled-zzip STATIC IMPORTED GLOBAL)
+set_target_properties(bundled-zzip PROPERTIES IMPORTED_LOCATION
+    ${ZZIP_LIBRARY})
+add_dependencies(bundled-zzip bundled-zzip-project)
+
+set(ZZIP_FOUND TRUE)
+set(ZZIP_LIBRARIES ${ZZIP_LIBRARY})
+set(ZZIP_INCLUDE_DIRS ${ZZIP_INCLUDE_DIR})
+
+message(STATUS "Using bundled zzip")
-- 
GitLab