How to build (without uploading) specific source files using src_filter

I’m using platformIO in visual studio code, but I’m using a Raspberry Pi as an ISP for some AVR chips embedded in custom board.

I’m trying to set up targets so that I can create .hex files to use on the raspberry pi with avrdude to program several microcontrollers on the board.

My problem is that it doesn’t seem to be doing what I expect.

My platformio.ini file is as follows:

[env:uno]
platform = atmelavr
board = uno
framework = arduino
board_build.f_cpu = 8000000L
extra_scripts = post:extra_script.py

[test]
src_filter = +<*> -<.git/> -<svn/> -<example/> -<examples/> -<src>

What I expected was that running platformio run -e uno -t test would build the source files in the test directory. extra_script.py just saves the hex output of the build for me, I’ve verified that it works when I run it without a target specified.

But when I run the command, I get the following:

PS U:\git\biobox\BioBox-Code\arduino\light_test> platformio run -e uno -t test -v
Processing uno (extra_scripts: post:extra_script.py; board_build.f_cpu: 8000000L; framework: arduino; platform: atmelavr; board: uno; src_filter: +<*> -<.git/> -<svn/> -<example/> -<examples/> -<src>)
----------------------------------------------------------------------------------------------------------
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/uno.html
PLATFORM: Atmel AVR 1.12.5 > Arduino Uno
HARDWARE: ATMEGA328P 8MHz, 2KB RAM, 31.50KB Flash
PACKAGES: toolchain-atmelavr 1.50400.0 (5.4.0), framework-arduinoavr 2.10623.190209 (1.6.23)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 12 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <SPI> 1.0 (C:\Users\dsball\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\SPI)
|-- <StateManager> (U:\git\biobox\BioBox-Code\arduino\light_test\lib\StateManager)
|   |-- <SPI> 1.0 (C:\Users\dsball\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\SPI)
|   |-- <CRC16> (U:\git\biobox\BioBox-Code\arduino\light_test\lib\CRC16)
`test' is up to date.
====================================== [SUCCESS] Took 2.19 seconds ======================================

I know the target is getting called, but it seems like the source filter isn’t doing anything … When I put it directly in [env:uno], the files in are compiled rather than files in as I expect, which seems to indicate that the option has no effect and that I’ve applied it wrong - what am I missing?

I’ve seen posts suggesting potential issues using preprocessor directives to ignore code segments based on build flags, but that seems clunky and bug prone … I’d like to just specify a target main source file using --target.

Shouldn’t that be [env:test]?

Ok, if I understand what you’re trying to do, you’re trying to make it so that the different environments compile specific folders? Using a global [env] so that you don’t need to repeat everyone for each environment, and a override of the location of the src dir to be the actual workspacedir, rather than workspace/src (since src_filter operates relative to the src dir, which is why your filter wasn’t working)… this seems to work with two different main.cpp files in the dummy and test dirs.


[platformio]
src_dir = ${workspacedir}

[env]
platform = atmelavr
board = uno
framework = arduino
board_build.f_cpu = 8000000L
src_filter = +<*> -<.git/> -<svn/> -<example/> -<examples/> -<src/>
;extra_scripts = post:extra_script.py

[env:test]
src_filter = ${env.src_filter} -<dummy/>

[env:dummy]
src_filter = ${env.src_filter} -<test/>

For use of the -t parameter, you might want to go back and look at the docs… it’s for instructions like monitor, upload, etc. Or are you actually defining a custom target in your extra_script?

No, extra_script just has a post-linker operation to save the .hex file (It works, though there might be something easier…).

Import("env", "projenv")
from shutil import copyfile

def save_hex(*args, **kwargs):
    print("Copying hex output to project directory...")
    target = str(kwargs['target'][0])
    copyfile(target, 'output.hex')
    print("Done.")

def save_elf(*args, **kwargs):
    print("Copying elf output to project directory...")
    target = str(kwargs['target'][0])
    copyfile(target, 'output.elf')
    print("Done.")

env.AddPostAction("$BUILD_DIR/${PROGNAME}.elf", save_elf)   #post action for the target hex file
env.AddPostAction("$BUILD_DIR/${PROGNAME}.hex", save_hex)   #post action for the target hex 

Your solution worked great with a few tweaks, and I have a bit better of an understanding of how to use environments now. If only a little :wink: I needed to remove the lib directory as well, since it’s in the workspace and was compiling even incomplete libraries. I’ll leave that here for posterity in case it helps anybody else. Thanks!

[platformio]
src_dir = ${workspacedir}

[env]
platform = atmelavr
board = uno
framework = arduino
board_build.f_cpu = 8000000L
extra_scripts = post:extra_script.py
src_filter = +<*> -<.git/> -<src/> -<tests/> -<example/> -<examples/> -<lib/>

[env:main]
src_filter = ${env.src_filter} +<src/>

[env:subtest]
src_filter = ${env.src_filter} +<tests/subtest/>
3 Likes