PlatformIO Community

Src_filter = -<src/> +<other_folder/> not taken into account

Hello all

Coming to you again with a weird phenomenon that happened to me, still working for Respiraworks.
I hadn’t tested this for some time, but it appears now that building for one of the environments which uses a different src_dir than src/ does not work anymore: platformio still includes main.cpp from the src dir, and does not include the other directory.

I have built a minimal example for this.

The most relevant file being platformio.ini:

[platformio]
default_envs = nucleo_l452re

[env:nucleo_l452re]
platform = ststm32
board = nucleo_l452re
framework = arduino

[env:copy_env]
platform = ststm32
board = nucleo_l452re
framework = arduino
src_filter = ${env.src_filter} -<src/> +<src_copy/>

When in this project, platformio run -e copy_env builds properly even if src_copy/main.cpp should clearly fail compilation.

As a matter of fact, it is bringing me a warning from src/main.cpp (which should be excluded): src/main.cpp:9:7: warning: unused variable 'i' [-Wunused-variable] , and we can see in verbose mode that it DOES build src/main.cpp, and does not build src_copy/main.cpp.
Illustrated by:

pio run -e copy_env -v >test.txt
Processing copy_env (platform: ststm32; board: nucleo_l452re; framework: arduino; src_filter: +<*> -<.git/> -<.svn/> -<src/> +<src_copy/>)
[...]
arm-none-eabi-g++ -o .pio/build/copy_env/src/main.cpp.o -c -std=gnu++14 -fno-threadsafe-statics -fno-rtti -fno-exceptions -fno-use-cxa-atexit -mfpu=fpv4-sp-d16 -mfloat-abi=hard -Os -mcpu=cortex-m4 -mthumb -ffunction-sections -fdata-sections -Wall -nostdlib --param max-inline-insns-single=500 -DPLATFORMIO=50101 -DSTM32L452xx -DSTM32L4xx -DARDUINO=10808 -DARDUINO_ARCH_STM32 -DARDUINO_NUCLEO_L452RE -DBOARD_NAME=\"NUCLEO_L452RE\" -DHAL_UART_MODULE_ENABLED -I[bunch of includes] src/main.cpp

Per docs

PATH is relative to src_dir

So excluding src by doing -<src> is not possible since all path exclusions / inclusions are seen as being inside src already.

I’m sure if you would have the structure

src
   source_orig
   source_copy

And would do

src_filter = +<*> -<.git/> -<.svn/> -<example/> -<examples/> -<test/> -<tests/> -<source_orig/>

the filter would work to only compile the source_copy folder.

Thanks a lot, that does work!
In the main project, I prefer not changing the whole structure and having something along the lines of

src_filter = ${env.src_filter} -<../src/> +<../other_dir/>

Which works as well.

The weird thing is that it did work a few months ago without making the paths relative to src/. The last time I used other environments was in late 2020 and I so I can’t really pinpoint when this changed.

Thanks for this quick reply! :wink:

As an afterthought, are you sure you don’t just want to change the complete src_dir to just re-point the main source directory from src/ to something else?

Indeed, using src_dir=./ with src_filter only including the desired directory for each environment works as well, and looks simpler to follow.
Will check with other members of the team which option is best suited for our needs, but I find this second one more straightforward.

If you do this then also include/ and lib/ are maybe seen as normal source directories which might break the library dependency finder. I meant more src_dir = src copy and src_dir = src as options.

Indeed, what I had in mind with src_dir=./ was ditching env.src_filter altogether and having src_filter = +<desired_folder/> in each environment, so it only matches the desired folder. Which is pretty much how the project structure was designed: all of the common source code is actually in lib/ and each environment has its own src directory (containing the main.cpp file, some environment-specific headers and some non-code files).