PlatformIO ini for building a single example from an arduino library that doesn't use /src

Hey, I want to build a specific example, e.g. examples/PM25_test/PM25_test.ino and to reference the root folder for includes (lib_dir, lib_extra_dirs, build_src_filter, build flag -I.) in the “most correct” way.

I appreciate that not having the library code, and main code file (examples), in the src folder is going against the platformIO desires. Ignoring what platformIO wants, is what I’m asking possible without refactoring the file tree?

I have had some hacky build settings work, but not reliably on another machine, I’ve clearly not quite got it write (and now changed too many times). Simplest win is to copy the sketch to root folder, but that’s not what I want as it alters the file tree along with duplicating examples.

PlatformIO.ini contents (with example copied to root):
; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
description = "Adafruit PM2.5 Sensor Library"
default_envs = adafruit_feather_esp32s3, raspberypi_pico, arduino_nano_new_bootloader, huzzah, adafruit_pyportal_m4
src_dir = ./

[env]
framework = arduino
monitor_speed = 115200
lib_deps = 
    Adafruit BusIO
    Adafruit SH110X
	; ${PROJECT_DIR}/


[common:esp32]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
framework = arduino
lib_deps = 
    Adafruit BusIO
	ESPSoftwareSerial
	; ${PROJECT_DIR}/
monitor_filters = esp32_exception_decoder, time


[common:esp8266]
platform = espressif8266
lib_ignore = 

[common:atsamd]
platform = atmelsam
platform_packages = 
	platformio/framework-arduino-samd-adafruit@^1.7.13
	platformio/tool-jlink@^1.78811.0
lib_ldf_mode = chain+
lib_compat_mode = strict
lib_archive = no
lib_ignore = 

[common:rp2040]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#develop
board = rpipicow
framework = arduino
board_build.core = earlephilhower
board_build.filesystem_size = 0.5m
build_flags = -DUSE_TINYUSB
lib_ignore = 
lib_compat_mode = soft

[common:avr]
platform = atmelavr

[env:arduino_uno]
extends = common:avr
board = uno


; ESP32 Boards:

[env:featheresp32]
extends = common:esp32
board = featheresp32
build_flags = -DARDUINO_FEATHER_ESP32
board_build.filesystem = littlefs
board_build.partitions = min_spiffs.csv

; Adafruit Feather ESP32-S3 2MB PSRAM
[env:adafruit_feather_esp32s3]
extends = common:esp32
board = adafruit_feather_esp32s3
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3 -DBOARD_HAS_PSRAM
board_build.partitions = min_spiffs.csv

; Adafruit Feather ESP32-S3 NO PSRAM
[env:adafruit_feather_esp32s3_nopsram]
extends = common:esp32
board = adafruit_feather_esp32s3_nopsram
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3_NOPSRAM
board_build.partitions = min_spiffs.csv


; Raspberry Pi RP2040/RP2350 boards

[env:raspberypi_pico]
extends = common:rp2040
board = rpipico

[env:raspberypi_picow]
extends = common:rp2040

[env:raspberypi_pico2w]
extends = common:rp2040
board = rpipico2w


; Arduino AVR Boards

[env:arduino_nano_new_bootloader]
extends = common:avr
board = nanoatmega328new

[env:arduino_nano_old_bootloader]
extends = common:avr
board = nanoatmega328


; ESP8266 Boards

; Adafruit Feather HUZZAH ESP8266
[env:huzzah]
extends=common:esp8266
board = huzzah
board_build.f_cpu = 80000000L
; Arduino CLI uses this from adafruit_ci#ci-wippersnapper
; esp8266:esp8266:huzzah:xtal=80,vt=flash,exception=disabled,stacksmash=disabled,ssl=all,mmu=3232,non32xfer=fast,eesz=4M2M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=115200
build_flags =
    -Wl,--gc-sections
    -Wl,-Map=output.map
    -DARDUINO_ESP8266_ADAFRUIT_HUZZAH
    -DDEBUG_ESP_PORT=Serial
    -DVTABLES_IN_FLASH
    -DNO_EXCEPTIONS
    -DNO_STACK_SMASH_PROTECTION
    -DSSL_ALL
    -DMMU_3232
    -DNON32XFER_FAST
    -DDEBUG_DISABLED
    -DDEBUG_LEVEL_NONE
board_build.eesz=4M2M
board_build.filesystem = littlefs

; SAMD51 Boards ;

; Adafruit PyPortal M4
[env:adafruit_pyportal_m4]
extends = common:atsamd
board = adafruit_pyportal_m4
build_flags = -DUSE_TINYUSB
              -DADAFRUIT_PYPORTAL

I did try defining lib_deps with {PROJECT_DIR} or ./ but it fails during deep copying, and couldn’t get that quite working correctly.

Your platformio.ini is far too complex.

If I have understood you correctly, you are the author of the library. List dependencies in the library.json so that they are automatically installed when your library is used. (See this example)

Keep your example simple and include a sample platformio.ini which also contains the lib_deps specification for your library. This should be all that a user of your library needs.

The user can then use the example files (.ino and platformio.ini) to create a new project.

1 Like