| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2021 The Meson development team |
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from .common import cmake_is_debug |
| 6 |
from .. import mlog |
| 7 |
from ..mesonlib import Version |
| 8 |
|
| 9 |
from pathlib import Path |
| 10 |
import re |
| 11 |
import typing as T |
| 12 |
|
| 13 |
if T.TYPE_CHECKING: |
| 14 |
from .traceparser import CMakeTraceParser |
| 15 |
from ..environment import Environment |
| 16 |
from ..compilers import Compiler |
| 17 |
from ..dependencies import MissingCompiler |
| 18 |
|
| 19 |
# Small duplication of ExtraFramework to parse full |
| 20 |
# framework paths as exposed by CMake |
| 21 |
def _get_framework_latest_version(path: Path) -> str: |
| 22 |
versions: list[Version] = [] |
| 23 |
for each in path.glob('Versions/*'): |
| 24 |
# macOS filesystems are usually case-insensitive |
| 25 |
if each.name.lower() == 'current': |
| 26 |
continue |
| 27 |
versions.append(Version(each.name)) |
| 28 |
if len(versions) == 0: |
| 29 |
# most system frameworks do not have a 'Versions' directory |
| 30 |
return 'Headers' |
| 31 |
return 'Versions/{}/Headers'.format(sorted(versions)[-1]._s) |
| 32 |
|
| 33 |
def _get_framework_include_path(path: Path) -> T.Optional[str]: |
| 34 |
trials = ('Headers', 'Versions/Current/Headers', _get_framework_latest_version(path)) |
| 35 |
for each in trials: |
| 36 |
trial = path / each |
| 37 |
if trial.is_dir(): |
| 38 |
return trial.as_posix() |
| 39 |
return None |
| 40 |
|
| 41 |
class ResolvedTarget: |
| 42 |
def __init__(self) -> None: |
| 43 |
self.include_directories: T.List[str] = [] |
| 44 |
self.link_flags: T.List[str] = [] |
| 45 |
self.public_link_flags: T.List[str] = [] |
| 46 |
self.public_compile_opts: T.List[str] = [] |
| 47 |
self.libraries: T.List[str] = [] |
| 48 |
self.target_dependencies: T.List[str] = [] |
| 49 |
|
| 50 |
def resolve_cmake_trace_targets(target_name: str, |
| 51 |
trace: 'CMakeTraceParser', |
| 52 |
env: 'Environment', |
| 53 |
*, |
| 54 |
clib_compiler: T.Union['MissingCompiler', 'Compiler'] = None, |
| 55 |
not_found_warning: T.Callable[[str], None] = lambda x: None) -> ResolvedTarget: |
| 56 |
res = ResolvedTarget() |
| 57 |
targets = [target_name] |
| 58 |
|
| 59 |
# recognise arguments we should pass directly to the linker |
| 60 |
reg_is_lib = re.compile(r'^(-l[a-zA-Z0-9_]+|-l?pthread)$') |
| 61 |
reg_is_maybe_bare_lib = re.compile(r'^[a-zA-Z0-9_]+$') |
| 62 |
|
| 63 |
is_debug = cmake_is_debug(env) |
| 64 |
|
| 65 |
processed_targets: T.List[str] = [] |
| 66 |
while len(targets) > 0: |
| 67 |
curr = targets.pop(0) |
| 68 |
|
| 69 |
# Skip already processed targets |
| 70 |
if curr in processed_targets: |
| 71 |
continue |
| 72 |
|
| 73 |
if curr not in trace.targets: |
| 74 |
curr_path = Path(curr) |
| 75 |
if reg_is_lib.match(curr): |
| 76 |
res.libraries += [curr] |
| 77 |
elif curr_path.is_absolute() and curr_path.exists(): |
| 78 |
if any(x.endswith('.framework') for x in curr_path.parts): |
| 79 |
# Frameworks detected by CMake are passed as absolute paths |
| 80 |
# Split into -F/path/to/ and -framework name |
| 81 |
path_to_framework = [] |
| 82 |
# Try to slice off the `Versions/X/name.tbd` |
| 83 |
for x in curr_path.parts: |
| 84 |
path_to_framework.append(x) |
| 85 |
if x.endswith('.framework'): |
| 86 |
break |
| 87 |
curr_path = Path(*path_to_framework) |
| 88 |
framework_path = curr_path.parent |
| 89 |
framework_name = curr_path.stem |
| 90 |
res.public_compile_opts += [f"-F{framework_path}"] |
| 91 |
res.libraries += [f'-F{framework_path}', '-framework', framework_name] |
| 92 |
else: |
| 93 |
res.libraries += [curr] |
| 94 |
elif reg_is_maybe_bare_lib.match(curr) and clib_compiler: |
| 95 |
# CMake library dependencies can be passed as bare library names, |
| 96 |
# CMake brute-forces a combination of prefix/suffix combinations to find the |
| 97 |
# right library. Assume any bare argument passed which is not also a CMake |
| 98 |
# target must be a system library we should try to link against. |
| 99 |
flib = clib_compiler.find_library(curr, []) |
| 100 |
if flib is not None: |
| 101 |
res.libraries += flib |
| 102 |
else: |
| 103 |
not_found_warning(curr) |
| 104 |
else: |
| 105 |
not_found_warning(curr) |
| 106 |
continue |
| 107 |
|
| 108 |
tgt = trace.targets[curr] |
| 109 |
cfgs = [] |
| 110 |
cfg = '' |
| 111 |
mlog.debug(tgt) |
| 112 |
|
| 113 |
if 'INTERFACE_INCLUDE_DIRECTORIES' in tgt.properties: |
| 114 |
res.include_directories += [x for x in tgt.properties['INTERFACE_INCLUDE_DIRECTORIES'] if x] |
| 115 |
|
| 116 |
if 'INTERFACE_LINK_OPTIONS' in tgt.properties: |
| 117 |
res.public_link_flags += [x for x in tgt.properties['INTERFACE_LINK_OPTIONS'] if x] |
| 118 |
res.link_flags += res.public_link_flags |
| 119 |
|
| 120 |
if 'INTERFACE_COMPILE_DEFINITIONS' in tgt.properties: |
| 121 |
res.public_compile_opts += ['-D' + re.sub('^-D', '', x) for x in tgt.properties['INTERFACE_COMPILE_DEFINITIONS'] if x] |
| 122 |
|
| 123 |
if 'INTERFACE_COMPILE_OPTIONS' in tgt.properties: |
| 124 |
res.public_compile_opts += [x for x in tgt.properties['INTERFACE_COMPILE_OPTIONS'] if x] |
| 125 |
|
| 126 |
if 'IMPORTED_CONFIGURATIONS' in tgt.properties: |
| 127 |
cfgs = [x for x in tgt.properties['IMPORTED_CONFIGURATIONS'] if x] |
| 128 |
cfg = cfgs[0] |
| 129 |
|
| 130 |
if is_debug: |
| 131 |
if 'DEBUG' in cfgs: |
| 132 |
cfg = 'DEBUG' |
| 133 |
elif 'RELEASE' in cfgs: |
| 134 |
cfg = 'RELEASE' |
| 135 |
else: |
| 136 |
if 'RELEASE' in cfgs: |
| 137 |
cfg = 'RELEASE' |
| 138 |
|
| 139 |
if f'IMPORTED_IMPLIB_{cfg}' in tgt.properties: |
| 140 |
res.libraries += [x for x in tgt.properties[f'IMPORTED_IMPLIB_{cfg}'] if x] |
| 141 |
elif 'IMPORTED_IMPLIB' in tgt.properties: |
| 142 |
res.libraries += [x for x in tgt.properties['IMPORTED_IMPLIB'] if x] |
| 143 |
elif f'IMPORTED_LOCATION_{cfg}' in tgt.properties: |
| 144 |
targets += [x for x in tgt.properties[f'IMPORTED_LOCATION_{cfg}'] if x] |
| 145 |
elif 'IMPORTED_LOCATION' in tgt.properties: |
| 146 |
targets += [x for x in tgt.properties['IMPORTED_LOCATION'] if x] |
| 147 |
|
| 148 |
if 'LINK_LIBRARIES' in tgt.properties: |
| 149 |
link_libraries = [x for x in tgt.properties['LINK_LIBRARIES'] if x] |
| 150 |
targets += link_libraries |
| 151 |
res.target_dependencies += link_libraries |
| 152 |
if 'INTERFACE_LINK_LIBRARIES' in tgt.properties: |
| 153 |
link_libraries = [x for x in tgt.properties['INTERFACE_LINK_LIBRARIES'] if x] |
| 154 |
targets += link_libraries |
| 155 |
res.target_dependencies += link_libraries |
| 156 |
|
| 157 |
if f'IMPORTED_LINK_DEPENDENT_LIBRARIES_{cfg}' in tgt.properties: |
| 158 |
targets += [x for x in tgt.properties[f'IMPORTED_LINK_DEPENDENT_LIBRARIES_{cfg}'] if x] |
| 159 |
elif 'IMPORTED_LINK_DEPENDENT_LIBRARIES' in tgt.properties: |
| 160 |
targets += [x for x in tgt.properties['IMPORTED_LINK_DEPENDENT_LIBRARIES'] if x] |
| 161 |
|
| 162 |
processed_targets += [curr] |
| 163 |
|
| 164 |
# Do not sort flags here -- this breaks |
| 165 |
# semantics of eg. `-framework CoreAudio` |
| 166 |
# or `-Lpath/to/root -llibrary` |
| 167 |
# see eg. #11113 |
| 168 |
|
| 169 |
return res |