2018-02-20 16:38:22 +00:00
|
|
|
open Import
|
|
|
|
open Build.O
|
|
|
|
|
2018-05-25 16:53:10 +00:00
|
|
|
module CC = Compilation_context
|
2018-02-20 16:38:22 +00:00
|
|
|
module SC = Super_context
|
|
|
|
|
|
|
|
module Program = struct
|
|
|
|
type t =
|
|
|
|
{ name : string
|
2018-03-03 18:03:37 +00:00
|
|
|
; main_module_name : Module.Name.t
|
2018-02-20 16:38:22 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
module Linkage = struct
|
|
|
|
type t =
|
|
|
|
{ mode : Mode.t
|
|
|
|
; ext : string
|
|
|
|
; flags : string list
|
|
|
|
}
|
|
|
|
|
|
|
|
let byte =
|
|
|
|
{ mode = Byte
|
|
|
|
; ext = ".bc"
|
|
|
|
; flags = []
|
|
|
|
}
|
|
|
|
|
|
|
|
let native =
|
|
|
|
{ mode = Native
|
|
|
|
; ext = ".exe"
|
|
|
|
; flags = []
|
|
|
|
}
|
|
|
|
|
|
|
|
let custom =
|
|
|
|
{ mode = Byte
|
|
|
|
; ext = ".exe"
|
|
|
|
; flags = ["-custom"]
|
|
|
|
}
|
|
|
|
|
|
|
|
let native_or_custom (context : Context.t) =
|
|
|
|
match context.ocamlopt with
|
|
|
|
| None -> custom
|
|
|
|
| Some _ -> native
|
|
|
|
|
|
|
|
let make ~mode ~ext ?(flags=[]) () =
|
|
|
|
{ mode
|
|
|
|
; ext
|
|
|
|
; flags
|
|
|
|
}
|
2018-03-12 11:40:53 +00:00
|
|
|
|
|
|
|
let o_flags = ["-output-complete-obj"]
|
2018-03-19 23:13:02 +00:00
|
|
|
let so_flags_windows = o_flags
|
|
|
|
let so_flags_unix = ["-output-complete-obj"; "-runtime-variant"; "_pic"]
|
2018-03-12 11:40:53 +00:00
|
|
|
|
|
|
|
let of_user_config (ctx : Context.t) (m : Jbuild.Executables.Link_mode.t) =
|
|
|
|
let wanted_mode : Mode.t =
|
|
|
|
match m.mode with
|
|
|
|
| Byte -> Byte
|
|
|
|
| Native -> Native
|
|
|
|
| Best -> Native
|
|
|
|
in
|
|
|
|
let real_mode : Mode.t =
|
|
|
|
match m.mode with
|
|
|
|
| Byte -> Byte
|
|
|
|
| Native -> Native
|
|
|
|
| Best -> if Option.is_some ctx.ocamlopt then Native else Byte
|
|
|
|
in
|
|
|
|
let ext =
|
|
|
|
match wanted_mode, m.kind with
|
|
|
|
| Byte , Exe -> ".bc"
|
|
|
|
| Native , Exe -> ".exe"
|
|
|
|
| Byte , Object -> ".bc" ^ ctx.ext_obj
|
|
|
|
| Native , Object -> ".exe" ^ ctx.ext_obj
|
|
|
|
| Byte , Shared_object -> ".bc" ^ ctx.ext_dll
|
|
|
|
| Native , Shared_object -> ctx.ext_dll
|
|
|
|
in
|
|
|
|
let flags =
|
|
|
|
match m.kind with
|
|
|
|
| Exe ->
|
|
|
|
if wanted_mode = Native && real_mode = Byte then
|
|
|
|
["-custom"]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
| Object -> o_flags
|
|
|
|
| Shared_object ->
|
2018-03-19 23:13:02 +00:00
|
|
|
let so_flags =
|
|
|
|
if ctx.os_type = "Win32" then
|
|
|
|
so_flags_windows
|
|
|
|
else
|
|
|
|
so_flags_unix
|
|
|
|
in
|
2018-03-12 11:40:53 +00:00
|
|
|
if real_mode = Native then
|
|
|
|
(* The compiler doesn't pass these flags in native mode. This
|
|
|
|
looks like a bug in the compiler. *)
|
|
|
|
List.concat_map ctx.native_c_libraries ~f:(fun flag ->
|
|
|
|
["-cclib"; flag])
|
|
|
|
@ so_flags
|
|
|
|
else
|
|
|
|
so_flags
|
|
|
|
in
|
|
|
|
{ ext
|
|
|
|
; mode = real_mode
|
|
|
|
; flags
|
|
|
|
}
|
2018-02-20 16:38:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
let link_exe
|
|
|
|
~name
|
|
|
|
~(linkage:Linkage.t)
|
|
|
|
~top_sorted_modules
|
|
|
|
?(link_flags=Build.arr (fun _ -> []))
|
|
|
|
?(js_of_ocaml=Jbuild.Js_of_ocaml.default)
|
2018-05-25 16:53:10 +00:00
|
|
|
cctx
|
2018-02-20 16:38:22 +00:00
|
|
|
=
|
2018-05-25 16:53:10 +00:00
|
|
|
let sctx = CC.super_context cctx in
|
|
|
|
let ctx = SC.context sctx in
|
|
|
|
let dir = CC.dir cctx in
|
|
|
|
let obj_dir = CC.obj_dir cctx in
|
|
|
|
let requires = CC.requires cctx in
|
2018-02-20 16:38:22 +00:00
|
|
|
let mode = linkage.mode in
|
|
|
|
let exe = Path.relative dir (name ^ linkage.ext) in
|
|
|
|
let compiler = Option.value_exn (Context.compiler ctx mode) in
|
|
|
|
let artifacts ~ext modules =
|
|
|
|
List.map modules ~f:(Module.obj_file ~obj_dir ~ext)
|
|
|
|
in
|
|
|
|
let modules_and_cm_files =
|
|
|
|
Build.memoize "cm files"
|
|
|
|
(top_sorted_modules >>^ fun modules ->
|
|
|
|
(modules,
|
|
|
|
artifacts modules ~ext:(Cm_kind.ext (Mode.cm_kind mode))))
|
|
|
|
in
|
|
|
|
let register_native_objs_deps build =
|
|
|
|
match mode with
|
|
|
|
| Byte -> build
|
|
|
|
| Native ->
|
|
|
|
build >>>
|
|
|
|
Build.dyn_paths (Build.arr (fun (modules, _) ->
|
|
|
|
artifacts modules ~ext:ctx.ext_obj))
|
|
|
|
in
|
|
|
|
SC.add_rule sctx
|
2018-03-30 21:59:43 +00:00
|
|
|
(Build.fanout3
|
2018-02-20 16:38:22 +00:00
|
|
|
(register_native_objs_deps modules_and_cm_files >>^ snd)
|
2018-05-25 16:53:10 +00:00
|
|
|
(Ocaml_flags.get (CC.flags cctx) mode)
|
2018-02-20 16:38:22 +00:00
|
|
|
link_flags
|
|
|
|
>>>
|
2018-03-30 21:59:43 +00:00
|
|
|
Build.of_result_map requires ~f:(fun libs ->
|
|
|
|
Build.paths (Lib.L.archive_files libs ~mode ~ext_lib:ctx.ext_lib))
|
2018-02-20 16:38:22 +00:00
|
|
|
>>>
|
|
|
|
Build.run ~context:ctx
|
|
|
|
(Ok compiler)
|
2018-03-30 21:59:43 +00:00
|
|
|
[ Dyn (fun (_, flags,_) -> As flags)
|
2018-02-20 16:38:22 +00:00
|
|
|
; A "-o"; Target exe
|
|
|
|
; As linkage.flags
|
2018-03-30 21:59:43 +00:00
|
|
|
; Dyn (fun (_, _, link_flags) -> As link_flags)
|
|
|
|
; Arg_spec.of_result_map requires ~f:(fun libs ->
|
2018-02-20 16:38:22 +00:00
|
|
|
Lib.L.link_flags libs ~mode ~stdlib_dir:ctx.stdlib_dir)
|
2018-03-30 21:59:43 +00:00
|
|
|
; Dyn (fun (cm_files, _, _) -> Deps cm_files)
|
2018-02-20 16:38:22 +00:00
|
|
|
]);
|
2018-03-12 11:40:53 +00:00
|
|
|
if linkage.ext = ".bc" then
|
2018-03-30 21:59:43 +00:00
|
|
|
let rules =
|
|
|
|
Js_of_ocaml_rules.build_exe sctx ~dir ~js_of_ocaml ~src:exe ~requires
|
2018-02-20 16:38:22 +00:00
|
|
|
in
|
2018-03-30 21:59:43 +00:00
|
|
|
let cm_and_flags =
|
|
|
|
Build.fanout
|
|
|
|
(modules_and_cm_files >>^ snd)
|
2018-05-25 16:53:10 +00:00
|
|
|
(SC.expand_and_eval_set sctx ~scope:(CC.scope cctx) ~dir
|
|
|
|
js_of_ocaml.flags
|
2018-05-04 15:49:25 +00:00
|
|
|
~standard:(Build.return (Js_of_ocaml_rules.standard sctx)))
|
2018-03-30 21:59:43 +00:00
|
|
|
in
|
|
|
|
SC.add_rules sctx (List.map rules ~f:(fun r -> cm_and_flags >>> r))
|
2018-02-20 16:38:22 +00:00
|
|
|
|
|
|
|
let build_and_link_many
|
2018-05-25 16:53:10 +00:00
|
|
|
~programs
|
2018-02-20 16:38:22 +00:00
|
|
|
~linkages
|
|
|
|
?link_flags
|
|
|
|
?(js_of_ocaml=Jbuild.Js_of_ocaml.default)
|
2018-05-25 16:53:10 +00:00
|
|
|
cctx
|
2018-02-20 16:38:22 +00:00
|
|
|
=
|
2018-06-28 08:46:20 +00:00
|
|
|
let dep_graphs = Ocamldep.rules cctx in
|
2018-02-20 16:38:22 +00:00
|
|
|
|
|
|
|
(* CR-someday jdimino: this should probably say [~dynlink:false] *)
|
2018-05-25 16:53:10 +00:00
|
|
|
Module_compilation.build_modules cctx ~js_of_ocaml ~dep_graphs;
|
2018-02-20 16:38:22 +00:00
|
|
|
|
|
|
|
List.iter programs ~f:(fun { Program.name; main_module_name } ->
|
|
|
|
let top_sorted_modules =
|
2018-03-03 18:03:37 +00:00
|
|
|
let main = Option.value_exn
|
2018-05-25 16:53:10 +00:00
|
|
|
(Module.Name.Map.find (CC.modules cctx) main_module_name) in
|
2018-02-20 16:38:22 +00:00
|
|
|
Ocamldep.Dep_graph.top_closed_implementations dep_graphs.impl
|
|
|
|
[main]
|
|
|
|
in
|
|
|
|
List.iter linkages ~f:(fun linkage ->
|
2018-05-25 16:53:10 +00:00
|
|
|
link_exe cctx
|
2018-02-20 16:38:22 +00:00
|
|
|
~name
|
|
|
|
~linkage
|
|
|
|
~top_sorted_modules
|
|
|
|
~js_of_ocaml
|
2018-02-25 22:04:46 +00:00
|
|
|
?link_flags))
|
2018-02-20 16:38:22 +00:00
|
|
|
|
2018-05-25 16:53:10 +00:00
|
|
|
let build_and_link ~program =
|
|
|
|
build_and_link_many ~programs:[program]
|