How do I add a custom tool and library?

I would like to add a custom tool and headers for supporting compile-time configuration, namely json2cpp. It’s converts JSON files into class header files containing firmware configuration as constants. The generated files also reference its supporting headers in order to compile.

It’s unclear how I would go about creating a platform IO dependency on json2cpp from reading the documentation. I would like to to perform the following:

  1. Automatically clone json2cpp as a repo from GitHub
  2. Build the json2cpp project using CMake.
  3. Install the json2cpp artifacts in the current project as a tool or locally
  4. Invoke json2cpp as part of the pre-build process to generate headers in src folder from a JSON file
  5. Reference json2cpp supporting header files during the build

Usually when PlatformIO uses a “tool”, it is uses it in a “package”, each OS (windows_amd64, linux_x86_64, …) has its own. For example, tool-avrdude in the registry or API. Using precompiled packages lessens the requirements on the user’s PC. But those “packages” usually get used inside of platforms.

You can e.g. at run determine the type of OS (ref), lookup the download URL from their github (ref), download it, unpack it and invoke it. Let me show you an example…

I’ve created a working showcase with an ESP32 firmware that prints the data from the autogenerated json2cpp output at

Wow! Cool! This is precisely what I’m looking for!

And just for completeness’ sake: If it’s not important that it’s firmware compile-time constant, a much easier way is to use the filesystem capabilities of the ESP32. I.e., put the configuration JSON file in the data/ folder of the project, package it it as a e.g. LittleFS filesystem and upload it, read the file back at runtime using the LittleFS and ArduinoJSON library and act on it.

1 Like

I don’t want any performance penalties when you’re down to the nanoseconds. Of course you can load it before hand. Ideally, I would like it burned in the binary so the device is configured and ready to go.