Cmake executable with auto-generated sources

2019-03-20 14:22发布

问题:

I want to make an executable from, for example, test_runner.cpp:

add_executable(myexe ${CMAKE_CURRENT_BINARY_DIR}/test_runner.cpp)

But this particular cpp file is itself auto-generated in a pre-build command:

add_custom_command(
    TARGET myexe PRE_BUILD
    COMMAND deps/cxxtest-4.4/bin/cxxtestgen --error-printer -o "${CMAKE_CURRENT_BINARY_DIR}/test_runner.cpp" src/My_test_suite.h
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)

But now I can't generate new cmake build files because it complains about the missing source, which is indeed missing until pre-build.

回答1:

The crux of the issue is to apply the GENERATED property to "test_runner.cpp". This tells CMake not to check for its existence at configure time, since it gets created as part of the build process.

You can apply this property manually (e.g. using set_source_files_properties). However, the proper way to handle this is to use the other form of add_custom_command, i.e. add_custom_command(OUTPUT ...) rather than add_custom_command(TARGET ...).

If you specify "test_runner.cpp" as the output of the add_custom_command(OUTPUT ...) call, then any target consuming it (in this case "myexe") will cause the custom command to be invoked before that target is built.

So you really only need to change your code to something like:

set(TestRunner "${CMAKE_CURRENT_BINARY_DIR}/test_runner.cpp")
add_executable(myexe ${TestRunner})
add_custom_command(
    OUTPUT ${TestRunner}
    COMMAND deps/cxxtest-4.4/bin/cxxtestgen --error-printer -o "${TestRunner}" src/My_test_suite.h
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)


标签: c++ cmake