1

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 generatorOutputis 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.

bjhend
  • 1,538
  • 11
  • 25
  • CMake definitely does not delete a file at *configure stage* (when `cmake` is run). At *build stage* the things are controlled by a build system, not by CMake itself. Which *build system* do you use? Make? Ninja? – Tsyvarev Jun 13 '18 at 07:52
  • Oops, forgot to mention the build system, although I had it in mind. See edit above. – bjhend Jun 13 '18 at 08:03

1 Answers1

0

Editing auto-generated files is a bad idea, generally.

If you want to generate your files only once, then consider using execute_process to generate them during configuration step, not the build one.

Otherwise, if you really need them to be generated during build, you'd better arrange thing such way that user code can be plugged in without editing these files.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • Thanks, you're right. But I cannot change that yet, because it's a complex software and my current task is to modernize the build system by converting to CMake. I've already discovered and applied ``execute_process`` for the initial generation during CMake configuration, but I also need regeneration on a later build in some cases. – bjhend Jun 13 '18 at 06:17