You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/reference/dune/env.rst
+18-1Lines changed: 18 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,24 @@ Fields supported in ``<settings>`` are:
49
49
or not where ``<mode>`` is either ``no``, ``file`` (to generate sourcemap in a ``.map`` file next the the generated javascript file) or ``inline`` (to inline the sourcemap at the end of the generated JavaScript file).
50
50
51
51
- ``(js_of_ocaml (runtest_alias <alias-name>))`` specifies the alias under which
52
-
:ref:`inline_tests` and tests (:ref:`tests-stanza`) run for the `js` mode.
52
+
:ref:`inline_tests` and tests (:ref:`tests-stanza`) run for the ``js`` mode.
53
+
54
+
- ``(js_of_ocaml (enabled_if <blang expression>))`` specifies whether the ``js`` mode is enabled. It is enabled by default.
specifies ``wasm_of_ocaml`` flags. See :ref:`wasmoo-field` for more details.
58
+
59
+
- ``(wasm_of_ocaml (compilation_mode <mode>))`` controls whether to use separate
60
+
compilation or not where ``<mode>`` is either ``whole_program`` or
61
+
``separate``.
62
+
63
+
- ``(wasm_of_ocaml (sourcemap <mode>))`` controls whether to generate sourcemap
64
+
or not where ``<mode>`` is either ``no``, ``file`` (to generate sourcemap in a ``.map`` file next the the generated javascript file) or ``inline`` (to inline the sourcemap at the end of the generated JavaScript file).
65
+
66
+
- ``(wasm_of_ocaml (runtest_alias <alias-name>))`` specifies the alias under which
67
+
:ref:`inline_tests` and tests (:ref:`tests-stanza`) run for the ``wasm`` mode.
68
+
69
+
- ``(wasm_of_ocaml (enabled_if <blang expression>))`` specifies whether the ``wasm`` mode is enabled. It is enabled by default.
53
70
54
71
- ``(binaries <binaries>)``, where ``<binaries>`` is a list of entries of the
55
72
form ``(<filepath> as <name>)``. ``(<filepath> as <name>)`` makes the binary
Copy file name to clipboardExpand all lines: doc/reference/dune/executable.rst
+36-4Lines changed: 36 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,15 @@ executable stanzas is as follows:
14
14
There can be additional modules in the current directory; you only need to
15
15
specify the entry point. Given an ``executable`` stanza with ``(name <name>)``,
16
16
Dune will know how to build ``<name>.exe``. If requested, it will also know how
17
-
to build ``<name>.bc`` and ``<name>.bc.js`` (Dune 2.0 and up also need specific
18
-
configuration (see the ``modes`` optional field below)).
17
+
to build ``<name>.bc``, ``<name>.bc.js`` and ``<name>.bc.wasm.js`` (Dune 2.0
18
+
and up also needs specific configuration (see the ``modes`` optional field
19
+
below)).
19
20
20
21
``<name>.exe`` is a native code executable, ``<name>.bc`` is a bytecode
21
-
executable which requires ``ocamlrun`` to run, and ``<name>.bc.js`` is a
22
-
JavaScript generated using ``js_of_ocaml``.
22
+
executable which requires ``ocamlrun`` to run, ``<name>.bc.js`` is a
23
+
JavaScript generated using ``js_of_ocaml``, and ``<name>.bc.wasm.js`` is a
24
+
Wasm loader script generated using ``wasm_of_ocaml`` (the Wasm modules are included in
25
+
directory ``<name>.bc.wasm.assets``).
23
26
24
27
Please note: in case native compilation is not available, ``<name>.exe`` will be
25
28
a custom bytecode executable, in the sense of ``ocamlc -custom``. This means
@@ -91,6 +94,8 @@ files for executables. See
91
94
92
95
- ``js_of_ocaml``: See the section about :ref:`jsoo-field`
93
96
97
+
- ``wasm_of_ocaml``: See the section about :ref:`wasmoo-field`
98
+
94
99
- ``flags``, ``ocamlc_flags``, and ``ocamlopt_flags``: See
95
100
:doc:`/concepts/ocaml-flags`.
96
101
@@ -165,6 +170,7 @@ available.
165
170
non-OCaml application.
166
171
- ``js`` for producing JavaScript from bytecode executables, see
167
172
:doc:`/reference/dune-project/explicit_js_mode`.
173
+
- ``wasm`` for producing JavaScript from bytecode executables.
168
174
- ``plugin`` for producing a plugin (``.cmxs`` if native or ``.cma`` if
169
175
bytecode).
170
176
@@ -186,6 +192,7 @@ Additionally, you can use the following shorthands:
186
192
- ``byte`` for ``(byte exe)``
187
193
- ``native`` for ``(native exe)``
188
194
- ``js`` for ``(byte js)``
195
+
- ``wasm`` for ``(byte wasm)``
189
196
- ``plugin`` for ``(best plugin)``
190
197
191
198
For instance, the following ``modes`` fields are all equivalent:
@@ -216,6 +223,7 @@ The extensions for the various linking modes are chosen as follows:
216
223
.. (native/best shared_object) %{ext_dll}
217
224
.. c .bc.c
218
225
.. js .bc.js
226
+
.. wasm .bc.wasm.js
219
227
.. (best plugin) %{ext_plugin}
220
228
.. (byte plugin) .cma
221
229
.. (native plugin) .cmxs
@@ -264,14 +272,38 @@ options using ``(js_of_ocaml (<js_of_ocaml-options>))``.
264
272
- ``(sourcemap <config>)`` where ``<config>>`` is one of ``no``, ``file`` or ``inline``.
265
273
This is only available inside ``executable`` stanzas.
266
274
275
+
- ``(enabled_if <blang expression>)`` to specify whether the ``js`` mode is enabled. It is enabled by default.
276
+
This is only available inside ``executable`` stanzas.
277
+
267
278
``<flags>`` is specified in the :doc:`/reference/ordered-set-language`.
279
+
``<blang expression>`` is specified using the :doc:`/reference/boolean-language`,
268
280
269
281
The default values for ``flags``, ``compilation_mode`` and ``sourcemap`` depend on the selected build profile. The
270
282
build profile ``dev`` (the default) will enable inline sourcemap, separate compilation and pretty
271
283
JavaScript output.
272
284
273
285
See :ref:`jsoo` for more information.
274
286
287
+
.. _wasmoo-field:
288
+
289
+
wasm_of_ocaml
290
+
~~~~~~~~~~~~~
291
+
292
+
In ``library`` and ``executable`` stanzas, you can specify ``wasm_of_ocaml``
293
+
options using ``(wasm_of_ocaml (<wasm_of_ocaml-options>))``.
294
+
295
+
``<wasm_of_ocaml-options>`` are all optional. They are the same as the ``<js_of_ocaml-options>`` above plus:
296
+
297
+
- ``(wasm_files (<files-list>))`` to specify ``wasm_of_ocaml``
298
+
Wasm runtime files.
299
+
300
+
For the ``(sourcemap <config>)`` option, ``<config>`` must be one of ``no`` or ``inline``. Source maps are put within the ``.bc.wasm.assets`` directory.
301
+
302
+
The default values for ``flags``, ``compilation_mode`` and ``sourcemap`` depend on the selected build profile. The
303
+
build profile ``dev`` (the default) will enable sourcemaps, separate compilation and pretty Wasm output.
Wasm_of_ocaml_ is a compiler from OCaml to WebAssembly (Wasm for
12
+
short). The compiler works by translating OCaml bytecode to Wasm code.
13
+
The compiler can currently be installed from [its Github repository](https://github.com/ocaml-wasm/wasm_of_ocaml).
14
+
15
+
Compiling to Wasm
16
+
=================
17
+
18
+
Dune has full support for building wasm_of_ocaml libraries and executables transparently.
19
+
There's no need to customise or enable anything to compile OCaml
20
+
libraries/executables to Wasm.
21
+
22
+
To build a Wasm executable, just define an executable as you would normally.
23
+
Consider this example:
24
+
25
+
.. code:: console
26
+
27
+
$ echo 'print_endline "hello from wasm"' > foo.ml
28
+
29
+
With the following ``dune`` file:
30
+
31
+
.. code:: dune
32
+
33
+
(executable (name foo) (modes wasm))
34
+
35
+
And then request the ``.wasm.js`` target:
36
+
37
+
.. code:: console
38
+
39
+
$ dune build ./foo.bc.wasm.js
40
+
$ node _build/default/foo.bc.wasm.js
41
+
hello from wasm
42
+
43
+
If you're using the js_of_ocaml syntax extension, you must remember to add the
44
+
appropriate PPX in the ``preprocess`` field:
45
+
46
+
.. code:: dune
47
+
48
+
(executable
49
+
(name foo)
50
+
(modes wasm)
51
+
(preprocess (pps js_of_ocaml-ppx)))
52
+
53
+
Selective Compilation
54
+
=====================
55
+
56
+
The ``js`` and ``wasm`` modes can be selectively disabled using the ``(js_of_ocaml (enabled_if ...))`` and ``(wasm_of_ocaml (enabled_if ...))`` options. This allows for instance to generate one, or the other dependings on a profile:
57
+
58
+
.. code:: dune
59
+
60
+
(env
61
+
(js-only
62
+
(wasm_of_ocaml
63
+
(enabled_if false)))
64
+
(wasm-only
65
+
(js_of_ocaml
66
+
(enabled_if false))))
67
+
68
+
To be able to invoke the generated code using the same JavaScript script name in all cases, you can add a rule to copy the Wasm launcher script when the js_of_ocaml compilation is disabled.
69
+
70
+
.. code:: dune
71
+
72
+
(rule
73
+
(action
74
+
(copy foo.bc.wasm.js foo.bc.js))
75
+
(enabled_if
76
+
(= %{profile} wasm-only)))
77
+
78
+
Separate Compilation
79
+
====================
80
+
81
+
Dune supports two modes of compilation:
82
+
83
+
- Direct compilation of a bytecode program to Wasm. This mode allows
84
+
wasm_of_ocaml to perform whole-program deadcode elimination and whole-program
85
+
inlining.
86
+
87
+
- Separate compilation, where compilation units are compiled to Wasm
88
+
separately and then linked together. This mode is useful during development as
89
+
it builds more quickly.
90
+
91
+
The separate compilation mode will be selected when the build profile
92
+
is ``dev``, which is the default. It can also be explicitly specified
93
+
in an ``env`` stanza (see :doc:`/reference/dune/env`) or per executable
94
+
inside ``(wasm_of_ocaml (compilation_mode ...))`` (see :doc:`/reference/dune/executable`)
95
+
96
+
Sourcemap
97
+
=========
98
+
99
+
Wasm_of_ocaml can generate sourcemaps for the generated Wasm code.
100
+
By default, they are generated when using the ``dev`` build profile and are not generated otherwise.
101
+
The behavior can explicitly be specified in an ``env`` stanza (see :doc:`/reference/dune/env`)
102
+
or per executable inside ``(wasm_of_ocaml (sourcemap ...))`` (see :doc:`/reference/dune/executable`)
0 commit comments