Platformio.ini common settings for release and debug but not for native

Hello,

I have setup a platformIO project where I use “[env]” to define common settings for release and debug. See shortened example below:

[env]
platform = https://github.com/platformio/platform-ststm32.git
board = nucleo_f091rc
framework = stm32cube
board_build.ldscript = LinkerScript.ld

[env:release]
build_flags = -Os

[env:debug]
build_type = debug
build_flags = -DDEBUG -O2 -ggdb3 -g3

Now I want to add unit testing so I added the native environment:

[env:native]
platform = native

When I try to run the unittest I get the “Unknown Board ID” error, because [env:native] also inherits the common settings from [env]. If I copy the [env] settings in [env:release] and [env:debug] and remove the [env] section it works. But then I loose the advantage of a common section for release and debug settings. What is the best way to handle this? Any good practices?

3 Likes

Per docs there are multiple ways.

You can put options that are shared but not shared with all environments also in a seperate environment (not called env) and reference them with the name of the environment and attribute.

[common_embedded]
platform = https://github.com/platformio/platform-ststm32.git
board = nucleo_f091rc
framework = stm32cube
linker_script = LinkerScript.ld

[env:release]
platform = ${common_embedded.platform}
board = ${common_embedded.board}
framework = ${common_embedded.framework}
board_build.ldscript = ${common_embedded.linker_script}
build_flags = -Os

[env:debug]
platform = ${common_embedded.platform}
board = ${common_embedded.board}
framework = ${common_embedded.framework}
board_build.ldscript = ${common_embedded.linker_script}
build_type = debug
build_flags = -DDEBUG -O2 -ggdb3 -g3

[env:native]
platform = native
; clean, no auto-inherit

The settings are still only defined once but the inclusion has to be repeated in every environment.

A shorter way would be with the extends directive. A common embedded environment is created which is simply inherited and extended by other more concrete embedded environments.

[common_embedded]
platform = https://github.com/platformio/platform-ststm32.git
board = nucleo_f091rc
framework = stm32cube
board_build.ldscript = LinkerScript.ld

[env:release]
extends = common_embedded
build_flags = -Os

[env:debug]
extends = common_embedded
build_type = debug
build_flags = -DDEBUG -O2 -ggdb3 -g3

[env:native]
platform = native
; extends nothing. 
; clean, no auto-inherit
2 Likes

The “extends” directive is what I was looking for, thanks for pointing it out!

If anybody else wondered, it is possible to combine extends and interpolation:

[env:native]
platform: native
test_filter = native/*
; notice `lib_deps` is not defined

[env:native_arduino]
extends = env:native
; `platform` is inherited automatically
test_filter =
    ${env:native.test_filter}
    native_arduino/*
lib_deps =
    ${env:native.lib_deps}
    fabiobatsilva/ArduinoFake