Skip to content

Commit

Permalink
Merge pull request #10 from swig-fortran/doc
Browse files Browse the repository at this point in the history
Tweak documentation and fix CMake project version
  • Loading branch information
sethrj authored Oct 21, 2019
2 parents 32712ab + 55426c5 commit 75490a2
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 51 deletions.
48 changes: 11 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
#---------------------------------------------------------------------------#

cmake_minimum_required(VERSION 3.8)
project(Flibcpp VERSION 0.3.0 LANGUAGES CXX Fortran)

# Determine version number from git metadata
include("${CMAKE_CURRENT_LIST_DIR}/cmake/FlibcppVersion.cmake")
flibcpp_find_version(Flibcpp "${CMAKE_CURRENT_LIST_DIR}/cmake/git-version.txt")
message(STATUS "Flibcpp version: ${Flibcpp_VERSION}")

project(Flibcpp VERSION "${Flibcpp_VERSION}" LANGUAGES CXX Fortran)

#---------------------------------------------------------------------------#
# OPTIONS
Expand Down Expand Up @@ -85,41 +91,6 @@ endif()
# Enable testing based on BUILD_TESTING flag
include(CTest)

#---------------------------------------------------------------------------#
# VERSIONING
#---------------------------------------------------------------------------#

# Get a possible Git version generated using git-archive (see the .gitattributes
# file)
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/cmake/git-version.txt"
FLIBCPP_VERSION_STRING)

if (NOT FLIBCPP_VERSION_STRING MATCHES "\\$Format:")
# First line are decorators, second is hash
list(GET FLIBCPP_VERSION_STRING 0 _tag)
string(REGEX REPLACE "tag: *" "" _tag "${_tag}")
list(GET FLIBCPP_VERSION_STRING 1 _hash)
string(REGEX REPLACE " +" "" _hash "${_hash}")
set(FLIBCPP_VERSION_STRING "${_tag}-g${_hash}")
else()
find_package(Git)
if (Git_FOUND)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "describe"
ERROR_QUIET
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE FLIBCPP_VERSION_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else()
set(FLIBCPP_VERSION_STRING "${Flibcpp_VERSION}")
endif()
endif()

set(FLIBCPP_VERSION_CPP "${CMAKE_CURRENT_BINARY_DIR}/flibcpp_version.cpp")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/flibcpp_version.cpp.in"
"${FLIBCPP_VERSION_CPP}" @ONLY)

#---------------------------------------------------------------------------#
# LIBRARY
#---------------------------------------------------------------------------#
Expand Down Expand Up @@ -212,7 +183,10 @@ function(flibcpp_add_module name)
)
endfunction()

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

# Also install 'import_flc' if using SWIG
Expand Down
136 changes: 136 additions & 0 deletions cmake/FlibcppVersion.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
##---------------------------------------------------------------------------##
## File : flibcpp/cmake/FlibcppVersion.cmake
#[=======================================================================[.rst:

FlibcppVersion
--------------

.. command:: flibcpp_find_version

Get the project version using Git descriptions to ensure the version numbers
are always synchronized between Git and CMake::

flibcpp_find_version(<projname> <git-version-file>)


``<projname>``
Name of the project.

This command sets the following variables in the parent package::

${PROJNAME}_VERSION
${PROJNAME}_VERSION_STRING

The project version string uses PEP-440 semantic versioning, and will look
like v0.1.2 if the version is actually a tagged release, or v0.1.3+abcdef if
it's not.

If a non-tagged version is exported, or an untagged shallow git clone is used,
it's impossible to determine the version from the tag, so a warning will be
issued and the version will be set to 0.0.0.

#]=======================================================================]

if (CMAKE_SCRIPT_MODE_FILE)
cmake_minimum_required(VERSION 3.8)
endif()

function(flibcpp_find_version PROJNAME GIT_VERSION_FILE)
# Get a possible Git version generated using git-archive (see the
# .gitattributes file)
file(STRINGS "${GIT_VERSION_FILE}" _TEXTFILE)

if (_TEXTFILE MATCHES "\\$Format:")
# Not a "git archive": use live git information
set(_CACHE_VAR "${PROJNAME}_GIT_DESCRIBE")
set(_CACHED_VERSION "${${_CACHE_VAR}}")
if (NOT _CACHED_VERSION)
# Building from a git checkout rather than a distribution
find_package(Git QUIET REQUIRED)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "describe" "--tags" "--match" "v*"
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
ERROR_VARIABLE _GIT_ERR
OUTPUT_VARIABLE _VERSION_STRING
RESULT_VARIABLE _GIT_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (NOT _GIT_RESULT EQUAL "0")
message(WARNING "Failed to get ${PROJNAME} version from git: "
"${_GIT_ERR}")
elseif (NOT _VERSION_STRING)
message(WARNING "Failed to get ${PROJNAME} version from git: "
"git describe returned an empty string")
else()
# Process description tag: e.g. v0.4.0-2-gc4af497 or v0.4.0
string(REGEX MATCH "v([0-9.]+)(-[0-9]+-g([0-9a-f]+))?" _MATCH
"${_VERSION_STRING}"
)
if (_MATCH)
set(_VERSION_STRING "${CMAKE_MATCH_1}")
if (CMAKE_MATCH_2)
# *not* a tagged release
set(_VERSION_HASH "${CMAKE_MATCH_3}")
endif()
endif()
endif()
if (NOT _VERSION_STRING)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "log" "-1" "--format=%h" "HEAD"
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
OUTPUT_VARIABLE _VERSION_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
set(_CACHED_VERSION "${_VERSION_STRING}" "${_VERSION_HASH}")
set("${_CACHE_VAR}" "${_CACHED_VERSION}" CACHE INTERNAL
"Version string and hash for ${PROJNAME}")
endif()
list(GET _CACHED_VERSION 0 _VERSION_STRING)
list(GET _CACHED_VERSION 1 _VERSION_HASH)
else()
# First line are decorators, second is hash.
list(GET _TEXTFILE 0 _TAG)
string(REGEX MATCH "tag: *v([0-9.]+)" _MATCH "${_TAG}")
if (_MATCH)
set(_VERSION_STRING "${CMAKE_MATCH_1}")
else()
# *not* a tagged release
list(GET _TEXTFILE 1 _HASH)
string(REGEX MATCH " *([0-9a-f]+)" _MATCH "${_HASH}")
if (_MATCH)
set(_VERSION_HASH "${CMAKE_MATCH_1}")
endif()
endif()
endif()

if (NOT _VERSION_STRING)
message(WARNING "Could not determine version number for ${PROJNAME}: "
"perhaps a non-release archive?")
set(_VERSION_STRING "0.0.0")
endif()

if (_VERSION_HASH)
set(_FULL_VERSION_STRING "v${_VERSION_STRING}+${_VERSION_HASH}")
else()
set(_FULL_VERSION_STRING "v${_VERSION_STRING}")
endif()

set(${PROJNAME}_VERSION "${_VERSION_STRING}" PARENT_SCOPE)
set(${PROJNAME}_VERSION_STRING "${_FULL_VERSION_STRING}" PARENT_SCOPE)
endfunction()

if (CMAKE_SCRIPT_MODE_FILE)
# This script is being run from the command line. Useful for debugging.
if (NOT DEFINED GIT_VERSION_FILE)
message(FATAL_ERROR "Run this script with "
"cmake -D GIT_VERSION_FILE=git-version.txt -P FlibcppVersion.cmake")
endif()
flibcpp_find_version(local ${GIT_VERSION_FILE})
message(STATUS "${LOCAL_VERSION}")
message(STATUS "${LOCAL_VERSION_STRING}")
endif()

##---------------------------------------------------------------------------##
## end of flibcpp/cmake/FlibcppVersion.cmake
##---------------------------------------------------------------------------##
2 changes: 1 addition & 1 deletion cmake/flibcpp_version.cpp.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern "C" const char flibcpp_version[] = "@FLIBCPP_VERSION_STRING@";
extern "C" const char flibcpp_version[] = "@Flibcpp_VERSION_STRING@";
extern "C" const int flibcpp_version_major = @PROJECT_VERSION_MAJOR@;
extern "C" const int flibcpp_version_minor = @PROJECT_VERSION_MINOR@;
extern "C" const int flibcpp_version_patch = @PROJECT_VERSION_PATCH@;
22 changes: 14 additions & 8 deletions doc/interface.rst → doc/appendices/interface.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.. ############################################################################
.. File : doc/interface.rst
.. File : doc/appendices/interface.rst
.. ############################################################################
.. highlight:: swig
Expand All @@ -15,39 +15,45 @@ flc

The primary file defines typemaps.

.. literalinclude:: ../include/flc.i
.. literalinclude:: ../../include/flc.i
:linenos:

flc_algorithm
=============

.. literalinclude:: ../include/flc_algorithm.i
.. literalinclude:: ../../include/flc_algorithm.i
:linenos:

flc_chrono
=============

.. literalinclude:: ../include/flc_chrono.i
.. literalinclude:: ../../include/flc_chrono.i
:linenos:

flc_random
=============

.. literalinclude:: ../include/flc_random.i
.. literalinclude:: ../../include/flc_random.i
:linenos:

flc_set
=============

.. literalinclude:: ../../include/flc_set.i
:linenos:

flc_string
=============

.. literalinclude:: ../include/flc_string.i
.. literalinclude:: ../../include/flc_string.i
:linenos:

flc_vector
=============

.. literalinclude:: ../include/flc_vector.i
.. literalinclude:: ../../include/flc_vector.i
:linenos:

.. ############################################################################
.. end of doc/interface.rst
.. end of doc/appendices/interface.rst
.. ############################################################################
6 changes: 3 additions & 3 deletions doc/license.rst → doc/appendices/license.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
.. ############################################################################
.. File : doc/license.rst
.. File : doc/appendices/license.rst
.. ############################################################################
*******
License
*******

.. include:: ../LICENSE
.. include:: ../../LICENSE

.. ############################################################################
.. end of doc/license.rst
.. end of doc/appendices/license.rst
.. ############################################################################
4 changes: 2 additions & 2 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ which wrap the C++ library.
:maxdepth: 2
:caption: Appendices

interface.rst
license.rst
appendices/interface.rst
appendices/license.rst

0 comments on commit 75490a2

Please sign in to comment.