add_subdirectory(ndlz)

if(HAVE_ZFP)
    if(BLOSC_ZFP_PROVIDER STREQUAL "BUNDLED")
        include(FetchContent)
        # Configure the upstream ZFP subproject when brought in via FetchContent.
        # These are upstream ZFP options, not C-Blosc2 plugin options.  Keep the
        # embedded ZFP build minimal (library only), static/PIC-friendly, and free of
        # extra components like tests, examples, Python/Fortran bindings, utilities,
        # or OpenMP support.
        #
        # Use normal variables (not CACHE entries) so these settings stay local to the
        # embedded ZFP configure step and do not pollute the parent cache or collide
        # with other subprojects using similarly generic option names.
        # ZFP declares these via option(), so also force CMP0077=NEW for the fetched
        # subproject through CMAKE_POLICY_DEFAULT_CMP0077; otherwise CMake may emit dev
        # warnings and let option() clear the normal variables.
        set(BLOSC_SAVED_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
        set(BLOSC_HAD_BUILD_UTILITIES FALSE)
        if(DEFINED BUILD_UTILITIES)
            set(BLOSC_HAD_BUILD_UTILITIES TRUE)
            set(BLOSC_SAVED_BUILD_UTILITIES ${BUILD_UTILITIES})
        endif()
        set(BLOSC_HAD_BUILD_EXAMPLES FALSE)
        if(DEFINED BUILD_EXAMPLES)
            set(BLOSC_HAD_BUILD_EXAMPLES TRUE)
            set(BLOSC_SAVED_BUILD_EXAMPLES ${BUILD_EXAMPLES})
        endif()
        set(BLOSC_HAD_BUILD_CFP FALSE)
        if(DEFINED BUILD_CFP)
            set(BLOSC_HAD_BUILD_CFP TRUE)
            set(BLOSC_SAVED_BUILD_CFP ${BUILD_CFP})
        endif()
        set(BLOSC_HAD_BUILD_ZFORP FALSE)
        if(DEFINED BUILD_ZFORP)
            set(BLOSC_HAD_BUILD_ZFORP TRUE)
            set(BLOSC_SAVED_BUILD_ZFORP ${BUILD_ZFORP})
        endif()
        set(BLOSC_HAD_BUILD_ZFPY FALSE)
        if(DEFINED BUILD_ZFPY)
            set(BLOSC_HAD_BUILD_ZFPY TRUE)
            set(BLOSC_SAVED_BUILD_ZFPY ${BUILD_ZFPY})
        endif()
        set(BLOSC_HAD_BUILD_TESTING FALSE)
        if(DEFINED BUILD_TESTING)
            set(BLOSC_HAD_BUILD_TESTING TRUE)
            set(BLOSC_SAVED_BUILD_TESTING ${BUILD_TESTING})
        endif()
        set(BLOSC_HAD_ZFP_WITH_OPENMP FALSE)
        if(DEFINED ZFP_WITH_OPENMP)
            set(BLOSC_HAD_ZFP_WITH_OPENMP TRUE)
            set(BLOSC_SAVED_ZFP_WITH_OPENMP ${ZFP_WITH_OPENMP})
        endif()
        set(BLOSC_HAD_ZFP_ENABLE_PIC FALSE)
        if(DEFINED ZFP_ENABLE_PIC)
            set(BLOSC_HAD_ZFP_ENABLE_PIC TRUE)
            set(BLOSC_SAVED_ZFP_ENABLE_PIC ${ZFP_ENABLE_PIC})
        endif()
        set(BLOSC_HAD_CMAKE_POLICY_DEFAULT_CMP0077 FALSE)
        if(DEFINED CMAKE_POLICY_DEFAULT_CMP0077)
            set(BLOSC_HAD_CMAKE_POLICY_DEFAULT_CMP0077 TRUE)
            set(BLOSC_SAVED_CMAKE_POLICY_DEFAULT_CMP0077 ${CMAKE_POLICY_DEFAULT_CMP0077})
        endif()
        set(BLOSC_HAD_CMAKE_WARN_DEPRECATED FALSE)
        if(DEFINED CMAKE_WARN_DEPRECATED)
            set(BLOSC_HAD_CMAKE_WARN_DEPRECATED TRUE)
            set(BLOSC_SAVED_CMAKE_WARN_DEPRECATED ${CMAKE_WARN_DEPRECATED})
        endif()
        set(BLOSC_HAD_CMAKE_INSTALL_BINDIR FALSE)
        if(DEFINED CMAKE_INSTALL_BINDIR)
            set(BLOSC_HAD_CMAKE_INSTALL_BINDIR TRUE)
            set(BLOSC_SAVED_CMAKE_INSTALL_BINDIR ${CMAKE_INSTALL_BINDIR})
        endif()
        set(BLOSC_HAD_CMAKE_INSTALL_LIBDIR FALSE)
        if(DEFINED CMAKE_INSTALL_LIBDIR)
            set(BLOSC_HAD_CMAKE_INSTALL_LIBDIR TRUE)
            set(BLOSC_SAVED_CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
        endif()
        set(BLOSC_HAD_CMAKE_INSTALL_INCLUDEDIR FALSE)
        if(DEFINED CMAKE_INSTALL_INCLUDEDIR)
            set(BLOSC_HAD_CMAKE_INSTALL_INCLUDEDIR TRUE)
            set(BLOSC_SAVED_CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR})
        endif()

        set(BUILD_SHARED_LIBS OFF)
        set(BUILD_UTILITIES OFF)
        set(BUILD_EXAMPLES OFF)
        set(BUILD_CFP OFF)
        set(BUILD_ZFORP OFF)
        set(BUILD_ZFPY OFF)
        set(BUILD_TESTING OFF)
        set(ZFP_WITH_OPENMP OFF)
        set(ZFP_ENABLE_PIC ON)
        set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
        set(CMAKE_WARN_DEPRECATED FALSE)
        # zfp derives build output directories from CMAKE_INSTALL_*DIR.  Keep these
        # relative while configuring the fetched subproject so an absolute parent
        # install layout (e.g. wheel staging on Windows) does not leak into zfp's
        # archive/library output paths.
        set(CMAKE_INSTALL_BINDIR bin)
        set(CMAKE_INSTALL_LIBDIR lib)
        set(CMAKE_INSTALL_INCLUDEDIR include)

        if(BLOSC_ZFP_SOURCE_DIR)
            message(STATUS "Using zfp via FetchContent from local checkout: ${BLOSC_ZFP_SOURCE_DIR}")
            FetchContent_Declare(
                zfp
                SOURCE_DIR "${BLOSC_ZFP_SOURCE_DIR}")
        else()
            message(STATUS "Using zfp ${BLOSC_ZFP_VERSION} via FetchContent download")
            FetchContent_Declare(
                zfp
                URL "https://github.com/LLNL/zfp/archive/refs/tags/${BLOSC_ZFP_VERSION}.tar.gz"
                DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
        endif()
        FetchContent_GetProperties(zfp)
        if(NOT zfp_POPULATED)
            if(POLICY CMP0169)
                cmake_policy(PUSH)
                cmake_policy(SET CMP0169 OLD)
            endif()
            FetchContent_Populate(zfp)
            if(POLICY CMP0169)
                cmake_policy(POP)
            endif()
            add_subdirectory("${zfp_SOURCE_DIR}" "${zfp_BINARY_DIR}" EXCLUDE_FROM_ALL)
        endif()

        set(BUILD_SHARED_LIBS ${BLOSC_SAVED_BUILD_SHARED_LIBS})
        if(BLOSC_HAD_BUILD_UTILITIES)
            set(BUILD_UTILITIES ${BLOSC_SAVED_BUILD_UTILITIES})
        else()
            unset(BUILD_UTILITIES)
        endif()
        if(BLOSC_HAD_BUILD_EXAMPLES)
            set(BUILD_EXAMPLES ${BLOSC_SAVED_BUILD_EXAMPLES})
        else()
            unset(BUILD_EXAMPLES)
        endif()
        if(BLOSC_HAD_BUILD_CFP)
            set(BUILD_CFP ${BLOSC_SAVED_BUILD_CFP})
        else()
            unset(BUILD_CFP)
        endif()
        if(BLOSC_HAD_BUILD_ZFORP)
            set(BUILD_ZFORP ${BLOSC_SAVED_BUILD_ZFORP})
        else()
            unset(BUILD_ZFORP)
        endif()
        if(BLOSC_HAD_BUILD_ZFPY)
            set(BUILD_ZFPY ${BLOSC_SAVED_BUILD_ZFPY})
        else()
            unset(BUILD_ZFPY)
        endif()
        if(BLOSC_HAD_BUILD_TESTING)
            set(BUILD_TESTING ${BLOSC_SAVED_BUILD_TESTING})
        else()
            unset(BUILD_TESTING)
        endif()
        if(BLOSC_HAD_ZFP_WITH_OPENMP)
            set(ZFP_WITH_OPENMP ${BLOSC_SAVED_ZFP_WITH_OPENMP})
        else()
            unset(ZFP_WITH_OPENMP)
        endif()
        if(BLOSC_HAD_ZFP_ENABLE_PIC)
            set(ZFP_ENABLE_PIC ${BLOSC_SAVED_ZFP_ENABLE_PIC})
        else()
            unset(ZFP_ENABLE_PIC)
        endif()
        if(BLOSC_HAD_CMAKE_POLICY_DEFAULT_CMP0077)
            set(CMAKE_POLICY_DEFAULT_CMP0077 ${BLOSC_SAVED_CMAKE_POLICY_DEFAULT_CMP0077})
        else()
            unset(CMAKE_POLICY_DEFAULT_CMP0077)
        endif()
        if(BLOSC_HAD_CMAKE_WARN_DEPRECATED)
            set(CMAKE_WARN_DEPRECATED ${BLOSC_SAVED_CMAKE_WARN_DEPRECATED})
        else()
            unset(CMAKE_WARN_DEPRECATED)
        endif()
        if(BLOSC_HAD_CMAKE_INSTALL_BINDIR)
            set(CMAKE_INSTALL_BINDIR ${BLOSC_SAVED_CMAKE_INSTALL_BINDIR})
        else()
            unset(CMAKE_INSTALL_BINDIR)
        endif()
        if(BLOSC_HAD_CMAKE_INSTALL_LIBDIR)
            set(CMAKE_INSTALL_LIBDIR ${BLOSC_SAVED_CMAKE_INSTALL_LIBDIR})
        else()
            unset(CMAKE_INSTALL_LIBDIR)
        endif()
        if(BLOSC_HAD_CMAKE_INSTALL_INCLUDEDIR)
            set(CMAKE_INSTALL_INCLUDEDIR ${BLOSC_SAVED_CMAKE_INSTALL_INCLUDEDIR})
        else()
            unset(CMAKE_INSTALL_INCLUDEDIR)
        endif()

        set(ZFP_FETCHED TRUE PARENT_SCOPE)
        set(ZFP_SOURCE_DIR "${zfp_SOURCE_DIR}" PARENT_SCOPE)
        set(ZFP_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/blosc2/thirdparty/zfp" PARENT_SCOPE)
        if(TARGET zfp)
            set_target_properties(zfp PROPERTIES POSITION_INDEPENDENT_CODE ON)
            set_property(TARGET zfp PROPERTY INTERFACE_INCLUDE_DIRECTORIES
                "$<BUILD_INTERFACE:${zfp_SOURCE_DIR}/include>"
                "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/blosc2/thirdparty/zfp>")
        endif()
    endif()

    add_subdirectory(zfp)
endif()

set(SOURCES ${SOURCES} ${PROJECT_SOURCE_DIR}/plugins/codecs/codecs-registry.c PARENT_SCOPE)
