使ocamlbuild将.ml和.mli文件传递给ocamldoc

时间:2012-12-11 20:44:21

标签: ocaml ocamlbuild ocamldoc

我想在生成的文档中包含源代码。当我在命令行上调用ocamldoc时,这会起作用:ocamldoc -I _build -html -keep-code -colorize-code *.{ml,mli} -d .docdir。但是,我很难将其与ocamlbuild进行整合。

我在myocamlbuild.ml中使用以下代码:

open Ocamlbuild_plugin;;

dispatch begin function
  | After_options ->
      Options.ocamldoc := S[A"ocamldoc"; A"-keep-code"; A"-colorize-code"]
  | _ -> ()
end

但是这只包含没有相应接口文件的文件的来源 - 与所说的here相反,看起来ocamlbuild拒绝将.ml个文件传递给ocamldoc当存在.mli文件时。有没有办法哄ocamlbuild做我想做的事情?

2 个答案:

答案 0 :(得分:4)

因为我需要破解某些类似的东西,如果我在这里作为答案发布它可能会有所帮助(至少在OCamlbuild功能之前)。所以,这是我myocamlbuild.ml的相关部分:

open Ocamlbuild_plugin;;

dispatch begin function
  | After_rules ->
     (* Using both .ml and .mli files to build documentation: *)
     rule "ocaml: ml & mli -> odoc"
       ~insert:`top
       ~tags:["ocaml"; "doc"; "doc_use_interf_n_implem"]
       ~prod:"%.odoc"
       (* "%.cmo" so that cmis of ml dependencies are already built: *)
       ~deps:["%.ml"; "%.mli"; "%.cmo"]
       begin fun env build ->
         let mli = env "%.mli" and ml = env "%.ml" and odoc = env "%.odoc" in
         let tags =
           (Tags.union (tags_of_pathname mli) (tags_of_pathname ml))
           ++"doc_use_interf_n_implem"++"ocaml"++"doc" in
         let include_dirs = Pathname.include_dirs_of (Pathname.dirname ml) in
         let include_flags =
           List.fold_right (fun p acc -> A"-I" :: A p :: acc) include_dirs [] in
         Cmd (S [!Options.ocamldoc; A"-dump"; Px odoc;
                 T (tags++"doc"++"pp"); S (include_flags);
                 A"-intf"; P mli; A"-impl"; P ml])
       end;

    (* Specifying merge options with tags: *)
    pflag ["ocaml"; "doc"; "doc_use_interf_n_implem"] "merge"
      (fun s -> S[A"-m"; A s]);
end

然后将标记doc_use_interf_n_implem添加到.ml和/或.mli文件中,应该从实现和界面生成文档。

使用上面的代码,添加合并选项也可以通过添加像merge(A)这样的标签来完成(以合并所有)。

请注意,这可能会破坏复杂项目中的内容。值得注意的是,我没有使用camlp[45]处理过的文件进行测试,也没有使用其他4.00.1版本的OCamlbuild版本进行测试。

答案 1 :(得分:3)

目前似乎在OCamlbuild中没有对此的支持。您能否提交此on the bugtracker的功能请求?