forked from facebookarchive/pfff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_codegraph.ml
728 lines (657 loc) · 24.9 KB
/
main_codegraph.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
(*
* Please imagine a long and boring gnu-style copyright notice
* appearing just here.
*)
open Common
module Model = Model3
module View = View3
module E = Database_code
module GC = Graph_code
module GC2 = Graph_code_opti
module DM = Dependencies_matrix_code
(*****************************************************************************)
(* Purpose *)
(*****************************************************************************)
(*
* Main entry point of codegraph, a package/module/type/function/...
* hierarchical dependency visualizer using mainly a Dependency
* Structure Matrix (DSM).
* A node-link display of hierarchical graphs (or hypergraphs) would be nice
* too, but it is far more complex to draw than matrices and does
* not scale as well visually apparently.
* See http://en.wikipedia.org/wiki/Design_structure_matrix
*
* It seems there are a few commercial projects using DSM (Ndepend,
* Structure101, Intellij), so this looks like a viable direction to pursue
* to visualize a software architecture.
*
* requirements:
* - different granularities for x-to-x relationships
* (packages to packages, modules to packages, functions, constructors, etc),
* so one can get:
* * package (or directory) projection to reduce the size of the graph and
* get a high-level view of the architecture ("package mode")
* * with or without external/ dependencies
* * possibiltiy to get a slice of the graph for just a directory
* with a package (or directory) projection for external dependencies
* ("module mode")
* * possiblity to zoom to see the actual functions of a package involved
* in a dependency. This is especially useful for edges where
* we don't understand why there exists a dependency.
* - variable arrow size (but the count number in the matrix does that too)
* - variable node size (but the count number in same row does that too)
*
* This tool also contains some actions to generate data for different
* graph visualizer, e.g. Gephi, Guess. todo? backend for Graphviz? Phylomel?
* old: $ pm_depend [-lang X] [-with-extern] [-depth n] -o filename /path/dir
*
* related work:
* - Lattix, the startup where the original paper on DSM at OOPSLA'05
* comes from.
* - Ndepend,
* http://www.ndepend.com/Doc_VS_Arch.aspx
* http://codebetter.com/patricksmacchia/2009/08/24/identify-code-structure-patterns-at-a-glance/
* - Structure101
* http://www.headwaysoftware.com/products/index.php#page-top
* - Intellij IDEA dsm tool
* http://blogs.jetbrains.com/idea/2008/01/intellij-idea-dependency-analysis-with-dsm/
* http://www.jetbrains.com/idea/features/dependency_analysis.html
*
* - http://depfind.sourceforge.net/, a dependency extraction tool for
* Java
*
* - http://mcis.polymtl.ca/~bram/makao/index.html also use GUESS
* and Prolog :)
* - http://infotectonica.com/juliet/tour/, seems more oriented on
* query, anserwing questions like who uses this field.
*
* - google search images: dependency+graph+visualization, get many
* links from there
*
*
* history:
* - quick look at work on software architecture because of Banatre
* while a master student at IRISA, and later Schmidt, while a PhD,
* looking at work of Shaw and Garlan and the different
* architecture patterns (whiteboard, pipe, layers, etc).
* - started to draw hypergraphs of architecture while supervising a
* student project at EMN (submarine game), and advocated they were
* better than UML diagrams (I think after reading Harel's papers on
* history of statecharts in HOPL-III)
* - dir to dir dependencies during coccinelle project?
* Projections were hardcoded each time for each use.
* No generic framework (like the hierarchical dependency matrix).
* Done for C (then for PHP later, and then for OCaml far later).
* - very nice picture of architecture of Linux kernel sent by Gilles,
* the "map of the Linux kernel"
* - found that having a graph of module dependencies was very useful
* when refactored the c-- and mmm codebase, thx to ocamldot.
* But felt the need to have variable-size arrows (and nodes) and also
* the ability to get more information when clicking on an edge,
* to actually see what are the functions involved in a dependency
* for instance.
* - flibotonomy by Greg Scheschte for PHP, but focused on the nodes
* instead of the edges (which I think are more important).
* - overlay, and cmf -y to display dependencies at "package" level
* - pm_depend, ocaml dependencies backend, ~package_depth, ~with_extern.
* In some ways it extracts the dependency information I have
* in my Makefiles where I care about the order of the directories
* and files. The ~package_depth and ~with_extern parameters are just
* special cases of the general idea of displaying at different
* granularity dependencies depending on the directory.
* Finally it was limited to just package/module (pm_depend) but quickly
* you want to know the actual functions/classes that are involved in
* a dependency.
* - gephi/guess visualization, but even with -no_extern, it does not
* scale very well for www. It's ok for pfff, but even for
* the full source of pfff the graph is quite noisy.
* - discover DSM of ndepend.com while doing a google search images on
* "dependency+graph+visualization"
*
* - gradually realize the importance of layered structures
* (which are actually enforced in OCaml by the linker)
* - gradually realize the importance of dependencies and how
* they are at the essence of software architecture. Code is
* a tree when looked locally (AST), but it's really a graph
* of dependencies when looked globally.
*
* todo:
* - can codegraph does a good job to convey the software architecture
* of codegraph itself? does it show clearly that Graph_code.graph and
* dependencies_matrix_code.dm are the essential data structures? And
* can it show the important fields?
* - maybe edge-bundles could make the node-link display approach
* scale better.
* - generate a node-link graph for the current matrix configuration; use
* graphviz as a first step.
*
* (comments that were in graph_modules_packages_ml.ml)
* alternatives:
* - ocamldoc -dot ...
* But if there is one parse error or a module not found, then
* ocamldoc fails. Also there is no package "projection" so it's
* hard to apply on large projects. There is also no with-extern view
* with the slice of the graph to a directory.
* TODO it can potentially support more code though, by using camlp4.
* - ocamldoc with graphviz
* graphviz does not do variable node size for free as in gephi
* - ocamldoc -dot-reduce ...
* The -dot-reduce is good for layering, but sometimes
* it's also good to see things without the reduction (especially
* with gephi). See for instance the graph for tiger with ocamldoc
* vs pm_depend. I can see all the real callers to option.ml.
* TODO the reduce and layering is also useful
* - ocamldoc -dot-colors
* TODO this is useful.
* It's somehow covered by the strongly-connected + coloring in gephi.
* - graphviz backend? graphviz is good for layers, but
* you lose space because the high-level stuff is at the top but alone.
* With gephi/fatlas, by putting high-level stuff at the center,
* you lose less space? Also graphviz does not scale very well.
*)
(*****************************************************************************)
(* Flags *)
(*****************************************************************************)
let verbose = ref false
let lang = ref "ml"
let deps_style = ref DM.DepsInOut
let skip_list = ref None
let output_dir = ref None
(* action mode *)
let action = ref ""
(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)
let constraints_of_info_txt info_txt =
let h = Hashtbl.create 101 in
pr2_gen info_txt;
let rec aux current node =
match node with
| Common2.Tree (node, xs) ->
let title = node.Outline.title in
let entry =
match title with
| "__ROOT__" -> "."
| _ -> Filename.concat current title
in
let children = xs +> List.map (fun (Common2.Tree (node, _)) ->
(match entry with
| "." -> node.Outline.title
| _ -> Filename.concat entry node.Outline.title
)
)
in
if not (null children) then begin
pr2_gen (entry, children);
Hashtbl.add h entry children;
let new_current =
match entry with
| "." -> ""
| _ -> entry
in
List.iter (aux new_current) xs
end
in
aux "" info_txt;
h
(*****************************************************************************)
(* Model Helpers *)
(*****************************************************************************)
let dep_file_of_dir dir =
Filename.concat dir "graph_code.marshall"
let skip_file_of_dir dir =
Filename.concat dir "skip_list.txt"
let build_model root =
let file = dep_file_of_dir root in
let g = Graph_code.load file in
let gopti =
Common.cache_computation ~verbose:!verbose file ".opti"
(fun () -> Graph_code_opti.convert g)
in
(* todo: find -name "info.txt" *)
let constraints =
if Sys.file_exists (Filename.concat root "info.txt")
then
let info_txt = Info_code.load (Filename.concat root "info.txt") in
constraints_of_info_txt info_txt
else Hashtbl.create 0
in
{ Model.g_deprecated = g; gopti = gopti;
root = Common.realpath root;
constraints
}
let dir_node xs =
(Common.join "/" xs, Database_code.Dir)
let package_node xs =
(Common.join "." xs, Database_code.Package)
(*****************************************************************************)
(* Language specific, building the graph *)
(*****************************************************************************)
let build_graph_code lang root =
let pwd = Sys.getcwd () in
let skip_file =
match !skip_list with
| None -> skip_file_of_dir pwd
| Some f -> f
in
let skip_list =
if Sys.file_exists skip_file
then begin
if !verbose then pr2 (spf "Using skip file: %s" skip_file);
Skip_code.load skip_file
end
else []
in
let g =
match lang with
| "ml" -> Graph_code_ml.build ~verbose:!verbose root skip_list
| "cmt" -> Graph_code_cmt.build ~verbose:!verbose root skip_list
| "php" -> Graph_code_php.build ~verbose:!verbose (Left root) skip_list
| "web" -> raise Todo
| "c" -> Graph_code_c.build ~verbose:!verbose root skip_list
| "objc" -> Graph_code_objc.build ~verbose:!verbose root skip_list
| "clang2" -> Graph_code_clang.build ~verbose:!verbose root skip_list
| "java" -> Graph_code_java.build ~verbose:!verbose root skip_list
| "bytecode" ->
let graph_code_java =
None
(* Some (Graph_code_java.build ~verbose:!verbose ~only_defs:true
root skip_list)
*)
in
Graph_code_bytecode.build ~verbose:!verbose ~graph_code_java
root skip_list
| "dot" -> Graph_code.graph_of_dotfile (Filename.concat root "graph.dot")
| _ -> failwith ("language not supported: " ^ lang)
in
let output_dir =
match !output_dir with
| None -> pwd
| Some d -> d
in
Graph_code.save g (dep_file_of_dir output_dir)
(*****************************************************************************)
(* Language specific, building stdlib *)
(*****************************************************************************)
let build_stdlib lang root dst =
let skip_list =
if Sys.file_exists (skip_file_of_dir root)
then Skip_code.load (skip_file_of_dir root)
else []
in
match lang with
| "java" ->
Builtins_java.extract_from_sources ~skip_list ~src:root ~dst
| "clang" ->
Uninclude_clang.uninclude ~verbose:!verbose root skip_list dst
| _ -> failwith ("language not supported: " ^ lang)
(*****************************************************************************)
(* Graph adjuster (overlay-ish) *)
(*****************************************************************************)
let adjust_graph graph_file adjust_file whitelist_file dest_file =
let g = Graph_code.load graph_file in
let adjust = Graph_code.load_adjust adjust_file in
let whitelist = Graph_code.load_whitelist whitelist_file in
Graph_code.adjust_graph g adjust whitelist;
Graph_code.save g dest_file;
()
(*****************************************************************************)
(* Analysis *)
(*****************************************************************************)
let analyze_backward_deps graph_file =
let g = GC.load graph_file in
let gopti =
Common.cache_computation ~verbose:!verbose graph_file ".opti"
(fun () -> Graph_code_opti.convert g)
in
let config = DM.basic_config_opti gopti in
(* DM.threshold_pack := 90; *)
let config = DM.expand_node_opti (("flib", E.Dir)) config gopti in
let dm, gopti = DM.build config None gopti in
let n = Array.length dm.DM.matrix in
(* todo: filter biggest offenders? *)
let res = ref [] in
for i = 0 to n - 1 do
for j = i + 1 to n - 1 do
let n = dm.DM.matrix.(i).(j) in
if n > 0 then begin
let xs = DM.explain_cell_list_use_edges (i, j) dm gopti in
pr2 (spf " (%d, %d) = %d" i j (List.length xs));
Common.push2 xs res;
end
done
done;
let edges = List.flatten !res in
pr2 (spf "total backward deps = %d" (List.length edges));
let xxs = Common.group_by_mapped_key (fun (n1, n2) -> n2) edges in
pr2 (spf "#dst =%d" (List.length xxs));
xxs +> List.map (fun (n, xs) -> (n, xs), List.length xs)
+> Common.sort_by_val_highfirst
+> Common.take_safe 100
+> List.iter (fun ((n, xs), cnt) ->
let file = GC.file_of_node n g in
pr2 (spf "%-30s = %d (file = %s)" (GC.string_of_node n) cnt
file)
);
let file = graph_file ^ ".whitelist" in
pr2 (spf "generating whitelist in %s" file);
GC.save_whitelist edges file g;
()
(*****************************************************************************)
(* Main action, viewing the graph *)
(*****************************************************************************)
(* algo:
* - find root of project with a dependencies.marshall file
* - display slice if needed of the dependency hieararchical matrix
* using arguments in xs.
* No need of -with_extern anymore, external stuff will be collapsed.
* No need of -package_depth, can expand on demand after.
*
* todo: How load graph? Build on demand? easier to test things that way ...
* maybe can just cache and look if we need to recompute the code graph?
* same for the dependency matrix that we can cache too.
*)
let main_action xs =
Logger.log Config_pfff.logger "codegraph" None;
let dir =
match xs with
| [x] ->
if x = "."
(* getcwd() display a realpath *)
then List.hd (Common.cmd_to_list "pwd")
else failwith "go the directory you want"
| _ -> failwith "give just one directory"
in
let inits = Common2.inits_of_absolute_dir dir in
let root =
inits +> List.rev +> List.find (fun path ->
Sys.file_exists (dep_file_of_dir path))
in
pr2 (spf "Using root = %s" root);
let model = build_model root in
let path =
if root =*= dir
then []
else begin
(* Propose a specific slice of the graph.
* If run cg from a/b/c, then Expand a, Expand a/b,
* then Focus a/b/c, and optionally Expand a/b/c.
*)
let readable_subdir =
let xs = Common.split "/" root in
let ys = Common.split "/" dir in
let (a, b) = Common2.splitAt (List.length xs) ys in
assert (xs =*= a);
b
in
let dir_or_package, start =
if GC.has_node (dir_node readable_subdir) model.Model.g_deprecated
then dir_node, readable_subdir
else package_node,
try
Common2.tails readable_subdir +> List.find (fun xs ->
GC.has_node (package_node xs) model.Model.g_deprecated
)
with Not_found ->
failwith "could not find a Dir or Package"
in
let (str, kind) = dir_or_package start in
pr2 (spf "focusing on %s %s"
(Database_code.string_of_entity_kind kind) str);
let rec aux before xs =
match xs with
| [] -> raise Impossible
| [x] ->
let node = dir_or_package (List.rev (x::before)) in
[DM.Focus (node, !deps_style); DM.Expand node;]
| x::xs ->
let node = dir_or_package (List.rev (x::before)) in
(DM.Expand node)::aux (x::before) xs
in
(aux [] start)
end
in
let w = Model.init_world path model in
View.mk_gui w
(*****************************************************************************)
(* Extra Actions *)
(*****************************************************************************)
(* quite similar to analyze_backward_deps *)
let test_thrift_alive graph_file =
let g = GC.load graph_file in
let gopti =
Common.cache_computation ~verbose:!verbose graph_file ".opti"
(fun () -> Graph_code_opti.convert g)
in
let config = DM.basic_config_opti gopti in
DM.threshold_pack := max_int;
let config = DM.expand_node_opti (("lib", E.Dir)) config gopti in
let config = DM.expand_node_opti (("lib/thrift", E.Dir)) config gopti in
let config = DM.expand_node_opti (("lib/thrift/packages", E.Dir)) config gopti in
let dm, gopti = DM.build config None gopti in
let n = Array.length dm.DM.matrix in
let kflib = Hashtbl.find dm.DM.name_to_i ("flib", E.Dir) in
for j = 0 to n - 1 do
let (s, kind) = dm.DM.i_to_name.(j) in
if s =~ "lib/thrift/packages/.*"
then begin
let v = dm.DM.matrix.(kflib).(j) in
if v > 0 then begin
pr2 (spf "%s is USED in flib/" s);
let xs = DM.explain_cell_list_use_edges (kflib, j) dm gopti in
xs +> Common.take_safe 5 +> List.iter (fun (n1, n2) ->
pr2 (spf " %s --> %s"
(GC.string_of_node n1) (GC.string_of_node n2))
)
end else begin
if DM.is_dead_column j dm
then begin
pr2 (spf "%s appeared DEAD" s);
end
end
end
done
(* quite similar to analyze_backward_deps *)
let test_pad graph_file =
let g = GC.load graph_file in
g +> GC.iter_use_edges (fun n1 n2 ->
let file = GC.file_of_node n2 g in
if file =~ ".*flib/intern/thrift/lib"
then begin
let file2 = GC.file_of_node n1 g in
if file2 =~ ".*tests/" || file2 =~ ".*/__tests__/.*"
then begin
pr2_once (spf "%s --> %s" file2 file);
(*pr2 (spf " %s --> %s" (GC.string_of_node n1) (GC.string_of_node n2));*)
end
end
)
(* ---------------------------------------------------------------------- *)
(* Phylomel *)
(* ---------------------------------------------------------------------- *)
(*
open Vec2
open BarnesHut
let update_state n fs bs fig =
let delta = 0.05 in
(* Update forces *)
ForceDirectedLayout.do_calc_forces fs bs fig;
(* Euler integration on each body *)
for i=0 to n - 1 do
let b = bs.(i) in
let f = fs.(i) in
b.p.x <-
b.p.x +. delta *. b.v.x +. 1./.2. *. delta *. delta *. f.x;
b.p.y <-
b.p.y +. delta *. b.v.y +. 1./.2. *. delta *. delta *. f.y;
b.v.x <- b.v.x +. delta *. f.x;
b.v.y <- b.v.y +. delta *. f.y;
f.x <- 0.;
f.y <- 0.
done
let test_phylomel geno_file =
let svg_file = "/tmp/foo.svg" in
(* We create four things :
* - genotypes collection
* - distance matrix
* - minimum spanning tree
* - figure (graphical tree)
*)
let collec =
Genotypes.read_file geno_file +> Genotypes.remove_duplicates
in
let dmat = GenoMat.create collec in
let tree = Tree.prim_complete collec dmat in
(*
let dist_mat =
[|
[||];
[|1|];
[|1;2|];
|]
in
let adj_mat =
[|
[||];
[|true|];
[|true;false|];
|]
in
let tree = Tree.create adj_mat dist_mat in
let infos = [|"n0"; "n1"; "n2"|] in
*)
let fig = Phylogram.radial_layout ~reframe:false 800. tree in
(* Creates force array, bodies *)
let n = Phylogram.size fig in
let fs = Array.init n (fun _ -> Vec2.null ()) in
let bs = Array.map ForceDirectedLayout.body_of_pos fig.Phylogram.ps in
for i=0 to 2000 do
update_state n fs bs fig
done;
let x0, y0 = (10.,10.) in
Phylogram.unsafe_reframe (10.,10.) fig.Phylogram.ps;
Phylogram.unsafe_crop_width (800.-.2.*.x0) fig.Phylogram.ps;
fig.Phylogram.h <- Phylogram.height fig.Phylogram.ps +. 2. *. y0;
(* let x0 = 10. in *)
(* unsafe_reframe (10., 10.) fig.ps; *)
(* unsafe_crop_width (800.-.2.*.x0) fig.ps; *)
let nodeinfo =
(fun i -> Genotype.description collec.Genotypes.genos.(i))
(* (fun i -> infos.(i)) *)
in
Phylogram.write_svg_file nodeinfo fig svg_file;
()
*)
(* ---------------------------------------------------------------------- *)
let extra_actions () = [
"-build", " <dir>",
Common.mk_action_1_arg (fun dir -> build_graph_code !lang dir);
"-build_stdlib", " <src> <dst>",
Common.mk_action_2_arg (fun dir dst -> build_stdlib !lang dir dst);
"-adjust_graph", " <graph> <adjust_file> <whitelist> <dstfile>",
Common.mk_action_4_arg (fun graph file file2 dst ->
adjust_graph graph file file2 dst);
"-analyze", " <graph>",
Common.mk_action_1_arg (fun graph_file ->
analyze_backward_deps graph_file
);
"-protected_to_private", " <graph>",
Common.mk_action_1_arg (fun graph_file ->
let g = Graph_code.load graph_file in
Graph_code_analysis.protected_to_private g
);
"-thrift_alive", " <graph>",
Common.mk_action_1_arg test_thrift_alive;
"-test_pad", " <graph>",
Common.mk_action_1_arg test_pad;
(*
"-test_phylomel", " <geno file>",
Common.mk_action_1_arg test_phylomel;
*)
]
(*****************************************************************************)
(* The options *)
(*****************************************************************************)
let all_actions () =
extra_actions () ++
Test_program_lang.actions() ++
[]
let options () = [
"-lang", Arg.Set_string lang,
(spf " <str> choose language (default = %s)" !lang);
"-deps_in", Arg.Unit (fun () -> deps_style := DM.DepsIn),
" ";
"-deps_out", Arg.Unit (fun () -> deps_style := DM.DepsOut),
" ";
"-deps_inout", Arg.Unit (fun () -> deps_style := DM.DepsInOut),
" ";
"-dots_threshold", Arg.Int (fun i -> DM.threshold_pack := i),
" ";
"-skip_list", Arg.String (fun s -> skip_list := Some s),
" ";
"-o", Arg.String (fun s -> output_dir := Some s),
" ";
"-symlinks", Arg.Unit (fun () ->
Common.follow_symlinks := true;
), " ";
"-verbose", Arg.Unit (fun () ->
verbose := true;
DM.verbose := true;
), " ";
] ++
Common.options_of_actions action (all_actions()) ++
Common2.cmdline_flags_devel () ++
Common2.cmdline_flags_verbose () ++
[
"-version", Arg.Unit (fun () ->
pr2 (spf "CodeGraph version: %s" Config_pfff.version);
exit 0;
),
" guess what";
]
(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)
let main () =
Gc.set {(Gc.get ()) with Gc.stack_limit = 1000 * 1024 * 1024};
(* see http://www.elehack.net/michael/blog/2010/06/ocaml-memory-tuning *)
(*
Gc.set { (Gc.get()) with Gc.minor_heap_size = 2_000_000 };
Gc.set { (Gc.get()) with Gc.space_overhead = 200 };
*)
(* Common_extra.set_link(); *)
let usage_msg =
spf "Usage: %s [options] <dir> \nDoc: %s\nOptions:"
(Filename.basename Sys.argv.(0))
"https://github.com/facebook/pfff/wiki/Codegraph"
in
(* does side effect on many global flags *)
let args = Common.parse_options (options()) usage_msg Sys.argv in
(* must be done after Arg.parse, because Common.profile is set by it *)
Common.profile_code "Main total" (fun () ->
(match args with
(* --------------------------------------------------------- *)
(* actions, useful to debug subpart *)
(* --------------------------------------------------------- *)
| xs when List.mem !action (Common.action_list (all_actions())) ->
Common.do_action !action xs (all_actions())
| _ when not (Common.null_string !action) ->
failwith ("unrecognized action or wrong params: " ^ !action)
(* --------------------------------------------------------- *)
(* main entry *)
(* --------------------------------------------------------- *)
| x::xs ->
main_action (x::xs)
(* --------------------------------------------------------- *)
(* empty entry *)
(* --------------------------------------------------------- *)
| [] ->
Common.usage usage_msg (options());
failwith "too few arguments"
)
)
(*****************************************************************************)
let _ =
Common.main_boilerplate (fun () ->
main ();
)