Files
rocm-systems/cmake_modules/utils.cmake
T

147 lines
5.7 KiB
CMake
Raw Normal View History

2018-09-16 00:13:29 -05:00
################################################################################
## Copyright (C) Advanced Micro Devices. All rights reserved.
2018-09-16 00:13:29 -05:00
##
## Permission is hereby granted, free of charge, to any person obtaining a copy of
## this software and associated documentation files (the "Software"), to deal in
## the Software without restriction, including without limitation the rights to
## use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
## the Software, and to permit persons to whom the Software is furnished to do so,
## subject to the following conditions:
2018-09-16 00:13:29 -05:00
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
2018-09-16 00:13:29 -05:00
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
## FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
## COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
## IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
## CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2018-09-16 00:13:29 -05:00
################################################################################
## Parses the VERSION_STRING variable and places
## the first, second and third number values in
## the major, minor and patch variables.
2025-05-06 05:40:18 +00:00
function(parse_version VERSION_STRING)
2018-09-16 00:13:29 -05:00
2025-05-06 05:40:18 +00:00
string(FIND ${VERSION_STRING} "-" STRING_INDEX)
2018-09-16 00:13:29 -05:00
2025-05-06 05:40:18 +00:00
if(${STRING_INDEX} GREATER -1)
math(EXPR STRING_INDEX "${STRING_INDEX} + 1")
string(SUBSTRING ${VERSION_STRING} ${STRING_INDEX} -1 VERSION_BUILD)
endif()
2018-09-16 00:13:29 -05:00
2025-05-06 05:40:18 +00:00
string(REGEX MATCHALL "[0-9]+" VERSIONS ${VERSION_STRING})
list(LENGTH VERSIONS VERSION_COUNT)
2018-09-16 00:13:29 -05:00
2025-05-06 05:40:18 +00:00
if(${VERSION_COUNT} GREATER 0)
list(GET VERSIONS 0 MAJOR)
set(VERSION_MAJOR ${MAJOR} PARENT_SCOPE)
set(TEMP_VERSION_STRING "${MAJOR}")
endif()
2018-09-16 00:13:29 -05:00
2025-05-06 05:40:18 +00:00
if(${VERSION_COUNT} GREATER 1)
list(GET VERSIONS 1 MINOR)
set(VERSION_MINOR ${MINOR} PARENT_SCOPE)
set(TEMP_VERSION_STRING "${TEMP_VERSION_STRING}.${MINOR}")
endif()
2018-09-16 00:13:29 -05:00
2025-05-06 05:40:18 +00:00
if(${VERSION_COUNT} GREATER 2)
list(GET VERSIONS 2 PATCH)
set(VERSION_PATCH ${PATCH} PARENT_SCOPE)
set(TEMP_VERSION_STRING "${TEMP_VERSION_STRING}.${PATCH}")
endif()
2018-09-16 00:13:29 -05:00
2025-05-06 05:40:18 +00:00
set(VERSION_STRING "${TEMP_VERSION_STRING}" PARENT_SCOPE)
2018-09-16 00:13:29 -05:00
2025-05-06 05:40:18 +00:00
endfunction()
2018-09-16 00:13:29 -05:00
## Gets the current version of the repository
## using versioning tags and git describe.
## Passes back a packaging version string
## and a library version string.
function(get_version_from_tag DEFAULT_VERSION_STRING VERSION_PREFIX GIT)
2025-05-06 05:40:18 +00:00
parse_version(${DEFAULT_VERSION_STRING})
if(GIT)
execute_process(
COMMAND git describe --tags --dirty --long --match ${VERSION_PREFIX}-[0-9.]*
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_TAG_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT)
if(${RESULT} EQUAL 0)
parse_version(${GIT_TAG_STRING})
endif()
endif()
set(VERSION_STRING "${VERSION_STRING}" PARENT_SCOPE)
set(VERSION_MAJOR "${VERSION_MAJOR}" PARENT_SCOPE)
set(VERSION_MINOR "${VERSION_MINOR}" PARENT_SCOPE)
set(VERSION_PATCH "${VERSION_PATCH}" PARENT_SCOPE)
2018-09-16 00:13:29 -05:00
endfunction()
function(num_change_since_prev_pkg VERSION_PREFIX)
2025-05-06 05:40:18 +00:00
find_program(get_commits NAMES version_util.sh PATHS ${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules)
if(get_commits)
execute_process(
COMMAND ${get_commits} -c ${VERSION_PREFIX}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE NUM_COMMITS
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT)
set(NUM_COMMITS "${NUM_COMMITS}" PARENT_SCOPE)
if(${RESULT} EQUAL 0)
message("${NUM_COMMITS} were found since previous release")
else()
2025-05-06 05:40:18 +00:00
message("Unable to determine number of commits since previous release")
endif()
else()
message("WARNING: Didn't find version_util.sh")
2025-05-06 05:40:18 +00:00
set(NUM_COMMITS "unknown" PARENT_SCOPE)
endif()
endfunction()
function(get_package_version_number DEFAULT_VERSION_STRING VERSION_PREFIX GIT)
get_version_from_tag(${DEFAULT_VERSION_STRING} ${VERSION_PREFIX} GIT)
num_change_since_prev_pkg(${VERSION_PREFIX})
set(PKG_VERSION_STR "${VERSION_STRING}.${NUM_COMMITS}")
2025-05-06 05:40:18 +00:00
if(DEFINED ENV{ROCM_BUILD_ID})
set(VERSION_ID $ENV{ROCM_BUILD_ID})
else()
2019-11-05 08:45:57 -06:00
set(VERSION_ID "local-build-0")
endif()
2020-07-27 09:32:15 -05:00
set(PKG_VERSION_STR "${PKG_VERSION_STR}-${VERSION_ID}")
2025-05-06 05:40:18 +00:00
if(GIT)
execute_process(
COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT)
if(${RESULT} EQUAL 0)
# Check for dirty workspace.
2025-05-06 05:40:18 +00:00
execute_process(COMMAND git diff --quiet WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE RESULT)
if(${RESULT} EQUAL 1)
set(VERSION_HASH "${VERSION_HASH}-dirty")
endif()
else()
2025-05-06 05:40:18 +00:00
set(VERSION_HASH "unknown")
endif()
else()
2025-05-06 05:40:18 +00:00
set(VERSION_HASH "unknown")
endif()
set(PKG_VERSION_STR "${PKG_VERSION_STR}-${VERSION_HASH}")
set(PKG_VERSION_STR ${PKG_VERSION_STR} PARENT_SCOPE)
set(PKG_VERSION_HASH ${VERSION_HASH} PARENT_SCOPE)
2020-10-12 16:06:27 +05:30
set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR} PARENT_SCOPE)
set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR} PARENT_SCOPE)
set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH} PARENT_SCOPE)
endfunction()