forked from functori/ocamldot-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocamldot.mll
478 lines (436 loc) · 15.5 KB
/
ocamldot.mll
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
(* ocamldot.mll, July 1999, Trevor Jim *)
{
module StringSet =
Set.Make(struct type t = string let compare = String.compare end)
module StringMap =
Map.Make(struct type t = string let compare = String.compare end)
let exclude_paths = ref StringSet.empty
let dependencies = ref []
let currentSource = ref None
let addDepend t =
match !currentSource, t with
| None, _ | _, None -> () (* ignored source or destination file *)
| Some s, Some t ->
if s<>t
then dependencies := (s,t)::(!dependencies)
(* In case several files with the same name are present in the
project, force a renaming to avoid clash *)
let unique_name =
let renaming = Hashtbl.create 512 in
let counters = Hashtbl.create 512 in
fun filepath ->
let dirname = Filename.dirname filepath in
if StringSet.mem dirname !exclude_paths then None
else
Some (
let i = String.rindex filepath '.' in
let full = String.sub filepath 0 i in
try Hashtbl.find renaming full
with Not_found ->
let short = Filename.basename full in
let short = String.capitalize short in
let cpt, old_full =
try Hashtbl.find counters short
with Not_found ->
let cpt = ref (-1) in
Hashtbl.add counters short (cpt, full);
cpt, full
in
incr cpt;
let short =
if !cpt == 0 then short
else begin
let s = Format.sprintf "%s(%d)" short !cpt in
Format.eprintf
"Warning: module %s renamed to %s to avoid clash with %s !@."
full s old_full;
s
end
in
Hashtbl.add renaming full short;
short
)
}
rule processSource = parse
['.' '-' '/' 'A'-'Z' 'a'-'z' '_' '\192'-'\214' '\216'-'\246'
'\248'-'\255' '\'' '0'-'9' ]+ '.' ['A'-'Z' 'a'-'z']+
[' ' '\009']* ':'
{ currentSource := unique_name (Lexing.lexeme lexbuf);
processTargets lexbuf }
| eof
{ () }
| _
{ processSource lexbuf }
and processTargets = parse
[' ' '\009']+
{ processTargets lexbuf }
| '\\' [' ' '\009']* ['\010' '\013']+ [' ' '\009']+
{ processTargets lexbuf }
| ['.' '/' 'A'-'Z' 'a'-'z' '_' '\192'-'\214' '\216'-'\246'
'\248'-'\255' '\'' '0'-'9' ]+ '.' ['A'-'Z' 'a'-'z']+
{ addDepend (unique_name (Lexing.lexeme lexbuf));
processTargets lexbuf }
| eof
{ () }
| _
{ processSource lexbuf }
{
(********************************)
(* Utility functions for graphs *)
(********************************)
(**********************************************************************)
(* A graph is represented by a (string * StringSet) list, *)
(* that is, a list of (source,targets) pairs. *)
(**********************************************************************)
let emptyGraph = []
(**********************************************************************)
(* divideGraph graph source = (sourceTargets, graphWithoutSource) *)
(* *)
(* Return the targets of a source in a graph and the graph with the *)
(* source substracted from the sources. GraphWithoutSources may *)
(* still contain source as a target. *)
(**********************************************************************)
let divideGraph graph source =
let rec aux l =
match l with
[] -> (StringSet.empty,[])
| (s,ts)::tl ->
if s=source then (ts,tl)
else
let (sourceTargets,tlWithoutSource) = aux tl in
(sourceTargets,(s,ts)::tlWithoutSource) in
aux graph
(*********************************************)
(* Add the edge (source,target) to the graph *)
(*********************************************)
let addEdge graph source target =
let (sourceTargets,graphWithoutSource) = divideGraph graph source in
(source,StringSet.add target sourceTargets)::graphWithoutSource
(************************************************************)
(* Add the edges { (source,t) | t in targets } to the graph *)
(************************************************************)
let addEdges graph source targets =
let (sourceTargets,graphWithoutSource) = divideGraph graph source in
(source,StringSet.union targets sourceTargets)::graphWithoutSource
(**************************************************)
(* Remove the edge (source,target) from the graph *)
(**************************************************)
let removeEdge graph source target =
let rec loop l =
match l with
[] -> []
| (s,ts)::tl ->
if s=source
then (s,StringSet.remove target ts)::tl
else (s,ts)::(loop tl)
in loop graph
(*****************************************************************)
(* Remove the edges { (source,t) | t in targets } from the graph *)
(*****************************************************************)
let removeEdges graph source targets =
let rec loop l =
match l with
[] -> []
| (s,ts)::tl ->
if s=source
then (s,StringSet.diff ts targets)::tl
else (s,ts)::(loop tl)
in loop graph
(**********************************************************************)
(* Convert between an edge-list representation of graphs and our *)
(* representation. *)
(**********************************************************************)
let edgesOfGraph graph =
List.concat
(List.map
(fun (s,ts) ->
List.map (fun t -> (s,t)) (StringSet.elements ts))
graph)
let graphOfEdges edges =
List.fold_left
(fun g (s,t) -> addEdge g s t)
emptyGraph
edges
(****************************)
(* Is an edge in the graph? *)
(****************************)
let isEdge graph source target =
try
let sourceTargets = List.assoc source graph in
StringSet.mem target sourceTargets
with Not_found -> false
(*****************)
(* Print a graph *)
(*****************)
let printGraph graph =
let printEdges(source,targets) =
StringSet.iter
(fun t -> Printf.printf " \"%s\" -> \"%s\" ;\n" source t)
targets in
List.iter printEdges graph
(********************************)
(* Targets of a node in a graph *)
(********************************)
let targetsOf graph node = (* A set of nodes *)
try List.assoc node graph
with Not_found -> StringSet.empty
(*****************************************)
(* Sources that target a node in a graph *)
(*****************************************)
let sourcesOf graph node = (* A list of nodes *)
let rec aux l =
match l with
[] -> []
| (s,ts)::tl ->
if StringSet.mem node ts then s::(aux tl)
else aux tl in
aux graph
(******************************************************************)
(* Add an edge to a transitively closed graph, and return the new *)
(* transitive closure. *)
(******************************************************************)
let addEdgeTc graph source target =
let targetTargets = targetsOf graph target in
let (sourceTargets,graphWithoutSource) = divideGraph graph source in
let sourceSources = sourcesOf graphWithoutSource source in
let newSourceTargets =
StringSet.add target
(StringSet.union sourceTargets targetTargets) in
(source,newSourceTargets)::
(List.fold_right
(fun s g -> addEdges g s newSourceTargets)
sourceSources
graphWithoutSource)
(**********************************************************)
(* Compute the transitive closure of a graph from scratch *)
(**********************************************************)
let tc graph =
let loop graph (source,targets) =
let reachableFromSource =
List.fold_left
(fun r (s,ts) ->
if StringSet.mem s r then StringSet.union r ts
else r)
targets
graph in
(source,reachableFromSource)::
(List.map
(fun (s,ts) ->
if StringSet.mem source ts
then (s,StringSet.union ts reachableFromSource)
else (s,ts))
graph) in
List.fold_left loop [] graph
(************************************************************************)
(* The transitive kernel (tk) of a dag is a subset of the dag whose *)
(* transitive closure is the same as the transitive closure of the dag. *)
(* *)
(* IF THE GRAPH IS NOT A DAG, THIS CODE WON'T WORK PROPERLY!!! *)
(************************************************************************)
(************************************************************************)
(* Add an edge to a kernel dag and return the new kernel and transitive *)
(* closure of the new kernel. Requires the transitive closure of the *)
(* old kernel. *)
(************************************************************************)
let addEdgeTk kernel tcKernel source target =
if isEdge tcKernel source target
then (kernel,tcKernel)
else if source=target
then (addEdge kernel source target,tcKernel)
else
begin
let (sourceTargets,kernelWithoutSource) = divideGraph kernel source in
let targetTargets = StringSet.add target (targetsOf tcKernel target) in
let sourceSources = sourcesOf tcKernel source in
let kernelWithoutSource =
List.fold_left
(fun kws s -> removeEdges kws s targetTargets)
kernelWithoutSource
sourceSources in
((source,
StringSet.add target
(StringSet.diff sourceTargets targetTargets))
::kernelWithoutSource,
addEdgeTc tcKernel source target)
end
(**********************************)
(* The transitive kernel of a dag *)
(**********************************)
let tk dag =
let edges = edgesOfGraph dag in
let (kernel,tcKernel) =
List.fold_left
(fun (k,tck) (s,t) -> addEdgeTk k tck s t)
(emptyGraph,emptyGraph)
edges in
kernel
(**************************)
(* Print the dependencies *)
(**************************)
let doKernel = ref true
let printDepend graph =
if (!doKernel) then printGraph (tk graph)
else printGraph graph
let calledOnFile = ref false
let getDependFromFile file =
calledOnFile := true;
try
let ic = open_in file in
let lexbuf = Lexing.from_channel ic in
processSource lexbuf;
close_in ic
with Sys_error msg -> ()
| Exit -> ()
let getDependFromStdin () =
try
let lexbuf = Lexing.from_channel stdin in
processSource lexbuf
with Sys_error msg -> ()
| Exit -> ()
(**********************************)
(* Color and Cluster by directory *)
(**********************************)
let fold_dir f init path =
let collect cur_dir path (acum, dirs) =
let full_path = Filename.concat cur_dir path in
try
if Sys.is_directory full_path then
(acum, full_path :: dirs)
else
(f acum full_path, dirs)
with Sys_error _ ->
(acum, dirs) in
let rec fold_dir_ (acum, dirs) =
match dirs with
| [] ->
acum
| dir :: dirs ->
fold_dir_ (Array.fold_left (fun ad p -> collect dir p ad) (acum, dirs) (Sys.readdir dir)) in
if Sys.is_directory path then
fold_dir_ (init, [path])
else
f init path
let dir_to_mod_names graph dir =
let nodes =
List.fold_left (fun nodes (source, targets) ->
StringSet.add source (StringSet.union targets nodes)
) StringSet.empty graph in
fold_dir (fun dir_to_mod_names path ->
let file = Filename.basename path in
let mod_name = String.capitalize (try Filename.chop_extension file with _ -> file) in
if ((Filename.check_suffix file ".ml")
&& StringSet.mem mod_name nodes)
then
let dir = Filename.dirname path in
let files = mod_name :: (try StringMap.find dir dir_to_mod_names with Not_found -> []) in
StringMap.add dir files dir_to_mod_names
else
dir_to_mod_names
) StringMap.empty dir
let printColors dir_to_mod_names =
let num_dirs = StringMap.cardinal dir_to_mod_names in
let hsv i s v = Printf.sprintf "\"%f %f %f\"" ((float)i *. (1. /. (float)num_dirs)) s v in
StringMap.fold (fun dir mod_names i ->
List.iter (fun mod_name ->
Printf.printf "\"%s\" [style = filled, fillcolor = %s] ;\n" mod_name (hsv i 0.5 0.9) ;
) mod_names ;
i + 1
) dir_to_mod_names 0
|> ignore
let printClusters clusterDirs dir_to_mod_names =
StringMap.iter (fun dir mod_names ->
let base = Filename.basename dir in
if StringSet.mem base clusterDirs then (
Printf.printf "subgraph cluster_%s { label=\"%s\" ; style=filled\n" base base;
List.iter (fun mod_name ->
Printf.printf "\"%s\" ;\n" mod_name
) mod_names ;
Printf.printf "}\n"
)
) dir_to_mod_names
let colorAndCluster clusterDirs graph dir =
let dir_to_mod_names = dir_to_mod_names graph dir in
printColors dir_to_mod_names ;
printClusters clusterDirs dir_to_mod_names
(***************)
(* Entry point *)
(***************)
let usage = "Usage: ocamldot [options] <files>"
let clusters = ref []
let leftToRight = ref false
let landscape = ref false
let roots = ref []
let input_file = ref ""
;;
Arg.parse (Arg.align
[
("-c",
Arg.String(fun s -> clusters := s::!clusters),
"<c> cluster the modules in the <c> directory in the graph");
("-fullgraph",
Arg.Clear doKernel,
" draw the full graph (default is to draw only the kernel)");
("-landscape",
Arg.Set landscape,
" output in landscape format (default is portrait)");
("-lr",
Arg.Set leftToRight,
" draw graph from left to right (default is top to bottom)");
("-r",
Arg.String(fun s -> roots := s::!roots),
"<r> use <r> as a root in the graph; nodes reachable from <r> will be shown");
("-e",
Arg.String(fun s ->
(* hack to normalize format of input dir *)
let s =
Format.sprintf "%s/%s" (Filename.dirname s) (Filename.basename s) in
exclude_paths := StringSet.add s !exclude_paths),
"<e> exclude/ignore the files of the given path")
]) (fun f -> input_file := f) usage;
getDependFromFile !input_file;
if not(!calledOnFile) then getDependFromStdin();
print_string "digraph G {\n";
if !landscape
then print_string " size=\"10,7.5\" ;\n rotate=90 ;\n"
else print_string " size=\"7.5,10\" ;\n";
if (!leftToRight) then print_string " rankdir = LR ;\n"
else print_string " rankdir = TB ;\n";
let graph = graphOfEdges(!dependencies) in
begin
match !roots, !clusters with
[], [] -> printDepend graph
| roots, _ -> (* execute this part if some clusters are given with -c *)
(* Set up the graph so that the roots are printed at the same level *)
print_string " { rank=same ;\n";
List.iter
(fun r ->
print_string " ";
print_string r;
print_string " ;\n")
roots;
print_string " };\n";
(* Find the graph reachable from the roots *)
let tcGraph = tc graph in
let reachable node =
(List.exists (fun r -> r=node) roots)
||
(List.exists (fun r -> isEdge tcGraph r node) roots) in
let reachableFromRoots =
if roots == [] then
graph
else
List.concat
(List.map
(fun (source,targets) ->
if reachable source (*|| roots == []*)
then [(source,targets)]
else [])
graph) in
printDepend reachableFromRoots;
let clusterDirs = List.fold_left (fun z s -> StringSet.add s z) StringSet.empty !clusters in
colorAndCluster clusterDirs reachableFromRoots (Sys.getcwd ())
end;
print_string "}\n";
exit 0
;;
}