In my CMake file I have a custom command which calls some external code generation. The generated files are stubs for the user to put his/her own code into. Thus, the code generation makes sure not to overwrite already existing files. My custom command looks like this:
set(generatedOnceSrc foo.h foo.cpp bar.h bar.cpp)
add_custom_command(
OUTPUT ${generatedOnceSrc}
COMMAND ${generateCmd}
VERBATIM
)
add_executable(myProg ${generatedOnceSrc} ${frameworkSrc})
Now I've noticed that at least sometimes the first element of ${generatedOnceSrc}
(foo.h
in this case) becomes deleted and regenerated. Of course, that will cause trouble once the user started to edit the generated foo.h
.
The documentation of add_custom_command
does not mention such a behavior but also does not deny it. So my question is:
Am I right with my observation?
Note, that I already found a workaround like this:
set(generatedOnceSrc foo.h foo.cpp bar.h bar.cpp)
set_source_files_properties(generatorOutput PROPERTIES SYMBOLIC true)
add_custom_command(
OUTPUT generatorOutput
BYPRODUCTS ${generatedOnceSrc}
COMMAND ${generateCmd}
VERBATIM
)
add_custom_target(generatorTarget DEPENDS generatorOutput)
add_executable(myProg ${generatedOnceSrc} ${frameworkSrc})
add_dependencies(myProg generatorTarget)
Unfortunately, this calls ${generateCmd}
on every build, because the symbolic file generatorOutput
is always considered out-of-date and CMake does not check the existence of ${generatedOnceSrc}
any more.
Versions
- Generator: Unix Makefiles
- CMake 3.5.1
- Ubuntu 16.04
Edit
Forgot to mention that I observed this behavior with the Unix Makefiles generator, which is used by default on my Ubuntu 16.04 system.