Repository: mesonbuild/meson

Issue:
dependency() on a macOS framework cannot be found while cross-compiling.
**Describe the bug**
I'm attempting to build a Meson project in an "[osxcross](https://github.com/tpoechtrager/osxcross)" setting (build system: Linux; host system: Darwin).
I've hit what looks like an obvious mistake in Meson's programming, so I'd like to run that by folks before providing a complete reproduction (see below). (However, depending on what Meson needs, I'll see whether it's possible to get a reduced test case.)

The meson.build file has a dependency() on Apple's "Foundation" framework, among others.
In the cross environment, what's reported is: `ERROR: Dependency "Foundation" not found, tried pkgconfig"`
On an actual Mac, the log shows Meson doing something reasonable:
```
Finding framework path by running:  cc -v -E - -pipe

Looking for framework physfs in /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks
```
Meson has a cross file available, so the same strategy ought to work for my osxcross build, but it doesn't bother.
The code responsible for this decision is one of these snippets in `mesonbuild/dependencies/base.py`
```
        # On OSX, also try framework dependency detector  
        if mesonlib.is_osx():
            candidates.append(functools.partial(ExtraFrameworkDependency, name, env, kwargs))
```
The implementation in `mesonbuild/mesonlib.py` is
```
def is_osx() -> bool:
    return platform.system().lower() == 'darwin'
```
As far as I can tell, it's impossible for this to succeed in a cross-compilation environment, because it's checking the **build's** platform rather than the host's.
I am able to get past this error by editing these scripts in `/usr/local/lib/python3.9/dist-packages/mesonbuild/`.

**To Reproduce**
So far, my answer is nasty, along the lines of "download an enormous Docker image, clone a 1GB git repo into it, and run Meson setup."
I'm sorry.
What are your needs in terms of reduced test cases?

**Expected behavior**
I would expect Meson to perform the same search "ExtraFrameworkDependency" search anytime the host system makes it appropriate to do so.

**system parameters**
* Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)?
Yes.
* what operating system (e.g. MacOS Catalina, Windows 10, CentOS 8.0, Ubuntu 18.04, etc.)
Ubuntu 20.04 (Docker image).
* what Python version are you using e.g. 3.8.0
3.9.1
* what `meson --version`
0.56.0
* what `ninja --version` if it's a Ninja build
1.10.0



Discussion:

Resolving patch:
diff --git a/mesonbuild/dependencies/base.py b/mesonbuild/dependencies/base.py
--- a/mesonbuild/dependencies/base.py
+++ b/mesonbuild/dependencies/base.py
@@ -573,7 +573,7 @@ class PkgConfigDependency(ExternalDependency):
     # We cache all pkg-config subprocess invocations to avoid redundant calls
     pkgbin_cache = {}
 
-    def __init__(self, name, environment, kwargs, language: T.Optional[str] = None):
+    def __init__(self, name, environment: 'Environment', kwargs, language: T.Optional[str] = None):
         super().__init__('pkgconfig', environment, kwargs, language=language)
         self.name = name
         self.is_libtool = False
@@ -697,7 +697,7 @@ def _convert_mingw_paths(self, args: T.List[str]) -> T.List[str]:
         with / like /home/foo so leave them as-is so that the user gets an
         error/warning from the compiler/linker.
         '''
-        if not mesonlib.is_windows():
+        if not self.env.machines.build.is_windows():
             return args
         converted = []
         for arg in args:
@@ -948,7 +948,7 @@ def check_pkgconfig(self, pkgbin):
             return None
         except PermissionError:
             msg = 'Found pkg-config {!r} but didn\'t have permissions to run it.'.format(' '.join(pkgbin.get_command()))
-            if not mesonlib.is_windows():
+            if not self.env.machines.build.is_windows():
                 msg += '\n\nOn Unix-like systems this is often caused by scripts that are not executable.'
             mlog.warning(msg)
             return None
@@ -979,7 +979,7 @@ def extract_libtool_shlib(self, la_file):
 
         # Darwin uses absolute paths where possible; since the libtool files never
         # contain absolute paths, use the libdir field
-        if mesonlib.is_osx():
+        if self.env.machines[self.for_machine].is_darwin():
             dlbasename = os.path.basename(dlname)
             libdir = self.extract_libdir_field(la_file)
             if libdir is None:
@@ -1530,7 +1530,7 @@ def _detect_dep(self, name: str, package_version: str, modules: T.List[T.Tuple[s
                         libraries += [j]
                     elif os.path.isabs(j) and os.path.exists(j):
                         libraries += [j]
-                    elif mesonlib.is_windows() and reg_is_maybe_bare_lib.match(j):
+                    elif self.env.machines.build.is_windows() and reg_is_maybe_bare_lib.match(j):
                         # On Windows, CMake library dependencies can be passed as bare library names,
                         # e.g. 'version' should translate into 'version.lib'. CMake brute-forces a
                         # combination of prefix/suffix combinations to find the right library, however
@@ -2504,7 +2504,7 @@ def _build_external_dependency_list(name: str, env: Environment, for_machine: Ma
     # If it's explicitly requested, use the Extraframework detection method (only)
     if 'extraframework' == kwargs.get('method', ''):
         # On OSX, also try framework dependency detector
-        if mesonlib.is_osx():
+        if env.machines[for_machine].is_darwin():
             candidates.append(functools.partial(ExtraFrameworkDependency, name, env, kwargs))
         return candidates
 
@@ -2513,7 +2513,7 @@ def _build_external_dependency_list(name: str, env: Environment, for_machine: Ma
         candidates.append(functools.partial(PkgConfigDependency, name, env, kwargs))
 
         # On OSX, also try framework dependency detector
-        if mesonlib.is_osx():
+        if env.machines[for_machine].is_darwin():
             candidates.append(functools.partial(ExtraFrameworkDependency, name, env, kwargs))
 
         # Only use CMake as a last resort, since it might not work 100% (see #6113)


Test patch: