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

Cannot use generated target inside of subdirectory with Cmake 3.16 #2166

Open
lc-gd opened this issue Jan 23, 2025 · 0 comments
Open

Cannot use generated target inside of subdirectory with Cmake 3.16 #2166

lc-gd opened this issue Jan 23, 2025 · 0 comments

Comments

@lc-gd
Copy link

lc-gd commented Jan 23, 2025

./CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(helloword_sub LANGUAGES C)

if(NOT TARGET CycloneDDS::ddsc)
  find_package(CycloneDDS REQUIRED)
endif()

idlc_generate(TARGET HelloWorldData_lib FILES "HelloWorldData.idl" WARNINGS no-implicit-extensibility)

add_executable(HelloworldSubscriber subscriber.c)

target_link_libraries(HelloworldSubscriber HelloWorldData_lib CycloneDDS::ddsc)

add_subdirectory(pub)

./pub/CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(helloword_pub LANGUAGES C)

add_executable(HelloworldPublisher publisher.c)

target_link_libraries(HelloworldPublisher HelloWorldData_lib CycloneDDS::ddsc)

The above is the cmake for a slightly modified HelloWorld example program (from cyclonedds/examples/helloworld). Using Cmake 3.16.3 this project will not configure properly:

CMake Error at pub/CMakeLists.txt:4 (add_executable):
  Cannot find source file:

    [REMOVED]/helloworld/build/x86/HelloWorldData.c

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
  .hpp .hxx .in .txx


-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

This happens because prior to cmake 3.20 the GENERATED property is not propagated to subdirectories CMP0118 and so the second executable is not able to regenerate the .c & .h files that it depends on. This can be resolved with a bunch of manual work like adding the following to the CMakeLists.txt of any non-root user of the generated file(s).

set_source_files_properties(../HelloWorldData.c PROPERTIES GENERATED TRUE)
set_source_files_properties(../HelloWorldData.h PROPERTIES GENERATED TRUE)

Most people are going to look to the example programs as a starting point but as soon as any level of complexity is introduced into their project they could encounter this issue. I'd suggest increasing the minimum required CMake version on all examples to 3.20

Or if a cmake guru knows how to make the target re-apply the properties each time it's used (something to add to the generate cmakes?), that would be even better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant