Skip to content

Commit 602a09d

Browse files
authored
Merge pull request #4476 from BuckleScript/refactor_build
refactor build to make it easy to add new syntaxes
2 parents 2280dda + 6bdba19 commit 602a09d

18 files changed

+260
-184
lines changed

jscomp/bsb/bsb_db_encode.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ let encode_single (db : Bsb_db.map) (buf : Ext_buffer.t) =
8282
let len_encoding = make_encoding length buf in
8383
Map_string.iter db (fun _ module_info ->
8484
len_encoding buf
85-
(Hash_string.find_exn mapping module_info.dir lsl 1 + Obj.magic module_info.case ));
85+
(Hash_string.find_exn mapping module_info.dir lsl 1 + (Obj.magic (module_info.case : bool) : int)));
8686
nl buf
8787
end
8888
let encode (dbs : Bsb_db.t) buf =

jscomp/bsb/bsb_db_util.ml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let merge (acc : t) (sources : t) : t =
5050

5151
let sanity_check (map : t) =
5252
Map_string.iter map (fun m module_info ->
53-
if module_info.info = Mli then
53+
if module_info.info = Intf then
5454
Bsb_exception.no_implementation m
5555
)
5656

@@ -60,21 +60,21 @@ let sanity_check (map : t) =
6060
let check (x : module_info)
6161
name_sans_extension
6262
case
63-
is_re
63+
syntax_kind
6464
(module_info : Bsb_db.info)
6565
=
6666
let x_ml_info = x.info in
6767
(if x.name_sans_extension <> name_sans_extension
6868
|| x.case <> case
69-
|| x.is_re <> is_re
69+
|| x.syntax_kind <> syntax_kind
7070
|| x_ml_info = module_info
71-
|| x_ml_info = Ml_mli
71+
|| x_ml_info = Impl_intf
7272
then
7373
Bsb_exception.invalid_spec
7474
(Printf.sprintf
7575
"implementation and interface have different path names or different cases %s vs %s"
7676
x.name_sans_extension name_sans_extension));
77-
x.info <- Ml_mli;
77+
x.info <- Impl_intf;
7878
x
7979

8080

@@ -86,25 +86,25 @@ let add_basename
8686
(map : t)
8787
?(error_on_invalid_suffix)
8888
basename : t =
89-
let info = ref Bsb_db.Ml in
90-
let is_re = ref false in
89+
let info = ref Bsb_db.Impl in
90+
let syntax_kind = ref Bsb_db.Ml in
9191
let invalid_suffix = ref false in
92-
(match Ext_filename.get_extension_maybe basename with
93-
| ".ml" ->
92+
let file_suffix = Ext_filename.get_extension_maybe basename in
93+
(match () with
94+
| _ when file_suffix = Literals.suffix_ml ->
9495
()
95-
| ".re" ->
96-
is_re := true
97-
| ".mli" ->
98-
info := Mli
99-
| ".rei" ->
100-
info := Mli;
101-
is_re := true
96+
| _ when file_suffix = Literals.suffix_re ->
97+
syntax_kind := Reason
98+
| _ when file_suffix = Literals.suffix_mli ->
99+
info := Intf
100+
| _ when file_suffix = Literals.suffix_rei ->
101+
info := Intf;
102+
syntax_kind := Reason
102103
| _ ->
103104
invalid_suffix := true
104-
105105
);
106106
let info= !info in
107-
let is_re = !is_re in
107+
let syntax_kind = !syntax_kind in
108108
let invalid_suffix = !invalid_suffix in
109109
if invalid_suffix then
110110
match error_on_invalid_suffix with
@@ -127,7 +127,7 @@ let add_basename
127127
(fun opt_module_info ->
128128
match opt_module_info with
129129
| None ->
130-
{dir ; name_sans_extension ; info ; is_re ; case }
130+
{dir ; name_sans_extension ; info ; syntax_kind ; case }
131131
| Some x ->
132-
check x name_sans_extension case is_re info
132+
check x name_sans_extension case syntax_kind info
133133
)

jscomp/bsb/bsb_ninja_file_groups.ml

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,27 @@ let make_common_shadows
6161
}]
6262

6363

64-
64+
type suffixes = {
65+
impl : string;
66+
intf : string ;
67+
impl_ast : string;
68+
intf_ast : string;
69+
}
70+
71+
let re_suffixes = {
72+
impl = Literals.suffix_re;
73+
intf = Literals.suffix_rei;
74+
impl_ast = Literals.suffix_reast;
75+
intf_ast = Literals.suffix_reiast;
76+
77+
}
78+
79+
let ml_suffixes = {
80+
impl = Literals.suffix_ml;
81+
intf = Literals.suffix_mli;
82+
impl_ast = Literals.suffix_mlast;
83+
intf_ast = Literals.suffix_mliast
84+
}
6585
let emit_module_build
6686
(rules : Bsb_ninja_rule.builtin)
6787
(package_specs : Bsb_package_specs.t)
@@ -72,19 +92,16 @@ let emit_module_build
7292
namespace
7393
(module_info : Bsb_db.module_info)
7494
=
75-
let has_intf_file = module_info.info = Ml_mli in
76-
let is_re = module_info.is_re in
95+
let has_intf_file = module_info.info = Impl_intf in
96+
let config, ast_rule =
97+
match module_info.syntax_kind with
98+
| Reason -> re_suffixes, rules.build_ast_from_re
99+
| Ml -> ml_suffixes, rules.build_ast in
77100
let filename_sans_extension = module_info.name_sans_extension in
78-
let input_impl =
79-
Bsb_config.proj_rel
80-
(filename_sans_extension ^ if is_re then Literals.suffix_re else Literals.suffix_ml ) in
81-
let input_intf =
82-
Bsb_config.proj_rel
83-
(filename_sans_extension ^ if is_re then Literals.suffix_rei else Literals.suffix_mli) in
84-
let output_mlast =
85-
filename_sans_extension ^ if is_re then Literals.suffix_reast else Literals.suffix_mlast in
86-
let output_mliast =
87-
filename_sans_extension ^ if is_re then Literals.suffix_reiast else Literals.suffix_mliast in
101+
let input_impl = Bsb_config.proj_rel (filename_sans_extension ^ config.impl ) in
102+
let input_intf = Bsb_config.proj_rel (filename_sans_extension ^ config.intf) in
103+
let output_mlast = filename_sans_extension ^ config.impl_ast in
104+
let output_mliast = filename_sans_extension ^ config.intf_ast in
88105
let output_d = filename_sans_extension ^ Literals.suffix_d in
89106
let output_filename_sans_extension =
90107
Ext_namespace_encode.make ?ns:namespace filename_sans_extension
@@ -97,11 +114,7 @@ let emit_module_build
97114
make_common_shadows package_specs
98115
(Filename.dirname output_cmi)
99116
in
100-
let ast_rule =
101-
if is_re then
102-
rules.build_ast_from_re
103-
else
104-
rules.build_ast in
117+
105118
Bsb_ninja_targets.output_build oc
106119
~outputs:[output_mlast]
107120
~inputs:[input_impl]

jscomp/common/js_config.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ let jsx_version = ref (-1)
9595

9696
let refmt = ref None
9797

98-
let is_reason = ref false
9998

10099
let js_stdout = ref true
101100

jscomp/common/js_config.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ val force_cmj : bool ref
9090

9191
val jsx_version : int ref
9292
val refmt : string option ref
93-
val is_reason : bool ref
93+
9494

9595
val js_stdout : bool ref
9696

jscomp/core/js_implementation.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ let after_parsing_sig ppf outputprefix ast =
5858
end;
5959
if !Js_config.binary_ast then
6060
begin
61+
let sourcefile = !Location.input_name in
6162
Binary_ast.write_ast
6263
Mli
63-
~sourcefile:!Location.input_name
64-
~output:(outputprefix ^ if !Js_config.is_reason then Literals.suffix_reiast else Literals.suffix_mliast)
64+
~sourcefile
65+
~output:(outputprefix ^ Filename.extension sourcefile ^ "ast")
6566
(* to support relocate to another directory *)
6667
ast
6768

@@ -164,12 +165,12 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
164165
Ml_binary.write_ast Ml !Location.input_name ast oc;
165166
close_out oc ;
166167
end;
167-
if !Js_config.binary_ast then
168-
Binary_ast.write_ast ~sourcefile:!Location.input_name
169-
Ml ~output:(outputprefix ^
170-
if !Js_config.is_reason then Literals.suffix_reast else Literals.suffix_mlast
171-
)
172-
ast ;
168+
if !Js_config.binary_ast then begin
169+
let sourcefile = !Location.input_name in
170+
Binary_ast.write_ast ~sourcefile
171+
Ml ~output:(outputprefix ^ Filename.extension sourcefile ^ "ast")
172+
ast
173+
end ;
173174
if !Js_config.syntax_only then
174175
Warnings.check_fatal ()
175176
else

jscomp/ext/bsb_db.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ type case = bool
2828

2929

3030
type info =
31-
| Mli (* intemediate state *)
32-
| Ml
33-
| Ml_mli
31+
| Intf (* intemediate state *)
32+
| Impl
33+
| Impl_intf
34+
35+
type syntax_kind =
36+
| Ml
37+
| Reason
3438

3539
type module_info =
3640
{
3741
mutable info : info;
3842
dir : string ;
39-
is_re : bool;
43+
syntax_kind : syntax_kind;
4044
case : bool;
4145
name_sans_extension : string ;
4246
}

jscomp/ext/bsb_db.mli

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,24 @@
3333
type case = bool
3434

3535
type info =
36-
| Mli (* intemediate state *)
37-
| Ml
38-
| Ml_mli
36+
| Intf (* intemediate state *)
37+
| Impl
38+
| Impl_intf
3939

40+
type syntax_kind =
41+
| Ml
42+
| Reason
4043

4144

4245
type module_info =
4346
{
4447
mutable info : info;
4548
dir : string;
46-
is_re : bool;
49+
syntax_kind : syntax_kind;
50+
(* This is actually not stored in bsbuild meta info
51+
since creating .d file only emit .cmj/.cmi dependencies, so it does not
52+
need know which syntax it is written
53+
*)
4754
case : bool;
4855
name_sans_extension : string;
4956
}

jscomp/main/js_main.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ let process_implementation_file ppf name =
2222

2323

2424
let setup_reason_context () =
25-
Js_config.is_reason := true;
2625
Lazy.force Super_main.setup;
2726
Lazy.force Reason_outcome_printer_main.setup
2827

jscomp/ounit_tests/ounit_bsb_pkg_tests.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ let s_test1 s a =
3434

3535
let group0 = Map_string.of_list [
3636
"Liba",
37-
{Bsb_db.info = Ml_mli; dir= "a";is_re=false;case = false;
37+
{Bsb_db.info = Impl_intf; dir= "a";syntax_kind=Ml;case = false;
3838
name_sans_extension = "liba"}
3939
]
4040
let group1 = Map_string.of_list [
4141
"Ciba",
42-
{Bsb_db.info = Ml_mli; dir= "b";is_re=false;case = false;
42+
{Bsb_db.info = Impl_intf; dir= "b";syntax_kind=Ml;case = false;
4343
name_sans_extension = "liba"}
4444
]
4545

0 commit comments

Comments
 (0)