0

为了使我们的代码适用于多个 ocamlgraph 版本,我们在我们的一个文件中使用了这个片段

#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

在我们的dune文件中,我们使用 cppo 对其进行预处理,如下所示:

(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))))

现在我们要使用dune build @fmt. 问题是,ocamlformat 不理解尚未预处理的文件。一种解决方法是将受影响的文件添加到其中,.ocamlformat-ignore但该文件相当大,因此很遗憾没有对其进行自动格式化。

这个问题有简单的解决方案吗?也许有一个常见的模式如何用沙丘解决这个问题?

4

1 回答 1

1

从技术上讲不是答案,但有几个线索:

  • 您可以将已预处理的部分移动到另一个文件中dom.ml,并放弃对该文件进行格式化,因为该文件会比较小。我知道不理想。

  • 也许您可以查看 Dune 的Alternative dependencies,它允许您根据给定库的存在与否在不同的模块中选择一个模块。但据我所知,您仍然无法根据某些库的版本进行选择,请参阅this issue

  • 你可以混合使用 Makefile+Dune,预处理部分由 makefile 处理,格式化由 dune 处理。

  • 您也可以使用一流的模块,但我认为这并不令人满意,因为您希望在编译时而不是在运行时做出选择。

于 2021-01-23T10:11:05.920 回答