Skip to content

Commit 85b2139

Browse files
committed
Small changes stepping towards supporting native compilation
1 parent 60325b4 commit 85b2139

14 files changed

+931
-833
lines changed

jscomp/bsb/bsb_build_schemas.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ let reason = "reason"
6868
let react_jsx = "react-jsx"
6969

7070
let entries = "entries"
71-
let kind = "kind"
72-
let main = "main"
71+
let backend = "backend"
72+
let main_module = "main-module"
7373
let cut_generators = "cut-generators"
7474
let generators = "generators"
7575
let command = "command"
@@ -82,4 +82,4 @@ let error = "error"
8282
let suffix = "suffix"
8383
let gentypeconfig = "gentypeconfig"
8484
let path = "path"
85-
let ignored_dirs = "ignored-dirs"
85+
let ignored_dirs = "ignored-dirs"

jscomp/bsb/bsb_clean.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let (//) = Ext_path.combine
2929
let ninja_clean proj_dir =
3030
try
3131
let cmd = Bsb_global_paths.vendor_ninja in
32-
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
32+
let lib_artifacts_dir = !Bsb_global_backend.lib_artifacts_dir in
3333
let cwd = proj_dir // lib_artifacts_dir in
3434
if Sys.file_exists cwd then
3535
let eid =

jscomp/bsb/bsb_config_parse.ml

+17-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let resolve_package cwd package_name =
3030
let x = Bsb_pkg.resolve_bs_package ~cwd package_name in
3131
{
3232
Bsb_config_types.package_name ;
33-
package_install_path = x // Bsb_config.lib_ocaml
33+
package_install_path = x // !Bsb_global_backend.lib_ocaml_dir
3434
}
3535

3636
type json_map = Ext_json_types.t Map_string.t
@@ -49,8 +49,8 @@ let extract_main_entries (map :json_map) =
4949
let kind = ref "js" in
5050
let main = ref None in
5151
let _ = map
52-
|? (Bsb_build_schemas.kind, `Str (fun x -> kind := x))
53-
|? (Bsb_build_schemas.main, `Str (fun x -> main := Some x))
52+
|? (Bsb_build_schemas.backend, `Str (fun x -> kind := x))
53+
|? (Bsb_build_schemas.main_module, `Str (fun x -> main := Some x))
5454
in
5555
let path = begin match !main with
5656
(* This is technically optional when compiling to js *)
@@ -74,7 +74,15 @@ let extract_main_entries (map :json_map) =
7474
begin match Map_string.find_opt map Bsb_build_schemas.entries with
7575
| Some (Arr {content = s}) -> entries := extract_entries s
7676
| _ -> ()
77-
end; !entries
77+
end;
78+
if not !Bsb_global_backend.backend_is_set then
79+
begin match !entries with
80+
| []
81+
| (Bsb_config_types.JsTarget _) :: _ -> Bsb_global_backend.set_backend Bsb_config_types.Js
82+
| (Bsb_config_types.NativeTarget _) :: _ -> Bsb_global_backend.set_backend Bsb_config_types.Native
83+
| (Bsb_config_types.BytecodeTarget _) :: _ -> Bsb_global_backend.set_backend Bsb_config_types.Bytecode
84+
end;
85+
!entries
7886
#else
7987
let extract_main_entries (_ :json_map) = []
8088
#end
@@ -173,7 +181,7 @@ let check_stdlib (map : json_map) cwd (*built_in_package*) =
173181
check_version_exit map stdlib_path;
174182
Some {
175183
Bsb_config_types.package_name = current_package;
176-
package_install_path = stdlib_path // Bsb_config.lib_ocaml;
184+
package_install_path = stdlib_path // !Bsb_global_backend.lib_ocaml_dir;
177185
}
178186

179187
| _ -> assert false
@@ -403,6 +411,9 @@ let interpret_json
403411
let refmt = extract_refmt map per_proj_dir in
404412
let gentype_config = extract_gentype_config map per_proj_dir in
405413
let bs_suffix = extract_bs_suffix_exn map in
414+
(* This line has to be before any calls to Bsb_global_backend.backend, because it'll read the entries
415+
array from the bsconfig and set the backend_ref to the first entry, if any. *)
416+
let entries = extract_main_entries map in
406417
(* The default situation is empty *)
407418
let built_in_package = check_stdlib map per_proj_dir in
408419
let package_specs =
@@ -470,7 +481,7 @@ let interpret_json
470481
generate_merlin =
471482
extract_boolean map Bsb_build_schemas.generate_merlin true;
472483
reason_react_jsx ;
473-
entries = extract_main_entries map;
484+
entries;
474485
generators = extract_generators map ;
475486
cut_generators ;
476487
number_of_dev_groups;

jscomp/bsb/bsb_global_backend.ml

+26-30
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,32 @@
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
26-
let cmdline_backend = ref None
27-
28-
(* If cmdline_backend is set we use it, otherwise we use the first entry's backend. *)
29-
let backend = lazy
30-
begin match !cmdline_backend with
31-
| Some cmdline_backend -> cmdline_backend
32-
| None ->
33-
let entries = Bsb_config_parse.entries_from_bsconfig Bsb_global_paths.cwd in
34-
let new_cmdline_backend = begin match entries with
35-
| [] -> Bsb_config_types.Js
36-
| (Bsb_config_types.JsTarget _) :: _ -> Bsb_config_types.Js
37-
| (Bsb_config_types.NativeTarget _) :: _ -> Bsb_config_types.Native
38-
| (Bsb_config_types.BytecodeTarget _) :: _ -> Bsb_config_types.Bytecode
39-
end in
40-
cmdline_backend := Some (new_cmdline_backend);
41-
new_cmdline_backend
42-
end
43-
#else
44-
let backend = lazy Bsb_config_types.Js
45-
46-
(* No cost of using this variable below when compiled in JS mode. *)
47-
let cmdline_backend = ref (Some Bsb_config_types.Js)
48-
#end
25+
let backend_is_set = ref false
26+
27+
let backend = ref Bsb_config_types.Js
28+
29+
let lib_artifacts_dir = ref Bsb_config.lib_bs
30+
31+
let lib_ocaml_dir = ref Bsb_config.lib_ocaml
32+
33+
let backend_string = ref Literals.js
4934

5035
let (//) = Ext_path.combine
5136

52-
let lib_artifacts_dir = lazy
53-
begin match Lazy.force backend with
54-
| Bsb_config_types.Js -> Bsb_config.lib_bs
55-
| Bsb_config_types.Native -> Bsb_config.lib_lit // "native"
56-
| Bsb_config_types.Bytecode -> Bsb_config.lib_lit // "bytecode"
57-
end
37+
let set_backend b =
38+
backend_is_set := true;
39+
backend := b;
40+
match b with
41+
| Bsb_config_types.Js ->
42+
lib_artifacts_dir := Bsb_config.lib_bs;
43+
lib_ocaml_dir := Bsb_config.lib_ocaml;
44+
backend_string := Literals.js;
45+
| Bsb_config_types.Native ->
46+
lib_artifacts_dir := Bsb_config.lib_lit // "bs-native";
47+
lib_ocaml_dir := Bsb_config.lib_lit // "ocaml-native";
48+
backend_string := Literals.native;
49+
| Bsb_config_types.Bytecode ->
50+
lib_artifacts_dir := Bsb_config.lib_lit // "bs-bytecode";
51+
lib_ocaml_dir := Bsb_config.lib_lit // "ocaml-bytecode";
52+
backend_string := Literals.bytecode;
53+

jscomp/bsb/bsb_global_backend.mli

+16-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@
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-
val cmdline_backend : Bsb_config_types.compilation_kind_t option ref
25+
(* Flag to track whether backend has been set to a value. *)
26+
val backend_is_set : bool ref
2627

27-
val backend : Bsb_config_types.compilation_kind_t Lazy.t
28+
(* Target backend *)
29+
val backend : Bsb_config_types.compilation_kind_t ref
30+
31+
(* path to all intermediate build artifacts, would be lib/bs when compiling to JS *)
32+
val lib_artifacts_dir : string ref
33+
34+
(* path to the compiled artifacts, would be lib/ocaml when compiling to JS *)
35+
val lib_ocaml_dir : string ref
36+
37+
(* string representation of the target backend, would be "js" when compiling to js *)
38+
val backend_string: string ref
39+
40+
(* convenience setter to update all the refs according to the given target backend *)
41+
val set_backend : Bsb_config_types.compilation_kind_t -> unit
2842

29-
val lib_artifacts_dir : string Lazy.t

jscomp/bsb/bsb_merlin_gen.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ let output_merlin_namespace buffer ns=
8080
match ns with
8181
| None -> ()
8282
| Some x ->
83-
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
83+
let lib_artifacts_dir = !Bsb_global_backend.lib_artifacts_dir in
8484
Buffer.add_string buffer merlin_b ;
8585
Buffer.add_string buffer lib_artifacts_dir ;
8686
Buffer.add_string buffer merlin_flg ;
@@ -188,7 +188,7 @@ let merlin_file_gen ~per_proj_dir:(per_proj_dir:string)
188188
Buffer.add_string buffer merlin_b;
189189
Buffer.add_string buffer path ;
190190
);
191-
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
191+
let lib_artifacts_dir = !Bsb_global_backend.lib_artifacts_dir in
192192
Ext_list.iter res_files.files (fun x ->
193193
if not (Bsb_file_groups.is_empty x) then
194194
begin

jscomp/bsb/bsb_ninja_check.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let record ~per_proj_dir ~file (file_or_dirs : string list) : unit =
9595
(Unix.stat (Filename.concat per_proj_dir x )).st_mtime
9696
)
9797
in
98-
write file
98+
write (Ext_string.concat3 file "_" !Bsb_global_backend.backend_string)
9999
{ st_mtimes ;
100100
dir_or_files;
101101
source_directory = per_proj_dir ;
@@ -108,7 +108,7 @@ let record ~per_proj_dir ~file (file_or_dirs : string list) : unit =
108108
bit in case we found a different version of compiler
109109
*)
110110
let check ~(per_proj_dir:string) ~forced ~file : check_result =
111-
read file (fun {
111+
read (Ext_string.concat3 file "_" !Bsb_global_backend.backend_string) (fun {
112112
dir_or_files ; source_directory; st_mtimes
113113
} ->
114114
if per_proj_dir <> source_directory then Bsb_source_directory_changed else

jscomp/bsb/bsb_ninja_gen.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ let output_ninja_and_namespace_map
119119
number_of_dev_groups;
120120
} : Bsb_config_types.t) : unit
121121
=
122-
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
122+
let lib_artifacts_dir = !Bsb_global_backend.lib_artifacts_dir in
123123
let cwd_lib_bs = per_proj_dir // lib_artifacts_dir in
124124
let ppx_flags = Bsb_build_util.ppx_flags ppx_files in
125125
let oc = open_out_bin (cwd_lib_bs // Literals.build_ninja) in

jscomp/bsb/bsb_ninja_regen.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let regenerate_ninja
3535
~forced ~per_proj_dir
3636
: Bsb_config_types.t option =
3737
let toplevel = toplevel_package_specs = None in
38-
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
38+
let lib_artifacts_dir = !Bsb_global_backend.lib_artifacts_dir in
3939
let lib_bs_dir = per_proj_dir // lib_artifacts_dir in
4040
let output_deps = lib_bs_dir // bsdeps in
4141
let check_result =

jscomp/bsb/bsb_world.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ let install_targets cwd ({files_to_install; namespace; package_name = _} : Bsb_c
3232
let install ~destdir file =
3333
Bsb_file.install_if_exists ~destdir file |> ignore
3434
in
35-
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
35+
let lib_artifacts_dir = !Bsb_global_backend.lib_artifacts_dir in
3636
let install_filename_sans_extension destdir namespace x =
3737
let x =
3838
Ext_namespace_encode.make ?ns:namespace x in
@@ -67,7 +67,7 @@ let build_bs_deps cwd (deps : Bsb_package_specs.t) (ninja_args : string array) =
6767
if Ext_array.is_empty ninja_args then [|vendor_ninja|]
6868
else Array.append [|vendor_ninja|] ninja_args
6969
in
70-
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
70+
let lib_artifacts_dir = !Bsb_global_backend.lib_artifacts_dir in
7171
Bsb_build_util.walk_all_deps cwd (fun {top; proj_dir} ->
7272
if not top then
7373
begin

jscomp/main/bsb_main.ml

+12-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,17 @@ let bsb_main_flags : (string * Arg.spec * string) list=
8585
we make it at this time to make `bsb -help` easier
8686
*)
8787
"-ws", Arg.Bool ignore,
88-
" [host:]port specify a websocket number (and optionally, a host). When a build finishes, we send a message to that port. For tools that listen on build completion."
88+
" [host:]port specify a websocket number (and optionally, a host). When a build finishes, we send a message to that port. For tools that listen on build completion." ;
89+
#if BS_NATIVE then
90+
"-backend", Arg.String (fun s ->
91+
match s with
92+
| "js" -> Bsb_global_backend.set_backend Bsb_config_types.Js
93+
| "native" -> Bsb_global_backend.set_backend Bsb_config_types.Native
94+
| "bytecode" -> Bsb_global_backend.set_backend Bsb_config_types.Bytecode
95+
| _ -> failwith "-backend should be one of: 'js', 'bytecode' or 'native'."
96+
),
97+
" Builds the entries specified in the bsconfig that match the given backend. Can be either 'js', 'bytecode' or 'native'.";
98+
#end
8999
]
90100

91101

@@ -99,7 +109,7 @@ let exec_command_then_exit command =
99109
(* Execute the underlying ninja build call, then exit (as opposed to keep watching) *)
100110
let ninja_command_exit ninja_args =
101111
let ninja_args_len = Array.length ninja_args in
102-
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
112+
let lib_artifacts_dir = !Bsb_global_backend.lib_artifacts_dir in
103113
if Ext_sys.is_windows_or_cygwin then
104114
let path_ninja = Filename.quote Bsb_global_paths.vendor_ninja in
105115
exec_command_then_exit

jscomp/syntax/native_ast_derive_abstract.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ let handleTdcl light (tdcl : Parsetree.type_declaration) =
169169
else maker_body) in
170170

171171
let myMaker =
172-
#if BS_NATIVE then
172+
#if BS_NATIVE_PPX then
173173

174174
Str.value Nonrecursive [
175175
Vb.mk
@@ -204,7 +204,7 @@ let code_sig_transform sigi = match sigi with
204204
} ->
205205
Sig.value (Val.mk ~loc:pstr_loc name typ)
206206
| _ ->
207-
#if BS_NATIVE then
207+
#if BS_NATIVE_PPX then
208208
Sig.type_ Nonrecursive []
209209
#else assert false
210210
#end

0 commit comments

Comments
 (0)