Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show the build duration time in Makefile

Tags:

makefile

within in Make file, i can printed the time before the build start and finished.

myitem:
    printf 'start time is %s\n' "$$(date --iso-seconds)"
    #other build command, i.e. g++ xxxx.cpp
    printf 'end time is %s\n' "$$(data --iso-seconds)"

Is there any other way that implemented like below in Make file?

myitem:
    starttime = $$(date)
    #start my build
    printf "The build took time:" NowTime - startTime
like image 782
bzhu Avatar asked Jun 09 '16 12:06

bzhu


2 Answers

If this is just for informational purposes, the absolutely easiest way is to run your build under the time command.

To answer your question in full, though, read on.

By default, each line of a recipe runs in a separate shell instance. To use a variable from an earlier command, use a single shell. Perhaps like this:

myitem:
    d=$$(date +%s)\
    ; build \
    && echo "Build took $$(($$(date +%s)-d)) seconds"

The backslashes escape the newlines so that this is seen as a single long logical line by make. The semicolons and && are statement separators - I find it helpful (because it's so ugly!) to put them at the beginning of the next line just to remind myself that this is a single compound command. If the build fails, the stuff after && will not execute; this is slightly inelegant, but better than thinking that your build succeeded when it didn't.

I switched to date +%s (seconds since epoch) because that is so much easier to work with programmatically.

In GNU Make, you could also use .ONESHELL.

like image 154
tripleee Avatar answered Nov 15 '22 12:11

tripleee


One easy way is to prefix your commands with /usr/bin/time, e.g.:

/usr/bin/time --format="took %E" <the build command, i.e. g++ xxxx.cpp>
like image 45
Maxim Egorushkin Avatar answered Nov 15 '22 11:11

Maxim Egorushkin