Commit 2bbe3ae8 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

cmake: use same script to determine build versio as VILLASnode

parent dfadcfff
# - Returns a version string from Git
#
# These functions force a re-configure on each git commit so that you can
# trust the values of the variables in your build system.
#
# get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
#
# Returns the refspec and sha hash of the current head revision
#
# git_describe(<var> [<additional arguments to git describe> ...])
#
# Returns the results of git describe on the source tree, and adjusting
# the output so that it tests false if an error occurs.
#
# git_get_exact_tag(<var> [<additional arguments to git describe> ...])
#
# Returns the results of git describe --exact-match on the source tree,
# and adjusting the output so that it tests false if there was no exact
# matching tag.
#
# Requires CMake 2.6 or newer (uses the 'function' command)
#
# Original Author:
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Copyright Iowa State University 2009-2010.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
if(__get_git_revision_description)
return()
endif()
set(__get_git_revision_description YES)
# We must run the following at "include" time, not at function call time,
# to find the path to this module rather than the path to a calling list file
get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
function(get_git_head_revision _refspecvar _hashvar)
set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
# We have reached the root directory, we are not in git
set(${_refspecvar} "nogit" PARENT_SCOPE)
set(${_hashvar} "nogit" PARENT_SCOPE)
return()
endif()
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
endwhile()
# check if this is a submodule
if(NOT IS_DIRECTORY ${GIT_DIR})
file(READ ${GIT_DIR} submodule)
string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
endif()
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
if(NOT EXISTS "${GIT_DATA}")
file(MAKE_DIRECTORY "${GIT_DATA}")
endif()
if(NOT EXISTS "${GIT_DIR}/HEAD")
return()
endif()
set(HEAD_FILE "${GIT_DATA}/HEAD")
configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
"${GIT_DATA}/grabRef.cmake"
@ONLY)
include("${GIT_DATA}/grabRef.cmake")
set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
endfunction()
function(git_describe _var)
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
get_git_head_revision(refspec hash)
if(NOT GIT_FOUND)
set(${_var} "nogit" PARENT_SCOPE)
return()
endif()
if(NOT hash)
set(${_var} "nogit" PARENT_SCOPE)
return()
endif()
# TODO sanitize
#if((${ARGN}" MATCHES "&&") OR
# (ARGN MATCHES "||") OR
# (ARGN MATCHES "\\;"))
# message("Please report the following error to the project!")
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
#endif()
#message(STATUS "Arguments to execute_process: ${ARGN}")
execute_process(COMMAND
"${GIT_EXECUTABLE}"
describe
${ARGN}
WORKING_DIRECTORY
"${CMAKE_SOURCE_DIR}"
RESULT_VARIABLE
res
OUTPUT_VARIABLE
out
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(out "${out}-${res}-NOTFOUND")
endif()
set(${_var} "${out}" PARENT_SCOPE)
endfunction()
function(git_get_exact_tag _var)
git_describe(out --exact-match ${ARGN})
set(${_var} "${out}" PARENT_SCOPE)
endfunction()
function(git_rev_parse _var)
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
if(NOT GIT_FOUND)
set(${_var} "nogit" PARENT_SCOPE)
return()
endif()
execute_process(COMMAND
"${GIT_EXECUTABLE}"
rev-parse
${ARGN}
WORKING_DIRECTORY
"${CMAKE_SOURCE_DIR}"
RESULT_VARIABLE
res
OUTPUT_VARIABLE
out
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(${_var} "unknown" PARENT_SCOPE)
return()
endif()
set(${_var} "${out}" PARENT_SCOPE)
endfunction()
#
# Internal file for GetGitRevisionDescription.cmake
#
# Requires CMake 2.6 or newer (uses the 'function' command)
#
# Original Author:
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Copyright Iowa State University 2009-2010.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
set(HEAD_HASH)
file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024)
string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS)
if(HEAD_CONTENTS MATCHES "ref")
# named branch
string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}")
if(EXISTS "@GIT_DIR@/${HEAD_REF}")
configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
elseif(EXISTS "@GIT_DIR@/logs/${HEAD_REF}")
configure_file("@GIT_DIR@/logs/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
set(HEAD_HASH "${HEAD_REF}")
endif()
else()
# detached HEAD
configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY)
endif()
if(NOT HEAD_HASH)
file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024)
string(STRIP "${HEAD_HASH}" HEAD_HASH)
endif()
# CMakeLists.txt.
#
# @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
# @copyright 2018, Institute for Automation of Complex Power Systems, EONERC
# @license GNU General Public License (version 3)
#
# VILLASnode
#
# This program 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 3 of the License, or
# any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
###################################################################################
# This CMake function sets the following variables:
#
# ${PREFIX}_VERSION_STR v0.6.3
# ${PREFIX}_VERSION 0.6.3
# ${PREFIX}_MAJOR_VERSION 0
# ${PREFIX}_MINOR_VERSION 6
# ${PREFIX}_PATCH_VERSION 3
# ${PREFIX}_RELEASE 1.ci_tests_release.20180823git1cd25c2f
# ${PREFIX}_GIT_REV 1cd25c2f
# ${PREFIX}_GIT_BRANCH ci-tests
# ${PREFIX}_GIT_BRANCH_NORM ci_tests
# ${PREFIX}_VARIANT release
# ${PREFIX}_VARIANT_NORM release
# ${PREFIX}_BUILD_ID v0.6.3-1cd25c2f-release
# ${PREFIX}_BUILD_DATE 20180823
function(GetVersion DIR PREFIX)
execute_process(
COMMAND git describe --tags --abbrev=0 --match "v*"
WORKING_DIRECTORY ${DIR}
OUTPUT_VARIABLE VERSION_STR
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
RESULT_VARIABLE RC
)
if(NOT RC EQUAL 0)
message(FATAL_ERROR
"Failed to retrieve version information from Git. "
"Make sure that the source directory is a Git repo and "
"contains at least one valid tag like 'v0.1.0'"
)
endif()
string(REGEX REPLACE "^v([0-9]+\\.[0-9]+\\.[0-9]+)$" "\\1" VERSION ${VERSION_STR})
string(REGEX REPLACE "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" MAJOR_VERSION ${VERSION_STR})
string(REGEX REPLACE "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" MINOR_VERSION ${VERSION_STR})
string(REGEX REPLACE "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" PATCH_VERSION ${VERSION_STR})
string(TIMESTAMP BUILD_DATE "%Y%m%d")
if(CMAKE_BUILD_TYPE)
string(TOLOWER "${CMAKE_BUILD_TYPE}" VARIANT)
else()
set(VARIANT "release")
endif()
if(DEFINED ENV{CI})
string(APPEND VARIANT "-ci")
string(SUBSTRING $ENV{CI_COMMIT_SHA} 0 7 GIT_REV)
set(GIT_BRANCH $ENV{CI_COMMIT_REF_NAME})
else()
execute_process(
COMMAND git rev-parse --short=7 HEAD
WORKING_DIRECTORY ${DIR}
OUTPUT_VARIABLE GIT_REV
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
if(DEFINED ENV{CI_COMMIT_TAG})
set(RELEASE 1)
else()
string(REPLACE "-" "_" GIT_BRANCH_NORM ${GIT_BRANCH})
string(REPLACE "-" "_" VARIANT_NORM ${VARIANT})
set(RELEASE "1.${GIT_BRANCH_NORM}_${VARIANT_NORM}.${BUILD_DATE}git${GIT_REV}")
endif()
set(BUILD_ID "v${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}-${GIT_REV}-${VARIANT}" )
# Return results to parent scope
set(${PREFIX}_VERSION_STR ${VERSION_STR} PARENT_SCOPE)
set(${PREFIX}_VERSION ${VERSION} PARENT_SCOPE)
set(${PREFIX}_MAJOR_VERSION ${MAJOR_VERSION} PARENT_SCOPE)
set(${PREFIX}_MINOR_VERSION ${MINOR_VERSION} PARENT_SCOPE)
set(${PREFIX}_PATCH_VERSION ${PATCH_VERSION} PARENT_SCOPE)
set(${PREFIX}_RELEASE ${RELEASE} PARENT_SCOPE)
set(${PREFIX}_GIT_REV ${GIT_REV} PARENT_SCOPE)
set(${PREFIX}_GIT_BRANCH ${GIT_BRANCH} PARENT_SCOPE)
set(${PREFIX}_GIT_BRANCH_NORM ${GIT_BRANCH_NORM} PARENT_SCOPE)
set(${PREFIX}_VARIANT ${VARIANT} PARENT_SCOPE)
set(${PREFIX}_VARIANT_NORM ${VARIANT_NORM} PARENT_SCOPE)
set(${PREFIX}_BUILD_ID ${BUILD_ID} PARENT_SCOPE)
set(${PREFIX}_BUILD_DATE ${BUILD_DATE} PARENT_SCOPE)
endfunction(GetVersion)
cmake_minimum_required(VERSION 3.5)
project(DPsim CXX)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set(TOPLEVEL_PROJECT ON)
else()
set(TOPLEVEL_PROJECT OFF)
endif()
# Enable C++11 support
set(CMAKE_CXX_STANDARD 17)
add_definitions(-D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS)
......@@ -31,28 +37,9 @@ check_symbol_exists(pipe unistd.h HAVE_PIPE)
check_symbol_exists(timerfd_create sys/timerfd.h HAVE_TIMERFD)
check_symbol_exists(getopt_long getopt.h HAVE_GETOPT)
include(GetGitRevisionDescription)
git_describe(DPSIM_VERSION --tags --dirty=-dirty)
# Parse the version information into pieces.
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" DPSIM_MAJOR_VERSION "${DPSIM_VERSION}")
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" DPSIM_MINOR_VERSION "${DPSIM_VERSION}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" DPSIM_PATCH_VERSION "${DPSIM_VERSION}")
set(DPSIM_SHORT_VERSION "${DPSIM_MAJOR_VERSION}.${DPSIM_MINOR_VERSION}.${DPSIM_PATCH_VERSION}")
if(DEFINED ENV{CI})
set(GIT_HASH $ENV{CI_COMMIT_SHA})
set(GIT_BRANCH $ENV{CI_COMMIT_REF_NAME})
set(GIT_REF_SPEC $ENV{CI_COMMIT_REF_NAME})
else()
git_rev_parse(GIT_BRANCH --abbrev-ref HEAD)
get_git_head_revision(GIT_REFSPEC GIT_HASH)
endif()
string(SUBSTRING ${GIT_HASH} 0 7 GIT_HASH_SHORT)
string(TIMESTAMP YMD "%Y%m%d")
set(DPSIM_RELEASE ${YMD}git${GIT_HASH_SHORT})
# Get version info and buildid from Git
include(GetVersion)
GetVersion(${PROJECT_SOURCE_DIR} "DPSIM")
add_subdirectory(Dependencies/libcps)
......@@ -97,16 +84,26 @@ if(BUILD_EXAMPLES)
add_subdirectory(Examples)
endif(BUILD_EXAMPLES)
# Show feature summary
include(FeatureSummary)
add_feature_info(CIM WITH_CIM "Loading Common Information Model Files")
add_feature_info(Python WITH_PYTHON "Use DPsim as a Python module")
add_feature_info(Shmem WITH_SHMEM "Interface DPsim solvers via shared-memory interfaces")
add_feature_info(RT WITH_RT "Extended real-time features")
feature_summary(WHAT ALL VAR enabledFeaturesText)
message(STATUS "Feature summary for DPsim")
message(STATUS "${enabledFeaturesText}")
if(TOPLEVEL_PROJECT)
# Show feature summary
include(FeatureSummary)
add_feature_info(CIM WITH_CIM "Loading Common Information Model Files")
add_feature_info(Python WITH_PYTHON "Use DPsim as a Python module")
add_feature_info(Shmem WITH_SHMEM "Interface DPsim solvers via shared-memory interfaces")
add_feature_info(RT WITH_RT "Extended real-time features")
feature_summary(WHAT ALL VAR enabledFeaturesText)
message(STATUS "Building ${CMAKE_PROJECT_DESCRIPTION}:")
message(STATUS "Feature summary for DPsim")
message(STATUS "${enabledFeaturesText}")
message(STATUS " VERSION: ${DPSIM_VERSION}")
message(STATUS " RELEASE: ${DPSIM_RELEASE}")
message(STATUS " GIT_REV: ${DPSIM_GIT_REV}")
message(STATUS " GIT_BRANCH: ${DPSIM_GIT_BRANCH}")
message(STATUS " VARIANT: ${DPSIM_VARIANT}")
message(STATUS " BUILD_ID: ${DPSIM_BUILD_ID}")
message(STATUS " BUILD_DATE: ${DPSIM_BUILD_DATE}")
endif()
include(DPsimPackaging)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment