From 848100cb2fe6ac3851b481a08876dc85129b0208 Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Verschelde?= Date: Tue, 24 Jan 2017 19:34:09 +0100 Subject: [PATCH] CMake: Add version logic and make it generate Version.hpp Part of #82. --- .gitignore | 1 + CMakeLists.txt | 51 ++++++++++++++++++++++++++++++++++++++++++- Source/Version.hpp.in | 46 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 Source/Version.hpp.in diff --git a/.gitignore b/.gitignore index ab0ef35..2e8676a 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ Makefile cscope.* Data/users Data/config.txt +Source/Version.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d798d41..ac908f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,12 +16,61 @@ if(UNIX AND NOT APPLE) endif() +### Version + +# Version for the current (stable) or next (development) release +set(LUGARU_VERSION_MAJOR 1) +set(LUGARU_VERSION_MINOR 2) +set(LUGARU_VERSION_PATCH 0) + +# MAJOR.MINOR, or MAJOR.MINOR.PATCH if PATCH != 0 +set(LUGARU_VERSION_NUMBER "${LUGARU_VERSION_MAJOR}.${LUGARU_VERSION_MINOR}") +if(LUGARU_VERSION_PATCH) + set(LUGARU_VERSION_NUMBER "${LUGARU_VERSION_NUMBER}.${LUGARU_VERSION_PATCH}") +endif() + +# Set to "" for stable (tagged) builds, "-dev" for dev builds +set(LUGARU_VERSION_SUFFIX "-dev") # development +#set(LUGARU_VERSION_SUFFIX "") # stable + +# Set to 7-char git commit hash if available, otherwise "". +# On stable (tagged) builds, this is ignored. +set(LUGARU_VERSION_HASH "") +if(LUGARU_VERSION_SUFFIX STREQUAL "-dev" AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git) + find_package(Git) + if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE "LUGARU_VERSION_HASH" + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + message(STATUS "Git commit hash: ${LUGARU_VERSION_HASH}") + endif() +endif() + +set(LUGARU_VERSION_RELEASE "" CACHE STRING "Optional release string, e.g. for distro packages release number") + +# Final string built from the above constants, following the scheme: +# MAJOR.MINOR[.PATCH][-dev] [(git HASH)] [[RELEASE]] +set(LUGARU_VERSION_STRING "${LUGARU_VERSION_NUMBER}${LUGARU_VERSION_SUFFIX}") +if(NOT LUGARU_VERSION_HASH STREQUAL "") + set(LUGARU_VERSION_STRING "${LUGARU_VERSION_STRING} (git ${LUGARU_VERSION_HASH})") +endif() +if(NOT LUGARU_VERSION_RELEASE STREQUAL "") + set(LUGARU_VERSION_STRING "${LUGARU_VERSION_STRING} [${LUGARU_VERSION_RELEASE}]") +endif() + +message(STATUS "Version string: ${LUGARU_VERSION_STRING}") +configure_file(${SRCDIR}/Version.hpp.in ${SRCDIR}/Version.hpp ESCAPE_QUOTES @ONLY) + + ### CMake config if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE RelWithDebInfo) endif(NOT CMAKE_BUILD_TYPE) -message("CMake build type: ${CMAKE_BUILD_TYPE}") +message(STATUS "CMake build type: ${CMAKE_BUILD_TYPE}") set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wno-parentheses -pedantic --std=gnu++11 ${CMAKE_CXX_FLAGS}") diff --git a/Source/Version.hpp.in b/Source/Version.hpp.in new file mode 100644 index 0000000..180ecc7 --- /dev/null +++ b/Source/Version.hpp.in @@ -0,0 +1,46 @@ +/* +Copyright (C) 2003, 2010 - Wolfire Games +Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file) + +This file is part of Lugaru. + +Lugaru is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +Lugaru is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Lugaru. If not, see . +*/ + +/** Version number is either MAJOR.MINOR, or MAJOR.MINOR.PATCH if PATCH != 0 */ +const unsigned int VERSION_MAJOR = @LUGARU_VERSION_MAJOR@; +const unsigned int VERSION_MINOR = @LUGARU_VERSION_MINOR@; +const unsigned int VERSION_PATCH = @LUGARU_VERSION_PATCH@; +const std::string VERSION_NUMBER = "@LUGARU_VERSION_NUMBER@"; + +/** Set to "" for stable (tagged) builds, "-dev" for dev builds */ +const std::string VERSION_SUFFIX = "@LUGARU_VERSION_SUFFIX@"; +/** Set to 7-char git commit hash if available, otherwise "" */ +const std::string VERSION_HASH = "@LUGARU_VERSION_HASH@"; +/** Optional release string, e.g. for distro packages release number */ +const std::string VERSION_RELEASE = "@LUGARU_VERSION_RELEASE@"; + +/** Final string built from the above constants, following the scheme + * defined in CMakeLists.txt, typically: + * MAJOR.MINOR[.PATCH][-dev] [(git HASH)] [[RELEASE]] + * + * For example: + * "1.2-dev (git ab12c34)" + * "1.2-dev (git ab12c34) [OSS Lugaru official]" + * "1.3.1 [Mageia 1.3.1-2.mga6]" + */ +const std::string VERSION_STRING = "@LUGARU_VERSION_STRING@"; + +/** Build type (Release, Debug, RelWithDebInfo) to output to the terminal */ +const std::string VERSION_BUILD_TYPE = "@CMAKE_BUILD_TYPE@"; -- 2.39.2