Library.json Global build flag


#1

Hi,

I’m working on a library that’s public on platformio’s library registry, but I’ve kind of hit a problem and was wondering if there is a way to solve it.

My library has quite a few dependencies (pubsubclient, i2cdevlib, arduinojson, …), and they all require their own build flags in order to compile correctly. For example, for ArduinoJson I need the -DARDUINOJSON_ENABLE_STD_STRING define build flag to be set. The problem is that because all libraries are linked statically, the build_flags I define in my library.json only apply for building my own library, not the dependencies where I would need them.

Until now, I’ve just been telling users that they should put something like

build_flags =
    -DMQTT_MAX_PACKET_SIZE=512
    -DARDUINOJSON_ENABLE_STD_STRING
    -DBUFFER_LENGTH=32

in their platformio.ini, but that’s not really user-friendly. And now I’ve added another library that requires yet another build flag (that also depends on the platform).

So I was wondering if there’s some way for libraries to set global build flags. I’ve already looked at extra_scripts, but the flags defined there only seem to apply to my own library as well :confused:

# ...
env.Prepend(  # also tried Append and ProcessFlags, didn't work either.
    BUILD_FLAGS=[
        '-DMQTT_MAX_PACKET_SIZE=512',
        '-DARDUINOJSON_ENABLE_STD_STRING',
        '-DBUFFER_LENGTH=32',
    ]
)

Cheers


#2

Did you come up with a good way of dealing with this?


#3

You have to use DefaultEnvironment() to pass flags to a global build environment. For example,

    global_env = DefaultEnvironment()
    global_env.Append(
        CPPDEFINES=[
            ("MQTT_MAX_PACKET_SIZE", 512),
            "ARDUINOJSON_ENABLE_STD_STRING",
            ("BUFFER_LENGTH", 32)
        ]
    )

See example in http://docs.platformio.org/en/latest/librarymanager/config.html#extrascript


#4

I can’t seem to get this to work for me. I can see the defines in the global environment with print global_env.Dump() but run -v shows that those flags are not being used to build the libraries.

My library.json is here: https://github.com/EnviroDIY/ModularSensors/blob/develop/library.json

    "build":
    {
        "srcFilter":
        [
            "+<*.c>",
            "+<*.cpp>",
            "+<*.h>",
            "+<sensors/*.c>",
            "+<sensors/*.cpp>",
            "+<sensors/*.h>"
        ],
        "extraScript": "pioScripts/pio_set_global_flags.py",        
        "libLDFMode": "deep+"
},

then the extra script (pio_set_global_flags.py): https://github.com/EnviroDIY/ModularSensors/blob/develop/pioScripts/pio_set_global_flags.py

Import('env')
from os.path import join, realpath

# append flags to local build environment (for just this library)
env.Append(
    CPPDEFINES=[
        ("NEOSWSERIAL_EXTERNAL_PCINT",),
        ("SDI12_EXTERNAL_PCINT",)
    ]
)
print ">>>>>LOCAL ENV<<<<<"
print env.Dump()

# append the same flags to the global build environment (for all libraries, etc)
global_env = DefaultEnvironment()
global_env.Append(
    CPPDEFINES=[
        ("NEOSWSERIAL_EXTERNAL_PCINT",),
        ("SDI12_EXTERNAL_PCINT",)
    ]
)
print "<<<<<GLOBAL ENV>>>>>"
print global_env.Dump()

And just some of the relevant chunks of the verbose output:


>>>>>LOCAL ENV<<<<<
'CPPDEFINES': [ ('PLATFORMIO', 30602),
('ARDUINO_AVR_ENVIRODIY_MAYFLY',),
('F_CPU', '$BOARD_F_CPU'),
('ARDUINO_ARCH_AVR',),
('ARDUINO', 10805),
('NEOSWSERIAL_EXTERNAL_PCINT',),
('SDI12_EXTERNAL_PCINT',)],

<<<<<GLOBAL ENV>>>>>
'CPPDEFINES': [ ('PLATFORMIO', 30602),
('ARDUINO_AVR_ENVIRODIY_MAYFLY',),
('F_CPU', '$BOARD_F_CPU'),
('ARDUINO_ARCH_AVR',),
('ARDUINO', 10805),
('NEOSWSERIAL_EXTERNAL_PCINT',),
('SDI12_EXTERNAL_PCINT',)],

Collected 44 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <EnviroDIY_ModularSensors> 0.17.1 #1b2407d [git+https://github.com/EnviroDIY/ModularSensors#develop] (xx_myFolder_xx\.piolibdeps\EnviroDIY_ModularSensors)
|   |-- <Arduino-SDI-12> 1.3.4 (xx_myFolder_xx\.piolibdeps\Arduino-SDI-12_ID1486)

avr-g++ -o .pioenvs\mayfly\src\main.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega1284p -DPLATFORMIO=30602 -DARDUINO_AVR_ENVIRODIY_MAYFLY -DF_CPU=8000000L -DARDUINO_ARCH_AVR -DARDUINO=10805 -DNEOSWSERIAL_EXTERNAL_PCINT -DSDI12_EXTERNAL_PCINT -I.piolibdeps\SoftwareSerial_ExtInts\src -I.piolibdeps\AltSoftSerial -I.piolibdeps\EnviroDIY_ModularSensors\src -I.piolibdeps\YosemitechModbus_ID2078\src -I.piolibdeps\TinyGSM\src -I.piolibdeps\MS5803_ID5431\src -I.piolibdeps\Arduino-SDI-12_ID1486\src -I.piolibdeps\DallasTemperature_ID54 -I.piolibdeps\OneWire_ID1 "-I.piolibdeps\Adafruit MPL115A2_ID406" "-I.piolibdeps\DHT sensor library_ID19" "-I.piolibdeps\Adafruit BME280 Library_ID166" "-I.piolibdeps\Adafruit AM2315_ID773" "-I.piolibdeps\Adafruit Unified Sensor_ID31" -I.piolibdeps\ADS1X15_ID344 -I.piolibdeps\SdFat_ID322\src -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\SPI\src -I.piolibdeps\EnableInterrupt_ID311 -I.piolibdeps\EnableInterrupt_ID311\utility -I.piolibdeps\EnviroDIY_DS3231_ID2079\src -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\Wire\src -I.piolibdeps\KellerModbus\src -I.piolibdeps\SensorModbusMaster_ID1824\src -Isrc -Iinclude -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\cores\arduino -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\variants\mayfly src\main.cpp

avr-g++ -o .pioenvs\mayfly\lib289\Arduino-SDI-12_ID1486\SDI12.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega1284p -DPLATFORMIO=30602 -DARDUINO_AVR_ENVIRODIY_MAYFLY -DF_CPU=8000000L -DARDUINO_ARCH_AVR -DARDUINO=10805 -I.piolibdeps\Arduino-SDI-12_ID1486\src -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\cores\arduino -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\variants\mayfly .piolibdeps\Arduino-SDI-12_ID1486\src\SDI12.cpp
avr-g++ -o .pioenvs\mayfly\lib289\Arduino-SDI-12_ID1486\SDI12_boards.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega1284p -DPLATFORMIO=30602 -DARDUINO_AVR_ENVIRODIY_MAYFLY -DF_CPU=8000000L -DARDUINO_ARCH_AVR -DARDUINO=10805 -I.piolibdeps\Arduino-SDI-12_ID1486\src -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\cores\arduino -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\variants\mayfly .piolibdeps\Arduino-SDI-12_ID1486\src\SDI12_boards.cpp
avr-gcc-ar rc .pioenvs\mayfly\lib289\libArduino-SDI-12_ID1486.a .pioenvs\mayfly\lib289\Arduino-SDI-12_ID1486\SDI12.cpp.o .pioenvs\mayfly\lib289\Arduino-SDI-12_ID1486\SDI12_boards.cpp.o

avr-g++ -o .pioenvs\mayfly\libe71\EnviroDIY_ModularSensors\sensors\SDI12Sensors.cpp.o -c -fno-exceptions -fno-threadsafe-statics -fpermissive -std=gnu++11 -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega1284p -DPLATFORMIO=30602 -DARDUINO_AVR_ENVIRODIY_MAYFLY -DF_CPU=8000000L -DARDUINO_ARCH_AVR -DARDUINO=10805 -DNEOSWSERIAL_EXTERNAL_PCINT -DSDI12_EXTERNAL_PCINT -I.piolibdeps\EnviroDIY_ModularSensors\src -I.piolibdeps\YosemitechModbus_ID2078\src -I.piolibdeps\TinyGSM\src -I.piolibdeps\MS5803_ID5431\src -I.piolibdeps\KellerModbus\src -I.piolibdeps\SensorModbusMaster_ID1824\src -I.piolibdeps\Arduino-SDI-12_ID1486\src -I.piolibdeps\DallasTemperature_ID54 -I.piolibdeps\OneWire_ID1 "-I.piolibdeps\Adafruit MPL115A2_ID406" "-I.piolibdeps\DHT sensor library_ID19" "-I.piolibdeps\Adafruit BME280 Library_ID166" "-I.piolibdeps\Adafruit AM2315_ID773" "-I.piolibdeps\Adafruit Unified Sensor_ID31" -I.piolibdeps\ADS1X15_ID344 -I.piolibdeps\SdFat_ID322\src -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\SPI\src -I.piolibdeps\EnableInterrupt_ID311 -I.piolibdeps\EnableInterrupt_ID311\utility -I.piolibdeps\EnviroDIY_DS3231_ID2079\src -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\libraries\__cores__\arduino\Wire\src -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\cores\arduino -IC:\Users\sdamiano.STROUDWATER\.platformio\packages\framework-arduinoavr\variants\mayfly .piolibdeps\EnviroDIY_ModularSensors\src\sensors\SDI12Sensors.cpp

avr-g++ -o .pioenvs\mayfly\firmware.elf -Os -mmcu=atmega1284p -Wl,--gc-sections -flto -fuse-linker-plugin .pioenvs\mayfly\src\main.cpp.o -L.pioenvs\mayfly -Wl,--start-group .pioenvs\mayfly\lib0ea\libSensorModbusMaster_ID1824.a .pioenvs\mayfly\libab8\libKellerModbus.a .pioenvs\mayfly\lib46b\libWire.a .pioenvs\mayfly\lib2cc\libEnviroDIY_DS3231_ID2079.a .pioenvs\mayfly\lib078\libEnableInterrupt_ID311.a .pioenvs\mayfly\lib2ec\libSPI.a .pioenvs\mayfly\lib9bd\libSdFat_ID322.a .pioenvs\mayfly\lib03e\libADS1X15_ID344.a ".pioenvs\mayfly\lib587\libAdafruit Unified Sensor_ID31.a" ".pioenvs\mayfly\lib287\libAdafruit AM2315_ID773.a" ".pioenvs\mayfly\lib573\libAdafruit BME280 Library_ID166.a" ".pioenvs\mayfly\lib998\libDHT sensor library_ID19.a" ".pioenvs\mayfly\lib609\libAdafruit MPL115A2_ID406.a" .pioenvs\mayfly\libd7f\libOneWire_ID1.a .pioenvs\mayfly\libc2a\libDallasTemperature_ID54.a .pioenvs\mayfly\lib289\libArduino-SDI-12_ID1486.a .pioenvs\mayfly\lib2fb\libMS5803_ID5431.a .pioenvs\mayfly\libfe5\libTinyGSM.a .pioenvs\mayfly\lib37a\libYosemitechModbus_ID2078.a .pioenvs\mayfly\libe71\libEnviroDIY_ModularSensors.a .pioenvs\mayfly\lib843\libAltSoftSerial.a .pioenvs\mayfly\lib065\libSoftwareSerial_ExtInts.a .pioenvs\mayfly\libFrameworkArduinoVariant.a .pioenvs\mayfly\libFrameworkArduino.a -lm -Wl,--end-group


#5

I’ll back to this issue soon and have just opened a request on Github: