0
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} source)

project(abc)
#a_certain_source_file.cpp is a generated file built by another project.
add_library(${PROJECT_NAME} STATIC ${source} ${xyz_BIN_DIR}/a_certain_source_file.cpp)
add_dependencies(${PROJECT_NAME} xyz)


target_link_libraries(${PROJECT_NAME} PRIVATE xyz)
# include xyz_SOURCE_DIR directory to include a_certain_source_file.cpp
target_include_directories(${PROJECT_NAME} PRIVATE ${xyz_SOURCE_DIR})


# Installation
install(TARGETS ${PROJECT_NAME} DESTINATION ${DST_LIB_DIR})

I have a CMakeLists.txt as above. Trying to build project abc. But to build it, I also need "a_certain_source_file.cpp" which is an auto generated source file from another project called xyz. If xyz had been built from this same CMakeLists.txt, there would have been no problem in add_dependencies working. I am unable to get the dependency on "a_certain_source_file.cpp" resolved with the way i have my CMakeLists.txt right now. Any CMake Enthusiasts or specialists that can help ?

I also saw a close match here - cmake: add_dependencies does not work with external projects but I don't need anything downloaded. So am not sure if this is what I need.

badri
  • 575
  • 2
  • 8
  • 22
  • What is `xyz`? A target? – arrowd Dec 21 '20 at 07:15
  • Yes, xyz is a target that is built separately in another project. when xyz is built, it actually auto generates source files (.cpp and .hpp files) which are needed in project abc. – badri Dec 21 '20 at 11:12

1 Answers1

0

Create custom command with add_custom_command marking ${xyz_BIN_DIR}/a_certain_source_file.cpp as OUTPUT and make it depend on a xyz target. This will teach CMake that ${xyz_BIN_DIR}/a_certain_source_file.cpp is a generated file, and what should it do to generate it.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • The add_custom_command is present in libs/xyz/CMakeLists.txt which builds "xyz". But what I really want is "abc" to be built from its sources along with an extra file "a_certain_source_file.cpp" which is auto-generated by "xyz". – badri Dec 22 '20 at 03:34
  • Then mentioning generated file in sources of some other target should automatically set proper dependency relation and invoke it during the build. It is hard to answer what's wrong in your case without diving into the code. – arrowd Dec 22 '20 at 07:57
  • Thanks @arrowd. That's why i have the line add_dependencies(${PROJECT_NAME} xyz) in the CMakeLists.txt of project "abc". But this add_dependencies isn't working because "abc" and "xyz" are totally different projects. I have other projects where the dependency is within the same project and add_dependencies works in such circumstances. I don't know how to get dependencies between different projects. – badri Dec 22 '20 at 13:04
  • But its clear that these two are handled totally different in CMake i.e. a) adding dependencies within a project and b) adding dependencies between projects. – badri Dec 22 '20 at 13:04