Finding Dependencies for Erlang Code

// December 17th, 2009 // Uncategorized

There was a short but lively discussion on the Erlang Questions list today about Makefiles and building Erlang projects.  It included a link to an interesting document on Recursive Makefiles.  In that document, I noticed an interesting tidbit that I’ve never thought about.

When building big Makefiles, you want accurate (but automatic) dependency information.  To make this easier, some C compilers (notably gcc) have a "-M" option.  Given this option, the compiler will create a file (usually with .d extension) containing Makefile code linking each file to its dependencies.

This is handy, since it would be a pain to parse C and C++ files for included files.  Getting the include paths correct would be difficult as well.  By running through all of the files once with -M (and generating appropriate dependencies for the .d files as well) creates a system whereby make can maintain its own dependency information.

I find this neat.  In fact, since this system is very nearly as old as I am, I find it extra neat.

Sadly, the Erlang compiler doesn’t have a facility to automatically generate dependencies.  That said, the Erlang compiler is written in Erlang, and most of its internals are exposed.  It seemed, then, that it would not be too much trouble to use those internals to generate the dependency information I wanted.

Ignoring, a particularly sadistic list comprehension that I had to decipher, it wasn’t too much trouble to put the pieces together.  Thus, I present you with a rough escript that will generate dependencies for a list of Erlang source files.  Despite Erlang’s verbose nature, it clocks in at 50 lines.  Not too shabby!

You can download it at:  http://needlesslymessianic.com/files/erl_deps

I don’t know about you, but I think this is pretty cool.  I think it’s most cool because you have all of the flexibility you get having a self-hosted language (like C, PyPy, and Rubinius; unlike CPython or Matts Ruby), but all of the introspection that you get in a dynamic language.  Being able to use the same functions that the compiler uses is very powerful.

Anyways, enjoy.  Comments welcome.

Comments are closed.