| 1 | This file documents what a Rocq developer needs to know about the |
| 2 | Dune-based build system. |
| 3 | |
| 4 | About Dune |
| 5 | ========== |
| 6 | |
| 7 | Rocq uses the [Dune](https://github.com/ocaml/dune) build system. |
| 8 | |
| 9 | ## Quick Start |
| 10 | |
| 11 | Usually, using the latest version of Dune is recommended, see the |
| 12 | first line of the `dune-project` file for the minimum required |
| 13 | version. |
| 14 | |
| 15 | It is strongly recommended that you use the helper targets available |
| 16 | in `Makefile`, `make` will display help. Note that dune will call |
| 17 | configure for you if needed, so no need to call `./configure` in the |
| 18 | regular development workflow, unless you want to tweak options. |
| 19 | |
| 20 | |
| 21 | 2 common operations are: |
| 22 | |
| 23 | - `make check` : build all ml targets as fast as possible |
| 24 | - `make world` : build a complete Rocq distribution |
| 25 | |
| 26 | For more targeted builds, you can also call `dune` directly. First, |
| 27 | call `make dunestrap` to generate necessary build files (the `make` |
| 28 | targets above do it automatically). Then you can use: |
| 29 | |
| 30 | - `dune exec -- rocq <args...>`: |
| 31 | build and launch rocq (does not build the worker and Prelude.vo). |
| 32 | with arguments of your choice |
| 33 | - `dune build $target`: where `$target` can refer to the build |
| 34 | directory or the source directory [but will be placed under |
| 35 | `_build`] |
| 36 | |
| 37 | You need to run `make dunestrap` again if the dependencies between the |
| 38 | core library .v files have changed. |
| 39 | |
| 40 | `dune build @install` will build all the public Rocq artifacts; `dune |
| 41 | build` builds the `@default` alias, defined in the top level `dune` file. |
| 42 | |
| 43 | Dune puts build artifacts in a separate directory `_build/$context`; |
| 44 | usual `context` is `default`; dune also produces an "install" layout |
| 45 | under `_build/install/$context/`. Depending on whether you want refer |
| 46 | to the source layout or to the install layout, you may refer to |
| 47 | targets in one or the other directory. It will also generate an |
| 48 | `.install` file so files can be properly installed by package |
| 49 | managers. |
| 50 | |
| 51 | Dune doesn't allow leftovers of object files it may generate in-tree |
| 52 | [as to avoid conflicts], so please be sure your tree is clean from |
| 53 | objects files generated by the make-based system or from manual |
| 54 | compilation. |
| 55 | |
| 56 | Contrary to other systems, Dune doesn't use a global `Makefile` but |
| 57 | local build files named `dune` which are later composed to form a |
| 58 | global build, for example `plugins/ltac/dune` or `kernel/dune`. |
| 59 | |
| 60 | As a developer, Dune should take care of all OCaml-related build tasks |
| 61 | including library management, `merlin` setup, linking order, |
| 62 | etc... You should not have to modify `dune` files in regular workflow |
| 63 | unless you are adding a new binary, library, or plugin, or want to |
| 64 | tweak some low-level option. |
| 65 | |
| 66 | ## The bootstrap process / rule generation |
| 67 | |
| 68 | Dune is able to build all the OCaml parts of Rocq in a pretty standard |
| 69 | way, using its built-in rule generation for OCaml. Public tools |
| 70 | written in OCaml are distributed in the `rocq-runtime` package. |
| 71 | |
| 72 | The set of public `.v` files present in this repository, usually |
| 73 | referred as the "Rocq prelude" are distributed in the |
| 74 | `rocq-core` package. As of June 2022, Dune has a set of built-in |
| 75 | rules for `.v` files which is capable of building Rocq's core |
| 76 | library. |
| 77 | |
| 78 | However, in order to have a bit more control, we generate ourselves a |
| 79 | set of custom rules using the `tools/dune_rule_gen` binary, which are |
| 80 | then stored in the `theories/dune` file. This allows us to have a |
| 81 | finer control over the build rules without having to bump the Dune |
| 82 | version. The generation of the `theories/dune` and |
| 83 | `user-contrib/*/dune` files is known as "bootstrap". |
| 84 | |
| 85 | The rule generation code in `tools/dune_rule_gen` is mostly derived |
| 86 | from Dune's built-in rules, and it works in an straightforward way: it |
| 87 | will scan a directory with `.v` files in it, and output the |
| 88 | corresponding build rule. The script will look at some configuration |
| 89 | values such as whether native is enabled or not and adapt rule |
| 90 | generation accordingly. |
| 91 | |
| 92 | In the case of native, the script supports two modes, `coqc |
| 93 | -native-compiler on` and `coqnative`. The default is the first, as |
| 94 | currently `coqnative` incurs a 33% build time overhead on a powerful |
| 95 | 16-core machine. |
| 96 | |
| 97 | There are several modes for the rule generation script to work, |
| 98 | depending on the parameter passed. As of today, it support `-async`. |
| 99 | |
| 100 | `-async` will pass `-async-proofs on` to `coqc`. |
| 101 | |
| 102 | ## Per-User Custom Settings |
| 103 | |
| 104 | Dune will read the file `~/.config/dune/config`; see `man |
| 105 | dune-config`. Among others, you can set in this file the custom number |
| 106 | of build threads `(jobs N)` and display options `(display _mode_)`. |
| 107 | |
| 108 | ## Running binaries (rocq compile, rocq top) |
| 109 | |
| 110 | You should generally run `make world` before running rocq so that the |
| 111 | corelib is built and placed in `_build/install`. |
| 112 | |
| 113 | If building the corelib produces an error outside the prelude, `make |
| 114 | world` will still result in a usable state (of course you won't be |
| 115 | able to Require any failed files). |
| 116 | |
| 117 | For errors inside the prelude, invoke rocq with |
| 118 | `-boot -noinit -R _build/default/theories/Corelib Corelib`. |
| 119 | For instance if you get an error in Datatypes, |
| 120 | ~~~bash |
| 121 | dune exec -- rocq compile -boot -noinit -R _build/default/theories/Corelib Corelib _build/default/theories/Corelib/Init/Datatypes.v |
| 122 | ~~~ |
| 123 | should reproduce it. |
| 124 | |
| 125 | ~~~bash |
| 126 | dune exec -- rocq compile -boot -noinit -R _build/default/theories/Corelib Corelib -R theories/Corelib Corelib theories/Corelib/Init/Datatypes.v |
| 127 | ~~~ |
| 128 | should be equivalent. |
| 129 | |
| 130 | Giving `-boot -R _build/default/theories/Corelib Corelib` to |
| 131 | an IDE while visiting a file in `theories/Corelib/Init` should combine with |
| 132 | `theories/Corelib/Init/_CoqProject` to produce the above result, eg |
| 133 | ~~~bash |
| 134 | dune exec -- rocqide -boot -R _build/default/theories/Corelib Corelib theories/Corelib/Init/Datatypes.v |
| 135 | ~~~ |
| 136 | should work to reproduce the error interactively. |
| 137 | |
| 138 | ## Building custom toplevels |
| 139 | |
| 140 | You can build custom toplevels by tweaking the `toplevel/dune` files, |
| 141 | for example, to add plugins to be linked statically using the |
| 142 | `(libraries ...)` field. |
| 143 | |
| 144 | Note that Rocq relies on a hidden Dune hack, which will add `-linkall` |
| 145 | to binaries if they depend on the `findlib.dynload` library. As of |
| 146 | today, `rocq-runtime.vernac` uses `findlib.dynload`, so if your toplevel |
| 147 | hooks at the `rocq-runtime.vernac` or above level, you should be OK, |
| 148 | otherwise add `-linkall` to Dune's `(link_flags ...)` field for your |
| 149 | binary. |
| 150 | |
| 151 | ## Targets |
| 152 | |
| 153 | The default dune target is `dune build` (or `dune build @install`), |
| 154 | which will scan all sources in the Rocq tree and then build the whole |
| 155 | project, creating an "install" overlay in `_build/install/default`. |
| 156 | |
| 157 | You can build some other target by doing `dune build $TARGET`, where |
| 158 | `$TARGET` can be a `.cmxa`, a binary, a file that Dune considers a |
| 159 | target, an alias, etc... |
| 160 | |
| 161 | In order to build a single package, you can do `dune build |
| 162 | $PACKAGE.install`. |
| 163 | |
| 164 | A very useful target is `dune build @check`, that will compile all the |
| 165 | ml files in quick mode. |
| 166 | |
| 167 | Dune also provides targets for documentation, testing, and release |
| 168 | builds, please see below. |
| 169 | |
| 170 | ## Testing and documentation targets |
| 171 | |
| 172 | There are two ways to run the test suite using Dune: |
| 173 | |
| 174 | - After building Rocq with `make world`, you can run the test-suite |
| 175 | in place, generating output files in the source tree |
| 176 | by running `make -C test-suite` from the top directory of the source tree |
| 177 | (equivalent to running `make test-suite` from the `test-suite` directory). |
| 178 | This permits incremental usage since output files will be preserved. |
| 179 | |
| 180 | - You can also run the test suite in a hygienic way using `make |
| 181 | test-suite` or `dune runtest`. This is convenient for full runs from |
| 182 | scratch, for instance in CI. |
| 183 | |
| 184 | Since `dune` still invokes the test-suite makefile, the |
| 185 | environment variable `NJOBS` is used to set the `-j` option |
| 186 | that is passed to make (for example, with the command |
| 187 | `NJOBS=8 dune runtest`). This use of `NJOBS` will be |
| 188 | removed when the test suite is fully ported to Dune. |
| 189 | |
| 190 | There is preliminary support to build the API documentation and |
| 191 | reference manual in HTML format, use `dune build {@doc,@refman-html}` |
| 192 | to generate them. |
| 193 | |
| 194 | So far these targets will build the documentation artifacts, however |
| 195 | no install rules are generated yet. |
| 196 | |
| 197 | ## Developer shell |
| 198 | |
| 199 | You can create a developer shell with `dune utop $library`, where |
| 200 | `$library` can be any directory in the current workspace. For example, |
| 201 | `dune utop engine` or `dune utop plugins/ltac` will launch `utop` with |
| 202 | the right libraries already loaded. |
| 203 | |
| 204 | ## ocamldebug |
| 205 | |
| 206 | You can use [ocamldebug](https://ocaml.org/learn/tutorials/debug.html#The-OCaml-debugger) with Dune; after a build, do: |
| 207 | |
| 208 | ``` |
| 209 | dune exec -- dev/dune-dbg coqc foo.v |
| 210 | (ocd) source db |
| 211 | ``` |
| 212 | |
| 213 | to start `coqc.byte foo.v`, other targets are `{checker,rocqide,coqtop}`: |
| 214 | |
| 215 | ``` |
| 216 | dune exec -- dev/dune-dbg checker foo.vo |
| 217 | (ocd) source db |
| 218 | ``` |
| 219 | |
| 220 | More info in the [wiki](https://github.com/rocq-prover/rocq/wiki/OCamldebug). |
| 221 | |
| 222 | Unfortunately, dependency handling is not fully refined / automated, |
| 223 | you may find the occasional hiccup due to libraries being renamed, |
| 224 | etc... Please report any issue. |
| 225 | |
| 226 | For running in emacs, use `coqdev-ocamldebug` from `coqdev.el`. |
| 227 | |
| 228 | ### Debugging hints |
| 229 | |
| 230 | - To debug a failure/error/anomaly, add a breakpoint in |
| 231 | `Vernacinterp.interp_gen` (in `vernac/vernacinterp.ml`) at the with |
| 232 | clause of the "try ... with ..." block, then go "back" a few steps |
| 233 | to find where the failure/error/anomaly has been raised |
| 234 | |
| 235 | - Alternatively, for an error or an anomaly, add breakpoints where it |
| 236 | was raised (eg in `user_err` or `anomaly` in `lib/cErrors.ml`, or |
| 237 | the functions in `pretyping/pretype_errors.ml`, or other raises |
| 238 | depending on the error) |
| 239 | |
| 240 | - If there is a linking error (eg from "source db"), do a "dune |
| 241 | build rocq-runtime.install" and try again. |
| 242 | |
| 243 | ## Dropping from coqtop: |
| 244 | |
| 245 | The following should work: |
| 246 | ``` |
| 247 | dune exec -- rocq repl-with-drop |
| 248 | > Drop. |
| 249 | ``` |
| 250 | |
| 251 | ## Compositionality, developer and release modes. |
| 252 | |
| 253 | By default [in "developer mode"], Dune will compose all the packages |
| 254 | present in the tree and perform a global build. That means that for |
| 255 | example you could drop the `ltac2` folder under `plugins` and get a |
| 256 | build using `ltac2`, that will use the current Rocq version. |
| 257 | |
| 258 | This is very useful to develop plugins and Rocq libraries as your |
| 259 | plugin will correctly track dependencies and rebuild incrementally as |
| 260 | needed. |
| 261 | |
| 262 | However, it is not always desirable to go this way. For example, the |
| 263 | current Rocq source tree contains two packages [Rocq and RocqIDE], and in |
| 264 | the OPAM RocqIDE package we don't want to build RocqIDE against the |
| 265 | local copy of Rocq. For this purpose, Dune supports the `-p` option, so |
| 266 | `dune build -p rocqide` will build RocqIDE against the system-installed |
| 267 | version of Rocq libs, and use a "release" profile that for example |
| 268 | enables stronger compiler optimizations. |
| 269 | |
| 270 | ## OPAM file generation |
| 271 | |
| 272 | `.opam` files will be automatically generated by Dune from the package |
| 273 | descriptions in the `dune-project` file; see Dune's manual for more |
| 274 | details. For now we have disabled this due to some bugs. |
| 275 | |
| 276 | ## Stanzas |
| 277 | |
| 278 | `dune` files contain the so-called "stanzas", that may declare: |
| 279 | |
| 280 | - libraries, |
| 281 | - executables, |
| 282 | - documentation, arbitrary blobs. |
| 283 | |
| 284 | The concrete options for each stanza can be seen in the Dune manual, |
| 285 | but usually the default setup will work well with the current Rocq |
| 286 | sources. Note that declaring a library or an executable won't make it |
| 287 | installed by default, for that, you need to provide a "public name". |
| 288 | |
| 289 | ## Workspaces and Profiles |
| 290 | |
| 291 | Dune provides support for tree workspaces so the developer can set |
| 292 | global options --- such as flags --- on all packages, or build Rocq |
| 293 | with different OPAM switches simultaneously [for example to test |
| 294 | compatibility]; for more information, please refer to the Dune manual. |
| 295 | |
| 296 | ## Inlining reports |
| 297 | |
| 298 | The `ireport` profile will produce standard OCaml [inlining |
| 299 | reports](https://caml.inria.fr/pub/docs/manual-ocaml/flambda.html#sec488). These |
| 300 | are to be found under `_build/default/$lib/$lib.objs/$module.$round.inlining.org` |
| 301 | and are in Emacs `org-mode` format. |
| 302 | |
| 303 | Note that due to https://github.com/ocaml/dune/issues/1401 , we must |
| 304 | perform a full rebuild each time as otherwise Dune will remove the |
| 305 | files. We hope to solve this in the future. |
| 306 | |
| 307 | ## Planned and Advanced features |
| 308 | |
| 309 | Dune supports or will support extra functionality that may result very |
| 310 | useful to Rocq, some examples are: |
| 311 | |
| 312 | - Cross-compilation. |
| 313 | - Automatic Generation of OPAM files. |
| 314 | - Multi-directory libraries. |
| 315 | |
| 316 | ## FAQ |
| 317 | |
| 318 | - I get "Error: Dynlink error: Interface mismatch": |
| 319 | |
| 320 | You are likely running a partial build which doesn't include |
| 321 | implicitly loaded plugins / vo files. See the "Running binaries |
| 322 | [coqtop / rocqide]" section above as to how to correctly call Rocq's |
| 323 | binaries. |
| 324 | |
| 325 | ## Dune cheat sheet |
| 326 | |
| 327 | - `dune build` build all targets in the current workspace |
| 328 | - `dune build @check` build all ML targets as fast as possible, setup merlin |
| 329 | - `dune utop $dir` open a shell for libraries in `$dir` |
| 330 | - `dune exec -- $file` build and execute binary `$file`, can be in path or be an specific name |
| 331 | - `dune build _build/$context/$foo` build target `$foo$` in `$context`, with build dir layout |
| 332 | - `dune build _build/install/$context/foo` build target `$foo$` in `$context`, with install dir layout |
| 333 | |
| 334 | ### packaging: |
| 335 | |
| 336 | - `dune subst` generate metadata for a package to be installed / distributed, necessary for opam |
| 337 | - `dune build -p $pkg` build a package in release mode |