View raw

1 #!/bin/sh 2 # Build a statically-linked ogit binary using musl. 3 # 4 # Prerequisites: 5 # - opam (>= 2.1) 6 # - musl-gcc (install musl-tools on Debian/Ubuntu, sys-libs/musl on Gentoo) 7 # - A C compiler (gcc or clang) 8 # 9 # This script creates a dedicated opam switch with static musl compilation, 10 # installs dependencies, builds, strips, and copies the binary to dist/. 11 # 12 # Usage: 13 # ./scripts/build-release.sh 14 15 set -eu 16 17 SWITCH_NAME="ogit-static" 18 PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)" 19 OCAML_VERSION="$( 20 awk '/\(ocaml \(= / { 21 version = $3 22 gsub(/[()]/, "", version) 23 print version 24 exit 25 }' "${PROJECT_ROOT}/dune-project" 26 )" 27 DIST_DIR="${PROJECT_ROOT}/dist" 28 29 if [ -z "${OCAML_VERSION}" ]; then 30 echo "ERROR: Could not determine the OCaml version from dune-project." 31 exit 1 32 fi 33 34 # Verify musl-gcc is available 35 if ! command -v musl-gcc >/dev/null 2>&1; then 36 echo "ERROR: musl-gcc not found." 37 echo " Debian/Ubuntu: apt install musl-tools" 38 echo " Gentoo: emerge sys-libs/musl" 39 echo " Fedora: dnf install musl-gcc" 40 exit 1 41 fi 42 43 # Verify opam is available 44 if ! command -v opam >/dev/null 2>&1; then 45 echo "ERROR: opam not found. Install from https://opam.ocaml.org/doc/Install.html" 46 exit 1 47 fi 48 49 echo "==> Creating opam switch '${SWITCH_NAME}' (if not exists)..." 50 if ! opam switch list 2>/dev/null | grep -q "${SWITCH_NAME}"; then 51 opam switch create "${SWITCH_NAME}" \ 52 --packages="ocaml-variants.${OCAML_VERSION}+options,ocaml-option-static,ocaml-option-musl" \ 53 --no-install 54 fi 55 56 echo "==> Installing dependencies..." 57 opam install --switch="${SWITCH_NAME}" --deps-only --yes "${PROJECT_ROOT}" 58 59 echo "==> Building..." 60 opam exec --switch="${SWITCH_NAME}" -- dune build --root="${PROJECT_ROOT}" --force 61 62 BINARY="${PROJECT_ROOT}/_build/default/bin/main.exe" 63 64 if [ ! -f "${BINARY}" ]; then 65 echo "ERROR: Build produced no binary at ${BINARY}" 66 exit 1 67 fi 68 69 echo "==> Stripping binary..." 70 strip "${BINARY}" 71 72 echo "==> Copying to dist/..." 73 mkdir -p "${DIST_DIR}" 74 cp "${BINARY}" "${DIST_DIR}/ogit" 75 76 echo "" 77 echo "Done. Binary at: ${DIST_DIR}/ogit" 78 echo "" 79 file "${DIST_DIR}/ogit" 80 ls -lh "${DIST_DIR}/ogit" 81