Warning, /graphics/kphotoalbum/cmake/modules/UpdateVersion.cmake is written in an unsupported language. File is not indexed.

0001 # SPDX-FileCopyrightText: 2012-2021 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0002 # SPDX-FileCopyrightText: 2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 #
0004 # SPDX-License-Identifier: BSD-3-Clause
0005 
0006 # query_git_version uses git-describe to extract version information from git and sets the variable named ${OUTPUT_VARIABLE} accordingly.
0007 # If BASE_DIR is not a git directory, OUTPUT_VARIABLE is not set.
0008 function(query_git_version BASE_DIR OUTPUT_VARIABLE)
0009     if(EXISTS "${BASE_DIR}/.git")
0010         include("${CMAKE_CURRENT_LIST_DIR}/GitDescription.cmake")
0011         # shallow cloning in gitlab CI requires --always parameter to not fail due to missing tag information:
0012         git_get_description(GIT_VERSION GIT_ARGS --dirty --always)
0013         if(GIT_VERSION)
0014             set(${OUTPUT_VARIABLE} "${GIT_VERSION}" PARENT_SCOPE)
0015         endif()
0016     endif()
0017 endfunction()
0018 
0019 
0020 
0021 # set_version_variable sets OUTPUT_VARIABLE to GIT_VERSION if that is usable.
0022 # GIT_VERSION is considered usable if starts with the scheme "v<MAJOR>.<MINOR>".
0023 # If GIT_VERSION starts with a "g", it is considered a commit hash and appended to the FALLBACK_VERSION.
0024 # GIT_VERSION examples:
0025 #  "v5.8" -> tagged commit
0026 #  "v5.8.1" -> tagged commit
0027 #  "v5.8.1-160-g4fb259da" -> commit 160 after the last tagged version
0028 #  "g4fb259da" -> no last recent tag available(usually caused by shallow cloning in Gitlab CI)
0029 #  "v5.8.1-160-g4fb259da-dirty" -> commit with local changes
0030 #  "v5.8.1-dirty" tagged commit with local changes
0031 # PROJECT_VERSION example: "5.8.1"
0032 function(set_version_variable OUTPUT_VARIABLE GIT_VERSION FALLBACK_VERSION)
0033     if("${GIT_VERSION}" STREQUAL "")
0034         set("${OUTPUT_VARIABLE}" "${FALLBACK_VERSION}" PARENT_SCOPE)
0035     elseif("${GIT_VERSION}" MATCHES "^v([0-9]+[.][0-9]+([.][0-9]+)?)")
0036         if(NOT "${FALLBACK_VERSION}" STREQUAL "")
0037             # if both are set, check if project version and git version match
0038             if(NOT "${CMAKE_MATCH_1}" VERSION_EQUAL "${FALLBACK_VERSION}")
0039                 message(AUTHOR_WARNING "Git version does not match PROJECT_VERSION!(${GIT_VERSION} vs ${FALLBACK_VERSION})")
0040             endif()
0041         endif()
0042         set("${OUTPUT_VARIABLE}" "${GIT_VERSION}" PARENT_SCOPE)
0043     elseif("${GIT_VERSION}" MATCHES "^g")
0044         message(STATUS "Git version only contains the hash and no tag info. Falling back to project version...")
0045         set("${OUTPUT_VARIABLE}" "v${FALLBACK_VERSION}-${GIT_VERSION}" PARENT_SCOPE)
0046     else()
0047         message(AUTHOR_WARNING "Unexpected input in git version! Please file a bug!(${GIT_VERSION})")
0048     endif()
0049 endfunction()
0050 
0051 # for unit tests, run cmake -D UPDATEVERSION_RUN_UNITTESTS=true -P UpdateVersion.cmake
0052 if(UPDATEVERSION_RUN_UNITTESTS)
0053     function(expect value expected_value)
0054         if(NOT "${value}" STREQUAL "${expected_value}")
0055             message(ERROR " Expected value: ${expected_value}, actual value: ${value}")
0056         endif()
0057     endfunction()
0058     set_version_variable(output "v5.8.0" "5.8")
0059     expect("${output}" "v5.8.0")
0060     set_version_variable(output "v5.8" "5.8")
0061     expect("${output}" "v5.8")
0062     set_version_variable(output "v5.8" "5.8.0")
0063     expect("${output}" "v5.8")
0064     set_version_variable(output "v5.8.1" "5.8.1")
0065     expect("${output}" "v5.8.1")
0066     set_version_variable(output "v5.8.1-160-g4fb259da" "5.8.1")
0067     expect("${output}" "v5.8.1-160-g4fb259da")
0068     set_version_variable(output "g4fb259da" "5.8")
0069     expect("${output}" "v5.8-g4fb259da")
0070     set_version_variable(output "v5.8.1-dirty" "5.8.1")
0071     expect("${output}" "v5.8.1-dirty")
0072     set_version_variable(output "v5.8.1-160-g4fb259da-dirty" "5.8.1")
0073     expect("${output}" "v5.8.1-160-g4fb259da-dirty")
0074     return()
0075 endif()
0076 
0077 
0078 
0079 if(NOT DEFINED BASE_DIR)
0080     message(FATAL_ERROR "UpdateVersion.cmake: BASE_DIR not set. Please supply base working directory!")
0081 endif()
0082 
0083 # Step 1: query git if available:
0084 query_git_version("${BASE_DIR}" GIT_VERSION)
0085 set_version_variable(PROJECT_VERSION "${GIT_VERSION}" "${PROJECT_VERSION}")
0086 
0087 # Step 2: configure version.h
0088 if(PROJECT_VERSION)
0089     # make sure we have the right variable set:
0090     set("${PROJECT_NAME}_VERSION" "${PROJECT_VERSION}")
0091 
0092     message(STATUS "Setting version information to ${PROJECT_VERSION}...")
0093     if(NOT DEFINED OUTPUT_DIR)
0094         set(OUTPUT_DIR "${BASE_DIR}")
0095     endif()
0096     # write version info to a temporary file
0097     configure_file("${OUTPUT_DIR}/version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/version.h~")
0098     # update info iff changed
0099     execute_process(COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${CMAKE_CURRENT_BINARY_DIR}/version.h~" "${OUTPUT_DIR}/version.h")
0100     # make sure info doesn't get stale
0101     file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/version.h~")
0102 else()
0103     # if we got no version, but we have a version.h, don't complain:
0104     if(NOT EXISTS "${OUTPUT_DIR}/version.h")
0105         message(SEND_ERROR "The generated file 'version.h' does not exist!")
0106         message(AUTHOR_WARNING "When creating a release tarball, please make sure to run cmake -P ${CMAKE_CURRENT_LIST_FILE}")
0107     endif()
0108 endif()
0109 # vi:expandtab:tabstop=4 shiftwidth=4: