From 6c10ed5a7654f83586eac71e9e45cd5c086ab234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ojeda=20B=C3=A4r?= Date: Tue, 10 Nov 2020 09:32:55 +0100 Subject: [PATCH 1/4] Add support for passing arguments to instrumentation backends MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nicolás Ojeda Bär --- src/dune_rules/dune_file.ml | 10 ++++++++-- src/dune_rules/preprocess.ml | 7 +++---- src/dune_rules/preprocess.mli | 1 + 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/dune_rules/dune_file.ml b/src/dune_rules/dune_file.ml index 1a677b62f1a..a4f9edfa871 100644 --- a/src/dune_rules/dune_file.ml +++ b/src/dune_rules/dune_file.ml @@ -231,7 +231,11 @@ module Buildable = struct located (multi_field "instrumentation" ( Dune_lang.Syntax.since Stanza.syntax (2, 7) - >>> fields (field "backend" (located Lib_name.decode)) )) + >>> fields + (field "backend" + (let+ libname = located Lib_name.decode + and+ flags = repeat String_with_vars.decode in + (libname, flags))) )) in let preprocess = let init = @@ -239,7 +243,9 @@ module Buildable = struct Module_name.Per_item.map preprocess ~f:(Preprocess.map ~f) in List.fold_left instrumentation - ~f:(Preprocess.Per_module.add_instrumentation ~loc:loc_instrumentation) + ~f:(fun accu (instrumentation, flags) -> + Preprocess.Per_module.add_instrumentation accu + ~loc:loc_instrumentation ~flags instrumentation) ~init in let foreign_stubs = diff --git a/src/dune_rules/preprocess.ml b/src/dune_rules/preprocess.ml index 9abe5850ff3..26a4519b5ed 100644 --- a/src/dune_rules/preprocess.ml +++ b/src/dune_rules/preprocess.ml @@ -197,19 +197,18 @@ module Per_module = struct else No_preprocessing - let add_instrumentation t ~loc libname = + let add_instrumentation t ~loc ~flags:flags' libname = Per_module.map t ~f:(fun pp -> match pp with | No_preprocessing -> let pps = [ With_instrumentation.Instrumentation_backend libname ] in - let flags = [] in let staged = false in - Pps { loc; pps; flags; staged } + Pps { loc; pps; flags = flags'; staged } | Pps { loc; pps; flags; staged } -> let pps = With_instrumentation.Instrumentation_backend libname :: pps in - Pps { loc; pps; flags; staged } + Pps { loc; pps; flags = flags @ flags'; staged } | Action (loc, _) | Future_syntax loc -> User_error.raise ~loc diff --git a/src/dune_rules/preprocess.mli b/src/dune_rules/preprocess.mli index 9e720ba5c29..fcaed6aa6df 100644 --- a/src/dune_rules/preprocess.mli +++ b/src/dune_rules/preprocess.mli @@ -77,6 +77,7 @@ module Per_module : sig val add_instrumentation : With_instrumentation.t t -> loc:Loc.t + -> flags:String_with_vars.t list -> Loc.t * Lib_name.t -> With_instrumentation.t t From 947e923742f8519b70539a66e764758b51f65128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ojeda=20B=C3=A4r?= Date: Tue, 10 Nov 2020 09:34:32 +0100 Subject: [PATCH 2/4] Update tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nicolás Ojeda Bär --- .../test-cases/instrumentation.t/ppx/hello.ml | 2 +- .../instrumentation.t/ppx/hello_ppx.ml | 13 +++++++- .../test-cases/instrumentation.t/run.t | 33 +++++++++++++------ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/test/blackbox-tests/test-cases/instrumentation.t/ppx/hello.ml b/test/blackbox-tests/test-cases/instrumentation.t/ppx/hello.ml index 58f4b502a1b..7f63d09714f 100644 --- a/test/blackbox-tests/test-cases/instrumentation.t/ppx/hello.ml +++ b/test/blackbox-tests/test-cases/instrumentation.t/ppx/hello.ml @@ -1 +1 @@ -let hello () = print_endline "Hello, Dune!" +let hello s = print_endline (Printf.sprintf "Hello from %s!" s) diff --git a/test/blackbox-tests/test-cases/instrumentation.t/ppx/hello_ppx.ml b/test/blackbox-tests/test-cases/instrumentation.t/ppx/hello_ppx.ml index b3c3edaebc2..242a0c13111 100644 --- a/test/blackbox-tests/test-cases/instrumentation.t/ppx/hello_ppx.ml +++ b/test/blackbox-tests/test-cases/instrumentation.t/ppx/hello_ppx.ml @@ -1,12 +1,23 @@ open Ast_helper open Longident +let place = ref None + let impl str = + let arg = + match !place with + | None -> Exp.ident (Location.mknoloc (Lident "__MODULE__")) + | Some s -> Exp.constant (Const.string s) + in Str.eval (Exp.apply (Exp.ident (Location.mknoloc (Ldot (Lident "Hello", "hello")))) - [Nolabel, Exp.construct (Location.mknoloc (Lident "()")) None]) :: str + [Nolabel, arg]) :: str open Ppxlib +let () = + Driver.add_arg "-place" (Arg.String (fun s -> place := Some s)) + ~doc:"PLACE where to say hello from" + let () = Driver.register_transformation_using_ocaml_current_ast ~impl "hello" diff --git a/test/blackbox-tests/test-cases/instrumentation.t/run.t b/test/blackbox-tests/test-cases/instrumentation.t/run.t index 47c6702d794..bee52bba12d 100644 --- a/test/blackbox-tests/test-cases/instrumentation.t/run.t +++ b/test/blackbox-tests/test-cases/instrumentation.t/run.t @@ -1,5 +1,6 @@ $ cat >dune-project < (lang dune 2.7) + > (wrapped_executables false) > EOF "Hello" is an instrumentation backend that instruments by printing "Hello, @@ -18,7 +19,7 @@ Dune!" at the beginning of the module. > EOF $ cat >mylib.ml < let f () = print_endline "Mylib" + > let f () = () > EOF $ cat >main.ml <dune < (executable + > (name main) + > (modules main) + > (instrumentation (backend hello -place Spain))) + > EOF + + $ dune build --instrument-with hello + $ _build/default/main.exe + Hello from Spain! Can also enable with an environment variable. $ DUNE_INSTRUMENT_WITH=hello dune build $ _build/default/main.exe - Hello, Dune! + Hello from Spain! Instrumentation can also be controlled by using the dune-workspace file. @@ -80,7 +92,7 @@ Instrumentation can also be controlled by using the dune-workspace file. $ dune build $ _build/default/main.exe - Hello, Dune! + Hello from Spain! It can also be controlled on a per-context scope. @@ -92,7 +104,7 @@ It can also be controlled on a per-context scope. $ dune build $ _build/coverage/main.exe - Hello, Dune! + Hello from Spain! Per-context setting takes precedence over per-workspace setting. @@ -119,6 +131,7 @@ Next, we check the backend can be used when it is installed. > EOF $ cat >installed/dune-project < (lang dune 2.7) + > (wrapped_executables false) > EOF $ cat >installed/dune < (executable @@ -130,4 +143,4 @@ Next, we check the backend can be used when it is installed. $ OCAMLPATH=$PWD/_install/lib dune build --root installed Entering directory 'installed' $ installed/_build/default/main.exe - Hello, Dune! + Hello from Main! From 05e132d23cba153cb36544c7592ccbda2ce1977c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ojeda=20B=C3=A4r?= Date: Wed, 11 Nov 2020 06:01:56 +0100 Subject: [PATCH 3/4] CHANGES.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nicolás Ojeda Bär --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index c7b0531f137..abe6f96baab 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -69,6 +69,9 @@ Unreleased - `dune describe` now also includes information about executables in addition to that of libraries. (#3892, #3895, @nojb) +- instrumentations backends can now receive arguments via `(instrumentation + (backend ))`. (#3906, #3932, @nojb) + 2.7.1 (2/09/2020) ----------------- From 1e93f799083744166254bc0591657ff1ff23d728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ojeda=20B=C3=A4r?= Date: Wed, 11 Nov 2020 06:06:50 +0100 Subject: [PATCH 4/4] Update manual MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nicolás Ojeda Bär --- doc/instrumentation.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/instrumentation.rst b/doc/instrumentation.rst index 42f3c94fb5a..8c42baaa3fb 100644 --- a/doc/instrumentation.rst +++ b/doc/instrumentation.rst @@ -23,7 +23,9 @@ executable stanza: (library (name ...) (instrumentation - (backend ))) + (backend ))) + +The backend ```` can be passed arguments using ````. This field can be repeated multiple times in order to support various backends. For instance: @@ -33,7 +35,7 @@ backends. For instance: (library (name foo) (modules foo) - (instrumentation (backend bisect_ppx)) + (instrumentation (backend bisect_ppx --bisect-silent yes)) (instrumentation (backend landmarks))) This will instruct Dune that when either the ``bisect_ppx`` or ``landmarks``