CMake custom target macro definition

bnp117
Posts: 17
Joined: Fri Jun 15, 2018 3:08 am

CMake custom target macro definition

Postby bnp117 » Wed Apr 29, 2020 3:45 am

I'm currently trying to migrate my build system from GNU make to Cmake. In my original Makefile, i had multiple different targets that were just invoking the `build` target with different flags controlling portions of the code.

For example, i may have a target like:

Code: Select all

foo:
    CFLAGS='-DFOO=1' $(MAKE) all
    
bar:
    CFLAGS='-DBAR=1' $(MAKE) all
    
tests:
    CFLAGS='-DUNIT_TESTING' $(MAKE) TEST_COMPONENTS='my_component' flash monitor
this way I'd be able to enable/disable certain functionality by changing the targets

Code: Select all

make tests
A common use of these flags was to enable/disable additional debugging helpers. The UNIT_TESTING flag would switch my main() function to invoke the unit-tester runner and also enable additional unit-testing drivers.

Is there a way I can do this in CMake+Ninja+idf_ext.py ? I know i can create custom targets via CMake, and I can set some define_cache_entry via idf_ext.py, but I just can't seem to put it all together. Thanks!

tl;dr I enabled additional helper code via the flag UNIT_TESTING which also enabled the unity driver; compiling all this code was done via a make target, trying to accomplish the same thing with CMake+Ninja.

bnp117
Posts: 17
Joined: Fri Jun 15, 2018 3:08 am

Re: CMake custom target macro definition

Postby bnp117 » Thu Apr 30, 2020 2:33 am

I was able to solve this by assigning a value to a cmake variable in idf_ext.py via an action callback via define_cache_entry. Then, in my CMakeList file I pass this along as a compile definition. Definitely seems round about, but hey it works.

waterfox
Posts: 20
Joined: Sun Mar 22, 2020 10:19 pm

Re: CMake custom target macro definition

Postby waterfox » Tue Jul 07, 2020 4:16 pm

Hi,
I also want to compile my code for different types of Boards. At the moment I have to go to the menuconfig and switch there to the config I want to use.
For Example: I have an Analog and Digital board. So I have to select analog or digital into the menuconfig.
I would like to set the board type directly before compiling. Or better compile the code for both boards into different directories.
I have seen that I can have "sdkconfig.default.target". But it seems like I can only select there if I have an esp32 or esp32S.

Is there anyway to switch the boards into the idf.py compile command?
@bnp117 can you tell me how you solve that problem? I have not understand what you have done.

Thanks

bnp117
Posts: 17
Joined: Fri Jun 15, 2018 3:08 am

Re: CMake custom target macro definition

Postby bnp117 » Tue Jul 14, 2020 4:20 am

I do basically exactly this, checkout my repo Jolt Wallet.

I have a few different sdkconfig.defaults for a few different target boards here. Basically I first apply sdkconfig.defaults, followed by sdkconfig.defaults.${TARGET_BOARD}, followed by sdkconfig.defaults.personal.

The logic for applying these sdkconfig.defaults is here. The target board is determined by the environment variable TARGET_BOARD. If this env var is not set, then a default is assigned (in this case, "joltv1"). This file is included in my main CMakeLists.txt here. At the bottom of my cfg.cmake, I add the TARGET_BOARD to the compile definitions so i can assign it to a char array in my code. This way I can have some other query commands (whether it be through a REST API, over serial, bluetooth, whatever) return this value so some other service can know what hardware flavor it's talking to.

Who is online

Users browsing this forum: esp_certified and 213 guests