| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2013-2019 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import glob |
| 7 |
import os |
| 8 |
import re |
| 9 |
import pathlib |
| 10 |
import shutil |
| 11 |
import subprocess |
| 12 |
import typing as T |
| 13 |
import functools |
| 14 |
|
| 15 |
from mesonbuild.interpreterbase.decorators import FeatureDeprecated |
| 16 |
|
| 17 |
from .. import mesonlib, mlog |
| 18 |
from ..tooldetect import get_llvm_tool_names |
| 19 |
from ..mesonlib import version_compare, version_compare_many, search_version |
| 20 |
from .base import DependencyException, DependencyMethods, detect_compiler, strip_system_includedirs, strip_system_libdirs, SystemDependency, ExternalDependency, DependencyCandidate |
| 21 |
from .cmake import CMakeDependency |
| 22 |
from .configtool import ConfigToolDependency |
| 23 |
from .detect import packages |
| 24 |
from .factory import DependencyFactory |
| 25 |
from .misc import threads_factory |
| 26 |
from .pkgconfig import PkgConfigDependency |
| 27 |
|
| 28 |
if T.TYPE_CHECKING: |
| 29 |
from ..envconfig import MachineInfo |
| 30 |
from ..environment import Environment |
| 31 |
from ..compilers import Compiler |
| 32 |
from ..mesonlib import MachineChoice |
| 33 |
from ..interpreter.type_checking import PkgConfigDefineType |
| 34 |
from .base import DependencyObjectKWs |
| 35 |
|
| 36 |
|
| 37 |
def get_shared_library_suffix(environment: 'Environment', for_machine: MachineChoice) -> str: |
| 38 |
"""This is only guaranteed to work for languages that compile to machine |
| 39 |
code, not for languages like C# that use a bytecode and always end in .dll |
| 40 |
""" |
| 41 |
m = environment.machines[for_machine] |
| 42 |
if m.is_windows(): |
| 43 |
return '.dll' |
| 44 |
elif m.is_darwin(): |
| 45 |
return '.dylib' |
| 46 |
return '.so' |
| 47 |
|
| 48 |
|
| 49 |
class GTestDependencySystem(SystemDependency): |
| 50 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs) -> None: |
| 51 |
kwargs['language'] = 'cpp' |
| 52 |
super().__init__(name, environment, kwargs) |
| 53 |
self.main = kwargs.get('main', False) |
| 54 |
|
| 55 |
sysroot = environment.properties[self.for_machine].get_sys_root() or '' |
| 56 |
self.src_dirs = [sysroot + '/usr/src/gtest/src', sysroot + '/usr/src/googletest/googletest/src'] |
| 57 |
if not self._add_sub_dependency(threads_factory(environment, {'native': self.for_machine})): |
| 58 |
self.is_found = False |
| 59 |
return |
| 60 |
self.detect() |
| 61 |
|
| 62 |
def detect(self) -> None: |
| 63 |
gtest_detect = self.clib_compiler.find_library("gtest", []) |
| 64 |
gtest_main_detect = self.clib_compiler.find_library("gtest_main", []) |
| 65 |
if gtest_detect and (not self.main or gtest_main_detect): |
| 66 |
self.is_found = True |
| 67 |
self.compile_args = [] |
| 68 |
self.link_args = gtest_detect |
| 69 |
if self.main: |
| 70 |
self.link_args += gtest_main_detect |
| 71 |
self.sources = [] |
| 72 |
self.prebuilt = True |
| 73 |
elif self.detect_srcdir(): |
| 74 |
self.is_found = True |
| 75 |
self.compile_args = ['-I' + d for d in self.src_include_dirs] |
| 76 |
self.link_args = [] |
| 77 |
if self.main: |
| 78 |
self.sources = [self.all_src, self.main_src] |
| 79 |
else: |
| 80 |
self.sources = [self.all_src] |
| 81 |
self.prebuilt = False |
| 82 |
else: |
| 83 |
self.is_found = False |
| 84 |
|
| 85 |
def detect_srcdir(self) -> bool: |
| 86 |
for s in self.src_dirs: |
| 87 |
if os.path.exists(s): |
| 88 |
self.src_dir = s |
| 89 |
self.all_src = mesonlib.File.from_absolute_file( |
| 90 |
os.path.join(self.src_dir, 'gtest-all.cc')) |
| 91 |
self.main_src = mesonlib.File.from_absolute_file( |
| 92 |
os.path.join(self.src_dir, 'gtest_main.cc')) |
| 93 |
self.src_include_dirs = [os.path.normpath(os.path.join(self.src_dir, '..')), |
| 94 |
os.path.normpath(os.path.join(self.src_dir, '../include')), |
| 95 |
] |
| 96 |
return True |
| 97 |
return False |
| 98 |
|
| 99 |
def log_info(self) -> str: |
| 100 |
if self.prebuilt: |
| 101 |
return 'prebuilt' |
| 102 |
else: |
| 103 |
return 'building self' |
| 104 |
|
| 105 |
|
| 106 |
class GTestDependencyPC(PkgConfigDependency): |
| 107 |
|
| 108 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs): |
| 109 |
assert name == 'gtest' |
| 110 |
if kwargs.get('main'): |
| 111 |
name = 'gtest_main' |
| 112 |
super().__init__(name, environment, kwargs) |
| 113 |
|
| 114 |
|
| 115 |
class GMockDependencySystem(SystemDependency): |
| 116 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs) -> None: |
| 117 |
kwargs['language'] = 'cpp' |
| 118 |
super().__init__(name, environment, kwargs) |
| 119 |
self.main = kwargs.get('main', False) |
| 120 |
if not self._add_sub_dependency(threads_factory(environment, {'native': self.for_machine})): |
| 121 |
self.is_found = False |
| 122 |
return |
| 123 |
|
| 124 |
# If we are getting main() from GMock, we definitely |
| 125 |
# want to avoid linking in main() from GTest |
| 126 |
gtest_kwargs = kwargs.copy() |
| 127 |
gtest_kwargs['native'] = self.for_machine |
| 128 |
if self.main: |
| 129 |
gtest_kwargs['main'] = False |
| 130 |
|
| 131 |
# GMock without GTest is pretty much useless |
| 132 |
# this also mimics the structure given in WrapDB, |
| 133 |
# where GMock always pulls in GTest |
| 134 |
found = self._add_sub_dependency(gtest_factory(environment, gtest_kwargs)) |
| 135 |
if not found: |
| 136 |
self.is_found = False |
| 137 |
return |
| 138 |
|
| 139 |
# GMock may be a library or just source. |
| 140 |
# Work with both. |
| 141 |
gmock_detect = self.clib_compiler.find_library("gmock", []) |
| 142 |
gmock_main_detect = self.clib_compiler.find_library("gmock_main", []) |
| 143 |
if gmock_detect and (not self.main or gmock_main_detect): |
| 144 |
self.is_found = True |
| 145 |
self.link_args += gmock_detect |
| 146 |
if self.main: |
| 147 |
self.link_args += gmock_main_detect |
| 148 |
self.prebuilt = True |
| 149 |
return |
| 150 |
|
| 151 |
for d in ['/usr/src/googletest/googlemock/src', '/usr/src/gmock/src', '/usr/src/gmock']: |
| 152 |
if os.path.exists(d): |
| 153 |
self.is_found = True |
| 154 |
# Yes, we need both because there are multiple |
| 155 |
# versions of gmock that do different things. |
| 156 |
d2 = os.path.normpath(os.path.join(d, '..')) |
| 157 |
self.compile_args += ['-I' + d, '-I' + d2, '-I' + os.path.join(d2, 'include')] |
| 158 |
all_src = mesonlib.File.from_absolute_file(os.path.join(d, 'gmock-all.cc')) |
| 159 |
main_src = mesonlib.File.from_absolute_file(os.path.join(d, 'gmock_main.cc')) |
| 160 |
if self.main: |
| 161 |
self.sources += [all_src, main_src] |
| 162 |
else: |
| 163 |
self.sources += [all_src] |
| 164 |
self.prebuilt = False |
| 165 |
return |
| 166 |
|
| 167 |
self.is_found = False |
| 168 |
|
| 169 |
def log_info(self) -> str: |
| 170 |
if self.prebuilt: |
| 171 |
return 'prebuilt' |
| 172 |
else: |
| 173 |
return 'building self' |
| 174 |
|
| 175 |
|
| 176 |
class GMockDependencyPC(PkgConfigDependency): |
| 177 |
|
| 178 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs): |
| 179 |
assert name == 'gmock' |
| 180 |
if kwargs.get('main'): |
| 181 |
name = 'gmock_main' |
| 182 |
super().__init__(name, environment, kwargs) |
| 183 |
|
| 184 |
|
| 185 |
class LLVMDependencyConfigTool(ConfigToolDependency): |
| 186 |
""" |
| 187 |
LLVM uses a special tool, llvm-config, which has arguments for getting |
| 188 |
c args, cxx args, and ldargs as well as version. |
| 189 |
""" |
| 190 |
tool_name = 'llvm-config' |
| 191 |
__cpp_blacklist = {'-DNDEBUG'} |
| 192 |
|
| 193 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs): |
| 194 |
kwargs['language'] = 'cpp' |
| 195 |
self.tools = get_llvm_tool_names('llvm-config') |
| 196 |
|
| 197 |
# Fedora starting with Fedora 30 adds a suffix of the number |
| 198 |
# of bits in the isa that llvm targets, for example, on x86_64 |
| 199 |
# and aarch64 the name will be llvm-config-64, on x86 and arm |
| 200 |
# it will be llvm-config-32. |
| 201 |
if environment.machines[kwargs['native']].is_64_bit: |
| 202 |
self.tools.append('llvm-config-64') |
| 203 |
else: |
| 204 |
self.tools.append('llvm-config-32') |
| 205 |
|
| 206 |
# It's necessary for LLVM <= 3.8 to use the C++ linker. For 3.9 and 4.0 |
| 207 |
# the C linker works fine if only using the C API. |
| 208 |
super().__init__(name, environment, kwargs) |
| 209 |
self.provided_modules: T.List[str] = [] |
| 210 |
self.required_modules: mesonlib.OrderedSet[str] = mesonlib.OrderedSet() |
| 211 |
self.module_details: T.List[str] = [] |
| 212 |
if not self.is_found: |
| 213 |
return |
| 214 |
|
| 215 |
self.provided_modules = self.get_config_value(['--components'], 'modules') |
| 216 |
modules = kwargs.get('modules', []) |
| 217 |
self.check_components(modules) |
| 218 |
opt_modules = kwargs.get('optional_modules', []) |
| 219 |
self.check_components(opt_modules, required=False) |
| 220 |
|
| 221 |
cargs = mesonlib.OrderedSet(self.get_config_value(['--cppflags'], 'compile_args')) |
| 222 |
self.compile_args = list(cargs.difference(self.__cpp_blacklist)) |
| 223 |
self.compile_args = strip_system_includedirs(environment, self.for_machine, self.compile_args) |
| 224 |
|
| 225 |
if version_compare(self.version, '>= 3.9'): |
| 226 |
self._set_new_link_args(environment) |
| 227 |
else: |
| 228 |
self._set_old_link_args() |
| 229 |
self.link_args = strip_system_libdirs(environment, self.for_machine, self.link_args) |
| 230 |
self.link_args = self.__fix_bogus_link_args(self.link_args) |
| 231 |
if not self._add_sub_dependency(threads_factory(environment, {'native': self.for_machine})): |
| 232 |
self.is_found = False |
| 233 |
return |
| 234 |
|
| 235 |
def __fix_bogus_link_args(self, args: T.List[str]) -> T.List[str]: |
| 236 |
"""This function attempts to fix bogus link arguments that llvm-config |
| 237 |
generates. |
| 238 |
|
| 239 |
Currently it works around the following: |
| 240 |
- FreeBSD: when statically linking -l/usr/lib/libexecinfo.so will |
| 241 |
be generated, strip the -l in cases like this. |
| 242 |
- Windows: We may get -LIBPATH:... which is later interpreted as |
| 243 |
"-L IBPATH:...", if we're using an msvc like compilers convert |
| 244 |
that to "/LIBPATH", otherwise to "-L ..." |
| 245 |
""" |
| 246 |
|
| 247 |
new_args = [] |
| 248 |
for arg in args: |
| 249 |
if arg.startswith('-l') and arg.endswith('.so'): |
| 250 |
new_args.append(arg.lstrip('-l')) |
| 251 |
elif arg.startswith('-LIBPATH:'): |
| 252 |
cpp = self.env.coredata.compilers[self.for_machine]['cpp'] |
| 253 |
new_args.extend(cpp.get_linker_search_args(arg.lstrip('-LIBPATH:'))) |
| 254 |
else: |
| 255 |
new_args.append(arg) |
| 256 |
return new_args |
| 257 |
|
| 258 |
def __check_libfiles(self, shared: bool) -> None: |
| 259 |
"""Use llvm-config's --libfiles to check if libraries exist.""" |
| 260 |
mode = '--link-shared' if shared else '--link-static' |
| 261 |
|
| 262 |
# Set self.required to true to force an exception in get_config_value |
| 263 |
# if the returncode != 0 |
| 264 |
restore = self.required |
| 265 |
self.required = True |
| 266 |
|
| 267 |
try: |
| 268 |
# It doesn't matter what the stage is, the caller needs to catch |
| 269 |
# the exception anyway. |
| 270 |
self.link_args = self.get_config_value(['--libfiles', mode], '') |
| 271 |
finally: |
| 272 |
self.required = restore |
| 273 |
|
| 274 |
def _set_new_link_args(self, environment: 'Environment') -> None: |
| 275 |
"""How to set linker args for LLVM versions >= 3.9""" |
| 276 |
try: |
| 277 |
mode = self.get_config_value(['--shared-mode'], 'link_args')[0] |
| 278 |
except IndexError: |
| 279 |
mlog.debug('llvm-config --shared-mode returned an error') |
| 280 |
self.is_found = False |
| 281 |
return |
| 282 |
|
| 283 |
if not self.static and mode == 'static': |
| 284 |
# If llvm is configured with LLVM_BUILD_LLVM_DYLIB but not with |
| 285 |
# LLVM_LINK_LLVM_DYLIB and not LLVM_BUILD_SHARED_LIBS (which |
| 286 |
# upstream doesn't recommend using), then llvm-config will lie to |
| 287 |
# you about how to do shared-linking. It wants to link to a a bunch |
| 288 |
# of individual shared libs (which don't exist because llvm wasn't |
| 289 |
# built with LLVM_BUILD_SHARED_LIBS. |
| 290 |
# |
| 291 |
# Therefore, we'll try to get the libfiles, if the return code is 0 |
| 292 |
# or we get an empty list, then we'll try to build a working |
| 293 |
# configuration by hand. |
| 294 |
try: |
| 295 |
self.__check_libfiles(True) |
| 296 |
except DependencyException: |
| 297 |
lib_ext = get_shared_library_suffix(environment, self.for_machine) |
| 298 |
libdir = self.get_config_value(['--libdir'], 'link_args')[0] |
| 299 |
# Sort for reproducibility |
| 300 |
matches = sorted(glob.iglob(os.path.join(libdir, f'libLLVM*{lib_ext}'))) |
| 301 |
if not matches: |
| 302 |
if self.required: |
| 303 |
raise |
| 304 |
self.is_found = False |
| 305 |
return |
| 306 |
|
| 307 |
self.link_args = self.get_config_value(['--ldflags'], 'link_args') |
| 308 |
libname = os.path.basename(matches[0]).rstrip(lib_ext).lstrip('lib') |
| 309 |
self.link_args.append(f'-l{libname}') |
| 310 |
return |
| 311 |
elif self.static and mode == 'shared': |
| 312 |
# If, however LLVM_BUILD_SHARED_LIBS is true # (*cough* gentoo *cough*) |
| 313 |
# then this is correct. Building with LLVM_BUILD_SHARED_LIBS has a side |
| 314 |
# effect, it stops the generation of static archives. Therefore we need |
| 315 |
# to check for that and error out on static if this is the case |
| 316 |
try: |
| 317 |
self.__check_libfiles(False) |
| 318 |
except DependencyException: |
| 319 |
if self.required: |
| 320 |
raise |
| 321 |
self.is_found = False |
| 322 |
return |
| 323 |
|
| 324 |
link_args = ['--link-static', '--system-libs'] if self.static else ['--link-shared'] |
| 325 |
self.link_args = self.get_config_value( |
| 326 |
['--libs', '--ldflags'] + link_args + list(self.required_modules), |
| 327 |
'link_args') |
| 328 |
|
| 329 |
def _set_old_link_args(self) -> None: |
| 330 |
"""Setting linker args for older versions of llvm. |
| 331 |
|
| 332 |
Old versions of LLVM bring an extra level of insanity with them. |
| 333 |
llvm-config will provide the correct arguments for static linking, but |
| 334 |
not for shared-linking, we have to figure those out ourselves, because |
| 335 |
of course we do. |
| 336 |
""" |
| 337 |
if self.static: |
| 338 |
self.link_args = self.get_config_value( |
| 339 |
['--libs', '--ldflags', '--system-libs'] + list(self.required_modules), |
| 340 |
'link_args') |
| 341 |
else: |
| 342 |
# llvm-config will provide arguments for static linking, so we get |
| 343 |
# to figure out for ourselves what to link with. We'll do that by |
| 344 |
# checking in the directory provided by --libdir for a library |
| 345 |
# called libLLVM-<ver>.(so|dylib|dll) |
| 346 |
libdir = self.get_config_value(['--libdir'], 'link_args')[0] |
| 347 |
|
| 348 |
expected_name = f'libLLVM-{self.version}' |
| 349 |
re_name = re.compile(fr'{expected_name}.(so|dll|dylib)$') |
| 350 |
|
| 351 |
for file_ in os.listdir(libdir): |
| 352 |
if re_name.match(file_): |
| 353 |
self.link_args = [f'-L{libdir}', |
| 354 |
'-l{}'.format(os.path.splitext(file_.lstrip('lib'))[0])] |
| 355 |
break |
| 356 |
else: |
| 357 |
raise DependencyException( |
| 358 |
'Could not find a dynamically linkable library for LLVM.') |
| 359 |
|
| 360 |
def check_components(self, modules: T.List[str], required: bool = True) -> None: |
| 361 |
"""Check for llvm components (modules in meson terms). |
| 362 |
|
| 363 |
The required option is whether the module is required, not whether LLVM |
| 364 |
is required. |
| 365 |
""" |
| 366 |
for mod in sorted(set(modules)): |
| 367 |
status = '' |
| 368 |
|
| 369 |
if mod not in self.provided_modules: |
| 370 |
if required: |
| 371 |
self.is_found = False |
| 372 |
if self.required: |
| 373 |
raise DependencyException( |
| 374 |
f'Could not find required LLVM Component: {mod}') |
| 375 |
status = '(missing)' |
| 376 |
else: |
| 377 |
status = '(missing but optional)' |
| 378 |
else: |
| 379 |
self.required_modules.add(mod) |
| 380 |
|
| 381 |
self.module_details.append(mod + status) |
| 382 |
|
| 383 |
def log_details(self) -> str: |
| 384 |
if self.module_details: |
| 385 |
return 'modules: ' + ', '.join(self.module_details) |
| 386 |
return '' |
| 387 |
|
| 388 |
class LLVMDependencyCMake(CMakeDependency): |
| 389 |
def __init__(self, name: str, env: 'Environment', kwargs: DependencyObjectKWs) -> None: |
| 390 |
kwargs['language'] = 'cpp' |
| 391 |
self.llvm_modules = kwargs.get('modules', []) |
| 392 |
self.llvm_opt_modules = kwargs.get('optional_modules', []) |
| 393 |
|
| 394 |
for_machine = kwargs['native'] |
| 395 |
compilers = env.coredata.compilers[for_machine] |
| 396 |
if not compilers or not {'c', 'cpp'}.issubset(compilers): |
| 397 |
# Initialize basic variables |
| 398 |
ExternalDependency.__init__(self, name, env, kwargs) |
| 399 |
|
| 400 |
# Initialize CMake specific variables |
| 401 |
self.found_modules: T.List[str] = [] |
| 402 |
|
| 403 |
langs: T.List[str] = [] |
| 404 |
if not compilers: |
| 405 |
langs = ['c', 'cpp'] |
| 406 |
else: |
| 407 |
if 'c' not in compilers: |
| 408 |
langs.append('c') |
| 409 |
if 'cpp' not in compilers: |
| 410 |
langs.append('cpp') |
| 411 |
|
| 412 |
mlog.warning( |
| 413 |
'The LLVM dependency was not found via CMake, as this method requires', |
| 414 |
'both a C and C++ compiler to be enabled, but', |
| 415 |
'only' if len(langs) == 1 else 'neither', |
| 416 |
'a', |
| 417 |
" nor ".join(l.upper() for l in langs).replace('CPP', 'C++'), |
| 418 |
'compiler is enabled for the', |
| 419 |
f"{self.for_machine}.", |
| 420 |
'Consider adding "{0}" to your project() call or using add_languages({0}, native : {1})'.format( |
| 421 |
', '.join(f"'{l}'" for l in langs), |
| 422 |
'true' if self.for_machine is mesonlib.MachineChoice.BUILD else 'false', |
| 423 |
), |
| 424 |
'before the LLVM dependency lookup.', |
| 425 |
fatal=False, |
| 426 |
) |
| 427 |
return |
| 428 |
|
| 429 |
super().__init__(name, env, kwargs, force_use_global_compilers=True) |
| 430 |
|
| 431 |
if not self.cmakebin.found(): |
| 432 |
return |
| 433 |
|
| 434 |
if not self.is_found: |
| 435 |
return |
| 436 |
|
| 437 |
# CMake will return not found due to not defined LLVM_DYLIB_COMPONENTS |
| 438 |
if not self.static and version_compare(self.version, '< 7.0') and self.llvm_modules: |
| 439 |
mlog.warning('Before version 7.0 cmake does not export modules for dynamic linking, cannot check required modules') |
| 440 |
return |
| 441 |
|
| 442 |
# Extract extra include directories and definitions |
| 443 |
inc_dirs = self.traceparser.get_cmake_var('PACKAGE_INCLUDE_DIRS') |
| 444 |
defs = self.traceparser.get_cmake_var('PACKAGE_DEFINITIONS') |
| 445 |
# LLVM explicitly uses space-separated variables rather than semicolon lists |
| 446 |
if len(defs) == 1: |
| 447 |
defs = defs[0].split(' ') |
| 448 |
temp = ['-I' + x for x in inc_dirs] + defs |
| 449 |
self.compile_args += [x for x in temp if x not in self.compile_args] |
| 450 |
self.compile_args = strip_system_includedirs(env, self.for_machine, self.compile_args) |
| 451 |
if not self._add_sub_dependency(threads_factory(env, {'native': self.for_machine})): |
| 452 |
self.is_found = False |
| 453 |
return |
| 454 |
|
| 455 |
def _main_cmake_file(self) -> str: |
| 456 |
# Use a custom CMakeLists.txt for LLVM |
| 457 |
return 'CMakeListsLLVM.txt' |
| 458 |
|
| 459 |
# Check version in CMake to return exact version as config tool (latest allowed) |
| 460 |
# It is safe to add .0 to latest argument, it will discarded if we use search_version |
| 461 |
def llvm_cmake_versions(self) -> T.List[str]: |
| 462 |
|
| 463 |
def ver_from_suf(req: str) -> str: |
| 464 |
return search_version(req.strip('-')+'.0') |
| 465 |
|
| 466 |
def version_sorter(a: str, b: str) -> int: |
| 467 |
if version_compare(a, "="+b): |
| 468 |
return 0 |
| 469 |
if version_compare(a, "<"+b): |
| 470 |
return 1 |
| 471 |
return -1 |
| 472 |
|
| 473 |
llvm_requested_versions = [ver_from_suf(x) for x in get_llvm_tool_names('') if version_compare(ver_from_suf(x), '>=0')] |
| 474 |
if self.version_reqs: |
| 475 |
llvm_requested_versions = [ver_from_suf(x) for x in get_llvm_tool_names('') if version_compare_many(ver_from_suf(x), self.version_reqs)] |
| 476 |
# CMake sorting before 3.18 is incorrect, sort it here instead |
| 477 |
return sorted(llvm_requested_versions, key=functools.cmp_to_key(version_sorter)) |
| 478 |
|
| 479 |
# Split required and optional modules to distinguish it in CMake |
| 480 |
def _extra_cmake_opts(self) -> T.List[str]: |
| 481 |
return ['-DLLVM_MESON_REQUIRED_MODULES={}'.format(';'.join(self.llvm_modules)), |
| 482 |
'-DLLVM_MESON_OPTIONAL_MODULES={}'.format(';'.join(self.llvm_opt_modules)), |
| 483 |
'-DLLVM_MESON_PACKAGE_NAMES={}'.format(';'.join(get_llvm_tool_names(self.name))), |
| 484 |
'-DLLVM_MESON_VERSIONS={}'.format(';'.join(self.llvm_cmake_versions())), |
| 485 |
'-DLLVM_MESON_DYLIB={}'.format('OFF' if self.static else 'ON')] |
| 486 |
|
| 487 |
def _map_module_list(self, modules: T.List[T.Tuple[str, bool]], components: T.List[T.Tuple[str, bool]]) -> T.List[T.Tuple[str, bool]]: |
| 488 |
res = [] |
| 489 |
for mod, required in modules: |
| 490 |
cm_targets = self.traceparser.get_cmake_var(f'MESON_LLVM_TARGETS_{mod}') |
| 491 |
if not cm_targets: |
| 492 |
if required: |
| 493 |
raise self._gen_exception(f'LLVM module {mod} was not found') |
| 494 |
else: |
| 495 |
mlog.warning('Optional LLVM module', mlog.bold(mod), 'was not found', fatal=False) |
| 496 |
continue |
| 497 |
for i in cm_targets: |
| 498 |
res += [(i, required)] |
| 499 |
return res |
| 500 |
|
| 501 |
def _original_module_name(self, module: str) -> str: |
| 502 |
orig_name = self.traceparser.get_cmake_var(f'MESON_TARGET_TO_LLVM_{module}') |
| 503 |
if orig_name: |
| 504 |
return orig_name[0] |
| 505 |
return module |
| 506 |
|
| 507 |
|
| 508 |
class ValgrindDependency(PkgConfigDependency): |
| 509 |
''' |
| 510 |
Consumers of Valgrind usually only need the compile args and do not want to |
| 511 |
link to its (static) libraries. |
| 512 |
''' |
| 513 |
def __init__(self, name: str, env: 'Environment', kwargs: DependencyObjectKWs): |
| 514 |
super().__init__(name, env, kwargs) |
| 515 |
|
| 516 |
def get_link_args(self, language: T.Optional[str] = None, raw: bool = False) -> T.List[str]: |
| 517 |
return [] |
| 518 |
|
| 519 |
packages['valgrind'] = ValgrindDependency |
| 520 |
|
| 521 |
|
| 522 |
class ZlibSystemDependency(SystemDependency): |
| 523 |
|
| 524 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs): |
| 525 |
super().__init__(name, environment, kwargs) |
| 526 |
from ..compilers.c import AppleClangCCompiler |
| 527 |
from ..compilers.cpp import AppleClangCPPCompiler |
| 528 |
|
| 529 |
m = self.env.machines[self.for_machine] |
| 530 |
|
| 531 |
# I'm not sure this is entirely correct. What if we're cross compiling |
| 532 |
# from something to macOS? |
| 533 |
if ((m.is_darwin() and isinstance(self.clib_compiler, (AppleClangCCompiler, AppleClangCPPCompiler))) or |
| 534 |
m.is_freebsd() or m.is_dragonflybsd() or m.is_android()): |
| 535 |
# No need to set includes, |
| 536 |
# on macos xcode/clang will do that for us. |
| 537 |
# on freebsd zlib.h is in /usr/include |
| 538 |
|
| 539 |
self.is_found = True |
| 540 |
self.link_args = ['-lz'] |
| 541 |
else: |
| 542 |
if self.clib_compiler.get_argument_syntax() == 'msvc': |
| 543 |
libs = ['zlib1', 'zlib'] |
| 544 |
else: |
| 545 |
libs = ['z'] |
| 546 |
for lib in libs: |
| 547 |
l = self.clib_compiler.find_library(lib, [], self.libtype) |
| 548 |
h = self.clib_compiler.has_header('zlib.h', '', dependencies=[self]) |
| 549 |
if l and h[0]: |
| 550 |
self.is_found = True |
| 551 |
self.link_args = l |
| 552 |
break |
| 553 |
else: |
| 554 |
return |
| 555 |
|
| 556 |
v, _ = self.clib_compiler.get_define('ZLIB_VERSION', '#include <zlib.h>', [], [self]) |
| 557 |
self.version = v.strip('"') |
| 558 |
|
| 559 |
|
| 560 |
class JNISystemDependency(SystemDependency): |
| 561 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs): |
| 562 |
super().__init__(name, environment, kwargs) |
| 563 |
|
| 564 |
self.feature_since = ('0.62.0', '') |
| 565 |
|
| 566 |
m = self.env.machines[self.for_machine] |
| 567 |
|
| 568 |
if 'java' not in environment.coredata.compilers[self.for_machine]: |
| 569 |
detect_compiler(self.name, environment, self.for_machine, 'java') |
| 570 |
self.javac = environment.coredata.compilers[self.for_machine]['java'] |
| 571 |
self.version = self.javac.version |
| 572 |
|
| 573 |
modules = kwargs.get('modules', []) |
| 574 |
for module in modules: |
| 575 |
if module not in {'jvm', 'awt'}: |
| 576 |
msg = f'Unknown JNI module ({module})' |
| 577 |
if self.required: |
| 578 |
mlog.error(msg) |
| 579 |
else: |
| 580 |
mlog.debug(msg) |
| 581 |
self.is_found = False |
| 582 |
return |
| 583 |
|
| 584 |
if kwargs.get('version') and not version_compare_many(self.version, kwargs['version'])[0]: |
| 585 |
mlog.error(f'Incorrect JDK version found ({self.version}), wanted {kwargs["version"]}') |
| 586 |
self.is_found = False |
| 587 |
return |
| 588 |
|
| 589 |
self.java_home = environment.properties[self.for_machine].get_java_home() |
| 590 |
if not self.java_home: |
| 591 |
self.java_home = self.__detect_java_home_from_compiler() |
| 592 |
if not self.java_home: |
| 593 |
self.java_home = pathlib.Path(shutil.which(self.javac.get_exe())).resolve().parents[1] |
| 594 |
if m.is_darwin(): |
| 595 |
problem_java_prefix = pathlib.Path('/System/Library/Frameworks/JavaVM.framework/Versions') |
| 596 |
if problem_java_prefix in self.java_home.parents: |
| 597 |
res = subprocess.run(['/usr/libexec/java_home', '--failfast', '--arch', m.cpu_family], |
| 598 |
stdout=subprocess.PIPE) |
| 599 |
if res.returncode != 0: |
| 600 |
msg = 'JAVA_HOME could not be discovered on the system. Please set it explicitly.' |
| 601 |
if self.required: |
| 602 |
mlog.error(msg) |
| 603 |
else: |
| 604 |
mlog.debug(msg) |
| 605 |
self.is_found = False |
| 606 |
return |
| 607 |
self.java_home = pathlib.Path(res.stdout.decode().strip()) |
| 608 |
|
| 609 |
platform_include_dir = self.__machine_info_to_platform_include_dir(m) |
| 610 |
if platform_include_dir is None: |
| 611 |
mlog.error("Could not find a JDK platform include directory for your OS, please open an issue or provide a pull request.") |
| 612 |
self.is_found = False |
| 613 |
return |
| 614 |
|
| 615 |
java_home_include = self.java_home / 'include' |
| 616 |
self.compile_args.append(f'-I{java_home_include}') |
| 617 |
self.compile_args.append(f'-I{java_home_include / platform_include_dir}') |
| 618 |
|
| 619 |
if modules: |
| 620 |
if m.is_windows(): |
| 621 |
java_home_lib = self.java_home / 'lib' |
| 622 |
java_home_lib_server = java_home_lib |
| 623 |
else: |
| 624 |
if version_compare(self.version, '<= 1.8.0'): |
| 625 |
java_home_lib = self.java_home / 'jre' / 'lib' / self.__cpu_translate(m.cpu_family) |
| 626 |
else: |
| 627 |
java_home_lib = self.java_home / 'lib' |
| 628 |
|
| 629 |
java_home_lib_server = java_home_lib / 'server' |
| 630 |
|
| 631 |
if 'jvm' in modules: |
| 632 |
jvm = self.clib_compiler.find_library('jvm', extra_dirs=[str(java_home_lib_server)]) |
| 633 |
if jvm is None: |
| 634 |
mlog.debug('jvm library not found.') |
| 635 |
self.is_found = False |
| 636 |
else: |
| 637 |
self.link_args.extend(jvm) |
| 638 |
if 'awt' in modules: |
| 639 |
jawt = self.clib_compiler.find_library('jawt', extra_dirs=[str(java_home_lib)]) |
| 640 |
if jawt is None: |
| 641 |
mlog.debug('jawt library not found.') |
| 642 |
self.is_found = False |
| 643 |
else: |
| 644 |
self.link_args.extend(jawt) |
| 645 |
|
| 646 |
self.is_found = True |
| 647 |
|
| 648 |
def __detect_java_home_from_compiler(self) -> T.Optional[pathlib.Path]: |
| 649 |
''' |
| 650 |
Ask the compiler for its home directory by parsing the `java.home` |
| 651 |
system property. |
| 652 |
|
| 653 |
This is necessary because not every OS wires `javac` up as a plain |
| 654 |
symlink (or chain of symlinks) pointing directly at the real JDK |
| 655 |
installation. |
| 656 |
''' |
| 657 |
try: |
| 658 |
p, _, e = mesonlib.Popen_safe(self.javac.exelist + ['-J-XshowSettings:properties', '-version']) |
| 659 |
except (OSError, subprocess.SubprocessError): |
| 660 |
return None |
| 661 |
if p.returncode != 0: |
| 662 |
return None |
| 663 |
# -XshowSettings writes to stderr, not stdout. |
| 664 |
for line in e.splitlines(): |
| 665 |
name, separator, value = line.partition('=') |
| 666 |
if separator and name.strip() == 'java.home': |
| 667 |
value = value.strip() |
| 668 |
if value: |
| 669 |
home = pathlib.Path(value) |
| 670 |
# Pre-JDK9 (JEP 220), java.home points at the JRE directory |
| 671 |
# bundled inside the JDK, not the JDK root itself, so we |
| 672 |
# must go up one level. |
| 673 |
if version_compare(self.version, '<= 1.8.0'): |
| 674 |
home = home.parent |
| 675 |
return home |
| 676 |
return None |
| 677 |
|
| 678 |
@staticmethod |
| 679 |
def __cpu_translate(cpu: str) -> str: |
| 680 |
''' |
| 681 |
The JDK and Meson have a disagreement here, so translate it over. In the event more |
| 682 |
translation needs to be done, add to following dict. |
| 683 |
''' |
| 684 |
java_cpus = { |
| 685 |
'x86_64': 'amd64', |
| 686 |
} |
| 687 |
|
| 688 |
return java_cpus.get(cpu, cpu) |
| 689 |
|
| 690 |
@staticmethod |
| 691 |
def __machine_info_to_platform_include_dir(m: 'MachineInfo') -> T.Optional[str]: |
| 692 |
'''Translates the machine information to the platform-dependent include directory |
| 693 |
|
| 694 |
When inspecting a JDK release tarball or $JAVA_HOME, inside the `include/` directory is a |
| 695 |
platform-dependent directory that must be on the target's include path in addition to the |
| 696 |
parent `include/` directory. |
| 697 |
''' |
| 698 |
if m.is_linux(): |
| 699 |
return 'linux' |
| 700 |
elif m.is_windows(): |
| 701 |
return 'win32' |
| 702 |
elif m.is_darwin(): |
| 703 |
return 'darwin' |
| 704 |
elif m.is_sunos(): |
| 705 |
return 'solaris' |
| 706 |
elif m.is_freebsd(): |
| 707 |
return 'freebsd' |
| 708 |
elif m.is_netbsd(): |
| 709 |
return 'netbsd' |
| 710 |
elif m.is_openbsd(): |
| 711 |
return 'openbsd' |
| 712 |
elif m.is_dragonflybsd(): |
| 713 |
return 'dragonfly' |
| 714 |
|
| 715 |
return None |
| 716 |
|
| 717 |
packages['jni'] = JNISystemDependency |
| 718 |
|
| 719 |
|
| 720 |
class JDKSystemDependency(JNISystemDependency): |
| 721 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs): |
| 722 |
super().__init__('jni', environment, kwargs) |
| 723 |
|
| 724 |
self.feature_since = ('0.59.0', '') |
| 725 |
self.featurechecks.append(FeatureDeprecated( |
| 726 |
'jdk system dependency', |
| 727 |
'0.62.0', |
| 728 |
'Use the jni system dependency instead' |
| 729 |
)) |
| 730 |
|
| 731 |
packages['jdk'] = JDKSystemDependency |
| 732 |
|
| 733 |
|
| 734 |
class DiaSDKSystemDependency(SystemDependency): |
| 735 |
|
| 736 |
def _try_path(self, diadir: str, cpu: str) -> bool: |
| 737 |
if not os.path.isdir(diadir): |
| 738 |
return False |
| 739 |
|
| 740 |
include = os.path.join(diadir, 'include') |
| 741 |
if not os.path.isdir(include): |
| 742 |
mlog.error('DIA SDK is missing include directory:', include) |
| 743 |
return False |
| 744 |
|
| 745 |
lib = os.path.join(diadir, 'lib', cpu, 'diaguids.lib') |
| 746 |
if not os.path.exists(lib): |
| 747 |
mlog.error('DIA SDK is missing library:', lib) |
| 748 |
return False |
| 749 |
|
| 750 |
bindir = os.path.join(diadir, 'bin', cpu) |
| 751 |
if not os.path.exists(bindir): |
| 752 |
mlog.error(f'Directory {bindir} not found') |
| 753 |
return False |
| 754 |
|
| 755 |
found = glob.glob(os.path.join(bindir, 'msdia*.dll')) |
| 756 |
if not found: |
| 757 |
mlog.error("Can't find msdia*.dll in " + bindir) |
| 758 |
return False |
| 759 |
if len(found) > 1: |
| 760 |
mlog.error('Multiple msdia*.dll files found in ' + bindir) |
| 761 |
return False |
| 762 |
self.dll = found[0] |
| 763 |
|
| 764 |
# Parse only major version from DLL name (eg '8' from 'msdia80.dll', '14' from 'msdia140.dll', etc.). |
| 765 |
# Minor version is not reflected in the DLL name, instead '0' is always used. |
| 766 |
# Aside from major version in DLL name, the SDK version is not visible to user anywhere. |
| 767 |
# The only place where the full version is stored, seems to be the Version field in msdia*.dll resources. |
| 768 |
dllname = os.path.basename(self.dll) |
| 769 |
versionstr = dllname[len('msdia'):-len('.dll')] |
| 770 |
if versionstr[-1] == '0': |
| 771 |
self.version = versionstr[:-1] |
| 772 |
else: |
| 773 |
mlog.error(f"Unexpected DIA SDK version string in '{dllname}'") |
| 774 |
self.version = 'unknown' |
| 775 |
|
| 776 |
self.compile_args.append('-I' + include) |
| 777 |
self.link_args.append(lib) |
| 778 |
self.is_found = True |
| 779 |
return True |
| 780 |
|
| 781 |
# Check if compiler has a built-in macro defined |
| 782 |
@staticmethod |
| 783 |
def _has_define(compiler: 'Compiler', dname: str, env: 'Environment') -> bool: |
| 784 |
defval, _ = compiler.get_define(dname, '', [], []) |
| 785 |
return defval is not None |
| 786 |
|
| 787 |
def __init__(self, name: str, environment: 'Environment', kwargs: DependencyObjectKWs) -> None: |
| 788 |
super().__init__(name, environment, kwargs) |
| 789 |
self.is_found = False |
| 790 |
|
| 791 |
compilers = environment.coredata.compilers.host |
| 792 |
if 'cpp' in compilers: |
| 793 |
compiler = compilers['cpp'] |
| 794 |
elif 'c' in compilers: |
| 795 |
compiler = compilers['c'] |
| 796 |
else: |
| 797 |
raise DependencyException('DIA SDK is only supported in C and C++ projects') |
| 798 |
|
| 799 |
is_msvc_clang = compiler.id == 'clang' and self._has_define(compiler, '_MSC_VER', environment) |
| 800 |
if compiler.id not in {'msvc', 'clang-cl'} and not is_msvc_clang: |
| 801 |
raise DependencyException('DIA SDK is only supported with Microsoft Visual Studio compilers') |
| 802 |
|
| 803 |
cpu_translate = {'arm': 'arm', 'aarch64': 'arm64', 'x86': '.', 'x86_64': 'amd64'} |
| 804 |
cpu_family = environment.machines.host.cpu_family |
| 805 |
cpu = cpu_translate.get(cpu_family) |
| 806 |
if cpu is None: |
| 807 |
raise DependencyException(f'DIA SDK is not supported for "{cpu_family}" architecture') |
| 808 |
|
| 809 |
vsdir = os.environ.get('VSInstallDir') |
| 810 |
if vsdir is None: |
| 811 |
raise DependencyException("Environment variable VSInstallDir required for DIA SDK is not set") |
| 812 |
|
| 813 |
diadir = os.path.join(vsdir, 'DIA SDK') |
| 814 |
if self._try_path(diadir, cpu): |
| 815 |
mlog.debug('DIA SDK was found at default path: ', diadir) |
| 816 |
self.is_found = True |
| 817 |
return |
| 818 |
mlog.debug('DIA SDK was not found at default path: ', diadir) |
| 819 |
|
| 820 |
return |
| 821 |
|
| 822 |
def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, |
| 823 |
configtool: T.Optional[str] = None, internal: T.Optional[str] = None, |
| 824 |
system: T.Optional[str] = None, default_value: T.Optional[str] = None, |
| 825 |
pkgconfig_define: PkgConfigDefineType = None) -> str: |
| 826 |
if system == 'dll' and self.is_found: |
| 827 |
return self.dll |
| 828 |
if default_value is not None: |
| 829 |
return default_value |
| 830 |
raise DependencyException(f'Could not get system variable and no default was set for {self!r}') |
| 831 |
|
| 832 |
packages['diasdk'] = DiaSDKSystemDependency |
| 833 |
|
| 834 |
packages['llvm'] = llvm_factory = DependencyFactory( |
| 835 |
'LLVM', |
| 836 |
[DependencyMethods.CMAKE, DependencyMethods.CONFIG_TOOL], |
| 837 |
cmake=LLVMDependencyCMake, |
| 838 |
configtool=LLVMDependencyConfigTool, |
| 839 |
) |
| 840 |
|
| 841 |
packages['gtest'] = gtest_factory = DependencyFactory( |
| 842 |
'gtest', |
| 843 |
[DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM], |
| 844 |
pkgconfig=GTestDependencyPC, |
| 845 |
system=GTestDependencySystem, |
| 846 |
) |
| 847 |
|
| 848 |
packages['gmock'] = gmock_factory = DependencyFactory( |
| 849 |
'gmock', |
| 850 |
[DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM], |
| 851 |
pkgconfig=GMockDependencyPC, |
| 852 |
system=GMockDependencySystem, |
| 853 |
) |
| 854 |
|
| 855 |
packages['zlib'] = zlib_factory = DependencyFactory( |
| 856 |
'zlib', |
| 857 |
[DependencyMethods.PKGCONFIG, DependencyMethods.CMAKE, DependencyMethods.SYSTEM], |
| 858 |
cmake=DependencyCandidate.from_dependency('ZLIB', CMakeDependency), |
| 859 |
system=ZlibSystemDependency, |
| 860 |
) |