#!/usr/bin/env bash set -euo pipefail usage() { cat <<'EOF' usage: scripts/release.sh --version VERSION [options] Options: --build-dir DIR Clean build directory (default: build-release) --dist-dir DIR Release output directory (default: dist) --jobs N Parallel build jobs --skip-tests Skip CTest and frontend tests --no-clean Reuse the configured build directory --container Build with containers/release.Dockerfile (the default) --host Explicitly build directly on this host -h, --help Show this help The release process never starts echoiond. Runtime validation uses readelf. EOF } die() { echo "release: $*" >&2 exit 2 } script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P) source_dir=$(cd -- "$script_dir/.." && pwd -P) version= build_dir="$source_dir/build-release" dist_dir="$source_dir/dist" jobs= run_tests=1 clean_build=1 use_container=1 while (($#)); do case $1 in --version) (($# >= 2)) || die "--version requires a value" version=$2 shift 2 ;; --build-dir) (($# >= 2)) || die "--build-dir requires a value" build_dir=$2 shift 2 ;; --dist-dir) (($# >= 2)) || die "--dist-dir requires a value" dist_dir=$2 shift 2 ;; --jobs) (($# >= 2)) || die "--jobs requires a value" jobs=$2 shift 2 ;; --skip-tests) run_tests=0 shift ;; --no-clean) clean_build=0 shift ;; --container) use_container=1 shift ;; --host) use_container=0 shift ;; -h|--help) usage exit 0 ;; *) die "unknown argument: $1" ;; esac done [[ $version =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]] || die "--version must contain only letters, digits, dot, underscore, or dash" if [[ -n $jobs && ! $jobs =~ ^[1-9][0-9]*$ ]]; then die "--jobs must be a positive integer" fi build_dir=$(realpath -m -- "$build_dir") dist_dir=$(realpath -m -- "$dist_dir") container_path() { local path=$1 case $path in "$source_dir") echo /workspace ;; "$source_dir"/*) echo "/workspace/${path#"$source_dir"/}" ;; *) die "container build and dist directories must be inside $source_dir" ;; esac } if ((use_container)); then command -v docker >/dev/null || die "docker is required for --container" container_build_dir=$(container_path "$build_dir") container_dist_dir=$(container_path "$dist_dir") image="echoion-release-builder:gcc-16.1.0" docker build \ --platform linux/amd64 \ --file "$source_dir/containers/release.Dockerfile" \ --tag "$image" \ "$source_dir" container_args=( --rm --user "$(id -u):$(id -g)" --volume "$source_dir:/workspace" --workdir /workspace --env HOME=/tmp/echoion-release-home --env npm_config_cache=/tmp/echoion-npm-cache ) if [[ -n ${SOURCE_DATE_EPOCH:-} ]]; then container_args+=(--env "SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH") fi release_args=( --host --version "$version" --build-dir "$container_build_dir" --dist-dir "$container_dist_dir" ) [[ -n $jobs ]] && release_args+=(--jobs "$jobs") ((run_tests)) || release_args+=(--skip-tests) ((clean_build)) || release_args+=(--no-clean) docker run --platform linux/amd64 "${container_args[@]}" "$image" \ bash scripts/release.sh "${release_args[@]}" exit fi case "$(uname -s)/$(uname -m)" in Linux/x86_64|Linux/amd64) ;; *) die "host release builds require Linux x86_64; use the amd64 container builder" ;; esac required_commands=( cmake ninja npm python3 patchelf readelf tar zstd gzip sha256sum dpkg-query ldconfig ) for command_name in "${required_commands[@]}"; do command -v "$command_name" >/dev/null || die "required command not found: $command_name" done [[ -x $source_dir/packaging/install.sh ]] || die "installer source is missing: packaging/install.sh" [[ -x $source_dir/packaging/uninstall.sh ]] || die "uninstaller source is missing: packaging/uninstall.sh" [[ -f $source_dir/packaging/service/echoion.service.in ]] || die "service template is missing: packaging/service/echoion.service.in" if [[ -z ${SOURCE_DATE_EPOCH:-} ]]; then SOURCE_DATE_EPOCH=$(git -C "$source_dir" log -1 --format=%ct 2>/dev/null || true) [[ $SOURCE_DATE_EPOCH =~ ^[0-9]+$ ]] || SOURCE_DATE_EPOCH=0 fi export SOURCE_DATE_EPOCH safe_remove_tree() { local path=$1 case $path in "$source_dir"/build*|/tmp/*) ;; *) die "refusing to clean non-build path outside source build*/ or /tmp: $path" ;; esac rm -rf -- "$path" } if ((clean_build)); then safe_remove_tree "$build_dir" fi mkdir -p "$build_dir" configure_args=(--preset linux-release -B "$build_dir") npm --prefix "$source_dir/EchoionWeb/frontend" ci --no-audit --no-fund cmake -E rm -rf "$source_dir/EchoionWeb/frontend/web-dist" ( cd "$source_dir" cmake "${configure_args[@]}" ) build_args=(--build "$build_dir" --config Release) [[ -n $jobs ]] && build_args+=(--parallel "$jobs") cmake "${build_args[@]}" if ((run_tests)); then ctest --test-dir "$build_dir" --build-config Release --output-on-failure npm --prefix "$source_dir/EchoionWeb/frontend" test npm --prefix "$source_dir/EchoionWeb/frontend" run i18n:check fi stage="$build_dir/runtime-stage" safe_remove_tree "$stage" cmake --install "$build_dir" \ --config Release \ --component Runtime \ --prefix "$stage" mapfile -t library_directories < <( find "$build_dir" -type f \( -name '*.so' -o -name '*.so.*' -o -name '*.ts' \) \ -printf '%h\n' | LC_ALL=C sort -u ) library_directories+=("$stage/lib") cxx_compiler=$("$source_dir/scripts/configured-cxx-compiler.sh" "$build_dir") compiler_runtime_files=() for runtime_name in libstdc++.so.6 libgcc_s.so.1; do runtime_path=$( "$cxx_compiler" -print-file-name="$runtime_name" ) || die "cannot locate $runtime_name with $cxx_compiler" [[ $runtime_path == /* && -f $runtime_path ]] || die "$cxx_compiler did not return an existing absolute path for $runtime_name" compiler_runtime_files+=("$runtime_path") library_directories+=("$(dirname -- "$runtime_path")") done search_directories=$(IFS=';'; echo "${library_directories[*]}") compiler_runtimes=$(IFS=';'; echo "${compiler_runtime_files[*]}") cmake \ "-DBUNDLE_ROOT=$stage" \ "-DBUNDLE_RESOLVER_EXECUTABLE=$build_dir/bin/echoiond" \ "-DBUNDLE_RESOLVER_LIBRARIES=$build_dir/lib/Echoion.ts" \ "-DBUNDLE_SEARCH_DIRECTORIES=$search_directories" \ "-DBUNDLE_SEED_FILES=$compiler_runtimes" \ "-DPATCHELF_EXECUTABLE=$(command -v patchelf)" \ -P "$source_dir/cmake/BundleRuntimeDependencies.cmake" dependency_report="$build_dir/dependency-report.txt" python3 "$source_dir/scripts/validate-release-elf.py" \ --root "$stage" \ --max-glibc 2.35 \ --report "$dependency_report" collect_licenses() { local destination=$1 local candidate relative package_name license_name digest target package source_path soname local -a license_roots=( "$source_dir/EchoionApi" "$source_dir/EchoionCore" "$source_dir/EchoionHost" "$source_dir/EchoionStandalone" "$source_dir/EchoionWeb" "$build_dir/_deps" ) if [[ -n ${GCC_ROOT:-} && -d $GCC_ROOT/share/licenses ]]; then license_roots+=("$GCC_ROOT/share/licenses") fi if [[ -n ${BOOST_ROOT:-} && -d $BOOST_ROOT/share/licenses ]]; then license_roots+=("$BOOST_ROOT/share/licenses") fi if [[ -n ${FFMPEG_ROOT:-} && -d $FFMPEG_ROOT/share/licenses ]]; then license_roots+=("$FFMPEG_ROOT/share/licenses") fi mkdir -p "$destination" for source_path in "${license_roots[@]}"; do [[ -d $source_path ]] || continue while IFS= read -r -d '' candidate; do relative=${candidate#"$source_path"/} if [[ $relative == */* ]]; then package_name=${relative%%/*} else package_name=$(basename -- "$source_path") fi license_name=$(basename -- "$candidate") package_name=${package_name//[^A-Za-z0-9._+-]/_} license_name=${license_name//[^A-Za-z0-9._+-]/_} digest=$(sha256sum "$candidate" | cut -c1-12) target="$destination/${package_name}--${license_name}--${digest}" install -m 0644 -- "$candidate" "$target" done < <( find "$source_path" -maxdepth 5 -type f \ \( -iname 'LICENSE' -o -iname 'LICENSE.txt' -o \ -iname 'LICENSE.md' -o -iname 'LICENSE.rst' -o \ -iname 'LICENSE-*' -o -iname 'LICENSE_*' -o \ -iname 'COPYING' -o -iname 'COPYING.*' -o \ -iname 'NOTICE' -o -iname 'NOTICE.*' -o \ -iname 'COPYRIGHT' -o -iname 'COPYRIGHT.*' \) \ -not -path '*/node_modules/*' -not -path '*/.git/*' \ -print0 | LC_ALL=C sort -z ) done if command -v dpkg-query >/dev/null && command -v ldconfig >/dev/null; then while IFS= read -r soname; do [[ -n $soname ]] || continue source_path=$(ldconfig -p 2>/dev/null | awk -v name="$soname" ' $1 == name && !path { path = $NF } END { if (path) print path } ') [[ -n $source_path ]] || continue package=$(dpkg-query -S "$(readlink -f -- "$source_path")" 2>/dev/null | sed -n '1s/: .*//p') || package= [[ -n $package ]] || continue package=${package%%:*} candidate="/usr/share/doc/${package}/copyright" [[ -f $candidate ]] || continue install -m 0644 -- "$candidate" "$destination/${package}--copyright" done < <( find "$stage/lib" -maxdepth 1 \( -type f -o -type l \) \ -printf '%f\n' | LC_ALL=C sort -u ) fi } collect_licenses "$stage/share/echoion/licenses" python3 "$source_dir/scripts/collect-frontend-licenses.py" \ --node-modules "$source_dir/EchoionWeb/frontend/node_modules" \ --output "$stage/share/echoion/licenses/frontend" python3 "$source_dir/scripts/generate-release-manifest.py" \ --root "$stage" \ --version "$version" \ --output "$stage/manifest.json" # Installer helpers are mutable prefix-level files and intentionally are not # included in manifest.files. They are added only after the immutable manifest. install -m 0755 "$source_dir/packaging/install.sh" "$stage/install.sh" install -m 0755 "$source_dir/packaging/uninstall.sh" "$stage/uninstall.sh" mkdir -p "$dist_dir" artifact_base="echoion-${version}-linux-x86_64" archive="$dist_dir/$artifact_base.tar.zst" installer="$dist_dir/$artifact_base.run" external_manifest="$dist_dir/$artifact_base.manifest.json" external_report="$dist_dir/$artifact_base.dependencies.txt" rm -f -- \ "$archive" "$archive.tmp" \ "$installer" "$installer.tmp" \ "$external_manifest" "$external_report" \ "$dist_dir/SHA256SUMS" bash "$source_dir/scripts/package-release-payload.sh" \ "$stage" "$artifact_base" "$SOURCE_DATE_EPOCH" \ "$archive" "$installer" "$build_dir" install -m 0644 "$stage/manifest.json" "$external_manifest" install -m 0644 "$dependency_report" "$external_report" ( cd "$dist_dir" sha256sum \ "$(basename "$archive")" \ "$(basename "$installer")" \ "$(basename "$external_manifest")" \ "$(basename "$external_report")" >SHA256SUMS ) echo "Release created without starting Echoion:" echo " $archive" echo " $installer" echo " $external_manifest" echo " $external_report" echo " $dist_dir/SHA256SUMS"