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

  • 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. 
Reply
  • 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. 
Children
No Data
Related