ESP32 - How to determine build environment in CMAKE

I have several products being built on the ESP32 platform, which uses CMAKE to determine which files to include in the build. I would like to determine the files to include via the cmakelists file programmatically. I know you can read in environment variables in cmake, so if platformio adds one for the build environment, that would be ideal.

My idea would look something like this:

include(${CMAKE_CURRENT_LIST_DIR}/Common/CMakeLists.txt)

if(DEFINED $ENV{BUILD_ENV})
    include(${CMAKE_CURRENT_LIST_DIR}/product/CMakeLists.txt)
endif()

Any suggestions?

Usually PlatformIO adds a PLATFORMIO macro being equal to the used PlatformIO core version.

Can you see that being passed as -DPLATFORMIO=.... in the compiler flags in the project task → Advanced → Verbose Build?

Thanks for the reply!

I just checked, and there is no additional build flags being passed into the project, other then the ones I put in there. If there is a way to read those build flags in cmake, then I can just add my own custom one?

After doing some more searching, I was able to find the solution, so I will share for others.

In the platformio.ini file, you can add

board_build.cmake_extra_args = 
	-DPRODUCT=something

Then in the cmake file, I can check the value of that define:

if(PRODUCT STREQUAL "something")
    include(${CMAKE_CURRENT_LIST_DIR}/product/CMakeLists.txt)
endif()

Then I can check for other strings as well to include what I want.