Cmake definitions

Why not the other way around? You can create an environement which uses build_flags = -DFORCE_CFG to create the macro, and based on that, the code will try to include src/forceCfg.h, and another environment which does not do that.

Or, if you really want to script it,

platformio.ini

[env:uno]
platform = atmelavr
board = uno
framework = arduino
extra_scripts = pre:force_cfg.py

force_cfg.py

from os.path import isfile, join
Import("env")

file_path = join(env.subst("$PROJECT_SRC_DIR"), "forceCfg.h")
if isfile(file_path):
    print("File exists, defining Macro")    
    env.Append(CPPDEFINES=[("FORCE_CFG")])

src/main.cpp

#include <Arduino.h>
#ifdef FORCE_CFG
#warning FORCE_CFG was defined
#else
#warning FORCE_CFG was not defined
#endif

#ifdef FORCE_CFG
  #include "forceCfg.h"
#endif

void setup(){}
void loop() {}

src/forceCfg.h

#pragma once

#define SOMETHING 123

When src/forceCfg.h exists, compiles as

src\main.cpp:3:2: warning: #warning FORCE_CFG was defined [-Wcpp]
 #warning FORCE_CFG was defined
  ^~~~~~~

When it is renamed to something else, compiles as

src\main.cpp:5:2: warning: #warning FORCE_CFG was not defined [-Wcpp]
 #warning FORCE_CFG was not defined
  ^~~~~~~
1 Like