Search code examples
cmakeqt-creator

How to update cmake project version if project set up not in top-level cmakelists?


(you may be shocked by the setup of the CMake design of our project. I know that it was not done in a good way, but unfortunately the efforts to change a running system are quite less. So the scope of this question is not how to change the project structure.)

I have a project with a top-level CMakeLists.txt. In this file the project version is specified. The project contains different products selectable by a define. From command line everything works as expected.

The problem comes when using Qt Creator. If I load the top level CMakeLists.txt and configure the cmake environment I can set a product (the configuration is stored in the *.user file next to the CMakeLists.txt). If I now want to work on a different product I have to change the settings, re-run cmake and have to compile everything again. So for this setup it is not possible to work on different products at the same time (for example: checking if the changes work as expected for every product).

To solve this I've create additional CMakeLists.txt (in different folders) which include the top-level CMakeLists.txt and in this additional files I set up the defines for each product. Now I can work on different products using Qt Creator at the same time. With this setting the project version is not updated in CMakeCache. The manual states, that it uses the project information from the top-level CMakeLists.txt, which is now the Qt Creator 'wrapper'. If there is no project defined cmake creates an artificial one called 'Project'.

Now my question: Is there a way to propagate the real project to the artificial top-level CMakeLists.txt?


Solution

  • Together with the comment from @Tsyvarev and this post I could build this:

    project(TOP_LEVEL_PROJECT VERSION 0.0.0.0 DESCRIPTION "foobar" LANGUAGES C CXX)
    ...
    add_subdirectory(${PROJECT_ROOT})
    get_directory_property(SUB_PROJECT_VERSION DIRECTORY ${PROJECT_ROOT} DEFINITION PROJECT_VERSION)
    set(CMAKE_PROJECT_VERSION ${SUB_PROJECT_VERSION} CACHE STATIC "" FORCE)
    # repeat for CMAKE_PROJECT_VERSION_MAJOR/_MINOR/_PATCH/_TWEAK
    

    Now I see that in the CMakeCache.txt the entries are updated as I want it.