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 -Tsvg >tmp.svg

The code, 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):


import re
import sys

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

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

print "digraph depend {"
for line in f:
    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 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 articles:

  • 2017-05-15 PhD viva: congratulations to Thomas Gransden
  • 2017-01-23 Daniel Morrison PhD
  • 2016-10-20 The Tree of Life (film)
  • 2016-10-12 Outreach talk at Uppingham School
  • 2016-06-05 Do not buy TP-Link products
  • 2016-05-11 The meaning of IELTS 6.0
  • 2015-09-11 Richard Hamming 1968 Turing Award lecture quote
  • 2015-06-22 Working with the nix package manager: OCaml, Isabelle and Lem
  • 2015-06-17 J. M. Coetzee novel, Youth
  • 2015-01-23 OCaml dependency graph via graphviz and dot file