Skip to content

Commit 06f3256

Browse files
committed
Add automatic version detection through cmake
1 parent c4af497 commit 06f3256

File tree

3 files changed

+108
-38
lines changed

3 files changed

+108
-38
lines changed

CMakeLists.txt

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
#---------------------------------------------------------------------------#
77

88
cmake_minimum_required(VERSION 3.8)
9-
project(Flibcpp VERSION 0.3.0 LANGUAGES CXX Fortran)
9+
include("${CMAKE_CURRENT_LIST_DIR}/cmake/FlibcppVersion.cmake")
10+
flibcpp_find_version(Flibcpp "${CMAKE_CURRENT_LIST_DIR}/cmake/git-version.txt")
11+
project(Flibcpp VERSION "${Flibcpp_VERSION}" LANGUAGES CXX Fortran)
1012

1113
#---------------------------------------------------------------------------#
1214
# OPTIONS
@@ -85,41 +87,6 @@ endif()
8587
# Enable testing based on BUILD_TESTING flag
8688
include(CTest)
8789

88-
#---------------------------------------------------------------------------#
89-
# VERSIONING
90-
#---------------------------------------------------------------------------#
91-
92-
# Get a possible Git version generated using git-archive (see the .gitattributes
93-
# file)
94-
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/cmake/git-version.txt"
95-
FLIBCPP_VERSION_STRING)
96-
97-
if (NOT FLIBCPP_VERSION_STRING MATCHES "\\$Format:")
98-
# First line are decorators, second is hash
99-
list(GET FLIBCPP_VERSION_STRING 0 _tag)
100-
string(REGEX REPLACE "tag: *" "" _tag "${_tag}")
101-
list(GET FLIBCPP_VERSION_STRING 1 _hash)
102-
string(REGEX REPLACE " +" "" _hash "${_hash}")
103-
set(FLIBCPP_VERSION_STRING "${_tag}-g${_hash}")
104-
else()
105-
find_package(Git)
106-
if (Git_FOUND)
107-
execute_process(
108-
COMMAND "${GIT_EXECUTABLE}" "describe"
109-
ERROR_QUIET
110-
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
111-
OUTPUT_VARIABLE FLIBCPP_VERSION_STRING
112-
OUTPUT_STRIP_TRAILING_WHITESPACE
113-
)
114-
else()
115-
set(FLIBCPP_VERSION_STRING "${Flibcpp_VERSION}")
116-
endif()
117-
endif()
118-
119-
set(FLIBCPP_VERSION_CPP "${CMAKE_CURRENT_BINARY_DIR}/flibcpp_version.cpp")
120-
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/flibcpp_version.cpp.in"
121-
"${FLIBCPP_VERSION_CPP}" @ONLY)
122-
12390
#---------------------------------------------------------------------------#
12491
# LIBRARY
12592
#---------------------------------------------------------------------------#
@@ -212,7 +179,10 @@ function(flibcpp_add_module name)
212179
)
213180
endfunction()
214181

215-
# Install primary flc module, compiling version info as well
182+
# Configure version information and generate primary flibcpp module
183+
set(FLIBCPP_VERSION_CPP "${CMAKE_CURRENT_BINARY_DIR}/flibcpp_version.cpp")
184+
configure_file("${CMAKE_CURRENT_LIST_DIR}/cmake/flibcpp_version.cpp.in"
185+
"${FLIBCPP_VERSION_CPP}" @ONLY)
216186
flibcpp_add_module(flc "${FLIBCPP_VERSION_CPP}")
217187

218188
# Also install 'import_flc' if using SWIG

cmake/FlibcppVersion.cmake

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
##---------------------------------------------------------------------------##
2+
## File : flibcpp/cmake/FlibcppVersion.cmake
3+
#[=======================================================================[.rst:
4+
5+
FlibcppVersion
6+
--------------
7+
8+
.. command:: flibcpp_find_version
9+
10+
Get the project version using Git descriptions to ensure the version numbers
11+
are always synchronized between Git and CMake::
12+
13+
flibcpp_find_version(<projname> <git-version-file>)
14+
15+
16+
``<projname>``
17+
Name of the project.
18+
19+
This command sets the following variables in the parent package::
20+
21+
${PROJNAME}_VERSION
22+
${PROJNAME}_VERSION_STRING
23+
24+
#]=======================================================================]
25+
26+
function(flibcpp_find_version PROJNAME GIT_VERSION_FILE)
27+
# Get a possible Git version generated using git-archive (see the
28+
# .gitattributes file)
29+
file(STRINGS "${GIT_VERSION_FILE}" _TEXTFILE)
30+
31+
if (_TEXTFILE MATCHES "\\$Format:")
32+
# Not a "git archive": use live git information
33+
set(_CACHE_VAR "${PROJNAME}_GIT_DESCRIBE")
34+
set(_VERSION_STRING "${${_CACHE_VAR}}")
35+
if (NOT _VERSION_STRING)
36+
# Building from a git checkout rather than a distribution
37+
find_package(Git REQUIRED)
38+
execute_process(
39+
COMMAND "${GIT_EXECUTABLE}" "describe" "--tags" "--match" "v*"
40+
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
41+
ERROR_VARIABLE _GIT_ERR
42+
OUTPUT_VARIABLE _VERSION_STRING
43+
RESULT_VARIABLE _GIT_RESULT
44+
OUTPUT_STRIP_TRAILING_WHITESPACE
45+
)
46+
if (NOT _GIT_RESULT EQUAL "0")
47+
message(FATAL_ERROR "Failed to get ${PROJNAME} version from git: "
48+
"${_GIT_ERR}")
49+
endif()
50+
if (NOT _VERSION_STRING)
51+
message(FATAL_ERROR "Failed to get ${PROJNAME} version from git: "
52+
"git describe returned an empty string")
53+
endif()
54+
set("${_CACHE_VAR}" "${_VERSION_STRING}" CACHE INTERNAL
55+
"Git description for ${PROJNAME}")
56+
endif()
57+
# Process description tag: e.g. v0.4.0-2-gc4af497 or v0.4.0
58+
string(REGEX MATCH "v([0-9.]+)(-[0-9]+-g([0-9a-f]+))?" _MATCH
59+
"${_VERSION_STRING}"
60+
)
61+
if (_MATCH)
62+
set(_VERSION_STRING "${CMAKE_MATCH_1}")
63+
if (CMAKE_MATCH_2)
64+
# *not* a tagged release
65+
set(_VERSION_HASH "${CMAKE_MATCH_3}")
66+
endif()
67+
endif()
68+
else()
69+
# First line are decorators, second is hash.
70+
list(GET _TEXTFILE 0 _TAG)
71+
string(REGEX MATCH "tag: *v([0-9.]+)" _MATCH "${_TAG}")
72+
if (_MATCH)
73+
set(_VERSION_STRING "${CMAKE_MATCH_1}")
74+
else()
75+
# *not* a tagged release
76+
list(GET _TEXTFILE 1 _HASH)
77+
string(REGEX MATCH " *([0-9a-f]+)" _MATCH "${_HASH}")
78+
if (_MATCH)
79+
set(_VERSION_HASH "${CMAKE_MATCH_1}")
80+
endif()
81+
endif()
82+
endif()
83+
84+
if (NOT _VERSION_STRING)
85+
message(FATAL_ERROR "Could not determine version for ${PROJNAME}")
86+
endif()
87+
88+
if (_VERSION_HASH)
89+
set(_FULL_VERSION_STRING "v${_VERSION_STRING}+${_VERSION_HASH}")
90+
else()
91+
set(_FULL_VERSION_STRING "v${_VERSION_STRING}")
92+
endif()
93+
94+
set(${PROJNAME}_VERSION "${_VERSION_STRING}" PARENT_SCOPE)
95+
set(${PROJNAME}_VERSION_STRING "${_FULL_VERSION_STRING}" PARENT_SCOPE)
96+
endfunction()
97+
98+
##---------------------------------------------------------------------------##
99+
## end of flibcpp/cmake/FlibcppVersion.cmake
100+
##---------------------------------------------------------------------------##

cmake/flibcpp_version.cpp.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extern "C" const char flibcpp_version[] = "@FLIBCPP_VERSION_STRING@";
1+
extern "C" const char flibcpp_version[] = "@Flibcpp_VERSION_STRING@";
22
extern "C" const int flibcpp_version_major = @PROJECT_VERSION_MAJOR@;
33
extern "C" const int flibcpp_version_minor = @PROJECT_VERSION_MINOR@;
44
extern "C" const int flibcpp_version_patch = @PROJECT_VERSION_PATCH@;

0 commit comments

Comments
 (0)