How to add EXTRA_CONF_FILE in CMakeLists.txt

Hello

I want to add an extra config file based on the option in prj.conf or in Kconfg.

For example:

CONFIG_WIFI_SCAN_ONLY=y

include the overlay-nrf7000-wifi-scan-only.conf file

How to do that? 

I have chek here https://docs.nordicsemi.com/bundle/ncs-latest/page/zephyr/develop/application/index.html#important_build_system_variables and test the next code in CMakeLists.txt but it doesn't work.

# Include the WiFi scan only overlay
if(CONFIG_WIFI_SCAN_ONLY)
    set(EXTRA_CONF_FILE "overlay-nrf7000-wifi-scan-only.conf")
endif()
 

Parents
  • Hi,

    The problem with this is that Kconfig files must be included before the configuration stage, i.e., they must be included before the following line in CMakeLists.txt:

    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

    However, at this point, the configuration is not finished, which means that CONFIG_WIFI_SCAN_ONLY has not been set yet, so if(CONFIG_WIFI_SCAN_ONLY) will not return true until after this stage.

    You can use snippets as an option.
    In your project directory, create a directory called snippets. If you have several snippets, you can create different subdirectories for them inside this (e.g., scan_only). Create two files inside the snippet directory: snippet.yml and a file with the configurations you want to set, e.g. scan_only.conf, like this:

    <app>/
    ├─── snippets
    │  ├─── snippet.yml
    │  ├─── scan_only.conf
    ├─── src/
    │  └─── main.c
    ├─── CMakeLists.txt
    └─── prj.conf

    Inside snippet.yml, add the following:

    name: scan_only
    append:
      EXTRA_CONF_FILE: scan_only.conf

    Make sure to rename the snippet and configuration file to the name of your file and snippet. Add all the configurations from overlay-nrf7000-wifi-scan-only.conf inside this scan_only.conf file.

    With this, you can build the project and add the snippets using the -S option:

    west build -S scan_only -b nrf7002dk/nrf5340/cpuapp

    For more information, see Using Snippets.

    Best regards,
    Marte

Reply
  • Hi,

    The problem with this is that Kconfig files must be included before the configuration stage, i.e., they must be included before the following line in CMakeLists.txt:

    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

    However, at this point, the configuration is not finished, which means that CONFIG_WIFI_SCAN_ONLY has not been set yet, so if(CONFIG_WIFI_SCAN_ONLY) will not return true until after this stage.

    You can use snippets as an option.
    In your project directory, create a directory called snippets. If you have several snippets, you can create different subdirectories for them inside this (e.g., scan_only). Create two files inside the snippet directory: snippet.yml and a file with the configurations you want to set, e.g. scan_only.conf, like this:

    <app>/
    ├─── snippets
    │  ├─── snippet.yml
    │  ├─── scan_only.conf
    ├─── src/
    │  └─── main.c
    ├─── CMakeLists.txt
    └─── prj.conf

    Inside snippet.yml, add the following:

    name: scan_only
    append:
      EXTRA_CONF_FILE: scan_only.conf

    Make sure to rename the snippet and configuration file to the name of your file and snippet. Add all the configurations from overlay-nrf7000-wifi-scan-only.conf inside this scan_only.conf file.

    With this, you can build the project and add the snippets using the -S option:

    west build -S scan_only -b nrf7002dk/nrf5340/cpuapp

    For more information, see Using Snippets.

    Best regards,
    Marte

Children
  • Hello,  

    Thank you for the feedback. I’m using VS Code with the NRF Connect plugin. In the build configuration, there's an option to add an "Extra Fragment," which works for me, so I don’t have to write the build command myself. However, this doesn’t meet my needs, as I’d like to set up a configuration option in my application to build with or without Wi-Fi, BLE, and other features. My goal is to make this as clear and manageable as possible for other developers on the team.

    Is there a way to include a snippet in the build based on a setting or value in prj.conf or a Kconfig file? From the Using Snippets page, it seems snippets can only be added before the configuration stage.

    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

  • Hi,

    You can add snippets in the VS Code extension, so this should be pretty straightforward to use.

    If you create snippets for the different features, then the other developers can simply include the snippet like this:

    Unfortunately, it is not possible to include Kconfig files based on a value of a Kconfig that is set in prj.conf.

    The reason for this is the order things are done in the configuration stage. The configuration files (Kconfig, prj.conf and any extra files, e.g. overlay-nrf7000-wifi-scan-only.conf) are included and generate the output configuration files autoconf.h and .config. These files includes all the merged configurations from the configuration files that were included, e.g. CONFIG_WIFI_SCAN_ONLY=y if you set this in prj.conf. At this point, the configuration stage is done. So CONFIG_WIFI_SCAN_ONLY is only set at the end of the configuration stage. Therefore, you cannot include a configuration file based on what CONFIG_WIFI_SCAN_ONLY is, as this option is only set after CMake is already done including all configurations files and generating the configurations.
    Using snippets based on a Kconfig option will not work, as you still need to include the configuration files at the beginning of the configuration stage before the Kconfig options are generated.
    You can read more about the configuration stage and how it works in Build System (CMake).

    Best regards,
    Marte

Related