Skip to content

Commit

Permalink
pythongh-109054: configure adds libatomic if needed
Browse files Browse the repository at this point in the history
Check if <cpython/pyatomic.h> needs to be linked to libatomic.
  • Loading branch information
vstinner committed Sep 7, 2023
1 parent 403ab13 commit b353cff
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile.pre.in
Expand Up @@ -267,6 +267,7 @@ STATIC_LIBPYTHON= @STATIC_LIBPYTHON@

LIBS= @LIBS@
LIBM= @LIBM@
LIBATOMIC= @LIBATOMIC@
LIBC= @LIBC@
SYSLIBS= $(LIBM) $(LIBC)
SHLIBS= @SHLIBS@
Expand Down
64 changes: 63 additions & 1 deletion configure.ac
Expand Up @@ -6962,6 +6962,64 @@ then
[Define if you want to disable the GIL])
fi

# gh-109054: Check if -latomic is needed to get <pyatomic.h> atomic functions.
# On aarch64, GCC may require programs to be linked explicitly to libatomic.
#
# 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 LIBS 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.
AC_SUBST([LIBATOMIC])
LIBATOMIC=""

_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([[
#define NDEBUG
// pyatomic.h needs uint8_t and Py_ssize_t types
#include <stdint.h> // int8_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 find a fitting type of ssize_t"
#endif
#include "cpython/pyatomic.h"
int main()
{
uint8_t byte;
_Py_atomic_store_uint8(&byte, 5);
if (_Py_atomic_load_uint8(&byte) != 5) {
return 1; // error
}
return 0; // all good
}
]])],[
ac_cv_libatomic_needed=no
],[ac_cv_libatomic_needed=yes],[ac_cv_libatomic_needed=yes])
])

if test $ac_cv_libatomic_needed = yes
then
LIBATOMIC="-latomic"
AC_MSG_RESULT([XXX add -latomic to LIBS... too late?])
fi
_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 +7287,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 @@ -7264,6 +7325,7 @@ AC_CONFIG_FILES(m4_normalize([
AC_CONFIG_FILES([Modules/ld_so_aix], [chmod +x Modules/ld_so_aix])
AC_OUTPUT


AC_MSG_NOTICE([creating Modules/Setup.local])
if test ! -f Modules/Setup.local
then
Expand Down

0 comments on commit b353cff

Please sign in to comment.