Problems defining component that uses code generation

dpfrey
Posts: 3
Joined: Mon Nov 23, 2020 5:03 pm

Problems defining component that uses code generation

Postby dpfrey » Mon Nov 23, 2020 5:39 pm

I am trying to build a component for ESP-IDF v4.1, that uses code generation to generate .cpp and .h files. There is a section in the build system documentation titled "Source Code Generation" that I am trying to use as a reference. The application is not open source, so I will post a minimal dummy component here that gives the same problems. This is what I start with in my CMakeLists.txt based on the documentation.

Code: Select all

add_custom_command(OUTPUT gen.c
                   COMMAND cp -a ${COMPONENT_DIR}/gen_template.c gen.c
                   DEPENDS ${COMPONENT_DIR}/gen_template.c)

add_custom_target(gen DEPENDS gen.c)
add_dependencies(${COMPONENT_LIB} gen)

idf_component_register(SRCS abc123.c
                            gen.c
                       INCLUDE_DIRS include)
When I try to build this, I get this error:

Code: Select all

add_custom_command command is not scriptable
This is concerning because this is pretty much copied directly from the documentation. I googled the error message and I found esp-idf GitHub issue #4540 where a user has also encountered the "add_custom_command command is not scriptable" problem. The solution posted by igrr doesn't use an example directly from the original poster's report, so it's a bit hard to know exactly what he's suggesting. Basically, it comes down to wrapping with "if(NOT CMAKE_BUILD_EARLY_EXPANSION)". Based on the ESP-IDF docs Build System > Build System Internals > Build Process > Enumeration section, I believe that adding this "if" statement will prevent the contained text from being processed in the first pass of the ESP-IDF build. This is what I tried next:

Code: Select all

if(NOT CMAKE_BUILD_EARLY_EXPANSION)
    add_custom_command(OUTPUT gen.c
                       COMMAND cp -a ${COMPONENT_DIR}/gen_template.c gen.c
                       DEPENDS ${COMPONENT_DIR}/gen_template.c)

    add_custom_target(gen DEPENDS gen.c)
    add_dependencies(${COMPONENT_LIB} gen)
endif()

idf_component_register(SRCS abc123.c
                            gen.c
                       INCLUDE_DIRS include)
And now we run into a different problem:

Code: Select all

CMake Warning (dev) at components/abc123/CMakeLists.txt:7 (add_dependencies):
  uninitialized variable 'COMPONENT_LIB'
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Error at components/abc123/CMakeLists.txt:7 (add_dependencies):
  add_dependencies called with incorrect number of arguments
Why isn't COMPONENT_LIB defined? The documentation indicates that it should be defined.

This is where I'm stuck at now. I'm wondering if the "CMAKE_BUILD_EARLY_EXPANSION" stuff is all just a red herring. If it were actually required, I would expect to see it in the docs.

locustcox
Posts: 20
Joined: Tue Sep 04, 2018 9:36 pm

Re: Problems defining component that uses code generation

Postby locustcox » Thu Jun 10, 2021 3:30 am

I know this is well after your original post, but if you're still having issues, let me know and I'll share a copy of my cmakelists that generates some code successfully.

You should just be able to move your add_dependencies outside of the CMAKE_EARLY_EXPANSION check and it should work.

Who is online

Users browsing this forum: No registered users and 283 guests