From 48d121653b545df4920f278c30016886dacc06de Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Wed, 24 Jan 2024 12:13:32 +0800 Subject: [PATCH] feat(linux): added fls() on Linux --- components/linux/CMakeLists.txt | 6 ++- components/linux/fls.c | 18 +++++++ components/linux/linux_include/strings.h | 23 +++++++++ .../linux/test_apps/.build-test-rules.yml | 6 +++ .../linux/test_apps/linux_test/CMakeLists.txt | 9 ++++ .../linux/test_apps/linux_test/README.md | 2 + .../test_apps/linux_test/main/CMakeLists.txt | 3 ++ .../test_apps/linux_test/main/linux_test.c | 51 +++++++++++++++++++ .../test_apps/linux_test/pytest_linux_test.py | 12 +++++ .../test_apps/linux_test/sdkconfig.defaults | 1 + tools/ci/check_copyright_config.yaml | 7 +++ 11 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 components/linux/fls.c create mode 100644 components/linux/linux_include/strings.h create mode 100644 components/linux/test_apps/.build-test-rules.yml create mode 100644 components/linux/test_apps/linux_test/CMakeLists.txt create mode 100644 components/linux/test_apps/linux_test/README.md create mode 100644 components/linux/test_apps/linux_test/main/CMakeLists.txt create mode 100644 components/linux/test_apps/linux_test/main/linux_test.c create mode 100644 components/linux/test_apps/linux_test/pytest_linux_test.py create mode 100644 components/linux/test_apps/linux_test/sdkconfig.defaults diff --git a/components/linux/CMakeLists.txt b/components/linux/CMakeLists.txt index c3b72b56bc..0bb5d4918c 100644 --- a/components/linux/CMakeLists.txt +++ b/components/linux/CMakeLists.txt @@ -3,10 +3,14 @@ if(NOT "${target}" STREQUAL "linux") return() endif() +set(includes "include") if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") list(APPEND srcs getrandom.c assert_func.c) +else() + list(APPEND srcs fls.c) + list(APPEND includes "linux_include") endif() -idf_component_register(INCLUDE_DIRS include +idf_component_register(INCLUDE_DIRS ${includes} REQUIRED_IDF_TARGETS linux SRCS ${srcs}) diff --git a/components/linux/fls.c b/components/linux/fls.c new file mode 100644 index 0000000000..d136926e2d --- /dev/null +++ b/components/linux/fls.c @@ -0,0 +1,18 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved. + * + * SPDX-License-Identifier: BSD-4-Clause-UC + * + * SPDX-FileContributor: 2024 Espressif Systems (Shanghai) CO LTD + */ + +#include "strings.h" + +int fls(unsigned int mask) +{ + if (mask == 0) { + return (0); + } + + return (sizeof(mask) << 3) - __builtin_clz(mask); +} diff --git a/components/linux/linux_include/strings.h b/components/linux/linux_include/strings.h new file mode 100644 index 0000000000..f8f4a15b19 --- /dev/null +++ b/components/linux/linux_include/strings.h @@ -0,0 +1,23 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* + * This is a helper include for fls(), which is present in the ESP-IDF toolchains and MacOS, but not on Linux. + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +int fls(unsigned int value); + +#ifdef __cplusplus +} +#endif + +#include_next diff --git a/components/linux/test_apps/.build-test-rules.yml b/components/linux/test_apps/.build-test-rules.yml new file mode 100644 index 0000000000..e88e6fc25b --- /dev/null +++ b/components/linux/test_apps/.build-test-rules.yml @@ -0,0 +1,6 @@ +# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps + +components/linux/test_apps/linux_test: + enable: + - if: IDF_TARGET in ["linux"] + reason: Test app is specifically for the linux target diff --git a/components/linux/test_apps/linux_test/CMakeLists.txt b/components/linux/test_apps/linux_test/CMakeLists.txt new file mode 100644 index 0000000000..6697ac02d1 --- /dev/null +++ b/components/linux/test_apps/linux_test/CMakeLists.txt @@ -0,0 +1,9 @@ +# For more information about build system see +# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(COMPONENTS main) +project(linux_test) diff --git a/components/linux/test_apps/linux_test/README.md b/components/linux/test_apps/linux_test/README.md new file mode 100644 index 0000000000..37c142df16 --- /dev/null +++ b/components/linux/test_apps/linux_test/README.md @@ -0,0 +1,2 @@ +| Supported Targets | Linux | +| ----------------- | ----- | diff --git a/components/linux/test_apps/linux_test/main/CMakeLists.txt b/components/linux/test_apps/linux_test/main/CMakeLists.txt new file mode 100644 index 0000000000..8f17817cc3 --- /dev/null +++ b/components/linux/test_apps/linux_test/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "linux_test.c" + INCLUDE_DIRS "." + PRIV_REQUIRES unity) diff --git a/components/linux/test_apps/linux_test/main/linux_test.c b/components/linux/test_apps/linux_test/main/linux_test.c new file mode 100644 index 0000000000..71731d5f43 --- /dev/null +++ b/components/linux/test_apps/linux_test/main/linux_test.c @@ -0,0 +1,51 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "unity.h" +#include "unity_test_runner.h" +#include "unity_test_utils_memory.h" +#include "esp_log.h" +#include + +static const char* TAG = "event_test_app"; + +void setUp(void) +{ + // If heap tracing is enabled in kconfig, leak trace the test + unity_utils_set_leak_level(0); + unity_utils_record_free_mem(); +} + +void tearDown(void) +{ + unity_utils_evaluate_leaks(); +} + +TEST_CASE("fls(0) = 0", "[fls]") +{ + TEST_ASSERT_EQUAL(0, fls(0)); +} + +TEST_CASE("fls(1) = 1", "[fls]") +{ + TEST_ASSERT_EQUAL(1, fls(1)); +} + +TEST_CASE("fls(0xF000) = 16", "[fls]") +{ + TEST_ASSERT_EQUAL(16, fls(0xF000)); +} + +TEST_CASE("fls(0x80000000) = 32", "[fls]") +{ + TEST_ASSERT_EQUAL(32, fls(0x80000000)); +} + +void app_main(void) +{ + ESP_LOGI(TAG, "Running linux test app"); + unity_run_menu(); +} diff --git a/components/linux/test_apps/linux_test/pytest_linux_test.py b/components/linux/test_apps/linux_test/pytest_linux_test.py new file mode 100644 index 0000000000..f0e77455b1 --- /dev/null +++ b/components/linux/test_apps/linux_test/pytest_linux_test.py @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: CC0-1.0 +import pytest +from pytest_embedded_idf.dut import IdfDut + + +@pytest.mark.linux +@pytest.mark.host_test +def test_linux_component(dut: IdfDut) -> None: + dut.expect_exact('Press ENTER to see the list of tests.') + dut.write('![ignore]') + dut.expect(r'\d{1} Tests 0 Failures 0 Ignored', timeout=10) diff --git a/components/linux/test_apps/linux_test/sdkconfig.defaults b/components/linux/test_apps/linux_test/sdkconfig.defaults new file mode 100644 index 0000000000..9b39f10b99 --- /dev/null +++ b/components/linux/test_apps/linux_test/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_IDF_TARGET="linux" diff --git a/tools/ci/check_copyright_config.yaml b/tools/ci/check_copyright_config.yaml index 9f80f8c529..0e505ee603 100644 --- a/tools/ci/check_copyright_config.yaml +++ b/tools/ci/check_copyright_config.yaml @@ -104,6 +104,13 @@ ethernet_component: - Apache-2.0 - MIT # To allow contributed drivers +linux_component: + include: + - 'components/linux/fls.c' + allowed_licenses: + - Apache-2.0 + - BSD-4-Clause-UC + systemview: include: - 'components/app_trace/sys_view'