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:

Dependencies:

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