1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
**********
Quickstart
**********
This document gives simple usage examples of dune. You can also look at
`examples <https://github.com/ocaml/dune/tree/master/example>`__ for complete
examples of projects using dune.
Building a hello world program
==============================
In a directory of your choice, write this ``dune`` file:
.. code:: scheme
;; This declares the hello_world executable implemented by hello_world.ml
(executable
(name hello_world))
This ``hello_world.ml`` file:
.. code:: ocaml
print_endline "Hello, world!"
And build it with:
.. code:: bash
dune build hello_world.exe
The executable will be built as ``_build/default/hello_world.exe``. Note that
native code executables will have the ``.exe`` extension on all platforms
(including non-Windows systems). The executable can be built and run in a single
step with ``dune exec ./hello_world.exe``.
Building a hello world program using Lwt
========================================
In a directory of your choice, write this ``dune`` file:
.. code:: scheme
(executable
(name hello_world)
(libraries lwt.unix))
This ``hello_world.ml`` file:
.. code:: ocaml
Lwt_main.run (Lwt_io.printf "Hello, world!\n")
And build it with:
.. code:: bash
dune build hello_world.exe
The executable will be built as ``_build/default/hello_world.exe``
Building a hello world program using Core and Jane Street PPXs
==============================================================
Write this ``dune`` file:
.. code:: scheme
(executable
(name hello_world)
(libraries core)
(preprocess (pps ppx_jane)))
This ``hello_world.ml`` file:
.. code:: ocaml
open Core
let () =
Sexp.to_string_hum [%sexp ([3;4;5] : int list)]
|> print_endline
And build it with:
.. code:: bash
dune build hello_world.exe
The executable will be built as ``_build/default/hello_world.exe``
Defining a library using Lwt and ocaml-re
=========================================
Write this ``dune`` file:
.. code:: scheme
(library
(name mylib)
(public_name mylib)
(libraries re lwt))
The library will be composed of all the modules in the same directory.
Outside of the library, module ``Foo`` will be accessible as
``Mylib.Foo``, unless you write an explicit ``mylib.ml`` file.
You can then use this library in any other directory by adding ``mylib``
to the ``(libraries ...)`` field.
Building a hello world program in byte-code
============================================
In a directory of your choice, write this ``dune`` file:
.. code:: scheme
;; This declares the hello_world executable implemented by hello_world.ml
;; to be build as native (.exe) or byte-code (.bc) version.
(executable
(name hello_world)
(modes byte exe))
This ``hello_world.ml`` file:
.. code:: ocaml
print_endline "Hello, world!"
And build it with:
.. code:: bash
dune build hello_world.bc
The executable will be built as ``_build/default/hello_world.bc``.
The executable can be built and run in a single
step with ``dune exec ./hello_world.bc``. This byte-code version allows the usage of
``ocamldebug``.
Setting the OCaml compilation flags globally
============================================
Write this ``dune`` file at the root of your project:
.. code:: scheme
(env
(dev
(flags (:standard -w +42)))
(release
(flags (:standard -O3))))
`dev` and `release` correspond to build profiles. The build profile
can be selected from the command line with ``--profile foo`` or from a
`dune-workspace` file by writing:
.. code:: scheme
(profile foo)
Using cppo
==========
Add this field to your ``library`` or ``executable`` stanzas:
.. code:: lisp
(preprocess (action (run %{bin:cppo} -V OCAML:%{ocaml_version} %{input-file})))
Additionally, if you want to include a ``config.h`` file, you need to
declare the dependency to this file via:
.. code:: scheme
(preprocessor_deps config.h)
Using the .cppo.ml style like the ocamlbuild plugin
---------------------------------------------------
Write this in your ``dune`` file:
.. code:: lisp
(rule
(targets foo.ml)
(deps (:first-dep foo.cppo.ml) <other files that foo.ml includes>)
(action (run %{bin:cppo} %{first-dep} -o %{targets})))
Defining a library with C stubs
===============================
Assuming you have a file called ``mystubs.c``, that you need to pass
``-I/blah/include`` to compile it and ``-lblah`` at link time, write
this ``dune`` file:
.. code:: scheme
(library
(name mylib)
(public_name mylib)
(libraries re lwt)
(foreign_stubs
(language c)
(names mystubs)
(flags -I/blah/include))
(c_library_flags (-lblah)))
Defining a library with C stubs using pkg-config
================================================
Same context as before, but using ``pkg-config`` to query the
compilation and link flags. Write this ``dune`` file:
.. code:: lisp
(library
(name mylib)
(public_name mylib)
(libraries re lwt)
(foreign_stubs
(language c)
(names mystubs)
(flags (:include c_flags.sexp)))
(c_library_flags (:include c_library_flags.sexp)))
(rule
(targets c_flags.sexp c_library_flags.sexp)
(action (run ./config/discover.exe)))
Then create a ``config`` subdirectory and write this ``dune`` file:
.. code:: scheme
(executable
(name discover)
(libraries dune.configurator))
as well as this ``discover.ml`` file:
.. code:: ocaml
module C = Configurator.V1
let () =
C.main ~name:"foo" (fun c ->
let default : C.Pkg_config.package_conf =
{ libs = ["-lgst-editing-services-1.0"]
; cflags = []
}
in
let conf =
match C.Pkg_config.get c with
| None -> default
| Some pc ->
match (C.Pkg_config.query pc ~package:"gst-editing-services-1.0") with
| None -> default
| Some deps -> deps
in
C.Flags.write_sexp "c_flags.sexp" conf.cflags;
C.Flags.write_sexp "c_library_flags.sexp" conf.libs)
Using a custom code generator
=============================
To generate a file ``foo.ml`` using a program from another directory:
.. code:: lisp
(rule
(targets foo.ml)
(deps (:gen ../generator/gen.exe))
(action (run %{gen} -o %{targets})))
Defining tests
==============
Write this in your ``dune`` file:
.. code:: scheme
(test (name my_test_program))
And run the tests with:
.. code:: bash
dune runtest
It will run the test program (the main module is ``my_test_program.ml``) and
error if it exits with a nonzero code.
In addition, if a ``my_test_program.expected`` file exists, it will be compared
to the standard output of the test program and the differences will be
displayed. It is possible to replace the ``.expected`` file with the last output
using:
.. code:: bash
dune promote
Building a custom toplevel
==========================
A toplevel is simply an executable calling ``Topmain.main ()`` and linked with
the compiler libraries and ``-linkall``. Moreover, currently toplevels can only
be built in bytecode.
As a result, write this in your ``dune`` file:
.. code:: scheme
(executable
(name mytoplevel)
(libraries compiler-libs.toplevel mylib)
(link_flags (-linkall))
(modes byte))
And write this in ``mytoplevel.ml``
.. code:: ocaml
let () = Topmain.main ()
|