#!/usr/bin/env sh set -eu repo_root="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)" cd "$repo_root" out_dir="${DOSH_PACKAGE_DIR:-target/dosh-release}" version="${DOSH_VERSION:-$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | sed -n '1p')}" normalize_os() { case "$(uname -s)" in Darwin) printf '%s\n' macos ;; Linux) printf '%s\n' linux ;; FreeBSD) printf '%s\n' freebsd ;; MINGW*|MSYS*|CYGWIN*) printf '%s\n' windows ;; *) uname -s | tr '[:upper:]' '[:lower:]' ;; esac } normalize_arch() { case "$(uname -m)" in x86_64|amd64) printf '%s\n' x86_64 ;; arm64|aarch64) printf '%s\n' aarch64 ;; armv7l) printf '%s\n' armv7 ;; *) uname -m | tr '[:upper:]' '[:lower:]' ;; esac } host_os="$(normalize_os)" host_arch="$(normalize_arch)" os="${DOSH_PACKAGE_OS:-$host_os}" arch="${DOSH_PACKAGE_ARCH:-$host_arch}" target="${DOSH_PACKAGE_TARGET:-}" if [ -z "$target" ] && [ "$os" = "windows" ]; then if [ "$host_os" != "windows" ]; then case "$arch" in x86_64) target="x86_64-pc-windows-gnu" ;; aarch64) target="aarch64-pc-windows-gnullvm" ;; *) echo "set DOSH_PACKAGE_TARGET for windows/$arch" >&2; exit 1 ;; esac elif [ "$arch" != "$host_arch" ]; then case "$arch" in x86_64) target="x86_64-pc-windows-msvc" ;; aarch64) target="aarch64-pc-windows-msvc" ;; *) echo "set DOSH_PACKAGE_TARGET for windows/$arch" >&2; exit 1 ;; esac fi fi if [ "$os" = "windows" ]; then artifact="dosh-$os-$arch.zip" versioned_artifact="dosh-$version-$os-$arch.zip" else artifact="dosh-$os-$arch.tar.gz" versioned_artifact="dosh-$version-$os-$arch.tar.gz" fi stage="$out_dir/stage/dosh" write_versioned="${DOSH_PACKAGE_VERSIONED:-0}" if [ -n "$target" ]; then release_dir="target/$target/release" else release_dir="target/release" fi need_target_linker() { case "${target:-}" in x86_64-pc-windows-gnu) if ! command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1 \ && ! command -v x86_64-w64-mingw32-clang >/dev/null 2>&1; then echo "windows/x86_64 packaging requires x86_64-w64-mingw32-gcc or x86_64-w64-mingw32-clang" >&2 exit 1 fi ;; aarch64-pc-windows-gnullvm) if ! command -v aarch64-w64-mingw32-clang >/dev/null 2>&1; then echo "windows/aarch64 packaging requires aarch64-w64-mingw32-clang" >&2 exit 1 fi ;; esac } if command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then if [ -z "${DOSH_BUILD_GIT_HASH:-}" ]; then export DOSH_BUILD_GIT_HASH="$(git rev-parse --short=12 HEAD)" fi if [ -z "${DOSH_BUILD_COMMIT_DATE:-}" ]; then export DOSH_BUILD_COMMIT_DATE="$(git show -s --format=%cs HEAD)" fi if [ -z "${DOSH_BUILD_GIT_DIRTY+x}" ]; then if git diff --quiet --ignore-submodules --; then export DOSH_BUILD_GIT_DIRTY=0 else export DOSH_BUILD_GIT_DIRTY=1 fi fi fi need_target_linker if [ "$os" = "windows" ]; then if [ -n "$target" ]; then cargo build --release --target "$target" --bin dosh-client --bin dosh-bench else cargo build --release --bin dosh-client --bin dosh-bench fi else if [ -n "$target" ]; then cargo build --release --target "$target" else cargo build --release fi fi rm -rf "$stage" mkdir -p "$stage/bin" "$out_dir" for bin in dosh-client dosh-server dosh-auth dosh-bench; do if [ -f "$release_dir/$bin" ]; then install -m 0755 "$release_dir/$bin" "$stage/bin/$bin" elif [ -f "$release_dir/$bin.exe" ]; then install -m 0755 "$release_dir/$bin.exe" "$stage/bin/$bin.exe" fi done if [ -f "$stage/bin/dosh-client.exe" ]; then cp "$stage/bin/dosh-client.exe" "$stage/bin/dosh.exe" elif [ -f "$stage/bin/dosh-client" ]; then cp "$stage/bin/dosh-client" "$stage/bin/dosh" fi strip_bin() { file="$1" [ -f "$file" ] || return 0 case "$file" in *.exe) if [ "${target:-}" = "aarch64-pc-windows-gnullvm" ] && command -v llvm-strip >/dev/null 2>&1; then llvm-strip "$file" 2>/dev/null || true elif command -v x86_64-w64-mingw32-strip >/dev/null 2>&1; then x86_64-w64-mingw32-strip "$file" 2>/dev/null || true elif command -v strip >/dev/null 2>&1; then strip "$file" 2>/dev/null || true fi ;; *) if command -v strip >/dev/null 2>&1; then strip "$file" 2>/dev/null || true fi ;; esac } powershell_word() { printf "'%s'" "$(printf '%s' "$1" | sed "s/'/''/g")" } powershell_compress_archive() { for ps in powershell.exe powershell pwsh.exe pwsh; do if command -v "$ps" >/dev/null 2>&1; then "$ps" -NoProfile -Command "Compress-Archive -Force -LiteralPath $(powershell_word "$stage") -DestinationPath $(powershell_word "$out_dir/$artifact")" return 0 fi done return 1 } for bin in "$stage"/bin/*; do strip_bin "$bin" done printf '%s\n' "$version" >"$stage/VERSION" if [ "$os" = "windows" ]; then if powershell_compress_archive; then : elif command -v zip >/dev/null 2>&1; then (cd "$out_dir/stage" && zip -qr "../$artifact" dosh) else echo "windows packaging requires PowerShell or zip" >&2 exit 1 fi else tar -C "$out_dir/stage" -czf "$out_dir/$artifact" dosh fi write_sha256() { file="$1" if command -v sha256sum >/dev/null 2>&1; then sha256sum "$file" >"$file.sha256" elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$file" >"$file.sha256" else echo "warning: sha256sum/shasum not found; skipping checksum for $file" >&2 fi } write_sha256 "$out_dir/$artifact" if [ "$write_versioned" = "1" ]; then cp "$out_dir/$artifact" "$out_dir/$versioned_artifact" write_sha256 "$out_dir/$versioned_artifact" fi printf 'Wrote:\n' cat <