2018-04-11 15:14:53 +00:00
|
|
|
************
|
|
|
|
Configurator
|
|
|
|
************
|
|
|
|
|
|
|
|
Configurator is a small library designed to query features available on the
|
|
|
|
system, in order to generate configuration for dune builds. Such generated
|
|
|
|
configuration is usually in the form of command line flags, generated headers,
|
|
|
|
stubs, but there are no limitations on this.
|
|
|
|
|
|
|
|
Configurator allows you to query for the following features:
|
|
|
|
|
2018-04-12 02:12:56 +00:00
|
|
|
* Variables defined in ``ocamlc - config``,
|
2018-04-11 15:14:53 +00:00
|
|
|
|
2018-04-12 02:12:56 +00:00
|
|
|
* pkg-config_ flags for packages,
|
2018-04-11 15:14:53 +00:00
|
|
|
|
2018-04-12 02:12:56 +00:00
|
|
|
* Test features by compiling C code,
|
2018-04-11 15:14:53 +00:00
|
|
|
|
|
|
|
* Extract compile time information such as ``#define`` variables.
|
|
|
|
|
2018-04-11 16:50:16 +00:00
|
|
|
Configurator is designed to be cross compilation friendly and avoids _running_
|
|
|
|
any compiled code to extract any of the information above.
|
2018-04-11 15:14:53 +00:00
|
|
|
|
|
|
|
Configurator started as an `independent library
|
|
|
|
<https://github.com/janestreet/configurator>`__, but now lives in dune. You do
|
|
|
|
not need to install anything to use configurator.
|
|
|
|
|
|
|
|
Usage
|
|
|
|
=====
|
|
|
|
|
|
|
|
We'll describe configurator with a simple example. Everything else can be easily
|
2018-04-11 17:05:44 +00:00
|
|
|
learned by studying `configurator's API
|
|
|
|
<https://github.com/ocaml/dune/blob/master/src/configurator/v1.mli>`__.
|
2018-04-11 15:14:53 +00:00
|
|
|
|
|
|
|
To use configurator, we write an executable that will query the system using
|
|
|
|
configurator's API and output a set of targets reflecting the results. For
|
|
|
|
example:
|
|
|
|
|
|
|
|
.. code-block:: ocaml
|
|
|
|
|
|
|
|
module C = Configurator.V1
|
|
|
|
|
|
|
|
let clock_gettime_code = {|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|}
|
|
|
|
|
|
|
|
let () =
|
|
|
|
C.main ~name:"foo" (fun c ->
|
|
|
|
let has_clock_gettime = C.c_test c clock_gettime_code ~link_flags:["-lrt"] in
|
|
|
|
|
|
|
|
C.C_define.gen_header_file c ~fname:"config.h"
|
|
|
|
[ "HAS_CKOCK_GETTIME", Switch has_ckock_gettime ]);
|
|
|
|
|
|
|
|
Usually, the module above would be named ``discover.ml``. The next step is to
|
|
|
|
invoke it as an executable and tell dune about the targets that it produces:
|
|
|
|
|
|
|
|
.. code-block:: lisp
|
|
|
|
|
|
|
|
(executable
|
|
|
|
((name discover)
|
2018-07-06 08:10:59 +00:00
|
|
|
(libraries (dune.configurator))))
|
2018-04-11 15:14:53 +00:00
|
|
|
|
|
|
|
(rule
|
|
|
|
((targets (config.h))
|
|
|
|
(action (run ./discover.exe))))
|
|
|
|
|
|
|
|
Another common pattern is to produce a flags file with configurator and then use
|
|
|
|
this flag file using ``:include``:
|
|
|
|
|
|
|
|
.. code-block:: lisp
|
|
|
|
|
|
|
|
(library
|
|
|
|
((name mylib)
|
|
|
|
(c_names (foo))
|
|
|
|
(c_library_flags (:include (flags.sexp)))))
|
|
|
|
|
2018-04-17 11:55:21 +00:00
|
|
|
For this, generate the list of flags for your library — for example
|
|
|
|
using ``Configurator.V1.Pkg_config`` — and then write them to a file,
|
|
|
|
in the above example ``flags.sexp``, with
|
|
|
|
``Configurator.V1.write_flags "flags.sexp" flags``.
|
|
|
|
|
2018-04-11 15:14:53 +00:00
|
|
|
Upgrading from the old Configurator
|
|
|
|
===================================
|
|
|
|
|
|
|
|
The old configurator is the independent `configurator
|
|
|
|
<https://github.com/janestreet/configurator>`__ opam package. It is deprecated
|
|
|
|
and users are encouraged to migrate to dune's own configurator. The advantage of
|
|
|
|
the transition include:
|
|
|
|
|
2018-04-12 02:12:56 +00:00
|
|
|
* No extra dependencies,
|
2018-04-11 15:14:53 +00:00
|
|
|
|
2018-04-12 02:12:56 +00:00
|
|
|
* No need to manually pass ``-ocamlc`` flag,
|
2018-04-11 15:14:53 +00:00
|
|
|
|
2018-04-12 02:12:56 +00:00
|
|
|
* New configurator is cross compilation compatible.
|
2018-04-11 15:14:53 +00:00
|
|
|
|
|
|
|
The following steps must be taken to transition from the old configurator:
|
|
|
|
|
2018-04-12 02:12:56 +00:00
|
|
|
* Mentions of the ``configurator`` opam package should be removed.
|
2018-04-11 15:14:53 +00:00
|
|
|
|
2018-07-06 08:10:59 +00:00
|
|
|
* The library name ``configurator`` should be changed ``dune.configurator``.
|
2018-04-11 15:14:53 +00:00
|
|
|
|
|
|
|
* The ``-ocamlc`` flag in rules that run configurator scripts should be removed.
|
|
|
|
This information is now passed automatically by dune.
|
|
|
|
|
|
|
|
* The new configurator API is versioned explicitly. The version that is
|
|
|
|
compatible with old configurator is under the ``V1`` module. Hence, to
|
|
|
|
transition one's code it's enough to add this module alias:
|
|
|
|
|
|
|
|
.. code-block:: ocaml
|
|
|
|
|
|
|
|
module Configurator = Configurator.V1
|
|
|
|
|
|
|
|
.. _pkg-config: https://www.freedesktop.org/wiki/Software/pkg-config/
|