#!/bin/sh # Build a statically-linked ogit binary using musl. # # Prerequisites: # - opam (>= 2.1) # - musl-gcc (install musl-tools on Debian/Ubuntu, sys-libs/musl on Gentoo) # - A C compiler (gcc or clang) # # This script creates a dedicated opam switch with static musl compilation, # installs dependencies, builds, strips, and copies the binary to dist/. # # Usage: # ./scripts/build-release.sh set -eu SWITCH_NAME="ogit-static" PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)" OCAML_VERSION="$( awk '/\(ocaml \(= / { version = $3 gsub(/[()]/, "", version) print version exit }' "${PROJECT_ROOT}/dune-project" )" DIST_DIR="${PROJECT_ROOT}/dist" if [ -z "${OCAML_VERSION}" ]; then echo "ERROR: Could not determine the OCaml version from dune-project." exit 1 fi # Verify musl-gcc is available if ! command -v musl-gcc >/dev/null 2>&1; then echo "ERROR: musl-gcc not found." echo " Debian/Ubuntu: apt install musl-tools" echo " Gentoo: emerge sys-libs/musl" echo " Fedora: dnf install musl-gcc" exit 1 fi # Verify opam is available if ! command -v opam >/dev/null 2>&1; then echo "ERROR: opam not found. Install from https://opam.ocaml.org/doc/Install.html" exit 1 fi echo "==> Creating opam switch '${SWITCH_NAME}' (if not exists)..." if ! opam switch list 2>/dev/null | grep -q "${SWITCH_NAME}"; then opam switch create "${SWITCH_NAME}" \ --packages="ocaml-variants.${OCAML_VERSION}+options,ocaml-option-static,ocaml-option-musl" \ --no-install fi echo "==> Installing dependencies..." opam install --switch="${SWITCH_NAME}" --deps-only --yes "${PROJECT_ROOT}" echo "==> Building..." opam exec --switch="${SWITCH_NAME}" -- dune build --root="${PROJECT_ROOT}" --force BINARY="${PROJECT_ROOT}/_build/default/bin/main.exe" if [ ! -f "${BINARY}" ]; then echo "ERROR: Build produced no binary at ${BINARY}" exit 1 fi echo "==> Stripping binary..." strip "${BINARY}" echo "==> Copying to dist/..." mkdir -p "${DIST_DIR}" cp "${BINARY}" "${DIST_DIR}/ogit" echo "" echo "Done. Binary at: ${DIST_DIR}/ogit" echo "" file "${DIST_DIR}/ogit" ls -lh "${DIST_DIR}/ogit"