OCaml Cross Compilation: An Experiment
a year ago
- #Zig
- #cross-compilation
- #OCaml
- OCaml cross-compilation is complex and often target-specific, with various efforts addressing different platforms like mobile, Windows, or embedded systems.
- Cross-compilation is useful for mobile/embedded systems, distributing software for other OSs, and cloud environments with different OS requirements.
- Docker simplifies cross-compilation but introduces performance overhead, which is problematic for iterative serverless development workflows.
- Three main components for OCaml cross-compilation: C cross-compiler tools, OCaml cross-compiler, and modified opam packages for the target environment.
- Zig provides a simplified C cross-compilation solution with its `zig cc` toolchain, eliminating the need for complex setup.
- An OCaml cross-compiler overlay repository for Amazon Linux 2023 (x86_64 and aarch64) uses Zig under the hood.
- Packages must be cross-compiled by rewriting opam package definitions, which is tedious and requires handling transitive dependencies.
- `packman` is a tool to automate rewriting opam packages for cross-compilation, though manual intervention is still needed for some cases.
- Future work includes refining `packman`, testing more packages, and exploring dune-based package management to avoid opam rewrites.
- Challenges remain for non-Linux targets like Windows and macOS due to assembly instructions in the OCaml compiler.