CMake: How to add source-file dependency on generated files?

p-rimes
Posts: 89
Joined: Thu Jun 08, 2017 6:20 pm

CMake: How to add source-file dependency on generated files?

Postby p-rimes » Sat Sep 22, 2018 5:05 pm

Here is one example of something that was easy to do with the Make-based approach, but not sure how to do it in CMake, and the esp-idf documentation doesn't say how to do it, now:
  • Have some esp-idf component (e.g. graphics)
  • Within this graphics component, some generated file (e.g. logo.h)
  • Also within this same graphics component, an implementation file (e.g. graphics_lib.c), which depends on logo.h
  • graphics_lib.c cannot start compiling until logo.h is generated.
This was so easy with a Makefile target:
https://github.com/espressif/esp-idf/bl ... generation

Code: Select all

graphics_lib.o: logo.h
But the CMake build-system documentation only applies the dependency at the COMPONENT level (e.g. where to mention graphics_lib.c depends on logo.h? they both build at the same time, so sometimes logo.h doesn't exist or is halfway finished, when graphics_lib.c is compiling.)
https://github.com/espressif/esp-idf/bl ... generation

Code: Select all

add_custom_target(logo DEPENDS logo.h)
add_dependencies(${COMPONENT_NAME} logo)

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: CMake: How to add source-file dependency on generated files?

Postby ESP_Angus » Mon Sep 24, 2018 6:12 am

Hi p-primes,

Apologies, the CMake build system docs give incorrect instructions for this. Attached is a modified idf-template project which generates a logo.h as part of the "main" component.

The missing steps are:
  • Add the header as a "source file" for the component. Similar to the second example in the CMake FAQ here.
  • Use CMAKE_CURRENT_BINARY_DIR to qualify all the paths to the generated header.
  • Add CMAKE_CURRENT_BINARY_DIR to the private header paths for the component.
The final CMakeLists.txt looks something like:

Code: Select all

set(COMPONENT_SRCS "main.c" ${CMAKE_CURRENT_BINARY_DIR}/logo.h)
set(COMPONENT_PRIV_INCLUDEDIRS "${CMAKE_CURRENT_BINARY_DIR}")

register_component()

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/logo.h
     COMMAND xxd -i ${COMPONENT_PATH}/logo.bmp ${CMAKE_CURRENT_BINARY_DIR}/logo.h
     DEPENDS ${COMPONENT_PATH}/logo.bmp
     VERBATIM)

set_property(DIRECTORY "${COMPONENT_PATH}" APPEND PROPERTY
     ADDITIONAL_MAKE_CLEAN_FILES ${CMAKE_CURRENT_BINARY_DIR}/logo.h)
This generates dependencies in the correct order, the generated files are created before any other source files in the component. Using Ninja, in build.ninja this can be seen here:

Code: Select all

build cmake_object_order_depends_target_main: phony || main/logo.h
build main/CMakeFiles/main.dir/main.c.obj: C_COMPILER__main ../main/main.c || cmake_object_order_depends_target_main
Using make, the component dependencies are in the generated "CMakeFiles/main.dir/depend.make" file (this file is not generated by CMake, it's generated the first time make is run before any actual building takes place):

Code: Select all

main/CMakeFiles/main.dir/main.c.obj: main/logo.h
I remember that previously you also required the "generated" header to be part of the source tree, not generated in the build directory. This looks like it should work as well, just by changing the paths. But I haven't verified that.

Will update the CMake docs ASAP.
Attachments
idf-template-logo-h.zip
(5.66 KiB) Downloaded 551 times

User avatar
mbratch
Posts: 299
Joined: Fri Jun 11, 2021 1:51 pm

Re: CMake: How to add source-file dependency on generated files?

Postby mbratch » Thu Oct 13, 2022 5:05 pm

I know this is an older post, but this is the first post I've seen that addresses this issue head on. That is, the issue of caoxing CMake to set the dependency of a source file on a generated header file work properly. Everything I've read, including this solution, I've tried and nothing seems to work. In the case of this posted solution, it builds the header file, but when it goes to build the cpp file, it can't find the header.

I suspect the way this needs to work is different now after 4 years since this was posted.

Still searching...
Last edited by mbratch on Fri Oct 14, 2022 1:29 am, edited 1 time in total.

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: CMake: How to add source-file dependency on generated files?

Postby ESP_igrr » Thu Oct 13, 2022 8:15 pm

Hi mbratch,
I suggest making a new post and sharing the code snippet which reproduces your issue. Then we can give you some advice on how to solve it.

User avatar
mbratch
Posts: 299
Joined: Fri Jun 11, 2021 1:51 pm

Re: CMake: How to add source-file dependency on generated files?

Postby mbratch » Fri Oct 14, 2022 1:29 am

ESP_igrr wrote:
Thu Oct 13, 2022 8:15 pm
Hi mbratch,
I suggest making a new post and sharing the code snippet which reproduces your issue. Then we can give you some advice on how to solve it.
Thanks. Will do.

Who is online

Users browsing this forum: Baidu [Spider] and 74 guests