gccNGPackages: Rework threading model patch based on feedback on IRC (#424918)

This commit is contained in:
John Ericson 2025-07-13 23:13:25 -04:00 committed by GitHub
commit f50bcb34e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 61 additions and 79 deletions

View File

@ -1,59 +1,50 @@
From 1eaf7ce77bb4eb73e5565ede220557c2ef0290b0 Mon Sep 17 00:00:00 2001
From 46280d27a5c96b2efa7a61f49ee8dc14f7e10d0c Mon Sep 17 00:00:00 2001
From: John Ericson <git@JohnEricson.me>
Date: Sun, 22 Aug 2021 01:14:22 -0400
Subject: [PATCH] Allow explicitly specifying the thread model for runtime libs
Subject: [PATCH] Factor out thread model detection with `GCC_AC_THREAD_MODEL`
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Previously, they always scraped the thread mode from `$CC -v', now, that
is the default but one may pass `--with-threads=MODEL` to be explicit
instead.
This macro deduplicates the
One use-case is bootstraping with a shorter critical path. The
traditionally route was to build an entire "static stage" GCC, build
libc, and then build GCC again supporting dynamic linking,
multithreading, etc. But this is wasteful in that GCC itself is built
twice.
$CC -v 2>&1 | sed -n 's/^Thread model: //p'
With this change, rather than having to mess with spec files we can just
configure the runtime libraries the way we want directly. In turn, that
opens to just building libgcc twice rather than all of GCC.
check that was occurring in various runtime libs.
Frankly, specs were always a rather indirect approach to coordinate this
during GCC's bootstrap, since GCC itself really doesn't care what the
threading model is, just that the runtime libraries agree among
themselves. Relying on a hard-coded spec for this also keeps us one step
further from the long-term goal of multi-target GCC, for what it's
worth.
Additionally, as a bit of an Easter egg, this also allows overriding
what the compiler would return by setting the
`gcc_cv_target_thread_file` cache variable first. I admit that it is in
fact this Easter egg that led me to write the patch. The use-case for it
is for making multilib builds where the library sets do not all share
the same thread model easier. See also `THREAD_MODEL_SPEC` for more
about the varying thread models use-case.
For the record, "single stage" builds of GCC have some precedent in
downstream packaging, for example with [1]. That one, as far as I can
tell, builds libgcc once, but with the headers of the libc
implementation provided and then cyclic linking down later. They are
both fine approaches, but I don't think one should have to be forced
into cyclic dependencies if they don't want to. That opens the door to
non-terminating programs due to, e.g., atomics used in a threads
implementation being lowered to threads absent hardware support.
Arguably one could could try to define on `THREAD_MODEL_SPEC` on more
platforms (besides e.g. AIX) but the ramifications of this are a bit
unclear. Setting `gcc_cv_target_thread_file` directly is a "low tech"
solution that will work for now for sure. Of course, since setting a
cache variable like this a hacky trick, I will not expect this to be at
all stable/guaranteed to work, going forward.
Finally, I understand that such custom bootstrapping is not officially
supported. I don't mean to imply it should be --- a lot more cleanup
work to the build system would be necessary before supporting it
wouldn't be a huge additional maintainer burden --- I just hope to add a
reasonable knob for those comfortable with doing unsupported things
already.
Thanks to Arsen who on IRC discussed these things with me, including in
particular making it a cache var not `--with-model` flag, to not
prematurely foster expectations that this is stable.
[1]: https://github.com/richfelker/musl-cross-make
Suggested-by: Arsen Arsenović <arsen@aarsen.me>
---
config/gthr.m4 | 32 ++++++++++++++++++++++++++++++++
config/gthr.m4 | 23 +++++++++++++++++++++++
libatomic/configure.ac | 4 +---
libgcc/configure.ac | 4 +---
libphobos/m4/druntime/os.m4 | 2 +-
libstdc++-v3/acinclude.m4 | 8 +++-----
5 files changed, 38 insertions(+), 12 deletions(-)
5 files changed, 29 insertions(+), 12 deletions(-)
diff --git a/config/gthr.m4 b/config/gthr.m4
index 11996247f15..7aed5d3df0d 100644
index 11996247f15..e8fac4a5721 100644
--- a/config/gthr.m4
+++ b/config/gthr.m4
@@ -5,6 +5,35 @@ dnl Public License, this file may be distributed as part of a program
@@ -5,6 +5,26 @@ dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
@ -62,34 +53,25 @@ index 11996247f15..7aed5d3df0d 100644
+dnl usage: GCC_AC_THREAD_MODEL
+AC_DEFUN([GCC_AC_THREAD_MODEL],
+[
+# With threads
+# Specify the threading model for this GCC runtime library
+# Pass with no value to take from compiler's metadata
+# Pass with a value to specify a thread package
+# 'single' means single threaded -- without threads.
+AC_ARG_WITH(threads,
+[AS_HELP_STRING([[--with-threads=MODEL]],
+ [specify thread model for this GCC
+ runtime library])],,
+[with_threads=''])
+
+if test x"$with_threads" = x'yes'; then
+ AC_MSG_ERROR([Cannot pass bare --with-threads, must pass explicit --with-threads=MODEL])
+elif test x"$with_threads" = x'no'; then
+ target_thread_file=single
+elif test x"$with_threads" = x''; then
+ AC_MSG_CHECKING([for thread model used by GCC])
+ target_thread_file=`$CC -v 2>&1 | sed -n 's/^Thread model: //p'`
+ AC_MSG_RESULT([$target_thread_file])
+else
+ target_thread_file=$with_threads
+fi
+AC_CACHE_CHECK([for the threading model used by GCC], [gcc_cv_target_thread_file], [
+ # Set new cache variable
+ gcc_cv_target_thread_file=`$CC -v 2>&1 | sed -n 's/^Thread model: //p'`
+])
+# Set variable name (not prefixed enough to be a good cache variable
+# name) traditionally used for this purpose, to avoid having to change
+# a bunch of configure scripts.
+target_thread_file="$gcc_cv_target_thread_file"
+])
+
+
dnl Define header location by thread model
dnl usage: GCC_AC_THREAD_HEADER([thread_model])
@@ -23,6 +52,9 @@ case $1 in
@@ -23,6 +43,9 @@ case $1 in
vxworks) thread_header=config/gthr-vxworks.h ;;
win32) thread_header=config/i386/gthr-win32.h ;;
mcf) thread_header=config/i386/gthr-mcf.h ;;
@ -100,7 +82,7 @@ index 11996247f15..7aed5d3df0d 100644
AC_SUBST(thread_header)
])
diff --git a/libatomic/configure.ac b/libatomic/configure.ac
index 85824fa7614..691f4948f6c 100644
index aafae71028d..a1aa3bdf69f 100644
--- a/libatomic/configure.ac
+++ b/libatomic/configure.ac
@@ -162,9 +162,7 @@ libtool_VERSION=3:0:2
@ -115,7 +97,7 @@ index 85824fa7614..691f4948f6c 100644
case "$target" in
*aarch64*)
diff --git a/libgcc/configure.ac b/libgcc/configure.ac
index 4e8c036990f..acf54e24c79 100644
index 85e4f1bc48b..d44493f9653 100644
--- a/libgcc/configure.ac
+++ b/libgcc/configure.ac
@@ -305,9 +305,7 @@ AC_SUBST([use_tm_clone_registry])
@ -130,7 +112,7 @@ index 4e8c036990f..acf54e24c79 100644
# Check for assembler CFI support.
AC_CACHE_CHECK([whether assembler supports CFI directives], [libgcc_cv_cfi],
diff --git a/libphobos/m4/druntime/os.m4 b/libphobos/m4/druntime/os.m4
index 15cde3b04b8..28bd4d3d2bd 100644
index ef8ca434407..7bb91362dbe 100644
--- a/libphobos/m4/druntime/os.m4
+++ b/libphobos/m4/druntime/os.m4
@@ -32,7 +32,7 @@ case $1 in
@ -143,10 +125,10 @@ index 15cde3b04b8..28bd4d3d2bd 100644
AC_SUBST(DCFG_THREAD_MODEL)
])
diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4
index 51a08bcc8b1..71697f59d6d 100644
index a0094c2dd95..66fc3abe4fd 100644
--- a/libstdc++-v3/acinclude.m4
+++ b/libstdc++-v3/acinclude.m4
@@ -4342,9 +4342,7 @@ dnl Substs:
@@ -4345,9 +4345,7 @@ dnl Substs:
dnl thread_header
dnl
AC_DEFUN([GLIBCXX_ENABLE_THREADS], [
@ -157,7 +139,7 @@ index 51a08bcc8b1..71697f59d6d 100644
GCC_AC_THREAD_HEADER([$target_thread_file])
])
@@ -4354,7 +4352,8 @@ dnl Check if gthread implementation defines the types and functions
@@ -4357,7 +4355,8 @@ dnl Check if gthread implementation defines the types and functions
dnl required by the c++0x thread library. Conforming gthread
dnl implementations can define __GTHREADS_CXX0X to enable use with c++0x.
dnl
@ -167,7 +149,7 @@ index 51a08bcc8b1..71697f59d6d 100644
dnl
AC_DEFUN([GLIBCXX_CHECK_GTHREADS], [
GLIBCXX_ENABLE(libstdcxx-threads,auto,,[enable C++11 threads support])
@@ -4369,7 +4368,6 @@ AC_DEFUN([GLIBCXX_CHECK_GTHREADS], [
@@ -4372,7 +4371,6 @@ AC_DEFUN([GLIBCXX_CHECK_GTHREADS], [
CXXFLAGS="$CXXFLAGS -fno-exceptions \
-I${toplevel_srcdir}/libgcc -I${toplevel_builddir}/libgcc"
@ -176,5 +158,5 @@ index 51a08bcc8b1..71697f59d6d 100644
posix)
CXXFLAGS="$CXXFLAGS -DSUPPORTS_WEAK -DGTHREAD_USE_WEAK -D_PTHREADS"
--
2.44.1
2.47.2

View File

@ -51,21 +51,10 @@ stdenv.mkDerivation (finalAttrs: {
];
patches = [
# Submitted:
# - https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577639.html
# - https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577640.html
# - https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577638.html
#
# In Git: https://github.com/Ericson2314/gcc/tree/prog-target-15
(getVersionFile "gcc/0001-find_a_program-First-search-with-machine-prefix.patch")
(getVersionFile "gcc/0002-driver-for_each_pass-Pass-to-callback-whether-dir-is.patch")
(getVersionFile "gcc/0003-find_a_program-Only-search-for-prefixed-paths-in-und.patch")
# Submitted: https://inbox.sourceware.org/gcc-patches/20210818203840.1550133-1-John.Ericson@Obsidian.Systems/T/#t
# In Git: https://github.com/Ericson2314/gcc/tree/libgcc-custom-threading-model-15
(getVersionFile "gcc/custom-threading-model.patch")
# TODO: fix up and send to upstream
(getVersionFile "gcc/fix-collect2-paths.diff")
];

View File

@ -169,7 +169,7 @@ stdenv.mkDerivation (finalAttrs: {
configureFlags = [
"--disable-dependency-tracking"
"--with-threads=single"
"gcc_cv_target_thread_file=single"
# $CC cannot link binaries, let alone run then
"cross_compiling=true"
# Do not have dynamic linker without libc

View File

@ -158,7 +158,7 @@ stdenv.mkDerivation (finalAttrs: {
configureFlags = [
"--disable-dependency-tracking"
"--with-threads=single"
"gcc_cv_target_thread_file=single"
# $CC cannot link binaries, let alone run then
"cross_compiling=true"
];

View File

@ -53,7 +53,7 @@ stdenv.mkDerivation (finalAttrs: {
configureFlags = [
"--disable-dependency-tracking"
"--with-toolexeclibdir=${builtins.placeholder "out"}/lib"
"--with-threads=posix"
"gcc_cv_target_thread_file=posix"
"cross_compiling=true"
"--disable-multilib"

View File

@ -1,10 +1,18 @@
{
# TODO: fix up and send to upstream
"gcc/fix-collect2-paths.diff" = [
{
after = "15";
path = ../15;
}
];
# Submitted (001--003):
# - https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577639.html
# - https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577640.html
# - https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577638.html
#
# In Git: https://github.com/Ericson2314/gcc/tree/prog-target-15
"gcc/0001-find_a_program-First-search-with-machine-prefix.patch" = [
{
after = "15";
@ -23,6 +31,9 @@
path = ../15;
}
];
# Submitted: https://gcc.gnu.org/pipermail/gcc-patches/2025-July/689429.html
# In Git: https://github.com/Ericson2314/gcc/tree/libgcc-custom-threading-model-15
"gcc/custom-threading-model.patch" = [
{
after = "15";