Skip to content

Cross compiling OCaml Code to Windows

Christoph M. Wintersteiger edited this page Jan 29, 2018 · 7 revisions

All of this is based on the instructions and infrastructure of the ocaml-cross project.

Install system packages

sudo apt-get install gcc-mingw-w64-x86-64 libffi-dev libgmp-dev libsqlite3-dev gawk

The earliest ocaml compiler to support all of this is 4.04.0

If you have a working opam/ocaml setup, run opam switch 4.04.0, otherwise initialize opam by running opam init --comp=4.04.0. Don't forget to restart your terminal or to run eval `opam config env` afterward.

Add repositories that contain the cross-compilation (*-windows) packages

opam repository add everest-windows git://github.com/wintersteiger/opam-cross-windows#everest-win-pkgs

(This is fork of opam-cross-windows with additional packages required by Everest.)

Pin a couple packages to development repositories

These packages contain fixes that are not available in the official opam repository yet, so we need to pin them to development repositories. (Check opam-cross-windows for updates on this.)

opam pin add ocamlbuild https://github.com/ocaml/ocamlbuild.git
opam pin add topkg https://github.com/whitequark/topkg.git

Install the default (native) set of packages

At the time of writing, these are the packages that are required by Everest.

opam install -y ocamlfind depext ppx_deriving ppx_deriving_yojson zarith pprint wasm process fileutils stdint batteries sqlite3 menhir fix sedlex ctypes ulex visitors

Install the cross (non-native, mingw64) versions of all packages

The following are the same packages as the native ones with -windows added onto their names. They contain Windows native code object files/libraries; ocaml-windows is the base system.

opam install -y ocaml-windows ocamlfind-windows zarith-windows pprint-windows wasm-windows process-windows fileutils-windows stdint-windows batteries-windows sqlite3-windows menhir-windows fix-windows ctypes-windows ulex-windows yojson-windows sedlex-windows ppx_deriving-windows ppx_deriving_yojson-windows visitors-windows

Cross-compile FStar as a proof of concept

In your FStar directory, compile the snapshot with the ocamlfind toolchain set to windows:

FSTAR_OCAMLBUILD_EXTRAS="-toolchain windows" make -C src/ocaml-output

This will produce bin/fstar.exe which should be a proper Windows binary and file bin/fstar.exe should report

bin/fstar.exe: PE32+ executable (console) x86-64, for MS Windows

And, obviously, it should run and work.

Caveats and Footnotes

  • This has been tested only in WSL/Bash on Windows, which can run Windows binaries natively, thus being able to compile & run binaries required during building of packages. On a free-standing Linux, this may require some further changes to some of the package installation scripts or, in the worst case, wine.
  • zarith-windows and sqlite3-windows use static libraries (libgmp and the libsqlite3 amalgamation) to avoid problems with dynamic linking on the host system. This means that those libraries can not be upgraded by replacing DLLs later.
  • The jbuilder-windows package sometimes fails because of concurrency issues when running with -j; just retry.