Skip to content

Commit 2e00e36

Browse files
Sudha247jonludlam
authored andcommitted
Add a doc entry for setting up an OxCaml project with dune pkg (ocaml#12780)
Closes ocaml#11652 Adds a tutorial for setting up an OxCaml project with Dune package management. I contemplated where this should go, and thought this might be a good place, after all. It shows (1) how to setup an OxCaml project, (2) servers as a live example of the previous two tutorials. ocaml#12111 is a more comprehensive tutorial about parametric libraries and OxCaml, but it doesn't specifically cover the topic of Dune package management with OxCaml. I recently ported an OxCaml project to build with Dune package management and the configuration was not as straightforward as I expected, and thought a tutorial on this topic might be useful. This takes some material from https://github.com/gridbugs/hello-oxcaml (thanks @gridbugs!). --------- Signed-off-by: Sudha Parimala <sudharg247@gmail.com>
1 parent 9dc6567 commit 2e00e36

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

doc/tutorials/dune-package-management/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ dependencies
2525
pinning
2626
repos
2727
locking
28+
oxcaml
2829
:::
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Setting up an OxCaml project
2+
3+
As an example of how to add custom repositories and pin dependencies in projects
4+
using Dune Package Management, let's look at setting up a basic
5+
[OxCaml](https://oxcaml.org/) project. OxCaml is a compiler branch with a fast
6+
moving set of extensions to OCaml. Some tweaks are required to set up a project
7+
that compiles with OxCaml when using Dune package management.
8+
9+
## Setting up the `dune-workspace`
10+
11+
OxCaml has a custom `opam-repository` that provides compatible dependencies.
12+
Dune can use packages from this repositories by configuring it in the
13+
dune-workspace file using the {doc}`repositories stanza
14+
</reference/dune-workspace/repository>` and {doc}`lock_dir stanza
15+
</reference/dune-workspace/lock_dir>`.
16+
17+
```{code-block} dune
18+
(lang dune 3.21)
19+
20+
(pkg enabled)
21+
22+
(repository
23+
(name oxcaml)
24+
(url git+https://github.com/oxcaml/opam-repository))
25+
26+
(lock_dir
27+
(repositories overlay oxcaml upstream))
28+
```
29+
30+
We would like dune to pick the OxCaml compiler rather than the latest upstream
31+
one. This can be achieved by manually adding a constraint on the compiler that
32+
is picked in the {doc}`package stanza </reference/packages>` in
33+
the dune-project.
34+
35+
```{code-block} dune
36+
(lang dune 3.21)
37+
38+
(package
39+
(name hello-oxcaml)
40+
(depends
41+
(ocaml-variants
42+
(= 5.2.0+ox))))
43+
```
44+
45+
Let's add a test program that uses OxCaml syntax.
46+
47+
::::{dropdown} `main.ml`
48+
:icon: file-code
49+
50+
:::{literalinclude} oxcaml/main.ml
51+
:language: dune
52+
:::
53+
54+
::::
55+
56+
And to build it, we add a `dune` file.
57+
58+
```{code-block} dune
59+
(executable
60+
(public_name main))
61+
```
62+
63+
## Building the project
64+
65+
Let's compile and execute the project. Note that when you run `dune pkg lock`
66+
or `dune build` the very first time, Dune pulls in and builds all the
67+
dependencies before building the project itself. It is expected to take a
68+
considerable amount of time.
69+
70+
Note: With autolocking enabled one doesn't need to explicitly run `dune pkg
71+
lock` to generate a lock directory. However, a known issue,
72+
[dune#12851](https://github.com/ocaml/dune/issues/12851) currently prevents us
73+
from using autolocking.
74+
75+
<!-- TODO: Update this after 12851 is fixed. -->
76+
77+
```
78+
$ dune pkg lock
79+
Solution for dune.lock
80+
81+
Dependencies common to all supported platforms:
82+
- conf-autoconf.0.2
83+
- conf-which.1
84+
- ocaml.5.2.0
85+
- ocaml-config.3
86+
- ocaml-variants.5.2.0+ox
87+
88+
$ dune exec ./main.exe
89+
...
90+
43
91+
```
92+
We have successfully built the project with the OxCaml compiler and run the executable.
93+
94+
## Developer tools
95+
96+
Note that OxCaml provides its own forks of the developer tools
97+
`ocaml-lsp-server`, `ocamlformat`, etc. Make sure to include constraints for the
98+
tools in the `dune-workspace` if you intend to use them for your development
99+
workflows.
100+
101+
Please see {doc}`/howto/customize-dev-tools-lock-directories` for details about
102+
configuring your workspace for developer tools. Please be aware that there are
103+
known issues in the current Dune release that may prevent this functionality
104+
from working reliably.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let () =
2+
(* The `local_` keyword requires OxCaml *)
3+
let local_ i = 42 in
4+
let j = i + 1 in
5+
Printf.printf "%d\n" j

0 commit comments

Comments
 (0)