Skip to content

Commit

Permalink
[cmake] Compute the Morse version from git describe
Browse files Browse the repository at this point in the history
The version which appears in morse --version, and in the documentation
now reflects the exact state of your repository.
Thanks to Thomas Moulard for sharing its cmake macro
  • Loading branch information
Arnaud Degroote committed Mar 17, 2012
1 parent fef8b84 commit 4957887
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 5 deletions.
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
cmake_minimum_required(VERSION 2.6)

set(PACKAGE_NAME morse)
set(PACKAGE_VERSION 0.5)
#set(PACKAGE_VERSION ${PROJECT_VERSION})




option(BUILD_CORE_SUPPORT "Build and install morse core" ON)
option(BUILD_DOC_SUPPORT "Build and install morse documentation (Requiere Sphinx)" ON)
Expand Down Expand Up @@ -36,6 +39,14 @@ if(NOT PYTHON_INSTDIR)
endif(NOT PYTHON_INSTDIR)
MESSAGE(STATUS "will install python files in ${PYTHON_INSTDIR}")

# Compute revision
ADD_CUSTOM_TARGET(revisiontag ALL)
ADD_CUSTOM_COMMAND(TARGET revisiontag COMMAND ${CMAKE_COMMAND}
-DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
-P ${CMAKE_CURRENT_SOURCE_DIR}/config/version.cmake)

INSTALL(FILES ${CMAKE_BINARY_DIR}/version.py DESTINATION ${PYTHON_INSTDIR}/morse/)

if (BUILD_YARP2_SUPPORT)
EXECUTE_PROCESS(COMMAND
${PYTHON3_EXECUTABLE} -c "import yarp;"
Expand Down Expand Up @@ -135,6 +146,7 @@ ELSE()
SET (SPHINX_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/doc/html)
SET (SPHINX_LATEX_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/doc/tex)
SET (SPHINX_MAN_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/doc/man)
SET (ENV${PYTHONPATH} ${CMAKE_CURRENT_BINARY_DIR}:${CMAKE_CURRENT_SOURCE_DIR}/src/:ENV${PYTHONPATH})
ADD_CUSTOM_COMMAND(
OUTPUT ${SPHINX_OUTPUT}
COMMAND ${SPHINX_BUILD} -b html
Expand Down
4 changes: 2 additions & 2 deletions bin/morse.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ try:
except ImportError:
log_handler = logging.StreamHandler()

from morse.version import VERSION

logger = logging.getLogger('morse')

formatter = logging.Formatter("* %(message)s\n")
Expand All @@ -29,8 +31,6 @@ except ImportError:
logger.error("You need Python>=3.2 to run morse.")
sys.exit()

VERSION = "@PACKAGE_VERSION@"

#Python version must be egal or bigger than...
MIN_PYTHON_VERSION = "3.2"
#Python version must be smaller than...
Expand Down
117 changes: 117 additions & 0 deletions config/version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# VERSION_COMPUTE
# ---------------
#
# Deduce automatically the version number.
# This mechanism makes sure that version number is always up-to-date and
# coherent (i.e. strictly increasing as commits are made).
#
# There is two cases:
# - the software comes from a release (stable version). In this case, the
# software is retrieved through a tarball which does not contain the `.git'
# directory. Hence, there is no way to search in the Git history to generate
# the version number.
# In this case, a 'version.py' file is put at the top-directory of the source
# tree which contains the project version.
#
# - the softwares comes from git (possibly unstable version).
# 'git describe' is used to retrieve the version number
# (see 'man git-describe'). This tool generates a version number from the git
# history. The version number follows this pattern:
#
# TAG[-N-SHA1][-dirty]
#
# TAG: last matching tag (i.e. last signed tag starting with v, i.e. v0.1)
# N: number of commits since the last maching tag
# SHA1: sha1 of the current commit
# -dirty: added if the workig directory is dirty (there is some uncommitted
# changes).
#
# For stable releases, i.e. the current commit is a matching tag, -N-SHA1 is
# omitted. If the HEAD is on the signed tag v0.1, the version number will be
# 0.1.
#
# If the HEAD is two commits after v0.5 and the last commit is 034f6d...
# The version number will be:
# - 0.5-2-034f if there is no uncommitted changes,
# - 0.5-2-034f-dirty if there is some uncommitted changes.
#
SET(PROJECT_STABLE False)

# Check if a version is embedded in the project.
IF(EXISTS ${SOURCE_DIR}/version.py)
# Yes, use it. This is a stable version.
FILE(COPY ${SOURCE_DIR}/version.py DESTINATION ${CMAKE_BINARY_DIR})
SET(PROJECT_STABLE True)
ELSE(EXISTS ${SOURCE_DIR}/version.py)
# No, there is no 'version.py' file. Deduce the version from git.

# Search for git.
FIND_PROGRAM(GIT git)
IF(NOT GIT)
MESSAGE("Warning: failed to compute the version number, git not found.")
SET(PROJECT_VERSION UNKNOWN)
ENDIF()

# Run describe: search for *signed* tags starting with v, from the HEAD and
# display only the first four characters of the commit id.
EXECUTE_PROCESS(
COMMAND ${GIT} describe --abbrev=4 HEAD
WORKING_DIRECTORY ${SOURCE_DIR}
RESULT_VARIABLE GIT_DESCRIBE_RESULT
OUTPUT_VARIABLE GIT_DESCRIBE_OUTPUT
ERROR_VARIABLE GIT_DESCRIBE_ERROR
OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Run diff-index to check whether the tree is clean or not.
EXECUTE_PROCESS(
COMMAND ${GIT} diff-index --name-only HEAD
WORKING_DIRECTORY ${SOURCE_DIR}
RESULT_VARIABLE GIT_DIFF_INDEX_RESULT
OUTPUT_VARIABLE GIT_DIFF_INDEX_OUTPUT
ERROR_VARIABLE GIT_DIFF_INDEX_ERROR
OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Check if the tree is clean.
IF(NOT GIT_DIFF_INDEX_RESULT AND NOT GIT_DIFF_INDEX_OUTPUT)
SET(PROJECT_DIRTY False)
ELSE()
SET(PROJECT_DIRTY True)
ENDIF()

# Check if git describe worked and store the returned version number.
IF(GIT_DESCRIBE_RESULT)
MESSAGE(
"Warning: failed to compute the version number,"
" 'git describe' failed:\n"
"\t" ${GIT_DESCRIBE_ERROR})
SET(PROJECT_VERSION UNKNOWN)
ELSE()
# Get rid of the tag prefix to generate the final version.
STRING(REGEX REPLACE "^v" "" PROJECT_VERSION "${GIT_DESCRIBE_OUTPUT}")
IF(NOT PROJECT_VERSION)
MESSAGE(
"Warning: failed to compute the version number,"
"'git describe' returned an empty string.")
SET(PROJECT_VERSION UNKNOWN)
ENDIF()

# If there is a dash in the version number, it is an unstable release,
# otherwise it is a stable release.
# I.e. 1.0, 2, 0.1.3 are stable but 0.2.4-1-dg43 is unstable.
STRING(REGEX MATCH "-" PROJECT_STABLE "${PROJECT_VERSION}")
IF(NOT PROJECT_STABLE STREQUAL -)
SET(PROJECT_STABLE True)
ELSE()
SET(PROJECT_STABLE False)
ENDIF()
ENDIF()

# Append dirty if the project is dirty.
IF(PROJECT_DIRTY)
SET(PROJECT_VERSION "${PROJECT_VERSION}-dirty")
ENDIF()
FILE(WRITE version.py "VERSION=\"${PROJECT_VERSION}\"\n")
ENDIF(EXISTS ${SOURCE_DIR}/version.py)

5 changes: 3 additions & 2 deletions doc/conf.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# serve to show the default.

import sys, os
from version import VERSION

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down Expand Up @@ -48,9 +49,9 @@ copyright = u'2009-2010, LAAS-CNRS/ONERA ; 2010-2012, LAAS-CNRS'
# built documents.
#
# The short X.Y version.
version = '@PACKAGE_VERSION@'
version = VERSION
# The full version, including alpha/beta/rc tags.
release = '@PACKAGE_VERSION@'
release = VERSION

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down

0 comments on commit 4957887

Please sign in to comment.