# only add PUBLIC dependencies as well
#   https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#creating-a-package-configuration-file
include(CMakeFindDependencyMacro)

# Search in <PackageName>_ROOT:
#   https://cmake.org/cmake/help/v3.12/policy/CMP0074.html
if(POLICY CMP0074)
    cmake_policy(SET CMP0074 NEW)
endif()

# locate the installed FindABC.cmake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/Modules")

# this section stores which configuration options were set
set(HAVE_THREADS @HAVE_THREADS@)
set(HAVE_IPP @HAVE_IPP@)
set(HAVE_LZ4_CONFIG @HAVE_LZ4_CONFIG@)
set(HAVE_ZLIB_NG @HAVE_ZLIB_NG@)
set(HAVE_ZLIB_NG_CONFIG @HAVE_ZLIB_NG_CONFIG@)
set(HAVE_ZSTD_CONFIG @HAVE_ZSTD_CONFIG@)
set(DEACTIVATE_IPP @DEACTIVATE_IPP@)
set(DEACTIVATE_ZLIB @DEACTIVATE_ZLIB@)
set(DEACTIVATE_ZSTD @DEACTIVATE_ZSTD@)
set(PREFER_EXTERNAL_LZ4 @PREFER_EXTERNAL_LZ4@)
set(PREFER_EXTERNAL_ZLIB @PREFER_EXTERNAL_ZLIB@)
set(PREFER_EXTERNAL_ZSTD @PREFER_EXTERNAL_ZSTD@)

# find dependencies and their targets, which are used in our Blosc2Targets.cmake
#   additionally, the Blosc2_..._FOUND variables are used to support
#   find_package(Blosc2 ... COMPONENTS ... ...)
#   this enables downstream projects to express the need for specific features.
set(CMAKE_THREAD_PREFER_PTHREAD TRUE) # pre 3.1
set(THREADS_PREFER_PTHREAD_FLAG TRUE) # CMake 3.1+
if(HAVE_THREADS)
    find_dependency(Threads)
    set(Blosc2_THREADS_FOUND TRUE)
else()
    set(Blosc2_THREADS_FOUND FALSE)
endif()

if(NOT DEACTIVATE_IPP AND HAVE_IPP)
    find_dependency(IPP)
    set(Blosc2_IPP_FOUND FALSE)
else()
    set(Blosc2_IPP_FOUND TRUE)
endif()

if(PREFER_EXTERNAL_LZ4 AND HAVE_LZ4_CONFIG)
    find_dependency(lz4 CONFIG)
endif()
set(Blosc2_LZ4_FOUND TRUE)

if(DEACTIVATE_ZLIB)
    set(Blosc2_ZLIB_FOUND FALSE)
elseif(NOT DEACTIVATE_ZLIB AND PREFER_EXTERNAL_ZLIB)
    if(HAVE_ZLIB_NG)
        if (HAVE_ZLIB_NG_CONFIG)
            find_dependency(zlib-ng CONFIG)
        endif()
    else()
        find_dependency(ZLIB)
    endif()
    set(Blosc2_ZLIB_FOUND TRUE)
endif()

if(DEACTIVATE_ZSTD)
    set(Blosc2_ZSTD_FOUND FALSE)
elseif(NOT DEACTIVATE_ZSTD AND PREFER_EXTERNAL_ZSTD)
    if(HAVE_ZSTD_CONFIG)
        find_dependency(zstd CONFIG)
    endif()
    set(Blosc2_ZSTD_FOUND TRUE)
endif()

# define central Blosc2::blosc2_shared/static targets
include("${CMAKE_CURRENT_LIST_DIR}/Blosc2Targets.cmake")

# check if components are fulfilled and set Blosc2_<COMPONENT>_FOUND vars
#   Blosc2_FIND_COMPONENTS is a list set by find_package(... COMPONENTS ... ...)
#   likewise Blosc2_FIND_REQUIRED_... per component specified
foreach(comp ${Blosc2_FIND_COMPONENTS})
    if(NOT Blosc2_${comp}_FOUND)
        if(Blosc2_FIND_REQUIRED_${comp})
            set(Blosc2_FOUND FALSE)
        endif()
    endif()
endforeach()

