Conditional Compile Directive based on build_type

Conditional compile directives appear to be a popular subject here but I have not found this one documented (or I have not found the right search parameters.) This is similar to a previous item @maxgerhardt helped with.

I’d like to get build_type into my CPP definitions. I can see it in the project config:

build_type              =  debug

… but using things like:

env.Append(CPPDEFINES=[
  ("BUILD_TYPE", env["build_type"])
])

don’t seem to be correct.

Looking at

which is implemented as

and adding string escaping per Injection of version of the computed libraries - #2 by maxgerhardt, my first guess would be to use

env.Append(CPPDEFINES=[
  ("BUILD_TYPE",  "\\\"%s\\\"" % env.GetBuildType())
])

with testing code

Serial.print("Build type: ");
Serial.println(BUILD_TYPE);
2 Likes

That’s it! Thanks so much!

Or using Stringification, requires PlatformIO Core 6.0.3 (pio upgrade --dev) or above:

env.Append(CPPDEFINES=[
  ("BUILD_TYPE",  env.StringifyMacro(env.GetBuildType()))
])
2 Likes