From 2240cc5854fb2422b2f4e5d1cdd666c6b9617e4d Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 7 Nov 2022 13:50:51 -0800 Subject: [PATCH 1/7] cmake/gcc: Don't use -nostdinc with toolchain picolibc When using picolibc from the toolchain, we need to use the standard include paths to make sure the library headers are found, especially for libstdc++. Add toolchain picolibc to the list of cases for which this is the case. Signed-off-by: Keith Packard --- cmake/compiler/gcc/compiler_flags.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/compiler/gcc/compiler_flags.cmake b/cmake/compiler/gcc/compiler_flags.cmake index 2dbb1e3e08eae..cfa594d4314de 100644 --- a/cmake/compiler/gcc/compiler_flags.cmake +++ b/cmake/compiler/gcc/compiler_flags.cmake @@ -104,6 +104,7 @@ set_compiler_property(PROPERTY warning_error_coding_guideline set_compiler_property(PROPERTY cstd -std=) if (NOT CONFIG_NEWLIB_LIBC AND + NOT (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_USE_MODULE) AND NOT COMPILER STREQUAL "xcc" AND NOT CONFIG_HAS_ESPRESSIF_HAL AND NOT CONFIG_NATIVE_APPLICATION) From 04870f53ff9a9b415977037528e574033e1f2b33 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 6 Jan 2023 13:25:51 -0800 Subject: [PATCH 2/7] toolchain/zephyr: Add TOOLCHAIN_HAS_PICOLIBC for sdk >= 0.16 Zephyr SDK version 0.16 will include picolibc support; add a variable so that tests may check for it. Signed-off-by: Keith Packard --- cmake/toolchain/zephyr/generic.cmake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmake/toolchain/zephyr/generic.cmake b/cmake/toolchain/zephyr/generic.cmake index f59d18c56fcd9..8f3f8252c80f9 100644 --- a/cmake/toolchain/zephyr/generic.cmake +++ b/cmake/toolchain/zephyr/generic.cmake @@ -4,4 +4,8 @@ include(${ZEPHYR_SDK_INSTALL_DIR}/cmake/zephyr/generic.cmake) set(TOOLCHAIN_KCONFIG_DIR ${ZEPHYR_SDK_INSTALL_DIR}/cmake/zephyr) +if(SDK_VERSION VERSION_GREATER_EQUAL 0.16) + set(TOOLCHAIN_HAS_PICOLIBC ON CACHE BOOL "True if toolchain supports picolibc") +endif() + message(STATUS "Found toolchain: zephyr ${SDK_VERSION} (${ZEPHYR_SDK_INSTALL_DIR})") From 17cbb1b27b794f473f27768018727f41ea4c7ad0 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 2 Nov 2022 13:11:41 -0700 Subject: [PATCH 3/7] libc/picolibc: Use libc-level PICOLIBC_MODULE to control picolibc mode C++ is supported with picolibc only when the toolchain version of picolibc is use -- libstdc++ must be built with picolibc support and libstdc++ is included with the toolchain. That creates a circular dependency if the control of whether to use the module is placed under the picolibc configuration block (as PICOLIBC_USE_MODULE is). Make an explicit PICOLIBC_MODULE variable at the libc level to be the user-visible control for this feature and make code depending on whether the picolibc module should be used depend on both PICOLIBC and PICOLIBC_MODULE. Change PICOLIBC_USE_MODULE to be a hidden configuration variable that the picolibc module cmake files can use to determine whether to include the picolibc module sources in the zephyr application build. This symbol can be removed when the picolibc module is updated to use PICOLIBC AND PICOLIBC_MODULE instead Signed-off-by: Keith Packard --- CMakeLists.txt | 2 +- cmake/compiler/gcc/compiler_flags.cmake | 2 +- lib/libc/Kconfig | 6 ++++++ lib/libc/picolibc/CMakeLists.txt | 2 +- lib/libc/picolibc/Kconfig | 18 +++++++++--------- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0788c10a5ebf3..be99801239562 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -695,7 +695,7 @@ add_custom_command( # Make sure Picolibc is built before the rest of the system; there's no explicit # reference to any of the files as they're all picked up by various compiler # settings -if(CONFIG_PICOLIBC_USE_MODULE) +if(CONFIG_PICOLIBC AND CONFIG_PICOLIBC_MODULE) set(picolibc_dependency PicolibcBuild) endif() diff --git a/cmake/compiler/gcc/compiler_flags.cmake b/cmake/compiler/gcc/compiler_flags.cmake index cfa594d4314de..f59f7c65b5435 100644 --- a/cmake/compiler/gcc/compiler_flags.cmake +++ b/cmake/compiler/gcc/compiler_flags.cmake @@ -104,7 +104,7 @@ set_compiler_property(PROPERTY warning_error_coding_guideline set_compiler_property(PROPERTY cstd -std=) if (NOT CONFIG_NEWLIB_LIBC AND - NOT (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_USE_MODULE) AND + NOT (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_MODULE) AND NOT COMPILER STREQUAL "xcc" AND NOT CONFIG_HAS_ESPRESSIF_HAL AND NOT CONFIG_NATIVE_APPLICATION) diff --git a/lib/libc/Kconfig b/lib/libc/Kconfig index 697a6ee99439d..11b46cc655632 100644 --- a/lib/libc/Kconfig +++ b/lib/libc/Kconfig @@ -68,6 +68,12 @@ config EXTERNAL_LIBC endchoice # LIBC_IMPLEMENTATION +config PICOLIBC_MODULE + bool "Use picolibc module" + default y if !CPLUSPLUS + help + Use picolibc module instead of picolibc included with toolchain + config HAS_NEWLIB_LIBC_NANO bool diff --git a/lib/libc/picolibc/CMakeLists.txt b/lib/libc/picolibc/CMakeLists.txt index bfbe4bca8c332..02a487a68fdb5 100644 --- a/lib/libc/picolibc/CMakeLists.txt +++ b/lib/libc/picolibc/CMakeLists.txt @@ -7,7 +7,7 @@ zephyr_library_sources(libc-hooks.c) # used by the network stack zephyr_compile_definitions(__LINUX_ERRNO_EXTENSIONS__) -if(NOT CONFIG_PICOLIBC_USE_MODULE) +if(CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_MODULE) # Use picolibc provided with the toolchain diff --git a/lib/libc/picolibc/Kconfig b/lib/libc/picolibc/Kconfig index 492a02a87f03c..be2584c94d8e5 100644 --- a/lib/libc/picolibc/Kconfig +++ b/lib/libc/picolibc/Kconfig @@ -3,13 +3,6 @@ if PICOLIBC -config PICOLIBC_USE_MODULE - bool "Use picolibc module" - default y - select PICOLIBC_MODULE - help - Use picolibc module instead of picolibc included with toolchain - config PICOLIBC_HEAP_SIZE int "Picolibc heap size (bytes)" default 16384 if MMU @@ -40,7 +33,14 @@ config PICOLIBC_IO_FLOAT help Include floating point support in printf/scanf functions. -if PICOLIBC_USE_MODULE +if PICOLIBC_MODULE + +config PICOLIBC_USE_MODULE + bool + default y + help + Hidden option that tells the picolibc module + whether to build picolibc or to skip it. choice PICOLIBC_OPTIMIZATIONS prompt "Optimization level" @@ -148,6 +148,6 @@ config PICOLIBC_GLOBAL_ERRNO which can be used to avoid TLS variable usage by the library if necessary. -endif # PICOLIBC_USE_MODULE +endif # PICOLIBC_MODULE endif # PICOLIBC From 4c4632e5942faebd32c4825cf73da9a1b6b24305 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 7 Nov 2022 13:53:35 -0800 Subject: [PATCH 4/7] libc/picolibc: Allow toolchain picolibc use with C++ The Zephyr SDK includes C++ support for picolibc when using picolibc packaged with the SDK. Signed-off-by: Keith Packard --- lib/libc/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/libc/Kconfig b/lib/libc/Kconfig index 11b46cc655632..5a6fe655b8885 100644 --- a/lib/libc/Kconfig +++ b/lib/libc/Kconfig @@ -19,6 +19,7 @@ config PICOLIBC_SUPPORTED bool depends on ARC || ARM || ARM64 || MIPS || RISCV depends on "$(ZEPHYR_TOOLCHAIN_VARIANT)" != "arcmwdt" + depends on !CPLUSPLUS || !PICOLIBC_MODULE default y help Selected when the target has support for picolibc. From 3cb0c7447b6c27cc467b654b384acef9ac0b0acb Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 6 Jan 2023 13:29:14 -0800 Subject: [PATCH 5/7] subsys/cpp: Use toolchain picolibc for C++ tests The picolibc module is not supported for C++, so make sure the toolchain has C++ support and then use it for the tests. Signed-off-by: Keith Packard --- tests/subsys/cpp/cxx/testcase.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/subsys/cpp/cxx/testcase.yaml b/tests/subsys/cpp/cxx/testcase.yaml index 378bdaef530f0..079152b8940d6 100644 --- a/tests/subsys/cpp/cxx/testcase.yaml +++ b/tests/subsys/cpp/cxx/testcase.yaml @@ -22,6 +22,6 @@ tests: - CONFIG_NEWLIB_LIBC_NANO=y cpp.main.picolibc: tags: picolibc - filter: CONFIG_PICOLIBC_SUPPORTED + filter: (TOOLCHAIN_HAS_PICOLIBC == 1) and CONFIG_PICOLIBC_SUPPORTED extra_configs: - CONFIG_PICOLIBC=y From eb84c8c1ac846ef4c02465ab0ae5cd544be342c7 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 6 Jan 2023 13:36:21 -0800 Subject: [PATCH 6/7] subsys/cpp: Also run C++ tests with picolibc when possible When the toolchain has picolibc support, run samples/subsys/cpp/cpp_synchronization and tests/subsys/cpp/libcxx tests using it. Signed-off-by: Keith Packard --- .../subsys/cpp/cpp_synchronization/sample.yaml | 15 +++++++++++++++ tests/subsys/cpp/libcxx/testcase.yaml | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/samples/subsys/cpp/cpp_synchronization/sample.yaml b/samples/subsys/cpp/cpp_synchronization/sample.yaml index e3e9669a1ad09..621224a7b5589 100644 --- a/samples/subsys/cpp/cpp_synchronization/sample.yaml +++ b/samples/subsys/cpp/cpp_synchronization/sample.yaml @@ -13,3 +13,18 @@ tests: - "Create semaphore (.*)" - "main: Hello World!" - "coop_thread_entry: Hello World!" + sample.cpp.synchronization.picolibc: + filter: (TOOLCHAIN_HAS_PICOLIBC == 1) and CONFIG_PICOLIBC_SUPPORTED + extra_configs: + - CONFIG_PICOLIBC=y + tags: cpp + toolchain_exclude: issm xcc + integration_platforms: + - qemu_x86 + harness: console + harness_config: + type: multi_line + regex: + - "Create semaphore (.*)" + - "main: Hello World!" + - "coop_thread_entry: Hello World!" diff --git a/tests/subsys/cpp/libcxx/testcase.yaml b/tests/subsys/cpp/libcxx/testcase.yaml index 3a8f23b18da45..98f9007c9a673 100644 --- a/tests/subsys/cpp/libcxx/testcase.yaml +++ b/tests/subsys/cpp/libcxx/testcase.yaml @@ -21,6 +21,15 @@ tests: - CONFIG_NEWLIB_LIBC_NANO=y integration_platforms: - mps2_an385 + cpp.libcxx.picolibc: + filter: (TOOLCHAIN_HAS_PICOLIBC == 1) and CONFIG_PICOLIBC_SUPPORTED + toolchain_exclude: xcc + tags: cpp + timeout: 60 + extra_configs: + - CONFIG_PICOLIBC=y + integration_platforms: + - mps2_an385 cpp.libcxx.arcmwdtlib: toolchain_allow: arcmwdt min_flash: 54 From 9cba2fd2c4056a1df55a3a3a56e78d3a1cbbb611 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Fri, 13 Jan 2023 14:59:28 +0100 Subject: [PATCH 7/7] kconfig: review proposal on PR#53338 Review proposal for PR#53338. Changes can be moved to correct commits or this commit message can be re-written and changes used as is. Signed-off-by: Torsten Rasmussen --- lib/libc/Kconfig | 10 +++------- lib/libc/picolibc/Kconfig | 17 +++++++++-------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/libc/Kconfig b/lib/libc/Kconfig index 5a6fe655b8885..6a53a65eb2ce5 100644 --- a/lib/libc/Kconfig +++ b/lib/libc/Kconfig @@ -19,7 +19,8 @@ config PICOLIBC_SUPPORTED bool depends on ARC || ARM || ARM64 || MIPS || RISCV depends on "$(ZEPHYR_TOOLCHAIN_VARIANT)" != "arcmwdt" - depends on !CPLUSPLUS || !PICOLIBC_MODULE + # Picolibc with C++ support in Zephyr SDK is handled by Zephyr SDK's own Kconfig. + depends on !(CPLUSPLUS && "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "zephyr") default y help Selected when the target has support for picolibc. @@ -43,6 +44,7 @@ config PICOLIBC select THREAD_LOCAL_STORAGE if ARCH_HAS_THREAD_LOCAL_STORAGE && TOOLCHAIN_SUPPORTS_THREAD_LOCAL_STORAGE select LIBC_ERRNO if THREAD_LOCAL_STORAGE depends on !NATIVE_APPLICATION + depends on PICOLIBC_SUPPORTED help Build with picolibc library. The picolibc library is built as a module if PICOLIBC_MODULE is set, otherwise picolibc is @@ -69,12 +71,6 @@ config EXTERNAL_LIBC endchoice # LIBC_IMPLEMENTATION -config PICOLIBC_MODULE - bool "Use picolibc module" - default y if !CPLUSPLUS - help - Use picolibc module instead of picolibc included with toolchain - config HAS_NEWLIB_LIBC_NANO bool diff --git a/lib/libc/picolibc/Kconfig b/lib/libc/picolibc/Kconfig index be2584c94d8e5..94cfec08e4b34 100644 --- a/lib/libc/picolibc/Kconfig +++ b/lib/libc/picolibc/Kconfig @@ -3,6 +3,14 @@ if PICOLIBC +config PICOLIBC_USE_MODULE + bool "Picolibc as module" + default y + depends on ZEPHYR_PICOLIBC_MODULE + depends on !CPLUSPLUS + help + Use picolibc module instead of picolibc included with toolchain + config PICOLIBC_HEAP_SIZE int "Picolibc heap size (bytes)" default 16384 if MMU @@ -33,14 +41,7 @@ config PICOLIBC_IO_FLOAT help Include floating point support in printf/scanf functions. -if PICOLIBC_MODULE - -config PICOLIBC_USE_MODULE - bool - default y - help - Hidden option that tells the picolibc module - whether to build picolibc or to skip it. +if PICOLIBC_USE_MODULE choice PICOLIBC_OPTIMIZATIONS prompt "Optimization level"