Dependency tracking

by Mithrandir

Let’s say you work on a project on your favourite language (for the purpose of this article, we will assume that that language is either C or Haskell, you’ll see why at the end of the article). Your project is complicated that you’ve split it across several libraries and several other files. Of course, you need a Makefile to build it. But, on that Makefile you need to be sure that all dependencies are listed as they should be, that they are up to date. The question arises: aren’t there any tools for automatic dependency tracking?

The answer is yes, there are (otherwise there wouldn’t be this article). I was asking myself that question while working on a C project until I saw that ghc has a -M option, described as follows:

-M              Output Makefile rules recording the
dependencies of a list of Haskell files.

So, I go to the HaCoTeB working dir, delete the Makefile and run ghc -M HaCoTeB.hs.  Then I look into the content of the Makefile:

# DO NOT DELETE: Beginning of Haskell dependencies
DataDefs.o : DataDefs.hs
TextParser.o : TextParser.hs
TextParser.o : DataDefs.hi
CodeParser.o : CodeParser.hs
CodeParser.o : DataDefs.hi
HaCoTeB.o : HaCoTeB.hs
HaCoTeB.o : CodeParser.hi
HaCoTeB.o : TextParser.hi
HaCoTeB.o : DataDefs.hi
# DO NOT DELETE: End of Haskell dependencies

Nice trick. Now, all I need to do here is to figure a way to do this automatically when invoking make. Solution is found at the Haskell Wiki: include the dependencies into a file and include that file into the Makefile (or, let ghc -M modify your Makefile)

Of course, ghc has another option, --make, which will make all of the above superfluous in the most cases. However, gcc doesn’t have it so that solution is necessary for C.

It turns out that it has a lot of -M options. -MM is what I was looking for, in conjunction with -MF to specify the output file.

That’s all that’s needed for automatic dependency tracking.

PS: Why have I excluded your language? Either because it is interpreted and there are no Makefiles needed or because the compiler is pretty smart to figure them by himself without requiring you to lift any more fingers.

PPS: Posted with a still bugged version of HaCoTeB.

About these ads