Commit 535e8728 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

cmake: improve version detection and generation of config.h

parent 2af4cc8d
......@@ -27,26 +27,58 @@ project(villas-common
LANGUAGES C CXX
)
# Some more project settings
set(PROJECT_AUTHOR "Steffen Vogel")
set(PROJECT_COPYRIGHT "2018, Institute for Automation of Complex Power Systems, RWTH Aachen University")
set(PROJECT_HOMEPAGE_URL "https://git.rwth-aachen.de/acs/public/villas/VILLAScommon")
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
set(TOPLEVEL_PROJECT ON)
else()
set(TOPLEVEL_PROJECT OFF)
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
add_definitions(-D__STDC_FORMAT_MACROS -D_GNU_SOURCE)
include(FindPkgConfig)
include(FeatureSummary)
include(GetVersion)
# Get version info and buildid from Git
if(TOPLEVEL_PROJECT)
GetVersion(${PROJECT_SOURCE_DIR} "CMAKE_PROJECT")
endif()
# Check packages
find_package(OpenSSL 1.0.0 REQUIRED)
find_package(CURL 7.29 REQUIRED)
find_package(Criterion)
include(FindPkgConfig)
pkg_check_modules(JANSSON IMPORTED_TARGET jansson)
pkg_check_modules(JANSSON IMPORTED_TARGET REQUIRED jansson>=2.7)
add_subdirectory(lib)
add_subdirectory(tests)
if(CRITERION_FOUND)
add_subdirectory(tests)
endif()
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/include/villas/config.h.in
${CMAKE_CURRENT_BINARY_DIR}/include/villas/config.h
)
if(TOPLEVEL_PROJECT)
feature_summary(WHAT ALL VAR FEATURES)
message(STATUS "${FEATURES}")
message(STATUS "Building ${PROJECT_DESCRIPTION}:")
message(STATUS " VERSION: ${CMAKE_PROJECT_VERSION}")
message(STATUS " RELEASE: ${CMAKE_PROJECT_RELEASE}")
message(STATUS " GIT_REV: ${CMAKE_PROJECT_GIT_REV}")
message(STATUS " GIT_BRANCH: ${CMAKE_PROJECT_GIT_BRANCH}")
message(STATUS " VARIANT: ${CMAKE_PROJECT_VARIANT}")
message(STATUS " BUILD_ID: ${CMAKE_PROJECT_BUILD_ID}")
message(STATUS " BUILD_DATE: ${CMAKE_PROJECT_BUILD_DATE}")
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)
......@@ -30,36 +30,33 @@
#define V 2
#endif
#define PROJECT_URL "@CMAKE_PROJECT_HOMEPAGE_URL@"
#define PROJECT_NAME "@CMAKE_PROJECT_DESCRIPTION@"
#define PROJECT_VERSION_STR "@CMAKE_PROJECT_VERSION_STR@"
#define PROJECT_VERSION "@CMAKE_PROJECT_VERSION@"
#define PROJECT_MAJOR_VERSION @CMAKE_PROJECT_MAJOR_VERSION@
#define PROJECT_MINOR_VERSION @CMAKE_PROJECT_MINOR_VERSION@
#define PROJECT_PATCH_VERSION @CMAKE_PROJECT_PATCH_VERSION@
#define PROJECT_RELEASE "@CMAKE_PROJECT_RELEASE@"
#define PROJECT_GIT_REV "@CMAKE_PROJECT_GIT_REV@"
#define PROJECT_GIT_BRANCH "@CMAKE_PROJECT_GIT_BRANCH@"
#define PROJECT_GIT_BRANCH_NORM "@CMAKE_PROJECT_GIT_BRANCH_NORM@"
#define PROJECT_VARIANT "@CMAKE_PROJECT_VARIANT@"
#define PROJECT_VARIANT_NORM "@CMAKE_PROJECT_VARIANT_NORM@"
#define PROJECT_BUILD_ID "@CMAKE_PROJECT_BUILD_ID@"
#define PROJECT_BUILD_DATE "@CMAKE_PROJECT_BUILD_DATE@"
#define PROJECT_HOMEPAGE_URL "@CMAKE_PROJECT_HOMEPAGE_URL@"
#define PROJECT_NAME "@CMAKE_PROJECT_DESCRIPTION@"
#define USER_AGENT "@CMAKE_PROJECT_DESCRIPTION@ (@CMAKE_PROJECT_BUILD_ID@)"
/* Available Libraries */
#cmakedefine LIBCONFIG_FOUND
/* Paths */
#define PLUGIN_PATH PREFIX "/share/villas/node/plugins"
#define WEB_PATH PREFIX "/share/villas/node/web"
#define PLUGIN_PATH "@CMAKE_INSTALL_PREFIX@/share/villas/node/plugins"
#define WEB_PATH "@CMAKE_INSTALL_PREFIX@/share/villas/node/web"
#define SYSFS_PATH "/sys"
#define PROCFS_PATH "/proc"
/** Default number of values in a sample */
#define DEFAULT_SAMPLELEN 64
#define DEFAULT_QUEUELEN 1024
/** Number of hugepages which are requested from the the kernel.
* @see https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt */
#define DEFAULT_NR_HUGEPAGES 100
/** Width of log output in characters */
#define LOG_WIDTH 80
#define LOG_HEIGHT 25
/** Socket priority */
#define SOCKET_PRIO 7
/* Protocol numbers */
#define IPPROTO_VILLAS 137
#define ETH_P_VILLAS 0xBABE
#define USER_AGENT PROJECT_NAME " (" BUILDID ")"
/* Required kernel version */
#define KERNEL_VERSION_MAJ 3
#define KERNEL_VERSION_MIN 6
......@@ -67,7 +67,6 @@ target_link_libraries(villas-common PUBLIC
${CMAKE_DL_LIBS}
)
target_compile_definitions(villas-common PRIVATE
BUILDID=\"abc\"
_GNU_SOURCE
target_compile_definitions(villas-common PUBLIC
-D__STDC_FORMAT_MACROS -D_GNU_SOURCE
)
......@@ -39,14 +39,14 @@ pthread_t main_thread;
void print_copyright()
{
printf(PROJECT_NAME " %s (built on %s %s)\n",
CLR_BLU(BUILDID), CLR_MAG(__DATE__), CLR_MAG(__TIME__));
CLR_BLU(PROJECT_BUILD_ID), CLR_MAG(__DATE__), CLR_MAG(__TIME__));
printf(" Copyright 2014-2017, Institute for Automation of Complex Power Systems, EONERC\n");
printf(" Steffen Vogel <StVogel@eonerc.rwth-aachen.de>\n");
}
void print_version()
{
printf("%s\n", BUILDID);
printf("%s\n", PROJECT_BUILD_ID);
}
int version_parse(const char *s, struct version *v)
......
......@@ -20,10 +20,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##############################################################################
set(SOURCES
add_executable(unit-tests-common
main.cpp
logging.cpp
advio.cpp
bitset.cpp
graph.cpp
......@@ -38,10 +38,6 @@ set(SOURCES
utils.cpp
)
add_executable(unit-tests-common ${SOURCES})
find_package(Criterion REQUIRED)
target_include_directories(unit-tests-common PUBLIC
${PROJECT_SOURCE_DIR}/include
${CRITERION_INCLUDE_DIRS}
......@@ -51,3 +47,8 @@ target_link_libraries(unit-tests-common PUBLIC
villas-common
${CRITERION_LIBRARIES}
)
add_custom_target(run-unit-tests-common
COMMAND $<TARGET_FILE:unit-tests-common> ${CRITERION_OPTS}
USES_TERMINAL
)
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