From 2f6214fe72387806f29c992c56fa01ee5b98399d Mon Sep 17 00:00:00 2001 From: Dylan Reinhold Date: Wed, 18 Dec 2013 23:07:12 -0800 Subject: [PATCH] Add git tag / SHA1 as a build string Every build from commit 20271c6 (Apr 10 2013) is versioned as 1.6.0. Add a build version after the version number Digital Speech Decoder 1.6.0 (build:v1.6.0-20-g16110bd) The build string uses git describe to list the tag closest to head, and the number of commits past that tag, the a litteral g and ending with the current SHA1 --- CMakeLists.txt | 6 ++ cmake/git_revision.cmake | 130 ++++++++++++++++++++++++++++++++++++ cmake/git_revision.cmake.in | 38 +++++++++++ dsd_main.c | 3 +- git_ver.c.in | 2 + git_ver.h | 1 + 6 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 cmake/git_revision.cmake create mode 100644 cmake/git_revision.cmake.in create mode 100644 git_ver.c.in create mode 100644 git_ver.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a6cc76c..2eb5ead 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,11 +3,17 @@ cmake_minimum_required(VERSION 2.6) SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${dsd_SOURCE_DIR}/cmake/") +include(git_revision) +git_describe(GIT_TAG) + find_package(LibSndFile REQUIRED) find_package(LibMbe REQUIRED) FILE(GLOB SRCS *.c) +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/git_ver.c.in" "${CMAKE_CURRENT_BINARY_DIR}/git_ver.c" @ONLY) +list(APPEND SRCS "${CMAKE_CURRENT_BINARY_DIR}/git_ver.c" git_ver.h) + INCLUDE_DIRECTORIES( "${PROJECT_SOURCE_DIR}" "${LIBSNDFILE_INCLUDE_DIR}" diff --git a/cmake/git_revision.cmake b/cmake/git_revision.cmake new file mode 100644 index 0000000..fcfbdf0 --- /dev/null +++ b/cmake/git_revision.cmake @@ -0,0 +1,130 @@ +# - Returns a version string from Git +# +# These functions force a re-configure on each git commit so that you can +# trust the values of the variables in your build system. +# +# get_git_head_revision( [ ...]) +# +# Returns the refspec and sha hash of the current head revision +# +# git_describe( [ ...]) +# +# Returns the results of git describe on the source tree, and adjusting +# the output so that it tests false if an error occurs. +# +# git_get_exact_tag( [ ...]) +# +# Returns the results of git describe --exact-match on the source tree, +# and adjusting the output so that it tests false if there was no exact +# matching tag. +# +# Requires CMake 2.6 or newer (uses the 'function' command) +# +# Original Author: +# 2009-2010 Ryan Pavlik +# http://academic.cleardefinition.com +# Iowa State University HCI Graduate Program/VRAC +# +# Copyright Iowa State University 2009-2010. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +if(__get_git_revision_description) + return() +endif() +set(__get_git_revision_description YES) + +# We must run the following at "include" time, not at function call time, +# to find the path to this module rather than the path to a calling list file +get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH) + +function(get_git_head_revision _refspecvar _hashvar) + set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}") + set(GIT_DIR "${GIT_PARENT_DIR}/.git") + while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories + set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}") + get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH) + if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT) + # We have reached the root directory, we are not in git + set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE) + set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE) + return() + endif() + set(GIT_DIR "${GIT_PARENT_DIR}/.git") + endwhile() + # check if this is a submodule + if(NOT IS_DIRECTORY ${GIT_DIR}) + file(READ ${GIT_DIR} submodule) + string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule}) + get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH) + get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE) + endif() + set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data") + if(NOT EXISTS "${GIT_DATA}") + file(MAKE_DIRECTORY "${GIT_DATA}") + endif() + + if(NOT EXISTS "${GIT_DIR}/HEAD") + return() + endif() + set(HEAD_FILE "${GIT_DATA}/HEAD") + configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY) + + configure_file("cmake/git_revision.cmake.in" + "${GIT_DATA}/grabRef.cmake" + @ONLY) + include("${GIT_DATA}/grabRef.cmake") + + set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE) + set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE) +endfunction() + +function(git_describe _var) + if(NOT GIT_FOUND) + find_package(Git QUIET) + endif() + get_git_head_revision(refspec hash) + if(NOT GIT_FOUND) + set(${_var} "GIT-NOTFOUND" PARENT_SCOPE) + return() + endif() + if(NOT hash) + set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE) + return() + endif() + + # TODO sanitize + #if((${ARGN}" MATCHES "&&") OR + # (ARGN MATCHES "||") OR + # (ARGN MATCHES "\\;")) + # message("Please report the following error to the project!") + # message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}") + #endif() + + #message(STATUS "Arguments to execute_process: ${ARGN}") + + execute_process(COMMAND + "${GIT_EXECUTABLE}" + describe + ${hash} + ${ARGN} + WORKING_DIRECTORY + "${CMAKE_SOURCE_DIR}" + RESULT_VARIABLE + res + OUTPUT_VARIABLE + out + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + if(NOT res EQUAL 0) + set(out "${out}-${res}-NOTFOUND") + endif() + + set(${_var} "${out}" PARENT_SCOPE) +endfunction() + +function(git_get_exact_tag _var) + git_describe(out --exact-match ${ARGN}) + set(${_var} "${out}" PARENT_SCOPE) +endfunction() diff --git a/cmake/git_revision.cmake.in b/cmake/git_revision.cmake.in new file mode 100644 index 0000000..888ce13 --- /dev/null +++ b/cmake/git_revision.cmake.in @@ -0,0 +1,38 @@ +# +# Internal file for GetGitRevisionDescription.cmake +# +# Requires CMake 2.6 or newer (uses the 'function' command) +# +# Original Author: +# 2009-2010 Ryan Pavlik +# http://academic.cleardefinition.com +# Iowa State University HCI Graduate Program/VRAC +# +# Copyright Iowa State University 2009-2010. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +set(HEAD_HASH) + +file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024) + +string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS) +if(HEAD_CONTENTS MATCHES "ref") + # named branch + string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}") + if(EXISTS "@GIT_DIR@/${HEAD_REF}") + configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY) + elseif(EXISTS "@GIT_DIR@/logs/${HEAD_REF}") + configure_file("@GIT_DIR@/logs/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY) + set(HEAD_HASH "${HEAD_REF}") + endif() +else() + # detached HEAD + configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY) +endif() + +if(NOT HEAD_HASH) + file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024) + string(STRIP "${HEAD_HASH}" HEAD_HASH) +endif() diff --git a/dsd_main.c b/dsd_main.c index 47fc4ec..8ce83d8 100644 --- a/dsd_main.c +++ b/dsd_main.c @@ -24,6 +24,7 @@ #include "nxdn_const.h" #include "dmr_const.h" #include "provoice_const.h" +#include "git_ver.h" int comp (const void *a, const void *b) @@ -340,7 +341,7 @@ main (int argc, char **argv) char versionstr[25]; mbe_printVersion (versionstr); - printf ("Digital Speech Decoder 1.7.0-dev\n"); + printf ("Digital Speech Decoder 1.7.0-dev (build:%s)\n", GIT_TAG); printf ("mbelib version %s\n", versionstr); initOpts (&opts); diff --git a/git_ver.c.in b/git_ver.c.in new file mode 100644 index 0000000..36e042b --- /dev/null +++ b/git_ver.c.in @@ -0,0 +1,2 @@ +#define _GIT_TAG "@GIT_TAG@" +const char GIT_TAG[] = _GIT_TAG; diff --git a/git_ver.h b/git_ver.h new file mode 100644 index 0000000..be9b355 --- /dev/null +++ b/git_ver.h @@ -0,0 +1 @@ +extern const char GIT_TAG[];