Dynamically select libraries before build, by user settings

I maintain a project which runs on different hardware boards and offers different features, depending on the board selected by the user. Each feature needs specific libraries, like gps, i2c, display, etc.

To keep build times and firmware small, i want to include only those set of libraries, which is needed for a certain user selected set of functions.

How can i automate this with platformio?
I want to define sets of libraries and need a switch in platformio.ini which allows the user to select.

It should not hurt the build to tell PlatformIO to download all needed libraries for every feature, which get optimized out during the linking stage anyways. Especially if the inclusion of those libraries is sitting behind #ifdef ..., the appropriate lib_ldf_mode will filter them out before the actual build starts. Hence, there should be no need to write the library dependencies any more complicated than this. Just create an environment for each board, add in all the required libraries for this type of board (you can also refactor it to have common environments / blocks to share data between environments per doc) and set e.g. lib_ldf_mode = chain+.

If you want this sort of library selection logic, take a look at Marlin and their features to source files / library mapping logic:

1 Like