Skip to content

Commit 9728aae

Browse files
authored
Replace FlibcppVersion with CgvFindVersion (#27)
1 parent dc7948b commit 9728aae

8 files changed

+182
-144
lines changed

.gitattributes

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/.* export-ignore
2-
/cmake/git-version.txt export-subst

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
cmake_minimum_required(VERSION 3.8)
99

1010
# 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")
11+
include("${CMAKE_CURRENT_LIST_DIR}/cmake/CgvFindVersion.cmake")
12+
cgv_find_version(Flibcpp)
1313

1414
project(Flibcpp VERSION "${Flibcpp_VERSION}" LANGUAGES CXX Fortran)
1515
cmake_policy(VERSION 3.8...3.19)

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ support those for now.
4949

5050
## License
5151

52-
The entirety of this repository and its generated code are released under an
53-
MIT license. See the LICENSE file.
52+
The source code for FlibCpp, including auto-generated code, is released under
53+
an MIT license. See the LICENSE file.
5454

5555
It must be linked against an existing C++ standard library implementation (e.g.
5656
[libc++](https://libcxx.llvm.org) or [libstdc++](https://gcc.gnu.org/wiki/Libstdc++)), so those are not covered by this license.
5757

5858
SWIG is not required to use this project. SWIG-generated code does _not_
5959
inherit SWIG's GPL license.
60+
61+
The [CgvFindVersion.cmake helper
62+
script](https://github.com/sethrj/cmake-git-version) used for determining the
63+
release info is released independently under an Apache-2.0 license.

cmake/.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.gitattributes export-ignore
2+
/CgvFindVersion.cmake export-subst

cmake/CgvFindVersion.cmake

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#---------------------------------*-CMake-*----------------------------------#
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# https://github.com/sethrj/cmake-git-version
5+
#
6+
# Copyright 2021 UT-Battelle, LLC and Seth R Johnson
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#[=======================================================================[.rst:
20+
21+
CgvFindVersion
22+
--------------
23+
24+
.. command:: cgv_find_version
25+
26+
Get the project version using Git descriptions to ensure the version numbers
27+
are always synchronized between Git and CMake::
28+
29+
cgv_find_version([<projname>])
30+
31+
``<projname>``
32+
Name of the project.
33+
34+
This command sets the following variables in the parent package::
35+
36+
${projname}_VERSION
37+
${projname}_VERSION_STRING
38+
39+
It takes the project name and version file path as optional arguments to
40+
support using it before the CMake ``project`` command.
41+
42+
The project version string uses an approximation to SemVer strings, appearing
43+
as v0.1.2 if the version is actually a tagged release, or v0.1.3+abcdef if
44+
it's not.
45+
46+
If a non-tagged version is exported, or an untagged shallow git clone is used,
47+
it's impossible to determine the version from the tag, so a warning will be
48+
issued and the version will be set to 0.0.0.
49+
50+
The exact regex used to match the version tag is::
51+
52+
v([0-9.]+)(-dev[0-9.]+)?
53+
54+
55+
.. note:: In order for this script to work properly with archived git
56+
repositories (generated with ``git-archive`` or GitHub's release tarball
57+
feature), it's necessary to add to your ``.gitattributes`` file::
58+
59+
CgvFindVersion.cmake export-subst
60+
61+
#]=======================================================================]
62+
63+
if(CMAKE_SCRIPT_MODE_FILE)
64+
cmake_minimum_required(VERSION 3.8)
65+
endif()
66+
67+
function(cgv_find_version)
68+
set(projname "${ARGV0}")
69+
if(NOT projname)
70+
set(projname "${CMAKE_PROJECT_NAME}")
71+
if(NOT projname)
72+
message(FATAL_ERROR "Project name is not defined")
73+
endif()
74+
endif()
75+
76+
# Get a possible Git version generated using git-archive (see the
77+
# .gitattributes file)
78+
set(_ARCHIVE_TAG "$Format:%D$")
79+
set(_ARCHIVE_HASH "$Format:%h$")
80+
81+
set(_TAG_REGEX "v([0-9.]+)(-dev[0-9.]+)?")
82+
set(_HASH_REGEX "([0-9a-f]+)")
83+
84+
if(_ARCHIVE_HASH MATCHES "%h")
85+
# Not a "git archive": use live git information
86+
set(_CACHE_VAR "${projname}_GIT_DESCRIBE")
87+
set(_CACHED_VERSION "${${_CACHE_VAR}}")
88+
if(NOT _CACHED_VERSION)
89+
# Building from a git checkout rather than a distribution
90+
if(NOT GIT_EXECUTABLE)
91+
find_package(Git QUIET REQUIRED)
92+
endif()
93+
execute_process(
94+
COMMAND "${GIT_EXECUTABLE}" "describe" "--tags" "--match" "v*"
95+
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
96+
ERROR_VARIABLE _GIT_ERR
97+
OUTPUT_VARIABLE _VERSION_STRING
98+
RESULT_VARIABLE _GIT_RESULT
99+
OUTPUT_STRIP_TRAILING_WHITESPACE
100+
)
101+
if(_GIT_RESULT)
102+
message(AUTHOR_WARNING "No git tags in ${projname} matched 'v*': "
103+
"${_GIT_ERR}")
104+
elseif(NOT _VERSION_STRING)
105+
message(WARNING "Failed to get ${projname} version from git: "
106+
"git describe returned an empty string")
107+
else()
108+
# Process description tag: e.g. v0.4.0-2-gc4af497 or v0.4.0
109+
# or v2.0.0-dev2
110+
string(REGEX MATCH "^${_TAG_REGEX}(-[0-9]+-g${_HASH_REGEX})?" _MATCH
111+
"${_VERSION_STRING}"
112+
)
113+
if(_MATCH)
114+
set(_VERSION_STRING "${CMAKE_MATCH_1}")
115+
set(_VERSION_STRING_SUFFIX "${CMAKE_MATCH_2}")
116+
if(CMAKE_MATCH_3)
117+
# *not* a tagged release
118+
set(_VERSION_HASH "${CMAKE_MATCH_4}")
119+
endif()
120+
endif()
121+
endif()
122+
if(NOT _VERSION_STRING)
123+
execute_process(
124+
COMMAND "${GIT_EXECUTABLE}" "log" "-1" "--format=%h" "HEAD"
125+
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
126+
OUTPUT_VARIABLE _VERSION_HASH
127+
OUTPUT_STRIP_TRAILING_WHITESPACE
128+
)
129+
endif()
130+
set(_CACHED_VERSION "${_VERSION_STRING}" "${_VERSION_STRING_SUFFIX}" "${_VERSION_HASH}")
131+
set("${_CACHE_VAR}" "${_CACHED_VERSION}" CACHE INTERNAL
132+
"Version string and hash for ${projname}")
133+
endif()
134+
list(GET _CACHED_VERSION 0 _VERSION_STRING)
135+
list(GET _CACHED_VERSION 1 _VERSION_STRING_SUFFIX)
136+
list(GET _CACHED_VERSION 2 _VERSION_HASH)
137+
else()
138+
string(REGEX MATCH "tag: *${_TAG_REGEX}" _MATCH "${_ARCHIVE_TAG}")
139+
if(_MATCH)
140+
set(_VERSION_STRING "${CMAKE_MATCH_1}")
141+
set(_VERSION_STRING_SUFFIX "${CMAKE_MATCH_2}")
142+
else()
143+
message(AUTHOR_WARNING "Could not match a version tag for "
144+
"git description '${_ARCHIVE_TAG}': perhaps this archive was not "
145+
"exported from a tagged commit?")
146+
string(REGEX MATCH " *${_HASH_REGEX}" _MATCH "${_ARCHIVE_HASH}")
147+
if(_MATCH)
148+
set(_VERSION_HASH "${CMAKE_MATCH_1}")
149+
endif()
150+
endif()
151+
endif()
152+
153+
if(NOT _VERSION_STRING)
154+
set(_VERSION_STRING "0.0.0")
155+
endif()
156+
157+
if(_VERSION_HASH)
158+
set(_FULL_VERSION_STRING "v${_VERSION_STRING}${_VERSION_STRING_SUFFIX}+${_VERSION_HASH}")
159+
else()
160+
set(_FULL_VERSION_STRING "v${_VERSION_STRING}${_VERSION_STRING_SUFFIX}")
161+
endif()
162+
163+
set(${projname}_VERSION "${_VERSION_STRING}" PARENT_SCOPE)
164+
set(${projname}_VERSION_STRING "${_FULL_VERSION_STRING}" PARENT_SCOPE)
165+
endfunction()
166+
167+
if(CMAKE_SCRIPT_MODE_FILE)
168+
cgv_find_version(TEMP)
169+
message("VERSION=\"${TEMP_VERSION}\"")
170+
message("VERSION_STRING=\"${TEMP_VERSION_STRING}\"")
171+
endif()

cmake/FlibcppVersion.cmake

-136
This file was deleted.

cmake/git-version.txt

-2
This file was deleted.

doc/introduction.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ should automatically find the necessary CMake configure file.
7171
An `example Fortran application`_ that depends only on Flibcpp is
7272
available on Github.
7373

74-
.. _example Fortran application: https://github.com/swig-fortran/flibcpp-example-app).
74+
.. _example Fortran application: https://github.com/swig-fortran/flibcpp-example-app
7575

7676
----
7777

0 commit comments

Comments
 (0)