6.20. libarchive 3.8.7
libarchive provides archive libraries and BSD archive tools, including bsdtar, bsdcpio, and bsdunzip.
Input assumption:
libarchive-3.8.7.tar.xz is already present in LBI_SOURCES from the chapter 4 source staging step.
Licenses:
- BSD-2-Clause (primary)
- BSD-3-Clause, public domain, and other permissive notices in selected files
Dependencies:
- musl (libc)
- cmake
- make
- zlib-ng (libz)
libarchive is a multi-format archive reading and writing library plus BSD archive utilities. we need it to provide tar, cpio, and unzip command compatibility in the target userspace.
Extract and Enter the Source Tree
cd "$LBI_SOURCES"
tar -xf libarchive-3.8.7.tar.xz
cd libarchive-3.8.7
Configure libarchive (CMake Path)
Working directory requirement: run this block from the
libarchive-3.8.7 source root (the directory containing CMakeLists.txt and build/version), not from inside build/.
lbi_cmake build \
-DCMAKE_C_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang" \
-DCMAKE_AR="$LBI_ROOT/system/tools/bin/$LBI_TARGET-ar" \
-DCMAKE_RANLIB="$LBI_ROOT/system/tools/bin/$LBI_TARGET-ranlib" \
-DCMAKE_C_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT $LWI_CFLAGS" \
-DCMAKE_SHARED_LINKER_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT $LBI_CUSTOM_LDFLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT $LBI_CUSTOM_LDFLAGS" \
-DBUILD_SHARED_LIBS=ON \
-DENABLE_TAR=ON \
-DENABLE_CPIO=ON \
-DENABLE_UNZIP=ON \
-DENABLE_CAT=OFF \
-DENABLE_ZLIB=ON \
-DENABLE_OPENSSL=OFF \
-DENABLE_BZip2=OFF \
-DENABLE_LZMA=OFF \
-DENABLE_ZSTD=OFF \
-DENABLE_LZ4=OFF \
-DENABLE_LZO=OFF \
-DENABLE_LIBB2=OFF \
-DENABLE_LIBXML2=OFF \
-DENABLE_EXPAT=OFF \
-DENABLE_ICONV=OFF \
-DENABLE_ACL=OFF \
-DENABLE_XATTR=OFF \
-DENABLE_TEST=OFF
Build libarchive
cmake --build build $LWI_MAKE_FLAGS
Install libarchive
DESTDIR="$LBI_ROOT" cmake --install build
Normalize Installed Paths to LBI Layout
Path note: upstream
libarchive CMake install rules hardcode some destinations (bin, include, share/man). Move those outputs into the book's layout after install.
install -d \
"$LBI_ROOT/system/binaries" \
"$LBI_ROOT/system/headers" \
"$LBI_ROOT/system/documentation/man-pages/man1" \
"$LBI_ROOT/system/documentation/man-pages/man3" \
"$LBI_ROOT/system/documentation/man-pages/man5"
if [ -d "$LBI_ROOT/system/bin" ]; then
mv -v "$LBI_ROOT/system/bin/"* "$LBI_ROOT/system/binaries/" 2>/dev/null || true
rmdir -v "$LBI_ROOT/system/bin" 2>/dev/null || true
fi
if [ -d "$LBI_ROOT/system/include" ]; then
mv -v "$LBI_ROOT/system/include/"* "$LBI_ROOT/system/headers/" 2>/dev/null || true
rmdir -v "$LBI_ROOT/system/include" 2>/dev/null || true
fi
for sec in man1 man3 man5; do
if [ -d "$LBI_ROOT/system/share/man/$sec" ]; then
mv -v "$LBI_ROOT/system/share/man/$sec/"* \
"$LBI_ROOT/system/documentation/man-pages/$sec/" 2>/dev/null || true
rmdir -v "$LBI_ROOT/system/share/man/$sec" 2>/dev/null || true
fi
done
Post-install pkg-config Fix
sed -i \
-e 's|^prefix=.*$|prefix=/system|' \
-e 's|^exec_prefix=.*$|exec_prefix=${prefix}|' \
-e 's|^libdir=.*$|libdir=${exec_prefix}/libraries|' \
-e 's|^includedir=.*$|includedir=${prefix}/headers|' \
"$LBI_ROOT/system/libraries/pkgconfig/libarchive.pc"
Provide tar, cpio, and unzip Commands
ln -sf bsdtar "$LBI_ROOT/system/binaries/tar"
ln -sf bsdcpio "$LBI_ROOT/system/binaries/cpio"
ln -sf bsdunzip "$LBI_ROOT/system/binaries/unzip"
Command Explanations
lbi_cmake build: Configures libarchive with the book's CMake helper and writes generated files into thebuilddirectory.-DCMAKE_C_COMPILER,-DCMAKE_AR, and-DCMAKE_RANLIB: Use the target-prefixed LLVM compiler and archive tools.-DCMAKE_C_FLAGS,-DCMAKE_SHARED_LINKER_FLAGS, and-DCMAKE_EXE_LINKER_FLAGS: Target the selected triple/sysroot and preserve local compile/link tuning.-DBUILD_SHARED_LIBS=ON: Builds shared libraries for the target system.-DENABLE_TAR=ON,-DENABLE_CPIO=ON, and-DENABLE_UNZIP=ON: Builds the archive command-line tools needed for command compatibility.-DENABLE_CAT=OFF: Skips the optionalbsdcattool.-DENABLE_ZLIB=ON: Enables gzip/deflate support through the installed zlib-compatible library.-DENABLE_OPENSSL=OFF,-DENABLE_BZip2=OFF,-DENABLE_LZMA=OFF,-DENABLE_ZSTD=OFF,-DENABLE_LZ4=OFF,-DENABLE_LZO=OFF,-DENABLE_LIBB2=OFF,-DENABLE_LIBXML2=OFF,-DENABLE_EXPAT=OFF,-DENABLE_ICONV=OFF,-DENABLE_ACL=OFF, and-DENABLE_XATTR=OFF: Disable optional dependencies that are not part of this early target userspace.-DENABLE_TEST=OFF: Skips test programs during the bootstrap build.cmake --build build $LWI_MAKE_FLAGS: Builds the configured tree using the shared parallel build setting.DESTDIR="$LBI_ROOT" cmake --install build: Installs into the target root rather than the host filesystem.install -d ...: Creates the final destination directories required by the book's layout.mv -v "$LBI_ROOT/system/bin/"* ...andmv -v "$LBI_ROOT/system/include/"* ...: Move upstream CMake install outputs into/system/binariesand/system/headers.for sec in man1 man3 man5; do ... done: Moves installed manual pages from upstream'sshare/manlayout into the book's documentation tree.2>/dev/null || true: Allows cleanup moves and directory removals to be harmless when an optional directory is empty or absent.sed -i ... libarchive.pc: Rewrites pkg-config metadata so later builds discover libarchive under/system/librariesand/system/headers.ln -sf bsdtar ... tar,ln -sf bsdcpio ... cpio, andln -sf bsdunzip ... unzip: Provide standard archive command names through libarchive's BSD tools.