Pass global #define at run time

I’m running PIO in a bash script.

This script takes the git version of a bunch of dependancies and appends them to a file version.txt.

#define MY_MELVANIMATE_GIT_TAG "0xfff22401"
#define ESPMANAGER_GIT_TAG "0x5e5640e9"
#define MELVANIMATE_GIT_TAG "0x58138fe3"

I’d like these defines to be global within the project, but they are not seen by my libs, even though it is the first include in the main .ino file.

Is there a possibility of passing in defines at the level of pio run ? e.g.

pio run -t upload -e default -d -Dmyvariable=xyz

Unless you are going to run the command from terminal manually you can define it in the build_flags section of platformio.INI file and get the result.

To answer your query, yes you can.

this is all part of a bash script, so modifying the the ini is not an option really… i guess i could get into awking the ino… but it would be easier to pass them in globally

http://docs.platformio.org/en/latest/envvars.html#envvar-PLATFORMIO_BUILD_FLAGS

2 Likes

doh. worked flawlessly… thank you… don’t know why i missed it… i scoured the faq

OK… this has taken me a while to figure out.

Defines passed in using

export PLATFORMIO_BUILD_FLAGS="-DMY_MELVANIMATE_GIT_TAG=$MYMELGIT -DESPMANAGER_GIT_TAG=$MANGIT -DMELVANIMATE_GIT_TAG=$MELGIT"

actually overwrite the build flags defined in the platformio.ini file. This is why my sketches stopped working all of a sudden! and I could not for the life of me figure out why!!!

I need these defined platformio.ini to also persist

build_flags = -Wl,-Tesp8266.flash.4m.ld -DDEBUG_DEVICE -DWS2812_UART_METHOD -DDebug_ESPManager=Serial

hence the SPIFFS flash config not working.

Is this intended behaviour or is there away to add build flags, not overwrite them?

Are you able to post your script?

I am trying get the git version of my code and pass in a define by setting a build flag but I can’t get the example to work. (thread here)

How did you set $MYMELGIT and do you know how I would do something similar on Windows?

This is the whole script.

It does a bunch of stuff:

  • generates the git tags for various sub projects, and exports the defines
  • builds all the web based material using gulp which also copies it to the data folder
  • goes through each parameter passed into the script to clean, build and copy the results to a server on my network that acts as the update gateway.
#!/bin/bash

CURRENT=$(pwd)
set -e

echo "PWD = $CURRENT"
MY_PATH=`dirname "$0"`
FULL_PATH=$PWD

BUILD_NAME_VARIABLE="MyMelvanimate"

pushd .

ver=`git describe --tags --always`
ver_define=`echo $ver | tr "[:lower:].-" "[:upper:]_"`
MYMELGIT="\"0x`git rev-parse --short=8 HEAD 2>/dev/null`\""
cd $FULL_PATH/lib/ESPmanager/src
MANGIT="\"0x`git rev-parse --short=8 HEAD 2>/dev/null`\""
cd $FULL_PATH/lib/Melvanimate/src
MELGIT="\"0x`git rev-parse --short=8 HEAD 2>/dev/null`\""
#pushd $MY_PATH
popd

echo $MYMELGIT $MANGIT $MELGIT

export PLATFORMIO_BUILD_FLAGS="-DMY_MELVANIMATE_GIT_TAG=$MYMELGIT -DESPMANAGER_GIT_TAG=$MANGIT -DMELVANIMATE_GIT_TAG=$MELGIT"

for var in "$@"
do
    rm -rf ./tmp.package
    mkdir -pv ./tmp.package
    echo "Building $var"
    platformio run -e $var -t clean
    platformio run -e $var -t idedata
    platformio run -e $var 
    cp -r -v $CURRENT/$BUILD_NAME_VARIABLE/data $CURRENT/tmp.package/data
    cp $CURRENT/.pioenvs/$var/firmware.bin $CURRENT/tmp.package/data
    python $CURRENT/buildmanifest.py $CURRENT/tmp.package/data ./tmp.package/manifest.json
    cp $CURRENT/.pioenvs/$var/firmware.elf $CURRENT/tmp.package/
    ssh  -p 22  -i ~/.ssh/travis travis@192.168.1.42 "rm -rf ~/projects/sticilface/local/$BUILD_NAME_VARIABLE/$var/"
    ssh  -p 22  -i ~/.ssh/travis travis@192.168.1.42 "mkdir -p ~/projects/sticilface/local/$BUILD_NAME_VARIABLE/$var/"
    scp  -P 22  -i ~/.ssh/travis -rp $CURRENT/tmp.package/. "travis@192.168.1.42:~/projects/sticilface/local/$BUILD_NAME_VARIABLE/$var/"
done

no experience with windows I’m afraid

Thanks! I will have a look through and see if I can make setting the variables windows compatible :slight_smile:

I ended up running

git describe --abbrev=4 --dirty --always --tags > temp
SET /P VERSION= < temp
del temp
SET PLATFORMIO_BUILD_FLAGS='-DGIT_VERSION="%VERSION%"'

which is a bit gross haha

Any powershell experts that can let me know how to do this without the temp file?

Also, I have the same problem where the PLATFORMIO_BUILD_FLAGS overwrites the build_flags set in the .ini file

Thanks, fixed in PIO Core 3.3.0a11

Thats great thanks. Will update and let you know.