Skip to content

Commit c6a41f7

Browse files
committed
Fix existing code compiled away with BS_NATIVE
And this sets us better for #3762 :) This shouldn't affect bsb / bsc, everything's compiled away.
1 parent 7cea069 commit c6a41f7

11 files changed

+206
-145
lines changed

jscomp/bsb_helper/bsb_helper_depfile_gen.ml

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ let oc_intf
243243

244244

245245
let emit_d
246+
compilation_kind
246247
(index : Bsb_dir_index.t)
247248
(namespace : string option) (mlast : string) (mliast : string) =
248249
let data =
@@ -251,9 +252,12 @@ let emit_d
251252
let buf = Ext_buffer.create 2048 in
252253
let filename =
253254
Ext_filename.new_extension mlast Literals.suffix_d in
254-
let lhs_suffix = Literals.suffix_cmj in
255-
let rhs_suffix = Literals.suffix_cmj in
256-
255+
let lhs_suffix, rhs_suffix =
256+
match compilation_kind with
257+
| Js -> Literals.suffix_cmj, Literals.suffix_cmj
258+
| Bytecode -> Literals.suffix_cmo, Literals.suffix_cmo
259+
| Native -> Literals.suffix_cmx, Literals.suffix_cmx
260+
in
257261
oc_impl
258262
mlast
259263
index
@@ -271,64 +275,3 @@ let emit_d
271275
buf
272276
end;
273277
write_file filename buf
274-
275-
276-
277-
278-
279-
280-
#if BS_NATIVE then
281-
(* OPT: Don't touch the .d file if nothing changed *)
282-
let emit_dep_file
283-
compilation_kind
284-
(fn : string)
285-
(index : Bsb_dir_index.t)
286-
(namespace : string option) : unit =
287-
let data =
288-
Bsb_db_decode.read_build_cache
289-
~dir:Filename.current_dir_name
290-
in
291-
let set = read_deps fn in
292-
match Ext_string.ends_with_then_chop fn Literals.suffix_mlast with
293-
| Some input_file ->
294-
(* #if BS_NATIVE then *)
295-
let lhs_suffix, rhs_suffix =
296-
match compilation_kind with
297-
| Js -> Literals.suffix_cmj, Literals.suffix_cmj
298-
| Bytecode -> Literals.suffix_cmo, Literals.suffix_cmi
299-
| Native -> Literals.suffix_cmx, Literals.suffix_cmx
300-
in
301-
(* #else
302-
let lhs_suffix = Literals.suffix_cmj in
303-
let rhs_suffix = Literals.suffix_cmj in
304-
#end *)
305-
let buf = Ext_buffer.create 64 in
306-
oc_impl
307-
set
308-
input_file
309-
index
310-
data
311-
namespace
312-
buf
313-
lhs_suffix
314-
rhs_suffix
315-
;
316-
write_file (input_file ^ Literals.suffix_d ) buf
317-
318-
| None ->
319-
begin match Ext_string.ends_with_then_chop fn Literals.suffix_mliast with
320-
| Some input_file ->
321-
let filename = (input_file ^ Literals.suffix_d) in
322-
let buf = Ext_buffer.create 64 in
323-
oc_intf
324-
set
325-
input_file
326-
index
327-
data
328-
namespace
329-
buf;
330-
write_file filename buf
331-
| None ->
332-
raise (Arg.Bad ("don't know what to do with " ^ fn))
333-
end
334-
#end

jscomp/bsb_helper/bsb_helper_depfile_gen.mli

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,11 @@ type kind = Js | Bytecode | Native
2929
*)
3030
val deps_of_channel : in_channel -> string list
3131

32-
#if BS_NATIVE then
33-
(**
34-
[make compilation_kind filename index namespace]
35-
emit [.d] file based on filename (shoud be [.mlast] or [.mliast])
36-
*)
37-
val emit_dep_file:
38-
kind ->
39-
string ->
40-
Bsb_dir_index.t ->
41-
string option ->
42-
unit
43-
#end
4432

45-
val emit_d:
33+
val emit_d:
34+
kind ->
4635
Bsb_dir_index.t ->
4736
string option ->
4837
string ->
4938
string -> (* empty string means no mliast *)
50-
unit
39+
unit

jscomp/bsb_helper/bsb_helper_extract.ml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ let read_dependency_graph_from_mlast_file fn =
2727
try
2828
let dep_size = input_binary_int ic in
2929
let dep_data = really_input_string ic dep_size in
30-
let splitted_data = Ext_string.split dep_data '\t' in
31-
let set = match splitted_data with
32-
| final_length :: rest ->
33-
let set = String_set.of_list rest in
34-
assert (String_set.cardinal set = (int_of_string final_length));
35-
set
36-
| _ -> assert false in
30+
let splitted_data = Ext_string.split dep_data '\n' in
31+
let set = String_set.of_list splitted_data in
3732
close_in ic;
3833
set
3934
with exn ->

jscomp/bsb_helper/bsb_helper_linker.ml

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,66 @@
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
2526
type 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

jscomp/bsb_helper/bsb_helper_linker.mli

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@
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
2526
type link_t = LinkBytecode of string | LinkNative of string
2627

27-
val link : link_t -> main_module:string -> batch_files:string list -> includes:string list -> unit
28+
val link : link_t ->
29+
main_module:string ->
30+
batch_files:string list ->
31+
includes:string list ->
32+
ocaml_dependencies:string list ->
33+
namespace:string option ->
34+
warnings:string ->
35+
warn_error:string ->
36+
verbose:bool ->
37+
cwd: string ->
38+
unit
39+
#end
40+

jscomp/bsb_helper/bsb_helper_packer.ml

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,84 @@
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
2526
type pack_t = PackBytecode | PackNative
2627

27-
let pack pack_byte_or_native ~batch_files ~includes =
28-
let suffix_object_files, suffix_library_files, compiler = begin match pack_byte_or_native with
29-
| PackBytecode -> Literals.suffix_cmo, Literals.suffix_cma , "ocamlc.opt"
30-
| PackNative -> Literals.suffix_cmx, Literals.suffix_cmxa, "ocamlopt.opt"
28+
let ( // ) = Ext_path.combine
29+
30+
(* The packer 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 pack pack_byte_or_native ~batch_files ~includes ~namespace ~warnings ~warn_error ~verbose ~cwd =
40+
let suffix_object_files, suffix_library_files, compiler, nested = begin match pack_byte_or_native with
41+
| PackBytecode -> Literals.suffix_cmo, Literals.suffix_cma , "ocamlc", "bytecode"
42+
| PackNative -> Literals.suffix_cmx, Literals.suffix_cmxa, "ocamlopt", "native"
3143
end in
3244
let module_to_filepath = Ext_list.fold_left batch_files String_map.empty
3345
(fun m v ->
3446
String_map.add m
35-
(Ext_filename.module_name v)
47+
(Ext_filename.module_name (module_of_filename v))
3648
(Ext_filename.chop_extension_maybe v)
3749
)
3850
in
3951
let dependency_graph = Ext_list.fold_left batch_files String_map.empty
4052
(fun m file ->
53+
let module_name = module_of_filename file in
54+
let suffix = if Sys.file_exists (module_name ^ Literals.suffix_mlast) then Literals.suffix_mlast
55+
else Literals.suffix_reast in
4156
String_map.add m
42-
(Ext_filename.module_name file)
43-
(Bsb_helper_extract.read_dependency_graph_from_mlast_file ((Ext_filename.chop_extension_maybe file) ^ Literals.suffix_mlast))
57+
(Ext_filename.module_name module_name)
58+
(Bsb_helper_extract.read_dependency_graph_from_mlast_file (module_name ^ suffix))
4459
)
4560
in
4661
let domain =
4762
String_map.fold dependency_graph String_set.empty
4863
(fun k _ acc -> String_set.add acc k)
4964
in
5065
let sorted_tasks = Bsb_helper_dep_graph.sort_files_by_dependencies ~domain dependency_graph in
51-
let list_of_object_files = Queue.fold
66+
let all_object_files = Queue.fold
5267
(fun acc v -> match String_map.find_opt module_to_filepath v with
5368
| Some file -> (file ^ suffix_object_files) :: acc
5469
| None -> failwith @@ "build.ninja is missing the file '" ^ v ^ "' that was used in the project. Try force-regenerating but this shouldn't happen."
5570
)
5671
[]
5772
sorted_tasks in
73+
let warning_command = if String.length warnings > 0 then
74+
"-w" :: warnings :: []
75+
else [] in
76+
let warning_command = if String.length warn_error > 0 then
77+
"-warn-error" :: warn_error :: warning_command
78+
else warning_command in
79+
5880
(* This list will be reversed so we append the otherlibs object files at the end, and they'll end at the beginning. *)
59-
if list_of_object_files <> [] then
81+
if all_object_files <> [] then
6082
let includes = Ext_list.fold_left includes [] (fun acc dir -> "-I" :: dir :: acc) in
6183
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
84+
let all_object_files = match namespace with
85+
| None -> all_object_files
86+
| Some namespace -> (namespace ^ suffix_object_files) :: all_object_files
87+
in
88+
let all_object_files = List.rev (all_object_files @ otherlibs) in
89+
let compiler_extension = if Ext_sys.is_windows_or_cygwin then ".opt.exe" else ".opt" in
90+
let local_compiler = Bsb_global_paths.ocaml_dir // "bin" // compiler ^ compiler_extension in
91+
92+
let super_errors = if false then ["-bs-super-errors"] else [] in
93+
let list_of_args = (local_compiler :: "-a" :: "-g" ::
94+
warning_command) @ super_errors @ "-o" :: (cwd // Literals.library_file ^ suffix_library_files) :: includes
95+
@ all_object_files in
96+
97+
if verbose then
98+
print_endline("Bsb_helper pack command:\n" ^ (String.concat " " list_of_args) ^ "\n");
99+
63100
Unix.execvp
64-
compiler
65-
(Array.of_list (compiler :: "-a" :: "-o" :: (Literals.library_file ^ suffix_library_files) :: includes @ all_object_files))
101+
local_compiler
102+
(Array.of_list list_of_args)
66103
else
67-
failwith @@ "No " ^ suffix_object_files ^ " to pack into a lib."
104+
Bsb_exception.no_files_to_pack suffix_object_files
105+
#end

jscomp/bsb_helper/bsb_helper_packer.mli

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
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
2526
type pack_t = PackBytecode | PackNative
2627

27-
val pack : pack_t -> batch_files:string list -> includes:string list -> unit
28+
val pack : pack_t ->
29+
batch_files:string list ->
30+
includes:string list ->
31+
namespace:string option ->
32+
warnings: string ->
33+
warn_error: string ->
34+
verbose: bool ->
35+
cwd:string ->
36+
unit
37+
#end

jscomp/core/js_name_of_module_id.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ let string_of_module_id
128128
| Package_found pkg, Package_script
129129
->
130130
#if BS_NATIVE then
131-
if Filename.is_relative dep_path then
131+
if Filename.is_relative pkg.rel_path then
132132
pkg.pkg_rel_path // js_file
133133
else
134-
pkg.dep_path // js_file
134+
pkg.rel_path // js_file
135135
#else
136136
pkg.pkg_rel_path // js_file
137137
#end
@@ -150,10 +150,10 @@ let string_of_module_id
150150
begin match module_system with
151151
| NodeJS | Es6 ->
152152
#if BS_NATIVE then
153-
if Filename.is_relative dep_path then
153+
if Filename.is_relative dep_pkg.rel_path then
154154
dep_pkg.pkg_rel_path // js_file
155155
else
156-
dep_path // js_file
156+
dep_pkg.rel_path // js_file
157157
#else
158158
dep_pkg.pkg_rel_path // js_file
159159
#end

0 commit comments

Comments
 (0)