1

I have a test (add_executable(MyTest ...)) which needs a file at run-time. The file is generated using a custom command. I would like to model that run-time dependency in CMake.

Currently, there is a build-time dependency on the generated file using add_dependencies(MyTest GenerateFile) where GenerateFile is a custom target that depends on the custom command's output. Disadvantage: MyTest does not start compiling code until GenerateFile has finished building. This is unnecessary -- I would like to build MyTest and generate the file in parallel.

Another option I considered was to add a new target MyTestAndGenerateFile that depends on both MyTest and GenerateFile. Disadvantage: Calling make MyTest does not generate the file any more. One has to remember to call make MyTestAndGenerateFile instead.

Alternatively, the new target could be called MyTest and the test could be added using add_executable(MyTest_Code ...). Disadvantage: When using the Visual Studio generator, the test code will now be located in a project called MyTest_Code instead of MyTest, which breaks naming conventions. Also, you now have to build the MyTest project, but run the MyTest_Code project, so you cannot use Set as start-up project any more.

In short, I am looking for a form of add_dependencies saying that the dependency is not needed at build time, but rather at run time.

  • CMake can't track how are you launching compiled binaries. The only way to make it track it is to add a custom target that would run it. – arrowd Mar 19 '18 at 11:26
  • @arrowd Maybe "run-time dependency" is misleading here, but I could not think of a better term. I only want to make sure that `GenerateFile` is brought up to date whenever `MyTest` is built, but without `MyTest` waiting for `GenerateFile` to finish before starting to build. – user1083696 Mar 19 '18 at 11:37
  • You may find [craig scott's article on test fixtures in cmake](https://crascit.com/2016/10/18/test-fixtures-with-cmake-ctest/) helpful. you could create a test which actually just replaces the custom command and sets up the generated file. and then make your other test depend on that fixture. – starball Sep 03 '22 at 07:47

1 Answers1

1

What you can try is to add a test that invokes the target GenerateFile, and make your actual test depend on the former. Something like this:

add_test(NAME MakeGenerateFile COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target GenerateFile)
add_test(MyTest MyTest)
set_tests_properties(MyTest PROPERTIES DEPENDS MakeGenerateFile)
piwi
  • 5,136
  • 2
  • 24
  • 48