Skip to content

Commit 7132e6e

Browse files
authored
Merge pull request #1079 from rgrinberg/opaque
Opaque Mode
2 parents b05e285 + 4cbd698 commit 7132e6e

File tree

14 files changed

+69
-36
lines changed

14 files changed

+69
-36
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ next
2424
- Add `(staged_pps ...)` to support staged ppx rewriters such as ones
2525
using the OCaml typer like `ppx_import` (#1080, fix #193, @diml)
2626

27+
- Use `-opaque` in the `dev` profile. This option trades off binary quality for
28+
compilation speed when compiling .cmx files. (#1079, fix #1058, @rgrinberg)
29+
2730
1.0.1 (19/07/2018)
2831
------------------
2932

src/compilation_context.ml

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module SC = Super_context
55
module Includes = struct
66
type t = string list Arg_spec.t Cm_kind.Dict.t
77

8-
let make sctx ~requires : _ Cm_kind.Dict.t =
8+
let make sctx ~opaque ~requires : _ Cm_kind.Dict.t =
99
match requires with
1010
| Error exn -> Cm_kind.Dict.make_all (Arg_spec.Dyn (fun _ -> raise exn))
1111
| Ok libs ->
@@ -18,15 +18,25 @@ module Includes = struct
1818
(SC.Libs.file_deps sctx libs ~ext:".cmi")
1919
]
2020
in
21-
let cmi_and_cmx_includes =
22-
Arg_spec.S [ iflags
23-
; Hidden_deps
24-
(SC.Libs.file_deps sctx libs ~ext:".cmi-and-.cmx")
25-
]
21+
let cmx_includes =
22+
Arg_spec.S
23+
[ iflags
24+
; Hidden_deps
25+
( if opaque then
26+
List.map libs ~f:(fun lib ->
27+
(lib, if Lib.is_local lib then
28+
".cmi"
29+
else
30+
".cmi-and-.cmx"))
31+
|> SC.Libs.file_deps_with_exts sctx
32+
else
33+
SC.Libs.file_deps sctx libs ~ext:".cmi-and-.cmx"
34+
)
35+
]
2636
in
2737
{ cmi = cmi_includes
2838
; cmo = cmi_includes
29-
; cmx = cmi_and_cmx_includes
39+
; cmx = cmx_includes
3040
}
3141

3242
let empty =
@@ -47,6 +57,7 @@ type t =
4757
; includes : Includes.t
4858
; preprocessing : Preprocessing.t
4959
; no_keep_locs : bool
60+
; opaque : bool
5061
}
5162

5263
let super_context t = t.super_context
@@ -62,12 +73,14 @@ let requires t = t.requires
6273
let includes t = t.includes
6374
let preprocessing t = t.preprocessing
6475
let no_keep_locs t = t.no_keep_locs
76+
let opaque t = t.opaque
6577

6678
let context t = Super_context.context t.super_context
6779

6880
let create ~super_context ~scope ~dir ?(dir_kind=File_tree.Dune_file.Kind.Dune)
6981
?(obj_dir=dir) ~modules ?alias_module ?lib_interface_module ~flags
70-
~requires ?(preprocessing=Preprocessing.dummy) ?(no_keep_locs=false) () =
82+
~requires ?(preprocessing=Preprocessing.dummy) ?(no_keep_locs=false)
83+
~opaque () =
7184
{ super_context
7285
; scope
7386
; dir
@@ -78,9 +91,10 @@ let create ~super_context ~scope ~dir ?(dir_kind=File_tree.Dune_file.Kind.Dune)
7891
; lib_interface_module
7992
; flags
8093
; requires
81-
; includes = Includes.make super_context ~requires
94+
; includes = Includes.make super_context ~opaque ~requires
8295
; preprocessing
8396
; no_keep_locs
97+
; opaque
8498
}
8599

86100
let for_alias_module t =

src/compilation_context.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ val create
2525
-> requires : Lib.t list Or_exn.t
2626
-> ?preprocessing : Preprocessing.t
2727
-> ?no_keep_locs : bool
28+
-> opaque : bool
2829
-> unit
2930
-> t
3031

@@ -45,3 +46,4 @@ val requires : t -> Lib.t list Or_exn.t
4546
val includes : t -> string list Arg_spec.t Cm_kind.Dict.t
4647
val preprocessing : t -> Preprocessing.t
4748
val no_keep_locs : t -> bool
49+
val opaque : t -> bool

src/gen_rules.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module Gen(P : Install_rules.Params) = struct
1717
let sctx = P.sctx
1818
let ctx = SC.context sctx
1919

20+
let opaque = ctx.profile = "dev" && ctx.version >= (4, 03, 0)
21+
2022
(* +-----------------------------------------------------------------+
2123
| Library stuff |
2224
+-----------------------------------------------------------------+ *)
@@ -199,6 +201,7 @@ module Gen(P : Install_rules.Params) = struct
199201
~requires
200202
~preprocessing:pp
201203
~no_keep_locs:lib.no_keep_locs
204+
~opaque
202205
in
203206

204207
let dep_graphs = Ocamldep.rules cctx in
@@ -520,6 +523,7 @@ module Gen(P : Install_rules.Params) = struct
520523
~flags
521524
~requires
522525
~preprocessing:pp
526+
~opaque
523527
in
524528

525529
Exe.build_and_link_many cctx

src/inline_tests.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ include Sub_system.Register_end_point(
233233
~scope
234234
~dir:inline_test_dir
235235
~modules
236+
~opaque:false
236237
~requires:runner_libs
237238
~flags:(Ocaml_flags.of_list ["-w"; "-24"]);
238239
in

src/module_compilation.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ let build_cm cctx ?sandbox ?(dynlink=true) ~dep_graphs ~cm_kind (m : Module.t) =
5858
| Cmi | Cmo -> other_targets
5959
in
6060
let dep_graph = Ml_kind.Dict.get dep_graphs ml_kind in
61+
let opaque = CC.opaque cctx in
6162
let other_cm_files =
6263
Build.dyn_paths
6364
(Ocamldep.Dep_graph.deps_of dep_graph m >>^ fun deps ->
6465
List.concat_map deps
6566
~f:(fun m ->
6667
let deps = [Module.cm_file_unsafe m ~obj_dir Cmi] in
67-
if Module.has_impl m && cm_kind = Cmx then
68+
if Module.has_impl m && cm_kind = Cmx && not opaque then
6869
Module.cm_file_unsafe m ~obj_dir Cmx :: deps
6970
else
7071
deps))
@@ -86,8 +87,8 @@ let build_cm cctx ?sandbox ?(dynlink=true) ~dep_graphs ~cm_kind (m : Module.t) =
8687
let in_dir = Target.file dir target in
8788
SC.add_rule sctx (Build.symlink ~src:in_obj_dir ~dst:in_dir))
8889
end;
89-
let opaque =
90-
if cm_kind = Cmi && not (Module.has_impl m) && ctx.version >= (4, 03, 0) then
90+
let opaque_arg =
91+
if opaque && cm_kind = Cmi then
9192
Arg_spec.A "-opaque"
9293
else
9394
As []
@@ -122,7 +123,7 @@ let build_cm cctx ?sandbox ?(dynlink=true) ~dep_graphs ~cm_kind (m : Module.t) =
122123
; Cm_kind.Dict.get (CC.includes cctx) cm_kind
123124
; As extra_args
124125
; if dynlink || cm_kind <> Cmx then As [] else A "-nodynlink"
125-
; A "-no-alias-deps"; opaque
126+
; A "-no-alias-deps"; opaque_arg
126127
; (match CC.alias_module cctx with
127128
| None -> S []
128129
| Some (m : Module.t) ->

src/super_context.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -706,14 +706,19 @@ module Libs = struct
706706
(lib_files_alias ~dir ~name:(Library.best_name lib) ~ext))
707707
|> Path.Set.of_list)
708708

709+
let file_deps_of_lib t (lib : Lib.t) ~ext =
710+
if Lib.is_local lib then
711+
Alias.stamp_file
712+
(lib_files_alias ~dir:(Lib.src_dir lib) ~name:(Lib.name lib) ~ext)
713+
else
714+
Build_system.stamp_file_for_files_of t.build_system
715+
~dir:(Lib.obj_dir lib) ~ext
716+
717+
let file_deps_with_exts t lib_exts =
718+
List.rev_map lib_exts ~f:(fun (lib, ext) -> file_deps_of_lib t lib ~ext)
719+
709720
let file_deps t libs ~ext =
710-
List.rev_map libs ~f:(fun (lib : Lib.t) ->
711-
if Lib.is_local lib then
712-
Alias.stamp_file
713-
(lib_files_alias ~dir:(Lib.src_dir lib) ~name:(Lib.name lib) ~ext)
714-
else
715-
Build_system.stamp_file_for_files_of t.build_system
716-
~dir:(Lib.obj_dir lib) ~ext)
721+
List.rev_map libs ~f:(file_deps_of_lib t ~ext)
717722
end
718723

719724
module Deps = struct

src/super_context.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ module Libs : sig
197197
all the files with extension [ext] of libraries [libs]. *)
198198
val file_deps : t -> Lib.L.t -> ext:string -> Path.t list
199199

200+
val file_deps_with_exts : t -> (Lib.t * string) list -> Path.t list
201+
200202
(** Setup the alias that depends on all files with a given extension
201203
for a library *)
202204
val setup_file_deps_alias

src/utop.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ let setup sctx ~dir ~(libs : Library.t list) ~scope =
7373
~scope
7474
~dir:utop_exe_dir
7575
~modules
76+
~opaque:false
7677
~requires
7778
~flags:(Ocaml_flags.append_common
7879
(Ocaml_flags.default ~profile:(Super_context.profile sctx))

test/blackbox-tests/test-cases/intf-only/run.t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ Successes:
1010
ocamlc .foo.objs/foo__Intf.{cmi,cmti}
1111
ocamlc .foo.objs/foo.{cmi,cmo,cmt}
1212
ocamlc test/.bar.objs/bar.{cmi,cmo,cmt}
13-
ocamlc test/bar.cma
14-
ocamlopt .foo.objs/foo.{cmx,o}
1513
ocamlopt test/.bar.objs/bar.{cmx,o}
1614
ocamlopt test/bar.{a,cmxa}
1715
ocamlopt test/bar.cmxs
18-
ocamlc foo.cma
16+
ocamlopt .foo.objs/foo.{cmx,o}
1917
ocamlopt foo.{a,cmxa}
2018
ocamlopt foo.cmxs
19+
ocamlc foo.cma
20+
ocamlc test/bar.cma
2121

2222
Errors:
2323

0 commit comments

Comments
 (0)