Skip to content

Commit 1c3c6b4

Browse files
committed
Add a lazy bsb_global_backend and use it everywhere
1 parent 60ca072 commit 1c3c6b4

11 files changed

+112
-16
lines changed

jscomp/bsb/bsb_clean.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ let (//) = Ext_path.combine
2929
let ninja_clean proj_dir =
3030
try
3131
let cmd = Bsb_global_paths.vendor_ninja in
32-
let cwd = proj_dir // Bsb_config.lib_bs in
32+
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
33+
let cwd = proj_dir // lib_artifacts_dir in
3334
if Sys.file_exists cwd then
3435
let eid =
3536
Bsb_unix.run_command_execv {cmd ; args = [|cmd; "-t"; "clean"|] ; cwd} in

jscomp/bsb/bsb_config.mli

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
val ocaml_bin_install_prefix : string -> string
2727
val proj_rel : string -> string
2828

29+
val lib_lit : string
2930
val lib_js : string
3031
val lib_bs : string
3132
val lib_es6 : string

jscomp/bsb/bsb_config_types.ml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type dependencies = dependency list
3333
(* `string` is a path to the entrypoint *)
3434
type entries_t = JsTarget of string | NativeTarget of string | BytecodeTarget of string
3535

36+
type compilation_kind_t = Js | Bytecode | Native
3637

3738
type reason_react_jsx =
3839
| Jsx_v2

jscomp/bsb/bsb_global_backend.ml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(* Copyright (C) 2019 - Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
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
49+
50+
let (//) = Ext_path.combine
51+
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

jscomp/bsb/bsb_global_backend.mli

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
(* Copyright (C) 2019 - Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
val cmdline_backend : Bsb_config_types.compilation_kind_t option ref
26+
27+
val backend : Bsb_config_types.compilation_kind_t Lazy.t
28+
29+
val lib_artifacts_dir : string Lazy.t

jscomp/bsb/bsb_merlin_gen.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ 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
8384
Buffer.add_string buffer merlin_b ;
84-
Buffer.add_string buffer Bsb_config.lib_bs ;
85+
Buffer.add_string buffer lib_artifacts_dir ;
8586
Buffer.add_string buffer merlin_flg ;
8687
Buffer.add_string buffer "-open ";
8788
Buffer.add_string buffer x
@@ -175,13 +176,14 @@ let merlin_file_gen ~per_proj_dir:(per_proj_dir:string)
175176
Buffer.add_string buffer merlin_b;
176177
Buffer.add_string buffer path ;
177178
);
179+
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
178180
Ext_list.iter res_files.files (fun x ->
179181
if not (Bsb_file_groups.is_empty x) then
180182
begin
181183
Buffer.add_string buffer merlin_s;
182184
Buffer.add_string buffer x.dir ;
183185
Buffer.add_string buffer merlin_b;
184-
Buffer.add_string buffer (Bsb_config.lib_bs//x.dir) ;
186+
Buffer.add_string buffer (lib_artifacts_dir//x.dir) ;
185187
end
186188
) ;
187189
Buffer.add_string buffer "\n";

jscomp/bsb/bsb_ninja_gen.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ let output_ninja_and_namespace_map
120120
number_of_dev_groups;
121121
} : Bsb_config_types.t) : unit
122122
=
123-
124-
let cwd_lib_bs = per_proj_dir // Bsb_config.lib_bs in
123+
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
124+
let cwd_lib_bs = per_proj_dir // lib_artifacts_dir in
125125
let ppx_flags = Bsb_build_util.ppx_flags ppx_files in
126126
let oc = open_out_bin (cwd_lib_bs // Literals.build_ninja) in
127127
let g_pkg_flg , g_ns_flg =
@@ -244,7 +244,7 @@ let output_ninja_and_namespace_map
244244

245245
Ext_option.iter namespace (fun ns ->
246246
let namespace_dir =
247-
per_proj_dir // Bsb_config.lib_bs in
247+
per_proj_dir // lib_artifacts_dir in
248248
Bsb_namespace_map_gen.output
249249
~dir:namespace_dir ns
250250
bs_file_groups;

jscomp/bsb/bsb_ninja_regen.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ 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_bs_dir = per_proj_dir // Bsb_config.lib_bs in
38+
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
39+
let lib_bs_dir = per_proj_dir // lib_artifacts_dir in
3940
let output_deps = lib_bs_dir // bsdeps in
4041
let check_result =
4142
Bsb_ninja_check.check

jscomp/bsb/bsb_parse_sources.ml

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ let prune_staled_bs_js_files
212212
(context : cxt)
213213
(cur_sources : _ Map_string.t )
214214
: unit =
215+
(* Doesn't need to use Bsb_global_backend.lib_artifacts_dir because this is only for JS. *)
215216
let lib_parent =
216217
Filename.concat (Filename.concat context.root Bsb_config.lib_bs)
217218
context.cwd in

jscomp/bsb/bsb_world.ml

+8-6
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ let install_targets cwd ({files_to_install; namespace; package_name} : Bsb_confi
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
3536
let install_filename_sans_extension destdir namespace x =
3637
let x =
3738
Ext_namespace.make ?ns:namespace x in
3839
install ~destdir (cwd // x ^ Literals.suffix_ml) ;
3940
install ~destdir (cwd // x ^ Literals.suffix_re) ;
4041
install ~destdir (cwd // x ^ Literals.suffix_mli) ;
4142
install ~destdir (cwd // x ^ Literals.suffix_rei) ;
42-
install ~destdir (cwd // Bsb_config.lib_bs//x ^ Literals.suffix_cmi) ;
43-
install ~destdir (cwd // Bsb_config.lib_bs//x ^ Literals.suffix_cmj) ;
44-
install ~destdir (cwd // Bsb_config.lib_bs//x ^ Literals.suffix_cmt) ;
45-
install ~destdir (cwd // Bsb_config.lib_bs//x ^ Literals.suffix_cmti) ;
43+
install ~destdir (cwd // lib_artifacts_dir//x ^ Literals.suffix_cmi) ;
44+
install ~destdir (cwd // lib_artifacts_dir//x ^ Literals.suffix_cmj) ;
45+
install ~destdir (cwd // lib_artifacts_dir//x ^ Literals.suffix_cmt) ;
46+
install ~destdir (cwd // lib_artifacts_dir//x ^ Literals.suffix_cmti) ;
4647
in
4748
let destdir = cwd // Bsb_config.lib_ocaml in (* lib is already there after building, so just mkdir [lib/ocaml] *)
4849
if not @@ Sys.file_exists destdir then begin Unix.mkdir destdir 0o777 end;
@@ -66,6 +67,7 @@ let build_bs_deps cwd (deps : Bsb_package_specs.t) (ninja_args : string array) =
6667
if Ext_array.is_empty ninja_args then [|vendor_ninja|]
6768
else Array.append [|vendor_ninja|] ninja_args
6869
in
70+
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
6971
Bsb_build_util.walk_all_deps cwd (fun {top; proj_dir} ->
7072
if not top then
7173
begin
@@ -76,7 +78,7 @@ let build_bs_deps cwd (deps : Bsb_package_specs.t) (ninja_args : string array) =
7678
~per_proj_dir:proj_dir in (* set true to force regenrate ninja file so we have [config_opt]*)
7779
let command =
7880
{Bsb_unix.cmd = vendor_ninja;
79-
cwd = proj_dir // Bsb_config.lib_bs;
81+
cwd = proj_dir // lib_artifacts_dir;
8082
args
8183
} in
8284
let eid =
@@ -105,4 +107,4 @@ let make_world_deps cwd (config : Bsb_config_types.t option) (ninja_args : strin
105107
*)
106108
Bsb_config_parse.package_specs_from_bsconfig ()
107109
| Some config -> config.package_specs in
108-
build_bs_deps cwd deps ninja_args
110+
build_bs_deps cwd deps ninja_args

jscomp/main/bsb_main.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,21 @@ let exec_command_then_exit command =
9191
(* Execute the underlying ninja build call, then exit (as opposed to keep watching) *)
9292
let ninja_command_exit ninja_args =
9393
let ninja_args_len = Array.length ninja_args in
94+
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
9495
if Ext_sys.is_windows_or_cygwin then
9596
let path_ninja = Filename.quote Bsb_global_paths.vendor_ninja in
9697
exec_command_then_exit
9798
(if ninja_args_len = 0 then
9899
Ext_string.inter3
99-
path_ninja "-C" Bsb_config.lib_bs
100+
path_ninja "-C" lib_artifacts_dir
100101
else
101102
let args =
102103
Array.append
103-
[| path_ninja ; "-C"; Bsb_config.lib_bs|]
104+
[| path_ninja ; "-C"; lib_artifacts_dir|]
104105
ninja_args in
105106
Ext_string.concat_array Ext_string.single_space args)
106107
else
107-
let ninja_common_args = [|"ninja.exe"; "-C"; Bsb_config.lib_bs |] in
108+
let ninja_common_args = [|"ninja.exe"; "-C"; lib_artifacts_dir |] in
108109
let args =
109110
if ninja_args_len = 0 then ninja_common_args else
110111
Array.append ninja_common_args ninja_args in

0 commit comments

Comments
 (0)