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 list expansion in preprocessor arguments #5820

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 16 additions & 4 deletions src/dune_rules/preprocessing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,21 @@ let get_cookies ~loc ~expander ~lib_name libs =
[ "--cookie"; sprintf "%s=%S" name value ])
|> List.concat

let ppx_driver_and_flags_internal sctx ~loc ~expander ~lib_name ~flags libs =
let ppx_driver_and_flags_internal sctx ~scope ~loc ~expander ~lib_name ~flags
libs =
let open Action_builder.O in
let dune_version = Scope.project scope |> Dune_project.dune_version in
let* flags =
Action_builder.List.map ~f:(Expander.expand_str expander) flags
if dune_version <= (3, 2) then
Action_builder.List.map ~f:(Expander.expand_str expander) flags
else
(* Allow list expansion *)
Action_builder.List.concat_map flags
~f:(Expander.expand ~mode:Many expander)
|> Action_builder.map
~f:
(List.map
~f:(Value.to_string ~dir:(Path.build @@ Expander.dir expander)))
in
let+ cookies =
Action_builder.of_memo (get_cookies ~loc ~lib_name ~expander libs)
Expand All @@ -435,7 +446,8 @@ let ppx_driver_and_flags sctx ~lib_name ~expander ~scope ~loc ~flags pps =
let open Action_builder.O in
let* libs = Resolve.Memo.read (Lib.DB.resolve_pps (Scope.libs scope) pps) in
let* exe, flags =
ppx_driver_and_flags_internal sctx ~loc ~expander ~lib_name ~flags libs
ppx_driver_and_flags_internal sctx ~loc ~expander ~scope ~lib_name ~flags
libs
in
let* libs = Resolve.Memo.read (Lib.closure libs ~linking:true) in
let+ driver =
Expand Down Expand Up @@ -698,7 +710,7 @@ let make sctx ~dir ~expander ~lint ~preprocess ~preprocessor_deps
let get_ppx_driver sctx ~loc ~expander ~scope ~lib_name ~flags pps =
let open Action_builder.O in
let* libs = Resolve.Memo.read (Lib.DB.resolve_pps (Scope.libs scope) pps) in
ppx_driver_and_flags_internal sctx ~loc ~expander ~lib_name ~flags libs
ppx_driver_and_flags_internal sctx ~loc ~expander ~scope ~lib_name ~flags libs

let ppx_exe sctx ~scope pp =
let open Resolve.Memo.O in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(library
(name driver_print_args)
(modules ())
(ppx.driver (main "(fun () -> Array.iter print_endline Sys.argv)")))

(rule (with-stdout-to test_ppx_args.ml (echo "")))

(library
(name ppx_with_cookies_print_args)
(kind (ppx_rewriter
(cookies (italy "%{env:ITALY=undefined}")
(france "%{env:FRANCE=undefined}"))))
(modules ())
(libraries driver_print_args))

(env (_ (env-vars (ITALY "Biscotti") (FRANCE "Petit Beurre") (AMERICA "Oreo") (ENGLAND "Snickerdoodle"))))

(library
(name test_ppx_args)
(modules test_ppx_args)
(preprocess
(pps -arg1 driver_print_args ppx_with_cookies_print_args -arg2 -arg3=%{env:AMERICA=undefined} --
-foo bar %{env:ENGLAND=undefined} %{read-lines:ppx-args})))

(library
(name driver_print_tool)
(public_name foo.driver_print_tool)
(kind ppx_rewriter)
(modules ())
(libraries compiler-libs.common)
(ppx.driver (main "\| (fun () ->
"\| Ast_mapper.run_main (fun argv ->
"\| Printf.eprintf "tool name: %s\nargs:%s\n"
"\| (Ast_mapper.tool_name ())
"\| (String.concat " " argv);
"\| Ast_mapper.default_mapper))
)))

(library
(name ppx_with_cookies_print_tool)
(kind (ppx_rewriter
(cookies (italy "%{env:ITALY=undefined}")
(france "%{env:FRANCE=undefined}"))))
(modules ())
(libraries driver_print_tool))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Args
From
A
File
14 changes: 14 additions & 0 deletions test/blackbox-tests/test-cases/dune-ppx-driver-system.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ Test the argument syntax
- test_ppx_args.pp.ml
[1]

Test the argument syntax with list expansion allowed (dune > 3.2)

$ dune build --root driver-tests-list-args
Entering directory 'driver-tests-list-args'
File "dune-project", line 1, characters 11-14:
1 | (lang dune 3.3)
^^^
Error: Version 3.3 of the dune language is not supported.
Supported versions of this extension in version 3.3 of the dune language:
- 1.0 to 1.12
- 2.0 to 2.9
- 3.0 to 3.2
[1]

Test that going through the -ppx option of the compiler works

$ dune build --root driver-tests test_ppx_staged.cma
Expand Down