Saturday, August 9, 2008

Poor Boy's Header Dependancy Graph

Spent a few days breaking up a nasty cluster of inter-header dependencies. The code base contained a large core of header files that each included lots other header files that were not needed. That kind of thing irks me like fingernails on a chalkboard. I decided to graph header includes to visually look for odd includes, connected clusters that can be merged into a single header file, and redundant includes. My solution simply generates a dot graph for easy visualization.

#!/bin/bash

# 'Cause bash bugs suck
set -o nounset
set -o errexit

BASEDIR=$1

echo "digraph depends_on {"

for file in `find $BASEDIR -name "*.[ch]" -print | grep -v "tools/"`
do 
    for include in `grep "#include" $file | tr \< \: | tr \> \: | tr \" \: | cut -f2 -d\:`
    do
        f=`basename $file`
        i=`basename $include`
        echo "    \"$f\" -> \"$i\";"
    done
done 

echo "}"