Date: 2015-01-23
Categories: misc

OCaml dependency graph via graphviz and dot file

I often want to visualize the dependency graph of OCaml source files, without using ocamldoc. The one-liner I currently use for this is:

# grep -v removes matching lines
ocamldep -one-line *.ml *.mli  |grep -v "cmx" |dot_of_depend.py |dot -Tsvg >tmp.svg

The code dot_of_depend.py, which I put in my personal bin directory, is as follows (I think I stole this from somewhere on the web, but have been unable to locate the original source):

#!/usr/bin/python

import re
import sys

def sanitize(s):
    s=re.sub('.*/','',s)
    s=re.sub('[^0-9a-zA-Z]+', '_', s)
    return s

f=sys.stdin #open('.depend','r')
counter=0
dictionary={} # for storing map from int to filename

print "digraph depend {"
for line in f:
    s=line.split();
    for x in range(2, len(s)):
            print (sanitize(s[0]))+" -> "+(sanitize(s[x]))
print "}"

This is effectively a light-weight version of the code here http://trevorjim.com/projects/ocamldot/ the functionality of which was then included in ocamldoc. The problem is that ocamldoc is quite heavyweight, whereas ocamldep and the above are really lightweight.


Related posts: