PlatformIO Community

[SOLVED] Different dynamic build flags for windows and linux

Hello,
thank you for this very great tool which helps us a lot developing for embedded devices. We would like to use VCS revision/tag “on-the-fly” which is presented in the documentation. However, we have both developers working on linux and windows machines.
Is there any way to have different dynamic build flags in the same platformio.ini file depending on the host operating system (linux vs. windows)?

Could you try Python hook? If it works, I’ll update docs:

[env:myenv]
build_flags = !python -c "import subprocess; print '-DPIO_SRC_REV=%s' % subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()"

The core idea, is to write into STDOUT the final build flag. You can run this command in your terminal python -c "import subprocess; print '-DPIO_SRC_REV=%s' % subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()"

You can put this code into a separate file rev.py and in platformio.ini => build_flags = !python rev.py.

Hello,
thank you for the very fast response. Unfortunately the python hook doesn’t seem to work:
*** [.pioenvs/example/firmware.elf] Implicit dependency/home/user/.platformio/platforms/teensy/builder/!python’ not found, needed by target .pioenvs/example/firmware.elf'.

I solved it using a separate script:
extra_scripts = pre:append_git_build_flag.py

import subprocess
Import("env")

git_commit = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip() 
git_commit = git_commit[0:8]
git_flag = str("-D__GIT_HASH__=0x") + str(git_commit)

print("Uploading with following git commit ID: ", git_commit)

env.Append(
  BUILD_FLAGS=[
      git_flag
  ]
)

See updated docs http://docs.platformio.org/en/latest/projectconf/section_env_build.html#dynamic-build-flags

Could you try it on Windows?

Hello, thank you for the fast reply. The solution I proposed above works for both Linux and Windows environments. We are fine with that.

I’m confused about the updated docs. It first states build_flags = !print_git_rev.bat but then says something about an git_rev_macro.py. Shouldn’t it be build_flags = !python git_rev_macro.py?

I guess the problem I described previously was because we have also other build flags. Mixing of both a command and other build flags doesn’t seem to work.

Thanks! This is a typo and I’ve just fixed that.