Skip to content

Commit

Permalink
Add a version.h header containing the full version information.
Browse files Browse the repository at this point in the history
Currently not really documented in Doxygen because ugh I have no idea
how to make that work.
  • Loading branch information
mosra committed Jun 1, 2020
1 parent 5be366d commit 7e0c03d
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,12 @@ set(CORRADE_DATA_INSTALL_DIR ${CORRADE_INCLUDE_INSTALL_PREFIX}/share/corrade)
set(CORRADE_CMAKE_MODULE_INSTALL_DIR ${CORRADE_INCLUDE_INSTALL_PREFIX}/share/cmake/Corrade)
set(CORRADE_INCLUDE_INSTALL_DIR ${CORRADE_INCLUDE_INSTALL_PREFIX}/include/Corrade)

# Library version
# Library version. CORRADE_VERSION_YEAR/MONTH is used in
# src/Corrade/CMakeLists.txt to generate the version.h header.
set(CORRADE_LIBRARY_VERSION 2.3)
set(CORRADE_LIBRARY_SOVERSION 2)
set(CORRADE_VERSION_YEAR 2019)
set(CORRADE_VERSION_MONTH 10)

# A single output location. After a decade of saying NO THIS IS A NON-SOLUTION
# TO A NON-PROBLEM I reconsidered my views and enabled this, because:
Expand Down
6 changes: 6 additions & 0 deletions doc/corrade-changelog.dox
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ namespace Corrade {
- Added an ability to disable unique globals across shared libraries using
@ref CORRADE_BUILD_STATIC_UNIQUE_GLOBALS on static builds that don't need
it
- Library version is now exposed through `CORRADE_VERSION_YEAR`,
`CORRADE_VERSION_MONTH`, `CORRADE_VERSION_COMMIT`, `CORRADE_VERSION_HASH`
and `CORRADE_VERSION_STRING` preprocessor defines in a new
`Corrade/version.h` header. This header is not included by any other header
to avoid trigerring a full rebuild when Git commit changes. If Git is not
found, only the first two defines are present.

@subsubsection corrade-changelog-latest-changes-containers Containers library

Expand Down
29 changes: 28 additions & 1 deletion src/Corrade/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,33 @@ else()
set(SHARED_OR_STATIC STATIC)
endif()

# Generate configure header
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake
${CMAKE_CURRENT_BINARY_DIR}/configure.h)

# Generate version header. If Git is found and this is a Git working copy,
# extract values from there, otherwise use just CORRADE_VERSION_YEAR/MONTH that
# are set in project root CMakeLists.
find_package(Git)
if(Git_FOUND)
# Match only tags starting with `v`, always use the long format so we have
# a commit hash also on a tagged version
execute_process(COMMAND ${GIT_EXECUTABLE} describe --match "v*" --long
OUTPUT_VARIABLE CORRADE_VERSION_STRING
RESULT_VARIABLE _CORRADE_VERSION_RESULT
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if(CORRADE_VERSION_STRING MATCHES "^v([0-9][0-9][0-9][0-9])\\.0?([0-9][0-9])-([0-9]+)-g([a-f0-9]+)$")
set(CORRADE_VERSION_YEAR ${CMAKE_MATCH_1})
set(CORRADE_VERSION_MONTH ${CMAKE_MATCH_2})
set(CORRADE_VERSION_COMMIT ${CMAKE_MATCH_3})
set(CORRADE_VERSION_HASH ${CMAKE_MATCH_4})
elseif(_CORRADE_VERSION_RESULT EQUAL 0)
message(WARNING "Can't match Git version from ${CORRADE_VERSION_STRING}")
endif()
endif()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/version.h.cmake
${CMAKE_CURRENT_BINARY_DIR}/version.h)

set(Corrade_HEADERS
Corrade.h)

Expand All @@ -40,7 +64,10 @@ add_custom_target(Corrade SOURCES ${Corrade_HEADERS})
set_target_properties(Corrade PROPERTIES FOLDER "Corrade")

install(FILES ${Corrade_HEADERS} DESTINATION ${CORRADE_INCLUDE_INSTALL_DIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/configure.h DESTINATION ${CORRADE_INCLUDE_INSTALL_DIR})
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/configure.h
${CMAKE_CURRENT_BINARY_DIR}/version.h
DESTINATION ${CORRADE_INCLUDE_INSTALL_DIR})

if(WITH_UTILITY) # Cyclic dependency of Containers and Utility
add_subdirectory(Containers)
Expand Down
2 changes: 2 additions & 0 deletions src/Corrade/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake
corrade_add_test(TargetTest TargetTest.cpp)
target_include_directories(TargetTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

corrade_add_test(VersionTest VersionTest.cpp)

# Baseline, C++11, is everywhere
corrade_add_test(Cpp11StandardTest CppStandardTest.cpp)
set_target_properties(Cpp11StandardTest PROPERTIES
Expand Down
64 changes: 64 additions & 0 deletions src/Corrade/Test/VersionTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include "Corrade/TestSuite/Tester.h"
#include "Corrade/TestSuite/Compare/Numeric.h"
#include "Corrade/version.h"

namespace Corrade { namespace Test { namespace {

struct VersionTest: TestSuite::Tester {
explicit VersionTest();

void test();
};

VersionTest::VersionTest() {
addTests({&VersionTest::test});
}

void VersionTest::test() {
Debug{} << "CORRADE_VERSION_YEAR:" << CORRADE_VERSION_YEAR;
Debug{} << "CORRADE_VERSION_MONTH:" << CORRADE_VERSION_MONTH;
#ifdef CORRADE_VERSION_COMMIT
Debug{} << "CORRADE_VERSION_COMMIT:" << CORRADE_VERSION_COMMIT;
Debug{} << "CORRADE_VERSION_HASH:" << reinterpret_cast<void*>(CORRADE_VERSION_HASH);
Debug{} << "CORRADE_VERSION_STRING:" << CORRADE_VERSION_STRING;
#else
Debug{} << "No Git version information available.";
#endif

CORRADE_COMPARE_AS(CORRADE_VERSION_YEAR, 2019, TestSuite::Compare::GreaterOrEqual);
CORRADE_COMPARE_AS(CORRADE_VERSION_YEAR, 2100, TestSuite::Compare::LessOrEqual);
CORRADE_COMPARE_AS(CORRADE_VERSION_MONTH, 0, TestSuite::Compare::Greater);
CORRADE_COMPARE_AS(CORRADE_VERSION_MONTH, 12, TestSuite::Compare::LessOrEqual);
#ifdef CORRADE_VERSION_COMMIT
CORRADE_COMPARE_AS(CORRADE_VERSION_COMMIT, 0, TestSuite::Compare::GreaterOrEqual);
#endif
}

}}}

CORRADE_TEST_MAIN(Corrade::Test::VersionTest)
38 changes: 38 additions & 0 deletions src/Corrade/version.h.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef Corrade_version_h
#define Corrade_version_h
/*
This file is part of Corrade.

Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020 Vladimír Vondruš <mosra@centrum.cz>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

/* Note: this header is deliberately not included from anywhere except
VersionTest to avoid triggering a full rebuild every time the Git commit
hash changes. */

#define CORRADE_VERSION_YEAR ${CORRADE_VERSION_YEAR}
#define CORRADE_VERSION_MONTH ${CORRADE_VERSION_MONTH}
#cmakedefine CORRADE_VERSION_COMMIT ${CORRADE_VERSION_COMMIT}
#cmakedefine CORRADE_VERSION_HASH 0x${CORRADE_VERSION_HASH}
#cmakedefine CORRADE_VERSION_STRING "${CORRADE_VERSION_STRING}"

#endif

0 comments on commit 7e0c03d

Please sign in to comment.