-
Notifications
You must be signed in to change notification settings - Fork 369
(docs) add pages describing all of OCaml's compilation targets #3378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
b0c71e6
c2a5d44
5bf3184
6a2a85a
4b08231
ee26da8
d2173de
91e2351
caa8033
05c4b31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| --- | ||
| id: "native-target" | ||
| short_title: "Native" | ||
| title: "Compilation Targets: Native" | ||
| description: "Compile OCaml to high-performance native code with ocamlopt. Maximum runtime performance with optimized machine code for production deployments." | ||
| category: "Compilation Targets" | ||
| --- | ||
|
|
||
| OCaml can compile to native code, delivering high-performance executables with optimized machine code for production environments. | ||
sabine marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## What is OCaml Native Code? | ||
|
|
||
| OCaml native code is generated by **ocamlopt**, the native-code compiler that produces standalone executables with integrated runtime and garbage collector. Key characteristics: | ||
sabine marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - Faster runtime performance than bytecode | ||
| - Stand-alone executables requiring no external runtime | ||
sabine marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - Cross-module inlining and optimization | ||
sabine marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - Production-ready deployments | ||
|
|
||
| ## When to Use Native Code | ||
|
|
||
| **Use native code** (ocamlopt) when you need faster runtime performance in production, want standalone executables, or are deploying to end users. | ||
|
|
||
| **Use bytecode** (ocamlc) for fast iteration during development, CI/testing environments where compilation speed matters, or maximum portability. | ||
|
|
||
| The typical OCaml workflow: develop with bytecode for fast compile times, switch to native code for production releases. The same source code compiles to both targets without modification. | ||
|
|
||
| ## Platform Support | ||
|
|
||
| The native-code compiler is available only on 64-bit systems (OCaml 5.0+): | ||
|
|
||
| - **x86-64 (AMD64)** - Linux, macOS, Windows | ||
| - **ARM64 (AArch64)** - Linux, macOS (including Apple Silicon) | ||
| - **RISC-V** - Linux | ||
| - **IBM Z (s390x)** - Linux (OCaml 5.1+) | ||
| - **PowerPC (PPC64)** - Linux | ||
|
|
||
| All native code platforms use **63-bit integers** (with 1 bit reserved for the garbage collector tag). | ||
|
|
||
| **Windows:** Native compilation is supported via MSVC, MinGW, or Cygwin toolchains. See [OCaml on Windows](https://ocaml.org/docs/ocaml-on-windows) for setup details. | ||
|
|
||
| If your target platform lacks native code support, the bytecode compiler provides a highly portable fallback. | ||
|
|
||
| ## Learn More | ||
|
|
||
| - [Dune Manual: Executable Linking Modes](https://dune.readthedocs.io/en/stable/reference/dune/executable.html#linking-modes) - How to specify native, bytecode, or other compilation modes in Dune | ||
| - [OCaml Manual: Native-code Compilation](https://ocaml.org/manual/latest/native.html) - Complete ocamlopt reference | ||
| - [Using the OCaml Compiler Toolchain](https://ocaml.org/docs/using-the-ocaml-compiler-toolchain) - Practical compilation guide | ||
| - [The Compiler Backend: Bytecode and Native code](https://ocaml.org/docs/compiler-backend) - Deep dive into compilation internals | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||
| --- | ||||||
| id: "bytecode-target" | ||||||
| short_title: "Bytecode" | ||||||
| title: "Compilation Targets: Bytecode" | ||||||
| description: "Compile OCaml to portable bytecode with ocamlc. Fast compilation, excellent portability, and easy debugging for OCaml development and production." | ||||||
| category: "Compilation Targets" | ||||||
| --- | ||||||
|
|
||||||
| OCaml can compile to bytecode, providing fast compilation, excellent portability, and predictable execution across different platforms. | ||||||
|
|
||||||
| ## What is OCaml Bytecode? | ||||||
|
|
||||||
| OCaml bytecode is a portable intermediate representation of OCaml programs that is executed by the OCaml bytecode interpreter. The bytecode system consists of: | ||||||
|
|
||||||
| - **ocamlc** - The bytecode compiler that compiles OCaml source files to bytecode | ||||||
| - **ocamlrun** - The bytecode interpreter that executes bytecode programs | ||||||
| - **Runtime system** - Includes the bytecode interpreter, garbage collector, and C primitive operations | ||||||
|
||||||
| - **Runtime system** - Includes the bytecode interpreter, garbage collector, and C primitive operations | |
| - **Runtime system** - Includes the bytecode interpreter, garbage collector, and primitive C operations |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| - Predictable and consistent execution behavior | |
| - Predictable and consistent execution behaviour |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||||
| --- | ||||||
| id: "javascript-target" | ||||||
| short_title: "JavaScript" | ||||||
| title: "Compilation Targets: JavaScript" | ||||||
| description: "Compile OCaml to JavaScript with Js_of_ocaml and Melange. Build type-safe web applications that run in browsers and Node.js with high performance." | ||||||
| category: "Compilation Targets" | ||||||
| --- | ||||||
|
|
||||||
| OCaml can compile to JavaScript, enabling you to write type-safe, high-performance code that runs in web browsers and Node.js environments. | ||||||
|
|
||||||
| ## Available Tools | ||||||
|
|
||||||
| ### Js_of_ocaml | ||||||
|
|
||||||
| [Js_of_ocaml](https://ocsigen.org/js_of_ocaml/) compiles OCaml bytecode to JavaScript. It provides: | ||||||
|
|
||||||
| - Excellent performance with compact output | ||||||
| - Strong integration with existing JavaScript libraries | ||||||
| - Access to browser APIs and DOM manipulation | ||||||
| - Support for the full OCaml language, including OCaml 5 effects | ||||||
| - Compatibility with most OCaml libraries and the standard library | ||||||
| - **32-bit integers** (JavaScript's bit operations are 32-bit) | ||||||
|
|
||||||
| **Note:** While Js_of_ocaml supports OCaml 5 effects, it has limited support for Domains (multicore parallelism). | ||||||
|
|
||||||
| Js_of_ocaml is ideal when you want to leverage existing OCaml code in web applications or need access to the complete OCaml ecosystem. | ||||||
|
|
||||||
| **Getting started:** Visit the [Js_of_ocaml documentation](https://ocsigen.org/js_of_ocaml/latest/manual/overview) to learn how to install and set up Js_of_ocaml, compile your first OCaml program to JavaScript, interact with JavaScript APIs and the DOM, and integrate with web applications. | ||||||
|
|
||||||
| ### Melange | ||||||
|
|
||||||
| [Melange](https://melange.re) compiles OCaml and Reason to JavaScript with a focus on JavaScript ecosystem integration. It provides: | ||||||
|
|
||||||
| - Idiomatic JavaScript output designed for readability | ||||||
| - Deep integration with NPM packages and JavaScript tooling | ||||||
| - Excellent TypeScript interoperability | ||||||
| - Optimized bundle sizes for modern web applications | ||||||
|
||||||
| - Optimized bundle sizes for modern web applications | |
| - Optimised bundle sizes for modern web applications |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||
| --- | ||||||
| id: "wasm-target" | ||||||
| short_title: "WebAssembly" | ||||||
| title: "Compilation Targets: WebAssembly" | ||||||
| description: "Compile OCaml to WebAssembly with wasm_of_ocaml and Wasocaml. Build high-performance applications for browsers, servers, and edge computing with Wasm." | ||||||
| category: "Compilation Targets" | ||||||
| --- | ||||||
|
|
||||||
| OCaml can compile to WebAssembly (WASM), enabling you to run high-performance OCaml code in web browsers, on the server, and in embedded environments with near-native speed. | ||||||
|
|
||||||
| ## What is WebAssembly? | ||||||
|
|
||||||
| WebAssembly is a binary instruction format designed as a portable compilation target for programming languages. It provides: | ||||||
| - Near-native performance in web browsers and standalone runtimes | ||||||
| - Sandboxed execution environment for security | ||||||
| - Broad platform support across browsers, servers, and edge computing | ||||||
| - Compact binary format for fast loading and parsing | ||||||
|
|
||||||
| ## Available Tools | ||||||
|
|
||||||
| ### wasm_of_ocaml | ||||||
|
|
||||||
| [wasm_of_ocaml](https://github.com/ocsigen/js_of_ocaml/blob/master/README_wasm_of_ocaml.md) compiles OCaml bytecode to WebAssembly. It provides: | ||||||
| - Full OCaml language support, including the standard library and OCaml 5 effects | ||||||
| - Compatibility with existing OCaml libraries | ||||||
| - Integration with JavaScript through Js_of_ocaml-style bindings | ||||||
| - Ability to run OCaml code in browsers and WASM runtimes | ||||||
| - Shared infrastructure with Js_of_ocaml for web development | ||||||
| - **31-bit integers** (similar to traditional OCaml bytecode on 32-bit systems) | ||||||
|
|
||||||
| **Note:** While wasm_of_ocaml supports OCaml 5 effects, it has limited support for Domains (multicore parallelism). | ||||||
|
|
||||||
| wasm_of_ocaml is ideal when you want to leverage existing OCaml code with WebAssembly's performance characteristics while maintaining compatibility with the OCaml ecosystem. | ||||||
|
|
||||||
| Visit the [wasm_of_ocaml documentation](https://github.com/ocsigen/js_of_ocaml/blob/master/README_wasm_of_ocaml.md) in the Js_of_ocaml repository to learn how to install and set up wasm_of_ocaml, compile OCaml programs to WebAssembly, interact with JavaScript APIs from WASM, and deploy WASM modules in web applications. | ||||||
|
|
||||||
| ### Wasocaml (Experimental) | ||||||
|
|
||||||
| [Wasocaml](https://github.com/OCamlPro/wasocaml) is an experimental OCaml compiler developed by OCamlPro that targets WebAssembly GC (WASM-GC). As a research project exploring direct compilation to WASM-GC, it provides: | ||||||
| - Direct compilation from OCaml's Flambda intermediate representation to WASM-GC | ||||||
| - Native WebAssembly garbage collection support | ||||||
| - Optimized performance through the Flambda optimizer | ||||||
|
||||||
| - Optimized performance through the Flambda optimizer | |
| - Optimised performance through the Flambda optimiser |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| **Important limitations:** Wasocaml is based on OCaml 4.14 and does not currently support OCaml 5 effects or multicore features. As an experimental project, it should be considered for research and experimentation rather than production use. | |
| **Important limitations:** Wasocaml is based on OCaml 4.14 and does not currently support OCaml 5 effects or multicore features. As an experimental project, it should be considered for research and experimentation rather than for use in production. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| - [ocaml-wasm Organization](https://github.com/ocaml-wasm) - Coordination hub for OCaml WebAssembly efforts | |
| - [ocaml-wasm Organisation](https://github.com/ocaml-wasm) - Coordination hub for OCaml WebAssembly efforts |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||
| --- | ||||||
| id: "unikernel-target" | ||||||
| short_title: "Unikernels" | ||||||
| title: "Compilation Targets: Unikernels" | ||||||
| description: "Compile OCaml to specialized unikernel targets using MirageOS. Create minimal, fast-booting applications for hvt, virtio, Xen, and more with reduced attack surfaces." | ||||||
|
||||||
| description: "Compile OCaml to specialized unikernel targets using MirageOS. Create minimal, fast-booting applications for hvt, virtio, Xen, and more with reduced attack surfaces." | |
| description: "Compile OCaml to specialised unikernel targets using MirageOS. Create minimal, fast-booting applications for hvt, virtio, Xen, and more with reduced attack surfaces." |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| OCaml can compile to specialized unikernel targets through [MirageOS](https://mirage.io), a library operating system that creates secure, single-purpose applications. | |
| OCaml can compile to specialised unikernel targets through [MirageOS](https://mirage.io), a library operating system that creates secure, single-purpose applications. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Unikernels are specialized, single-purpose virtual machine images that bundle your application with only the minimal OS functionality it needs. Unlike traditional applications that run on general-purpose operating systems, unikernels: | |
| Unikernels are specialised, single-purpose virtual machine images that bundle your application with only the minimal OS functionality it needs. Unlike traditional applications that run on general-purpose operating systems, unikernels: |
Uh oh!
There was an error while loading. Please reload this page.