From aafca3813aa62ed7c83ec9e824a4515d81c0d39d Mon Sep 17 00:00:00 2001 From: Hamish Coleman Date: Mon, 1 Nov 2021 10:39:25 +0000 Subject: [PATCH] Use script to calculate the build version --- CMakeLists.txt | 31 +++++-------------------------- configure.ac | 4 ++-- doc/Scripts.md | 8 ++++++++ scripts/version.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 28 deletions(-) create mode 100755 scripts/version.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 3252dd6..ea6ab36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,39 +6,18 @@ SET(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # N2n release information +set(N2N_OSNAME ${CMAKE_SYSTEM_NAME}) execute_process( - COMMAND cat VERSION + COMMAND scripts/version.sh WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} OUTPUT_VARIABLE PACKAGE_VERSION ) string(STRIP "${PACKAGE_VERSION}" PACKAGE_VERSION) -set(N2N_OSNAME ${CMAKE_SYSTEM_NAME}) -execute_process( - COMMAND git status - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - OUTPUT_VARIABLE GIT_OUTPUT - RESULT_VARIABLE GIT_ERROR_CODE -) -if (GIT_ERROR_CODE EQUAL 0) -execute_process( - COMMAND git rev-list --count HEAD - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - OUTPUT_VARIABLE GIT_REV -) -execute_process( - COMMAND git rev-parse --short HEAD - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - OUTPUT_VARIABLE GIT_ID -) -string(REGEX REPLACE "\n$" "" GIT_REV "${GIT_REV}") -string(REGEX REPLACE "\n$" "" GIT_ID "${GIT_ID}") -set(N2N_VERSION "${N2N_VERSION}.r${GIT_REV}.${GIT_ID}") -MESSAGE(STATUS "Build from git rev: ${N2N_VERSION}") -endif (GIT_ERROR_CODE EQUAL 0) +MESSAGE(STATUS "Build for version: ${PACKAGE_VERSION}") add_definitions(-DCMAKE_BUILD) -add_definitions(-DPACKAGE_OSNAME="${N2N_OSNAME}") -add_definitions(-DPACKAGE_VERSION="${PACKAGE_VERSION}" -DN2N_OSNAME="${N2N_OSNAME}") +add_definitions(-DPACKAGE_OSNAME="${N2N_OSNAME}" -DN2N_OSNAME="${N2N_OSNAME}") +add_definitions(-DPACKAGE_VERSION="${PACKAGE_VERSION}") # Build information diff --git a/configure.ac b/configure.ac index f7ca2f5..2d267ca 100644 --- a/configure.ac +++ b/configure.ac @@ -1,8 +1,8 @@ odnl> Do not add anything above -AC_INIT([edge], m4_esyscmd([cat VERSION | tr -d '\n'])) +AC_INIT([edge], m4_esyscmd([scripts/version.sh | tr -d '\n'])) dnl> Do not add anything above -N2N_VERSION_SHORT=${PACKAGE_VERSION} +N2N_VERSION_SHORT=$(scripts/version.sh short) if test -d ".git"; then # NOTE: keep in sync with the definitions for configure.in files under the packages folder diff --git a/doc/Scripts.md b/doc/Scripts.md index b946ca1..0f2f2cd 100644 --- a/doc/Scripts.md +++ b/doc/Scripts.md @@ -59,3 +59,11 @@ This shell script is used to run automated tests during development. A sample script to route all the host traffic towards a remote gateway, which is reachable via the n2n virtual interface. + +### `version.sh` + +This script is used to determine the current version number during the +build process. + +It looks at both the VERSION file and the GIT tags and outputs the +version number to use. diff --git a/scripts/version.sh b/scripts/version.sh new file mode 100755 index 0000000..5713c0a --- /dev/null +++ b/scripts/version.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# +# Output the current version number +# + +usage() { + echo "Usage: $0 [short]" + echo + echo "Determine the correct version number for the current build" + exit 0 +} + +# TODO: search for the top dir that contains the VERSION file? +VER_FILE_SHORT=$(cat VERSION) + +if git status >/dev/null; then + VER_GIT_SHORT=$(git describe --abbrev=0) + VER_GIT=$(git describe --abbrev=7 --dirty) + + if [ "$VER_FILE_SHORT" != "$VER_GIT_SHORT" ]; then + echo "Error: VERSION file does not match tag version ($VER_FILE_SHORT != $VER_GIT_SHORT)" + exit 1 + fi + + VER_SHORT="$VER_GIT_SHORT" + VER="$VER_GIT" +else + VER_SHORT="$VER_FILE_SHORT" + VER="$VER_FILE_SHORT" +fi + +case "$1" in + short) + echo "$VER_SHORT" + ;; + "") + echo "$VER" + ;; + *) + usage + ;; +esac