Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass more information to ppx rewriters #2106

Merged
10 commits merged into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ unreleased
- Allow %{...} variables in pps flags (#2076, @mlasson review by @diml and
@aalekseyev).

- Add a 'cookies' option to ppx_rewriter/deriver flags in library stanzas
(#2106, @mlasson @diml). This allow to specify cookie requests from
variables expanded at each invocation of the preprocessor.

- Add more opam metadata and use it to generate `.opam` files. In particular, a
`package` field has been added to specify package specific information.
(#2017, #2091, @avsm, @jonludlam, @rgrinberg)
Expand Down
7 changes: 6 additions & 1 deletion doc/dune-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ to use the :ref:`include_subdirs` stanza.
available choices are ``ppx_rewriter`` and ``ppx_deriver`` and must be set
when the library is intended to be used as a ppx rewriter or a ``[@@deriving
...]`` plugin. The reason why ``ppx_rewriter`` and ``ppx_deriver`` are split
is historical and hopefully we won't need two options soon
is historical and hopefully we won't need two options soon. Both ppx kinds
support an optional field ``(cookies <cookies>)`` where ``<cookies>`` is a
list of pairs ``(<name> <value>)`` with ``<name>`` being the cookie name and
``<value>`` is a string that supports `Variables expansion`_ evaluated
by each invocation of the preprocessor (note: libraries that share
cookies with the same name should agree on their expanded value)

- ``(ppx_runtime_libraries (<library-names>))`` is for when the library is a ppx
rewriter or a ``[@@deriving ...]`` plugin and has runtime dependencies. You
Expand Down
8 changes: 4 additions & 4 deletions src/gen_meta.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ let gen_lib pub_name lib ~version =
let preds =
match Lib.kind lib with
| Normal -> []
| Ppx_rewriter | Ppx_deriver -> [Pos "ppx_driver"]
| Ppx_rewriter _ | Ppx_deriver _ -> [Pos "ppx_driver"]
in
let lib_deps = Lib.Meta.requires lib in
let ppx_rt_deps = Lib.Meta.ppx_runtime_deps lib in
Expand All @@ -95,7 +95,7 @@ let gen_lib pub_name lib ~version =
]
; (match Lib.kind lib with
| Normal -> []
| Ppx_rewriter | Ppx_deriver ->
| Ppx_rewriter _ | Ppx_deriver _ ->
(* Deprecated ppx method support *)
let no_ppx_driver = Neg "ppx_driver" and no_custom_ppx = Neg "custom_ppx" in
List.concat
Expand All @@ -107,10 +107,10 @@ let gen_lib pub_name lib ~version =
]
; match Lib.kind lib with
| Normal -> assert false
| Ppx_rewriter ->
| Ppx_rewriter _ ->
[ rule "ppx" [no_ppx_driver; no_custom_ppx]
Set "./ppx.exe --as-ppx" ]
| Ppx_deriver ->
| Ppx_deriver _ ->
[ rule "requires" [no_ppx_driver; no_custom_ppx] Add
"ppx_deriving"
; rule "ppxopt" [no_ppx_driver; no_custom_ppx] Set
Expand Down
4 changes: 2 additions & 2 deletions src/install_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ let init_meta sctx ~dir =

let lib_ppxs sctx ~(lib : Dune_file.Library.t) ~scope ~dir_kind =
match lib.kind with
| Normal | Ppx_deriver -> []
| Ppx_rewriter ->
| Normal | Ppx_deriver _ -> []
| Ppx_rewriter _ ->
let name = Dune_file.Library.best_name lib in
match (dir_kind : Dune_lang.File_syntax.t) with
| Dune ->
Expand Down
74 changes: 64 additions & 10 deletions src/lib_kind.ml
Original file line number Diff line number Diff line change
@@ -1,19 +1,73 @@
module Ppx_args = struct
module Cookie = struct
type t =
{ name : string
; value : String_with_vars.t
}

let decode =
let open Stanza.Decoder in
let* () = Syntax.since Stanza.syntax (1, 10) in
enter
(
let+ name = plain_string
(fun ~loc str ->
if String.contains str '=' then
Errors.fail loc "Character '=' is not allowed in cookie names"
else
str
)
and+ value = String_with_vars.decode in
{ name; value }
)

let encode { name; value } =
let open Dune_lang in
List
[ Encoder.string name
; String_with_vars.encode value
]
end

type t =
{ cookies : Cookie.t list
}

let decode =
let open Stanza.Decoder in
let args =
let+ cookies = field "cookies" (list Cookie.decode) ~default:[] in
{cookies}
in
fields args

let encode { cookies } =
let open Dune_lang.Encoder in
record_fields
[ field_l "cookies" Cookie.encode cookies ]
end

type t =
| Normal
| Ppx_deriver
| Ppx_rewriter
| Ppx_deriver of Ppx_args.t
| Ppx_rewriter of Ppx_args.t

let decode =
let open Dune_lang.Decoder in
enum
[ "normal" , Normal
; "ppx_deriver" , Ppx_deriver
; "ppx_rewriter" , Ppx_rewriter
sum
[ "normal" , return Normal
; "ppx_deriver" , (let+ args = Ppx_args.decode in Ppx_deriver args)
; "ppx_rewriter" , (let+ args = Ppx_args.decode in Ppx_rewriter args)
]

let encode t =
Dune_lang.Encoder.string (
match
match t with
| Normal -> "normal"
| Ppx_deriver -> "ppx_deriver"
| Ppx_rewriter -> "ppx_rewriter")
| Normal -> Dune_lang.atom "normal"
| Ppx_deriver x ->
List (Dune_lang.atom "ppx_deriver" :: Ppx_args.encode x)
| Ppx_rewriter x ->
List (Dune_lang.atom "ppx_rewriter" :: Ppx_args.encode x)
with
| List [x] -> x
| x -> x
17 changes: 15 additions & 2 deletions src/lib_kind.mli
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
module Ppx_args : sig
module Cookie : sig
type t =
{ name : string
; value : String_with_vars.t
}
end

type t =
{ cookies : Cookie.t list
}
end

type t =
| Normal
| Ppx_deriver
| Ppx_rewriter
| Ppx_deriver of Ppx_args.t
| Ppx_rewriter of Ppx_args.t

include Dune_lang.Conv with type t := t
2 changes: 1 addition & 1 deletion src/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module Gen (P : sig val sctx : Super_context.t end) = struct
; Dyn (fun (_, _, _, library_flags) -> As library_flags)
; As (match lib.kind with
| Normal -> []
| Ppx_deriver | Ppx_rewriter -> ["-linkall"])
| Ppx_deriver _ | Ppx_rewriter _ -> ["-linkall"])
; Dyn (fun (cm_files, _, _, _) -> Deps cm_files)
; Hidden_targets
(match mode with
Expand Down
11 changes: 4 additions & 7 deletions src/merlin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,12 @@ let pp_flags sctx ~expander ~dir_kind { preprocess; libname; _ } =
match Dune_file.Preprocess.remove_future_syntax preprocess
(Super_context.context sctx).version
with
| Pps { loc = _; pps; flags; staged = _ } -> begin
match Preprocessing.get_ppx_driver sctx ~scope ~dir_kind pps with
| Pps { loc; pps; flags; staged = _ } -> begin
match Preprocessing.get_ppx_driver sctx ~loc ~expander ~lib_name:libname ~flags ~scope ~dir_kind pps with
| Error _ -> None
| Ok exe ->
let flags = List.map ~f:(Expander.expand_str expander) flags in
| Ok (exe, flags) ->
(Path.to_absolute_filename exe
:: "--as-ppx"
:: Preprocessing.cookie_library_name libname
@ flags)
:: "--as-ppx" :: flags)
|> List.map ~f:quote_for_merlin
|> String.concat ~sep:" "
|> Filename.quote
Expand Down
Loading