0

When building a project with CMake, it first checks if it needs reconfiguration. This normally only happens when you change a CMakeLists.txt file. My CMake files contain code generation:

execute_process(
   COMMAND generate_code.sh input.file output.cpp 
)
add_library(lib STATIC output.cpp)

Now, when i change input.file, i want it to reconfigure. I would like to mark input.file as a "configuration dependency". How does this work? Or is there another way to have source files generated?

I know i can do this with configure_file() but this needs to copy the file and this seems like a workaround to me.

kuga
  • 1,483
  • 1
  • 17
  • 38
  • `execute_process()` is the wrong command to use, you want `add_custom_command()`. `input.file` isn't a configuration dependency but a build dependency (assuming `output.cpp` is part of an executable target). – fdk1342 Jan 14 '19 at 17:23
  • Hopefully when `include()` is used to include a file during configuration `cmake` tracks it as a configuration dependency. – fdk1342 Jan 14 '19 at 17:27
  • @Fred: `add_custom_command()` does not work, since `add_executable()` requires the source files (`output.cpp `) to be present at configuration time. `include(input.file)` does add the configuration dependecy, but tries to execute input.file as a CMake file! – kuga Jan 15 '19 at 08:22
  • My comment about `include` was because you said that only `CMakeLists.txt` changes cause configuration to be re-run. I was pointing out that cmake scripts run by using `include` will also cause it to track a configuration dependency. Not that you should try to use the `include(input.file)`. – fdk1342 Jan 15 '19 at 09:23
  • 1
    Then you are using `add_custom_command()` incorrectly. This is from the description of `add_custom_command()`: `A target created in the same directory (CMakeLists.txt file) that specifies any output of the custom command as a source file is given a rule to generate the file using the command at build time.` So either the target is not in defined in same `CMakeLists.txt` file or you are not specifiying the names correctly between the `add_custom_command()` and `add_executable()`. You should edit the question with the actual commands and change the title to match what you are asking. – fdk1342 Jan 15 '19 at 09:35
  • Thanks for explainig, Fred. I have to try it. If this works, it is probably the better solution! – kuga Jan 15 '19 at 09:39
  • I found a good example. https://crascit.com/2017/04/18/generated-sources-in-cmake-builds/. Read all the way and you'll see the section on generated files at build time. – fdk1342 Jan 15 '19 at 09:51
  • Works! even if the `add_custom_command` is inside a function. @Tsyvarev: Maybe we should reopen and rename the question? This is actually the better solution – kuga Jan 15 '19 at 17:04
  • @kuga: Then you need to formulate your question more clear. Currently you have words "configuration" and "reconfiguration" which in most cases refers to the stage, when CMake parses scripts, not when compilation process is performed. – Tsyvarev Jan 15 '19 at 17:23

0 Answers0