1

Edit: my question targets the early configure stage where CMake input files are still being parsed and thus have to be present before include() is being called. So the answer found here: Force CMake to generate configure_file target every build does not solve my problem since it generates files after include() statements have been interpreted.


I have a CMakeLists.txt that includes a file which is generated in the configure stage:

execute_process(COMMAND "my-generator -o generated.cmake") include(generated.cmake) 

Apart from the fact that this approach doesn't feel right (not to say elegant) I now need to re-generate that file before every build (my-generator produces output that incorporates the current time).

My assumption is that I can't use add_custom_command() or add_custom_target() because the file would be generated at compile time but needed in the configure-step.

This very old post suggests to touch the input file so I did this:

execute_process( COMMAND "my-generator -o generated.cmake" COMMAND cmake -E touch "${CMAKE_CURRENT_LIST_FILE}") 

.. which does not produce errors but calling make multiple times won't run the configure step more than once.

What do I do wrong? Is there a better approach?

3
  • 1
    That's not how you use CMake. I am glad, that it does not work, as it would lead to curious (elegant?) CMake project, nobody would understand. Commented Oct 17, 2017 at 10:42
  • 2
    I admit this is abusing CMake principles. Maybe there is another way to accomplish the need to create variables (e.g. a timestamp) used in CMake commands or variables like CPACK_PACKAGE_FILE_NAME. I don't know any. Commented Oct 17, 2017 at 12:30
  • If it's just to the variables, CMake is able to use all kind of configuration stage tools, like execute_process, environment variables manipulation, file parsing to get what you need. Almost everything can be substituted with variables. I only know one scenario when you'd might need to alter CMakeLists.txt file - patching a 3rd-party CMake file. But this does not involve regenerating it every time, because everything else can be driven with runtime variables. Commented Aug 12, 2024 at 9:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.