(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) module Log = (val Logs.src_log (Logs.Src.create "bottler")) type config_section = { name : string; needs : string; example : string list } type t = | Tool_not_found of string | Config_parse of string | Config_absent of { file : string; dir : string } | Config_unreadable of { file : string; reason : string } | Config_no_section of { file : string; sections : config_section list } | Config_no_key of { file : string; section : config_section; keys : string list; } | Scaleway_profile of string | Tar_unix of Unix.error | Tar_fatal of Tar.error | Brew_failed of { args : string; msg : string } | No_bottle of string | No_formula of string | Upload_unreachable of { url : string; status : int } | Bottle_not_served of { name : string; url : string } | Bottle_digest_mismatch of { name : string; url : string; served : string } | Unknown_revision of { version : string; revision : string } | Build_dir_held of string | Target_not_built of { name : string; path : string } | Formula_unreadable of { name : string; path : string } | Verify_unanswered of { name : string; url : string; reason : string; verified : string list; left : string list; } | Macos_too_new of { name : string; path : string; needs : int * int; target : string; } | Macos_weak_imports of { name : string; path : string; symbols : string list; switch : string; left_out : string list; target : string; } | Macos_objects_too_new of { objects : (string * string) list; target : string; } | Switch_too_new of { switch : string; needs : int * int; archives : string list; packages : string list; target : string; } | Platform_unbuildable of { platform : string; host : string; why : string } | Own_workspace of { root : string; file : string } | No_image | Worker_dirty of { worker : string; changes : string list } | Bottle_digest of { name : string; want : string; got : string } | Stage_raised of string (* Two spaces before every quoted line: the block is what a reader copies, so it is set apart from the sentences and carries its own indentation. *) let pp_example ppf lines = Fmt.(list ~sep:cut (fun ppf l -> Fmt.pf ppf " %s" l)) ppf lines let pp_keys ppf = function | [ k ] -> Fmt.pf ppf "%s key" k | keys -> Fmt.pf ppf "%a keys" Fmt.(list ~sep:(any ", ") string) keys (* Prose wraps at the margin; the quoted lines above never do, since a reader copies them. *) let pp_prose ppf s = Fmt.pf ppf "@[%a@]" Fmt.words s let pp_section file ppf s = Fmt.pf ppf "%s has no %s section.@,%a@,Add to %s:@,@,%a" file s.name pp_prose s.needs file pp_example s.example let pp_macos_too_new ppf name path (major, minor) target = Fmt.pf ppf "%s: %s needs macOS %d.%d. A macOS bottle is labelled for macOS %s, so \ brew would pour it there and on every release since, where it would not \ start. bottler builds with MACOSX_DEPLOYMENT_TARGET=%s, so an object \ linked into %s ignored it" name path major minor target target name let pp_weak_imports ppf ~name ~path ~symbols ~switch ~left_out ~target = let words = Fmt.(list ~sep:(any ", ") string) in Fmt.pf ppf "%a" pp_prose (Fmt.str "%s: %s imports %a weakly. macOS %s lacks these functions, so they \ would be NULL there and %s would crash calling them. The setenv of \ opam switch %s tells configure to leave out %a: a function outside \ that list needs adding to it (Macos.missing_functions), and one inside \ it comes from a package built before the switch carried that setenv, \ which opam reinstall --switch=%s rebuilds" name path words symbols target name switch words left_out switch) let pp_objects_too_new ppf ~objects ~target = Fmt.pf ppf "@[%a@,@,%a@]" pp_prose (Fmt.str "The release link took %d files built for a later macOS than %s, the \ macOS a macOS bottle is labelled for: a binary linked from them needs \ that macOS whatever its own minimum says. Each is a library the \ release switch builds for the target (Macos.static_packages) or \ should, or one outside it the link must not reach" (Stdlib.List.length objects) target) Fmt.(list ~sep:cut (fun ppf (path, v) -> pf ppf " %s (macOS %s)" path v)) objects let pp_switch_too_new ppf ~switch ~needs:(major, minor) ~archives ~packages ~target = let first = Stdlib.List.filteri (fun i _ -> i < 3) archives in let why = match packages with | [] -> Fmt.str "No package of the switch installed them (no .changes file under \ .opam-switch/install names them), so no reinstall rebuilds them. \ Remove them, or reinstall what put them there with \ MACOSX_DEPLOYMENT_TARGET=%s" target | ps -> Fmt.str "They stay so after %s were rebuilt with MACOSX_DEPLOYMENT_TARGET=%s \ in the switch's setenv, so their build ignores it" (String.concat ", " ps) target in Fmt.pf ppf "%a" pp_prose (Fmt.str "opam switch %s is built for macOS %d.%d: %d of its archives record it, \ such as %s, and a binary linked from them needs that macOS whatever \ its own minimum says, where a macOS bottle is labelled for macOS %s. \ %s" switch major minor (Stdlib.List.length archives) (String.concat ", " first) target why) let pp_own_workspace ppf root file = Fmt.pf ppf "%s has a dune-workspace of its own and no %s. A release builds in a \ context of its own, an opam context on the release switch under the \ release profile, and the workspace bottler would write for it would \ replace the tree's. Declare that context in %s, beside dune-workspace" root file file let pp_no_image ppf = Fmt.pf ppf "@[build.linux: static builds a Linux platform in a container of \ build.image, which homebrew.yml does not name. Pin the image by digest in \ its build section:@,\ @,\ %a@]" pp_example [ "build:"; " image: ocaml/opam:alpine-ocaml-5.4@sha256:HEX" ] let pp_worker_dirty ppf worker changes = Fmt.pf ppf "@[%s builds the commit HEAD names, and the tree has uncommitted \ changes, which the bottle's version would name and the worker would not \ build:@,\ @,\ %a@,\ @,\ Commit them, or build without --worker.@]" worker pp_example changes let pp_bottle_digest ppf name want = function | "" -> Fmt.pf ppf "the container named %s, SHA-256 %s, in its manifest and sent no such \ file" name want | got -> Fmt.pf ppf "%s arrived with SHA-256 %s where the container's manifest names %s" name got want let pp ppf = function | Tool_not_found name -> Fmt.pf ppf "%s not found. Install with: brew install %s" name name | Config_parse msg -> Fmt.pf ppf "config parse error: %s" msg | Config_absent { file; dir } -> Fmt.pf ppf "@[no %s in %s.@,%a@]" file dir pp_prose "bottler bottles the packages that file declares and has no default \ for it. Write one there, or name another with --config=FILE." | Config_unreadable { file; reason } -> Fmt.pf ppf "%s cannot be read: %s" file reason | Config_no_section { file; sections } -> Fmt.pf ppf "@[%a@]" Fmt.(list ~sep:(cut ++ cut) (pp_section file)) sections | Config_no_key { file; section; keys } -> Fmt.pf ppf "@[%s: the %s section has no %a.@,%a@,A %s section reads:@,@,%a@]" file section.name pp_keys keys pp_prose section.needs section.name pp_example section.example | Scaleway_profile msg -> Fmt.pf ppf "Scaleway profile load: %s" msg | Tar_unix err -> Fmt.pf ppf "tar: %s" (Unix.error_message err) | Tar_fatal e -> Fmt.pf ppf "tar: %a" Tar.pp_error e | Brew_failed { args; msg } -> Fmt.pf ppf "brew %s failed: %s" args msg | No_bottle name -> Fmt.pf ppf "no bottle produced for %s" name | No_formula name -> Fmt.pf ppf "formula %s not found in tap" name | Upload_unreachable { url; status } -> Fmt.pf ppf "upload reported success but %s is not publicly reachable (HTTP %d)" url status | Bottle_not_served { name; url } -> Fmt.pf ppf "%s: no bottle is served at %s, so a formula pointing there would 404 \ for every brew user. Upload the bottle before writing the formula" name url | Bottle_digest_mismatch { name; url; served } -> Fmt.pf ppf "%s: the bottle served at %s hashes to %s, not the digest this formula \ would carry. Re-upload the bottle, then release again" name url served | Unknown_revision { version; revision } -> Fmt.pf ppf "version %s names commit %s, which is not in this repository. A \ formula pins its source build to that commit, so brew would fetch a \ revision nobody can check out" version revision | Build_dir_held dir -> Fmt.pf ppf "another dune instance already holds the build directory %s. Dune \ forwards a build request to that instance, and the hand-off drops \ both the command line and the environment, so the release would be \ built with the dev profile and no PROJECT_VERSION. Stop that instance \ (dune shutdown) and release again" dir | Target_not_built { name; path } -> Fmt.pf ppf "%s: the release build reported success but produced no %s. The binary \ bottled for %s would be an earlier build, or the release would ship \ without it" name path name | Formula_unreadable { name; path } -> Fmt.pf ppf "%s: %s carries no readable desc, version and homepage, and a retired \ formula is rewritten from the ones it published. Writing it from \ anything else would name a release the tap never served for %s" name path name | Verify_unanswered { name; url; reason; verified; left } -> let names = function [] -> "none" | names -> String.concat ", " names in Fmt.pf ppf "%s: the store failed twice to serve %s (%s), so its digest is \ unchecked, no formula was written and the tap was not pushed. %d of \ %d verified: %s. %d left: %s. Every bottle is uploaded under its \ immutable key; release again once the store answers" name url reason (Stdlib.List.length verified) (Stdlib.List.length verified + Stdlib.List.length left) (names verified) (Stdlib.List.length left) (names left) | Macos_too_new { name; path; needs; target } -> pp_macos_too_new ppf name path needs target | Macos_weak_imports { name; path; symbols; switch; left_out; target } -> pp_weak_imports ppf ~name ~path ~symbols ~switch ~left_out ~target | Macos_objects_too_new { objects; target } -> pp_objects_too_new ppf ~objects ~target | Switch_too_new { switch; needs; archives; packages; target } -> pp_switch_too_new ppf ~switch ~needs ~archives ~packages ~target | Platform_unbuildable { platform; host; why } -> Fmt.pf ppf "%s bottles cannot be built on this %s machine: %s" platform host why | Own_workspace { root; file } -> pp_own_workspace ppf root file | No_image -> pp_no_image ppf | Worker_dirty { worker; changes } -> pp_worker_dirty ppf worker changes | Bottle_digest { name; want; got } -> pp_bottle_digest ppf name want got | Stage_raised exn -> Fmt.pf ppf "unexpected error: %s. Nothing after this step ran; this is a bottler \ defect to report with the line above" exn let err t = Error (Fmt.to_to_string pp t)