Skip to content

Commit 93b8040

Browse files
author
Hongbo Zhang
committed
move dependency into a single file
1 parent 4407586 commit 93b8040

File tree

4 files changed

+137
-91
lines changed

4 files changed

+137
-91
lines changed

jscomp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ release:snapshot
4040
$(NATIVE) -w -a -I bin -I +compiler-libs ocamlcommon.cmxa unix.cmxa $^ -o $@
4141

4242
## order matters
43-
_build/ocamlpack: _build/ext/ext.cmxa _build/common/common.cmxa _build/depends/depends.cmxa _build/depends/ocaml_pack_main.cmx
43+
_build/ocamlpack: _build/ext/ext.cmxa _build/common/common.cmxa _build/depends/depends.cmxa _build/ocamlpack_main.cmx
4444
$(NATIVE) -I +compiler-libs ocamlcommon.cmxa unix.cmxa $^ -o $@
4545

4646
snapshot: ./bin/ocamlpack snapshotcmj

jscomp/depends/ast_extract.ml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,98 @@ let build ppf files parse_implementation parse_interface =
198198
end
199199
) String_map.empty files
200200

201+
202+
203+
let handle_main_file ppf parse_implementation parse_interface main_file =
204+
let dirname = Filename.dirname main_file in
205+
let files =
206+
Sys.readdir dirname
207+
|> Ext_array.to_list_f
208+
(fun source_file ->
209+
if Ext_string.ends_with source_file ".ml" ||
210+
Ext_string.ends_with source_file ".mli" then
211+
Some (Filename.concat dirname source_file)
212+
else None
213+
) in
214+
let ast_table =
215+
build ppf files
216+
parse_implementation
217+
parse_interface in
218+
219+
let visited = Hashtbl.create 31 in
220+
let result = Queue.create () in
221+
let next module_name =
222+
match String_map.find module_name ast_table with
223+
| exception _ -> String_set.empty
224+
| {ast_info = Ml (_, lazy impl, _)} ->
225+
read_parse_and_extract Ml_kind impl
226+
| {ast_info = Mli (_, lazy intf,_)} ->
227+
read_parse_and_extract Mli_kind intf
228+
| {ast_info = Ml_mli(_,lazy impl, _, _, lazy intf, _)}
229+
->
230+
String_set.union
231+
(read_parse_and_extract Ml_kind impl)
232+
(read_parse_and_extract Mli_kind intf)
233+
in
234+
let rec visit visiting path current =
235+
if String_set.mem current visiting then
236+
Bs_exception.error (Bs_cyclic_depends (current::path))
237+
else
238+
if not (Hashtbl.mem visited current)
239+
&& String_map.mem current ast_table then
240+
begin
241+
String_set.iter
242+
(visit
243+
(String_set.add current visiting)
244+
(current::path))
245+
(next current) ;
246+
Queue.push current result;
247+
Hashtbl.add visited current ();
248+
end in
249+
visit (String_set.empty) [] (Ext_filename.module_name_of_file main_file) ;
250+
ast_table, result
251+
252+
253+
let build_queue ppf queue
254+
(ast_table : _ t String_map.t)
255+
after_parsing_impl
256+
after_parsing_sig
257+
=
258+
queue |> Queue.iter (fun modname ->
259+
match String_map.find modname ast_table with
260+
| {ast_info = Ml(source_file,ast, opref)}
261+
->
262+
after_parsing_impl ppf source_file
263+
opref ast
264+
| {ast_info = Mli (source_file,ast,opref) ; }
265+
->
266+
after_parsing_sig ppf source_file
267+
opref ast
268+
| {ast_info = Ml_mli(source_file1,impl,opref1,source_file2,intf,opref2)}
269+
->
270+
after_parsing_sig ppf source_file1 opref1 intf ;
271+
after_parsing_impl ppf source_file2 opref2 impl
272+
| exception Not_found -> assert false
273+
)
274+
275+
276+
let build_lazy_queue ppf queue (ast_table : _ t String_map.t)
277+
after_parsing_impl
278+
after_parsing_sig
279+
=
280+
queue |> Queue.iter (fun modname ->
281+
match String_map.find modname ast_table with
282+
| {ast_info = Ml(source_file,lazy ast, opref)}
283+
->
284+
after_parsing_impl ppf source_file
285+
opref ast
286+
| {ast_info = Mli (source_file,lazy ast,opref) ; }
287+
->
288+
after_parsing_sig ppf source_file
289+
opref ast
290+
| {ast_info = Ml_mli(source_file1,lazy impl,opref1,source_file2,lazy intf,opref2)}
291+
->
292+
after_parsing_sig ppf source_file1 opref1 intf ;
293+
after_parsing_impl ppf source_file2 opref2 impl
294+
| exception Not_found -> assert false
295+
)

jscomp/depends/ast_extract.mli

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type module_name = private string
3333

3434
module String_set = Depend.StringSet
3535

36-
val read_parse_and_extract : 'a kind -> 'a -> String_set.t
36+
3737

3838
type ('a,'b) ast_info =
3939
| Ml of
@@ -82,3 +82,26 @@ val build :
8282
(Format.formatter -> string -> 'b) ->
8383
(Format.formatter -> string -> 'c) ->
8484
('b, 'c) t String_map.t
85+
86+
val handle_main_file :
87+
Format.formatter ->
88+
(Format.formatter -> string -> Parsetree.structure lazy_t) ->
89+
(Format.formatter -> string -> Parsetree.signature lazy_t) ->
90+
string ->
91+
(Parsetree.structure lazy_t, Parsetree.signature lazy_t) t String_map.t *
92+
string Queue.t
93+
94+
95+
val build_queue :
96+
Format.formatter ->
97+
String_map.key Queue.t ->
98+
(Parsetree.structure, Parsetree.signature) t String_map.t ->
99+
(Format.formatter -> string -> string -> Parsetree.structure -> unit) ->
100+
(Format.formatter -> string -> string -> Parsetree.signature -> unit) -> unit
101+
102+
val build_lazy_queue :
103+
Format.formatter ->
104+
String_map.key Queue.t ->
105+
(Parsetree.structure lazy_t, Parsetree.signature lazy_t) t String_map.t ->
106+
(Format.formatter -> string -> string -> Parsetree.structure -> unit) ->
107+
(Format.formatter -> string -> string -> Parsetree.signature -> unit) -> unit

jscomp/ocaml_batch_compile.ml

Lines changed: 17 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -24,108 +24,27 @@
2424

2525

2626

27-
let build_queue ppf queue (ast_table : _ Ast_extract.t String_map.t) =
28-
queue |> Queue.iter (fun modname ->
29-
match String_map.find modname ast_table with
30-
| {ast_info = Ml(source_file,ast, opref)}
31-
->
32-
Js_implementation.after_parsing_impl ppf source_file
33-
opref ast
34-
| {ast_info = Mli (source_file,ast,opref) ; }
35-
->
36-
Js_implementation.after_parsing_sig ppf source_file
37-
opref ast
38-
| {ast_info = Ml_mli(source_file1,impl,opref1,source_file2,intf,opref2)}
39-
->
40-
Js_implementation.after_parsing_sig ppf source_file1 opref1 intf ;
41-
Js_implementation.after_parsing_impl ppf source_file2 opref2 impl
42-
| exception Not_found -> assert false
43-
)
44-
45-
let build_lazy_queue ppf queue (ast_table : _ Ast_extract.t String_map.t) =
46-
queue |> Queue.iter (fun modname ->
47-
match String_map.find modname ast_table with
48-
| {ast_info = Ml(source_file,lazy ast, opref)}
49-
->
50-
Js_implementation.after_parsing_impl ppf source_file
51-
opref ast
52-
| {ast_info = Mli (source_file,lazy ast,opref) ; }
53-
->
54-
Js_implementation.after_parsing_sig ppf source_file
55-
opref ast
56-
| {ast_info = Ml_mli(source_file1,lazy impl,opref1,source_file2,lazy intf,opref2)}
57-
->
58-
Js_implementation.after_parsing_sig ppf source_file1 opref1 intf ;
59-
Js_implementation.after_parsing_impl ppf source_file2 opref2 impl
60-
| exception Not_found -> assert false
61-
)
62-
63-
64-
6527
module String_set = Depend.StringSet
6628

67-
68-
let handle_main_file ppf main_file =
69-
let dirname = Filename.dirname main_file in
70-
let files =
71-
Sys.readdir dirname
72-
|> Ext_array.to_list_f
73-
(fun source_file ->
74-
if Ext_string.ends_with source_file ".ml" ||
75-
Ext_string.ends_with source_file ".mli" then
76-
Some (Filename.concat dirname source_file)
77-
else None
78-
) in
79-
let ast_table =
80-
Ast_extract.build ppf files
81-
Ocaml_parse.lazy_parse_implementation
82-
Ocaml_parse.lazy_parse_interface in
83-
84-
let visited = Hashtbl.create 31 in
85-
let result = Queue.create () in
86-
let next module_name =
87-
match String_map.find module_name ast_table with
88-
| exception _ -> String_set.empty
89-
| {ast_info = Ml (_, lazy impl, _)} ->
90-
Ast_extract.read_parse_and_extract Ml_kind impl
91-
| {ast_info = Mli (_, lazy intf,_)} ->
92-
Ast_extract.read_parse_and_extract Mli_kind intf
93-
| {ast_info = Ml_mli(_,lazy impl, _, _, lazy intf, _)}
94-
->
95-
String_set.union
96-
(Ast_extract.read_parse_and_extract Ml_kind impl)
97-
(Ast_extract.read_parse_and_extract Mli_kind intf)
98-
in
99-
let rec visit visiting path current =
100-
if String_set.mem current visiting then
101-
Bs_exception.error (Bs_cyclic_depends (current::path))
102-
else
103-
if not (Hashtbl.mem visited current)
104-
&& String_map.mem current ast_table then
105-
begin
106-
String_set.iter
107-
(visit
108-
(String_set.add current visiting)
109-
(current::path))
110-
(next current) ;
111-
Queue.push current result;
112-
Hashtbl.add visited current ();
113-
end in
114-
visit (String_set.empty) [] (Ext_filename.module_name_of_file main_file) ;
29+
let process_result ppf main_file ast_table result =
11530
if Js_config.get_diagnose () then
11631
Format.fprintf Format.err_formatter
11732
"Order: @[%a@]@."
11833
(Ext_format.pp_print_queue
11934
~pp_sep:Format.pp_print_space
12035
Format.pp_print_string)
12136
result ;
122-
build_lazy_queue ppf result ast_table;
37+
Ast_extract.build_lazy_queue ppf result ast_table
38+
Js_implementation.after_parsing_impl
39+
Js_implementation.after_parsing_sig
40+
;
12341
if not (!Clflags.compile_only) then
12442
Sys.command
12543
("node " ^ Filename.chop_extension main_file ^ ".js")
12644
else 0
12745

12846

47+
12948
let batch_compile ppf files main_file =
13049
Compenv.readenv ppf Before_compile;
13150
Compmisc.init_path false;
@@ -135,11 +54,20 @@ let batch_compile ppf files main_file =
13554
Ast_extract.build ppf files
13655
Ocaml_parse.parse_implementation
13756
Ocaml_parse.parse_interface in
138-
build_queue ppf (Ast_extract.sort (fun x -> x ) (fun x -> x )ast_table) ast_table
57+
Ast_extract.build_queue ppf
58+
(Ast_extract.sort (fun x -> x ) (fun x -> x )ast_table)
59+
ast_table
60+
Js_implementation.after_parsing_impl
61+
Js_implementation.after_parsing_sig
13962
end
14063
;
14164
if String.length main_file <> 0 then
142-
handle_main_file ppf main_file
65+
let ast_table, result =
66+
Ast_extract.handle_main_file ppf
67+
Ocaml_parse.lazy_parse_implementation
68+
Ocaml_parse.lazy_parse_interface
69+
main_file in
70+
process_result ppf main_file ast_table result
14371
else 0
14472

14573

0 commit comments

Comments
 (0)