0

I have a multiple source files getting generated using custom command and custom target in the cmake_binary_dir. These source files are not getting build as they are generated during buildtime. I don't want to use execute_process to create the sources during configuration time as I dont want these files to get generated over and over again. Add_library( OBJECT) and Target_sources() only works if these sources are already present before the build starts.

Is there any way to achieve this?, also I tried creating custom _command using file(glob...) to capture all source file but file commands also gets executed before the build time

add_custom_command(
   OUTPUT test.txt #phony txt script created to link command to target, the generated src files are dynamic and the names are not constant
   COMMAND ${CMAKE_COMMAND} -E make_directory out_dir
   COMMAND start.bat -i test.ini -o out_dir
)

add_custom_target(gen_src 
   DEPENDS test.txt
)

add_dependency(mainlib gen_src)
#There are multiple .c files that gets generated when this target is executed inside the out_dir folder

file(GLOB_RECURSE SOURCE_FILES
  out_dir/*.c
)

add_library(test_objs OBJECT)

target_sources(test_objs PUBLIC 
   test.txt
   ${SOURCE_FILES}
)
add_dependencies(testobjs gensrc)

add_dependencies(mainlib testobjs)
target_link_libraries(mainlib PRIVATE testobjs)

The above code only builds the SOURCE_FILES to .c.o object files on building the project twice. This is because the file(glob) fails to list the generated src files in the first run

deb
  • 15
  • 7
  • 1
    "Add_library( OBJECT) and Target_sources() only works if these sources are already present before the build starts." - No, this is wrong. Both `add_library(OBJECT)` and `target_sources()` work for generated files, as long as those files are defined as `OUTPUT` clause of `add_custom_command()`, called from the same `CMakeLists.txt`. Please, show (add to the question post) your **exact code** which doesn't work; preferably in form of [mcve]. – Tsyvarev Jun 27 '22 at 15:33
  • I do not understand. `I have a multiple source files getting generated using custom command` Great. What are the input source files? Are there any? How does it work _exactly_? Please post the commands, what are the output file _names_, what command generates what and when, and what are dependencies, and what is your current code. `These source files are not getting build...Add_library( OBJECT) and Target_sources()` So are they getting used for building or not? Please post your code. – KamilCuk Jun 27 '22 at 16:30
  • @Tsyvarev I have added updated my question to show my trial code – deb Jun 27 '22 at 17:29
  • "This is because the file(glob) fails to list the generated src files in the first run" - Yes, this is your **actual** problem. It is NOT `add_library` or `target_sources` which doesn't work with generated files. It is `file(GLOB)` which doesn't work with generated files. Because it doesn't work, the variable `SOURCE_FILES` is **empty**, so you pass **none files** to `add_library` / `target_sources`. See duplicate question about globbing generated files. – Tsyvarev Jun 27 '22 at 17:53
  • The linked answer uses execute_process which I don't want to use as I have mentioned before....is there any other way to solve this other than using execute_process? – deb Jun 28 '22 at 06:57

0 Answers0