From 62957ec09f360d426059bbc44a533529865bd025 Mon Sep 17 00:00:00 2001 From: Gregory Shue Date: Thu, 20 May 2021 18:49:09 -0700 Subject: [PATCH] lib: add custom_lib Enhance the example-application repository with a configurable, trivial library example and associated test cases. . This implementation appears to make no assumptions. . This implementation was verified by running the following commands in an example-application workspace and verifying all tests passed. 1. `west build -b native_posix -p always example-application/tests/lib/custom_lib/` 2. `./build/zephyr/zephyr.exe` 3. `west build -b native_posix -p always example-application/tests/lib/custom_lib/ -- -DCONFIG_CUSTOM_LIB_GET_VALUE_DEFAULT=6` 4. `./build/zephyr/zephyr.exe` 5. `zephyr/scripts/twister -T example-application/tests/ \ -p qemu_cortex_m0` 6. `cd zephyr/doc && make clean && make` built cleanly. . Note that `twister` does not follow the `zephyr/module.yml:tests` setting to discover tests in modules, so the testcase-root must be explicitly provided. Fixes #35177 Signed-off-by: Gregory Shue --- .github/workflows/build.yml | 5 +++ CMakeLists.txt | 2 ++ include/custom_lib/custom_lib.h | 24 +++++++++++++++ lib/CMakeLists.txt | 5 +-- lib/Kconfig | 2 ++ lib/custom_lib/CMakeLists.txt | 5 +++ lib/custom_lib/Kconfig | 19 ++++++++++++ lib/custom_lib/custom_lib.c | 13 ++++++++ tests/lib/custom_lib/CMakeLists.txt | 9 ++++++ tests/lib/custom_lib/prj.conf | 2 ++ tests/lib/custom_lib/src/main.c | 47 +++++++++++++++++++++++++++++ tests/lib/custom_lib/testcase.yaml | 7 +++++ 12 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 include/custom_lib/custom_lib.h create mode 100644 lib/custom_lib/CMakeLists.txt create mode 100644 lib/custom_lib/Kconfig create mode 100644 lib/custom_lib/custom_lib.c create mode 100644 tests/lib/custom_lib/CMakeLists.txt create mode 100644 tests/lib/custom_lib/prj.conf create mode 100644 tests/lib/custom_lib/src/main.c create mode 100644 tests/lib/custom_lib/testcase.yaml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7c875f4..22fa191 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,6 +27,11 @@ jobs: run: | west build -b custom_plank app + - name: Twister Unit Tests + working-directory: example-application + run: | + ../zephyr/scripts/twister -p qemu_cortex_m0 --testcase-root ./tests/ + - name: Archive firmware uses: actions/upload-artifact@v2 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index 704be97..b8b7f59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,5 +4,7 @@ # This CMake file is picked by the Zephyr build system because it is defined # as the module CMake entry point (see zephyr/module.yml). +zephyr_include_directories(include) + add_subdirectory(drivers) add_subdirectory(lib) diff --git a/include/custom_lib/custom_lib.h b/include/custom_lib/custom_lib.h new file mode 100644 index 0000000..cf02d90 --- /dev/null +++ b/include/custom_lib/custom_lib.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021, Legrand North America, LLC. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef EXAMPLE_APPLICATION_INCLUDE_CUSTOM_LIB_CUSTOM_LIB_H_ +#define EXAMPLE_APPLICATION_INCLUDE_CUSTOM_LIB_CUSTOM_LIB_H_ + +/** + * @brief Return parameter if non-zero, or Kconfig-controlled default + * + * Function returns the provided value if non-zero, or a Kconfig-controlled + * default value if the parameter is zero. This trivial function is + * provided in order to have a library interface example that is trivial + * to test. + * + * @param return_value_if_nonzero Value to return if non-zero + * @returns return_value_if_nonzero when the parameter is non-zero + * @returns CONFIG_CUSTOM_LIB_GET_VALUE_DEFAULT if parameter is zero + */ +int custom_lib_get_value(int return_value_if_nonzero); + +#endif /* EXAMPLE_APPLICATION_INCLUDE_CUSTOM_LIB_CUSTOM_LIB_H_ */ diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 9ed8728..d24bef0 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,6 +1,3 @@ # SPDX-License-Identifier: Apache-2.0 -# The visibility and compilation of libraries may be controlled -# with a Kconfig setting as follows: -# -# add_subdirectory_ifdef(CONFIG_CUSTOM_LIB custom_lib) +add_subdirectory_ifdef(CONFIG_CUSTOM_LIB custom_lib) diff --git a/lib/Kconfig b/lib/Kconfig index a84752d..5fd3678 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -3,4 +3,6 @@ menu "Libraries" +rsource "custom_lib/Kconfig" + endmenu diff --git a/lib/custom_lib/CMakeLists.txt b/lib/custom_lib/CMakeLists.txt new file mode 100644 index 0000000..d683936 --- /dev/null +++ b/lib/custom_lib/CMakeLists.txt @@ -0,0 +1,5 @@ +# Copyright (c) 2021, Legrand North America, LLC. +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library() +zephyr_library_sources(custom_lib.c) diff --git a/lib/custom_lib/Kconfig b/lib/custom_lib/Kconfig new file mode 100644 index 0000000..ada0c6e --- /dev/null +++ b/lib/custom_lib/Kconfig @@ -0,0 +1,19 @@ +# Copyright (c) 2021, Legrand North America, LLC. +# SPDX-License-Identifier: Apache-2.0 + +config CUSTOM_LIB + bool "custom_lib Support" + help + This option enables the custom_lib library + +config CUSTOM_LIB_GET_VALUE_DEFAULT + int "custom_lib_get_value() default return value" + depends on CUSTOM_LIB + default 0 + help + This option primarily exists as an example of a library Kconfig + setting. + + This option specifies the value for custom_lib_get_value() + to return when the input parameter is zero. (Otherwise + the function returns the input parameter value.) diff --git a/lib/custom_lib/custom_lib.c b/lib/custom_lib/custom_lib.c new file mode 100644 index 0000000..6711cc4 --- /dev/null +++ b/lib/custom_lib/custom_lib.c @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2021, Legrand North America, LLC. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +int custom_lib_get_value(int return_value_if_nonzero) +{ + return (return_value_if_nonzero != 0) ? return_value_if_nonzero + : CONFIG_CUSTOM_LIB_GET_VALUE_DEFAULT; +} diff --git a/tests/lib/custom_lib/CMakeLists.txt b/tests/lib/custom_lib/CMakeLists.txt new file mode 100644 index 0000000..d3b7fcf --- /dev/null +++ b/tests/lib/custom_lib/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (c) 2021, Legrand North America, LLC. +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(custom_lib) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/lib/custom_lib/prj.conf b/tests/lib/custom_lib/prj.conf new file mode 100644 index 0000000..f70c400 --- /dev/null +++ b/tests/lib/custom_lib/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_CUSTOM_LIB=y diff --git a/tests/lib/custom_lib/src/main.c b/tests/lib/custom_lib/src/main.c new file mode 100644 index 0000000..16c1681 --- /dev/null +++ b/tests/lib/custom_lib/src/main.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 Legrand North America, LLC. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* + * @file test custom_lib library + * + * This suite verifies that the methods provided with the custom_lib + * library works correctly. + */ + +#include +#include +#include + +static void test_get_value(void) +{ + /* Verify standard behavior */ + zassert_equal(custom_lib_get_value(INT_MIN), INT_MIN, + "get_value failed input of INT_MIN"); + zassert_equal(custom_lib_get_value(INT_MIN + 1), INT_MIN + 1, + "get_value failed input of INT_MIN + 1"); + zassert_equal(custom_lib_get_value(-1), -1, + "get_value failed input of -1"); + zassert_equal(custom_lib_get_value(1), 1, + "get_value failed input of 1"); + zassert_equal(custom_lib_get_value(INT_MAX - 1), INT_MAX - 1, + "get_value failed input of INT_MAX - 1"); + zassert_equal(custom_lib_get_value(INT_MAX), INT_MAX, + "get_value failed input of INT_MAX"); + + /* Verify override behavior */ + zassert_equal(custom_lib_get_value(0), + CONFIG_CUSTOM_LIB_GET_VALUE_DEFAULT, + "get_value failed input of 0"); +} + +void test_main(void) +{ + ztest_test_suite(lib_custom_lib_test, + ztest_unit_test(test_get_value) + ); + + ztest_run_test_suite(lib_custom_lib_test); +} diff --git a/tests/lib/custom_lib/testcase.yaml b/tests/lib/custom_lib/testcase.yaml new file mode 100644 index 0000000..7f2c5a2 --- /dev/null +++ b/tests/lib/custom_lib/testcase.yaml @@ -0,0 +1,7 @@ +common: + tags: extensibility +tests: + lib.custom_lib: + build_only: false # something benign to make a non-empty list + lib.custom_lib.non_default: + extra_args: CONFIG_CUSTOM_LIB_GET_VALUE_DEFAULT=6