Skip to content

Commit 5de6ec2

Browse files
committed
Add ppx as a dependency for building source
1 parent 8312800 commit 5de6ec2

9 files changed

+96
-51
lines changed

jscomp/bsb/bsb_build_util.ml

+23-21
Original file line numberDiff line numberDiff line change
@@ -67,33 +67,35 @@ let convert_and_resolve_path : string -> string -> string =
6767
./foo/bar => /absolute/path/to/projectRoot/./foo/bar
6868
Input is node path, output is OS dependent (normalized) path
6969
*)
70-
let resolve_bsb_magic_file ~cwd ~desc p =
70+
let resolve_bsb_magic_file ~cwd ~desc p : string * bool =
71+
7172
let no_slash = Ext_string.no_slash_idx p in
7273
if no_slash < 0 then
73-
p (*FIXME: better error message for "" input *)
74+
(* Single file FIXME: better error message for "" input *)
75+
p, false
7476
else
75-
let first_char = String.unsafe_get p 0 in
76-
if Filename.is_relative p &&
77-
first_char <> '.' then
78-
let package_name, rest =
79-
Bsb_pkg_types.extract_pkg_name_and_file p
80-
in
81-
let relative_path =
77+
let first_char = String.unsafe_get p 0 in
78+
if Filename.is_relative p &&
79+
first_char <> '.' then
80+
let package_name, rest =
81+
Bsb_pkg_types.extract_pkg_name_and_file p
82+
in
83+
let relative_path =
8284
if Ext_sys.is_windows_or_cygwin then Ext_string.replace_slash_backward rest
8385
else rest in
84-
(* let p = if Ext_sys.is_windows_or_cygwin then Ext_string.replace_slash_backward p else p in *)
85-
let package_dir = Bsb_pkg.resolve_bs_package ~cwd package_name in
86-
let path = package_dir // relative_path in
87-
if Sys.file_exists path then path
88-
else
89-
begin
90-
Bsb_log.error "@{<error>Could not resolve @} %s in %s@." p cwd ;
91-
failwith (p ^ " not found when resolving " ^ desc)
92-
end
86+
(* let p = if Ext_sys.is_windows_or_cygwin then Ext_string.replace_slash_backward p else p in *)
87+
let package_dir = Bsb_pkg.resolve_bs_package ~cwd package_name in
88+
let path = package_dir // relative_path in
89+
if Sys.file_exists path then path, true
90+
else
91+
begin
92+
Bsb_log.error "@{<error>Could not resolve @} %s in %s@." p cwd ;
93+
failwith (p ^ " not found when resolving " ^ desc)
94+
end
9395

94-
else
95-
(* relative path [./x/y]*)
96-
convert_and_resolve_path cwd p
96+
else
97+
(* relative path [./x/y]*)
98+
convert_and_resolve_path cwd p, true
9799

98100

99101

jscomp/bsb/bsb_build_util.mli

+9-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,15 @@ val get_list_string :
8585
Ext_json_types.t array ->
8686
string list
8787

88-
89-
val resolve_bsb_magic_file : cwd:string -> desc:string -> string -> string
88+
(* [resolve_bsb_magic_file]
89+
returns a tuple (path,checked)
90+
when checked is true, it means such file should exist without depending on env
91+
*)
92+
val resolve_bsb_magic_file :
93+
cwd:string ->
94+
desc:string ->
95+
string ->
96+
string * bool
9097

9198
type package_context = {
9299
cwd : string ;

jscomp/bsb/bsb_config_parse.ml

+22-12
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ let interpret_json
138138
since it is external configuration, no {!Bsb_build_util.convert_and_resolve_path}
139139
*)
140140
let bsc_flags = ref Bsb_default.bsc_flags in
141-
let ppx_flags = ref [] in
141+
let ppx_files : string list ref = ref [] in
142+
let ppx_checked_files : string list ref = ref [] in
142143
let js_post_build_cmd = ref None in
143144
let built_in_package = ref None in
144145
let generate_merlin = ref true in
@@ -175,8 +176,8 @@ let interpret_json
175176
| Some (Str {str})
176177
->
177178
Refmt_custom
178-
(Bsb_build_util.resolve_bsb_magic_file
179-
~cwd ~desc:Bsb_build_schemas.refmt str)
179+
(fst (Bsb_build_util.resolve_bsb_magic_file
180+
~cwd ~desc:Bsb_build_schemas.refmt str))
180181
| Some config ->
181182
Bsb_exception.config_error config "expect version 2 or 3"
182183
| None ->
@@ -189,11 +190,11 @@ let interpret_json
189190
Some { path =
190191
match String_map.find_opt obj Bsb_build_schemas.path with
191192
| None ->
192-
Bsb_build_util.resolve_bsb_magic_file
193+
fst @@ Bsb_build_util.resolve_bsb_magic_file
193194
~cwd ~desc:"gentype.exe"
194195
"gentype/gentype.exe"
195196
| Some (Str {str}) ->
196-
Bsb_build_util.resolve_bsb_magic_file
197+
fst @@ Bsb_build_util.resolve_bsb_magic_file
197198
~cwd ~desc:"gentype.exe" str
198199
| Some config ->
199200
Bsb_exception.config_error config
@@ -264,7 +265,7 @@ let interpret_json
264265
| Some (Str {str = p }) ->
265266
if p = "" then failwith "invalid pp, empty string found"
266267
else
267-
Some (Bsb_build_util.resolve_bsb_magic_file ~cwd ~desc:Bsb_build_schemas.pp_flags p)
268+
Some (fst @@ Bsb_build_util.resolve_bsb_magic_file ~cwd ~desc:Bsb_build_schemas.pp_flags p)
268269
| Some x ->
269270
Bsb_exception.errorf ~loc:(Ext_json.loc_of x) "pp-flags expected a string"
270271
| None ->
@@ -294,7 +295,7 @@ let interpret_json
294295

295296
|? (Bsb_build_schemas.js_post_build, `Obj begin fun m ->
296297
m |? (Bsb_build_schemas.cmd , `Str (fun s ->
297-
js_post_build_cmd := Some (Bsb_build_util.resolve_bsb_magic_file ~cwd ~desc:Bsb_build_schemas.js_post_build s)
298+
js_post_build_cmd := Some (fst @@ Bsb_build_util.resolve_bsb_magic_file ~cwd ~desc:Bsb_build_schemas.js_post_build s)
298299

299300
)
300301
)
@@ -313,10 +314,18 @@ let interpret_json
313314
|? (Bsb_build_schemas.bs_external_includes, `Arr (fun s -> bs_external_includes := get_list_string s))
314315
|? (Bsb_build_schemas.bsc_flags, `Arr (fun s -> bsc_flags := Bsb_build_util.get_list_string_acc s !bsc_flags))
315316
|? (Bsb_build_schemas.ppx_flags, `Arr (fun s ->
316-
ppx_flags := Ext_list.map (get_list_string s) (fun p ->
317+
let args = get_list_string s in
318+
let a,b = Ext_list.map_split_opt args (fun p ->
317319
if p = "" then failwith "invalid ppx, empty string found"
318-
else Bsb_build_util.resolve_bsb_magic_file ~cwd ~desc:Bsb_build_schemas.ppx_flags p
319-
)
320+
else
321+
let file, checked =
322+
Bsb_build_util.resolve_bsb_magic_file ~cwd ~desc:Bsb_build_schemas.ppx_flags p
323+
in
324+
let some_file = Some file in
325+
some_file, if checked then some_file else None
326+
) in
327+
ppx_files := a ;
328+
ppx_checked_files := b
320329
))
321330

322331
|? (Bsb_build_schemas.cut_generators, `Bool (fun b -> cut_generators := b))
@@ -385,8 +394,9 @@ let interpret_json
385394
warning = warning;
386395
external_includes = !bs_external_includes;
387396
bsc_flags = !bsc_flags ;
388-
ppx_flags = !ppx_flags ;
389-
pp_flags = pp_flags ;
397+
ppx_files = !ppx_files ;
398+
ppx_checked_files = !ppx_checked_files;
399+
pp_file = pp_flags ;
390400
bs_dependencies = !bs_dependencies;
391401
bs_dev_dependencies = !bs_dev_dependencies;
392402
refmt;

jscomp/bsb/bsb_config_types.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ type t =
5151
(* CapitalPackage *)
5252
external_includes : string list ;
5353
bsc_flags : string list ;
54-
ppx_flags : string list ;
55-
pp_flags : string option;
54+
ppx_files : string list ;
55+
ppx_checked_files : string list ;
56+
pp_file : string option;
5657
bs_dependencies : dependencies;
5758
bs_dev_dependencies : dependencies;
5859
built_in_dependency : dependency option;

jscomp/bsb/bsb_merlin_gen.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ let merlin_file_gen ~cwd
105105
built_in_ppx
106106
({bs_file_groups = res_files ;
107107
generate_merlin;
108-
ppx_flags;
109-
pp_flags ;
108+
ppx_files;
109+
pp_file;
110110
bs_dependencies;
111111
bs_dev_dependencies;
112112
bsc_flags;
@@ -121,10 +121,10 @@ let merlin_file_gen ~cwd
121121
if generate_merlin then begin
122122
let buffer = Buffer.create 1024 in
123123
output_merlin_namespace buffer namespace;
124-
Ext_list.iter ppx_flags (fun x ->
124+
Ext_list.iter ppx_files (fun x ->
125125
Buffer.add_string buffer (merlin_flg_ppx ^ x )
126126
);
127-
Ext_option.iter pp_flags (fun x ->
127+
Ext_option.iter pp_file (fun x ->
128128
Buffer.add_string buffer (merlin_flg_pp ^ x)
129129
);
130130
Ext_option.iter reason_react_jsx

jscomp/bsb/bsb_ninja_file_groups.ml

+22-4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ let emit_impl_build
9999
(package_specs : Bsb_package_specs.t)
100100
(group_dir_index : Bsb_dir_index.t)
101101
oc
102+
~has_checked_ppx
102103
~bs_suffix
103104
~no_intf_file:(no_intf_file : bool)
104105
js_post_build_cmd
@@ -131,6 +132,7 @@ let emit_impl_build
131132
Bsb_ninja_util.output_build oc
132133
~output:output_mlast
133134
~input
135+
~implicit_deps:(if has_checked_ppx then [ "${ppx_checked_files}" ] else [])
134136
~rule:( if is_re then
135137
Bsb_ninja_rule.build_ast_and_module_sets_from_re
136138
else
@@ -140,6 +142,7 @@ let emit_impl_build
140142
~output:output_mlastd
141143
~input:output_mlast
142144
~rule:Bsb_ninja_rule.build_bin_deps
145+
~implicit_deps:(if has_checked_ppx then [ "${ppx_checked_files}" ] else [])
143146
?shadows:(if Bsb_dir_index.is_lib_dir group_dir_index then None
144147
else Some [{Bsb_ninja_util.key = Bsb_build_schemas.bsb_dir_group ;
145148
op =
@@ -174,6 +177,7 @@ let emit_intf_build
174177
(group_dir_index : Bsb_dir_index.t)
175178
oc
176179
~is_re
180+
~has_checked_ppx
177181
namespace
178182
filename_sans_extension
179183
: info =
@@ -200,11 +204,14 @@ let emit_intf_build
200204
(if is_re then filename_sans_extension ^ Literals.suffix_rei
201205
else filename_sans_extension ^ Literals.suffix_mli))
202206
~rule:(if is_re then Bsb_ninja_rule.build_ast_and_module_sets_from_rei
203-
else Bsb_ninja_rule.build_ast_and_module_sets);
207+
else Bsb_ninja_rule.build_ast_and_module_sets)
208+
~implicit_deps:(if has_checked_ppx then [ "${ppx_checked_files}" ] else [])
209+
;
204210
Bsb_ninja_util.output_build oc
205211
~output:output_mliastd
206212
~input:output_mliast
207213
~rule:Bsb_ninja_rule.build_bin_deps
214+
~implicit_deps:(if has_checked_ppx then [ "${ppx_checked_files}" ] else [])
208215
?shadows:(if Bsb_dir_index.is_lib_dir group_dir_index then None
209216
else Some [{
210217
key = Bsb_build_schemas.bsb_dir_group;
@@ -225,6 +232,7 @@ let handle_module_info
225232
(group_dir_index : Bsb_dir_index.t)
226233
(package_specs : Bsb_package_specs.t)
227234
js_post_build_cmd
235+
~has_checked_ppx
228236
~bs_suffix
229237
oc module_name
230238
( {name_sans_extension = input} as module_info : Bsb_db.module_info)
@@ -233,10 +241,11 @@ let handle_module_info
233241
match module_info.ml_info, module_info.mli_info with
234242
| Ml_source (impl_is_re,_),
235243
Mli_source(intf_is_re,_) ->
236-
emit_impl_build
244+
emit_impl_build
237245
package_specs
238246
group_dir_index
239247
oc
248+
~has_checked_ppx
240249
~bs_suffix
241250
~no_intf_file:false
242251
~is_re:impl_is_re
@@ -247,6 +256,7 @@ let handle_module_info
247256
package_specs
248257
group_dir_index
249258
oc
259+
~has_checked_ppx
250260
~is_re:intf_is_re
251261
namespace
252262
input
@@ -255,6 +265,7 @@ let handle_module_info
255265
package_specs
256266
group_dir_index
257267
oc
268+
~has_checked_ppx
258269
~bs_suffix
259270
~no_intf_file:true
260271
js_post_build_cmd
@@ -263,6 +274,7 @@ let handle_module_info
263274
input
264275
| Ml_empty, Mli_source(is_re,_) ->
265276
emit_intf_build
277+
~has_checked_ppx
266278
package_specs
267279
group_dir_index
268280
oc
@@ -274,6 +286,7 @@ let handle_module_info
274286

275287
let handle_file_group
276288
oc
289+
~(has_checked_ppx : bool)
277290
~bs_suffix
278291
~custom_rules
279292
~package_specs
@@ -295,6 +308,7 @@ let handle_file_group
295308
if installable then
296309
String_hash_set.add files_to_install (Bsb_db.filename_sans_suffix_of_module_info module_info);
297310
(handle_module_info
311+
~has_checked_ppx
298312
~bs_suffix
299313
group.dir_index
300314
package_specs js_post_build_cmd
@@ -307,15 +321,19 @@ let handle_file_group
307321

308322

309323
let handle_file_groups
310-
oc ~package_specs
324+
oc
325+
~has_checked_ppx
326+
~package_specs
311327
~bs_suffix
312328
~js_post_build_cmd
313329
~files_to_install ~custom_rules
314330
(file_groups : Bsb_file_groups.file_groups)
315331
namespace (st : info) : info =
316332
Ext_list.fold_left file_groups st
317333
(handle_file_group
318-
oc ~bs_suffix ~package_specs ~custom_rules ~js_post_build_cmd
334+
oc
335+
~has_checked_ppx
336+
~bs_suffix ~package_specs ~custom_rules ~js_post_build_cmd
319337
files_to_install
320338
namespace
321339
)

jscomp/bsb/bsb_ninja_file_groups.mli

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ val zero : info
3131

3232
val handle_file_groups :
3333
out_channel ->
34+
has_checked_ppx:bool ->
3435
package_specs:Bsb_package_specs.t ->
3536
bs_suffix:bool ->
3637
js_post_build_cmd:string option ->

jscomp/bsb/bsb_ninja_gen.ml

+11-4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ let output_ninja_and_namespace_map
5757
package_name;
5858
external_includes;
5959
bsc_flags ;
60-
ppx_flags;
61-
pp_flags ;
60+
pp_file;
61+
ppx_files ;
62+
ppx_checked_files;
6263
bs_dependencies;
6364
bs_dev_dependencies;
6465
refmt;
@@ -79,7 +80,7 @@ let output_ninja_and_namespace_map
7980
let bsc = bsc_dir // bsc_exe in (* The path to [bsc.exe] independent of config *)
8081
let bsdep = bsc_dir // bsb_helper_exe in (* The path to [bsb_heler.exe] *)
8182
let cwd_lib_bs = cwd // Bsb_config.lib_bs in
82-
let ppx_flags = Bsb_build_util.ppx_flags ppx_flags in
83+
let ppx_flags = Bsb_build_util.ppx_flags ppx_files in
8384
let bsc_flags = String.concat Ext_string.single_space bsc_flags in
8485
let refmt_flags = String.concat Ext_string.single_space refmt_flags in
8586
let oc = open_out_bin (cwd_lib_bs // Literals.build_ninja) in
@@ -139,7 +140,7 @@ let output_ninja_and_namespace_map
139140
|] oc
140141
in
141142
let () =
142-
Ext_option.iter pp_flags (fun flag ->
143+
Ext_option.iter pp_file (fun flag ->
143144
Bsb_ninja_util.output_kv Bsb_ninja_global_vars.pp_flags
144145
(Bsb_build_util.pp_flag flag) oc
145146
);
@@ -149,6 +150,11 @@ let output_ninja_and_namespace_map
149150
("-bs-gentype " ^ path) oc
150151
)
151152
;
153+
if ppx_checked_files <> [] then
154+
Bsb_ninja_util.output_kv Bsb_ninja_global_vars.ppx_checked_files
155+
(String.concat " " ppx_files) oc
156+
;
157+
152158
Bsb_ninja_util.output_kvs
153159
[|
154160
Bsb_ninja_global_vars.bs_package_flags, bs_package_flags ;
@@ -236,6 +242,7 @@ let output_ninja_and_namespace_map
236242
(** Generate build statement for each file *)
237243
let all_info =
238244
Bsb_ninja_file_groups.handle_file_groups oc
245+
~has_checked_ppx:(ppx_checked_files <> [])
239246
~bs_suffix
240247
~custom_rules
241248
~js_post_build_cmd

jscomp/bsb/bsb_ninja_global_vars.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ let bsdep = "bsdep"
3434
let bsc_flags = "bsc_flags"
3535

3636
let ppx_flags = "ppx_flags"
37-
37+
let ppx_checked_files = "ppx_checked_files"
3838
let pp_flags = "pp_flags"
39-
4039
let bs_package_includes = "bs_package_includes"
4140

4241
let bs_package_dev_includes = "bs_package_dev_includes"

0 commit comments

Comments
 (0)