| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2012-2017 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import re |
| 7 |
import subprocess, os.path |
| 8 |
import typing as T |
| 9 |
|
| 10 |
from .. import mlog, options |
| 11 |
from ..mesonlib import first, MesonException, version_compare |
| 12 |
from .compilers import Compiler, clike_debug_args, PrefixArgumentLinkerOptionStyle |
| 13 |
|
| 14 |
if T.TYPE_CHECKING: |
| 15 |
from .. import build |
| 16 |
from ..compilers.compilers import Language |
| 17 |
from ..options import MutableKeyedOptionDictType |
| 18 |
from ..dependencies import Dependency |
| 19 |
from ..environment import Environment |
| 20 |
from ..linkers.linkers import DynamicLinker |
| 21 |
from ..mesonlib import MachineChoice |
| 22 |
|
| 23 |
swift_optimization_args: T.Dict[str, T.List[str]] = { |
| 24 |
'plain': [], |
| 25 |
'0': [], |
| 26 |
'g': [], |
| 27 |
'1': ['-O'], |
| 28 |
'2': ['-O'], |
| 29 |
'3': ['-O'], |
| 30 |
's': ['-O'], |
| 31 |
} |
| 32 |
|
| 33 |
class SwiftCompiler(Compiler): |
| 34 |
|
| 35 |
LINKER_OPTION_STYLE = PrefixArgumentLinkerOptionStyle('-Xlinker') |
| 36 |
language = 'swift' |
| 37 |
id = 'llvm' |
| 38 |
|
| 39 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 40 |
env: Environment, full_version: T.Optional[str] = None, |
| 41 |
linker: T.Optional['DynamicLinker'] = None): |
| 42 |
super().__init__([], exelist, version, for_machine, env, |
| 43 |
full_version=full_version, linker=linker) |
| 44 |
self.version = version |
| 45 |
if self.info.is_darwin(): |
| 46 |
try: |
| 47 |
self.sdk_path = subprocess.check_output(['xcrun', '--show-sdk-path'], |
| 48 |
universal_newlines=True, |
| 49 |
encoding='utf-8', stderr=subprocess.STDOUT).strip() |
| 50 |
except subprocess.CalledProcessError as e: |
| 51 |
mlog.error("Failed to get Xcode SDK path: " + e.output) |
| 52 |
raise MesonException('Xcode license not accepted yet. Run `sudo xcodebuild -license`.') |
| 53 |
except FileNotFoundError: |
| 54 |
mlog.error('xcrun not found. Install Xcode to compile Swift code.') |
| 55 |
raise MesonException('Could not detect Xcode. Please install it to compile Swift code.') |
| 56 |
|
| 57 |
def get_pic_args(self) -> T.List[str]: |
| 58 |
return [] |
| 59 |
|
| 60 |
def get_pie_args(self) -> T.List[str]: |
| 61 |
return [] |
| 62 |
|
| 63 |
def needs_static_linker(self) -> bool: |
| 64 |
return True |
| 65 |
|
| 66 |
def get_werror_args(self) -> T.List[str]: |
| 67 |
return ['-warnings-as-errors'] |
| 68 |
|
| 69 |
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]: |
| 70 |
return ['-emit-dependencies'] |
| 71 |
|
| 72 |
def get_dependency_compile_args(self, dep: Dependency) -> T.List[str]: |
| 73 |
args = dep.get_compile_args() |
| 74 |
# Some deps might sneak in a hardcoded path to an older macOS SDK, which can |
| 75 |
# cause compilation errors. Let's replace all .sdk paths with the current one. |
| 76 |
# SwiftPM does it this way: https://github.com/swiftlang/swift-package-manager/pull/6772 |
| 77 |
# Not tested on anything else than macOS for now. |
| 78 |
if not self.info.is_darwin(): |
| 79 |
return args |
| 80 |
pattern = re.compile(r'.*\/MacOSX[^\/]*\.sdk(\/.*|$)') |
| 81 |
for i, arg in enumerate(args): |
| 82 |
if arg.startswith('-I'): |
| 83 |
match = pattern.match(arg) |
| 84 |
if match: |
| 85 |
args[i] = '-I' + self.sdk_path + match.group(1) |
| 86 |
return args |
| 87 |
|
| 88 |
def depfile_for_object(self, objfile: str) -> T.Optional[str]: |
| 89 |
return os.path.splitext(objfile)[0] + '.' + self.get_depfile_suffix() |
| 90 |
|
| 91 |
def get_depfile_suffix(self) -> str: |
| 92 |
return 'd' |
| 93 |
|
| 94 |
def get_output_args(self, target: str) -> T.List[str]: |
| 95 |
return ['-o', target] |
| 96 |
|
| 97 |
def get_header_import_args(self, headername: str) -> T.List[str]: |
| 98 |
return ['-import-objc-header', headername] |
| 99 |
|
| 100 |
def get_warn_args(self, level: str) -> T.List[str]: |
| 101 |
return [] |
| 102 |
|
| 103 |
def get_std_exe_link_args(self) -> T.List[str]: |
| 104 |
return ['-emit-executable'] |
| 105 |
|
| 106 |
def get_module_args(self, modname: str) -> T.List[str]: |
| 107 |
return ['-module-name', modname] |
| 108 |
|
| 109 |
def get_mod_gen_args(self) -> T.List[str]: |
| 110 |
return ['-emit-module'] |
| 111 |
|
| 112 |
def get_include_args(self, path: str, is_system: bool) -> T.List[str]: |
| 113 |
return ['-I' + path] |
| 114 |
|
| 115 |
def get_compile_only_args(self) -> T.List[str]: |
| 116 |
return ['-c'] |
| 117 |
|
| 118 |
def get_options(self) -> MutableKeyedOptionDictType: |
| 119 |
opts = super().get_options() |
| 120 |
|
| 121 |
key = self.form_compileropt_key('std') |
| 122 |
opts[key] = options.UserComboOption( |
| 123 |
self.make_option_name(key), |
| 124 |
'Swift language version.', |
| 125 |
'none', |
| 126 |
# List them with swiftc -frontend -swift-version '' |
| 127 |
choices=['none', '4', '4.2', '5', '6']) |
| 128 |
|
| 129 |
return opts |
| 130 |
|
| 131 |
def get_option_std_args(self, target: build.BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 132 |
args: T.List[str] = [] |
| 133 |
|
| 134 |
std = self.get_compileropt_value('std', target, subproject) |
| 135 |
assert isinstance(std, str) |
| 136 |
|
| 137 |
if std != 'none': |
| 138 |
args += ['-swift-version', std] |
| 139 |
|
| 140 |
# Pass C compiler -std=... arg to swiftc |
| 141 |
c_langs: T.List[Language] = ['objc', 'c'] |
| 142 |
if target.uses_swift_cpp_interop(): |
| 143 |
c_langs = ['objcpp', 'cpp', *c_langs] |
| 144 |
|
| 145 |
c_lang = first(c_langs, lambda x: x in target.compilers) |
| 146 |
if c_lang is not None: |
| 147 |
cc = target.compilers[c_lang] |
| 148 |
args.extend(arg for c_arg in cc.get_option_std_args(target, subproject) for arg in ['-Xcc', c_arg]) |
| 149 |
|
| 150 |
return args |
| 151 |
|
| 152 |
def get_working_directory_args(self, path: str) -> T.Optional[T.List[str]]: |
| 153 |
if version_compare(self.version, '<4.2'): |
| 154 |
return None |
| 155 |
|
| 156 |
return ['-working-directory', path] |
| 157 |
|
| 158 |
def get_cxx_interoperability_args(self, target: T.Optional[build.BuildTarget] = None) -> T.List[str]: |
| 159 |
if target is not None and not target.uses_swift_cpp_interop(): |
| 160 |
return [] |
| 161 |
|
| 162 |
if version_compare(self.version, '<5.9'): |
| 163 |
raise MesonException(f'Compiler {self} does not support C++ interoperability') |
| 164 |
|
| 165 |
return ['-cxx-interoperability-mode=default'] |
| 166 |
|
| 167 |
def get_library_args(self) -> T.List[str]: |
| 168 |
return ['-parse-as-library'] |
| 169 |
|
| 170 |
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], |
| 171 |
build_dir: str) -> T.List[str]: |
| 172 |
for idx, i in enumerate(parameter_list): |
| 173 |
if i[:2] == '-I' or i[:2] == '-L': |
| 174 |
parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:])) |
| 175 |
|
| 176 |
return parameter_list |
| 177 |
|
| 178 |
def _sanity_check_compile_args(self, sourcename: str, binname: str |
| 179 |
) -> T.Tuple[T.List[str], T.List[str]]: |
| 180 |
args = self.exelist.copy() |
| 181 |
largs: T.List[str] = [] |
| 182 |
|
| 183 |
# TODO: I can't test this, but it doesn't seem right |
| 184 |
if self.is_cross: |
| 185 |
args.extend(self.get_compile_only_args()) |
| 186 |
else: |
| 187 |
largs.extend(self.get_external_link_args()) |
| 188 |
args.extend(self.get_output_args(binname)) |
| 189 |
args.append(sourcename) |
| 190 |
|
| 191 |
largs.extend(self.get_std_exe_link_args()) |
| 192 |
|
| 193 |
return args, largs |
| 194 |
|
| 195 |
def _sanity_check_source_code(self) -> str: |
| 196 |
return 'print("Swift compilation is working.")' |
| 197 |
|
| 198 |
def get_debug_args(self, is_debug: bool) -> T.List[str]: |
| 199 |
return clike_debug_args[is_debug] |
| 200 |
|
| 201 |
def get_optimization_args(self, optimization_level: str) -> T.List[str]: |
| 202 |
return swift_optimization_args[optimization_level] |