Something went wrong. Try again.
Homebrew bottle builder and tap manager for OCaml monorepos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
let docker_platform : Homebrew.Platform.t -> string option = function | X86_64_linux -> Some "linux/amd64" | Arm64_linux -> Some "linux/arm64" | Arm64_sonoma | Sonoma | Unknown _ -> None
let volume ~image ~switch platform = let safe c = match c with | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | '.' | '-' -> c | _ -> '-' in Fmt.str "bottler-opam-%s-%s-%s" (String.sub (Image.digest image) 0 12) (Homebrew.Platform.to_string platform) (String.map safe switch)
(* zstd: the compiler links its runtime's compression with [-lzstd]. *)let static_deps = "gmp-dev gmp-static libffi-dev zlib-dev zlib-static zstd-dev zstd-static \ musl-dev linux-headers m4 pkgconf"
(* The opam root is the volume's own, made by the opam the script runs: the image's root has the layout of its 2.1, which a later opam upgrades and then stops on. The container is the isolation opam's sandbox would add. *)(* opam init refuses a directory that exists and is no root, as an empty volume is, so the root is a directory of the volume. *)let volume_dir = "/opam"let opam_root = volume_dir ^ "/root"let q = Filename.quotelet src = "/home/opam/src"
(* Copy the tree in, write the workspace bottler gave it, and make its switch from its root opam files, as the host does. *)let prepare ~(context : Context.t) ~written ~exclude = let excludes = Stdlib.List.map (fun d -> "--exclude=" ^ q ("./" ^ d)) exclude |> String.concat " " in let written = match written with | None -> [] | Some content -> let path = Fpath.to_string context.workspace in [ Fmt.str "mkdir -p %s" (q (Filename.dirname path)); Fmt.str "printf '%%s' %s > %s" (q content) (q path); ] in [ "set -eu"; (* The archive alone goes to the command's output; the build's lines go to its error. *) "exec 3>&1 1>&2"; "export OPAMYES=1"; Fmt.str "if command -v apk >/dev/null; then apk add --no-cache %s; fi" static_deps; (* An ocaml/opam image's [opam] is 2.1, which cannot read a lock's [with-dev-setup]; it ships each later release as [opam-2.N], and the newest of them is the image's to decide, by its digest. *) "if ls /usr/bin/opam-2.* >/dev/null 2>&1; then ln -sf \"$(ls \ /usr/bin/opam-2.* | sort -t. -k2 -n | tail -1)\" /usr/bin/opam; fi"; "opam --version"; Fmt.str "[ -f %s/config ] || opam init --bare --no-setup --disable-sandboxing \ https://opam.ocaml.org" opam_root; Fmt.str "rm -rf %s && mkdir -p %s" src src; Fmt.str "tar -C /src %s -cf - . | tar -C %s -xf -" excludes src; "cd " ^ src; ] @ written @ [ "opam repository set-url default https://opam.ocaml.org"; "opam update --quiet"; ] @ Switch.script ~root:"./" ~switch:context.switch ~env:[] ?compiler:context.compiler ~packages:[] ()
let build_line ~(context : Context.t) ~version ~checks ~binaries = let targets = Stdlib.List.map snd binaries in String.concat " " (Stdlib.List.map q ([ "env"; "PROJECT_VERSION=" ^ version; "opam"; "exec"; "--switch=" ^ context.switch; "--"; ] @ Context.build context ~checks targets))
let script ~context ~written ~version ~checks ~exclude ~binaries ~primaries = let dir = Fpath.to_string (Context.dir context) in let strip (name, target) = Fmt.str "strip -o %s %s" (q ("/out/" ^ name)) (q (dir ^ "/" ^ target)) in let report name = Fmt.str "env -u PROJECT_VERSION %s --version > %s" (q ("/out/" ^ name)) (q ("/out/" ^ name ^ ".version")) in let files = Stdlib.List.map q (Returned.files ~binaries ~primaries) in String.concat "\n" (prepare ~context ~written ~exclude @ [ build_line ~context ~version ~checks ~binaries; "mkdir -p /out" ] @ Stdlib.List.map strip binaries @ Stdlib.List.map report primaries @ [ Fmt.str "cd /out && sha256sum %s > %s" (String.concat " " files) Returned.manifest; (* The CPU the container spent, the shell's and its children's, for the log. *) "times"; Fmt.str "tar -C /out -cf - %s %s >&3" Returned.manifest (String.concat " " files); ]) ^ "\n"
let cmd ~image ~platform ~volume ~src script = Bos.Cmd.( v "docker" % "run" % "--rm" % "--init" % "--platform" % platform % "--user" % "0:0" % "-e" % ("OPAMROOT=" ^ opam_root) % "-e" % "OPAMROOTISOK=1" % "-v" % (Fpath.to_string src ^ ":/src:ro") % "-v" % (volume ^ ":" ^ volume_dir) % "--entrypoint" % "sh" % Image.to_string image % "-c" % script)
let msg_error r = Result.map_error (fun (`Msg e) -> e) r
let build ~fs (config : Config.t) ~platform ~version ~runner ~run packages = let ( let* ) = Result.bind in match (docker_platform platform, config.build.image) with | None, _ -> Errors.err (Platform_unbuildable { platform = Homebrew.Platform.to_string platform; host = "container"; why = "no Linux container builds it"; }) | Some _, None -> Errors.err No_image | Some docker, Some image -> let* root = msg_error (Bos.OS.Dir.current ()) in let out = Fpath.( root // v config.build_dir / ("linux-" ^ Homebrew.Platform.to_string platform)) in let* _ = msg_error (Bos.OS.Path.delete ~recurse:true out) in let* _ = msg_error (Bos.OS.Dir.create out) in let* context = Context.resolve ~root ~scratch:Fpath.(root // v config.build_dir) ~switch:config.build.switch ?compiler:config.build.compiler ~static:true ~lto:config.build.lto () in let* context, written = Context.relative ~root context in let binaries = Stdlib.List.concat_map Config.binaries packages in let primaries = Stdlib.List.map (fun (p : Config.package) -> p.name) packages in let exclude = [ "_build"; "_opam"; ".git"; config.build_dir ] in let script = script ~context ~written ~version ~checks:config.build.checks ~exclude ~binaries ~primaries in let container = cmd ~image ~platform:docker ~volume:(volume ~image ~switch:context.switch platform) ~src:(Runner.src runner ~root) script in let* () = run ~out: (Returned.receive ~fs ~out ~names:(Returned.files ~binaries ~primaries)) (Runner.cmd runner container) in Ok out