From f55bf48baddeb99aff395e9fd94b7ac82ba8388d Mon Sep 17 00:00:00 2001 From: xiphon Date: Wed, 1 Jul 2020 01:29:09 +0000 Subject: [PATCH] cmake: fix git tag + commit detection and version.js generation --- ...ersionGui.cmake => GitGetVersionTag.cmake} | 67 ++++++++++--------- cmake/VersionGui.cmake | 23 +++---- src/version.js.in | 5 +- 3 files changed, 47 insertions(+), 48 deletions(-) rename cmake/{GenVersionGui.cmake => GitGetVersionTag.cmake} (52%) diff --git a/cmake/GenVersionGui.cmake b/cmake/GitGetVersionTag.cmake similarity index 52% rename from cmake/GenVersionGui.cmake rename to cmake/GitGetVersionTag.cmake index 0ec2e8ba..b66b1886 100644 --- a/cmake/GenVersionGui.cmake +++ b/cmake/GitGetVersionTag.cmake @@ -28,37 +28,40 @@ # # Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers -# Check what commit we're on -execute_process(COMMAND "${GIT}" rev-parse --short=9 HEAD RESULT_VARIABLE RET OUTPUT_VARIABLE COMMIT OUTPUT_STRIP_TRAILING_WHITESPACE) +function (git_get_version_tag git directory result_var) + execute_process(COMMAND "${git}" rev-parse --short HEAD + WORKING_DIRECTORY ${directory} + OUTPUT_VARIABLE COMMIT + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(NOT COMMIT) + message(WARNING "${directory}: cannot determine current commit. Make sure that you are building from a Git working tree") + set(${result_var} "unknown" PARENT_SCOPE) + return() + endif() -if(RET) - # Something went wrong, set the version tag to -unknown - - message(WARNING "Cannot determine current commit. Make sure that you are building either from a Git working tree or from a source archive.") - set(VERSIONTAG "unknown") - configure_file("src/version.js.in" "${TO}") -else() - string(SUBSTRING ${COMMIT} 0 9 COMMIT) - message(STATUS "You are currently on commit ${COMMIT}") - - # Get all the tags - execute_process(COMMAND "${GIT}" rev-list --tags --max-count=1 --abbrev-commit RESULT_VARIABLE RET OUTPUT_VARIABLE TAGGEDCOMMIT OUTPUT_STRIP_TRAILING_WHITESPACE) - - if(NOT TAGGEDCOMMIT) - message(WARNING "Cannot determine most recent tag. Make sure that you are building either from a Git working tree or from a source archive.") - set(VERSIONTAG "${COMMIT}") - else() - message(STATUS "The most recent tag was at ${TAGGEDCOMMIT}") - - # Check if we're building that tagged commit or a different one - if(COMMIT STREQUAL TAGGEDCOMMIT) - message(STATUS "You are building a tagged release") - set(VERSIONTAG "release") - else() - message(STATUS "You are ahead of or behind a tagged release") - set(VERSIONTAG "${COMMIT}") - endif() - endif() + execute_process(COMMAND "${git}" describe --tags --exact-match + WORKING_DIRECTORY ${directory} + OUTPUT_VARIABLE TAG + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(TAG) + message(STATUS "${directory}: building tagged release ${TAG}-${COMMIT}") + set(${result_var} "${TAG}-${COMMIT}" PARENT_SCOPE) + return() + endif() - configure_file("src/version.js.in" "${TO}") -endif() + execute_process(COMMAND "${git}" describe --tags --long + WORKING_DIRECTORY ${directory} + OUTPUT_VARIABLE MOST_RECENT_TAG + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(MOST_RECENT_TAG) + message(STATUS "${directory}: ahead of or behind a tagged release, building ${MOST_RECENT_TAG}") + set(${result_var} "${MOST_RECENT_TAG}" PARENT_SCOPE) + return() + endif() + + message(STATUS "${directory}: building ${COMMIT} commit") + set(${result_var} "${COMMIT}" PARENT_SCOPE) +endfunction() diff --git a/cmake/VersionGui.cmake b/cmake/VersionGui.cmake index b248cba0..fd0eddae 100644 --- a/cmake/VersionGui.cmake +++ b/cmake/VersionGui.cmake @@ -26,27 +26,24 @@ # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -function (write_static_version_header hash) - set(VERSIONTAG "${hash}") - configure_file("${CMAKE_SOURCE_DIR}/version.js.in" "${CMAKE_SOURCE_DIR}/version.js") +function (write_static_version_header VERSION_TAG_GUI VERSION_TAG_CORE) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/version.js.in" "${CMAKE_CURRENT_SOURCE_DIR}/version.js") endfunction () find_package(Git QUIET) if ("$Format:$" STREQUAL "") # We're in a tarball; use hard-coded variables. - write_static_version_header("release") + write_static_version_header("release" "release") elseif (GIT_FOUND OR Git_FOUND) message(STATUS "Found Git: ${GIT_EXECUTABLE}") - add_custom_command( - OUTPUT "${CMAKE_SOURCE_DIR}/version.js" - COMMAND "${CMAKE_COMMAND}" - "-D" "GIT=${GIT_EXECUTABLE}" - "-D" "TO=${CMAKE_SOURCE_DIR}/version.js" - "-P" "cmake/GenVersionGui.cmake" - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + + include(GitGetVersionTag) + git_get_version_tag(${GIT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR} VERSION_TAG_GUI) + git_get_version_tag(${GIT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/monero VERSION_TAG_CORE) + write_static_version_header(${VERSION_TAG_GUI} ${VERSION_TAG_CORE}) else() message(STATUS "WARNING: Git was not found!") - write_static_version_header("unknown") + write_static_version_header("unknown" "unknown") endif () add_custom_target(genversiongui ALL - DEPENDS "${CMAKE_SOURCE_DIR}/version.js") + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/version.js") diff --git a/src/version.js.in b/src/version.js.in index cbce82db..66f92852 100644 --- a/src/version.js.in +++ b/src/version.js.in @@ -1,3 +1,2 @@ -var GUI_VERSION = "@VERSIONTAG@" -var GUI_MONERO_VERSION = "@VERSIONTAG@" - +var GUI_VERSION = "@VERSION_TAG_GUI@"; +var GUI_MONERO_VERSION = "@VERSION_TAG_CORE@";