The default C library is normally computed based on the target
triplet. However, for embedded systems, it can be useful to leave the
triplet alone while changing which C library is used by default. Other
C libraries may still be available on the system so the compiler and
can be used by specifying suitable include and library paths at build
time.

This patch only allows selecting between newlib and picolibc as those
are typically used in embedded systems. Other targets continue to
select their default C library through the existing mechanism.

If something other than picolibc or newlib is specified using
--with-default-libc= emit an error and stop.

Signed-off-by: Keith Packard <kei...@keithp.com>
---
 gcc/config.gcc   | 54 +++++++++++++++++++++++++++++++++++-------------
 gcc/configure.ac |  4 ++++
 2 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index b20545da649..052c69db028 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -720,6 +720,9 @@ arrowlake arrowlake-s clearwaterforest pantherlake 
diamondrapids native"
 # MUST be separated by exactly one space.
 x86_cpus="generic intel"
 
+# Inferred target C library (either newlib or picolibc)
+target_libc=
+
 # Common parts for widely ported systems.
 case ${target} in
 *-*-darwin*)
@@ -1162,23 +1165,11 @@ case ${target} in
   esac
   ;;
 *-picolibc-*)
-  # picolibc provides __cxa_atexit
-  default_use_cxa_atexit=yes
-  # picolibc provides stdint.h
-  use_gcc_stdint=none
-  tm_file="${tm_file} picolibc-spec.h"
-  extra_options="${extra_options} picolibc.opt"
+  target_libc=picolibc
   ;;
 
 *-*-elf|arc*-*-elf*)
-  # Assume that newlib is being used and so __cxa_atexit is provided.
-  default_use_cxa_atexit=yes
-  use_gcc_stdint=wrap
-
-  case "${with_newlib}-${with_headers}" in
-  no-no) use_gcc_stdint=provide ;;
-  *) ;;
-  esac
+  target_libc=newlib
   ;;
 esac
 
@@ -6135,6 +6126,41 @@ case ${target} in
                ;;
 esac
 
+# Override target_libc using --with-default-libc option
+case "${with_default_libc}" in
+newlib|picolibc)
+  target_libc="${with_default_libc}"
+  ;;
+"")
+  ;;
+*)
+  echo "Unknown libc in --with-default-libc=$with_default_libc" 1>&2
+  exit 1
+  ;;
+esac
+
+# Set up newlib or picolibc
+case "${target_libc}" in
+newlib)
+  # Assume that newlib is being used and so __cxa_atexit is provided.
+  default_use_cxa_atexit=yes
+  use_gcc_stdint=wrap
+  case "${with_newlib}-${with_headers}" in
+  no-no) use_gcc_stdint=provide ;;
+  *) ;;
+  esac
+  ;;
+picolibc)
+  # picolibc provides __cxa_atexit
+  default_use_cxa_atexit=yes
+  # picolibc provides stdint.h
+  use_gcc_stdint=none
+  tm_file="${tm_file} picolibc-spec.h"
+  extra_options="${extra_options} picolibc.opt"
+   ;;
+*) ;;
+esac
+
 t=
 all_defaults="abi cpu cpu_32 cpu_64 arch arch_32 arch_64 tune tune_32 tune_64 
schedule float mode fpu nan fp_32 odd_spreg_32 divide llsc mips-plt synci tls 
lxc1-sxc1 madd4 isa_spec compact-branches msa cmodel"
 for option in $all_defaults
diff --git a/gcc/configure.ac b/gcc/configure.ac
index b6db9edfc83..9c5a4de6181 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -2582,6 +2582,10 @@ if { { test x$host != x$target && test "x$with_sysroot" 
= x ; } ||
 fi
 AC_SUBST(inhibit_libc)
 
+AC_ARG_WITH(default-libc,
+       [AS_HELP_STRING([--with-default-libc],
+                       [Use specified default C library])])
+
 # When building gcc with a cross-compiler, we need to adjust things so
 # that the generator programs are still built with the native compiler.
 # Also, we cannot run fixincludes.
-- 
2.49.0

Reply via email to