Reputation: 7154
to make our code work with multiple ocamlgraph versions, we are using this snippet in one of our files
#if OCAMLGRAPH_VERSION >= (2,0,0)
let module Dom = Dominator.Make_graph(struct
include G
let empty () = create ()
let add_edge g v1 v2 = add_edge g v1 v2; g
end) in
#elif OCAMLGRAPH_VERSION >= (1,8,6)
let module Dom = Dominator.Make_graph(G) in
#else
let module Dom = Dominator.Make(G) in
#endif
And in our dune
file we preprocess this with cppo like this:
(library
[...]
(preprocess (action (run %{bin:cppo} -V OCAMLGRAPH:%{read:ocamlgraph.version} %{input-file})))
(rule
(target ocamlgraph.version)
(action (with-stdout-to %{target} (run ocamlfind query -format %v ocamlgraph))))
Now we want to use dune build @fmt
. The problem is, that ocamlformat doesn't understand files that have yet to be preprocessed. One workaround would be to add the affected file to .ocamlformat-ignore
but the file is rather large so it would be a pity to not have it auto-formatted.
Is there an easy solution to this problem? Maybe there is a common pattern how this can be solved with dune?
Upvotes: 0
Views: 245
Reputation: 1596
Technically not an answer, but several leads:
You could move the part that is preprocessed into antother file dom.ml
and give up on formatting for this file which would than be small. Not ideal i know.
Maybe you can take a look at Dune's Alternative dependencies which allow you to select a module among different ones according to the presence or absence of a given library. But as far as i know, you still can't choose according to the version of some library, see this issue.
You can use a mix of Makefile+Dune, have the pre-processing part handled by makefile and the formatting by dune.
You could also use a first class module, but I suppose this is not satisfactory as you want the choice being made at compile time, and not at runtime.
Upvotes: 1