We have an in-house code generator similar to Bison (generating both h- and cpp-files). We describe it with an add_custom_command() call. The generation of the h/cpp-files is independent of Debug/Release, so I had assumed that CMake + Visual Studio would arrange for the files to be generated just once.
But the behavior I see is:
- first Debug build → generates the files
- first Release build → generates the files again
- later Debug build → no further generation
The CMake fragment we use look like:
add_custom_command(
OUTPUT
${CMAKE_BINARY_DIR}/foo.h
${CMAKE_BINARY_DIR}/foo.cpp
DEPENDS
${CMAKE_SOURCE_DIR}/foo.spec
COMMAND
${foogen} ${CMAKE_SOURCE_DIR}/foo.spec --outdir ${CMAKE_BINARY_DIR}
)
target_sources(prog1 PUBLIC
prog1.cpp
${CMAKE_BINARY_DIR}/foo.h
${CMAKE_BINARY_DIR}/foo.cpp
)
The second generation of the files also causes unnecessary rebuild of other sources depending on the h-file.
Am I using add_custom_command() the wrong way? Or is there a better way to do this?
Regards,
/Johan Holmberg