How to pass parameters to a Python script from platformio.ini?

Hi,

I run a Python script before compiling and pass it a parameter I’ve called extra_includes. It works, but PIO returns a warning.

So I’d like to know the correct way to pass parameters to a Python script from platformio.ini.

warning

Warning! Ignore unknown configuration option `extra_includes` in section [env:m5stack_cores3_____basic_____wakeup]

platformio.ini

[env:m5stack_cores3_____basic_____wakeup]
extra_includes = "../include/M5CoreS3/examples/Basic/wakeup/wakeup.ino"
extra_scripts = pre:create_main_cpp.py

python script

paths_to_include = env.GetProjectOption("extra_includes").split("\n")

What do you want the compiler to do with your .ino file?

  • Include it in the top of every compilation unit? (Which is weird, since it’s a code file.). Then that’s the same as build_flags = -include ../include/M5CoreS3/examples/Basic/wakeup/wakeup.ino
  • Add the directory in which the .ino file is in to include search path, so that #include <wakeup.ino> may be found? This is weird because the .ino file is not pre-proprocessed to be proper C++ code. It’s also simliar to build_flags = -I ../include/M5CoreS3/examples/Basic/wakeup/
  • Add it to the to-be-built source files?

I can divide my question into two completely separate parts:

  1. What I would like to do in this question is to pass parameters to a Python script called with extra_scripts.

  2. But in the end, what I’m trying to do is to be able to test very quickly in PIO examples intended to be used basically in the Arduino IDE. The idea is to make as few modifications as possible to the examples. My current DIY solution works as follows:

  • Create a PIO project.
  • Copy the directory containing the examples into the include directory.
  • In platformio.ini, create an example environment that I want to test.
  • Correct the ino file if necessary.
  • Each environment calls a Python script which creates a src/main.cpp file containing two #include:
#include <Arduino.h>
#include “../include/M5Atom/examples/Basics/Button/Button.ino”

It works, but adding the extra_includes option generates one warning per environment (so that’s a lot).

Note that this is related to my other question here:

The solution to the first part of my question is to rename the variable passed to the script with the prefix custom_.

So this:

extra_includes = "../include/M5CoreS3/examples/Basic/wakeup/wakeup.ino"

becomes something like this:

custom_extra_includes = "../include/M5CoreS3/examples/Basic/wakeup/wakeup.ino"

This is because custom options have to start with custom_ or board_ to not generate a warning.
Source: https://docs.platformio.org/en/latest/scripting/examples/platformio_ini_custom_options.html