Skip to content

Commit

Permalink
Use a dune-project option instead of a new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
voodoos committed Jan 10, 2023
1 parent 74f7692 commit 9dd68e1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 65 deletions.
20 changes: 20 additions & 0 deletions src/dune_engine/dune_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type t =
; accept_alternative_dune_file_name : bool
; generate_opam_files : bool
; use_standard_c_and_cxx_flags : bool option
; enable_project_indexation : bool
; file_key : File_key.t
; dialects : Dialect.DB.t
; explicit_js_mode : bool
Expand Down Expand Up @@ -182,6 +183,8 @@ let set_generate_opam_files generate_opam_files t =

let use_standard_c_and_cxx_flags t = t.use_standard_c_and_cxx_flags

let enable_project_indexation t = t.enable_project_indexation

let dialects t = t.dialects

let set_dialects dialects t = { t with dialects }
Expand All @@ -207,6 +210,7 @@ let to_dyn
; accept_alternative_dune_file_name
; generate_opam_files
; use_standard_c_and_cxx_flags
; enable_project_indexation
; file_key
; dialects
; explicit_js_mode
Expand Down Expand Up @@ -234,6 +238,7 @@ let to_dyn
, bool accept_alternative_dune_file_name )
; ("generate_opam_files", bool generate_opam_files)
; ("use_standard_c_and_cxx_flags", option bool use_standard_c_and_cxx_flags)
; ("enable_project_indexation", bool enable_project_indexation)
; ("file_key", string file_key)
; ("dialects", Dialect.DB.to_dyn dialects)
; ("explicit_js_mode", bool explicit_js_mode)
Expand Down Expand Up @@ -473,6 +478,8 @@ let expand_aliases_in_sandbox_default ~lang:_ = false
let use_standard_c_and_cxx_flags_default ~(lang : Lang.Instance.t) =
if lang.version >= (3, 0) then Some true else None

let enable_project_indexation_default ~lang:_ = false

let format_extension_key =
Extension.register Format_config.syntax Format_config.dparse_args
Format_config.to_dyn
Expand Down Expand Up @@ -511,6 +518,7 @@ let infer ~dir ?(info = Package.Info.empty) packages =
let executables_implicit_empty_intf =
executables_implicit_empty_intf_default ~lang
in
let enable_project_indexation = enable_project_indexation_default ~lang in
let explicit_js_mode = explicit_js_mode_default ~lang in
let strict_package_deps = strict_package_deps_default ~lang in
let cram = cram_default ~lang in
Expand All @@ -533,6 +541,7 @@ let infer ~dir ?(info = Package.Info.empty) packages =
; parsing_context
; generate_opam_files = false
; use_standard_c_and_cxx_flags = use_standard_c_and_cxx_flags_default ~lang
; enable_project_indexation
; file_key
; dialects = Dialect.DB.builtin
; explicit_js_mode
Expand Down Expand Up @@ -597,6 +606,7 @@ let encode : t -> Dune_lang.t list =
; accept_alternative_dune_file_name
; generate_opam_files
; use_standard_c_and_cxx_flags
; enable_project_indexation
; dialects
; explicit_js_mode
; format_config
Expand Down Expand Up @@ -656,6 +666,8 @@ let encode : t -> Dune_lang.t list =
(use_standard_c_and_cxx_flags_default ~lang))
then Some (constr "use_standard_c_and_cxx_flags" bool b)
else None)
; flag' "enable_project_indexation" enable_project_indexation
enable_project_indexation_default
; (if Bool.equal cram (cram_default ~lang) then None
else Some (constr "cram" Toggle.encode (Toggle.of_bool cram)))
; flag "expand_aliases_in_sandbox" expand_aliases_in_sandbox
Expand Down Expand Up @@ -752,6 +764,9 @@ let parse ~dir ~lang ~file ~dir_status =
and+ use_standard_c_and_cxx_flags =
field_o_b "use_standard_c_and_cxx_flags"
~check:(Dune_lang.Syntax.since Stanza.syntax (2, 8))
and+ enable_project_indexation =
field_o_b "enable_project_indexation"
~check:(Dune_lang.Syntax.since Stanza.syntax (3, 5))
and+ dialects =
multi_field "dialect"
(Dune_lang.Syntax.since Stanza.syntax (1, 11)
Expand Down Expand Up @@ -906,6 +921,10 @@ let parse ~dir ~lang ~file ~dir_status =
| None -> use_standard_c_and_cxx_flags_default ~lang
| some -> some
in
let enable_project_indexation =
Option.value enable_project_indexation
~default:(enable_project_indexation_default ~lang)
in
let cram =
match cram with
| None -> cram_default ~lang
Expand Down Expand Up @@ -962,6 +981,7 @@ let parse ~dir ~lang ~file ~dir_status =
; accept_alternative_dune_file_name
; generate_opam_files
; use_standard_c_and_cxx_flags
; enable_project_indexation
; dialects
; explicit_js_mode
; format_config
Expand Down
2 changes: 2 additions & 0 deletions src/dune_engine/dune_project.mli
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ val set_generate_opam_files : bool -> t -> t
compiler command line when building C stubs. *)
val use_standard_c_and_cxx_flags : t -> bool option

val enable_project_indexation : t -> bool

val dialects : t -> Dialect.DB.t

val set_dialects : Dialect.DB.t -> t -> t
Expand Down
117 changes: 54 additions & 63 deletions src/dune_rules/uideps.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@ open Import
module CC = Compilation_context
module SC = Super_context

let syntax =
let name = "project_indexation" in
let desc = "Enables project indexation" in
Dune_lang.Syntax.create ~name ~desc ~experimental:true
[ ((0, 1), `Since (3, 5)) ]

let key =
let decoder = Dune_lang.Decoder.return ((), []) in
Dune_project.Extension.register syntax decoder Unit.to_dyn

let activated project = Dune_project.is_extension_set project key
let activated project = Dune_project.enable_project_indexation project

let ocaml_uideps sctx ~dir =
Super_context.resolve_program ~loc:None ~dir sctx "ocaml-uideps"
Expand All @@ -38,58 +28,59 @@ let build_path cctx =
Lib_info.obj_dir info |> Obj_dir.byte_dir)

let cctx_rules cctx =
(* Indexing is performed by the external binary [ocaml-uideps] which performs
full shape reduction to compute the actual definition of all the elements in
the typedtree. This step is therefore dependent on all the cmts of those
definitions are used by all the cmts of modules in this cctx.
TODO: this large set of dependencies is bad for parallelism, if shape
reduction was only partial a that point it might speed things a lot. *)
let dir = CC.dir cctx in
let modules =
CC.modules cctx |> Modules.fold_no_vlib ~init:[] ~f:(fun x acc -> x :: acc)
in
let sctx = CC.super_context cctx in
let obj_dir = CC.obj_dir cctx in
let cm_kind = Lib_mode.Cm_kind.(Ocaml Cmi) in
let modules_with_cmts =
List.filter_map
~f:(fun module_ ->
Obj_dir.Module.cmt_file obj_dir ~ml_kind:Impl ~cm_kind module_
|> Option.map ~f:(fun cmt -> (module_, Path.build cmt)))
modules
in
let open Memo.O in
let* ocaml_uideps = ocaml_uideps sctx ~dir in
let context_dir =
CC.context cctx |> Context.name |> Context_name.build_dir |> Path.build
in
let* build_path = build_path cctx in
(* let all_cmts = List.map ~f:snd modules_with_cmts in *)
let intermediate_targets, intermediates =
List.fold_map modules_with_cmts ~init:[]
~f:(fun targets (_module_, for_cmt) ->
let fn = uideps_path_in_obj_dir ~for_cmt obj_dir in
let action =
Command.run ~dir:context_dir ocaml_uideps
[ A "process-cmt"
; A "-o"
; Target fn
; Dep for_cmt
; As (List.map ~f:Path.to_absolute_filename build_path)
(* TODO ulysse: these deps are incorrect. Fixing it might improve
performance by increasing parallelism. *)
(* ; Hidden_deps (Dep.Set.of_files all_cmts) *)
]
in
(Path.build fn :: targets, action))
in
let fn = uideps_path_in_obj_dir obj_dir in
let aggregate =
Command.run ~dir:context_dir ocaml_uideps
[ A "aggregate"; A "-o"; Target fn; Deps intermediate_targets ]
in
SC.add_rules sctx ~dir (aggregate :: intermediates)
let project = Compilation_context.scope cctx |> Scope.project in
if not @@ activated project then Memo.return ()
else
(* Indexing is performed by the external binary [ocaml-uideps] which performs
full shape reduction to compute the actual definition of all the elements in
the typedtree. This step is therefore dependent on all the cmts of those
definitions are used by all the cmts of modules in this cctx. *)
let dir = CC.dir cctx in
let modules =
CC.modules cctx
|> Modules.fold_no_vlib ~init:[] ~f:(fun x acc -> x :: acc)
in
let sctx = CC.super_context cctx in
let obj_dir = CC.obj_dir cctx in
let cm_kind = Lib_mode.Cm_kind.(Ocaml Cmi) in
let modules_with_cmts =
List.filter_map
~f:(fun module_ ->
Obj_dir.Module.cmt_file obj_dir ~ml_kind:Impl ~cm_kind module_
|> Option.map ~f:(fun cmt -> (module_, Path.build cmt)))
modules
in
let open Memo.O in
let* ocaml_uideps = ocaml_uideps sctx ~dir in
let context_dir =
CC.context cctx |> Context.name |> Context_name.build_dir |> Path.build
in
let* build_path = build_path cctx in
(* let all_cmts = List.map ~f:snd modules_with_cmts in *)
let intermediate_targets, intermediates =
List.fold_map modules_with_cmts ~init:[]
~f:(fun targets (_module_, for_cmt) ->
let fn = uideps_path_in_obj_dir ~for_cmt obj_dir in
let action =
Command.run ~dir:context_dir ocaml_uideps
[ A "process-cmt"
; A "-o"
; Target fn
; Dep for_cmt
; As (List.map ~f:Path.to_absolute_filename build_path)
(* TODO ulysse: these deps are incorrect. Fixing it might improve
performance by increasing parallelism. *)
(* ; Hidden_deps (Dep.Set.of_files all_cmts) *)
]
in
(Path.build fn :: targets, action))
in
let fn = uideps_path_in_obj_dir obj_dir in
let aggregate =
Command.run ~dir:context_dir ocaml_uideps
[ A "aggregate"; A "-o"; Target fn; Deps intermediate_targets ]
in
SC.add_rules sctx ~dir (aggregate :: intermediates)

let aggregate sctx ~dir ~target ~uideps =
let open Memo.O in
Expand Down
3 changes: 2 additions & 1 deletion test/blackbox-tests/test-cases/tuideps.t/dune-project
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(lang dune 3.5)
(implicit_transitive_deps false)
(using project_indexation 0.1)

(enable_project_indexation)
2 changes: 1 addition & 1 deletion test/blackbox-tests/test-cases/tuideps.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$ tree -a _build/default

$ dune clean
$ dune build
$ dune build @uideps

$ find . -name '*.uideps'
./_build/default/implicit-lib/.imp_lib.objs/.uideps
Expand Down

0 comments on commit 9dd68e1

Please sign in to comment.