So I have this Makefile based build system that my users feel is working too slowly. For the sake of this question lets define performance as the time it takes make to figure out what it should actually do.
I can see some avenues for optimization --
- Reducing the number of times Makefile is parsed and the DAG recalculated due to including a Makefile fragment.
- Reducing the number of going to an external Makefile with
make -C
- Reducing variable expansions
- etc.
-- however I want to know first where are my bottlenecks. Since optimization without profiling is a waste of life, I want to ask: How to profile a Makefile?
Assume that the system I inherited is fairly well designed, i.e. it already implements the most common tricks of the trade: (mostly) non recursive make, ccache, precompiled headers, auto generated header dependencies etc).
... and just to preempt some of the possible answer. I know that there might be faster and better build systems then GNU make - (Personally, I am eagerly waiting to see what the CMake folks will come up with regards to the Ninja system) - but unfortunately swapping build system is not in the cards.
Try running
make
with the-d
flag. This will output debugging information outlining exactly whatmake
is doing and in what order, as well as why it is doing it. I use that output to find things that I would otherwise miss, such as unintentional recursivemake
calls or things that are out of order and forcing certain files or parts of the source tree to be processed multiple times. My guess is that you'll be surprised at how muchmake
is doing behind the scenes that you weren't aware of.One warning though. The
-d
option causesmake
to produce an obscene amount of output. You will definitely want to redirect output to a file. You terminal will thank you later.If you are the source hacker type, you could build a custom version of
make
that put a high-resolution timestamp on each line that was printed due to the-d
flag. This would essentially create a timeline showing you exactly how much timemake
wasted on each step.