Skip to content

Commit efd3ec2

Browse files
gh-109054: configure checks if libatomic is needed
Fix building the _testcapi extension on Linux AArch64 which requires linking to libatomic when <cpython/pyatomic.h> is used: the _Py_atomic_or_uint64() function requires libatomic __atomic_fetch_or_8() on this platform. The configure script now checks if linking to libatomic is needed and generates a new LIBATOMIC variable used to build the _testcapi extension. Building the _testcapi extension now uses the LIBATOMIC variable in its LDFLAGS, since Modules/_testcapi/pyatomic.c uses <cpython/pyatomic.h>. Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent f63d378 commit efd3ec2

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fix building the ``_testcapi`` extension on Linux AArch64 which requires
2+
linking to libatomic when ``<cpython/pyatomic.h>`` is used: the
3+
``_Py_atomic_or_uint64()`` function requires libatomic
4+
``__atomic_fetch_or_8()`` on this platform. The configure script now checks
5+
if linking to libatomic is needed and generates a new LIBATOMIC variable
6+
used to build the _testcapi extension. Patch by Victor Stinner.

configure

+87-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+64-1
Original file line numberDiff line numberDiff line change
@@ -6962,6 +6962,65 @@ then
69626962
[Define if you want to disable the GIL])
69636963
fi
69646964

6965+
# gh-109054: Check if -latomic is needed to get <pyatomic.h> atomic functions.
6966+
# On aarch64, GCC may require programs to be linked explicitly to libatomic.
6967+
# Use _Py_atomic_or_uint64() which may require libatomic __atomic_fetch_or_8()
6968+
# on Linux aarch64 (or not depending on the compiler and the compiler flags).
6969+
#
6970+
# Avoid #include <Python.h> or #include <pyport.h>. The <Python.h> header
6971+
# requires <pyconfig.h> header which is only written below by AC_OUTPUT below.
6972+
# If the check is done after AC_OUTPUT, modifying LIBS has no effect anymore.
6973+
# <pyport.h> cannot be included alone, it's designed to be included by
6974+
# <Python.h>: it expects other includes and macros to be defined.
6975+
LIBATOMIC=""
6976+
6977+
_SAVE_VAR([CPPFLAGS])
6978+
CPPFLAGS="${BASECPPFLAGS} -I. -I${srcdir}/Include ${CPPFLAGS}"
6979+
6980+
AC_CACHE_CHECK([whether libatomic is needed by <pyatomic.h>],
6981+
[ac_cv_libatomic_needed],
6982+
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
6983+
// pyatomic.h needs uint64_t and Py_ssize_t types
6984+
#include <stdint.h> // int64_t, intptr_t
6985+
#ifdef HAVE_SYS_TYPES_H
6986+
# include <sys/types.h> // ssize_t
6987+
#endif
6988+
// Code adapted from Include/pyport.h
6989+
#if HAVE_SSIZE_T
6990+
typedef ssize_t Py_ssize_t;
6991+
#elif SIZEOF_VOID_P == SIZEOF_SIZE_T
6992+
typedef intptr_t Py_ssize_t;
6993+
#else
6994+
# error "unable to find a type fitting ssize_t"
6995+
#endif
6996+
6997+
#include "cpython/pyatomic.h"
6998+
6999+
int main()
7000+
{
7001+
// use _Py_atomic_or_uint64() which requires libatomic __atomic_fetch_or_8()
7002+
// on Linux aarch64.
7003+
uint64_t byte;
7004+
_Py_atomic_store_uint64(&byte, 2);
7005+
if (_Py_atomic_or_uint64(&byte, 8) != 2) {
7006+
return 1; // error
7007+
}
7008+
if (_Py_atomic_load_uint64(&byte) != 10) {
7009+
return 1; // error
7010+
}
7011+
return 0; // all good
7012+
}
7013+
]])],[
7014+
ac_cv_libatomic_needed=no
7015+
],[ac_cv_libatomic_needed=yes],[ac_cv_libatomic_needed=yes])
7016+
])
7017+
7018+
AS_VAR_IF([ac_cv_libatomic_needed], [yes],
7019+
[LIBATOMIC=${LIBATOMIC-"-latomic"}])
7020+
_RESTORE_VAR([CPPFLAGS])
7021+
7022+
7023+
# stdlib
69657024
AC_DEFUN([PY_STDLIB_MOD_SET_NA], [
69667025
m4_foreach([mod], [$@], [
69677026
AS_VAR_SET([py_cv_module_]mod, [n/a])])
@@ -7229,7 +7288,10 @@ PY_STDLIB_MOD([_hashlib], [], [test "$ac_cv_working_openssl_hashlib" = yes],
72297288
[$OPENSSL_INCLUDES], [$OPENSSL_LDFLAGS $OPENSSL_LDFLAGS_RPATH $LIBCRYPTO_LIBS])
72307289

72317290
dnl test modules
7232-
PY_STDLIB_MOD([_testcapi], [test "$TEST_MODULES" = yes])
7291+
PY_STDLIB_MOD([_testcapi],
7292+
[test "$TEST_MODULES" = yes], []
7293+
dnl Modules/_testcapi/pyatomic.c uses <cpython/pyatomic.h> header
7294+
[], [], [$LIBATOMIC])
72337295
PY_STDLIB_MOD([_testclinic], [test "$TEST_MODULES" = yes])
72347296
PY_STDLIB_MOD([_testclinic_limited], [test "$TEST_MODULES" = yes])
72357297
PY_STDLIB_MOD([_testinternalcapi], [test "$TEST_MODULES" = yes])
@@ -7262,6 +7324,7 @@ AC_CONFIG_FILES(m4_normalize([
72627324
Modules/Setup.stdlib
72637325
]))
72647326
AC_CONFIG_FILES([Modules/ld_so_aix], [chmod +x Modules/ld_so_aix])
7327+
# Generate files like pyconfig.h
72657328
AC_OUTPUT
72667329

72677330
AC_MSG_NOTICE([creating Modules/Setup.local])

0 commit comments

Comments
 (0)