makefile - 如何通过 OCamlbuild 使用 Menhir 错误消息生成?

标签 makefile ocaml ocamlbuild menhir

我正在使用 menhir--compile-errors 功能,我对此非常满意。我还使用 ocamlbuild 来管理我的项目的编译。由于该项目非常基础,因此到目前为止构建基础设施仍然微不足道。

我在项目的根目录中有一个 _tags 文件和一个简单的 Makefile。我还没有 myocamlbuild.ml 文件。 _tags 文件仅包含一行:

<src/*>: package(ppx_deriving.std)

Makefile 的相关部分是

all: src/ParsingErrors.ml
    ocamlbuild $(OPTIONS) src/Main.native

src/ParsingErrors.ml: src/Handcrafted.messages src/Parser.mly
    menhir --compile-errors src/Handcrafted.messages src/Parser.mly > src/ParsingErrors.ml

OPTIONS = -j 4 -use-menhir -use-ocamlfind -yaccflag --table -pkgs menhirLib,str,unix

OCamlbuild 和 Menhir 通常集成良好,但 --compile-errors 似乎是一个相当新的功能。我的设置并不理想,因为我不喜欢在源目录而不是构建目录中自动生成文件 src/ParsingErrors.ml。向 OCamlbuild 解释我希望 Menhir 构建此错误消息文件的最佳方式是什么?更改文件的命名约定(例如,更改为 src/Parser.messagessrc/Parser.ml)如果可以简化事情,那么我不会感到困扰。

注意:虽然我在其他项目中有 myocamlbuild.ml 文件,但我从在线来源复制了它们。我发现它们很难破译,而且我真的不明白如何写这些东西。

最佳答案

寻找此问题答案的最佳位置是 Menhir 的发行版,因为 Menhir 在其自己的编译过程中使用“menhir --compile-errors”和 ocamlbuild。

要查找的文件是 source tarball 中的 src/myocamlbuild.ml .

以下是相关摘录:

(* This rule generates an .ml file [target] from an .mly file [grammar] and a
   .messages file [messages]. *)

(* If the name of a witness file is passed, it is made an additional
   dependency. This triggers a separate rule (see below) which performs a
   completeness check, that is, which checks that the .messages file lists
   every possible syntax error. *)

let compile_errors grammar messages (witness : string list) target =
  rule
    "menhir/compile_errors"
    ~prod:target
    ~deps:([ grammar; messages ] @ witness)
    (fun env _ ->
      let grammar = env grammar in
      let tags = tags_of_pathname grammar ++ "ocaml" ++ "menhir" in
      Cmd(S[
        !Options.ocamlyacc; (* menhir *)
        T tags;
        P grammar;
        A "--compile-errors"; P (env messages);
        Sh ">"; Px (env target);
      ]))

(* A generic version of the above rule, with uniform naming. *)

let generic_compile_errors (check_completeness : bool) =
  compile_errors
    (* sources: *)
    "%.mly" "%Messages.messages"
    (* if present, this dependency forces a completeness check: *)
    (if check_completeness then [ "%Messages.witness" ] else [])
    (* target: *)
    "%Messages.ml"

我很乐意与 ocamlbuild 维护者讨论是否可以将 Menhir 的 myocamlbuild.ml 的某些部分移至 ocamlbuild 的标准规则集中。 (嗨,加布里埃尔!)

关于makefile - 如何通过 OCamlbuild 使用 Menhir 错误消息生成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40604749/

相关文章:

c - 链接错误 : Undefined reference to functions that're defined in a separate file?

lua - 如何使用 autotools 在 libdir 的子目录中安装 lua 模块

我可以安全地假设所有(或大部分) "cc"在 *nix 系统上支持 "-Wall"吗?

windows - GNU Make - 如何添加时间戳输出(最小化 makefile 修改)

c - Frama-C:找到循环结束的位置

opengl - 使用 ocamlbuild 构建 SDL 和 OpenGL 应用程序

list - Ocaml 模式匹配

ocaml sprintf 说明

ocaml - OCaml 的 _tags 文件的目的是什么,以及如何解释内容?

debugging - ocamldebug 如何与包含电池的项目一起使用?