linkerocamlocamlbuild

Tell ocamlbuild where to find module


I have an OCaml project, for which I use ocamlbuild. In this project, I have two subprojects, subproject1 and subproject2. subproject2 needs to use a module called Module from subproject1. To sum up, I have the following structure:

project/
|
|-- subproject1
|   |
|   |-- module.ml
|
|-- subproject2
    |
    |-- main.ml

If module.ml were located in subproject2, next to main.ml, I'd simply use the open directive in subproject2/main.ml:

(*subproject2/main.ml *)
open Module

But since module.ml is located in subproject1, how can I tell ocamlbuild to open subproject1/module.ml?


Solution

  • While I haven't found any straightforward way to include or directly link a source file, I've come up with a way to effectively include a module from a sibling directory.

    Once again, here is the structure of the project:

    project/
    |
    |-- subproject1
    |   |
    |   |-- module.ml
    |
    |-- subproject2
        |
        |-- main.ml
    

    From the project directory, the following ocamlbuild command will compile the whole project, with subproject2/main.ml as target, and subproject2/_build/ as output build directory:

    ocamlbuild -I subproject1 subproject2/_build/ subproject2/main.byte
    

    The expected main.byte file will however be output as subproject2/_build/subproject2/main.byte, so manual link editing will be necessary to end up with the usual main.byte symlink in subproject2.


    Since I like being able to use make, I wrote the following Makefile in subproject2/, so as to compile subproject2/ by simply running make from the inside, as make is supposed to work.

    all:
        # Move to project/
        cd ..;\
        # Run the ocamlbuild command
        ocamlbuild -I subproject1 subproject2/_build/ subproject2/main.byte\
        # Go back into subproject2
        cd subproject2;\
        # Try to remove the existing symlink, mute stderr, and continue on error
        rm main.byte 2> /dev/null || true;\
        # Create the desired symlink
        ln -s _build/subproject2/main.byte main.byte