aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Clarkson <mattyclarkson@gmail.com>2014-08-01 15:00:43 +0100
committerMatt Clarkson <mattyclarkson@gmail.com>2014-08-01 15:02:32 +0100
commitc927845d5ad5344cb681598001d2234cefa61145 (patch)
treeb08f7112ae4f5ef60c74c3d4d71248f3f3a8c66a
parentd591edf5139e0b160bfedc88c1e1632d21fc9f28 (diff)
downloadgoogle-benchmark-c927845d5ad5344cb681598001d2234cefa61145.tar.gz
get_git_version CMake function
-rw-r--r--.gitignore1
-rw-r--r--CMakeLists.txt29
-rw-r--r--cmake/GetGitVersion.cmake45
3 files changed, 52 insertions, 23 deletions
diff --git a/.gitignore b/.gitignore
index d85b3eb..4371bf7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,6 +3,7 @@
*.so.?*
*.dylib
*.cmake
+!/cmake/*.cmake
*~
/test/benchmark_test
/test/re_test
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 017a8cd..d6b4fb6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,10 @@
cmake_minimum_required (VERSION 2.8)
project (benchmark)
+# Make sure we can import out CMake functions
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
+
+# We need threads in this project
find_package(Threads REQUIRED)
# Import and build Google Test
@@ -39,31 +43,10 @@ if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "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()
+include(GetGitVersion)
+get_git_version(GIT_VERSION)
# 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}")
diff --git a/cmake/GetGitVersion.cmake b/cmake/GetGitVersion.cmake
new file mode 100644
index 0000000..423cf03
--- /dev/null
+++ b/cmake/GetGitVersion.cmake
@@ -0,0 +1,45 @@
+# - Returns a version string from Git tags
+#
+# This function inspects the annotated git tags for the project and returns a string
+# into a CMake variable
+#
+# get_git_version(<var>)
+#
+# - Example
+#
+# include(GetGitVersion)
+# get_git_version(GIT_VERSION)
+#
+# Requires CMake 2.6+
+
+if(__get_git_version)
+ return()
+endif()
+set(__get_git_version INCLUDED)
+
+function(get_git_version var)
+ 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()
+ message("-- git Version: ${GIT_VERSION}")
+ set(${var} ${GIT_VERSION} PARENT_SCOPE)
+endfunction()