Repository: mesonbuild/meson

Issue:
zipapp should not accept python interpreters without ctypes module
**Describe the bug**
ctypes is an optional module and python interpreters will not have them when compiled without libffi. Meson create_zipapp should fail if that is the case, as the packaged meson will not be able to start anyway, and it is a waste of time to discover that after meson succeeds installation.
 
**To Reproduce**
1. Remove libffi from system paths
2. Build&install python from tarball
3. create meson zipapp and install that
4. Try running meson

**Expected behavior**
Meson should fail at zipapp stage

**system parameters**
Linux, meson-latest

Fail if python was compiled without ctypes
Fixes #11111 


Discussion:
- Why should we assume that the zipapp is built on the same python interpreter that is going to run meson? Isn't the point of a zipapp to package it up for *re*distribution?
- What about installing meson from a wheel? If you install Meson from a wheel, on a system without access to ctypes, won't that succeed installation and then fail at runtime?
> Why should we assume that the zipapp is built on the same python interpreter that is going to run meson?

I've never said that, and if you check my PR you can see that I am checking against the interpreter parameter passed to zipapp, not the interpreter that is runing zipapp. Though if you are building against an interpreter that is not present on your system that would fail, I hadn't considered that case.

> What about installing meson from a wheel? If you install Meson from a wheel, on a system without access to ctypes, won't that succeed installation and then fail at runtime?

Yes, in the weird hypothetical system where you build your system python but install meson from some package manager. But it the more likely case of building both your python and meson, this error can be caught.
Well, have you checked to see if indeed setuptools can build a wheel (`python setup.py bdist_wheel` or even `install` tbh) on a machine without ctypes?
I was following zipapp steps described here https://github.com/mesonbuild/meson#creating-a-standalone-script

But tried now doing `setup.py build` and `setup.py install` and they succeeded and installed a binary to `$PYTHON_PREFIX/bin/meson` that fails to start up with the ctypes error
So then we'd end up with zipapp erroring out when setup.py does not. :p

Tbh the idea of not having ctypes seems odd to me. I can't really find documentation on what stdlib modules are expected to be "optional", and I've only ever actually seen ctypes be unavailable when I update my system libffi but don't recompile a custom python and the ctypes module simply errors out with missing library dependencies.

If this is actually supposed to be an optional stdlib module, what does CPython upstream expect people to do in order to declare their dependency on "a python interpreter that has ctypes support"?
As observed in the linked issue, `python setup.py install` works fine to install Meson on such systems, and then still fails at runtime when ctypes is not available.

(And so does producing generic wheels.)

Resolving patch:
diff --git a/mesonbuild/utils/universal.py b/mesonbuild/utils/universal.py
--- a/mesonbuild/utils/universal.py
+++ b/mesonbuild/utils/universal.py
@@ -17,7 +17,6 @@
 from __future__ import annotations
 from pathlib import Path
 import argparse
-import ctypes
 import enum
 import sys
 import stat
@@ -699,6 +698,7 @@ def windows_detect_native_arch() -> str:
     if sys.platform != 'win32':
         return ''
     try:
+        import ctypes
         process_arch = ctypes.c_ushort()
         native_arch = ctypes.c_ushort()
         kernel32 = ctypes.windll.kernel32


Test patch: