Custom debug flags

PlatformIO removes all debug/optimization flags before a debug session or when Build Configurations is set to debug and overrides them with -0g -g2 -ggdb2 for ASFLAGS, CCFLAGS, and LINKFLAGS build scopes.

An extra script allows us to override PlatformIO’s default behavior and declare custom flags. See example below where we override -Og with -O0:

platformio.ini:

[env:teensy31]
platform = teensy
board = teensy31
framework = arduino
extra_scripts = custom_debug_flags.py

custom_debug_flags.py:

Import("env")

if env.GetBuildType() == "debug":
   for scope in ("ASFLAGS", "CCFLAGS", "LINKFLAGS"):
      for i, flag in enumerate(env[scope]):
         if flag == "-Og":
            env[scope][i] = "-O0"