cmake_minimum_required(VERSION 3.16)
project(build_libarchive)
include(ExternalProject)

if(NOT DEFINED ZLIB_REPO OR ZLIB_REPO STREQUAL "")
    set(ZLIB_REPO https://github.com/madler/zlib.git)
endif ()
if(NOT DEFINED ZSTD_REPO OR ZSTD_REPO STREQUAL "")
    set(ZSTD_REPO https://github.com/facebook/zstd)
endif ()
if(NOT DEFINED LZ4_REPO OR LZ4_REPO STREQUAL "")
    set(LZ4_REPO https://github.com/lz4/lz4)
endif ()
if(NOT DEFINED LIBARCHIVE_REPO OR LIBARCHIVE_REPO STREQUAL "")
    set(LIBARCHIVE_REPO https://github.com/libarchive/libarchive.git)
endif ()

ExternalProject_Add(zlib
        GIT_REPOSITORY ${ZLIB_REPO}
        GIT_TAG v1.3.1
        GIT_SHALLOW TRUE
        SOURCE_DIR zlib
        BINARY_DIR zlib-build
        CMAKE_GENERATOR ${CMAKE_GENERATOR}
        CMAKE_ARGS
            -DBUILD_SHARED_LIBS:BOOL=OFF
            -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
            -DPLATFORM=${PLATFORM}
            -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/INSTALL
            -DCMAKE_BUILD_TYPE:STRING=Release
        )

ExternalProject_Add(zstd
        GIT_REPOSITORY ${ZSTD_REPO}
        GIT_TAG v1.5.5
        GIT_SHALLOW TRUE
        SOURCE_DIR zstd
        SOURCE_SUBDIR build/cmake
        BINARY_DIR zstd-build
        CMAKE_GENERATOR ${CMAKE_GENERATOR}
        CMAKE_ARGS
                    -DZSTD_BUILD_PROGRAMS:BOOL=OFF
                    -DZSTD_BUILD_SHARED:BOOL=OFF
                    -DBUILD_SHARED_LIBS:BOOL=OFF
                    -DZSTD_BUILD_STATIC:BOOL=ON
                    -DCMAKE_BUILD_TYPE:STRING=Release
                    -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
                    -DPLATFORM=${PLATFORM}
                    -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/INSTALL
        )

ExternalProject_Add(lz4
        GIT_REPOSITORY ${LZ4_REPO}
        GIT_TAG v1.9.4
        GIT_SHALLOW TRUE
        SOURCE_DIR lz4
        BINARY_DIR lz4-build
        SOURCE_SUBDIR build/cmake
        CMAKE_GENERATOR ${CMAKE_GENERATOR}
        CMAKE_ARGS
                    -DBUILD_SHARED_LIBS:BOOL=OFF
                    -DLZ4_BUILD_CLI:BOOL=OFF
                    -DLZ4_BUILD_LEGACY_LZ4C:BOOL=OFF
                    -DLZ4_BUNDLE_MODE:BOOL=ON
                    -DCMAKE_BUILD_TYPE:STRING=Release
                    -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
                    -DPLATFORM=${PLATFORM}
                    -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/INSTALL
        )

if ("${PLATFORM}" STREQUAL "OS64" OR "${PLATFORM}" STREQUAL "OS64COMBINED")
    set(EXTRA_ARGS -DHAVE__FSEEKI64:BOOL=OFF -DHAVE_FUTIMESAT:BOOL=OFF)
endif()

ExternalProject_Add(
        libarchive
        DEPENDS zlib zstd lz4
        GIT_REPOSITORY ${LIBARCHIVE_REPO}
        GIT_TAG v3.7.4
        GIT_SHALLOW TRUE
        SOURCE_DIR libarchive
        BINARY_DIR libarchive-build
        CMAKE_GENERATOR ${CMAKE_GENERATOR}
        CMAKE_ARGS
            -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
            -DPLATFORM=${PLATFORM}
            -DDEPLOYMENT_TARGET:STRING=14.0
            -DCMAKE_BUILD_TYPE:STRING=Release
            -DBUILD_SHARED_LIBS:BOOL=OFF
            -DENABLE_TEST:BOOL=OFF
            -DENABLE_TAR:BOOL=OFF
            -DENABLE_CPIO:BOOL=OFF
            -DENABLE_CAT:BOOL=OFF
            -DENABLE_OPENSSL:BOOL=OFF
            -DENABLE_ACL:BOOL=OFF
            -DENABLE_ICONV:BOOL=OFF
            -DENABLE_LIBB2:BOOL=OFF
            -DENABLE_LIBXML2:BOOL=OFF
            -DENABLE_UNZIP:BOOL=OFF
            -DENABLE_EXPAT:BOOL=OFF
            -DZLIB_USE_STATIC_LIBS:BOOL=ON
            -DCMAKE_PREFIX_PATH=${CMAKE_BINARY_DIR}/INSTALL
            -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/INSTALL
            ${EXTRA_ARGS}
)