ankish
ankish

Reputation: 11

changing the project name on every build

I have a custom makefile used for versioning. It basically sets the version according to some predefined rules and fetching info from git repo I want to intergrate it with the esp32 cmake project. Till now i managed to run the makefile from the cmakelist.txt file using

add_custom_target(versioning ALL COMMAND ${CMAKE_COMMAND} -P "sometext.cmake" )

and in the sometext.cmake i am calling the makefile which sets a FirmwareVersion variable.

I use this variable in the root cmakelist.txt to set the project(${FirmwareVersion})

But i want the project name to be changed every time firmwareVersion is changed.

But project command is only if there is some changes in cmakelist.txt

Any help to accomplish it is highly apreciated.

Thanks

Upvotes: 1

Views: 1194

Answers (2)

fabian
fabian

Reputation: 82461

You can tell cmake to do a reconfiguration if a file changes. Since the .git directory contains a HEAD file either denoting the commit hash of the detached head or a reference to the file containing the commit hash, you could these files as configuration dependencies.

# assume the .git directory is in the same directory as this CMakeLists.txt
set(GIT_HEAD_FILE "${CMAKE_CURRENT_SOURCE_DIR}/.git/HEAD")

# add dependency to the head itself
set_property(DIRECTORY . APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${GIT_HEAD_FILE}")

file(READ "${GIT_HEAD_FILE}" GIT_HEAD_CONTENT)

if (GIT_HEAD_CONTENT MATCHES "ref: +([^ ]+)")
    # add file referenced in HEAD for non detached head state
    set_property(DIRECTORY . APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/.git/${CMAKE_MATCH_1}")
endif()

... your logic depending on the git version goes here ...

This gets a bit more complicated if you intend on using your repository as submodule of another repo, since the .git directory is not necessarily located in the submodule.

Upvotes: 0

zaufi
zaufi

Reputation: 7119

The project() command also has VERSION named keyword. That is what you actually want. You can store the actual version in some extra CMake file (like version.cmake)

set(VERSION_FROM_GIT "1.2.3+9a6b075")

and use like

include("${CMAKE_CURRENT_SOURCE_DIR}/version.cmake")
project(MyProject VERSION ${VERSION_FROM_GIT} ...)
...

It's up to you how you going to update this file... There are plenty of ways to do that.

Upvotes: 2

Related Questions