Any creative idea how to use enviroment variables to calculate full build time of a makefile? Can I store the time somewhere and compare it in the end?
I know the OP meant Windows OS, but for the record, on any Linux machine you can use time
:
>time make -j
real 0m9.774s
user 0m31.562s
sys 0m1.092s
On Windows 7 you've got PowerShell. So we'll assume:
Makefile
is in directory C:\Dev\ProjDir
mingw32-make
and is in your PATH
If PowerShell isn't your usual console then do:
C:\Dev\ProjDir>powershell
At the PowerShell prompt, run:
PS C:\Dev\ProjDir> Measure-Command { mingw32-make | Out-Default }
This will run your build and display the output, followed by a timing report, e.g.
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 244
Ticks : 2443841
TotalDays : 2.82851967592593E-06
TotalHours : 6.78844722222222E-05
TotalMinutes : 0.00407306833333333
TotalSeconds : 0.2443841
TotalMilliseconds : 244.3841
Continued for OP's follow-up:
I was wondering if it is possible to be implemented as part of the makefile, so I can decide which part to measure
The idea of timing "part of a makefile" does not make clear sense. When you run make
, it parses
the entire makefile before it makes anything and works out the entire sequence of
steps it must take to make the specified or default target(s). Then, it takes all those
steps.
Perhaps you want to measure the time taken to make a particular set of targets? You
can do that in the way already described. For example, suppose that your makefile
can make two libraries libfoo.lib
and libbar.lib
and you would like to time the
build of just libfoo.lib
. To make libfoo.lib
by itself, the make command you would
run is:
mingw32-make libfoo.lib
So to time this command, run:
PS C:\Dev\ProjDir> Measure-Command { mingw32-make libfoo.lib | Out-Default }
Or suppose that your makefile makes app.exe
from source files foo.c
and bar.c
, and
you would like to time just the build of the object files foo.obj
, bar.obj
. The
make command you would run to build just those object files is:
mingw32-make foo.obj bar.obj
So you would time it with:
PS C:\Dev\ProjDir> Measure-Command { mingw32-make foo.obj bar.obj | Out-Default }
Perhaps you would like to be able to invoke powershell's Measure-Command
inside your makefile
to time the building of particular targets?
For this, you need a command that invokes PowerShell to run some other command. That is:
powershell -c "some other command"
So in your makefile you can add a target for timing the build of any other targets:
.phony: time
time:
powershell -c "Measure-Command { $(MAKE) $(targets) | Out-Default }"
You would use the time
target like so:
C:\Dev\ProjDir>mingw32-make time targets=app.exe
or:
C:\Dev\ProjDir>mingw32-make time targets="foo.obj bar.obj"
And of course, in your makefile, the commands to build a particular target can
include powershell -c "some other command"
wherever you like.
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