@@ -266,6 +266,23 @@ let[@inline never] invalid_use_of_target_variable t
266
266
~field: multiplicity ~variable: var_multiplicity;
267
267
assert false )
268
268
269
+ let expand_read_macro ~dir ~source s ~read ~pack =
270
+ let path = relative ~source dir s in
271
+ let read =
272
+ let open Memo.Build.O in
273
+ let + x = Build_system. read_file path ~f: read in
274
+ pack x
275
+ in
276
+ Need_full_expander
277
+ (fun t ->
278
+ if Dune_project. dune_version (Scope. project t.scope) > = (3 , 0 ) then
279
+ Without read
280
+ else
281
+ (* To prevent it from working in certain position before Dune 3.0. It'd
282
+ be nice if we could invite the user to upgrade to (lang dune 3.0),
283
+ but this is a bigger refactoring. *)
284
+ With (Action_builder. memo_build read))
285
+
269
286
let expand_pform_gen ~(context : Context.t ) ~bindings ~dir ~source
270
287
(pform : Pform.t ) : expansion_result =
271
288
match Pform.Map. find bindings pform with
@@ -571,18 +588,23 @@ let expand_pform_gen ~(context : Context.t) ~bindings ~dir ~source
571
588
(let open Memo.Build.O in
572
589
let + b = Artifacts.Bin. binary_available t.bin_artifacts_host s in
573
590
b |> string_of_bool |> string ))
574
- | Read ->
575
- let path = relative ~source dir s in
576
- Direct
577
- (With (Action_builder. map (Action_builder. contents path) ~f: string ))
591
+ | Read -> expand_read_macro ~dir ~source s ~read: Io. read_file ~pack: string
578
592
| Read_lines ->
579
- let path = relative ~source dir s in
580
- Direct
581
- (With (Action_builder. map (Action_builder. lines_of path) ~f: strings))
593
+ expand_read_macro ~dir ~source s ~read: Io. lines_of_file ~pack: strings
582
594
| Read_strings ->
583
- let path = relative ~source dir s in
584
- Direct
585
- (With (Action_builder. map (Action_builder. strings path) ~f: strings))))
595
+ expand_read_macro ~dir ~source s ~read: Io. lines_of_file
596
+ ~pack: (fun lines ->
597
+ strings
598
+ (List. map lines ~f: (fun line ->
599
+ match Scanf. unescaped line with
600
+ | Error () ->
601
+ User_error. raise
602
+ ~loc: (Loc. in_file (relative ~source dir s))
603
+ [ Pp. textf
604
+ " This file must be a list of lines escaped using \
605
+ OCaml's conventions"
606
+ ]
607
+ | Ok s -> s)))))
586
608
587
609
(* Make sure to delay exceptions *)
588
610
let expand_pform_gen ~context ~bindings ~dir ~source pform =
0 commit comments