Library dependency problem related to Core 5.0?

@ivankravets You might want to have a look at it…

I was notified of problem in the CI of a popular Arduino library that might be related to the new PIO Core version. I’ve isolated the problem to a simple project. It fails to compile:

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
lib_deps = 
    DHT sensor library
#include <Arduino.h>
#include <DHT.h>

void setup() { }

void loop() { }
> Executing task in folder DHT: platformio run <

Processing esp32doit-devkit-v1 (platform: espressif32; board: esp32doit-devkit-v1; framework: arduino)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32doit-devkit-v1.html
PLATFORM: Espressif 32 (1.12.4) > DOIT ESP32 DEVKIT V1
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: 
 - framework-arduinoespressif32 3.10004.200129 (1.0.4) 
 - tool-esptoolpy 1.20600.0 (2.6.0) 
 - toolchain-xtensa32 2.50200.80 (5.2.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Library Manager: Installing DHT sensor library
Library Manager: DHT sensor library @ 1.3.10 has been installed!
Library Manager: Installing dependencies...
Library Manager: Installing Adafruit Unified Sensor
Library Manager: Adafruit Unified Sensor @ 1.1.4 has been installed!
Found 28 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <DHT sensor library> 1.3.10
Building in release mode
Compiling .pio/build/esp32doit-devkit-v1/src/main.cpp.o
Generating partitions .pio/build/esp32doit-devkit-v1/partitions.bin
Compiling .pio/build/esp32doit-devkit-v1/lib8ae/DHT sensor library/DHT.cpp.o
Compiling .pio/build/esp32doit-devkit-v1/lib8ae/DHT sensor library/DHT_U.cpp.o
Archiving .pio/build/esp32doit-devkit-v1/libFrameworkArduinoVariant.a
Indexing .pio/build/esp32doit-devkit-v1/libFrameworkArduinoVariant.a
In file included from .pio/libdeps/esp32doit-devkit-v1/DHT sensor library/DHT_U.cpp:15:0:
.pio/libdeps/esp32doit-devkit-v1/DHT sensor library/DHT_U.h:36:29: fatal error: Adafruit_Sensor.h: No such file or directory

*************************************************************************
* Looking for Adafruit_Sensor.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:Adafruit_Sensor.h"
* Web  > https://platformio.org/lib/search?query=header:Adafruit_Sensor.h
*
*************************************************************************

 #include <Adafruit_Sensor.h>
                             ^
compilation terminated.
Compiling .pio/build/esp32doit-devkit-v1/FrameworkArduino/Esp.cpp.o
Compiling .pio/build/esp32doit-devkit-v1/FrameworkArduino/FunctionalInterrupt.cpp.o
*** [.pio/build/esp32doit-devkit-v1/lib8ae/DHT sensor library/DHT_U.cpp.o] Error 1
Compiling .pio/build/esp32doit-devkit-v1/FrameworkArduino/HardwareSerial.cpp.o
======================================================================================================================= [FAILED] Took 2.65 seconds =======================================================================================================================
The terminal process "platformio 'run'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

Even though the library Adafruit Unified Sensor (a dependency of DHT) is downloaded (see log), but it does not appear as a dependency and the necessary include path is not added.

If the library is explicitly declared in platformio.ini, it works.

Hi @manuelbl

This is a normal behavior now. We now install all dependencies from Arduino’s library.properties manifest but DO NOT INCLUDE all of them automatically into the build process. It was a bug and we fixed it. See Release 5.0.0 · platformio/platformio-core · GitHub

So, dependency is included in the main header file, it will be added to the build process. Otherwise, you need to #include it in your project.

Adafruit adds ALL dependencies to their library.properties even if they are not compatible with the current hardware. That was a bug when people use Adafruit libraries for ESP32/8266 and PlatformIO automatically builds AVR dependencies.

This should work

#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

void setup() { }

void loop() { }

That’s a bit surprising…

How could I write a library similar to the DHT library in the example above such that library users do not need to care about indirect dependencies. In particular such that:

  • library users only need to include DHT.h
  • library users only need to declare DHT in platformio.ini
  • the header file DHT.h can still include additional header files from indirect dependencies such as Adafruit_Sensor.h

Is there something special that can be declared in library.properties to make it work?

PlatformIO uses chain+ mode for library.properties dependencies. It means,

Parses ALL C/C++ source files of the project and follows only by nested includes ( #include ... , chain…) from the libraries. It also parses C, CC, CPP files from libraries which have the same name as included header file.

#include <DHT.h> does not include any dependent headers. I’ve just checked the source code of the DHT library and found DHT_U.h. This file includes #include <Adafruit_Sensor.h>.

The working code will be:

#include <Arduino.h>
#include <DHT_U.h>

void setup() { }

void loop() { }
1 Like

Hi Ivan,

you stated that the issue LDF regression between 4.3.4 and 5.0.1 · Issue #3668 · platformio/platformio-core · GitHub described the same problem. I don’t think so; Adafruit_Sensor.h is included from both Adafruit_BMP280.h and Adafruit_BME280.h, so LDF should’ve picked it up. I still believe it’s a regression in PlatformIO 5.

Continuing:

If I don’t define lib_ldf_mode, the default mode seems to be chain:

LDF Modes: Finder ~ chain, Compatibility ~ soft

That doesn’t work and produces results described in the issue.

However, deep works. Then again, chain+ doesn’t work but this time it’s missing ESP8266WiFi.h (which is present in the dependency graph). Finally, deep+ fails due to the same problem as chain+.

Still continuing:

The plus finder modes probably fail due to conditional importing such as the one in ESPAsyncWiFiManager.h:

#if defined(ESP8266)
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#else

However, my target for the build is d1_mini which definitely has ESP8266 defined.