Skip to content

Commit 6f5f968

Browse files
authored
Merge pull request #4925 from rescript-lang/customize_runtime
part 2: support `external-stdlib` config
2 parents 568376a + 886ea1c commit 6f5f968

22 files changed

+1907
-1776
lines changed

docs/docson/build-schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@
481481
"type": "boolean",
482482
"description": "(Experimental) whether to use the OCaml standard library. Default: true"
483483
},
484+
"external-stdlib" : {
485+
"type" : "string",
486+
"description": "Use the external stdlib library instead of the one shipped with the compiler package"
487+
},
484488
"bs-external-includes": {
485489
"type": "array",
486490
"items": {

jscomp/bsb/bsb_build_schemas.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ let export_none = "none"
6565

6666

6767
let use_stdlib = "use-stdlib"
68+
let external_stdlib = "external-stdlib"
6869
let reason = "reason"
6970
let react_jsx = "react-jsx"
7071

jscomp/bsb/bsb_config_parse.ml

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -90,48 +90,26 @@ let extract_package_name_and_namespace
9090
- the running bsb and vendoring bsb is the same
9191
- the running bsb need delete stale build artifacts
9292
(kinda check npm upgrade)
93-
*)
94-
let check_version_exit (map : json_map) stdlib_path =
95-
match Map_string.find_exn map Bsb_build_schemas.version with
96-
| Str {str } ->
97-
if str <> Bs_version.version then
98-
begin
99-
Format.fprintf Format.err_formatter
100-
"@{<error>bs-platform version mismatch@} Running bsb @{<info>%s@} (%s) vs vendored @{<info>%s@} (%s)@."
101-
Bs_version.version
102-
(Filename.dirname (Filename.dirname Sys.executable_name))
103-
str
104-
stdlib_path
105-
;
106-
exit 2
107-
end
108-
| _ -> assert false
109-
110-
let check_stdlib (map : json_map) cwd (*built_in_package*) =
93+
94+
Note if the setup is correct:
95+
the running compiler and node_modules/bs-platform
96+
should be the same version,
97+
The exact check is that the running compiler should have a
98+
compatible runtime version installed, the location of the
99+
compiler is actually not relevant.
100+
We disable the check temporarily
101+
e.g,
102+
```
103+
bsc -runtime runtime_dir@version
104+
```
105+
*)
106+
let check_stdlib (map : json_map) (*built_in_package*) : bool =
111107
match map.?( Bsb_build_schemas.use_stdlib) with
112-
| Some (False _) -> None
108+
| Some (False _) -> false
113109
| None
114-
| Some _ ->
115-
begin
116-
let current_package : Bsb_pkg_types.t = Global !Bs_version.package_name in
117-
if Sys.getenv_opt "RES_SKIP_STDLIB_CHECK" = None then begin
118-
let stdlib_path =
119-
Bsb_pkg.resolve_bs_package ~cwd current_package in
120-
let json_spec =
121-
Ext_json_parse.parse_json_from_file
122-
(* No exn raised: stdlib has package.json *)
123-
(Filename.concat stdlib_path Literals.package_json) in
124-
match json_spec with
125-
| Obj {map} ->
126-
check_version_exit map stdlib_path;
127-
128-
| _ -> assert false
129-
end;
130-
Some {
131-
Bsb_config_types.package_name = current_package;
132-
package_install_path = Filename.dirname Bsb_global_paths.bsc_dir // Bsb_config.lib_ocaml;
133-
}
134-
end
110+
| Some _ ->
111+
true
112+
135113

136114

137115

@@ -355,7 +333,7 @@ let interpret_json
355333
array from the bsconfig and set the backend_ref to the first entry, if any. *)
356334

357335
(* The default situation is empty *)
358-
let built_in_package = check_stdlib map per_proj_dir in
336+
let built_in_package : bool = check_stdlib map in
359337

360338
let pp_flags : string option =
361339
extract_string map Bsb_build_schemas.pp_flags (fun p ->
@@ -410,7 +388,7 @@ let interpret_json
410388
js_post_build_cmd = (extract_js_post_build map per_proj_dir);
411389
package_specs =
412390
(match package_kind with
413-
| Toplevel -> Bsb_package_specs.from_map map
391+
| Toplevel -> Bsb_package_specs.from_map ~cwd:per_proj_dir map
414392
| Pinned_dependency x
415393
| Dependency x -> x);
416394
file_groups = groups;
@@ -434,7 +412,7 @@ let package_specs_from_bsconfig () =
434412
let json = Ext_json_parse.parse_json_from_file Literals.bsconfig_json in
435413
begin match json with
436414
| Obj {map} ->
437-
Bsb_package_specs.from_map map,
415+
Bsb_package_specs.from_map ~cwd:Bsb_global_paths.cwd map,
438416
extract_pinned_dependencies map
439417
| _ -> assert false
440418
end

jscomp/bsb/bsb_config_types.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type t =
6161
bs_dependencies : dependencies;
6262
bs_dev_dependencies : dependencies;
6363
pinned_dependencies : Set_string.t;
64-
built_in_dependency : dependency option;
64+
built_in_dependency : bool;
6565
warning : Bsb_warning.t;
6666
(*TODO: maybe we should always resolve bs-platform
6767
so that we can calculate correct relative path in

jscomp/bsb/bsb_merlin_gen.ml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,13 @@ let merlin_file_gen ~per_proj_dir:(per_proj_dir:string)
165165
Buffer.add_string buffer merlin_b;
166166
Buffer.add_string buffer path ;
167167
);
168-
Ext_option.iter built_in_dependency (fun package ->
169-
let path = package.package_install_path in
170-
Buffer.add_string buffer (merlin_s ^ path );
171-
Buffer.add_string buffer (merlin_b ^ path)
172-
);
168+
if built_in_dependency then (
169+
let path =
170+
(Filename.dirname Bsb_global_paths.bsc_dir)
171+
// "lib" //"ocaml" in
172+
Buffer.add_string buffer (merlin_s ^ path );
173+
Buffer.add_string buffer (merlin_b ^ path)
174+
);
173175
let bsc_string_flag = bsc_flg_to_merlin_ocamlc_flg bsc_flags in
174176
Buffer.add_string buffer bsc_string_flag ;
175177
Buffer.add_string buffer (warning_to_merlin_flg warning);

jscomp/bsb/bsb_ninja_gen.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ let output_ninja_and_namespace_map
237237
~gentype_config
238238
~has_postbuild:js_post_build_cmd
239239
~pp_file
240-
~has_builtin:(built_in_dependency <> None)
240+
~has_builtin:built_in_dependency
241241
~reason_react_jsx
242242
~package_specs
243243
~namespace

jscomp/bsb/bsb_ninja_rule.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ let make_custom_rules
172172
if read_cmi <> `is_cmi then begin
173173
Ext_buffer.add_string buf " -bs-package-name ";
174174
Ext_buffer.add_string buf package_name;
175-
Ext_buffer.add_string buf (Bsb_package_specs.package_flag_of_package_specs package_specs "$in_d")
175+
Ext_buffer.add_string buf
176+
(Bsb_package_specs.package_flag_of_package_specs package_specs ~dirname:"$in_d")
176177
end;
177178
begin match bs_dependencies, bs_dev_dependencies with
178179
| [], [] -> ()

jscomp/bsb/bsb_package_specs.ml

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ type spec = {
3737
suffix : Ext_js_suffix.t
3838
}
3939

40+
(*FIXME: use assoc list instead *)
4041
module Spec_set = Set.Make( struct type t = spec
4142
let compare = Pervasives.compare
4243
end)
4344

44-
type t = Spec_set.t
45+
type t = {
46+
modules : Spec_set.t;
47+
runtime: string option;
48+
(* This has to be resolved as early as possible, since
49+
the path will be inherited in sub projects
50+
*)
51+
}
4552

4653
let (.?()) = Map_string.find_opt
4754

@@ -89,8 +96,8 @@ and from_json_single suffix (x : Ext_json_types.t) : spec =
8996
| Str {str = format; loc } ->
9097
{format = supported_format format loc ; in_source = false ; suffix }
9198
| Obj {map; loc} ->
92-
begin match Map_string.find_exn map "module" with
93-
| Str {str = format} ->
99+
begin match map .?("module") with
100+
| Some(Str {str = format}) ->
94101
let in_source =
95102
match map.?(Bsb_build_schemas.in_source) with
96103
| Some (True _) -> true
@@ -108,13 +115,10 @@ and from_json_single suffix (x : Ext_json_types.t) : spec =
108115
Bsb_exception.errorf ~loc:(Ext_json.loc_of x) "expect a string field"
109116
| None -> suffix in
110117
{format = supported_format format loc ; in_source ; suffix}
111-
| Arr _ ->
112-
Bsb_exception.errorf ~loc
113-
"package-specs: when the configuration is an object, `module` field should be a string, not an array. If you want to pass multiple module specs, try turning package-specs into an array of objects (or strings) instead."
114-
| _ ->
118+
| Some _ ->
115119
Bsb_exception.errorf ~loc
116-
"package-specs: the `module` field of the configuration object should be a string."
117-
| exception _ ->
120+
"package-specs: when the configuration is an object, `module` field should be a string, not an array. If you want to pass multiple module specs, try turning package-specs into an array of objects (or strings) instead."
121+
| None ->
118122
Bsb_exception.errorf ~loc
119123
"package-specs: when the configuration is an object, the `module` field is mandatory."
120124
end
@@ -144,12 +148,16 @@ let package_flag ({format; in_source; suffix } : spec) dir =
144148
(Ext_js_suffix.to_string suffix)
145149
)
146150

151+
(* FIXME: we should adapt it *)
147152
let package_flag_of_package_specs (package_specs : t)
148-
(dirname : string ) : string =
149-
Spec_set.fold (fun format acc ->
153+
~(dirname : string ) : string =
154+
let res = Spec_set.fold (fun format acc ->
150155
Ext_string.inter2 acc (package_flag format dirname )
151-
) package_specs Ext_string.empty
152-
156+
) package_specs.modules Ext_string.empty in
157+
match package_specs.runtime with
158+
| None -> res
159+
| Some x ->
160+
res ^ " -runtime " ^ x
153161
let default_package_specs suffix =
154162
Spec_set.singleton
155163
{ format = NodeJS ; in_source = false; suffix }
@@ -161,7 +169,7 @@ let default_package_specs suffix =
161169
162170
*)
163171
let get_list_of_output_js
164-
(package_specs : Spec_set.t)
172+
(package_specs : t)
165173
(output_file_sans_extension : string)
166174
=
167175
Spec_set.fold
@@ -174,17 +182,17 @@ let get_list_of_output_js
174182
(if spec.in_source then Bsb_config.rev_lib_bs_prefix basename
175183
else Bsb_config.lib_bs_prefix_of_format spec.format // basename)
176184
:: acc
177-
) package_specs []
185+
) package_specs.modules []
178186

179187

180188
let list_dirs_by
181-
(package_specs : Spec_set.t)
189+
(package_specs : t)
182190
(f : string -> unit)
183191
=
184192
Spec_set.iter (fun (spec : spec) ->
185193
if not spec.in_source then
186194
f (Bsb_config.top_prefix_of_format spec.format)
187-
) package_specs
195+
) package_specs.modules
188196

189197
type json_map = Ext_json_types.t Map_string.t
190198

@@ -201,10 +209,20 @@ let extract_bs_suffix_exn (map : json_map) : Ext_js_suffix.t =
201209
Bsb_exception.config_error config
202210
"expect a string exteion like \".js\" here"
203211

204-
let from_map map =
212+
let from_map ~(cwd:string) map =
205213
let suffix = extract_bs_suffix_exn map in
206-
match map.?(Bsb_build_schemas.package_specs) with
214+
let modules = match map.?(Bsb_build_schemas.package_specs) with
207215
| Some x ->
208216
from_json suffix x
209-
| None -> default_package_specs suffix
217+
| None -> default_package_specs suffix in
218+
let runtime =
219+
match map.?(Bsb_build_schemas.external_stdlib) with
220+
| None -> None
221+
| Some(Str{str; _}) ->
222+
Some (Bsb_pkg.resolve_bs_package ~cwd (Bsb_pkg_types.string_as_package str))
223+
| _ -> assert false in
224+
{
225+
runtime;
226+
modules
227+
}
210228

jscomp/bsb/bsb_package_specs.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type t
2828

2929

3030
val from_map:
31+
cwd:string ->
3132
Ext_json_types.t Map_string.t -> t
3233

3334
val get_list_of_output_js :
@@ -39,8 +40,11 @@ val get_list_of_output_js :
3940
Sample output: {[ -bs-package-output commonjs:lib/js/jscomp/test]}
4041
*)
4142
val package_flag_of_package_specs :
42-
t -> string -> string
43+
t ->
44+
dirname:string ->
45+
string
4346

47+
(* used to ensure each dir does exist *)
4448
val list_dirs_by :
4549
t ->
4650
(string -> unit) ->

jscomp/bsb/bsb_pkg.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ val resolve_bs_package :
3838
cwd:string -> Bsb_pkg_types.t -> string
3939

4040

41+
(** used by watcher *)
4142
val to_list:
4243
(Bsb_pkg_types.t ->
4344
string ->

0 commit comments

Comments
 (0)