Skip to content

Commit 75490a2

Browse files
authored
Merge pull request #10 from swig-fortran/doc
Tweak documentation and fix CMake project version
2 parents 32712ab + 55426c5 commit 75490a2

File tree

6 files changed

+167
-51
lines changed

6 files changed

+167
-51
lines changed

CMakeLists.txt

+11-37
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
#---------------------------------------------------------------------------#
77

88
cmake_minimum_required(VERSION 3.8)
9-
project(Flibcpp VERSION 0.3.0 LANGUAGES CXX Fortran)
9+
10+
# Determine version number from git metadata
11+
include("${CMAKE_CURRENT_LIST_DIR}/cmake/FlibcppVersion.cmake")
12+
flibcpp_find_version(Flibcpp "${CMAKE_CURRENT_LIST_DIR}/cmake/git-version.txt")
13+
message(STATUS "Flibcpp version: ${Flibcpp_VERSION}")
14+
15+
project(Flibcpp VERSION "${Flibcpp_VERSION}" LANGUAGES CXX Fortran)
1016

1117
#---------------------------------------------------------------------------#
1218
# OPTIONS
@@ -85,41 +91,6 @@ endif()
8591
# Enable testing based on BUILD_TESTING flag
8692
include(CTest)
8793

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-
12394
#---------------------------------------------------------------------------#
12495
# LIBRARY
12596
#---------------------------------------------------------------------------#
@@ -212,7 +183,10 @@ function(flibcpp_add_module name)
212183
)
213184
endfunction()
214185

215-
# Install primary flc module, compiling version info as well
186+
# Configure version information and generate primary flibcpp module
187+
set(FLIBCPP_VERSION_CPP "${CMAKE_CURRENT_BINARY_DIR}/flibcpp_version.cpp")
188+
configure_file("${CMAKE_CURRENT_LIST_DIR}/cmake/flibcpp_version.cpp.in"
189+
"${FLIBCPP_VERSION_CPP}" @ONLY)
216190
flibcpp_add_module(flc "${FLIBCPP_VERSION_CPP}")
217191

218192
# Also install 'import_flc' if using SWIG

cmake/FlibcppVersion.cmake

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
The project version string uses PEP-440 semantic versioning, and will look
25+
like v0.1.2 if the version is actually a tagged release, or v0.1.3+abcdef if
26+
it's not.
27+
28+
If a non-tagged version is exported, or an untagged shallow git clone is used,
29+
it's impossible to determine the version from the tag, so a warning will be
30+
issued and the version will be set to 0.0.0.
31+
32+
#]=======================================================================]
33+
34+
if (CMAKE_SCRIPT_MODE_FILE)
35+
cmake_minimum_required(VERSION 3.8)
36+
endif()
37+
38+
function(flibcpp_find_version PROJNAME GIT_VERSION_FILE)
39+
# Get a possible Git version generated using git-archive (see the
40+
# .gitattributes file)
41+
file(STRINGS "${GIT_VERSION_FILE}" _TEXTFILE)
42+
43+
if (_TEXTFILE MATCHES "\\$Format:")
44+
# Not a "git archive": use live git information
45+
set(_CACHE_VAR "${PROJNAME}_GIT_DESCRIBE")
46+
set(_CACHED_VERSION "${${_CACHE_VAR}}")
47+
if (NOT _CACHED_VERSION)
48+
# Building from a git checkout rather than a distribution
49+
find_package(Git QUIET REQUIRED)
50+
execute_process(
51+
COMMAND "${GIT_EXECUTABLE}" "describe" "--tags" "--match" "v*"
52+
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
53+
ERROR_VARIABLE _GIT_ERR
54+
OUTPUT_VARIABLE _VERSION_STRING
55+
RESULT_VARIABLE _GIT_RESULT
56+
OUTPUT_STRIP_TRAILING_WHITESPACE
57+
)
58+
if (NOT _GIT_RESULT EQUAL "0")
59+
message(WARNING "Failed to get ${PROJNAME} version from git: "
60+
"${_GIT_ERR}")
61+
elseif (NOT _VERSION_STRING)
62+
message(WARNING "Failed to get ${PROJNAME} version from git: "
63+
"git describe returned an empty string")
64+
else()
65+
# Process description tag: e.g. v0.4.0-2-gc4af497 or v0.4.0
66+
string(REGEX MATCH "v([0-9.]+)(-[0-9]+-g([0-9a-f]+))?" _MATCH
67+
"${_VERSION_STRING}"
68+
)
69+
if (_MATCH)
70+
set(_VERSION_STRING "${CMAKE_MATCH_1}")
71+
if (CMAKE_MATCH_2)
72+
# *not* a tagged release
73+
set(_VERSION_HASH "${CMAKE_MATCH_3}")
74+
endif()
75+
endif()
76+
endif()
77+
if (NOT _VERSION_STRING)
78+
execute_process(
79+
COMMAND "${GIT_EXECUTABLE}" "log" "-1" "--format=%h" "HEAD"
80+
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
81+
OUTPUT_VARIABLE _VERSION_HASH
82+
OUTPUT_STRIP_TRAILING_WHITESPACE
83+
)
84+
endif()
85+
set(_CACHED_VERSION "${_VERSION_STRING}" "${_VERSION_HASH}")
86+
set("${_CACHE_VAR}" "${_CACHED_VERSION}" CACHE INTERNAL
87+
"Version string and hash for ${PROJNAME}")
88+
endif()
89+
list(GET _CACHED_VERSION 0 _VERSION_STRING)
90+
list(GET _CACHED_VERSION 1 _VERSION_HASH)
91+
else()
92+
# First line are decorators, second is hash.
93+
list(GET _TEXTFILE 0 _TAG)
94+
string(REGEX MATCH "tag: *v([0-9.]+)" _MATCH "${_TAG}")
95+
if (_MATCH)
96+
set(_VERSION_STRING "${CMAKE_MATCH_1}")
97+
else()
98+
# *not* a tagged release
99+
list(GET _TEXTFILE 1 _HASH)
100+
string(REGEX MATCH " *([0-9a-f]+)" _MATCH "${_HASH}")
101+
if (_MATCH)
102+
set(_VERSION_HASH "${CMAKE_MATCH_1}")
103+
endif()
104+
endif()
105+
endif()
106+
107+
if (NOT _VERSION_STRING)
108+
message(WARNING "Could not determine version number for ${PROJNAME}: "
109+
"perhaps a non-release archive?")
110+
set(_VERSION_STRING "0.0.0")
111+
endif()
112+
113+
if (_VERSION_HASH)
114+
set(_FULL_VERSION_STRING "v${_VERSION_STRING}+${_VERSION_HASH}")
115+
else()
116+
set(_FULL_VERSION_STRING "v${_VERSION_STRING}")
117+
endif()
118+
119+
set(${PROJNAME}_VERSION "${_VERSION_STRING}" PARENT_SCOPE)
120+
set(${PROJNAME}_VERSION_STRING "${_FULL_VERSION_STRING}" PARENT_SCOPE)
121+
endfunction()
122+
123+
if (CMAKE_SCRIPT_MODE_FILE)
124+
# This script is being run from the command line. Useful for debugging.
125+
if (NOT DEFINED GIT_VERSION_FILE)
126+
message(FATAL_ERROR "Run this script with "
127+
"cmake -D GIT_VERSION_FILE=git-version.txt -P FlibcppVersion.cmake")
128+
endif()
129+
flibcpp_find_version(local ${GIT_VERSION_FILE})
130+
message(STATUS "${LOCAL_VERSION}")
131+
message(STATUS "${LOCAL_VERSION_STRING}")
132+
endif()
133+
134+
##---------------------------------------------------------------------------##
135+
## end of flibcpp/cmake/FlibcppVersion.cmake
136+
##---------------------------------------------------------------------------##

cmake/flibcpp_version.cpp.in

+1-1
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@;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.. ############################################################################
2-
.. File : doc/interface.rst
2+
.. File : doc/appendices/interface.rst
33
.. ############################################################################
44
55
.. highlight:: swig
@@ -15,39 +15,45 @@ flc
1515

1616
The primary file defines typemaps.
1717

18-
.. literalinclude:: ../include/flc.i
18+
.. literalinclude:: ../../include/flc.i
1919
:linenos:
2020

2121
flc_algorithm
2222
=============
2323

24-
.. literalinclude:: ../include/flc_algorithm.i
24+
.. literalinclude:: ../../include/flc_algorithm.i
2525
:linenos:
2626

2727
flc_chrono
2828
=============
2929

30-
.. literalinclude:: ../include/flc_chrono.i
30+
.. literalinclude:: ../../include/flc_chrono.i
3131
:linenos:
3232

3333
flc_random
3434
=============
3535

36-
.. literalinclude:: ../include/flc_random.i
36+
.. literalinclude:: ../../include/flc_random.i
37+
:linenos:
38+
39+
flc_set
40+
=============
41+
42+
.. literalinclude:: ../../include/flc_set.i
3743
:linenos:
3844

3945
flc_string
4046
=============
4147

42-
.. literalinclude:: ../include/flc_string.i
48+
.. literalinclude:: ../../include/flc_string.i
4349
:linenos:
4450

4551
flc_vector
4652
=============
4753

48-
.. literalinclude:: ../include/flc_vector.i
54+
.. literalinclude:: ../../include/flc_vector.i
4955
:linenos:
5056

5157
.. ############################################################################
52-
.. end of doc/interface.rst
58+
.. end of doc/appendices/interface.rst
5359
.. ############################################################################
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
.. ############################################################################
2-
.. File : doc/license.rst
2+
.. File : doc/appendices/license.rst
33
.. ############################################################################
44
55
*******
66
License
77
*******
88

9-
.. include:: ../LICENSE
9+
.. include:: ../../LICENSE
1010

1111
.. ############################################################################
12-
.. end of doc/license.rst
12+
.. end of doc/appendices/license.rst
1313
.. ############################################################################

doc/index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ which wrap the C++ library.
5151
:maxdepth: 2
5252
:caption: Appendices
5353

54-
interface.rst
55-
license.rst
54+
appendices/interface.rst
55+
appendices/license.rst
5656

0 commit comments

Comments
 (0)