Repository: mesonbuild/meson

Issue:
Meson cannot build dependancies of numpy when python from msys2 is used
**Describe the bug**
When using MSYS2's python package https://packages.msys2.org/package/python?repo=msys&variant=x86_64, which is installed as a dependency of some other packages, running `pip install numpy` fails as the platform is not recognised.

**To Reproduce**
Install MSYS2, then the python package linked above (via pacman in msys2), then invoke `pip install numpy` from MSYS2's pip.

**Expected behavior**
I expect the platform to be recognized.

**system parameters**
* This is a compilation that is invoked via pip, 23.3.1 when installing numpy.
* Windows 11
* python 3.11.6
* meson: 1.2.99
* ninja: 1.11.1

A log:
```
      Traceback (most recent call last):
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\mesonmain.py", line 194, in run
          return options.run_func(options)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\msetup.py", line 358, in run
          app.generate()
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\msetup.py", line 183, in generate
          return self._generate(env, capture, vslite_ctx)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\msetup.py", line 228, in _generate
          intr.run()
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreter\interpreter.py", line 3002, in run
          super().run()
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\interpreterbase.py", line 164, in run
          self.evaluate_codeblock(self.ast, start=1)
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\interpreterbase.py", line 190, in evaluate_codeblock
          raise e
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\interpreterbase.py", line 182, in evaluate_codeblock
          self.evaluate_statement(cur)
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\interpreterbase.py", line 198, in evaluate_statement
          self.assignment(cur)
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\interpreterbase.py", line 635, in assignment
          value = self.evaluate_statement(node.value)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\interpreterbase.py", line 200, in evaluate_statement
          return self.method_call(cur)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\interpreterbase.py", line 550, in method_call
          res = obj.method_call(method_name, args, kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\baseobjects.py", line 94, in method_call
          return method(args, kwargs)
                 ^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\decorators.py", line 109, in wrapped
          ret = f(*wrapped_args, **wrapped_kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\decorators.py", line 127, in wrapped
          return f(*wrapped_args, **wrapped_kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\decorators.py", line 826, in wrapped
          return f(*wrapped_args, **wrapped_kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\interpreterbase\decorators.py", line 64, in wrapped
          return f(*wrapped_args, **wrapped_kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\modules\python.py", line 204, in dependency_method
          dep = self._dependency_method_impl(kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\modules\python.py", line 189, in _dependency_method_impl
          dep = find_external_dependency('python', self.interpreter.environment, new_kwargs, candidates)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\dependencies\detect.py", line 124, in find_external_dependency
          d = c()
              ^^^
        File "C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\vendored-meson\meson\mesonbuild\dependencies\python.py", line 224, in __init__
          if mesonlib.is_windows() and self.get_windows_python_arch().endswith('64') and mesonlib.version_compare(self.version, '<3.12'):
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      AttributeError: 'NoneType' object has no attribute 'endswith'
      The Meson build system
      Version: 1.2.99
      Source dir: C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357
      Build dir: C:\Users\Username\AppData\Local\Temp\pip-install-jfdxvsfp\numpy_81efa6ac209f4b209228086012fb9357\.mesonpy-zanut6e3\build
      Build type: native build
      Project name: NumPy
      Project version: 1.26.2
      C compiler for the host machine: cc (gcc 13.2.0 "cc (Rev2, Built by MSYS2 project) 13.2.0")
      C linker for the host machine: cc ld.bfd 2.41
      C++ compiler for the host machine: c++ (gcc 13.2.0 "c++ (Rev2, Built by MSYS2 project) 13.2.0")
      C++ linker for the host machine: c++ ld.bfd 2.41
      Cython compiler for the host machine: cython (cython 3.0.5)
      Host machine cpu family: x86_64
      Host machine cpu: x86_64
      Program python found: YES (C:\utils\msys64\mingw64\bin\python.exe)
      Did not find pkg-config by name 'pkg-config'
      Found Pkg-config: NO
      Unknown Windows Python platform 'mingw_x86_64'
      Unknown Windows Python platform 'mingw_x86_64'

      ..\..\meson.build:41:12: ERROR: Unhandled python exception
```


Discussion:
That's a Meson that's vendored in NumPy; it may or may not be a problem with actual Meson here. The line that's crashing does exist here, but it's hard to say if what it's calling is patched or broken in other ways. It would be easier to see here if it were a package that doesn't vendor, like contourpy or something.
It's a meson bug (or rather, sysconfig.get_platform() was extended some time ago to include more info, like ucrt/clang), but I think you can work around this by installing pkgconf.

I'll but it on my list.

edit: confirmed that installing pkgconf is a workaround.


Resolving patch:
diff --git a/mesonbuild/dependencies/python.py b/mesonbuild/dependencies/python.py
--- a/mesonbuild/dependencies/python.py
+++ b/mesonbuild/dependencies/python.py
@@ -245,14 +245,15 @@ def find_libpy(self, environment: 'Environment') -> None:
             self.is_found = True
 
     def get_windows_python_arch(self) -> T.Optional[str]:
-        if self.platform == 'mingw':
-            pycc = self.variables.get('CC')
-            if pycc.startswith('x86_64'):
+        if self.platform.startswith('mingw'):
+            if 'x86_64' in self.platform:
                 return 'x86_64'
-            elif pycc.startswith(('i686', 'i386')):
+            elif 'i686' in self.platform:
                 return 'x86'
+            elif 'aarch64' in self.platform:
+                return 'aarch64'
             else:
-                mlog.log(f'MinGW Python built with unknown CC {pycc!r}, please file a bug')
+                mlog.log(f'MinGW Python built with unknown platform {self.platform!r}, please file a bug')
                 return None
         elif self.platform == 'win32':
             return 'x86'
@@ -309,7 +310,7 @@ def get_windows_link_args(self, limited_api: bool) -> T.Optional[T.List[str]]:
                             '''))
             # base_prefix to allow for virtualenvs.
             lib = Path(self.variables.get('base_prefix')) / libpath
-        elif self.platform == 'mingw':
+        elif self.platform.startswith('mingw'):
             if self.static:
                 libname = self.variables.get('LIBRARY')
             else:


Test patch: