Skip to content

Commit

Permalink
refactor(engine): remove [Dune_sexp] from the engine (ocaml#10826)
Browse files Browse the repository at this point in the history
Dune_sexp belongs to dune's frontend and the engine has no business
relying on it.

This is a change we've done internally, so this is just syncing.

Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg authored and anmonteiro committed Nov 17, 2024
1 parent 8192aec commit 9799585
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 63 deletions.
58 changes: 53 additions & 5 deletions bin/print_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ let rec encode : Action.For_shell.t -> Dune_lang.t =
| With_accepted_exit_codes (pred, t) ->
List
[ atom "with-accepted-exit-codes"
; Predicate_lang.encode Dune_lang.Encoder.int pred
; Predicate_lang.encode Dune_sexp.Encoder.int pred
; encode t
]
| Dynamic_run (a, xs) -> List (atom "run_dynamic" :: program a :: List.map xs ~f:string)
Expand Down Expand Up @@ -103,22 +103,70 @@ let rec encode : Action.For_shell.t -> Dune_lang.t =
| Extension ext -> List [ atom "ext"; Dune_sexp.Quoted_string (Sexp.to_string ext) ]
;;

let encode_path p =
let make constr arg =
Dune_sexp.List [ Dune_sexp.atom constr; Dune_sexp.atom_or_quoted_string arg ]
in
let open Path in
match p with
| In_build_dir p -> make "In_build_dir" (Path.Build.to_string p)
| In_source_tree p -> make "In_source_tree" (Path.Source.to_string p)
| External p -> make "External" (Path.External.to_string p)
;;

let encode_file_selector file_selector =
let open Dune_sexp.Encoder in
let module File_selector = Dune_engine.File_selector in
let dir = File_selector.dir file_selector in
let predicate = File_selector.predicate file_selector in
let only_generated_files = File_selector.only_generated_files file_selector in
record
[ "dir", encode_path dir
; "predicate", Predicate_lang.Glob.encode predicate
; "only_generated_files", bool only_generated_files
]
;;

let encode_alias alias =
let open Dune_sexp.Encoder in
let dir = Dune_engine.Alias.dir alias in
let name = Dune_engine.Alias.name alias in
record
[ "dir", encode_path (Path.build dir)
; "name", Dune_sexp.atom_or_quoted_string (Dune_util.Alias_name.to_string name)
]
;;

let encode_dep_set deps =
Dune_sexp.List
(Dep.Set.to_list_map
deps
~f:
(let open Dune_sexp.Encoder in
function
| File_selector g -> pair string encode_file_selector ("glob", g)
| Env e -> pair string string ("Env", e)
| File f -> pair string encode_path ("File", f)
| Alias a -> pair string encode_alias ("Alias", a)
| Universe -> string "Universe"))
;;

let print_rule_sexp ppf (rule : Dune_engine.Reflection.Rule.t) =
let sexp_of_action action = Action.for_shell action |> encode in
let paths ps =
Dune_lang.Encoder.list
Dune_sexp.Encoder.list
(fun p ->
Path.Build.relative rule.targets.root p
|> Path.Build.to_string
|> Dune_sexp.atom_or_quoted_string)
(Filename.Set.to_list ps)
in
let sexp =
Dune_lang.Encoder.record
Dune_sexp.Encoder.record
(List.concat
[ [ "deps", Dep.Set.encode rule.deps
[ [ "deps", encode_dep_set rule.deps
; ( "targets"
, Dune_lang.Encoder.record
, Dune_sexp.Encoder.record
[ "files", paths rule.targets.files
; "directories", paths rule.targets.dirs
] )
Expand Down
12 changes: 11 additions & 1 deletion src/dune_config_file/dune_config_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ module Dune_config = struct
module Sandboxing_preference = struct
type t = Sandbox_mode.t list

let decode = repeat Sandbox_mode.decode
let decode : Sandbox_mode.t Dune_sexp.Decoder.t =
let open Dune_sexp.Decoder in
enum
[ "none", None
; "symlink", Some Sandbox_mode.Symlink
; "copy", Some Copy
; "hardlink", Some Hardlink
]
;;

let decode = repeat decode
end

module Cache = struct
Expand Down
7 changes: 0 additions & 7 deletions src/dune_engine/alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ open Import
module Name = struct
include Dune_util.Alias_name

let encode s = Dune_sexp.Encoder.string (to_string s)

let of_string s =
match of_string_opt s with
| Some s -> s
Expand Down Expand Up @@ -96,11 +94,6 @@ let register_as_standard name =

let default = make_standard Name.default

let encode { dir; name } =
let open Dune_sexp.Encoder in
record [ "dir", Dpath.encode (Path.build dir); "name", Name.encode name ]
;;

let get_ctx (path : Path.Build.t) =
match Path.Build.extract_first_component path with
| None -> None
Expand Down
1 change: 0 additions & 1 deletion src/dune_engine/alias.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ val name : t -> Name.t

val dir : t -> Path.Build.t
val to_dyn : t -> Dyn.t
val encode : t Dune_sexp.Encoder.t
val of_user_written_path : loc:Loc.t -> Path.t -> t
val fully_qualified_name : t -> Path.Build.t
val default : dir:Path.Build.t -> t
Expand Down
17 changes: 7 additions & 10 deletions src/dune_engine/dep.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@ module T = struct
| Universe, Universe -> Ordering.Eq
;;

let encode t =
let open Dune_sexp.Encoder in
let to_dyn t =
let open Dyn in
match t with
| File_selector g -> pair string File_selector.encode ("glob", g)
| Env e -> pair string string ("Env", e)
| File f -> pair string Dpath.encode ("File", f)
| Alias a -> pair string Alias.encode ("Alias", a)
| Universe -> string "Universe"
| File_selector g -> variant "File_selector" [ File_selector.to_dyn g ]
| Env e -> variant "Env" [ string e ]
| File f -> variant "File" [ Path.to_dyn f ]
| Alias a -> variant "Alias" [ Alias.to_dyn a ]
| Universe -> variant "Universe" []
;;

let to_dyn t = Dyn.String (Dune_sexp.to_string (encode t))
end

include T
Expand Down Expand Up @@ -254,7 +252,6 @@ module Set = struct
let of_files l = of_list_map l ~f:file
let of_files_set = Path.Set.fold ~init:empty ~f:(fun f acc -> add acc (file f))
let add_paths t paths = Path.Set.fold paths ~init:t ~f:(fun p set -> add set (File p))
let encode t = Dune_sexp.Encoder.list encode (to_list t)

(* This is to force the rules to be loaded for directories without files when
depending on [(source_tree x)]. Otherwise, we wouldn't clean up stale
Expand Down
1 change: 0 additions & 1 deletion src/dune_engine/dep.mli
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ module Set : sig

val of_files : Path.t list -> t
val of_files_set : Path.Set.t -> t
val encode : t -> Dune_sexp.t
val add_paths : t -> Path.Set.t -> t
val digest : t -> Digest.t
end
Expand Down
14 changes: 0 additions & 14 deletions src/dune_engine/dpath.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,3 @@ let analyse_dir (fn : Path.t) =
;;

type t = Path.t

let encode p =
(* CR rgrinberg: only reason this lives here is to implement
[$ dune rules]. Seems like it should just live there along with all the
other encoders in the engine. *)
let make constr arg =
Dune_sexp.List [ Dune_sexp.atom constr; Dune_sexp.atom_or_quoted_string arg ]
in
let open Path in
match p with
| In_build_dir p -> make "In_build_dir" (Path.Build.to_string p)
| In_source_tree p -> make "In_source_tree" (Path.Source.to_string p)
| External p -> make "External" (Path.External.to_string p)
;;
2 changes: 0 additions & 2 deletions src/dune_engine/dpath.mli
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ val describe_path : Path.t -> string

type t = Path.t

val encode : Path.t Dune_sexp.Encoder.t

module Build : sig
type t = Path.Build.t

Expand Down
1 change: 0 additions & 1 deletion src/dune_engine/dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
dune_async_io
threads.posix
predicate_lang
dune_sexp
dune_cache
dune_cache_storage
dune_glob
Expand Down
10 changes: 1 addition & 9 deletions src/dune_engine/file_selector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type t =

let dir t = t.dir
let only_generated_files t = t.only_generated_files
let predicate t = t.predicate

let digest_exn { dir; predicate; only_generated_files } =
Digest.generic (dir, Predicate_lang.Glob.digest_exn predicate, only_generated_files)
Expand All @@ -37,15 +38,6 @@ let to_dyn { dir; predicate; only_generated_files } =
]
;;

let encode { dir; predicate; only_generated_files } =
let open Dune_sexp.Encoder in
record
[ "dir", Dpath.encode dir
; "predicate", Predicate_lang.Glob.encode predicate
; "only_generated_files", bool only_generated_files
]
;;

let equal x y = compare x y = Eq

let hash { dir; predicate; only_generated_files } =
Expand Down
2 changes: 1 addition & 1 deletion src/dune_engine/file_selector.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type t
val dir : t -> Path.t
val only_generated_files : t -> bool
val of_glob : dir:Path.t -> Glob.t -> t
val predicate : t -> Predicate_lang.Glob.t

val of_predicate_lang
: dir:Path.t
Expand All @@ -18,7 +19,6 @@ val of_predicate_lang
val equal : t -> t -> bool
val hash : t -> int
val compare : t -> t -> Ordering.t
val encode : t Dune_sexp.Encoder.t

(** [to_dyn] is used as a marshallable representation of [t] (to compute
digests), so it must be injective *)
Expand Down
10 changes: 0 additions & 10 deletions src/dune_engine/sandbox_mode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,6 @@ let symlink = Some Symlink
let copy = Some Copy
let hardlink = Some Hardlink

let decode =
let open Dune_sexp.Decoder in
enum
[ "none", None
; "symlink", Some Symlink
; "copy", Some Copy
; "hardlink", Some Hardlink
]
;;

let to_string = function
| None -> "none"
| Some Symlink -> "symlink"
Expand Down
1 change: 0 additions & 1 deletion src/dune_engine/sandbox_mode.mli
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,5 @@ val none : t
val symlink : t
val copy : t
val hardlink : t
val decode : t Dune_sexp.Decoder.t
val to_string : t -> string
val to_dyn : t -> Dyn.t

0 comments on commit 9799585

Please sign in to comment.