Invoking PIO CLI commands via cmake

I have a folder, as below:

   CMakeLists.txt.         <===== A
      platformio.ini    <============ With environments [env:B_X], [env:B_Y] etc.
      CMakeLists.txt.      <===== B
      platformio.ini    <============ With environments [env:C_X], [env:C_Y] etc.
      CMakeLists.txt       <===== C

I want to be able to invoke cmake at the top level directory (corresponding to CMakeLists.txt # A), and build specific environments from within my projectB and projectC folders using calls to add_subdirectory(projectB) and add_subdirectory(projectC). For example, I want the following environments built/flashed/monitored under the projectB and projectC folders:



… and I want the following pio commands run for the below targets:

   pio run

   pio flash

   pio monitor

I’m looking for advice on what the contents of the respective CMakeLists.txt files – #A, #B and #C – need to be, to create the build, flash`` and monitor``` targets above.

My thinking is that I need to use cmake’s add_custom_command() function somewhere, to trigger calls to pio run flash etc, but I’m a cmake newbie, and need some handholding with this.

Example snippets or github link would be greatly appreciated!

You mean pio run -t upload (-e environment_name>) here.

You can look at what CMakeLists.txt PlatformIO exports, since it needs to do the same (CMake target → run pio run…). I think the main piece of code you need is

1 Like