Skip to content

gh-109054: configure checks if libatomic is needed #109101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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. Patch by Victor Stinner.
85 changes: 84 additions & 1 deletion configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 61 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -6962,6 +6962,62 @@ then
[Define if you want to disable the GIL])
fi

# gh-109054: Check if -latomic is needed to get <pyatomic.h> atomic functions.
# On Linux aarch64, GCC may require programs and libraries to be linked
# explicitly to libatomic. Call _Py_atomic_or_uint64() which may require
# libatomic __atomic_fetch_or_8(), or not, depending on the C compiler and the
# compiler flags.
#
# Avoid #include <Python.h> or #include <pyport.h>. The <Python.h> header
# requires <pyconfig.h> header which is only written below by AC_OUTPUT below.
# If the check is done after AC_OUTPUT, modifying LIBATOMIC has no effect
# anymore. <pyport.h> cannot be included alone, it's designed to be included
# by <Python.h>: it expects other includes and macros to be defined.
_SAVE_VAR([CPPFLAGS])
CPPFLAGS="${BASECPPFLAGS} -I. -I${srcdir}/Include ${CPPFLAGS}"

AC_CACHE_CHECK([whether libatomic is needed by <pyatomic.h>],
[ac_cv_libatomic_needed],
[AC_RUN_IFELSE([AC_LANG_SOURCE([[
// pyatomic.h needs uint64_t and Py_ssize_t types
#include <stdint.h> // int64_t, intptr_t
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h> // ssize_t
#endif
// Code adapted from Include/pyport.h
#if HAVE_SSIZE_T
typedef ssize_t Py_ssize_t;
#elif SIZEOF_VOID_P == SIZEOF_SIZE_T
typedef intptr_t Py_ssize_t;
#else
# error "unable to define Py_ssize_t"
#endif

#include "cpython/pyatomic.h"

int main()
{
uint64_t byte;
_Py_atomic_store_uint64(&byte, 2);
if (_Py_atomic_or_uint64(&byte, 8) != 2) {
return 1; // error
}
if (_Py_atomic_load_uint64(&byte) != 10) {
return 1; // error
}
return 0; // all good
}
]])],[
ac_cv_libatomic_needed=no
],[ac_cv_libatomic_needed=yes],[ac_cv_libatomic_needed=yes])
])

AS_VAR_IF([ac_cv_libatomic_needed], [yes],
[LIBATOMIC=${LIBATOMIC-"-latomic"}])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realised we could probably just use ${VAR="default"} instead of the more verbose VAR=${VAR-"default"}. However, let's stick to the existing practise :) And hopefully, configure.ac is git rm'd some day.

_RESTORE_VAR([CPPFLAGS])


# stdlib
AC_DEFUN([PY_STDLIB_MOD_SET_NA], [
m4_foreach([mod], [$@], [
AS_VAR_SET([py_cv_module_]mod, [n/a])])
Expand Down Expand Up @@ -7229,7 +7285,10 @@ PY_STDLIB_MOD([_hashlib], [], [test "$ac_cv_working_openssl_hashlib" = yes],
[$OPENSSL_INCLUDES], [$OPENSSL_LDFLAGS $OPENSSL_LDFLAGS_RPATH $LIBCRYPTO_LIBS])

dnl test modules
PY_STDLIB_MOD([_testcapi], [test "$TEST_MODULES" = yes])
PY_STDLIB_MOD([_testcapi],
[test "$TEST_MODULES" = yes], []
dnl Modules/_testcapi/pyatomic.c uses <cpython/pyatomic.h> header
[], [], [$LIBATOMIC])
PY_STDLIB_MOD([_testclinic], [test "$TEST_MODULES" = yes])
PY_STDLIB_MOD([_testclinic_limited], [test "$TEST_MODULES" = yes])
PY_STDLIB_MOD([_testinternalcapi], [test "$TEST_MODULES" = yes])
Expand Down Expand Up @@ -7262,6 +7321,7 @@ AC_CONFIG_FILES(m4_normalize([
Modules/Setup.stdlib
]))
AC_CONFIG_FILES([Modules/ld_so_aix], [chmod +x Modules/ld_so_aix])
# Generate files like pyconfig.h
AC_OUTPUT

AC_MSG_NOTICE([creating Modules/Setup.local])
Expand Down