From 04fd20d62b4e2b5a048298fa4177f09fb8353078 Mon Sep 17 00:00:00 2001 From: Eugene Shalygin Date: Thu, 15 Nov 2018 18:20:02 +0100 Subject: [PATCH] cmake: read project version from the version.hpp file --- CMakeLists.txt | 9 ++++++--- cmake/Modules/LibtorrentMacros.cmake | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c1a60f55..3f5268e20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,18 @@ cmake_minimum_required(VERSION 3.12.0 FATAL_ERROR) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules) +include(LibtorrentMacros) + +read_version("${CMAKE_CURRENT_SOURCE_DIR}/include/libtorrent/version.hpp" VER_MAJOR VER_MINOR VER_TINY) + project(libtorrent DESCRIPTION "Bittorrent library" - VERSION 1.2.0 + VERSION ${VER_MAJOR}.${VER_MINOR}.${VER_TINY} ) set (SOVERSION "10") -list(APPEND CMAKE_MODULE_PATH ${libtorrent_SOURCE_DIR}/cmake/Modules) include(GNUInstallDirs) include(GeneratePkgConfig) -include(LibtorrentMacros) set(libtorrent_include_files add_torrent_params diff --git a/cmake/Modules/LibtorrentMacros.cmake b/cmake/Modules/LibtorrentMacros.cmake index e73eb0e1f..e73fbfe5a 100644 --- a/cmake/Modules/LibtorrentMacros.cmake +++ b/cmake/Modules/LibtorrentMacros.cmake @@ -80,3 +80,28 @@ function(select_cxx_standard _target _minimal_supported_version) message(STATUS "Building in C++${_std} mode") endfunction() + +# function for parsing version variables that are set in version.hpp file +# the version identifiers there are defined as follows: +# #define LIBTORRENT_VERSION_MAJOR 1 +# #define LIBTORRENT_VERSION_MINOR 2 +# #define LIBTORRENT_VERSION_TINY 0 + +function(read_version _verFile _outVarMajor _outVarMinor _outVarTiny) + file(STRINGS ${_verFile} verFileContents REGEX ".+LIBTORRENT_VERSION_[A-Z]+.[0-9]+.*") +# message(STATUS "version file contents: ${verFileContents}") + # the verFileContents variable contains something like the following: + # #define LIBTORRENT_VERSION_MAJOR 1;#define LIBTORRENT_VERSION_MINOR 2;#define LIBTORRENT_VERSION_TINY 0 + set(_regex ".+_MAJOR +([0-9]+);.+_MINOR +([0-9]+);.+_TINY +([0-9]+)") + # note quotes around _regex, they are needed because the variable contains semicolons + string(REGEX MATCH "${_regex}" _tmp "${verFileContents}") + if (NOT _tmp) + message(FATAL_ERROR "Could not detect project version number from ${_verFile}") + endif() + +# message(STATUS "Matched version string: ${_tmp}") + + set(${_outVarMajor} ${CMAKE_MATCH_1} PARENT_SCOPE) + set(${_outVarMinor} ${CMAKE_MATCH_2} PARENT_SCOPE) + set(${_outVarTiny} ${CMAKE_MATCH_3} PARENT_SCOPE) +endfunction()