# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: Copyright (C) 2026 Max A. Vilkki a.k.a. codise
#
# liblzma (0BSD) from lib/xz (v5.8.4): the LZMA decoder that libarchive's 7z reader needs
# to unpack HVSC's releases (DESIGN §8.1). Only liblzma is built, none of xz's tools.
#
# Built by xz's own CMake, like SDL3: its feature checks cover the three platforms, which a
# source list of ours would have to repeat. The normal variables below win over its
# option() and cache defaults and only live in this directory's scope.
#
# Decoders only. The encoders would be dropped by the linker anyway, but without them
# nothing can call one by accident. The list is what libarchive's 7z reader may ask for:
# LZMA and LZMA2, and the branch filters and delta that 7-Zip puts in front of them. HVSC
# #85 itself only needs LZMA (its BCJ2 and PPMd are decoded by libarchive).
#
# Size: measured with libarchive, see lib/cmake/libarchive.
#
# Built on macOS (arm64 and x86_64) and Windows (MSVC 19.44, x64); Linux is unverified.

set(BUILD_SHARED_LIBS OFF)
set(BUILD_TESTING OFF)
set(XZ_ENCODERS "")
set(XZ_DECODERS "lzma1;lzma2;x86;arm;armthumb;arm64;powerpc;ia64;sparc;riscv;delta")
set(XZ_CHECKS "crc32")
# One decoder at a time, on our own background thread.
set(XZ_THREADS no)
set(XZ_NLS OFF)
set(XZ_LZIP_DECODER OFF)
set(XZ_MICROLZMA_ENCODER OFF)
set(XZ_MICROLZMA_DECODER OFF)
set(XZ_TOOL_XZ OFF)
set(XZ_TOOL_XZDEC OFF)
set(XZ_TOOL_LZMADEC OFF)
set(XZ_TOOL_LZMAINFO OFF)
set(XZ_TOOL_SCRIPTS OFF)
set(XZ_DOC OFF)

add_subdirectory("${FLIPSID_LIB_DIR}/xz" "${CMAKE_CURRENT_BINARY_DIR}/xz"
    SYSTEM EXCLUDE_FROM_ALL)

add_library(ext::lzma ALIAS liblzma)

# Third-party warnings aren't ours to fix. MSVC: xz picks its own level there (/W4 less
# the warnings it doesn't want), and a /w on top makes cl warn about the override (D9025)
# for every file.
if(NOT MSVC)
    target_compile_options(liblzma PRIVATE -w)
endif()

flipsid_configure_third_party(liblzma)
