From 0bd585fa0b54a2b85cea6a6768c7e1d953b24334 Mon Sep 17 00:00:00 2001
From: Dmytro Aleksandrov <alkersan@gmail.com>
Date: Sat, 14 Sep 2024 16:49:35 -0400
Subject: [PATCH] Add possibility to opt-out from using vendored dependencies
 on Unix systems

This is to allow Linux distribution maintainers to keep delivering the project
using the distro-provided libs
---
 src/CMakeLists.txt | 259 ++++++++++++++++++++++++---------------------
 1 file changed, 138 insertions(+), 121 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 87286c17..68de7c25 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.15)
 OPTION (ENABLE_CHECK_VERSION "Check for version updates" ON)
 OPTION (ENABLE_TELEMETRY "Enable sending telemetry" ON)
 OPTION (DRIVELIST_FILTER_SYSTEM_DRIVES "Filter System drives from displayed drives" ON)
+OPTION (ENABLE_VENDORING "Use vendored dependencies" ON)
 
 set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "Which macOS architectures to build for")
 
@@ -38,127 +39,143 @@ endif(APPLE)
 # Bundled code will occasionally use identical options - eg, BUILD_TESTING.
 set(BUILD_TESTING OFF)
 
-# Bundled liblzma
-set(XZ_MICROLZMA_DECODER OFF)
-set(XZ_MICROLZMA_ENCODER OFF)
-set(XZ_LZIP_DECODER OFF)
-set(XZ_ENABLE_SANDBOX OFF)
-set(XZ_BUILD_SHARED_LIBS OFF)
-set(XZ_ENABLE_DOXYGEN OFF)
-set(XZ_DECODERS
-    lzma1
-    lzma2
-    delta
-)
-set(XZ_ENCODERS "")
-set(CREATE_LZMA_SYMLINKS OFF)
-set(CREATE_XZ_SYMLINKS OFF)
-add_subdirectory(dependencies/xz-5.6.2)
-set(LIBLZMA_FOUND true)
-set(LIBLZMA_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/xz-5.6.2/src/liblzma/api FORCE)
-set(LIBLZMA_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/xz-5.6.2/src/liblzma/api FORCE)
-set(LIBLZMA_LIBRARY liblzma)
-set(LIBLZMA_LIBRARIES liblzma)
-
-# Bundled zstd
-set(ZSTD_BUILD_PROGRAMS OFF CACHE BOOL "" FORCE)
-set(ZSTD_BUILD_SHARED OFF CACHE BOOL "" FORCE)
-set(ZSTD_BUILD_TESTS OFF CACHE BOOL "" FORCE)
-set(ZSTD_BUILD_DICTBUILDER OFF CACHE BOOL "" FORCE)
-add_subdirectory(dependencies/zstd-1.5.6/build/cmake)
-set(Zstd_FOUND true)
-set(ZSTD_FOUND true)
-set(Zstd_VERSION "1.5.6")
-set(ZSTD_VERSION "1.5.6")
-set(Zstd_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/zstd-1.5.6/lib)
-set(ZSTD_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/zstd-1.5.6/lib)
-set(Zstd_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/zstd-1.5.6/lib)
-set(ZSTD_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/zstd-1.5.6/lib)
-set(Zstd_LIBRARIES libzstd_static)
-set(ZSTD_LIBRARIES libzstd_static)
-
-# Bundled zlib
-set(ZLIB_BUILD_EXAMPLES OFF)
-add_subdirectory(dependencies/zlib-1.3.1)
-set(ZLIB_FOUND TRUE)
-set(ZLIB_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/zlib-1.3.1 CACHE PATH "zlib include dir")
-set(ZLIB_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/zlib-1.3.1 CACHE PATH "zlib include dir")
-set(ZLIB_LIBRARY zlibstatic)
-set(ZLIB_LIBRARIES zlibstatic)
-
-# Bundled libarchive
-set(ARCHIVE_ENABLE_WERROR OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_INSTALL OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_TEST OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_CNG OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_MBEDTLS OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_NETTLE OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_OPENSSL OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_BZip2 OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_LZ4 OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_LZO OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_LIBB2 OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_LIBXML2 OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_EXPAT OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_PCREPOSIX OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_PCRE2POSIX OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_LIBGCC OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_TAR OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_CPIO OFF CACHE BOOL "")
-set(ARCHIVE_ENABLE_CAT OFF CACHE BOOL "")
-set(ARCHIVE_BUILD_SHARED_LIBS OFF CACHE BOOL "")
-add_subdirectory(dependencies/libarchive-3.7.4)
-set(LibArchive_FOUND true)
-set(LibArchive_LIBRARIES archive_static)
-set(LibArchive_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/libarchive-3.7.4/libarchive)
-
-# Bundled libcurl
-set(CMAKE_CURL_INCLUDES)
-set(BUILD_STATIC_CURL true)
-set(BUILD_CURL_EXE OFF CACHE BOOL "" FORCE)
-set(BUILD_LIBCURL_DOCS OFF CACHE BOOL "" FORCE)
-set(ENABLE_CURL_MANUAL OFF CACHE BOOL "" FORCE)
-set(CURL_DISABLE_ALTSVC ON)
-set(CURL_DISABLE_KERBEROS_AUTH ON)
-set(CURL_DISABLE_DICT ON)
-set(CURL_DISABLE_DISABLE_FORM_API ON)
-set(CURL_DISABLE_FTP ON)
-set(CURL_DISABLE_GOPHER ON)
-set(CURL_DISABLE_IMAP ON)
-set(CURL_DISABLE_LDAP ON)
-set(CURL_DISABLE_LDAPS ON)
-set(CURL_DISABLE_MQTT ON)
-set(CURL_DISABLE_NETRC ON)
-set(CURL_DISABLE_POP3 ON)
-set(CURL_DISABLE_RTSP ON)
-set(CURL_DISABLE_SMTP ON)
-set(CURL_DISABLE_TELNET ON)
-set(CURL_DISABLE_TFTP ON)
-set(CURL_ZSTD ON)
-set(CURL_ENABLE_EXPORT_TARGET OFF CACHE BOOL "" FORCE)
-if (APPLE)
-    # TODO: SecureTransport is a deprecated API in macOS, supporting
-    #       only up to TLS v1.2. cURL has not implemented the replacement,
-    #       Network.framework, and so we will need to select an alternative.
-    #       Best recommendation: Libressl, as used by Apple in the curl binary
-    #       on macOS.
-    set(CURL_USE_SECTRANSP ON)
-    set(CURL_DEFAULT_SSL_BACKEND "secure-transport")
-else()
-    if (WIN32)
-        set(CURL_USE_SCHANNEL ON)
-        set(CURL_DEFAULT_SSL_BACKEND "schannel")
-    else ()
-        set(CURL_USE_GNUTLS ON)
-        set(CURL_DEFAULT_SSL_BACKEND "gnutls")
-    endif(WIN32)
-endif(APPLE)
-
-add_subdirectory(dependencies/curl-8.8.0)
-set(CURL_FOUND true)
-set(CURL_LIBRARIES libcurl_static)
-set(CURL_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/curl-8.8.0/include)
-set(CURL_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/curl-8.8.0/include)
+if(ENABLE_VENDORING)
+    # Bundled liblzma
+    set(XZ_MICROLZMA_DECODER OFF)
+    set(XZ_MICROLZMA_ENCODER OFF)
+    set(XZ_LZIP_DECODER OFF)
+    set(XZ_ENABLE_SANDBOX OFF)
+    set(XZ_BUILD_SHARED_LIBS OFF)
+    set(XZ_ENABLE_DOXYGEN OFF)
+    set(XZ_DECODERS
+        lzma1
+        lzma2
+        delta
+    )
+    set(XZ_ENCODERS "")
+    set(CREATE_LZMA_SYMLINKS OFF)
+    set(CREATE_XZ_SYMLINKS OFF)
+    add_subdirectory(dependencies/xz-5.6.2)
+    set(LIBLZMA_FOUND true)
+    set(LIBLZMA_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/xz-5.6.2/src/liblzma/api FORCE)
+    set(LIBLZMA_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/xz-5.6.2/src/liblzma/api FORCE)
+    set(LIBLZMA_LIBRARY liblzma)
+    set(LIBLZMA_LIBRARIES liblzma)
+
+    # Bundled zstd
+    set(ZSTD_BUILD_PROGRAMS OFF CACHE BOOL "" FORCE)
+    set(ZSTD_BUILD_SHARED OFF CACHE BOOL "" FORCE)
+    set(ZSTD_BUILD_TESTS OFF CACHE BOOL "" FORCE)
+    set(ZSTD_BUILD_DICTBUILDER OFF CACHE BOOL "" FORCE)
+    add_subdirectory(dependencies/zstd-1.5.6/build/cmake)
+    set(Zstd_FOUND true)
+    set(ZSTD_FOUND true)
+    set(Zstd_VERSION "1.5.6")
+    set(ZSTD_VERSION "1.5.6")
+    set(Zstd_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/zstd-1.5.6/lib)
+    set(ZSTD_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/zstd-1.5.6/lib)
+    set(Zstd_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/zstd-1.5.6/lib)
+    set(ZSTD_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/zstd-1.5.6/lib)
+    set(Zstd_LIBRARIES libzstd_static)
+    set(ZSTD_LIBRARIES libzstd_static)
+
+    # Bundled zlib
+    set(ZLIB_BUILD_EXAMPLES OFF)
+    add_subdirectory(dependencies/zlib-1.3.1)
+    set(ZLIB_FOUND TRUE)
+    set(ZLIB_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/zlib-1.3.1 CACHE PATH "zlib include dir")
+    set(ZLIB_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/zlib-1.3.1 CACHE PATH "zlib include dir")
+    set(ZLIB_LIBRARY zlibstatic)
+    set(ZLIB_LIBRARIES zlibstatic)
+
+    # Bundled libarchive
+    set(ARCHIVE_ENABLE_WERROR OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_INSTALL OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_TEST OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_CNG OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_MBEDTLS OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_NETTLE OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_OPENSSL OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_BZip2 OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_LZ4 OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_LZO OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_LIBB2 OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_LIBXML2 OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_EXPAT OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_PCREPOSIX OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_PCRE2POSIX OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_LIBGCC OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_TAR OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_CPIO OFF CACHE BOOL "")
+    set(ARCHIVE_ENABLE_CAT OFF CACHE BOOL "")
+    set(ARCHIVE_BUILD_SHARED_LIBS OFF CACHE BOOL "")
+    add_subdirectory(dependencies/libarchive-3.7.4)
+    set(LibArchive_FOUND true)
+    set(LibArchive_LIBRARIES archive_static)
+    set(LibArchive_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/libarchive-3.7.4/libarchive)
+
+    # Bundled libcurl
+    set(CMAKE_CURL_INCLUDES)
+    set(BUILD_STATIC_CURL true)
+    set(BUILD_CURL_EXE OFF CACHE BOOL "" FORCE)
+    set(BUILD_LIBCURL_DOCS OFF CACHE BOOL "" FORCE)
+    set(ENABLE_CURL_MANUAL OFF CACHE BOOL "" FORCE)
+    set(CURL_DISABLE_ALTSVC ON)
+    set(CURL_DISABLE_KERBEROS_AUTH ON)
+    set(CURL_DISABLE_DICT ON)
+    set(CURL_DISABLE_DISABLE_FORM_API ON)
+    set(CURL_DISABLE_FTP ON)
+    set(CURL_DISABLE_GOPHER ON)
+    set(CURL_DISABLE_IMAP ON)
+    set(CURL_DISABLE_LDAP ON)
+    set(CURL_DISABLE_LDAPS ON)
+    set(CURL_DISABLE_MQTT ON)
+    set(CURL_DISABLE_NETRC ON)
+    set(CURL_DISABLE_POP3 ON)
+    set(CURL_DISABLE_RTSP ON)
+    set(CURL_DISABLE_SMTP ON)
+    set(CURL_DISABLE_TELNET ON)
+    set(CURL_DISABLE_TFTP ON)
+    set(CURL_ZSTD ON)
+    set(CURL_ENABLE_EXPORT_TARGET OFF CACHE BOOL "" FORCE)
+    if (APPLE)
+        # TODO: SecureTransport is a deprecated API in macOS, supporting
+        #       only up to TLS v1.2. cURL has not implemented the replacement,
+        #       Network.framework, and so we will need to select an alternative.
+        #       Best recommendation: Libressl, as used by Apple in the curl binary
+        #       on macOS.
+        set(CURL_USE_SECTRANSP ON)
+        set(CURL_DEFAULT_SSL_BACKEND "secure-transport")
+    else()
+        if (WIN32)
+            set(CURL_USE_SCHANNEL ON)
+            set(CURL_DEFAULT_SSL_BACKEND "schannel")
+        else ()
+            set(CURL_USE_GNUTLS ON)
+            set(CURL_DEFAULT_SSL_BACKEND "gnutls")
+        endif(WIN32)
+    endif(APPLE)
+
+    add_subdirectory(dependencies/curl-8.8.0)
+    set(CURL_FOUND true)
+    set(CURL_LIBRARIES libcurl_static)
+    set(CURL_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependencies/curl-8.8.0/include)
+    set(CURL_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/dependencies/curl-8.8.0/include)
+
+elseif (NOT ENABLE_VENDORING)
+    # Find the libraries that were subject to vendoring in the system instead,
+    # To keep the possibility of delivery via Linux distro packages
+    find_package(ZLIB)
+    if(ZLIB_FOUND)
+        set(EXTRALIBS ${EXTRALIBS} ZLIB::ZLIB)
+    endif()
+    find_package(LibLZMA)
+    if(LIBLZMA_FOUND)
+        set(EXTRALIBS ${EXTRALIBS} LibLZMA::LibLZMA)
+    endif()
+    find_package(CURL 7.32.0 REQUIRED)
+    find_package(LibArchive 3.7.4 REQUIRED)
+endif()
 
 # Adding headers explicity so they are displayed in Qt Creator
 set(HEADERS config.h imagewriter.h networkaccessmanagerfactory.h nan.h drivelistitem.h drivelistmodel.h drivelistmodelpollthread.h driveformatthread.h powersaveblocker.h cli.h
