Skip to content

Commit

Permalink
Add name mangling for dune executables
Browse files Browse the repository at this point in the history
Executables generated by dune are mangled by default. Executables that
are written by the user are mangled starting from 2.0. The user can turn
off this option per project with the `wrapped_exes` option.

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
  • Loading branch information
rgrinberg committed Jul 7, 2019
1 parent 6e29c28 commit f6aa4db
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/cinaps.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let gen_rules sctx t ~dir ~scope ~dir_kind =
~dir_kind
in
let modules =
Modules.exe modules
Modules.exe_unwrapped modules
|> Modules.map_user_written ~f:(Preprocessing.pp_module preprocess)
in

Expand Down
15 changes: 12 additions & 3 deletions src/dir_contents.ml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ end = struct
let make_modules sctx (d : _ Dir_with_dune.t) ~modules =
let scope = d.scope in
let libs, exes =
let modules_exe =
let project = Scope.project scope in
let name = Dune_project.name project in
let wrapped_exes = Dune_project.wrapped_exes project in
if wrapped_exes then
fun modules -> Modules.exe_wrapped ~name ~src_dir:d.ctx_dir ~modules
else
Modules.exe_unwrapped

in
List.filter_partition_map d.data ~f:(fun stanza ->
match (stanza : Stanza.t) with
| Library lib ->
Expand Down Expand Up @@ -272,8 +282,7 @@ end = struct
lib.private_modules)
in
Left ( lib
, let src_dir = Path.build src_dir in
Modules.lib ~lib ~src_dir ~modules ~main_module_name ~wrapped
, Modules.lib ~lib ~src_dir ~modules ~main_module_name ~wrapped
)
| Executables exes
| Tests { exes; _} ->
Expand All @@ -283,7 +292,7 @@ end = struct
~kind:Modules_field_evaluator.Exe_or_normal_lib
~private_modules:Ordered_set_lang.standard
in
Right (exes, Modules.exe modules)
Right (exes, modules_exe modules)
| _ -> Skip)
in
let libraries =
Expand Down
47 changes: 20 additions & 27 deletions src/dune_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ type t =
; extension_args : Univ_map.t
; parsing_context : Univ_map.t
; implicit_transitive_deps : bool
; wrapped_exes : bool
; dune_version : Syntax.Version.t
; allow_approx_merlin : bool
; generate_opam_files : bool
Expand Down Expand Up @@ -220,7 +221,7 @@ let to_dyn
; homepage ; documentation ; project_file ; parsing_context = _
; bug_reports ; maintainers
; extension_args = _; stanza_parser = _ ; packages
; implicit_transitive_deps ; dune_version
; implicit_transitive_deps ; wrapped_exes ; dune_version
; allow_approx_merlin ; generate_opam_files } =
let open Dyn.Encoder in
record
Expand All @@ -240,6 +241,7 @@ let to_dyn
(Package.Name.Map.to_list packages)
; "implicit_transitive_deps",
bool implicit_transitive_deps
; "wrapped_exes", bool wrapped_exes
; "dune_version", Syntax.Version.to_dyn dune_version
; "allow_approx_merlin", bool allow_approx_merlin
; "generate_opam_files", bool generate_opam_files
Expand Down Expand Up @@ -490,31 +492,7 @@ let interpret_lang_and_extensions ~(lang : Lang.Instance.t)
(parsing_context, stanza_parser, extension_args)

let key =
Univ_map.Key.create ~name:"dune-project"
(fun { name; root; version; project_file; source
; license; authors; homepage; documentation ; bug_reports ; maintainers
; stanza_parser = _; packages = _ ; extension_args = _
; parsing_context ; implicit_transitive_deps ; dune_version
; allow_approx_merlin ; generate_opam_files } ->
let open Dyn.Encoder in
record
[ "name", Name.to_dyn name
; "root", Path.Source.to_dyn root
; "license", (option string) license
; "authors", (list string) authors
; "source", Dyn.Encoder.(option Source_kind.to_dyn) source
; "version", (option string) version
; "homepage", (option string) homepage
; "documentation", (option string) documentation
; "bug_reports", (option string) bug_reports
; "maintainers", (list string) maintainers
; "project_file", Project_file.to_dyn project_file
; "parsing_context", Univ_map.to_dyn parsing_context
; "implicit_transitive_deps", bool implicit_transitive_deps
; "dune_version", Syntax.Version.to_dyn dune_version
; "allow_approx_merlin", bool allow_approx_merlin
; "generate_opam_files", bool generate_opam_files
])
Univ_map.Key.create ~name:"dune-project" to_dyn

let set t = Dune_lang.Decoder.set key t
let get_exn () =
Expand All @@ -529,6 +507,9 @@ let filename = "dune-project"
let implicit_transitive_deps_default ~(lang : Lang.Instance.t) =
lang.version < (2, 0)

let wrapped_exes_default ~(lang : Lang.Instance.t) =
lang.version >= (2, 0)

let anonymous = lazy (
let lang = get_dune_lang () in
let name = Name.anonymous_root in
Expand All @@ -543,6 +524,7 @@ let anonymous = lazy (
interpret_lang_and_extensions ~lang ~explicit_extensions:[] ~project_file
in
let implicit_transitive_deps = implicit_transitive_deps_default ~lang in
let wrapped_exes = wrapped_exes_default ~lang in
{ name = name
; packages = Package.Name.Map.empty
; root = Path.Source.root
Expand All @@ -555,6 +537,7 @@ let anonymous = lazy (
; authors = []
; version = None
; implicit_transitive_deps
; wrapped_exes
; stanza_parser
; project_file
; extension_args
Expand Down Expand Up @@ -618,6 +601,9 @@ let parse ~dir ~lang ~opam_packages ~file =
and+ implicit_transitive_deps =
field_o_b "implicit_transitive_deps"
~check:(Syntax.since Stanza.syntax (1, 7))
and+ wrapped_exes =
field_o_b "wrapped_exes"
~check:(Syntax.since Stanza.syntax (1, 11))
and+ allow_approx_merlin =
field_o_b "allow_approximate_merlin"
~check:(Syntax.since Stanza.syntax (1, 9))
Expand Down Expand Up @@ -701,9 +687,12 @@ let parse ~dir ~lang ~opam_packages ~file =
Option.value implicit_transitive_deps
~default:(implicit_transitive_deps_default ~lang)
in
let wrapped_exes =
Option.value wrapped_exes ~default:(wrapped_exes_default ~lang) in
let allow_approx_merlin =
Option.value ~default:false allow_approx_merlin in
let generate_opam_files = Option.value ~default:false generate_opam_files in
let generate_opam_files =
Option.value ~default:false generate_opam_files in
{ name
; root = dir
; version
Expand All @@ -720,6 +709,7 @@ let parse ~dir ~lang ~opam_packages ~file =
; extension_args
; parsing_context
; implicit_transitive_deps
; wrapped_exes
; dune_version = lang.version
; allow_approx_merlin
; generate_opam_files
Expand Down Expand Up @@ -765,6 +755,7 @@ let make_jbuilder_project ~dir opam_packages =
; dune_version = lang.version
; allow_approx_merlin = true
; generate_opam_files = false
; wrapped_exes = false
}

let load ~dir ~files =
Expand Down Expand Up @@ -824,3 +815,5 @@ let dune_version t = t.dune_version

let set_parsing_context t parser =
Dune_lang.Decoder.set_many t.parsing_context parser

let wrapped_exes t = t.wrapped_exes
2 changes: 2 additions & 0 deletions src/dune_project.mli
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,5 @@ val set_parsing_context : t -> 'a Dune_lang.Decoder.t -> 'a Dune_lang.Decoder.t
val implicit_transitive_deps : t -> bool

val dune_version : t -> Syntax.Version.t

val wrapped_exes : t -> bool
24 changes: 14 additions & 10 deletions src/inline_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ module Backend = struct
let f x = Lib_name.encode (Lib.name x.lib) in
((1, 0),
record_fields @@
[ field_l "runner_libraries" lib (Result.ok_exn t.runner_libraries)
; field_i "flags" Ordered_set_lang.Unexpanded.encode_and_upgrade
t.info.flags
; field_o "generate_runner" Action_dune_lang.encode_and_upgrade
(Option.map t.info.generate_runner ~f:snd)
; field_l "extends" f (Result.ok_exn t.extends)
])
[ field_l "runner_libraries" lib (Result.ok_exn t.runner_libraries)
; field_i "flags" Ordered_set_lang.Unexpanded.encode_and_upgrade
t.info.flags
; field_o "generate_runner" Action_dune_lang.encode_and_upgrade
(Option.map t.info.generate_runner ~f:snd)
; field_l "extends" f (Result.ok_exn t.extends)
])
end
include M
include Sub_system.Register_backend(M)
Expand Down Expand Up @@ -207,8 +207,10 @@ include Sub_system.Register_end_point(

let loc = lib.buildable.loc in

let lib_name = snd lib.name in

let inline_test_name =
sprintf "%s.inline-tests" (Lib_name.Local.to_string (snd lib.name))
sprintf "%s.inline-tests" (Lib_name.Local.to_string lib_name)
in

let inline_test_dir = Path.Build.relative dir ("." ^ inline_test_name) in
Expand All @@ -224,7 +226,9 @@ include Sub_system.Register_end_point(
Module.generated ~src_dir name
in

let modules = Modules.singleton main_module in
let modules =
Modules.singleton main_module ~purpose:"inline_tests"
~for_:(Some lib_name) in

let bindings =
Pform.Map.singleton "library-name"
Expand Down Expand Up @@ -366,6 +370,6 @@ include Sub_system.Register_end_point(
|> List.map ~f:(fun fn ->
A.diff ~optional:true
fn (Path.extend_basename fn ~suffix:".corrected"))))))))
end)
end)

let linkme = ()
4 changes: 3 additions & 1 deletion src/link_time_code_gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ let generate_and_compile_module cctx ~precompiled_cmi ~name:basename ~code
Ocaml_version.supports_opaque_for_mli
(Super_context.context sctx).version
in
let modules =
Modules.singleton ~purpose:"link_time_code_gen" ~for_:None module_ in
let cctx =
Compilation_context.create
~super_context:sctx
~expander:(Compilation_context.expander cctx)
~scope:(Compilation_context.scope cctx)
~dir_kind:(Compilation_context.dir_kind cctx)
~obj_dir
~modules:(Modules.singleton module_)
~modules
~requires_compile:requires
~requires_link:(lazy requires)
~flags:Ocaml_flags.empty
Expand Down
40 changes: 20 additions & 20 deletions src/menhir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -215,27 +215,27 @@ module Run (P : PARAMS) : sig end = struct
Module.of_source ~visibility:Public ~kind:Impl source
in

(* The following incantation allows the mock [.ml] file to be preprocessed
by the user-specified [ppx] rewriters. *)

let mock_module =
Preprocessing.pp_module_as
(Compilation_context.preprocessing cctx)
name
mock_module
~lint:false
in

let dep_graphs =
let modules = Modules.singleton mock_module in
Ocamldep.rules cctx ~modules
let modules =
(* The following incantation allows the mock [.ml] file to be preprocessed
by the user-specified [ppx] rewriters. *)

let mock_module =
Preprocessing.pp_module_as
(Compilation_context.preprocessing cctx)
name
mock_module
~lint:false
in
Modules.singleton ~purpose:"menhir" ~for_:None mock_module
in

Module_compilation.ocamlc_i
~dep_graphs
cctx
mock_module
~output:(inferred_mli base);
let dep_graphs = Ocamldep.rules cctx ~modules in

Modules.iter_no_vlib modules ~f:(fun m ->
Module_compilation.ocamlc_i
~dep_graphs
cctx
m
~output:(inferred_mli base));

(* 3. A second invocation of Menhir reads the inferred [.mli] file. *)

Expand Down
1 change: 1 addition & 0 deletions src/module.ml
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ let generated ~src_dir name =
source

let generated_alias ~src_dir name =
let src_dir = Path.build src_dir in
let t = generated ~src_dir name in
{ t with kind = Alias }

Expand Down
2 changes: 1 addition & 1 deletion src/module.mli
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ val set_src_dir : t -> src_dir:Path.t -> t
val generated : src_dir:Path.t -> Name.t -> t

(** Represent the generated alias module. *)
val generated_alias : src_dir:Path.t -> Name.t -> t
val generated_alias : src_dir:Path.Build.t -> Name.t -> t
Loading

0 comments on commit f6aa4db

Please sign in to comment.