Include OCaml source files in each other
OCaml Makefile
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
pkg
src
src_test
.gitignore
.merlin
.travis.yml
CHANGELOG.md
LICENSE.txt
Makefile
README.md
_tags
descr
myocamlbuild.ml
opam

README.md

[%%include]

include is a syntax extension that allows to include an OCaml source file inside another one.

include requires OCaml 4.02 or later.

Installation

include can be installed via OPAM:

$ opam install ppx_include

Usage

In order to use include, require the package ppx_include.

Syntax

The structure item [%%include "file.ml"] or signature item [%%include "file.mli"] is replaced with the structure or signature items inside file.ml or file.mli. Whether the file is parsed as an interface or an implementation depends on the context of the [%%include] node; the extension is immaterial. The file can be located anywhere in the OCaml include path.

This can be most useful if you want to have the contents of recursive modules in several files:

module rec A : sig [%%include "a.mli"] end = struct [%%include "a.ml"] end
and B : sig [%%include "b.mli"] end = struct [%%include "b.ml"] end

Buildsystem integration

If your buildsystem uses separate source and build trees (like ocamlbuild does), include will not be able to find the included files. Since it is not possible to use existing mechanisms, such as ocamldep, to provide the dependencies to the buildsystem, it is necessary to specify dependencies manually.

ocamlbuild

For ocamlbuild, the myocamlbuild.ml file should look like this:

open Ocamlbuild_plugin

let () = dispatch (
  function
  | After_rules ->
    dep ["file:src_test/test_ppx_include.ml"]
        ["src_test/a.mli"; "src_test/b.mli"; "src_test/a.ml"; "src_test/b.ml"]
  | _ -> ())

License

include is distributed under the terms of MIT license.