Skip to content

Use Dune instead of OCamlbuild to build unikernels #969

@TheLortex

Description

@TheLortex

I used several unikernel samples to explore what are the problems blocking the Dune transition. I've taken them from mirage-skeleton. After identifying problems and workarounds, I managed to build entirely with Dune + ld/pkg-config the following unikernels with unix, xen and hvt target: DNS, http_fetch, echo_server. I'm aware this does not cover everything but it's a good start.

Used features of OCamlbuild that aren't implemented in Dune:

  1. -dontlink <pkg> used for unix, threads, str and num. This removes these packages from the final linking step, thus allowing to link on non-Unix targets.
  2. Predicates is sometimes used for library selection.
  3. Use of -output-obj in conjunction with ..._linkopts in META files to link C stubs and custom OCaml runtime.

Workaround to each problem

  1. Explore the dependency graph to figure out which packages have unneeded dependencies and update them accordingly. Use OCaml 4.07 and Stdlib.Bigarray in order not to depend on the legacy bigarray which includes undefined stubs on xen/freestanding.
  2. Use virtual libraries and implementations. To not break everything, it's possible to keep the library name package as the default implementation and make it depend on the virtual library package-virtual. This way the update is transparent for users of the package and the ones that want to use the virtual library feature just have to change their package dependency into a package-virtual dependency.
  3. Dune uses -output-complete-obj to build object files. This also links C stubs and runtime. Therefore we don't have to care about C stubs anywore as they are automatically linked along with used libraries. No need for ..._linkopts flags in META. For the custom runtime, ocamlopt has a -runtime-variant option to choose a custom runtime library to link.

Packages to update

  1. ocplib-endian, mirage-fs-lwt, mirage-console-lwt, xen-evtchn, sexplib, tcpip, asn1-combinators, mirage-entropy
  2. mirage-entropy, zarith + dependencies (asn1-combinators, nocrypto)
  3. mirage-xen-ocaml, ocaml-freestanding: just need to add a symlink from lib...asmrun.a to ocaml runtime directory so that ocaml finds the runtime variants. I've already modified these packages locally to make my experiments so it's not that hard to make these changes.

Issues and comments

  1. Duniverse forks may have already fixed some of these packages.
  2. The uses of virtual libraries disable cross-module optimisation such as inlining. We have to be careful with that.

How do you feel about this ? How should we proceed to update packages ? Am I missing something ? Feel free to comment !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions