From 01f502b753baf8881a3d1fa166dab443f4bac986 Mon Sep 17 00:00:00 2001 From: Jens Wetterich Date: Tue, 4 Jan 2022 09:48:28 +0100 Subject: [PATCH] pkg/etl: Add the embedded template library (etl) --- pkg/Kconfig | 1 + pkg/etl/Kconfig | 16 +++++++++ pkg/etl/Makefile | 9 +++++ pkg/etl/Makefile.dep | 1 + pkg/etl/Makefile.include | 35 +++++++++++++++++++ pkg/etl/config/etl_profile.h | 54 +++++++++++++++++++++++++++++ pkg/etl/doc.txt | 12 +++++++ tests/pkg_etl/Makefile | 9 +++++ tests/pkg_etl/Makefile.ci | 5 +++ tests/pkg_etl/app.config.test | 1 + tests/pkg_etl/main.cpp | 64 +++++++++++++++++++++++++++++++++++ tests/pkg_etl/tests/01-run.py | 19 +++++++++++ 12 files changed, 226 insertions(+) create mode 100644 pkg/etl/Kconfig create mode 100644 pkg/etl/Makefile create mode 100644 pkg/etl/Makefile.dep create mode 100644 pkg/etl/Makefile.include create mode 100644 pkg/etl/config/etl_profile.h create mode 100644 pkg/etl/doc.txt create mode 100644 tests/pkg_etl/Makefile create mode 100644 tests/pkg_etl/Makefile.ci create mode 100644 tests/pkg_etl/app.config.test create mode 100644 tests/pkg_etl/main.cpp create mode 100755 tests/pkg_etl/tests/01-run.py diff --git a/pkg/Kconfig b/pkg/Kconfig index f396515be3..ee53c704c7 100644 --- a/pkg/Kconfig +++ b/pkg/Kconfig @@ -20,6 +20,7 @@ rsource "emlearn/Kconfig" rsource "esp32_sdk/Kconfig" rsource "esp32_sdk_libs/Kconfig" rsource "esp8266_sdk/Kconfig" +rsource "etl/Kconfig" rsource "fff/Kconfig" rsource "gecko_sdk/Kconfig" rsource "gemmlowp/Kconfig" diff --git a/pkg/etl/Kconfig b/pkg/etl/Kconfig new file mode 100644 index 0000000000..ae2f59599a --- /dev/null +++ b/pkg/etl/Kconfig @@ -0,0 +1,16 @@ +# Copyright (c) 2022 Jens Wetterich +# +# 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. +# + +config PACKAGE_ETL + bool "Embedded Template Library" + depends on TEST_KCONFIG + depends on HAS_CPP + select MODULE_CPP + help + This is a drop-in replacement for the C++ standard library with fixed-size containers. + It does not rely on dynamic memory. It also provides type-traits for boards without standard library. + See the website for more details. diff --git a/pkg/etl/Makefile b/pkg/etl/Makefile new file mode 100644 index 0000000000..d95194509b --- /dev/null +++ b/pkg/etl/Makefile @@ -0,0 +1,9 @@ +PKG_NAME=etl +PKG_URL=https://github.com/ETLCPP/etl +PKG_VERSION=734df1a3725f9eeab3048a2a18cb84e3a82fbd8f # v20.24.1 +PKG_LICENSE=MIT + +include $(RIOTBASE)/pkg/pkg.mk + +all: + @: diff --git a/pkg/etl/Makefile.dep b/pkg/etl/Makefile.dep new file mode 100644 index 0000000000..e355de5d71 --- /dev/null +++ b/pkg/etl/Makefile.dep @@ -0,0 +1 @@ +FEATURES_REQUIRED += cpp diff --git a/pkg/etl/Makefile.include b/pkg/etl/Makefile.include new file mode 100644 index 0000000000..a8ae45fa15 --- /dev/null +++ b/pkg/etl/Makefile.include @@ -0,0 +1,35 @@ +INCLUDES += -I$(PKGDIRBASE)/etl/include +INCLUDES += -I$(RIOTPKG)/etl/config + +# There's nothing to build in this package, it's used as a header only library. +# So it's declared as a pseudo-module +PSEUDOMODULES += etl + +# Activate the usage of libstdcpp types and features if available +# This prevents unnecessary reimplementations +ifneq (,$(filter libstdcpp,$(FEATURES_USED))) + CXXEXFLAGS += -DFEATURE_LIBSTDCPP +# Kconfig alternative +else ifdef CONFIG_KCONFIG_HAS_LIBSTDCPP + CXXEXFLAGS += -DFEATURE_LIBSTDCPP +endif + +# Some boards don't provide NAN or by default don't define limits like UINT8_MAX in CPP context +# This activates the appropriate workarounds +ifneq (,$(filter arch_avr8,$(FEATURES_USED))) + CXXEXFLAGS += -DNO_CPP_NAN_SUPPORT + CXXEXFLAGS += -D__STDC_LIMIT_MACROS +else ifneq (,$(filter arch_riscv,$(FEATURES_USED))) + CXXEXFLAGS += -DNO_CPP_NAN_SUPPORT + CXXEXFLAGS += -D__STDC_LIMIT_MACROS +# Kconfig alternatives +else ifdef CONFIG_CPU_ARCH_AVR8 + CXXEXFLAGS += -DNO_CPP_NAN_SUPPORT + CXXEXFLAGS += -D__STDC_LIMIT_MACROS +else ifdef CONFIG_CPU_ARCH_RISCV + CXXEXFLAGS += -DNO_CPP_NAN_SUPPORT + CXXEXFLAGS += -D__STDC_LIMIT_MACROS +endif + +# Some compilers generate this warning. Due to -Werror it prevents this header-only library from compiling when included +CXXEXFLAGS += -Wno-unused-function diff --git a/pkg/etl/config/etl_profile.h b/pkg/etl/config/etl_profile.h new file mode 100644 index 0000000000..97fe2d23d9 --- /dev/null +++ b/pkg/etl/config/etl_profile.h @@ -0,0 +1,54 @@ +/* +* Copyright (C) 2022 Jens Wetterich +* +* 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. +*/ + +/** +* @ingroup pkg_etl +* @brief +* @{ +* +* @file +* @brief Common settings for etl +* @details This activates the usage of the STL if available and/or the usage of + workarounds for missing NAN macros. +* This is configured by compiler defines the in Makefile.include +* @author Jens Wetterich +*/ +#ifndef ETL_PROFILE_H +#define ETL_PROFILE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef FEATURE_LIBSTDCPP +/** + * @internal + * @brief Deactivate the usage of the stdlib in ETL + */ +#define ETL_NO_STL +#endif + +#ifdef NO_CPP_NAN_SUPPORT +/** + * @internal + * @brief Activate the ETL workarounds when NAN macros are not supported + */ +#define ETL_NO_CPP_NAN_SUPPORT +#endif + +/** + * @internal + * @brief Activate the checking of bounds in push and pop operations in ETL containers + */ +#define ETL_CHECK_PUSH_POP + +#ifdef __cplusplus +} +#endif +#endif /* ETL_PROFILE_H */ +/** @} */ diff --git a/pkg/etl/doc.txt b/pkg/etl/doc.txt new file mode 100644 index 0000000000..3419f3099f --- /dev/null +++ b/pkg/etl/doc.txt @@ -0,0 +1,12 @@ +/** + * @defgroup pkg_etl Embedded Template Library + * @ingroup cpp + * @ingroup pkg + * @brief Embedded Template Library (etl) + * @details This is a drop-in replacement for the C++ standard library with fixed-size containers. + * It does not rely on dynamic memory. It also provides type-traits for boards without standard library. + * See the website for more details. + * + * @see https://www.etlcpp.com + * @see https://github.com/ETLCPP/etl + */ diff --git a/tests/pkg_etl/Makefile b/tests/pkg_etl/Makefile new file mode 100644 index 0000000000..072bc8e91b --- /dev/null +++ b/tests/pkg_etl/Makefile @@ -0,0 +1,9 @@ +include ../Makefile.tests_common +USEPKG += etl + +# Kconfig build is broken due to invalid module resolution with Kconfig for this board +BOARD_BLACKLIST := \ + 6lowpan-clicker \ + # + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg_etl/Makefile.ci b/tests/pkg_etl/Makefile.ci new file mode 100644 index 0000000000..31931b03b6 --- /dev/null +++ b/tests/pkg_etl/Makefile.ci @@ -0,0 +1,5 @@ +BOARD_INSUFFICIENT_MEMORY := \ + nucleo-l011k4 \ + stm32f030f4-demo \ + samd10-xmini \ + # diff --git a/tests/pkg_etl/app.config.test b/tests/pkg_etl/app.config.test new file mode 100644 index 0000000000..2e0058b64d --- /dev/null +++ b/tests/pkg_etl/app.config.test @@ -0,0 +1 @@ +CONFIG_PACKAGE_ETL=y diff --git a/tests/pkg_etl/main.cpp b/tests/pkg_etl/main.cpp new file mode 100644 index 0000000000..1b8b0ed628 --- /dev/null +++ b/tests/pkg_etl/main.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2022 Jens Wetterich + * + * 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. + */ + +/** + * @ingroup tests + * @{ + * @file + * @brief Unit test to test the usage of @ref pkg_etl package + * @author Jens Wetterich + */ +#include "etl/string.h" +#include "etl/to_string.h" +#include "etl/type_traits.h" +#include "etl/vector.h" +#include + +int main() { + puts("Testing etl"); + + /* Create test string */ + etl::string<100> teststr = "Test: is_same: "; + + /* Append some type_trait tests */ + etl::to_string(etl::is_same::value, teststr, true); + teststr += ", is_unsigned: "; + etl::to_string(etl::is_unsigned::value, teststr, true); + teststr += ", vector: "; + + /* Do some vector testing and append to the string */ + etl::vector testvec; + testvec.push_back('a'); + testvec.push_back('b'); + testvec.clear(); + testvec.push_back('c'); + testvec.push_back('d'); + testvec.push_back('e'); + testvec.push_back('a'); + testvec.push_back('b'); + teststr += testvec[0]; + teststr += testvec[1]; + teststr += testvec[2]; + teststr += testvec[3]; + teststr += testvec[4]; + teststr += ", size: "; + + /* Test string size */ + etl::to_string(teststr.size(), teststr, true); + + /* Test string compare */ + if (teststr == "Test: is_same: 1, is_unsigned: 0, vector: cdeab, size: 55") { + teststr += " -> valid"; + } else { + teststr += " -> false"; + } + + puts(teststr.c_str()); + return 0; +} +/** @} */ diff --git a/tests/pkg_etl/tests/01-run.py b/tests/pkg_etl/tests/01-run.py new file mode 100755 index 0000000000..7a570a875b --- /dev/null +++ b/tests/pkg_etl/tests/01-run.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2022 Jens Wetterich +# +# 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. + +import sys +from testrunner import run + + +def testfunc(child): + child.expect_exact("Test: is_same: 1, is_unsigned: 0, vector: cdeab, size: 55 -> valid") + print("All tests successful") + + +if __name__ == "__main__": + sys.exit(run(testfunc, timeout=1, echo=True, traceback=True))