Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate the version based on Git Describe #15400

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

/cmake/version.cmake export-subst
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ else()
FIND_AND_SET_CLANG17()
endif()

list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(version)
ParseGitDescribe()
project(
Metalium
VERSION 0.50.0
VERSION ${VERSION_NUMERIC}
DESCRIPTION "Tenstorrent Metalium"
HOMEPAGE_URL "https://github.com/tenstorrent/tt-metal"
LANGUAGES
CXX
)
message(STATUS "Metalium version: ${PROJECT_VERSION}")

if(${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR})
message(
Expand All @@ -40,8 +44,6 @@ if(${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR})
)
endif()

list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

include(project_options)
include(unity)
include(clang-tidy)
Expand Down
93 changes: 93 additions & 0 deletions cmake/version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent AI ULC
#
# SPDX-License-Identifier: Apache-2.0

# May be called prior to project()
function(ParseGitDescribe)
set(version "")
# These will be filled in by `git archive`.
# Building the source outside of git from something that was not exported via `git archive`
# is left as an exercise to whoever is wanting to do that.
set(fallbackVersion "$Format:%(describe)$")
set(fallbackHash "$Format:%h$")
Comment on lines +8 to +12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what this file looks like after it get tarball'd:

image


find_package(Git)
if(Git_FOUND)
execute_process(
COMMAND
${GIT_EXECUTABLE} describe --abbrev=10 --first-parent --dirty=-dirty
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE version
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
execute_process(
COMMAND
${GIT_EXECUTABLE} rev-parse --short=10 HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()
if(NOT version)
set(version ${fallbackVersion})
set(VERSION_HASH ${fallbackHash})
endif()

# Local modifications (dirty), or not
set(dirtyFlagRegex "\\-dirty")
set(VERSION_DIRTY FALSE)
if("${version}" MATCHES "${dirtyFlagRegex}$")
set(VERSION_DIRTY TRUE)
string(REGEX REPLACE "^(.*)${dirtyFlagRegex}$" "\\1" version "${version}")
endif()

# On a Tagged commit, or not
set(untaggedRegex "^(.*)\\-([0-9]+)\\-g([0-9a-f]+)$") # tag-count-ghash
if("${version}" MATCHES "${untaggedRegex}")
set(VERSION_TAGGED FALSE)
string(REGEX REPLACE "${untaggedRegex}" "\\1" tagname "${version}")
string(REGEX REPLACE "${untaggedRegex}" "\\2" VERSION_COMMIT_COUNT "${version}")
else()
set(VERSION_TAGGED TRUE)
set(tagname "${version}")
endif()

set(major "([0-9]+)")
set(segment "\\.[0-9]+")
set(status "\\-([a-zA-Z]+[0-9]+)") # eg: alpha, beta, RC
set(tagRegex "^[^0-9]*(${major}(${segment}(${segment}(${segment})?)?)?)(${status})?$")
if(NOT "${tagname}" MATCHES "${tagRegex}")
message(WARNING "Cannot parse tag ${tagname}")
return()
endif()

# Major[.Minor[.Patch[.Tweak]]] suitable for CMake
string(REGEX REPLACE "${tagRegex}" "\\1" VERSION_NUMERIC "${tagname}")

# Build a new regex because we cannot access a capture group that was not matched.
# And also only the first 9 capture groups are referencable.
set(statusRegex ".*(${status})$")
if("${tagname}" MATCHES "${statusRegex}")
string(REGEX REPLACE "${statusRegex}" "\\2" VERSION_STATUS "${tagname}")
endif()

set(VERSION_FULL "${VERSION_NUMERIC}")
if(VERSION_STATUS)
string(APPEND VERSION_FULL "-${VERSION_STATUS}")
endif()
if(VERSION_COMMIT_COUNT)
string(APPEND VERSION_FULL "+${VERSION_COMMIT_COUNT}.${VERSION_HASH}")
endif()
if(VERSION_DIRTY)
string(APPEND VERSION_FULL "+m")
endif()

message(STATUS "Version: ${VERSION_FULL}")

# Output variables
set(VERSION_FULL "${VERSION_FULL}" PARENT_SCOPE)
set(VERSION_NUMERIC "${VERSION_NUMERIC}" PARENT_SCOPE)
set(VERSION_HASH "${VERSION_HASH}" PARENT_SCOPE)
endfunction()
Loading