Skip to content
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

Python 3.11 beta3 fails to build (_ctypes extension) on N32-abi based MIPS #94656

Closed
akhuettel opened this issue Jul 7, 2022 · 9 comments
Closed
Labels
type-bug An unexpected behavior, bug, or error

Comments

@akhuettel
Copy link

Bug report

Python 3.11 beta3 fails to build (more specifically, its _ctypes extension) on Gentoo in all variants where
the N32 ABI is the main system ABI on MIPS.

building '_ctypes' extension
mips64-unknown-linux-gnu-gcc -fPIC -Wsign-compare -DNDEBUG -O2 -march=mips64 -mabi=n32 -mplt -pipe -fwrapv -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I./Include -I. -I/usr/include/ncursesw -I/var/tmp/portage/dev-lang/python-3.11.0_beta3/work/Python-3.11.0b3/Include -I/var/tmp/portage/dev-lang/python-3.11.0_beta3/work/Python-3.11.0b3 -c /var/tmp/portage/dev-lang/python-3.11.0_beta3/work/Python-3.11.0b3/Modules/_ctypes/_ctypes.c -o build/temp.linux-mips64-3.11/var/tmp/portage/dev-lang/python-3.11.0_beta3/work/Python-3.11.0b3/Modules/_ctypes/_ctypes.o
/var/tmp/portage/dev-lang/python-3.11.0_beta3/work/Python-3.11.0b3/Modules/_ctypes/_ctypes.c:118:10: fatal error: ffi.h: No such file or directory
  118 | #include <ffi.h>
      |          ^~~~~~~
compilation terminated.

3.10 and earlier build fine.

Explanation on MIPS ABI
MIPS has 3 ABI,

  • o32 (a pure 32bit ABI, e.g. with CHOST=mips-unknown-linux-gnu on BE)
  • n64 (a pure 64bit ABI, e.g. with CHOST=mips64-unknown-linux-gnu on BE)
  • n32 (a "mixed ABI" similar to x32, which also requires 64bit hardware, also with CHOST=mips64-unknown-linux-gnu on BE)

Each of these can be used alone, in a single-ABI install; historically there are also multilib installs where n32 provides the main system ABI but n64 is supported for applications).

On Gentoo, the library directory is "lib" for o32, "lib32" for n32, and "lib64" for n64.

Debugging done
I tested on a multilib install, where n32 is the main system ABI and the build fails.
pkgconfig provides useful and correct output:

dilfridge-mips64-multilib ~ # pkg-config libffi --cflags-only-I
-I/usr/lib32/libffi/include 
dilfridge-mips64-multilib ~ # pkg-config libffi --cflags-only-I 2>/dev/null | sed -e 's/^-I//;s/ *$//'
/usr/lib32/libffi/include
dilfridge-mips64-multilib ~ # ls -l /usr/lib32/libffi/include
total 24
-rw-r--r-- 1 root root 14253 Apr 18 14:51 ffi.h
-rw-r--r-- 1 root root  6578 Apr 18 14:51 ffitarget.h

build.log
config.log

Maybe python is somehow setting a wrong libdir to search, or a wrong configuration directory for pkgconfig?

MIPS installs with o32 or n64 ABI build python-3.11_beta3 fine.

@akhuettel akhuettel added the type-bug An unexpected behavior, bug, or error label Jul 7, 2022
@akhuettel
Copy link
Author

See also the original bug report https://bugs.gentoo.org/850151

@tiran
Copy link
Member

tiran commented Jul 7, 2022

3.11 still uses this weird mix of pkg-config and custom distutils code to detect libffi.

Could you please add import pdb; pdb.set_trace to detect_ctypes in setup.py and show us the value of

  • sysconfig.get_config_var("LIBFFI_INCLUDEDIR")
  • self.lib_dirs
  • self.compiler.find_library_file(self.lib_dirs, lib_name for ffi and ffi_pic?

@akhuettel
Copy link
Author

(Pdb) print(sysconfig.get_config_var("LIBFFI_INCLUDEDIR"))
/usr/lib32/libffi/include
(Pdb) print(self.lib_dirs)
['.', '/usr/local/lib', '/lib64', '/usr/lib64', '/lib', '/usr/lib']
(Pdb) print(self.compiler.find_library_file(self.lib_dirs, 'ffi')) 
None
(Pdb) print(self.compiler.find_library_file(self.lib_dirs, 'ffi_pic'))
None

@tiran
Copy link
Member

tiran commented Jul 7, 2022

Thanks!

Where is libffi.so / libffi_pic.so on your system? Is the file in /usr/lib32 or /usr/lib32/libffi/lib ?

@akhuettel
Copy link
Author

dilfridge-mips64-multilib / # find / -name libffi.so
/usr/lib32/libffi.so
dilfridge-mips64-multilib / # find / -name libffi_pic.so
dilfridge-mips64-multilib / # 

Basically, libffi is installed for the "main / system" abi which all the system binaries use, but not for any other abi which are only added on request.

@tiran
Copy link
Member

tiran commented Jul 7, 2022

setup.py adds hard-coded libdir paths and is not aware of 32-bit ABIs. It is only a special case for X86-64 / X86 systems with 32bit ABI /lib and 64bit ABI /lib64. It's ugly. Python 3.12 won't use setup.py and more and pkg-config for all libraries that support it.

In the mean time would this patch work for you:

diff --git a/setup.py b/setup.py
index 2edcb08b4fd..ed02d348ed3 100644
--- a/setup.py
+++ b/setup.py
@@ -852,7 +852,12 @@ def init_inc_lib_dirs(self):
             add_dir_to_list(self.compiler.include_dirs,
                             sysconfig.get_config_var("INCLUDEDIR"))
 
-        system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib']
+        if sys.maxsize > 2**32:
+            system_lib_dirs = ['/lib64', '/usr/lib64']
+        else:
+            system_lib_dirs = ['/lib32', '/usr/lib32']
+        system_lib_dirs.extend(['/lib', '/usr/lib'])
+
         system_include_dirs = ['/usr/include']
         # lib_dirs and inc_dirs are used to search for files;
         # if a file is found in one of those directories, it can

@floppym
Copy link
Contributor

floppym commented Jul 7, 2022

For Python 3.10, Gentoo applies this patch:

gentoo@69fccd4

We sed in the appropriate value for @@GENTOO_LIBDIR@@ when we build the package.

I think this patch just needs to be adapted for Python 3.11 downstream.

@akhuettel
Copy link
Author

@floppym I think you're right. Will test.
(I'm not member of python team but taking care of weird arches in release engineering, so...)

@akhuettel
Copy link
Author

My apologies for the noise. @floppym was correct and this was due to the missing patch on the Gentoo side. Fixed now.
There's nothing left to do here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

3 participants