Pierre T.
Pierre T.

Reputation: 378

How to generate and use the generated file

Is it possible, with ocp-build, to do the following actions:

  1. Compile a generator.
  2. Call the generator to generate a file.
  3. Compile the project with the generated file.

So far, I tried this:

(generator.ocp)

begin library "error_gen"
    sort = true
    files = [ "error_gen.ml" ]
    requires = [str]
end

(generated.ocp)

begin library "error_code"
    sort = true
    files = [
      "error_code.ml" (
        pp = [ "./_obuild/error_gen/error_gen.byte" ]
        pp_requires = [ "error_gen:byte" ]
      )
    ]
    requires = ["error_gen"]
end

(and the main.ocp)

begin program "main"
    sort = true
    files = []
    requires = ["error_code" "parser"]
end

It complains with this message:

Error: in project "error_code", the source filename "src/generated/error_code.ml" does not exist

I saw that some supports exist for version file generation, such as in the project ocp-indent

line 46.

"indentVersion.ml" (ocp2ml) (* auto-generated by ocp-build *)

Any helps greatly appreciated, thanks.

Upvotes: 0

Views: 123

Answers (1)

Fabrice Le Fessant
Fabrice Le Fessant

Reputation: 4284

In branch "next" of github.com/OCamlPro/ocp-build, you will find a version of ocp-build that might solve your issue:

begin library "error_code"
    sort = true
    files = [ "error_code.ml" ]
    build_rules = [
      "error_code.ml" (
        (* the files that are needed to call the command *)
        sources = [ "%{error_gen_FULL_DST_DIR}%/error_gen.byte" ]
        (* the commands to be executed, in the sub-directory of the library
           each command has the syntax:    { "COMMAND" "ARG1" "ARG2" ... }
        *)
        commands = [ 
           {   "%{error_gen_FULL_DST_DIR}%/error_gen.byte"   } 
        ]
      )
    ]
    requires = ["error_gen"]
end

This is, for example, used in wxOCaml:

https://github.com/OCamlPro/ocplib-wxOCaml/blob/next-ocpbuild/configure.ocp

Commands can be post-fixed with options:

{ "configure" } (chdir = "subdirectory")  (* to execute in a sub-directory *)
{ "cat" "toto" } (stdout = "new_toto")    (* to copy the stdout in "new_toto" *)

Upvotes: 1

Related Questions