Skip to content

Commit 5384597

Browse files
committed
Fix libarchive not detecting liblzma and zstd compression support
libarchive's CMakeLists.txt runs CHECK_FUNCTION_EXISTS and CHECK_C_SOURCE_COMPILES during configure time to detect compression library features. These checks fail when using CPM-managed dependencies because the actual library files don't exist yet at configure time (only the CMake targets exist). This causes libarchive to be built without LZMA/XZ and Zstandard support, resulting in errors like: "Failed to open file: ...json.xz" "Can't initialize filter; unable to run program xz -d -qq" Fix by: 1. Setting liblzma_DIR to point to xz's exported CMake config 2. Adding LIBLZMA_INCLUDE_DIRS (plural) which libarchive requires 3. Pre-caching CHECK_* results so configure-time checks are skipped: - HAVE_LZMA_STREAM_ENCODER_MT=0 (encoder disabled in our build) - LZMA_API_STATIC=1 (static library) - HAVE_LIBZSTD=1 (decompressor available) - HAVE_ZSTD_compressStream=0 (compressor disabled) - HAVE_ZSTD_minCLevel=0 (compressor disabled) CMake's CHECK_* macros skip execution when the result variable is already defined in the cache, allowing us to bypass the compile/link tests that would otherwise fail.
1 parent b3e42ed commit 5384597

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/Utilities/Compression/CMakeLists.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,22 @@ CPMAddPackage(
9898
)
9999

100100
if(TARGET liblzma)
101-
# Help libarchive find our liblzma (prevent using system liblzma during cross-compile)
101+
# Set Find-module variables so libarchive's find_package(LibLZMA) succeeds
102102
set(LIBLZMA_INCLUDE_DIR "${xz_SOURCE_DIR}/src/liblzma/api" CACHE PATH "" FORCE)
103+
set(LIBLZMA_INCLUDE_DIRS "${xz_SOURCE_DIR}/src/liblzma/api" CACHE PATH "" FORCE) # libarchive uses plural
103104
set(LIBLZMA_LIBRARY "liblzma" CACHE STRING "" FORCE)
104105
set(LIBLZMA_FOUND TRUE CACHE BOOL "" FORCE)
105106
set(LIBLZMA_LIBRARIES "liblzma" CACHE STRING "" FORCE)
106107
set(LIBLZMA_HAS_AUTO_DECODER TRUE CACHE BOOL "" FORCE)
107-
set(LIBLZMA_HAS_EASY_ENCODER FALSE CACHE BOOL "" FORCE) # Encoders disabled
108-
set(LIBLZMA_HAS_LZMA_PRESET FALSE CACHE BOOL "" FORCE) # Encoders disabled
108+
set(LIBLZMA_HAS_EASY_ENCODER TRUE CACHE BOOL "" FORCE) # Satisfy FindLibLZMA sanity check
109+
set(LIBLZMA_HAS_LZMA_PRESET TRUE CACHE BOOL "" FORCE) # Satisfy FindLibLZMA sanity check
109110
set(LibLZMA_FOUND TRUE CACHE BOOL "" FORCE)
110-
# libarchive's FindLibLZMA expects LibLZMA::LibLZMA target
111+
112+
# Pre-cache configure-time check results (checks require built library, but we only have target)
113+
# These values match our decoder-only build configuration
114+
set(LZMA_API_STATIC 1 CACHE INTERNAL "liblzma is static")
115+
set(HAVE_LZMA_STREAM_ENCODER_MT 0 CACHE INTERNAL "Encoder disabled in our build")
116+
111117
if(NOT TARGET LibLZMA::LibLZMA)
112118
add_library(LibLZMA::LibLZMA ALIAS liblzma)
113119
endif()
@@ -149,7 +155,14 @@ if(TARGET libzstd_static)
149155
set(ZSTD_LIBRARIES "libzstd_static" CACHE STRING "" FORCE)
150156
set(ZSTD_FOUND TRUE CACHE BOOL "" FORCE)
151157
set(Zstd_FOUND TRUE CACHE BOOL "" FORCE)
152-
# libarchive's FindZstd.cmake expects Zstd::Zstd target
158+
159+
# Pre-cache configure-time check results (checks require built library, but we only have target)
160+
# These values match our decoder-only build configuration
161+
set(HAVE_LIBZSTD 1 CACHE INTERNAL "zstd decompression available")
162+
set(HAVE_ZSTD_compressStream 0 CACHE INTERNAL "Compression disabled in our build")
163+
set(HAVE_ZSTD_minCLevel 0 CACHE INTERNAL "Compression disabled in our build")
164+
165+
# Create the imported targets that consumers expect
153166
if(NOT TARGET Zstd::Zstd)
154167
add_library(Zstd::Zstd ALIAS libzstd_static)
155168
endif()

0 commit comments

Comments
 (0)