PlatformIO Community

Advanced scripting env.append() macro with string does not compile

Hey everyone,

for a project I am trying to give the user the chance to define a station name (string), that is passed as a macro to the source code for building. I want to keep this away from any code as the device firmware should not depend on this name when building (git history etc.).

First, I tried it with dynamic build flags and a python script, reading from stdin via input(). Unfortunately, it skips the input line and the user cannot enter anything during build process. It works to pass a macro but only without user input via stdin.

Next, I tried it with advanced scripting and a pre: python script. Following the example on the site I tried

env.Append(CPPDEFINES=[
  ("STATION_NAME_1", "FKS7")
])

However, it does not compile and aborts with an error <command-line>:0:16: error: 'FKS7' was not declared in this scope. Hovering over the Macro reveals, that it is being interpreted as FKS7 (without double quotes). So I tried adding escaped double quotes to the user input (e.g. "“FKS7"”). At first it seemed to work, as in VSC it appears as everything is alright when hovering over the macro (see attached image) but building still fails with the same error message (also in the image).

VSC_PIO_Advanced_Scripting

For any hint on how to fix this, I would be very thankfull.

Cheers,
Daniel

PS.:
It happens every now and then that PlatformIO: Rebuilding IntelliSense Index is loading forever, not allowing me to build the (only) environement in platformio.ini. Building default works however.

You are mapping the macro to the FKS7 (an identifier, like a variable or function name), not "FKS7" as a string value. Either stringify it or say "\"FKS7\"" in the Python code.

Hey maxgerhardt,

thank you for the reply. For the output in the image I do use extra double quotes like so:

env.Append(CPPDEFINES=[
  ("STATION_NAME_1", "\"FKS7\"")
])

The code-snippet in the original post was my first attempt according to how it is written in the Advanced Scripting section of PIO documentation. After that I tried it with extra quotes (as you mentioned; see attached image). Unfortunately, the compiler error still remains even after that.

VSC_PIO_Advanced_Scripting

Any other idea? Or am I misunderstanding something?

Hmm, try and do a

  ("STATION_NAME_1", "\\\"FKS7\\\"")`

instead, maybe the escaping char \ needs to be injected here too.

1 Like

Hi again,

indeed, injecting also the escaping character did compile! Thank you very, very much! I tested it with user input and everything works as expected.

I don’t know if this tweak is so apparent, that I could not find any clue regarding that. In case it is not, perhaps it makes sense to add this piece of information somewhere in the documentation? Also as what is written as an example on the website is misleading. If someone from the team is reading this.

Just because this was resolved so smoothly :smile: do you have any idea regarding the PS from my original post? The (only) environment is loading forever and I cannot build or do anything with it, stating PlatformIO: Rebuilding IntelliSense Index. When changing do the default environement I can build.

Mhm as a first defense, restart VSCode – there might have been something wrong going on when wrong build flags were in there.

You can also press the refresh button above the projects task and then that “Loading” beneath it should go away.

grafik

Hm do you have a complete example for that? I experienced a maybe related issue that the SCons script (that’s what the extra_scripts file is run as) doesn’t have direct access to the standard ouput/input, but there’s something in between that forwards things [the output of the SCons script and the actual terminal / main process] (in my case there is an issue with flushing non-newline terminated output). Maybe redirecting / supporting stdin is something that the PlatformIO core doesn’t yet allow. (CC @ivankravets )

In any case, Issues · platformio/platformio-core · GitHub is always open for these bugreports.

Even if it does not strictly belong to this topic, there is an issue with non-newline output on stdout, at least for the case of advanced scripting (via extra_scripts). As you can see in the second screenshot, I added a ‘\n’ at the end of input(). Otherwise it would appear only until after an input was made and enter was pressed. It seems not to flush the output otherwise.

The point you are referring to was when I tried it with dynamic build flags (build_flags in platformio.ini). In that case compilation fails with the error stationName = input("\t***Enter station name***\n") EOFError: EOF when reading a line. Somehow, dynamic build flags do not support user input via stdin.

It became a bit of a mash up of multiple issues here. I guess I will make an individual issue for each case (escaping \ for strings, dynamic build flags failing with stdin, environment loading forever).

See guide
https://docs.platformio.org/en/latest/scripting/examples/asking_for_input.html

I’ve just tested and it works for me:

Screen Shot 2021-03-22 at 14.30.43

This does not work, following this example. Then restart VS code and it will be stuck with rebuilding the IntelliSense Index for ever. As suggested above I opened a bug for PIO Core but this was closed with out resolution.