Debian Codegraph @

mesonbuild/dependencies/framework.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2013-2021 The Meson development team
3
4 from __future__ import annotations
5
6 from .base import DependencyTypeName, ExternalDependency, DependencyException
7 from ..mesonlib import MesonException, Version
8 from .. import mlog
9 from pathlib import Path
10 import typing as T
11
12 if T.TYPE_CHECKING:
13 from ..environment import Environment
14 from .base import DependencyObjectKWs
15
16 class ExtraFrameworkDependency(ExternalDependency):
17 system_framework_paths: T.Optional[T.List[str]] = None
18
19 type_name = DependencyTypeName('extraframeworks')
20
21 def __init__(self, name: str, env: 'Environment', kwargs: DependencyObjectKWs) -> None:
22 paths = kwargs.get('paths', [])
23 super().__init__(name, env, kwargs)
24 # Full path to framework directory
25 self.framework_path: T.Optional[str] = None
26 if not self.clib_compiler:
27 raise DependencyException('No C-like compilers are available')
28 if self.system_framework_paths is None:
29 try:
30 self.system_framework_paths = self.clib_compiler.find_framework_paths()
31 except MesonException as e:
32 if 'non-clang' in str(e):
33 # Apple frameworks can only be found (and used) with the
34 # system compiler. It is not available so bail immediately.
35 self.is_found = False
36 return
37 raise
38 self.detect(name, paths)
39
40 def detect(self, name: str, paths: T.List[str]) -> None:
41 if not paths:
42 paths = self.system_framework_paths
43 for p in paths:
44 mlog.debug(f'Looking for framework {name} in {p}')
45 # We need to know the exact framework path because it's used by the
46 # Qt5 dependency class, and for setting the include path. We also
47 # want to avoid searching in an invalid framework path which wastes
48 # time and can cause a false positive.
49 framework_path = self._get_framework_path(p, name)
50 if framework_path is None:
51 continue
52 framework_name = framework_path.stem
53 # We want to prefer the specified paths (in order) over the system
54 # paths since these are "extra" frameworks.
55 # For example, Python2's framework is in /System/Library/Frameworks and
56 # Python3's framework is in /Library/Frameworks, but both are called
57 # Python.framework. We need to know for sure that the framework was
58 # found in the path we expect.
59 allow_system = p in self.system_framework_paths
60 args = self.clib_compiler.find_framework(framework_name, [p], allow_system)
61 if args is None:
62 continue
63 self.link_args = args
64 self.framework_path = framework_path.as_posix()
65 # The search is done case-insensitively, so the found name may differ
66 # from the one that was requested. Setting the name ensures the correct
67 # one is used when linking on case-sensitive filesystems.
68 self.name = framework_name
69 self.compile_args = ['-F' + self.framework_path]
70 # We need to also add -I includes to the framework because all
71 # cross-platform projects such as OpenGL, Python, Qt, GStreamer,
72 # etc do not use "framework includes":
73 # https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Tasks/IncludingFrameworks.html
74 incdir = self._get_framework_include_path(framework_path)
75 if incdir:
76 self.compile_args += ['-idirafter' + incdir]
77 self.is_found = True
78 return
79
80 def _get_framework_path(self, path: str, name: str) -> T.Optional[Path]:
81 p = Path(path)
82 lname = name.lower()
83 for d in p.glob('*.framework/'):
84 if lname == d.stem.lower():
85 return d
86 return None
87
88 def _get_framework_latest_version(self, path: Path) -> str:
89 versions: T.List[Version] = []
90 for each in path.glob('Versions/*'):
91 # macOS filesystems are usually case-insensitive
92 if each.name.lower() == 'current':
93 continue
94 versions.append(Version(each.name))
95 if len(versions) == 0:
96 # most system frameworks do not have a 'Versions' directory
97 return 'Headers'
98 return 'Versions/{}/Headers'.format(sorted(versions)[-1]._s)
99
100 def _get_framework_include_path(self, path: Path) -> T.Optional[str]:
101 # According to the spec, 'Headers' must always be a symlink to the
102 # Headers directory inside the currently-selected version of the
103 # framework, but sometimes frameworks are broken. Look in 'Versions'
104 # for the currently-selected version or pick the latest one.
105 trials = ('Headers', 'Versions/Current/Headers',
106 self._get_framework_latest_version(path))
107 for each in trials:
108 trial = path / each
109 if trial.is_dir():
110 return trial.as_posix()
111 return None
112
113 def log_info(self) -> str:
114 return self.framework_path or ''