#!/bin/bash
set -euo pipefail
# Deployment script for 7-Zip-zstd on Debian 11 (Bullseye) ARM64 (RK3588)
# --- Configuration Variables ---
REPO_URL="https://github.com/ib-bsb-br/7-Zip-zstd"
REPO_DIR="$HOME/7-Zip-zstd"
STAGING_DIR="/opt/7zip-zstd"
INSTALL_BIN_DIR="/usr/local/bin"
INSTALL_LIB_DIR="/usr/local/lib"
# Optional build toggles
RUN_GCC_NON_ASM_BUILD="${RUN_GCC_NON_ASM_BUILD:-yes}"
RUN_CLANG_BUILD="${RUN_CLANG_BUILD:-no}"
TEMP_DIR=""
# --- Helper Functions ---
cleanup_temp() {
echo "Cleaning up temporary files..."
if [ -n "$TEMP_DIR" ] && [ -d "$TEMP_DIR" ]; then
rm -rf "$TEMP_DIR"
echo "Removed temporary directory: $TEMP_DIR"
fi
}
trap cleanup_temp EXIT SIGINT SIGTERM
ensure_tool() {
local tool="$1"
local package="$2"
if ! command -v "$tool" >/dev/null 2>&1; then
echo "Installing missing tool: $tool (package: $package)"
if ! sudo apt install -y "$package"; then
echo "Error: Failed to install $package." >&2
exit 1
fi
else
echo "Tool already installed: $tool"
fi
}
ensure_package() {
local package="$1"
if ! dpkg -s "$package" >/dev/null 2>&1; then
echo "Installing package: $package"
if ! sudo apt install -y "$package"; then
echo "Error: Failed to install $package." >&2
exit 1
fi
else
echo "Package already installed: $package"
fi
}
download_asset() {
local url="$1"
local output="$2"
if [ -z "$url" ]; then
echo "No URL provided for $output; skipping download."
return 0
fi
echo "Downloading $url -> $output"
if ! curl -Lfo "$output" "$url"; then
echo "Error: Failed to download $url" >&2
exit 1
fi
}
confirm_continue() {
local message="$1"
read -r -p "$message (yes/NO): " confirmation
if [[ "$confirmation" != "yes" ]]; then
echo "Operation cancelled by user." >&2
exit 1
fi
}
# --- Main Script ---
echo "Starting 7-Zip-zstd deployment on RK3588 (ARM64)..."
# Step 1: Update package lists
if ! sudo apt update; then
echo "Error: Failed to update package lists." >&2
exit 1
fi
# Step 2: Install required tools
ensure_tool "curl" "curl"
ensure_tool "git" "git"
ensure_tool "make" "make"
ensure_tool "gcc" "gcc"
ensure_tool "clang" "clang"
ensure_tool "unzip" "unzip"
ensure_tool "7z" "p7zip-full"
# Step 3: Prepare temp directory
TEMP_DIR="$(mktemp -d)"
if [ ! -d "$TEMP_DIR" ]; then
echo "Error: Failed to create temporary directory." >&2
exit 1
fi
# Step 5: Clone repository if not present
if [ ! -d "$REPO_DIR" ]; then
echo "Cloning repository: $REPO_URL"
if ! git clone "$REPO_URL" "$REPO_DIR"; then
echo "Error: Failed to clone repository." >&2
exit 1
fi
else
echo "Repository already exists at $REPO_DIR; pulling latest changes."
if ! git -C "$REPO_DIR" pull --ff-only; then
echo "Error: Failed to update repository." >&2
exit 1
fi
fi
# Step 6: Build from source (ARM64)
echo "Building 7-Zip-zstd from source..."
BUILD_DIR="$REPO_DIR/CPP/7zip/Bundles/Alone2"
if [ ! -d "$BUILD_DIR" ]; then
echo "Error: Build directory not found: $BUILD_DIR" >&2
exit 1
fi
# The project makefiles enable -Werror. On some GCC versions for ARM64,
# sign-conversion/cast-align warnings can fail the build. Override the warning
# flags to avoid treating warnings as errors while preserving -Wall/-Wextra.
MAKE_WARN_OVERRIDES="CFLAGS_WARN_WALL=-Wall -Wextra"
MAKE_LTO_DISABLE="FLAGS_FLTO="
# Default GCC build
if ! (cd "$BUILD_DIR" && make -j"$(nproc)" -f makefile.gcc $MAKE_WARN_OVERRIDES); then
echo "Error: GCC build failed." >&2
exit 1
fi
# ARM64 assembler-optimized build
if ! (cd "$BUILD_DIR" && make -j"$(nproc)" -f ../../cmpl_gcc_arm64.mak $MAKE_WARN_OVERRIDES); then
echo "Error: ARM64 assembler build failed." >&2
exit 1
fi
# Optional GCC non-ASM build
if [ "$RUN_GCC_NON_ASM_BUILD" = "yes" ]; then
if ! (cd "$BUILD_DIR" && make -j"$(nproc)" -f ../../cmpl_gcc.mak $MAKE_WARN_OVERRIDES); then
echo "Error: GCC non-ASM build failed." >&2
exit 1
fi
else
echo "Skipping GCC non-ASM build (RUN_GCC_NON_ASM_BUILD=$RUN_GCC_NON_ASM_BUILD)."
fi
# Optional Clang build (disabled by default due to -flto=auto incompatibility on some clang versions)
if [ "$RUN_CLANG_BUILD" = "yes" ]; then
if ! (cd "$BUILD_DIR" && make -j"$(nproc)" -f ../../cmpl_clang.mak $MAKE_WARN_OVERRIDES $MAKE_LTO_DISABLE); then
echo "Error: Clang build failed." >&2
exit 1
fi
else
echo "Skipping Clang build (RUN_CLANG_BUILD=$RUN_CLANG_BUILD)."
fi
# Install resulting binaries
echo "Installing built binaries to $INSTALL_BIN_DIR..."
BINARIES_TO_INSTALL=()
for candidate in 7zz 7z 7za 7zr; do
if [ -x "$BUILD_DIR/$candidate" ]; then
BINARIES_TO_INSTALL+=("$BUILD_DIR/$candidate")
fi
done
if [ "${#BINARIES_TO_INSTALL[@]}" -eq 0 ]; then
echo "Error: No built binaries found in $BUILD_DIR." >&2
exit 1
fi
for bin_path in "${BINARIES_TO_INSTALL[@]}"; do
if ! sudo cp -f "$bin_path" "$INSTALL_BIN_DIR/"; then
echo "Error: Failed to install $(basename "$bin_path")." >&2
exit 1
fi
done
# Prefer the freshly built binary for validation
PRIMARY_7Z_BIN=""
if [ -x "$BUILD_DIR/7zz" ]; then
PRIMARY_7Z_BIN="$BUILD_DIR/7zz"
elif [ -x "$BUILD_DIR/7z" ]; then
PRIMARY_7Z_BIN="$BUILD_DIR/7z"
elif command -v 7z >/dev/null 2>&1; then
PRIMARY_7Z_BIN="$(command -v 7z)"
fi
if [ -z "$PRIMARY_7Z_BIN" ]; then
echo "Error: No usable 7-Zip binary found for validation." >&2
exit 1
fi
# Step 7: Validate codecs, hashers, and handlers
echo "Validating codecs, hashers, and handlers..."
if ! "$PRIMARY_7Z_BIN" i >/dev/null 2>&1; then
echo "Error: 7z codec listing failed." >&2
exit 1
fi
TEST_DIR="$TEMP_DIR/testdata"
mkdir -p "$TEST_DIR"
if [ ! -d "$TEST_DIR" ]; then
echo "Error: Failed to create test directory." >&2
exit 1
fi
TEST_FILE="$TEST_DIR/sample.txt"
echo "test content" > "$TEST_FILE"
# Zstandard tests
if ! "$PRIMARY_7Z_BIN" a "$TEST_DIR/zstd_fast.7z" -m0=zstd -mx0 "$TEST_FILE" >/dev/null 2>&1; then
echo "Error: Zstandard fast mode test failed." >&2
exit 1
fi
if ! "$PRIMARY_7Z_BIN" a "$TEST_DIR/zstd_ultra.7z" -m0=zstd -mx22 "$TEST_FILE" >/dev/null 2>&1; then
echo "Error: Zstandard ultra mode test failed." >&2
exit 1
fi
# LZ4 and LZ5 tests
if ! "$PRIMARY_7Z_BIN" a "$TEST_DIR/lz4_ultra.7z" -m0=lz4 -mx12 "$TEST_FILE" >/dev/null 2>&1; then
echo "Error: LZ4 test failed." >&2
exit 1
fi
if ! "$PRIMARY_7Z_BIN" a "$TEST_DIR/lz5_ultra.7z" -m0=lz5 -mx16 "$TEST_FILE" >/dev/null 2>&1; then
echo "Error: LZ5 test failed." >&2
exit 1
fi
# Fast LZMA2 test
if ! "$PRIMARY_7Z_BIN" a "$TEST_DIR/flzma2_ultra.7z" -m0=flzma2 -mx9 "$TEST_FILE" >/dev/null 2>&1; then
echo "Error: Fast LZMA2 test failed." >&2
exit 1
fi
# Hash tests
if ! "$PRIMARY_7Z_BIN" h "$TEST_FILE" >/dev/null 2>&1; then
echo "Error: Hash validation failed." >&2
exit 1
fi
echo "Codec and hash validation completed successfully."
echo "Deployment completed. Review outputs in $INSTALL_BIN_DIR and $TEMP_DIR."
exit 0
URL: https://ib.bsb.br/7zip