From ddc7f2c19fd104e99bc49210b1f6ac3f5cec1548 Mon Sep 17 00:00:00 2001
From: "Azamat H. Hackimov" <azamat.hackimov@gmail.com>
Date: Fri, 9 Oct 2020 02:18:36 +0300
Subject: [PATCH] Make LibArchive as shared library dependency

---
 CMakeLists.txt             | 11 +++++++++++
 src/app/CMakeLists.txt     |  2 +-
 third_party/CMakeLists.txt | 38 ++++++++++++++++++++------------------
 3 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1e7692679..26e09c418 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -51,6 +51,7 @@ option(USE_SHARED_CURL    "Use your installed copy of curl" off)
 option(USE_SHARED_GIFLIB  "Use your installed copy of giflib" off)
 option(USE_SHARED_JPEGLIB "Use your installed copy of jpeglib" off)
 option(USE_SHARED_ZLIB    "Use your installed copy of zlib" off)
+option(USE_SHARED_LIBARCHIVE "Use your installed copy of libarchive" off)
 option(USE_SHARED_LIBPNG  "Use your installed copy of libpng" off)
 option(USE_SHARED_TINYXML "Use your installed copy of tinyxml" off)
 option(USE_SHARED_PIXMAN  "Use your installed copy of pixman" off)
@@ -210,6 +211,16 @@ else()
 endif()
 include_directories(${ZLIB_INCLUDE_DIRS})
 
+# libarchive
+if(USE_SHARED_LIBARCHIVE)
+  find_package(LibArchive REQUIRED)
+else()
+  set(LibArchive_FOUND)
+  set(LibArchive_LIBRARIES archive_static)
+  set(LibArchive_INCLUDE_DIRS $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/libarchive/libarchive>)
+endif()
+include_directories(${LibArchive_INCLUDE_DIRS})
+
 # libpng
 if(USE_SHARED_LIBPNG)
   find_package(PNG REQUIRED)
diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt
index c42768ff6..e0495f98f 100644
--- a/src/app/CMakeLists.txt
+++ b/src/app/CMakeLists.txt
@@ -670,8 +670,8 @@ target_link_libraries(app-lib
   ${ZLIB_LIBRARIES}
   ${FREETYPE_LIBRARIES}
   ${HARFBUZZ_LIBRARIES}
+  ${LibArchive_LIBRARIES}
   json11
-  archive_static
   fmt
   tinyexpr)
 
diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt
index 2c0a08248..12017190f 100644
--- a/third_party/CMakeLists.txt
+++ b/third_party/CMakeLists.txt
@@ -126,24 +126,26 @@ endif()
 add_subdirectory(json11)
 
 # libarchive
-set(ENABLE_WERROR OFF CACHE BOOL "Treat warnings as errors - default is ON for Debug, OFF otherwise.")
-set(ENABLE_TEST OFF CACHE BOOL "Enable unit and regression tests")
-set(ENABLE_COVERAGE OFF CACHE BOOL "Enable code coverage (GCC only, automatically sets ENABLE_TEST to ON)")
-set(ENABLE_LZ4 OFF CACHE BOOL "Enable the use of the system LZ4 library if found")
-set(ENABLE_LZO OFF CACHE BOOL "Enable the use of the system LZO library if found")
-set(ENABLE_LZMA OFF CACHE BOOL "Enable the use of the system LZMA library if found")
-set(ENABLE_ZSTD OFF CACHE BOOL "Enable the use of the system zstd library if found")
-set(ENABLE_CNG OFF CACHE BOOL "Enable the use of CNG(Crypto Next Generation)")
-set(ENABLE_BZip2 OFF CACHE BOOL "Enable the use of the system BZip2 library if found")
-set(ENABLE_EXPAT OFF CACHE BOOL "Enable the use of the system EXPAT library if found")
-set(ENABLE_LIBXML2 OFF CACHE BOOL "Enable the use of the system libxml2 library if found")
-set(ENABLE_CAT OFF CACHE BOOL "Enable cat building")
-set(ENABLE_TAR OFF CACHE BOOL "Enable tar building")
-set(ENABLE_CPIO OFF CACHE BOOL "Enable cpio building")
-set(ENABLE_LIBB2 OFF CACHE BOOL "Enable the use of the system LIBB2 library if found")
-add_subdirectory(libarchive)
-target_include_directories(archive_static INTERFACE
-  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/libarchive/libarchive>)
+if(NOT USE_SHARED_LIBARCHIVE)
+  set(ENABLE_WERROR OFF CACHE BOOL "Treat warnings as errors - default is ON for Debug, OFF otherwise.")
+  set(ENABLE_TEST OFF CACHE BOOL "Enable unit and regression tests")
+  set(ENABLE_COVERAGE OFF CACHE BOOL "Enable code coverage (GCC only, automatically sets ENABLE_TEST to ON)")
+  set(ENABLE_LZ4 OFF CACHE BOOL "Enable the use of the system LZ4 library if found")
+  set(ENABLE_LZO OFF CACHE BOOL "Enable the use of the system LZO library if found")
+  set(ENABLE_LZMA OFF CACHE BOOL "Enable the use of the system LZMA library if found")
+  set(ENABLE_ZSTD OFF CACHE BOOL "Enable the use of the system zstd library if found")
+  set(ENABLE_CNG OFF CACHE BOOL "Enable the use of CNG(Crypto Next Generation)")
+  set(ENABLE_BZip2 OFF CACHE BOOL "Enable the use of the system BZip2 library if found")
+  set(ENABLE_EXPAT OFF CACHE BOOL "Enable the use of the system EXPAT library if found")
+  set(ENABLE_LIBXML2 OFF CACHE BOOL "Enable the use of the system libxml2 library if found")
+  set(ENABLE_CAT OFF CACHE BOOL "Enable cat building")
+  set(ENABLE_TAR OFF CACHE BOOL "Enable tar building")
+  set(ENABLE_CPIO OFF CACHE BOOL "Enable cpio building")
+  set(ENABLE_LIBB2 OFF CACHE BOOL "Enable the use of the system LIBB2 library if found")
+  add_subdirectory(libarchive)
+  target_include_directories(archive_static INTERFACE
+    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/libarchive/libarchive>)
+endif()
 
 # benchmark
 if(ENABLE_BENCHMARKS)
-- 
2.35.1

