From d591edf5139e0b160bfedc88c1e1632d21fc9f28 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 30 Jul 2014 20:12:04 +0100 Subject: [PATCH] Implemented git versioning This patch automatically versions the shared libraries from any annotated `git` tags: ``` git tag -a v1.0.0 ``` It expects semver version tags such as `v1.0.0`. It would be trivial to support `1.0.0` but looking around it seems that most C/C++ projects follow `vX.X.X` rather that `X.X.X` like a lot of `Node.js` stuff. This determines that the if the project has had a certain amount of commits since the last tag and also if the project is _dirty_ (has modified files), but does __nothing__ with that information. In the future a more robust release could be implemented in the script. This is pretty brittle and has little in the way of configuration. Ideally we should use `find_program` to work out where `git` is so that users can configure it. This implementation assumes that `git` will be available in `PATH` Outputs the following on the command line: ``` -- git Version: v[MAJOR].[MINOR].[PATCH]-[COMMITS_SINCE_TAG]-[SHA1](-dirty)? -- Version: [MAJOR].[MINOR].[PATCH] ``` --- CMakeLists.txt | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b557df1f..017a8cde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,9 +38,38 @@ if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86") add_definitions(-DARCH_X86) endif() +# Read the git tags to determine the project version +execute_process(COMMAND git describe --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8 + RESULT_VARIABLE status + OUTPUT_VARIABLE GIT_VERSION + ERROR_QUIET) +if(${status}) + set(GIT_VERSION "v0.0.0") +else() + string(STRIP ${GIT_VERSION} GIT_VERSION) + string(REGEX REPLACE "-[0-9]+-g" "-" GIT_VERSION ${GIT_VERSION}) +endif() + +# Work out if the repository is dirty +execute_process(COMMAND git update-index -q --refresh + OUTPUT_QUIET + ERROR_QUIET) +execute_process(COMMAND git diff-index --name-only HEAD -- + OUTPUT_VARIABLE GIT_DIFF_INDEX + ERROR_QUIET) +string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY) +if (${GIT_DIRTY}) + string(CONCAT GIT_VERSION ${GIT_VERSION} "-dirty") +endif() + +# Tell the user what versions we are using +message("-- git Version: ${GIT_VERSION}") +string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" VERSION ${GIT_VERSION}) +message("-- Version: ${VERSION}") + # The version of the libraries -set(GENERIC_LIB_VERSION "0.0.0") -set(GENERIC_LIB_SOVERSION "0") +set(GENERIC_LIB_VERSION ${VERSION}) +string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION) # Set up directories include_directories(${PROJECT_SOURCE_DIR}/include)