Files
signer/build_musl.sh

81 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Build portable static x86_64 Linux binaries in a pinned musl container.
# The Docker build context is the parent directory because Cargo.toml uses
# ../nostr_core_lib_rust as a path dependency. Set SIGNER_NOSTR_CORE_DIR
# when the sibling checkout is stored elsewhere.
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_NAME="$(basename "${PROJECT_DIR}")"
PARENT_DIR="$(dirname "${PROJECT_DIR}")"
IMAGE_NAME="${SIGNER_MUSL_IMAGE:-signer-musl-build:rust-1.88.0-alpine3.20}"
OUTPUT_DIR="${SIGNER_MUSL_OUTPUT_DIR:-${PROJECT_DIR}/dist}"
DEPENDENCY_DIR="${SIGNER_NOSTR_CORE_DIR:-${PARENT_DIR}/nostr_core_lib_rust}"
if ! command -v docker >/dev/null 2>&1; then
echo "error: Docker is required for the musl build" >&2
exit 1
fi
if [[ ! -f "${PROJECT_DIR}/Cargo.toml" ]]; then
echo "error: Cargo.toml not found in ${PROJECT_DIR}" >&2
exit 1
fi
if [[ ! -f "${DEPENDENCY_DIR}/core/Cargo.toml" || ! -f "${DEPENDENCY_DIR}/nips/Cargo.toml" ]]; then
echo "error: invalid nostr_core_lib_rust checkout: ${DEPENDENCY_DIR}" >&2
echo "expected core/Cargo.toml and nips/Cargo.toml" >&2
echo "hint: clone the complete sibling checkout or set SIGNER_NOSTR_CORE_DIR" >&2
exit 1
fi
mkdir -p "${OUTPUT_DIR}"
rm -f "${OUTPUT_DIR}/signer" \
"${OUTPUT_DIR}/signer_client"
CONTAINER_NAME="signer-musl-build-$$-${RANDOM}"
cleanup() {
docker rm -f "${CONTAINER_NAME}" >/dev/null 2>&1 || true
}
trap cleanup EXIT
printf '[INFO] Building musl image %s\n' "${IMAGE_NAME}"
if [[ "${DEPENDENCY_DIR}" != "${PARENT_DIR}/nostr_core_lib_rust" ]]; then
echo "error: SIGNER_NOSTR_CORE_DIR must point to the sibling ../nostr_core_lib_rust directory when using Docker" >&2
exit 1
fi
docker build \
--file "${PROJECT_DIR}/Dockerfile.musl" \
--tag "${IMAGE_NAME}" \
"${PARENT_DIR}"
printf '[INFO] Extracting portable binaries\n'
docker create --name "${CONTAINER_NAME}" "${IMAGE_NAME}" >/dev/null
docker cp "${CONTAINER_NAME}:/workspace/signer/target/x86_64-unknown-linux-musl/release/signer" \
"${OUTPUT_DIR}/signer"
docker cp "${CONTAINER_NAME}:/workspace/signer/target/x86_64-unknown-linux-musl/release/signer-client" \
"${OUTPUT_DIR}/signer_client"
chmod 0755 "${OUTPUT_DIR}/signer" \
"${OUTPUT_DIR}/signer_client"
for binary in \
"${OUTPUT_DIR}/signer" \
"${OUTPUT_DIR}/signer_client"; do
file "${binary}"
if ! file "${binary}" | grep -Eq 'ELF 64-bit LSB (pie )?executable, x86-64'; then
echo "error: ${binary} is not an x86_64 ELF executable" >&2
exit 1
fi
if ldd "${binary}" 2>&1 | grep -Eqi 'not a dynamic executable|statically linked'; then
:
else
echo "error: ${binary} appears to be dynamically linked" >&2
ldd "${binary}" 2>&1 || true
exit 1
fi
"${binary}" --version
done
printf '[SUCCESS] Portable binaries written to %s\n' "${OUTPUT_DIR}"