From 7ef2594327a640119b63effbafaad7cfa7bc07f6 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 21 Aug 2021 13:39:17 +0200 Subject: [PATCH 1/2] core/include/kernel_defines.h: add RIOT_VERSION_NUM macro --- Makefile.include | 9 +++++++++ core/include/kernel_defines.h | 28 ++++++++++++++++++++++++++++ makefiles/docker.inc.mk | 1 + 3 files changed, 38 insertions(+) diff --git a/Makefile.include b/Makefile.include index 365a3a32df15..f62a73319577 100644 --- a/Makefile.include +++ b/Makefile.include @@ -476,6 +476,8 @@ endif # set some settings useful for continuous integration builds ifeq ($(RIOT_CI_BUILD),1) RIOT_VERSION ?= buildtest + # set a dummy version number + RIOT_VERSION_CODE ?= RIOT_VERSION_NUM\(2042,5,23,0\) ifneq ($(filter $(BOARD_INSUFFICIENT_MEMORY), $(BOARD)),) $(info CI-build: skipping link step) RIOTNOLINK:=1 @@ -504,9 +506,11 @@ include $(RIOTMAKE)/cflags.inc.mk # Include VERSION for releases -include $(RIOTBASE)/VERSION +-include $(RIOTBASE)/EXTRAVERSION include $(RIOTMAKE)/git_version.inc.mk RIOT_VERSION ?= $(or $(GIT_VERSION),'UNKNOWN (builddir: $(RIOTBASE))') +RIOT_EXTRAVERSION ?= 0 # Deprecate using RIOT_VERSION_OVERRIDE but currently keep the behavior ifneq (,$(RIOT_VERSION_OVERRIDE)) @@ -514,6 +518,10 @@ ifneq (,$(RIOT_VERSION_OVERRIDE)) RIOT_VERSION = $(RIOT_VERSION_OVERRIDE) endif +# Generate machine readable RIOT VERSION macro +RIOT_VERSION_CODE ?= $(shell echo ${RIOT_VERSION} | \ + sed -E 's/([0-9]+).([0-9]+).?([0-9]+)?.*/RIOT_VERSION_NUM\\\(\1,\2,0\3,${RIOT_EXTRAVERSION}\\\)/') + # Set module by prepending APPLICATION name with 'application_'. # It prevents conflict with application and modules with the same name. APPLICATION_MODULE ?= application_$(APPLICATION) @@ -978,6 +986,7 @@ $(RIOTBUILD_CONFIG_HEADER_C).in: FORCE | $(CLEAN) CFLAGS_WITH_MACROS += $(CFLAGS) CFLAGS_WITH_MACROS += -DRIOT_VERSION=\"$(RIOT_VERSION)\" +CFLAGS_WITH_MACROS += -DRIOT_VERSION_CODE=$(RIOT_VERSION_CODE) # MODULE_NAME defines. Declared in 'makefiles/modules.inc.mk' CFLAGS_WITH_MACROS += $(EXTDEFINES) diff --git a/core/include/kernel_defines.h b/core/include/kernel_defines.h index a3bd2de98ea6..df26784ea14f 100644 --- a/core/include/kernel_defines.h +++ b/core/include/kernel_defines.h @@ -187,6 +187,34 @@ extern "C" { */ #define IS_USED(module) IS_ACTIVE(module) +/** + * @def RIOT_VERSION_NUM(major, minor, patch, extra) + * @brief Generates a 64 bit variable of a release version. + * Comparisons to this only apply to released branches + * + * To define @p extra add a file `EXTRAVERSION` to the RIOT root + * with the content + * + * RIOT_EXTRAVERSION = + * + * with `` being the number of your local version. + * This can be useful if you are maintaining a downstream + * release to base further work on. + * + * @warning This is only intended to be used with external boards or + * modules. + * In-tree code must not make use of this macro. + * + * @param[in] major Mayor version of the release + * @param[in] minor Minor version of the release + * @param[in] patch Patch level of the release + * @param[in] extra Extra version, user defined + * + * @returns A machine readable version variable + */ +#define RIOT_VERSION_NUM(major, minor, patch, extra) \ + (((0ULL + major) << 48) + ((0ULL + minor) << 32) + \ + ((0ULL + patch) << 16) + (extra)) /** * @cond INTERNAL */ diff --git a/makefiles/docker.inc.mk b/makefiles/docker.inc.mk index 2e4e61f32f38..501e0b30e1e4 100644 --- a/makefiles/docker.inc.mk +++ b/makefiles/docker.inc.mk @@ -59,6 +59,7 @@ export DOCKER_ENV_VARS += \ PROGRAMMER \ RIOT_CI_BUILD \ RIOT_VERSION \ + RIOT_VERSION_CODE \ SCANBUILD_ARGS \ SCANBUILD_OUTPUTDIR \ SIZE \ From eaf5162b5d91f968e99438438efd8f62f037a86b Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 21 Aug 2021 14:05:43 +0200 Subject: [PATCH 2/2] tests/unittests: add test for kernel_defines --- tests/unittests/tests-kernel_defines/Makefile | 1 + .../tests-kernel_defines.c | 64 +++++++++++++++++++ .../tests-kernel_defines.h | 37 +++++++++++ 3 files changed, 102 insertions(+) create mode 100644 tests/unittests/tests-kernel_defines/Makefile create mode 100644 tests/unittests/tests-kernel_defines/tests-kernel_defines.c create mode 100644 tests/unittests/tests-kernel_defines/tests-kernel_defines.h diff --git a/tests/unittests/tests-kernel_defines/Makefile b/tests/unittests/tests-kernel_defines/Makefile new file mode 100644 index 000000000000..48422e909a47 --- /dev/null +++ b/tests/unittests/tests-kernel_defines/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-kernel_defines/tests-kernel_defines.c b/tests/unittests/tests-kernel_defines/tests-kernel_defines.c new file mode 100644 index 000000000000..9714a738dfe3 --- /dev/null +++ b/tests/unittests/tests-kernel_defines/tests-kernel_defines.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2021 ML!PA Consulting GmbH + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + */ +#include "embUnit.h" +#include "kernel_defines.h" + +static void kernel_version_parse(uint64_t version, uint16_t *mayor, + uint16_t *minor, uint16_t *patch, uint16_t *extra) +{ + *mayor = version >> 48; + *minor = (version >> 32) & 0xFFFF; + *patch = (version >> 16) & 0xFFFF; + *extra = (version >> 0) & 0xFFFF; +} + +static void test_kernel_version(void) +{ + uint16_t mayor, minor, patch, extra; + + TEST_ASSERT(RIOT_VERSION_CODE); + TEST_ASSERT(RIOT_VERSION_CODE > RIOT_VERSION_NUM(2017, 1, 3, 0)); + TEST_ASSERT(RIOT_VERSION_CODE < RIOT_VERSION_NUM(2080, 10, 2, 0)); + + kernel_version_parse(RIOT_VERSION_NUM(2020, 7, 1, 0), + &mayor, &minor, &patch, &extra); + + TEST_ASSERT_EQUAL_INT(2020, mayor); + TEST_ASSERT_EQUAL_INT(7, minor); + TEST_ASSERT_EQUAL_INT(1, patch); + TEST_ASSERT_EQUAL_INT(0, extra); +#if RIOT_VERSION_CODE < RIOT_VERSION_NUM(2000, 1, 1, 0) + TEST_ASSERT(0); +#endif +#if RIOT_VERSION_CODE > RIOT_VERSION_NUM(2100, 1, 1, 1) + TEST_ASSERT(0); +#endif +} + +Test *tests_kernel_defines_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_kernel_version), + }; + + EMB_UNIT_TESTCALLER(kernel_defines_tests, NULL, NULL, fixtures); + + return (Test *)&kernel_defines_tests; +} + +void tests_kernel_defines(void) +{ + TESTS_RUN(tests_kernel_defines_tests()); +} +/** @} */ diff --git a/tests/unittests/tests-kernel_defines/tests-kernel_defines.h b/tests/unittests/tests-kernel_defines/tests-kernel_defines.h new file mode 100644 index 000000000000..41dd2d0cba84 --- /dev/null +++ b/tests/unittests/tests-kernel_defines/tests-kernel_defines.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2021 ML!PA Consulting GmbH + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @addtogroup unittests + * @{ + * + * @file + * @brief Unittests for the ``kernel_defines`` module + * + * @author Benjamin Valentin + */ +#ifndef TESTS_KERNEL_DEFINES_H +#define TESTS_KERNEL_DEFINES_H + +#include "embUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief The entry point of this test suite. + */ +void tests_kernel_defines(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_KERNEL_DEFINES_H */ +/** @} */