OrenIshShalom
OrenIshShalom

Reputation: 7162

subtract 2 (time) variables in makefile

I want to compute the time it takes to finish my command in makefile. Here is what I tried, but it doesn't work:

FILE = some_file.6.txt

.ONESHELL:
another_file.txt: ${FILE}
    @START=$$(date +%s.%N)
    @END=$$(date +%s.%N)
    @echo $$(($$END-$$START))

Here is the error I get:

$ make
/bin/sh: 3: arithmetic expression: expecting EOF: "1569658240.437512688-1569658240.436685866"
makefile:5: recipe for target 'another_file.txt' failed
make: *** [another_file.txt] Error 2

I've tried all combination of adding/removing both ( and $. Please help, thanks.

Upvotes: 0

Views: 175

Answers (1)

MadScientist
MadScientist

Reputation: 100946

This is not a makefile problem. It's a shell problem. This is trivially seen by running the command at a shell prompt rather than a makefile, and you'll get the same error:

$ /bin/sh -c 'echo $((1569658240.437512688-1569658240.436685866))'
/bin/sh: 1: arithmetic expression: expecting EOF: "1569658240.437512688-1569658240.436685866"

It's also an error in bash, so setting SHELL := /bin/bash won't help:

$ /bin/bash -c 'echo $((1569658240.437512688-1569658240.436685866))'
/bin/bash: 1569658240.437512688-1569658240.436685866: syntax error: invalid arithmetic operator (error token is ".437512688-1569658240.436685866")

If you check the documentation for your shell you'll see that arithmetic expressions only work on integer values, not floating point values as you're attempting above.

To perform more advanced math including on floating point values, you should investigate the bc program:

.ONESHELL:
another_file.txt: ${FILE}
        @START=$$(date +%s.%N)
        @END=$$(date +%s.%N)
        @echo $$END-$$START | bc

Upvotes: 1

Related Questions