0

In the below minimal example, if I know the name of the generated source file in advance (test1.txt), I could do the below

cmake_minimum_required(VERSION 3.20)
project(example)
add_executable(example main.cpp ${CMAKE_BINARY_DIR}/test1.txt)
set_source_files_properties(${CMAKE_BINARY_DIR}/test1.txt PROPERTIES GENERATED TRUE)

However, how can I achieve the same thing if I don't know the name of the generated files in advance? For my application, it will also work if the generated files are added to another target instead of the original one. Please see below of my failed attempt

cmake_minimum_required(VERSION 3.20)
project(example)
add_executable(example main.cpp)

add_custom_target(
  example-generate
  COMMAND $<TARGET_FILE:example>
  COMMAND "using bash commands to glob the generated files but need to find a way to output the result to a cmake variable"
  DEPENDS example)

add_library(example2 OBJECT) # the target with the generated unknown sources, but I can't find a way to add sources to it in build time

add_dependencies(example2 example-generate)
user3667089
  • 2,996
  • 5
  • 30
  • 56
  • Probably impossible in a single cmake project. If it's sufficient to run `example` once before configuration, you could consider adding a bootstrap step that creates and builds a cmake project building `example`. You could then run `example` during bootstrap or configuration of your main project and simply collect the sources (assuming you can collect them reliably using e.g. a globbing pattern... – fabian Jan 20 '22 at 19:15
  • soooo weeell run `example-generate` once manually, collect the names of the files, and then input them to cmake? – KamilCuk Jan 20 '22 at 19:44

1 Answers1

1

You can generate a dummy source file with a concrete name that #includes all the others.

You might need to write out a DEPFILE to get accurate incremental-build dependencies (if the list of files depends on something other than the example sources/binary or command-line invocation).

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86