| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright © 2024-2025 Intel Corporation |
| 3 |
|
| 4 |
"""Provides mixins for Apple compilers.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
import functools |
| 8 |
import subprocess |
| 9 |
import typing as T |
| 10 |
|
| 11 |
from ...mesonlib import MesonException |
| 12 |
|
| 13 |
|
| 14 |
@functools.lru_cache(maxsize=None) |
| 15 |
def _get_libomp_prefix() -> T.Optional[str]: |
| 16 |
"""Call `brew --prefix libomp` once and cache it. Returns None if unavailable.""" |
| 17 |
try: |
| 18 |
return subprocess.run( |
| 19 |
['brew', '--prefix', '--installed', 'libomp'], |
| 20 |
capture_output=True, |
| 21 |
encoding='utf-8', |
| 22 |
check=True, |
| 23 |
).stdout.strip() |
| 24 |
except (FileNotFoundError, subprocess.CalledProcessError): |
| 25 |
return None |
| 26 |
|
| 27 |
|
| 28 |
def _get_homebrew_libomp_root(cpu_family: str, is_cross: bool) -> str: |
| 29 |
"""Return the libomp root, preferring dynamic detection with arch-based fallback.""" |
| 30 |
if not is_cross: |
| 31 |
libomp_prefix = _get_libomp_prefix() |
| 32 |
if libomp_prefix is not None: |
| 33 |
return libomp_prefix |
| 34 |
# Fallback: brew not on PATH, use historical defaults based on architecture |
| 35 |
if cpu_family.startswith('x86'): |
| 36 |
return '/usr/local/opt/libomp' |
| 37 |
return '/opt/homebrew/opt/libomp' |
| 38 |
|
| 39 |
|
| 40 |
if T.TYPE_CHECKING: |
| 41 |
from ..._typing import ImmutableListProtocol |
| 42 |
from ...envconfig import MachineInfo |
| 43 |
from ..compilers import Compiler |
| 44 |
else: |
| 45 |
# This is a bit clever, for mypy we pretend that these mixins descend from |
| 46 |
# Compiler, so we get all of the methods and attributes defined for us, but |
| 47 |
# for runtime we make them descend from object (which all classes normally |
| 48 |
# do). This gives up DRYer type checking, with no runtime impact |
| 49 |
Compiler = object |
| 50 |
|
| 51 |
|
| 52 |
class AppleCompilerMixin(Compiler): |
| 53 |
|
| 54 |
"""Handle differences between Vanilla Clang and the Clang shipped with XCode.""" |
| 55 |
|
| 56 |
__BASE_OMP_FLAGS: ImmutableListProtocol[str] = ['-Xpreprocessor', '-fopenmp'] |
| 57 |
|
| 58 |
if T.TYPE_CHECKING: |
| 59 |
# Older versions of mypy can't figure this out |
| 60 |
info: MachineInfo |
| 61 |
|
| 62 |
def openmp_flags(self) -> T.List[str]: |
| 63 |
"""Flags required to compile with OpenMP on Apple. |
| 64 |
|
| 65 |
The Apple Clang Compiler doesn't have builtin support for OpenMP, it |
| 66 |
must be provided separately. As such, we need to add the -Xpreprocessor |
| 67 |
argument so that an external OpenMP can be found. |
| 68 |
|
| 69 |
:return: A list of arguments |
| 70 |
""" |
| 71 |
root = _get_homebrew_libomp_root(self.info.cpu_family, self.is_cross) |
| 72 |
return self.__BASE_OMP_FLAGS + [f'-I{root}/include'] |
| 73 |
|
| 74 |
def openmp_link_flags(self) -> T.List[str]: |
| 75 |
root = _get_homebrew_libomp_root(self.info.cpu_family, self.is_cross) |
| 76 |
link = self.find_library('omp', [f'{root}/lib']) |
| 77 |
if not link: |
| 78 |
raise MesonException("Couldn't find libomp") |
| 79 |
return self.__BASE_OMP_FLAGS + link |
| 80 |
|
| 81 |
def get_prelink_args(self, prelink_name: str, obj_list: T.List[str]) -> T.Tuple[T.List[str], T.List[str]]: |
| 82 |
# The objects are prelinked through the compiler, which injects -lSystem |
| 83 |
return [prelink_name], ['-nostdlib', '-r', '-o', prelink_name] + obj_list |
| 84 |
|
| 85 |
|
| 86 |
class AppleCStdsMixin(Compiler): |
| 87 |
|
| 88 |
"""Provide version overrides for the Apple Compilers.""" |
| 89 |
|
| 90 |
_C17_VERSION = '>=10.0.0' |
| 91 |
_C18_VERSION = '>=11.0.0' |
| 92 |
_C2X_VERSION = '>=11.0.3' |
| 93 |
_C23_VERSION = '>=17.0.0' |
| 94 |
_C2Y_VERSION = '>=17.0.0' |
| 95 |
|
| 96 |
|
| 97 |
class AppleCPPStdsMixin(Compiler): |
| 98 |
|
| 99 |
"""Provide version overrides for the Apple C++ Compilers.""" |
| 100 |
|
| 101 |
_CPP23_VERSION = '>=13.0.0' |
| 102 |
_CPP26_VERSION = '>=16.0.0' |