Invoking PIO CLI commands via cmake

I have a folder, as below:

top-level/
   CMakeLists.txt.         <===== A
   projectB/
      platformio.ini    <============ With environments [env:B_X], [env:B_Y] etc.
      src/
        ...
      CMakeLists.txt.      <===== B
   projectC/
      platformio.ini    <============ With environments [env:C_X], [env:C_Y] etc.
      src/
        ...
      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:

ProjectB:
   [env:B_X]
   [env:B_Y]

ProjectC:
   [env:C_X]
   [env:C_Y]

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

Build:
   pio run

Flash:
   pio flash

Monitor:
   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

https://github.com/platformio/platformio-core/blob/develop/platformio/project/tpls/clion/CMakeLists.txt.tpl#L21-L25

1 Like