Dynamic build flags GIT revision “on-the-fly” compilation problem Arduino Uno

I am attempting to make use of the “on-the-fly” GIT revision use case shown on Redirecting.... However, when compiling an Arduino Uno project to print out the revision I receive the following error:

<command-line>:0:11: warning: missing terminating " character
<command-line>:0:11: warning: missing terminating " character
<command-line>:0:13: error: exponent has no digits
AutomationBoardMqttController.ino:157:18: note: in expansion of macro 'PIO_SRC_REV'

The platform.ini file I am using is:

platform = atmelavr
framework = arduino
board = uno

; Build options
build_flags =
  !echo "-DPIO_SRC_REV="$(git rev-parse HEAD)

Many thanks for any assistance.

This took a while for me to solve. You need escape sequences… both in python… then in the PIO… to arrive at the “” enclosed string. I used the python example…

import subprocess

revision = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).strip()
print "-DGIT_REV=\\\"%s\\\"" % revision