include(ExternalProject)

set(Blosc_BUILD_SHARED ON)
set(Blosc_BUILD_STATIC OFF)
if (NOT BUILD_SHARED_LIBS)
    set(Blosc_BUILD_SHARED OFF)
    set(Blosc_BUILD_STATIC ON)
endif()

set(Blosc_ARGS
    -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
    -DCMAKE_INSTALL_LIBDIR=lib
    -DCMAKE_PREFIX_PATH=${CMAKE_INSTALL_PREFIX}
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DCMAKE_POLICY_DEFAULT_CMP0091:STRING=NEW
    -DCMAKE_FIND_FRAMEWORK=${CMAKE_FIND_FRAMEWORK}
    -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
    -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
    -DCMAKE_MSVC_RUNTIME_LIBRARY=${CMAKE_MSVC_RUNTIME_LIBRARY}
    -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
    -DBUILD_SHARED=${Blosc_BUILD_SHARED}
    -DBUILD_STATIC=${Blosc_BUILD_STATIC}
    -DBUILD_TESTS=OFF
    -DBUILD_FUZZERS=OFF
    -DBUILD_BENCHMARKS=OFF
    -DPREFER_EXTERNAL_ZLIB=ON)
if(CMAKE_CXX_STANDARD)
    set(Blosc_ARGS ${Blosc_ARGS} -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD})
endif()

ExternalProject_Add(
    Blosc
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/Blosc
    GIT_REPOSITORY https://github.com/Blosc/c-blosc.git
    GIT_TAG v1.20.1
    CMAKE_ARGS ${Blosc_ARGS})
