Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake -D settings are not passed to components (IDFGH-13793) #14651

Open
3 tasks done
KammutierSpule opened this issue Sep 30, 2024 · 11 comments
Open
3 tasks done

cmake -D settings are not passed to components (IDFGH-13793) #14651

KammutierSpule opened this issue Sep 30, 2024 · 11 comments
Labels
Status: Opened Issue is new Type: Bug bugs in IDF

Comments

@KammutierSpule
Copy link

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

IDF version.

5.3

Operating System used.

Linux

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

None

What is the expected behavior?

I expected that the defined option on main cmakefile or via command line, eg: cmake -DOPTION=ON to be passed to components and main component

What is the actual behavior?

At this moment, only the main CMakeLists.txt file receive this options (I test) but they don't appear on components or main

Steps to reproduce.

  1. on main root CMakeLists.txt add option(BUILD_OPTION "Set my option define" OFF)
  2. add something to verify that is is detected on main root, but not on components
  3. run the build of your project such way that it adds -DBUILD_OPTION=ON
    ...

Build or installation Logs.

No response

More Information.

No response

@KammutierSpule KammutierSpule added the Type: Bug bugs in IDF label Sep 30, 2024
@espressif-bot espressif-bot added the Status: Opened Issue is new label Sep 30, 2024
@github-actions github-actions bot changed the title cmake -D settings are not passed to components cmake -D settings are not passed to components (IDFGH-13793) Sep 30, 2024
@igrr
Copy link
Member

igrr commented Sep 30, 2024

Could you please show the part of your component CMakeLists file which uses the BUILD_OPTION?

For example, modifying hello_world example with option(...) and the following works as expected:

idf_component_register(SRCS "hello_world_main.c"
                    PRIV_REQUIRES spi_flash
                    INCLUDE_DIRS "")

message(STATUS "BUILD_OPTION from main/CMakelists.txt: ${BUILD_OPTION}")

@KammutierSpule
Copy link
Author

option(BUILD_OPTION "Set DEBUG define" OFF)
message(STATUS "BUILD_OPTION from root CMakelists.txt: ${BUILD_OPTION}")
message(STATUS "BUILD_OPTION from main/CMakelists.txt: ${BUILD_OPTION}")

I got:

-- BUILD_OPTION from root CMakelists.txt: ON
...
Processing 2 dependencies:
[1/2] espressif/esp_modem (1.1.0)
[2/2] idf (5.3.0)
-- BUILD_OPTION from main/CMakelists.txt: 

@igrr
Copy link
Member

igrr commented Sep 30, 2024

message(STATUS "BUILD_OPTION from main/CMakelists.txt: ${BUILD_OPTION}")

Is that the entire contents of your main/CMakeLists.txt? If no, could you please post the complete file?

I am guessing that the issue you are running into is that component CMakeLists files are evaluated twice: first time in script mode, to get the REQUIRES and PRIV_REQUIRES keyword arguments from idf_component_register, then second time normally via add_subdirectory. If your message(STATUS ...) occurs before idf_component_register, then it should be printed twice: first time with the option not defined (because we are in script mode) and the second time with the option defined.

@KammutierSpule
Copy link
Author

I don't have a project to share at moment.

then it should be printed twice: first time with the option not defined (because we are in script mode) and the second time with the option defined.

I can confirm you:

[2/2] idf (5.3.0)
-- BUILD_OPTION from main/CMakelists.txt: 
...
-- BUILD_OPTION from main/CMakelists.txt: ON

So what does it mean?

Am I able to create a workspace project with cmake build options without create a new folder/repository with a new project for every option?

@igrr
Copy link
Member

igrr commented Sep 30, 2024

Am I able to create a workspace project with cmake build options without create a new folder/repository with a new project for every option?

I'm sorry, I don't understand what you mean. Could you show an example of how you actually use this BUILD_OPTION in main/CMakeLists.txt? Then I might be able to suggest a solution.

@KammutierSpule
Copy link
Author

Am I able to create a workspace project with cmake build options without create a new folder/repository with a new project for every option?

I'm sorry, I don't understand what you mean. Could you show an example of how you actually use this BUILD_OPTION in main/CMakeLists.txt? Then I might be able to suggest a solution.

I have different uses cases: Debug/Release profiles, UnitTests profiles, different options that require different files to be included.. (different components/modules too)

@igrr
Copy link
Member

igrr commented Sep 30, 2024

Regarding different components, you can use the following in project CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)

option(COMPONENT_X_INCLUDED "Include feature X" OFF)
set(COMPONENTS main)
if(COMPONENT_X_INCLUDED)
  list(APPEND COMPONENTS component_x)
endif()

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

project(your-project)

For adding source files or compilation options, you can use the following in your component CMakeLists file:

idf_component_register(SRCS "hello_world_main.c"
                    PRIV_REQUIRES spi_flash
                    INCLUDE_DIRS "")
                    
if(ENABLE_DEBUG)   # assuming you have defined ENABLE_DEBUG option in project CMakeLists file
  target_compile_definitions(${COMPONENT_LIB} PRIVATE ENABLE_DEBUG=1)
  target_sources(${COMPONENT_LIB} PRIVATE debug_helpers.c)
endif()

and so on.

@KammutierSpule
Copy link
Author

Do you have more resources to read on how to fine control selected COMPONENTS to the build?

set(COMPONENTS main)
if(COMPONENT_X_INCLUDED)
  list(APPEND COMPONENTS component_x)
endif()

Does this mean if I use it, it will stop from autodetect main and components on the subfolders?

@igrr
Copy link
Member

igrr commented Sep 30, 2024

No, this way main component is still going to be found (it is listed in COMPONENTS list). Also any components that main depends on will be included into the build.

You can read about this in the docs: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html#including-components-in-the-build and the nearby sections.

Edit: you may also check out the talk about IDF build system from the recent DevCon as well as the slides, this might answer some of your questions.

@KammutierSpule
Copy link
Author

KammutierSpule commented Oct 3, 2024

Sorry I'm not understanding your example:

option(COMPONENT_X_INCLUDED "Include feature X" OFF)
set(COMPONENTS main)
if(COMPONENT_X_INCLUDED)
  list(APPEND COMPONENTS component_x)
endif()

If they are going to be found "automatically", what does this set(COMPONENTS main) and why append a component that will automatically be discovered?

I read it was possible to assign different paths to main (what about components?) and that way I could have a manual control of what components and main is added ?

Thanks for the slides (I didn't check the talk yet).

@igrr
Copy link
Member

igrr commented Oct 3, 2024

I read it was possible to assign different paths to main (what about components?) and that way I could have a manual control of what components and main is added ?

Yes, you can use the same method for this. If OPTION_A is set, you can set COMPONENTS to main_a, if OPTION_B is set, then to main_b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Opened Issue is new Type: Bug bugs in IDF
Projects
None yet
Development

No branches or pull requests

3 participants