# Dune, cross compilation, and Raspberry Pi

**URL:** <https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722>\
**Category:** Ecosystem\
**Tags:** dune\
**Created:** [April 27, 2019, 12:29pm UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722 "2019-04-27T12:29:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yaron\_Minsky](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yaron_minsky/32/11_2.png) [@Yaron\_Minsky](https://discuss.ocaml.org/u/Yaron_Minsky)\
**Post date:** [April 27, 2019, 12:29pm UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722/1 "2019-04-27T12:29:11Z")

</div>

I’m thinking of doing some home projects for doing stuff like driving LEDs from a Raspberry Pi using OCaml. Is there a good way of doing this with a cross compiler via OCaml? Or do I need to install OCaml and build my executables on the Pi? I know that there was some work to get cross compilation working properly with Dune, but I don’t know how to do it in practice.

y

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [April 28, 2019, 9:21pm UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722/2 "2019-04-28T21:21:50Z")

</div>

Not exactly your question, but you may be interested in

> **[dbuenzli/rpi-boot-ocaml](https://github.com/dbuenzli/rpi-boot-ocaml)**
>
> Raspberry Pi boot support for the OCaml system (unreleased) - dbuenzli/rpi-boot-ocaml

Cheers

---

<div class="post-metadata">

**Author:** ![jeremiedimino](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jeremiedimino/32/699_2.png) [@jeremiedimino](https://discuss.ocaml.org/u/jeremiedimino)\
**Post date:** [April 29, 2019, 7:04am UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722/3 "2019-04-29T07:04:58Z")

</div>

Currently, the cross compilation story with dune is as follow: once you have setup one of the cross-compiling environments available on [https://github.com/ocaml-cross](https://github.com/ocaml-cross), you can cross-compile with dune as follow:

```auto
$ dune build -x windows,android,ios ...

```

This will create `_build/default.windows`, `_build/default.android` and so on.

The longest step is building/installing the cross-compiler. You don’t need a special dune to cross compile.

---

<div class="post-metadata">

**Author:** ![avsm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/avsm/32/6_2.png) [@avsm](https://discuss.ocaml.org/u/avsm)\
**Post date:** [April 29, 2019, 2:11pm UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722/4 "2019-04-29T14:11:56Z")

</div>

If you’re working on a Mac/Win, then a cheap and cheerful way to get an rPI-compatible environment is to use Docker for Mac/Win, which transparently do CPU emulation via `qemu-user-static` and binfmt.

```auto
$ docker run -it ocaml/opam2-staging:debian-9-ocaml-4.08-linux-arm32v7
opam@fdd0cc1ae238:~/opam-repository$ uname -a
Linux fdd0cc1ae238 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 armv7l GNU/Linux
opam@fdd0cc1ae238:~/opam-repository$ ocamlc -config|grep arch
architecture: arm
opam@fdd0cc1ae238:~/opam-repository$ opam install -y dune
<...etc>

```

You can save the binary results of the build by adding a `-v <yourrepo>:/home/opam/src` to mount a local volume within the ARM container. This will be a pretty slow option, but probably faster than compiling within the rPi3.

---

<div class="post-metadata">

**Author:** ![Moe](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@Moe](https://discuss.ocaml.org/u/Moe)\
**Post date:** [December 17, 2023, 8:14pm UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722/5 "2023-12-17T20:14:20Z")

</div>

I know this thread is over 4 years old by now, but I was wondering if this is still the best way to cross compile for a different architectures? My goal is to compile from a M1 or a linux (x64) box to a raspberry pi 4, which I have lying around.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [December 17, 2023, 9:34pm UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722/6 "2023-12-17T21:34:52Z")

</div>

@anmonteiro seems to have a working cross compilation toolchain using Nix: [Cross-compiling implementations / how they work - #2 by anmonteiro](https://discuss.ocaml.org/t/cross-compiling-implementations-how-they-work/8686/2)

Not sure to what extent it works for Raspberry Pi.

---

<div class="post-metadata">

**Author:** ![pitag](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/pitag/32/3489_2.png) [@pitag](https://discuss.ocaml.org/u/pitag)\
**Post date:** [December 18, 2023, 5:56pm UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722/7 "2023-12-18T17:56:40Z")

</div>

Since you seem to be interested in cross-compiling specifically to experiment on a RPi, let me come back to the context of the `dbuenzli/rpi-boot-ocaml` pointer above: Nowadays, you could cross-compile to a bare metal RPi, using @dinosaure’s toolchain [gilbraltar](https://github.com/dinosaure/gilbraltar). I admit that bare metal is a bit limited and it’s quite nice to have drivers around 🙂 but also, it’s surprising how much can be done from scratch! E.g. @Lortex and others power LED strips bare metal over the UART and play music bare metal over the jack port in [well-typed-light-bulbs/rpi](https://github.com/well-typed-lightbulbs/rpi/tree/master).

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [December 18, 2023, 6:25pm UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722/8 "2023-12-18T18:25:09Z")

</div>

If you have an `M1` you can perhaps simply compile to aarch64 in a docker or podman container.

---

<div class="post-metadata">

**Author:** ![XVilka](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xvilka/32/5334_2.png) [@XVilka](https://discuss.ocaml.org/u/XVilka)\
**Post date:** [December 20, 2023, 10:30am UTC](https://discuss.ocaml.org/t/dune-cross-compilation-and-raspberry-pi/3722/9 "2023-12-20T10:30:20Z")

</div>

In case you want to target newer RPi, there is WIP patch series for QEMU to add Raspberry Pi 4B model: [[PATCH v4 00/45] Raspberry Pi 4B machine](https://lore.kernel.org/qemu-devel/1af36710e4ca4aefaced4ceadbdc8b4c@auriga.com/T/#m74a5ff689598bacea427a059da03c809caa27f80)

There is a high chance it will be ready/merged for QEMU 9.0, but it should be usable as is once applied; patches look solid in general.
