ocaml-obuild / obuild

simple package build system for ocaml
BSD 2-Clause "Simplified" License
55 stars 20 forks source link

How to use preprocessing in obuild ? #155

Closed douglarek closed 8 years ago

douglarek commented 8 years ago

Take filedslib for example.

name: hello
version: 0.1.0
synopsis: A demo.
obuild-ver: 1

executable hello
  main: main.ml
  src-dir: src
  build-deps: core
module Logon = struct
  type t =
    {
      session_id: string;
      time: Time.t;
      user: string;
      credentials: string;
    } with fields (* here *)
end
jeromemaloberti commented 8 years ago

AFAICT fieldslib seems to be only a library, you would need ppx_fields_conv (for ppx) or pa_fields_conv (for camlp4) to use it as an extension. The "modern" way would be the ppx version, just add ppx_fields_conv to the build-deps of your exe/lib and it should preprocess the file and compile it from obuild 0.1.8. If it doesn't please provide your test and open a ticket, I will fix the bug. It is also possible with camlp4 (a little bit more complicated but not much), however I couldn't install pa_fields_conv with ocaml 4.02.3 ...

douglarek commented 8 years ago

@jeromemaloberti thanks for your quick reply;

this is my project :https://gist.github.com/douglarek/9e7915e847245b236890179af3d49209;

It failed:

File "src/main.ml", line 32, characters 6-10:
Error: Syntax error
jeromemaloberti commented 8 years ago

Thank you. It is indeed a bug, I forgot to use the preprocessor when calling ocamldep to get the dependencies. I will fix it tonight.

jeromemaloberti commented 8 years ago

Actually, I got it wrong. The problem is that you're using the camlp4 extension with ppx. You must replace "with fields" with "[@@deriving fields]" and it will work. You can see the example: https://github.com/janestreet/ppx_fields_conv/blob/master/example/test.ml

douglarek commented 8 years ago

Thanks, I am a newbie to OCaml, but how can I use with fields rather than [@@devering fields], I am reading Real world OCaml book.

rgrinberg commented 8 years ago

@douglarek Why can't you just do [@@deriving fields]? It should generate the same code for you.

douglarek commented 8 years ago

@rgrinberg this is an example in book Real world OCaml, but it did not work with obuild

rgrinberg commented 8 years ago

@douglarek post your project in a git repo somewhere

douglarek commented 8 years ago

@rgrinberg Please follow https://gist.github.com/douglarek/9e7915e847245b236890179af3d49209

jeromemaloberti commented 8 years ago

Sorry for the late answer. You can use pa_fields_conv ("with fields") by adding pa_fields_conv to the build-deps (you need to install it, of course) and by adding: pp: camlp4o in your target description. So: executable hello main: main.ml src-dir: src pp: camlp4o build-deps: core, pa_fields_conv

jeromemaloberti commented 8 years ago

However, pa_fields_conv has been tagged deprecated, so maybe you should think about using ppx_fields_conv and replace "with fields" with "[@@deriving fields]" ...

douglarek commented 8 years ago

@jeromemaloberti But I just tried [@@deriving fields], it works without ppx_fields_conv preprocessing, maybe it is part of core. And my OCaml version is 4.02.

jeromemaloberti commented 8 years ago

Yes, and no. ppx are annotations, the code compile without the preprocessing, but no code was generated, so if you try to use the supposedly generated code, it will fail.

douglarek commented 8 years ago

@jeromemaloberti thanks, it is very kind of you to answer so lubberly problem.