From 7e457684ea05795be91265cc5bc0320995288871 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 7 Jan 2021 17:21:39 -0800 Subject: picolibc: Convert to companion library This allows configurations to include picolibc without excluding another C library. Signed-off-by: Keith Packard diff --git a/config/comp_libs/picolibc.in b/config/comp_libs/picolibc.in new file mode 100644 index 0000000..a3f07d6 --- /dev/null +++ b/config/comp_libs/picolibc.in @@ -0,0 +1,121 @@ +# picolibc options + +## depends on BARE_METAL && CONFIGURE_has_meson && CONFIGURE_has_ninja + +## help This option adds Picolibc to an existing configuration which may have +## help a C library, allowing you to install both and select between them +## help when compiling applications using the toolchain +## help +## help Picolibc is a C library intended for use on embedded systems. It is a +## help conglomeration of several library parts, all under BSD-compatible software +## help licenses that make them easily usable on embedded products. + +config PICOLIBC_CXA_ATEXIT + def_bool y + select LIBC_PROVIDES_CXA_ATEXIT + +config LIBC_PICOLIBC_TARGET_CFLAGS + string + prompt "Target CFLAGS for picolibc" + default "" + help + Used to add specific options when compiling the target libraries + (eg. -ffunction-sections -fdata-sections), which can't be defined + in global TARGET_CFLAGS, because they shall be not used for the + gcc target libraries. + Note: Both TARGET_CFLAGS and LIBC_PICOLIBC_TARGET_CFLAGS are used + to compile the libraries. + + Leave blank if you don't know better. + +config LIBC_PICOLIBC_IO_C99FMT + bool + prompt "Enable IOs on C99 formats" + default y + help + Enable support for IOs on C99 formats. + +config LIBC_PICOLIBC_REGISTER_FINI + bool + prompt "Enable finalization function registration using atexit" + help + Enable finalization function registration using atexit. + +config LIBC_PICOLIBC_ATEXIT_DYNAMIC_ALLOC + bool + prompt "Enable dynamic allocation of atexit entries" + help + Enable dynamic allocation of atexit entries. + +config LIBC_PICOLIBC_GLOBAL_ATEXIT + bool + prompt "Enable atexit data structure as global variable" + help + Enable atexit data structure as global variable, instead + of being thread-local. + +config LIBC_PICOLIBC_LITE_EXIT + bool + prompt "Enable lite exit" + default y + help + Enable lite exit, a size-reduced implementation of exit that doesn't + invoke clean-up functions such as _fini or global destructors. + +config LIBC_PICOLIBC_MULTITHREAD + bool + prompt "Enable support for multiple threads" + default y + help + Enable support for multiple threads. + +config LIBC_PICOLIBC_RETARGETABLE_LOCKING + bool + prompt "Enable retargetable locking" + help + Enable retargetable locking to allow the operating system to override + the dummy lock functions defined within picolibc. + +config LIBC_PICOLIBC_EXTRA_SECTIONS + bool + prompt "Place each function & data element in their own section" + help + Place each function & data symbol in their own section. This allows + the linker to garbage collect unused symbols at link time. + +config LIBC_PICOLIBC_ENABLE_TARGET_OPTSPACE + bool + prompt "Optimize picolibc for size" + default y + help + Pass --enable-target-optspace to picolibc configure. + + This will compile picolibc with -Os. + +config LIBC_PICOLIBC_LTO + bool + prompt "Enable Link Time Optimization" + depends on CC_GCC_USE_LTO + help + Builds the libraries with -flto to enable more aggressive link time + optimization. You will need to add -flto-partition=one to your + application's link line to keep the RETURN assembler macro together + with it's consumers. + +config LIBC_PICOLIBC_NANO_MALLOC + bool + prompt "Enable Nano Malloc" + default y + help + PICOLIBC has two implementations of malloc family's functions, one in + `mallocr.c' and the other one in `nano-mallocr.c'. This options + enables the nano-malloc implementation, which is for small systems + with very limited memory. Note that this implementation does not + support `--enable-malloc-debugging' any more. + +config LIBC_PICOLIBC_EXTRA_CONFIG_ARRAY + string + prompt "Extra config for picolibc" + default "" + help + Extra flags to pass to meson when configuring picolibc. diff --git a/config/libc/picolibc.in b/config/libc/picolibc.in deleted file mode 100644 index cc1d4d1..0000000 --- a/config/libc/picolibc.in +++ /dev/null @@ -1,124 +0,0 @@ -# picolibc options - -## depends on BARE_METAL && CONFIGURE_has_meson && CONFIGURE_has_ninja -## select LIBC_SUPPORT_THREADS_NATIVE - -## select CC_CORE_PASSES_NEEDED if CANADIAN -## select CC_CORE_PASS_2_NEEDED if ! CANADIAN - -## help Picolibc is a C library intended for use on embedded systems. It is a -## help conglomeration of several library parts, all under BSD-compatible software -## help licenses that make them easily usable on embedded products. Picolibc -## help is only available in source form. It can be compiled for a wide -## help array of processors, and will usually work on any architecture with -## help the addition of a few low-level routines. - -config PICOLIBC_CXA_ATEXIT - def_bool y - select LIBC_PROVIDES_CXA_ATEXIT - -config LIBC_PICOLIBC_TARGET_CFLAGS - string - prompt "Target CFLAGS for picolibc" - default "" - help - Used to add specific options when compiling the target libraries - (eg. -ffunction-sections -fdata-sections), which can't be defined - in global TARGET_CFLAGS, because they shall be not used for the - gcc target libraries. - Note: Both TARGET_CFLAGS and LIBC_PICOLIBC_TARGET_CFLAGS are used - to compile the libraries. - - Leave blank if you don't know better. - -config LIBC_PICOLIBC_IO_C99FMT - bool - prompt "Enable IOs on C99 formats" - default y - help - Enable support for IOs on C99 formats. - -config LIBC_PICOLIBC_REGISTER_FINI - bool - prompt "Enable finalization function registration using atexit" - help - Enable finalization function registration using atexit. - -config LIBC_PICOLIBC_ATEXIT_DYNAMIC_ALLOC - bool - prompt "Enable dynamic allocation of atexit entries" - help - Enable dynamic allocation of atexit entries. - -config LIBC_PICOLIBC_GLOBAL_ATEXIT - bool - prompt "Enable atexit data structure as global variable" - help - Enable atexit data structure as global variable, instead - of being thread-local. - -config LIBC_PICOLIBC_LITE_EXIT - bool - prompt "Enable lite exit" - default y - help - Enable lite exit, a size-reduced implementation of exit that doesn't - invoke clean-up functions such as _fini or global destructors. - -config LIBC_PICOLIBC_MULTITHREAD - bool - prompt "Enable support for multiple threads" - default y - help - Enable support for multiple threads. - -config LIBC_PICOLIBC_RETARGETABLE_LOCKING - bool - prompt "Enable retargetable locking" - help - Enable retargetable locking to allow the operating system to override - the dummy lock functions defined within picolibc. - -config LIBC_PICOLIBC_EXTRA_SECTIONS - bool - prompt "Place each function & data element in their own section" - help - Place each function & data symbol in their own section. This allows - the linker to garbage collect unused symbols at link time. - -config LIBC_PICOLIBC_ENABLE_TARGET_OPTSPACE - bool - prompt "Optimize picolibc for size" - default y - help - Pass --enable-target-optspace to picolibc configure. - - This will compile picolibc with -Os. - -config LIBC_PICOLIBC_LTO - bool - prompt "Enable Link Time Optimization" - depends on CC_GCC_USE_LTO - help - Builds the libraries with -flto to enable more aggressive link time - optimization. You will need to add -flto-partition=one to your - application's link line to keep the RETURN assembler macro together - with it's consumers. - -config LIBC_PICOLIBC_NANO_MALLOC - bool - prompt "Enable Nano Malloc" - default y - help - PICOLIBC has two implementations of malloc family's functions, one in - `mallocr.c' and the other one in `nano-mallocr.c'. This options - enables the nano-malloc implementation, which is for small systems - with very limited memory. Note that this implementation does not - support `--enable-malloc-debugging' any more. - -config LIBC_PICOLIBC_EXTRA_CONFIG_ARRAY - string - prompt "Extra config for picolibc" - default "" - help - Extra flags to pass to meson when configuring picolibc. diff --git a/samples/arm-picolibc-eabi/crosstool.config b/samples/arm-picolibc-eabi/crosstool.config index 7f7d42a..48a4543 100644 --- a/samples/arm-picolibc-eabi/crosstool.config +++ b/samples/arm-picolibc-eabi/crosstool.config @@ -3,10 +3,8 @@ CT_ARCH_ARM=y CT_MULTILIB=y CT_ARCH_FLOAT_SW=y CT_TARGET_VENDOR="picolibc" -CT_CC_LANG_CXX=n -CT_LIBC="picolibc" -CT_LIBC_NEWLIB=n -CT_LIBC_PICOLIBC=y +CT_LIBC_NONE=y CT_CC_GCC_CONFIG_TLS=y CT_TARGET_CFLAGS="-ftls-model=local-exec" CT_CC_GCC_MULTILIB_LIST="rmprofile" +CT_COMP_LIBS_PICOLIBC=y diff --git a/samples/arm-unknown-eabi/crosstool.config b/samples/arm-unknown-eabi/crosstool.config index 5636c36..3c8cbb7 100644 --- a/samples/arm-unknown-eabi/crosstool.config +++ b/samples/arm-unknown-eabi/crosstool.config @@ -6,3 +6,5 @@ CT_LIBC_NEWLIB_IO_LL=y CT_LIBC_NEWLIB_IO_FLOAT=y CT_LIBC_NEWLIB_DISABLE_SUPPLIED_SYSCALLS=y CT_CC_LANG_CXX=y +CT_COMP_LIBS_PICOLIBC=y +CT_TARGET_CFLAGS="-ftls-model=local-exec" diff --git a/scripts/build/companion_libs/340-picolibc.sh b/scripts/build/companion_libs/340-picolibc.sh new file mode 100644 index 0000000..c0128c1 --- /dev/null +++ b/scripts/build/companion_libs/340-picolibc.sh @@ -0,0 +1,124 @@ +# This file adds functions to build the Picolibc library +# Copyright © 2020 Keith Packard +# Licensed under the GPL v2 or later. See COPYING in the root of this package +# +# Edited by Keith Packard +# + +do_picolibc_get() { :; } +do_picolibc_extract() { :; } +do_picolibc_for_build() { :; } +do_picolibc_for_host() { :; } +do_picolibc_for_target() { :; } + +if [ "${CT_COMP_LIBS_PICOLIBC}" = "y" ]; then + +# Download picolibc +do_picolibc_get() { + CT_Fetch PICOLIBC +} + +do_picolibc_extract() { + CT_ExtractPatch PICOLIBC +} + +do_picolibc_for_target() { + local -a picolibc_opts + local cflags_for_target + + CT_DoStep INFO "Installing Picolibc library" + + CT_mkdir_pushd "${CT_BUILD_DIR}/build-picolibc-build-${CT_BUILD}" + + CT_DoLog EXTRA "Configuring Picolibc library" + + # Multilib is the default, so if it is not enabled, disable it. + if [ "${CT_MULTILIB}" != "y" ]; then + picolibc_opts+=("-Dmultilib=false") + fi + + yn_args="IO_C99FMT:io-c99-formats +IO_LL:io-long-long +REGISTER_FINI:newlib-register-fini +NANO_MALLOC:newlib-nano-malloc +ATEXIT_DYNAMIC_ALLOC:newlib-atexit-dynamic-alloc +GLOBAL_ATEXIT:newlib-global-atexit +LITE_EXIT:lite-exit +MULTITHREAD:newlib-multithread +RETARGETABLE_LOCKING:newlib-retargetable-locking + " + + for ynarg in $yn_args; do + var="CT_LIBC_PICOLIBC_${ynarg%:*}" + eval var=\$${var} + argument=${ynarg#*:} + + + if [ "${var}" = "y" ]; then + picolibc_opts+=( "-D$argument=true" ) + else + picolibc_opts+=( "-D$argument=false" ) + fi + done + + [ "${CT_USE_SYSROOT}" = "y" ] && \ + picolibc_opts+=( "-Dsysroot-install=true" ) + + [ "${CT_LIBC_PICOLIBC_EXTRA_SECTIONS}" = "y" ] && \ + CT_LIBC_PICOLIBC_TARGET_CFLAGS="${CT_LIBC_PICOLIBC_TARGET_CFLAGS} -ffunction-sections -fdata-sections" + + [ "${CT_LIBC_PICOLIBC_LTO}" = "y" ] && \ + CT_LIBC_PICOLIBC_TARGET_CFLAGS="${CT_LIBC_PICOLIBC_TARGET_CFLAGS} -flto" + + cflags_for_target="${CT_ALL_TARGET_CFLAGS} ${CT_LIBC_PICOLIBC_TARGET_CFLAGS}" + + # Note: picolibc handles the build/host/target a little bit differently + # than one would expect: + # build : not used + # host : the machine building picolibc + # target : the machine picolibc runs on + meson_cflags="" + for cflag in ${cflags_for_target}; do + meson_cflags="${meson_cflags} '${cflag}'," + done + cat << EOF > picolibc-cross.txt +[binaries] +c = '${CT_TARGET}-gcc' +ar = '${CT_TARGET}-ar' +as = '${CT_TARGET}-as' +strip = '${CT_TARGET}-strip' + +[host_machine] +system = '${CT_TARGET_VENDOR}' +cpu_family = '${CT_TARGET_ARCH}' +cpu = '${CT_TARGET_ARCH}' +endian = '${CT_ARCH_ENDIAN}' + +[properties] +c_args = [ ${meson_cflags} '-nostdlib', '-fno-common', '-ftls-model=local-exec' ] +needs_exe_wrapper = true +skip_sanity_check = true +EOF + + CT_DoExecLog CFG \ + meson \ + --cross-file picolibc-cross.txt \ + --prefix="${CT_PREFIX_DIR}" \ + -Dincludedir=picolibc/include \ + -Dlibdir=picolibc/${CT_TARGET}/lib \ + -Dspecsdir="${CT_SYSROOT_DIR}"/lib \ + "${CT_SRC_DIR}/picolibc" \ + "${picolibc_opts[@]}" \ + "${CT_LIBC_PICOLIBC_EXTRA_CONFIG_ARRAY[@]}" + + CT_DoLog EXTRA "Building C library" + CT_DoExecLog ALL ninja + + CT_DoLog EXTRA "Installing C library" + CT_DoExecLog ALL ninja install + + CT_Popd + CT_EndStep +} + +fi diff --git a/scripts/build/libc/picolibc.sh b/scripts/build/libc/picolibc.sh deleted file mode 100644 index 152125c..0000000 --- a/scripts/build/libc/picolibc.sh +++ /dev/null @@ -1,111 +0,0 @@ -# This file adds functions to build the Picolibc library -# Copyright © 2020 Keith Packard -# Licensed under the GPL v2 or later. See COPYING in the root of this package -# -# Edited by Keith Packard -# - -picolibc_start_files() -{ - CT_DoStep INFO "Installing C library headers & start files" - CT_DoExecLog ALL cp -a "${CT_SRC_DIR}/picolibc/newlib/libc/include/." \ - "${CT_HEADERS_DIR}" - CT_EndStep -} - -picolibc_main() -{ - local -a picolibc_opts - local cflags_for_target - - CT_DoStep INFO "Installing C library" - - CT_mkdir_pushd "${CT_BUILD_DIR}/build-libc" - - CT_DoLog EXTRA "Configuring Picolibc library" - - # Multilib is the default, so if it is not enabled, disable it. - if [ "${CT_MULTILIB}" != "y" ]; then - picolibc_opts+=("-Dmultilib=false") - fi - - yn_args="IO_C99FMT:io-c99-formats -IO_LL:io-long-long -REGISTER_FINI:newlib-register-fini -NANO_MALLOC:newlib-nano-malloc -ATEXIT_DYNAMIC_ALLOC:newlib-atexit-dynamic-alloc -GLOBAL_ATEXIT:newlib-global-atexit -LITE_EXIT:lite-exit -MULTITHREAD:newlib-multithread -RETARGETABLE_LOCKING:newlib-retargetable-locking - " - - for ynarg in $yn_args; do - var="CT_LIBC_PICOLIBC_${ynarg%:*}" - eval var=\$${var} - argument=${ynarg#*:} - - - if [ "${var}" = "y" ]; then - picolibc_opts+=( "-D$argument=true" ) - else - picolibc_opts+=( "-D$argument=false" ) - fi - done - - [ "${CT_USE_SYSROOT}" = "y" ] && \ - picolibc_opts+=( "-Dsysroot-install=true" ) - - [ "${CT_LIBC_PICOLIBC_EXTRA_SECTIONS}" = "y" ] && \ - CT_LIBC_PICOLIBC_TARGET_CFLAGS="${CT_LIBC_PICOLIBC_TARGET_CFLAGS} -ffunction-sections -fdata-sections" - - [ "${CT_LIBC_PICOLIBC_LTO}" = "y" ] && \ - CT_LIBC_PICOLIBC_TARGET_CFLAGS="${CT_LIBC_PICOLIBC_TARGET_CFLAGS} -flto" - - cflags_for_target="${CT_ALL_TARGET_CFLAGS} ${CT_LIBC_PICOLIBC_TARGET_CFLAGS}" - - # Note: picolibc handles the build/host/target a little bit differently - # than one would expect: - # build : not used - # host : the machine building picolibc - # target : the machine picolibc runs on - meson_cflags="" - for cflag in ${cflags_for_target}; do - meson_cflags="${meson_cflags} '${cflag}'," - done - cat << EOF > picolibc-cross.txt -[binaries] -c = '${CT_TARGET}-gcc' -ar = '${CT_TARGET}-ar' -as = '${CT_TARGET}-as' -strip = '${CT_TARGET}-strip' - -[host_machine] -system = '${CT_TARGET_VENDOR}' -cpu_family = '${CT_TARGET_ARCH}' -cpu = '${CT_TARGET_ARCH}' -endian = '${CT_ARCH_ENDIAN}' - -[properties] -c_args = [ ${meson_cflags} '-nostdlib', '-fno-common', '-ftls-model=local-exec' ] -needs_exe_wrapper = true -skip_sanity_check = true -EOF - CT_DoExecLog CFG \ - meson \ - --cross-file picolibc-cross.txt \ - --prefix=${CT_SYSROOT_DIR} \ - -Dspecsdir=${CT_SYSROOT_DIR}/lib \ - "${CT_SRC_DIR}/picolibc" \ - "${picolibc_opts[@]}" \ - "${CT_LIBC_PICOLIBC_EXTRA_CONFIG_ARRAY[@]}" - - CT_DoLog EXTRA "Building C library" - CT_DoExecLog ALL ninja - - CT_DoLog EXTRA "Installing C library" - CT_DoExecLog ALL ninja install - - CT_Popd - CT_EndStep -} -- cgit v0.10.2-6-g49f6