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
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With