Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

ocaml - use ocamlformat with cppo and dune

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?

question from:https://stackoverflow.com/questions/65854619/use-ocamlformat-with-cppo-and-dune

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...