2222 * along with this program; if not, write to the Free Software
2323 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424
25+ #if BS_NATIVE then
2526type link_t = LinkBytecode of string | LinkNative of string
2627
27- let link link_byte_or_native ~main_module ~batch_files ~includes =
28+ let ( // ) = Ext_path. combine
29+
30+ (* The linker is called with object files (.cmo / .cmx) which will be namespaced and we're using
31+ those names to read-in the mlast files which are not namespaced. So we strip the namespace
32+ before reading them in. *)
33+ let module_of_filename filename =
34+ let str = Ext_filename. chop_extension_maybe filename in
35+ match (String. rindex str '-' ) with
36+ | exception Not_found -> str
37+ | len -> String. sub str 0 len
38+
39+ let link link_byte_or_native ~main_module ~batch_files ~includes ~ocaml_dependencies ~namespace ~warnings ~warn_error ~verbose ~cwd =
2840 let suffix_object_files, suffix_library_files, compiler, output_file = begin match link_byte_or_native with
29- | LinkBytecode output_file -> Literals. suffix_cmo, Literals. suffix_cma , " ocamlc.opt " , output_file
30- | LinkNative output_file -> Literals. suffix_cmx, Literals. suffix_cmxa, " ocamlopt.opt " , output_file
41+ | LinkBytecode output_file -> Literals. suffix_cmo, Literals. suffix_cma , " ocamlc" , output_file
42+ | LinkNative output_file -> Literals. suffix_cmx, Literals. suffix_cmxa, " ocamlopt" , output_file
3143 end in
3244 (* Map used to track the path to the files as the dependency_graph that we're going to read from the mlast file only contains module names *)
3345 let module_to_filepath = Ext_list. fold_left batch_files String_map. empty
3446 (fun m v ->
3547 String_map. add m
36- (Ext_filename. module_name v )
48+ (Ext_filename. module_name (module_of_filename v) )
3749 (Ext_filename. chop_extension_maybe v)
3850 )
3951 in
4052 let dependency_graph = Ext_list. fold_left batch_files String_map. empty
4153 (fun m file ->
54+ let module_name = module_of_filename file in
55+ let suffix = if Sys. file_exists (module_name ^ Literals. suffix_mlast) then Literals. suffix_mlast
56+ else Literals. suffix_reast in
4257 String_map. add m
43- (Ext_filename. module_name file )
44- (Bsb_helper_extract. read_dependency_graph_from_mlast_file (( Ext_filename. chop_extension_maybe file) ^ Literals. suffix_mlast ))
58+ (Ext_filename. module_name module_name )
59+ (Bsb_helper_extract. read_dependency_graph_from_mlast_file (module_name ^ suffix ))
4560 )
4661 in
62+ let ocaml_dependencies =
63+ List. fold_left (fun acc v ->
64+ match v with
65+ | "threads" ->
66+ " -thread" :: (Bsb_global_paths. ocaml_dir // " lib" // " ocaml" // " threads" // " threads" ^ suffix_library_files) :: acc
67+ | v -> (Bsb_global_paths. ocaml_dir // " lib" // " ocaml" // v ^ suffix_library_files) :: acc
68+ ) [] ocaml_dependencies in
69+ let warning_command = if String. length warnings > 0 then
70+ " -w" :: warnings :: []
71+ else [] in
72+ let warning_command = if String. length warn_error > 0 then
73+ " -warn-error" :: warn_error :: warning_command
74+ else warning_command in
75+
4776 let tasks = Bsb_helper_dep_graph. simple_collect_from_main dependency_graph main_module in
77+ let namespace = match namespace with
78+ | None -> " "
79+ | Some namespace -> " -" ^ namespace
80+ in
4881 let list_of_object_files = Queue. fold
4982 (fun acc v -> match String_map. find_opt module_to_filepath v with
50- | Some file -> (file ^ suffix_object_files) :: acc
51- | None -> failwith @@ " build.ninja is missing the file ' " ^ v ^ " ' that was used in the project. Try force-regenerating but this shouldn't happen. "
83+ | Some file -> (file ^ namespace ^ suffix_object_files) :: acc
84+ | None -> Bsb_exception. missing_object_file v
5285 )
5386 []
5487 tasks in
@@ -59,9 +92,16 @@ let link link_byte_or_native ~main_module ~batch_files ~includes =
5992 in
6093 (* This list will be reversed so we append the otherlibs object files at the end, and they'll end at the beginning. *)
6194 let otherlibs = Bsb_helper_dep_graph. get_otherlibs_dependencies dependency_graph suffix_library_files in
62- let all_object_files = List. rev (list_of_object_files @ otherlibs) in
63- Unix. execvp
64- compiler
65- (Array. of_list (compiler :: " -o" :: output_file :: library_files @ all_object_files))
95+ let all_object_files = ocaml_dependencies @ library_files @ List. rev (list_of_object_files @ otherlibs) in
96+ let compiler_extension = if Ext_sys. is_windows_or_cygwin then " .opt.exe" else " .opt" in
97+ let local_compiler = Bsb_global_paths. ocaml_dir // " bin" // compiler ^ compiler_extension in
98+ let super_errors = if false then [" -bs-super-errors" ] else [] in
99+ let list_of_args = (local_compiler :: " -g" ::
100+ warning_command) @ super_errors @ " -o" :: output_file :: all_object_files in
101+ if verbose then
102+ print_endline(" Bsb_helper link command:\n " ^ (String. concat " " list_of_args) ^ " \n " );
103+
104+ Unix. execvp local_compiler (Array. of_list (list_of_args))
66105 end else
67106 failwith @@ " No " ^ suffix_object_files ^ " to link. Hint: is the main module in the entries array right?"
107+ #end
0 commit comments