Custom platformio.ini variables to be passed to build_flags?

Hi guys,
Is it possible to declare “custom” variables in platformio.ini as in a Makefile? I’m trying to port the below Makefile to platformio, but I’m confused about the variables, like “BOOTLOADER_START” and the others shown. This will be the platformio.ini file of an ATtiny85 I2C bootloader project I’ve written, currently compiled with avr-gcc+Make. If there is a better way to solve this, I’m all ears.
Thank you!

TARGET = bootloader

# Compiler options
CFLAGS += -Wall -g2 -Os -std=gnu99
# Splits up object files per function
CFLAGS += -ffunction-sections -fdata-sections
# The "nostartfiles" option allows ditching the compiler-supplied "crt1.S" file to include a custom one
CFLAGS += -nostartfiles
# Optimizations aimed at reducing memory space usage
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -mno-interrupts -mtiny-stack
CFLAGS += -fno-inline-small-functions -fno-move-loop-invariants -fno-tree-scev-cprop -fno-jump-tables
# Bootloader TWI address and memory start position
# Linker options
LDFLAGS = -Wl,--relax,--section-start=.text=$(BOOTLOADER_START),--gc-sections,-Map=$(TARGET).map

Use build_flags for that task, these will then be visible to your code. Same for adding new linker flags.

Use build_flags for that task, these will then be visible to your code. Same for adding new linker flags.

Thanks for your quick answer. Yes, I’m aware of that, what I define with “build_flags” is visible to the code, that’s great. I meant variables to be used inside “platformio.ini” itself. To declare a parameter once and then using it several times within the .ini, or even shared across different “.ini” files included with "extra_configs ". E.g. this doesn’t work:

build_flags =
; Bootloader setup
    -D TWI_ADDR=19
; Linker options

Unknown options in the platformio.ini will be treated as a warning, so it’s not very clean to do my_variable = someval inside the ini. Though it might still work technically. See e.g. an example in the docs. Accessing that has then to be done via ${environmentname.variablename}.

There also exists the possibility of using Advanced Scripting. In there you can also readout the CFLAGS of the env to get the value you specified via the build flags again.

I’ll take a look at both alternatives, thank you!

But it’s correct that I cant create arbitrary envs and access them from other board envs? eg here it says newenv doesnt exit. Yes, I can use [env] for such but this does have some limitations.

build_flags =
  !echo "-D"$(echo  "RGR")

build_flags =

platform = ststm32
board = nucleo_f030r8
framework = stm32cube
build_flags =