How to pass variables to west

I want to pass a variable to my program so I'm using the command:
west build -b nrf52840dk_nrf52840 -p --build-dir build -- -Dmy_variable=40

Then in my program I want to print it:

int main()
{
  #ifdef my_variable
    printf("my_variable = %i",my_variable);
  #else
    printf("my_variable is not defined");
  #endif
}


But the result is "my_variable is not defined". What am I doing wrong?

Parents
  • Hi,

    The slightly more complex but more in the "Zephyr spirit", method would be to add a Kconfig file to your project defining it:

    config MY_VARIABLE
    int "My variable explain explain explain"
    range 10 100
    help
        This is an example for how to set a custom variable for your application
    
    menu "Zephyr Kernel"
    source "Kconfig.zephyr"
    endmenu
    

    And then use -DCONFIG_MY_VARIABLE.

    This will let you define it in prj.conf if you want as well.
    And you can take advantages of features of Kconfig, such as the range attribue I show in my example. (That can also be removed, it is optional).

    Regards,
    Sigurd Hellesvik

Reply
  • Hi,

    The slightly more complex but more in the "Zephyr spirit", method would be to add a Kconfig file to your project defining it:

    config MY_VARIABLE
    int "My variable explain explain explain"
    range 10 100
    help
        This is an example for how to set a custom variable for your application
    
    menu "Zephyr Kernel"
    source "Kconfig.zephyr"
    endmenu
    

    And then use -DCONFIG_MY_VARIABLE.

    This will let you define it in prj.conf if you want as well.
    And you can take advantages of features of Kconfig, such as the range attribue I show in my example. (That can also be removed, it is optional).

    Regards,
    Sigurd Hellesvik

Children
  • I tried it, but I could not get it to work.

    I created the file Kconfig.application in my project folder. When I compile I still get the cmake warning:

    CMake Warning:
    Manually-specified variables were not used by the project:

    MY_VARIABLE

    How does west find the Kconfig file? Do I need to add something to CMakeLists.txt? Do you have any references for documentation how to do this?

  • I'm not sure if Kconfig.application is the right naming for Kconfig files.
    So I renamed it to Kconfig.defconfig.
    Then I also added in CMakeLists.txt the following:

    set(ZEPHYR_EXTRA_KCONFIG_FILES ${CMAKE_CURRENT_SOURCE_DIR}/Kconfig.defconfig)


    When I print the variable in CMakeLists.txt, it gets the correct value:
    message("MY VARIABLE is: ${MY_VARIABLE}")


    But in main.c MY_VARIABLE is still undefined.

    This is my Kconfig.defconfig:

    config MY_VARIABLE
    string "my variable explain bla bla"
    default "default my variable"
    help
    This is an example for how to set a custom variable for your application

  • Have a look at our samples. They use a file named simply "Kconfig". Does that work for you?

  • I solved it by adding CONFIG_ in front of my variable in main.c

  • So what exactly does:

    west build -b nrf52840dk_nrf52840 -p --build-dir build -- -Dmy_variable=40


    do? I thought it allowed you to use {y_variable} in CMakeLists.txt, but that does not seem to be the case. I want to configure how .conf files are added based on a custom variable "MONKEY_BUILD", but it is completely ignored in cmake. I've tried adding it as additional cmake in build config. Then tried this to simplify it down:

    west build -p auto -- -DMONKEY_BUILD=dev

    however, this outputs an empty string:

    message(STATUS "MONKEY_BUILD = ${MONKEY_BUILD}")
    Update
    ----------
    I thought I'd waste some more time on this and try out random stuff to try and learn more. Turns out I can only use existing CMAKE defined variables. For example: -DCMAKE_BUILD_TYPE=prod. And interestingly, I can set these to custom values. For example this is fine - 

    if(CMAKE_BUILD_TYPE STREQUAL "dev")
       list(APPEND OVERLAY_CONFIG "${CMAKE_SOURCE_DIR}/../0-monkey/config/build/dev.conf")
    endif()
    - which allows me to cleanly create a configuration geared towards unit testing. Since it takes nearly 10 minutes to do a pristine build with stage 1 & 2 bootloaders and all the other stuff in my main app, this is essential. 

    I may be misreading the documentation on this, but it looks very much like it is wrong. You can't just use a custom my_varaible and expect to use it to control the execution of your cmake code. I can crack on with my work now, but this has cost me time and left me feeling irritated. There are so many issues like this that cost us developers time and cause frustration. It would be great if you could fix / document them better. 
Related