Skip to content

Commit

Permalink
Install system packages required by OCaml in the ocaml stage
Browse files Browse the repository at this point in the history
Different releases of OCaml require different system libraries: libX11
isn't needed since 4.09, when the Graphics library was moved from the
core distribution to a separate package. On the opposite side, OCaml
5.1 can optionally use libzstd for compressing marshalled
data. Install these packages accordingly in the ocaml image.
  • Loading branch information
MisterDA committed Mar 6, 2023
1 parent fca399a commit be11aff
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
52 changes: 42 additions & 10 deletions src-opam/linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@ module RPM = struct
let install = ksprintf (run "yum install -y %s") in
update
@@ install
"sudo passwd bzip2 patch rsync nano gcc-c++ git tar curl xz \
libX11-devel which m4 diffutils findutils%s"
"sudo passwd bzip2 patch rsync nano gcc-c++ git tar curl xz which m4 \
diffutils findutils%s"
(match extra with None -> "" | Some x -> " " ^ x)
@@ groupinstall "\"Development Tools\""
|> crunch

let ocaml_dev_packages v =
if Ocaml_version.compare v Ocaml_version.Releases.v4_08 <= 0 then
Some "libX11-devel"
else if Ocaml_version.compare v Ocaml_version.Releases.v5_1_0 >= 0 then
Some "zstd"
else None

let add_user ?uid ?gid ?(sudo = false) username =
let uid = match uid with Some u -> sprintf "-u %d " u | None -> "" in
let gid = match gid with Some g -> sprintf "-g %d " g | None -> "" in
Expand Down Expand Up @@ -93,11 +100,16 @@ module Apt = struct
copy_heredoc
~src:[ heredoc ~strip:true "\tAcquire::Retries \"5\";" ]
~dst:"/etc/apt/apt.conf.d/mirror-retry" ()
@@ install
"build-essential curl git rsync sudo unzip nano libcap-dev \
libx11-dev%s"
@@ install "build-essential curl git rsync sudo unzip nano libcap-dev %s"
(match extra with None -> "" | Some x -> " " ^ x)

let ocaml_dev_packages v =
if Ocaml_version.compare v Ocaml_version.Releases.v4_08 <= 0 then
Some "libX11-dev"
else if Ocaml_version.compare v Ocaml_version.Releases.v5_1_0 >= 0 then
Some "libzstd-dev"
else None

let add_user ?uid ?gid ?(sudo = false) username =
let uid = match uid with Some u -> sprintf "--uid %d " u | None -> "" in
let gid = match gid with Some g -> sprintf "--gid %d " g | None -> "" in
Expand Down Expand Up @@ -134,10 +146,17 @@ module Apk = struct

let dev_packages ?extra () =
install
"build-base patch tar ca-certificates git rsync curl sudo bash \
libx11-dev nano coreutils xz ncurses-dev%s"
"build-base patch tar ca-certificates git rsync curl sudo bash nano \
coreutils xz ncurses-dev%s"
(match extra with None -> "" | Some x -> " " ^ x)

let ocaml_dev_packages v =
if Ocaml_version.compare v Ocaml_version.Releases.v4_08 <= 0 then
Some "libX11-dev"
else if Ocaml_version.compare v Ocaml_version.Releases.v5_1_0 >= 0 then
Some "zstd"
else None

let add_user ?uid ?gid ?(sudo = false) username =
let home = "/home/" ^ username in
(match gid with
Expand Down Expand Up @@ -197,12 +216,18 @@ module Zypper = struct
update
@@ install "-t pattern devel_C_C++"
@@ install
"sudo git unzip curl gcc-c++ libcap-devel xz libX11-devel bzip2 which \
rsync gzip%s"
"sudo git unzip curl gcc-c++ libcap-devel xz bzip2 which rsync gzip%s"
(match extra with None -> "" | Some x -> " " ^ x)
@@ run "zypper clean --all"
|> crunch

let ocaml_dev_packages v =
if Ocaml_version.compare v Ocaml_version.Releases.v4_08 <= 0 then
Some "libX11-devel"
else if Ocaml_version.compare v Ocaml_version.Releases.v5_1_0 >= 0 then
Some "zstd"
else None

let add_user ?uid ?gid ?(sudo = false) username =
let home = "/home/" ^ username in
run "useradd %s%s -d %s -m --user-group %s"
Expand Down Expand Up @@ -233,10 +258,17 @@ module Pacman = struct

let dev_packages ?extra () =
install
"make gcc patch tar ca-certificates git rsync curl sudo bash libx11 nano \
"make gcc patch tar ca-certificates git rsync curl sudo bash nano \
coreutils xz ncurses diffutils unzip%s"
(match extra with None -> "" | Some x -> " " ^ x)

let ocaml_dev_packages v =
if Ocaml_version.compare v Ocaml_version.Releases.v4_08 <= 0 then
Some "libx11"
else if Ocaml_version.compare v Ocaml_version.Releases.v5_1_0 >= 0 then
Some "zstd"
else None

let add_user ?uid ?gid ?(sudo = false) username =
let home = "/home/" ^ username in
run "useradd %s%s -d %s -m --user-group %s"
Expand Down
20 changes: 20 additions & 0 deletions src-opam/linux.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ module RPM : sig
(** [dev_packages ?extra ()] will install the base development tools and [sudo],
[passwd] and [git]. Extra packages may also be optionally supplied via [extra]. *)

val ocaml_dev_packages : Ocaml_version.t -> string option
(** [ocaml_dev_packages v] returns packages that are required by the
OCaml distribution at version [v]. *)

val install_system_ocaml : t
(** Install the system OCaml packages via Yum *)
end
Expand All @@ -69,6 +73,10 @@ module Apt : sig
(** [dev_packages ?extra ()] will install the base development tools and [sudo],
[passwd] and [git] and X11. Extra packages may also be optionally supplied via [extra]. *)

val ocaml_dev_packages : Ocaml_version.t -> string option
(** [ocaml_dev_packages v] returns packages that are required by the
OCaml distribution at version [v]. *)

val install_system_ocaml : t
(** Install the system OCaml packages via [apt-get] *)
end
Expand All @@ -85,6 +93,10 @@ module Apk : sig
(** [dev_packages ?extra ()] will install the base development tools and [sudo],
[passwd] and [git]. Extra packages may also be optionally supplied via [extra]. *)

val ocaml_dev_packages : Ocaml_version.t -> string option
(** [ocaml_dev_packages v] returns packages that are required by the
OCaml distribution at version [v]. *)

val add_user : ?uid:int -> ?gid:int -> ?sudo:bool -> string -> t
(** [add_user username] will install a new user with name [username] and a locked
password. If [sudo] is true then root access with no password will also be
Expand Down Expand Up @@ -117,6 +129,10 @@ module Zypper : sig
(** [dev_packages ?extra ()] will install the base development tools and [sudo],
[passwd] and [git]. Extra packages may also be optionally supplied via [extra]. *)

val ocaml_dev_packages : Ocaml_version.t -> string option
(** [ocaml_dev_packages v] returns packages that are required by the
OCaml distribution at version [v]. *)

val install_system_ocaml : t
(** Install the system OCaml packages via [zypper] *)
end
Expand All @@ -138,6 +154,10 @@ module Pacman : sig
(** [dev_packages ?extra ()] will install the base development tools and [sudo],
[passwd] and [git]. Extra packages may also be optionally supplied via [extra]. *)

val ocaml_dev_packages : Ocaml_version.t -> string option
(** [ocaml_dev_packages v] returns packages that are required by the
OCaml distribution at version [v]. *)

val install_system_ocaml : t
(** Install the system OCaml packages via [pacman] *)
end
Expand Down
21 changes: 21 additions & 0 deletions src-opam/opam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,27 @@ let gen_opam2_distro ?win10_revision ?winget ?(clone_opam_repo = true) ?arch
in
(D.tag_of_distro d, fn @@ clone @@ pers)

let ocaml_dev_packages distro v =
let open Linux in
match D.package_manager distro with
| `Apk ->
Option.fold ~none:empty ~some:(Apk.install "%s")
(Apk.ocaml_dev_packages v)
| `Apt ->
Option.fold ~none:empty ~some:(Apt.install "%s")
(Apt.ocaml_dev_packages v)
| `Yum ->
Option.fold ~none:empty ~some:(RPM.install "%s")
(RPM.ocaml_dev_packages v)
| `Zypper ->
Option.fold ~none:empty ~some:(Zypper.install "%s")
(Zypper.ocaml_dev_packages v)
| `Pacman ->
Option.fold ~none:empty ~some:(Pacman.install "%s")
(Pacman.ocaml_dev_packages v)
| `Windows -> empty
| `Cygwin -> empty

let create_switch ~arch distro t =
let create_switch switch pkg =
run "opam switch create %s %s" (OV.to_string switch) pkg
Expand Down
5 changes: 5 additions & 0 deletions src-opam/opam.mli
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ val gen_opam2_distro :
will be build in an prepended build stage. If specified, then
winget will be pulled from the [winget] external image. *)

val ocaml_dev_packages : Distro.t -> Ocaml_version.t -> Dockerfile.t
(** [ocaml_dev_packages distro version] returns packages that are
required under [distro] by the OCaml distribution at version
[version]. *)

val all_ocaml_compilers :
string -> Ocaml_version.arch -> Distro.t -> string * Dockerfile.t
(** [all_ocaml_compilers hub_id arch distro] will generate an opam2
Expand Down

0 comments on commit be11aff

Please sign in to comment.