#!/bin/sh set -eu ROOT_DIR=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) TARGET_ID=${1:?usage: sh scripts/package_release.sh } VERSION_ARG=${2:-} BIN_PATH="${ROOT_DIR}/zig-out/bin/xburner" OUT_DIR="${ROOT_DIR}/zig-out/release" OS_NAME=$(uname -s) write_launcher() { launcher_path=$1 binary_name=$2 cat > "${launcher_path}" </ && $3 ~ /^\// { print $3 } /^[[:space:]]*\/.*\(0x/ { print $1 } ' ;; *) ;; esac } should_bundle_dependency() { dep=$1 base=$(basename "${dep}") case "${OS_NAME}" in Darwin) case "${dep}" in /opt/homebrew/*|/usr/local/*) return 0 ;; *) return 1 ;; esac ;; Linux) case "${base}" in linux-vdso.so.*|ld-linux*.so.*|libc.so.*|libm.so.*|libpthread.so.*|librt.so.*|libdl.so.*|libgcc_s.so.*|libutil.so.*|libresolv.so.*) return 1 ;; *) return 0 ;; esac ;; *) return 1 ;; esac } resolve_dependency_path() { dep=$1 dep_dir=$(CDPATH= cd -- "$(dirname "${dep}")" && pwd -P) printf '%s/%s\n' "${dep_dir}" "$(basename "${dep}")" } run_install_name_tool() { stderr_file=$(mktemp "${TMPDIR:-/tmp}/xburner-install-name-tool.XXXXXX") if install_name_tool "$@" 2>"${stderr_file}"; then if [ -s "${stderr_file}" ]; then grep -Fv 'warning: changes being made to the file will invalidate the code signature in:' "${stderr_file}" >&2 || true fi rm -f "${stderr_file}" return 0 fi cat "${stderr_file}" >&2 rm -f "${stderr_file}" return 1 } bundle_runtime_libs_recursive() { artifact=$1 target_lib_dir=$2 seen_file=$3 collect_dependency_paths "${artifact}" | while IFS= read -r dep; do [ -n "${dep}" ] || continue if ! should_bundle_dependency "${dep}"; then continue fi dep_resolved=$(resolve_dependency_path "${dep}") if grep -Fx "${dep_resolved}" "${seen_file}" >/dev/null 2>&1; then continue fi dep_base=$(basename "${dep_resolved}") dest_path="${target_lib_dir}/${dep_base}" printf '%s\n' "${dep_resolved}" >> "${seen_file}" if [ ! -e "${dest_path}" ]; then cp -L "${dep_resolved}" "${dest_path}" fi bundle_runtime_libs_recursive "${dest_path}" "${target_lib_dir}" "${seen_file}" done } bundle_runtime_libs() { target_bin=$1 target_lib_dir=$2 seen_file="${target_lib_dir}/.seen" : > "${seen_file}" bundle_runtime_libs_recursive "${target_bin}" "${target_lib_dir}" "${seen_file}" rm -f "${seen_file}" } patch_macos_artifact() { artifact=$1 target_lib_dir=$2 kind=$3 collect_dependency_paths "${artifact}" | while IFS= read -r dep; do [ -n "${dep}" ] || continue if ! should_bundle_dependency "${dep}"; then continue fi dep_base=$(basename "${dep}") if [ ! -f "${target_lib_dir}/${dep_base}" ]; then continue fi if [ "${kind}" = "binary" ]; then replacement="@executable_path/lib/${dep_base}" else replacement="@loader_path/${dep_base}" fi run_install_name_tool -change "${dep}" "${replacement}" "${artifact}" done } patch_macos_loader_paths() { target_bin=$1 target_lib_dir=$2 find "${target_lib_dir}" -type f -name '*.dylib' | while IFS= read -r lib_path; do run_install_name_tool -id "@loader_path/$(basename "${lib_path}")" "${lib_path}" done patch_macos_artifact "${target_bin}" "${target_lib_dir}" binary find "${target_lib_dir}" -type f -name '*.dylib' | while IFS= read -r lib_path; do patch_macos_artifact "${lib_path}" "${target_lib_dir}" dylib done } codesign_runtime_artifacts() { target_bin=$1 target_lib_dir=$2 if ! command -v codesign >/dev/null 2>&1; then return fi find "${target_lib_dir}" -type f -name '*.dylib' | while IFS= read -r lib_path; do codesign --force --sign - "${lib_path}" >/dev/null 2>&1 || true done codesign --force --sign - "${target_bin}" >/dev/null 2>&1 || true } strip_runtime_artifacts() { target_bin=$1 target_lib_dir=$2 case "${OS_NAME}" in Darwin) strip -x "${target_bin}" >/dev/null 2>&1 || true find "${target_lib_dir}" -type f -name '*.dylib' | while IFS= read -r lib_path; do strip -x "${lib_path}" >/dev/null 2>&1 || true done ;; Linux) strip --strip-unneeded "${target_bin}" >/dev/null 2>&1 || true find "${target_lib_dir}" -type f \( -name '*.so' -o -name '*.so.*' \) | while IFS= read -r lib_path; do strip --strip-unneeded "${lib_path}" >/dev/null 2>&1 || true done ;; esac } main() { if [ ! -x "${BIN_PATH}" ]; then printf >&2 'missing %s; run `zig build -Doptimize=ReleaseFast` first\n' "${BIN_PATH}" exit 1 fi if [ -n "${VERSION_ARG}" ]; then VERSION="${VERSION_ARG}" else VERSION=$("${BIN_PATH}" --version | awk '{print $2}') fi ARCHIVE_BASENAME="xburner-${VERSION}-${TARGET_ID}" ARCHIVE_PATH="${OUT_DIR}/${ARCHIVE_BASENAME}.tar.gz" STAGE_DIR="${OUT_DIR}/${ARCHIVE_BASENAME}" LIB_DIR="${STAGE_DIR}/lib" BIN_REAL_NAME="xburner-bin" rm -rf "${STAGE_DIR}" mkdir -p "${STAGE_DIR}" "${LIB_DIR}" "${OUT_DIR}" cp "${BIN_PATH}" "${STAGE_DIR}/${BIN_REAL_NAME}" cp "${ROOT_DIR}/README.md" "${STAGE_DIR}/README.md" cp "${ROOT_DIR}/CHANGELOG.md" "${STAGE_DIR}/CHANGELOG.md" cp "${ROOT_DIR}/LICENSE" "${STAGE_DIR}/LICENSE" cp "${ROOT_DIR}/NOTICE" "${STAGE_DIR}/NOTICE" cp "${ROOT_DIR}/docs/install.md" "${STAGE_DIR}/INSTALL.md" cp -R "${ROOT_DIR}/licenses" "${STAGE_DIR}/licenses" if [ "${OS_NAME}" = "Linux" ]; then bundle_linux_grpc_shim "${STAGE_DIR}" "${LIB_DIR}" fi bundle_runtime_libs "${STAGE_DIR}/${BIN_REAL_NAME}" "${LIB_DIR}" if [ "${OS_NAME}" = "Darwin" ]; then patch_macos_loader_paths "${STAGE_DIR}/${BIN_REAL_NAME}" "${LIB_DIR}" fi strip_runtime_artifacts "${STAGE_DIR}/${BIN_REAL_NAME}" "${LIB_DIR}" if [ "${OS_NAME}" = "Darwin" ]; then codesign_runtime_artifacts "${STAGE_DIR}/${BIN_REAL_NAME}" "${LIB_DIR}" fi write_launcher "${STAGE_DIR}/xburner" "${BIN_REAL_NAME}" tar -C "${OUT_DIR}" -czf "${ARCHIVE_PATH}" "${ARCHIVE_BASENAME}" printf '%s\n' "${ARCHIVE_PATH}" } main