Skip to content

Commit

Permalink
refactor: move [Merge_files_into] into rules (#10797)
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
  • Loading branch information
rgrinberg authored Aug 4, 2024
1 parent 16328a3 commit bc04f9e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 39 deletions.
7 changes: 0 additions & 7 deletions bin/print_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ let rec encode : Action.For_shell.t -> Dune_lang.t =
List [ atom "diff"; path file1; target file2 ]
| Diff { optional = true; file1; file2; mode = _ } ->
List [ atom "diff?"; path file1; target file2 ]
| Merge_files_into (srcs, extras, into) ->
List
[ atom "merge-files-into"
; List (List.map ~f:path srcs)
; List (List.map ~f:string extras)
; target into
]
| Pipe (outputs, l) ->
List (atom (sprintf "pipe-%s" (Outputs.to_string outputs)) :: List.map l ~f:encode)
| Extension ext -> List [ atom "ext"; ext ]
Expand Down
3 changes: 0 additions & 3 deletions src/dune_engine/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ let fold_one_step t ~init:acc ~f =
| Remove_tree _
| Mkdir _
| Diff _
| Merge_files_into _
| Extension _ -> acc
;;

Expand Down Expand Up @@ -246,7 +245,6 @@ let rec is_dynamic = function
| Remove_tree _
| Diff _
| Mkdir _
| Merge_files_into _
| Extension _ -> false
;;

Expand Down Expand Up @@ -297,7 +295,6 @@ let is_useful_to memoize =
| Remove_tree _ -> false
| Diff _ -> true
| Mkdir _ -> false
| Merge_files_into _ -> true
| Run _ -> true
| Dynamic_run _ -> true
| System _ -> true
Expand Down
14 changes: 0 additions & 14 deletions src/dune_engine/action_exec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -481,20 +481,6 @@ let rec exec t ~display ~ectx ~eenv : done_or_more_deps Produce.t =
Fiber.return ())
in
Done)
| Merge_files_into (sources, extras, target) ->
let+ () =
maybe_async (fun () ->
let lines =
List.fold_left
sources
~init:(String.Set.of_list extras)
~f:(fun set source_path ->
Io.lines_of_file source_path |> String.Set.of_list |> String.Set.union set)
in
let target = Path.build target in
Io.write_lines target (String.Set.to_list lines))
in
Done
| Pipe (outputs, l) -> exec_pipe ~display ~ectx ~eenv outputs l
| Extension (module A) ->
let+ () =
Expand Down
1 change: 0 additions & 1 deletion src/dune_engine/action_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ module type Ast = sig
| Remove_tree of target
| Mkdir of target
| Diff of (path, target) Diff.t
| Merge_files_into of path list * string list * target
| Pipe of Outputs.t * t list
| Extension of ext
end
Expand Down
5 changes: 0 additions & 5 deletions src/dune_engine/action_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ module Make (Src : Action_intf.Ast) (Dst : Action_intf.Ast) = struct
| Mkdir x -> Mkdir (f_target ~dir x)
| Diff ({ file1; file2; _ } as diff) ->
Diff { diff with file1 = f_path ~dir file1; file2 = f_target ~dir file2 }
| Merge_files_into (sources, extras, target) ->
Merge_files_into
( List.map sources ~f:(f_path ~dir)
, List.map extras ~f:(f_string ~dir)
, f_target ~dir target )
| Pipe (outputs, l) -> Pipe (outputs, List.map l ~f:(fun t -> f t ~dir))
| Extension ext -> Extension (f_ext ~dir ext)
;;
Expand Down
8 changes: 0 additions & 8 deletions src/dune_engine/action_to_sh.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ let simplify act =
:: acc
| Diff { optional = false; file1; file2; mode = _ } ->
Run ("diff", [ file1; file2 ]) :: acc
| Merge_files_into (srcs, extras, target) ->
Sh
(Printf.sprintf
"{ echo -ne %s; cat %s; } | sort -u > %s"
(Filename.quote (List.map extras ~f:(sprintf "%s\n") |> String.concat ~sep:""))
(String.quote_list_for_shell srcs)
(String.quote_for_shell target))
:: acc
| Pipe (outputs, l) -> Pipe (List.map ~f:block l, outputs) :: acc
| Extension _ -> Sh "# extensions are not supported" :: acc
and block act =
Expand Down
58 changes: 57 additions & 1 deletion src/dune_rules/ocamldep.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,62 @@ end

open Modules_data

module Merge_files_into = struct
module Spec = struct
type ('src, 'dst) t = 'src list * string list * 'dst

let name = "merge_files_into"
let version = 1
let is_useful_to ~memoize:_ = true

let bimap (paths, extras, dst) path target =
List.map paths ~f:path, extras, target dst
;;

let encode
(type src dst)
((sources, extras, target) : (src, dst) t)
(input : src -> Dune_sexp.t)
(output : dst -> Dune_sexp.t)
: Dune_sexp.t
=
let open Dune_sexp in
List
[ atom_or_quoted_string name
; List (List.map sources ~f:input)
; List (List.map ~f:atom_or_quoted_string extras)
; output target
]
;;

let action (sources, extras, target) ~ectx:_ ~eenv:_ =
Async.async (fun () ->
let lines =
List.fold_left
sources
~init:(String.Set.of_list extras)
~f:(fun set source_path ->
Io.lines_of_file source_path |> String.Set.of_list |> String.Set.union set)
in
let target = Path.build target in
Io.write_lines target (String.Set.to_list lines))
;;
end

let action sources extras target =
let module M = struct
type path = Path.t
type target = Path.Build.t

module Spec = Spec

let v = sources, extras, target
end
in
Dune_engine.Action.Extension (module M)
;;
end

let parse_module_names ~dir ~(unit : Module.t) ~modules words =
List.concat_map words ~f:(fun m ->
let m = Module_name.of_string m in
Expand Down Expand Up @@ -135,7 +191,7 @@ let deps_of
(let+ sources, extras = paths in
(sources, extras), sources)
in
Action.Merge_files_into (sources, extras, all_deps_file))
Merge_files_into.action sources extras all_deps_file)
in
Action_builder.With_targets.map ~f:Action.Full.make produce_all_deps
|> Super_context.add_rule sctx ~dir
Expand Down

0 comments on commit bc04f9e

Please sign in to comment.