Tuesday, March 11, 2008

Report compilation total time using GNU make


When working constantly on the same application, it is very useful to have an idea of the time it takes to compile. Having this information allows you to plan your work ahead, compare machines, developing environments, etc.

I have put together this Makefile, to print the total compilation time. It requires gmake and the UNIX shell utilities date, bc, echo and printf.


Makefile


# Time expressed as the seconds of the year. More portable than seconds since
# the epoch: `date +%s'
COMPILE_START_TIME := $(shell date +\(%j\*24\*60\*60+%H\*60\*60+%M\*60+%S\))
COMPILE_CURRENT_TIME = `date +\(%j\*24\*60\*60+%H\*60\*60+%M\*60+%S\)`


# Print the time difference between Tcurrent and Tstart. This function can be
# used after any target, as shown in the form: (see example)
# $(call print_timer,$(COMPILE_CURRENT_TIME),$(COMPILE_START_TIME))

# args: Tcurrent = $(1), Tstart = $(2)
define print_timer
@CT=$(1) && \
printf "Compilation total time: %.2d:%.2d (min:sec)\n" \
`echo "($$CT - $(2))/60" | bc` \
`echo "($$CT - $(2))%60" | bc`
endef


all: target

target:
@echo "Compiling for 2 seconds ..." && sleep 2 && echo Done
$(call print_timer,$(COMPILE_CURRENT_TIME),$(COMPILE_START_TIME))


Using this Makefile and running make, produces the following output:


Compiling for 2 seconds ...
Done
Compilation total time: 00:02 (min:sec)

No comments: