| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2019 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from ..mesonlib import MesonException |
| 7 |
from ..options import OptionKey |
| 8 |
from ..tooldetect import detect_ninja |
| 9 |
from .. import mlog |
| 10 |
from pathlib import Path |
| 11 |
import functools |
| 12 |
import typing as T |
| 13 |
|
| 14 |
if T.TYPE_CHECKING: |
| 15 |
from ..compilers.compilers import Language |
| 16 |
from ..environment import Environment |
| 17 |
from ..interpreterbase import TYPE_var |
| 18 |
from .._typing import ImmutableListProtocol |
| 19 |
|
| 20 |
language_map: T.Mapping[Language, str] = { |
| 21 |
'c': 'C', |
| 22 |
'cpp': 'CXX', |
| 23 |
'cuda': 'CUDA', |
| 24 |
'objc': 'OBJC', |
| 25 |
'objcpp': 'OBJCXX', |
| 26 |
'nasm': 'ASM_NASM', |
| 27 |
'cs': 'CSharp', |
| 28 |
'java': 'Java', |
| 29 |
'fortran': 'Fortran', |
| 30 |
'swift': 'Swift', |
| 31 |
} |
| 32 |
|
| 33 |
backend_generator_map = { |
| 34 |
'ninja': 'Ninja', |
| 35 |
'xcode': 'Xcode', |
| 36 |
'vs2010': 'Visual Studio 10 2010', |
| 37 |
'vs2012': 'Visual Studio 11 2012', |
| 38 |
'vs2013': 'Visual Studio 12 2013', |
| 39 |
'vs2015': 'Visual Studio 14 2015', |
| 40 |
'vs2017': 'Visual Studio 15 2017', |
| 41 |
'vs2019': 'Visual Studio 16 2019', |
| 42 |
'vs2022': 'Visual Studio 17 2022', |
| 43 |
'vs2026': 'Visual Studio 18 2026', |
| 44 |
} |
| 45 |
|
| 46 |
blacklist_cmake_defs = [ |
| 47 |
'CMAKE_TOOLCHAIN_FILE', |
| 48 |
'CMAKE_PROJECT_INCLUDE', |
| 49 |
'MESON_PRELOAD_FILE', |
| 50 |
'MESON_PS_CMAKE_CURRENT_BINARY_DIR', |
| 51 |
'MESON_PS_CMAKE_CURRENT_SOURCE_DIR', |
| 52 |
'MESON_PS_DELAYED_CALLS', |
| 53 |
'MESON_PS_LOADED', |
| 54 |
'MESON_FIND_ROOT_PATH', |
| 55 |
'MESON_CMAKE_SYSROOT', |
| 56 |
'MESON_PATHS_LIST', |
| 57 |
'MESON_CMAKE_ROOT', |
| 58 |
] |
| 59 |
|
| 60 |
def cmake_is_debug(env: 'Environment') -> bool: |
| 61 |
if 'b_vscrt' in env.coredata.optstore: |
| 62 |
is_debug = env.coredata.optstore.get_value_for('buildtype') == 'debug' |
| 63 |
if env.coredata.optstore.get_value_for('b_vscrt') in {'mdd', 'mtd'}: |
| 64 |
is_debug = True |
| 65 |
return is_debug |
| 66 |
else: |
| 67 |
# Don't directly assign to is_debug to make mypy happy |
| 68 |
debug_opt = env.coredata.optstore.get_value_for('debug') |
| 69 |
assert isinstance(debug_opt, bool) |
| 70 |
return debug_opt |
| 71 |
|
| 72 |
class CMakeException(MesonException): |
| 73 |
pass |
| 74 |
|
| 75 |
class CMakeBuildFile: |
| 76 |
def __init__(self, file: Path, is_cmake: bool, is_temp: bool) -> None: |
| 77 |
self.file = file |
| 78 |
self.is_cmake = is_cmake |
| 79 |
self.is_temp = is_temp |
| 80 |
|
| 81 |
def __repr__(self) -> str: |
| 82 |
return f'<{self.__class__.__name__}: {self.file}; cmake={self.is_cmake}; temp={self.is_temp}>' |
| 83 |
|
| 84 |
def _flags_to_list(raw: str) -> T.List[str]: |
| 85 |
# Convert a raw commandline string into a list of strings |
| 86 |
res = [] |
| 87 |
curr = '' |
| 88 |
escape = False |
| 89 |
in_string = False |
| 90 |
for i in raw: |
| 91 |
if escape: |
| 92 |
# If the current char is not a quote, the '\' is probably important |
| 93 |
if i not in ['"', "'"]: |
| 94 |
curr += '\\' |
| 95 |
curr += i |
| 96 |
escape = False |
| 97 |
elif i == '\\': |
| 98 |
escape = True |
| 99 |
elif i in {'"', "'"}: |
| 100 |
in_string = not in_string |
| 101 |
elif i in {' ', '\n'}: |
| 102 |
if in_string: |
| 103 |
curr += i |
| 104 |
else: |
| 105 |
res += [curr] |
| 106 |
curr = '' |
| 107 |
else: |
| 108 |
curr += i |
| 109 |
res += [curr] |
| 110 |
res = [r for r in res if len(r) > 0] |
| 111 |
return res |
| 112 |
|
| 113 |
@functools.lru_cache(maxsize=None) |
| 114 |
def _cmake_get_generator_args(backend_name: str) -> ImmutableListProtocol[str]: |
| 115 |
assert backend_name in backend_generator_map |
| 116 |
args = ['-G', backend_generator_map[backend_name]] |
| 117 |
if backend_name == 'ninja': |
| 118 |
ninja = detect_ninja() |
| 119 |
if ninja: |
| 120 |
assert len(ninja) == 1 |
| 121 |
args += [f'-DCMAKE_MAKE_PROGRAM={ninja[0]}'] |
| 122 |
return args |
| 123 |
|
| 124 |
def cmake_get_generator_args(env: 'Environment') -> T.List[str]: |
| 125 |
backend_name = env.coredata.optstore.get_value_for(OptionKey('backend')) |
| 126 |
assert isinstance(backend_name, str) |
| 127 |
return list(_cmake_get_generator_args(backend_name)) |
| 128 |
|
| 129 |
def cmake_defines_to_args(raw: T.List[T.Dict[str, TYPE_var]], permissive: bool = False) -> T.List[str]: |
| 130 |
res: T.List[str] = [] |
| 131 |
|
| 132 |
for i in raw: |
| 133 |
for key, val in i.items(): |
| 134 |
if key in blacklist_cmake_defs: |
| 135 |
mlog.warning('Setting', mlog.bold(key), 'is not supported. See the meson docs for cross compilation support:') |
| 136 |
mlog.warning(' - URL: https://mesonbuild.com/CMake-module.html#cross-compilation') |
| 137 |
mlog.warning(' --> Ignoring this option') |
| 138 |
continue |
| 139 |
if isinstance(val, (str, int, float)): |
| 140 |
res += [f'-D{key}={val}'] |
| 141 |
elif isinstance(val, bool): |
| 142 |
val_str = 'ON' if val else 'OFF' |
| 143 |
res += [f'-D{key}={val_str}'] |
| 144 |
else: |
| 145 |
raise MesonException('Type "{}" of "{}" is not supported as for a CMake define value'.format(type(val).__name__, key)) |
| 146 |
|
| 147 |
return res |
| 148 |
|
| 149 |
# TODO: this function will become obsolete once the `cmake_args` kwarg is dropped |
| 150 |
def check_cmake_args(args: T.List[str]) -> T.List[str]: |
| 151 |
res: T.List[str] = [] |
| 152 |
dis = ['-D' + x for x in blacklist_cmake_defs] |
| 153 |
assert dis # Ensure that dis is not empty. |
| 154 |
for i in args: |
| 155 |
if any(i.startswith(x) for x in dis): |
| 156 |
mlog.warning('Setting', mlog.bold(i), 'is not supported. See the meson docs for cross compilation support:') |
| 157 |
mlog.warning(' - URL: https://mesonbuild.com/CMake-module.html#cross-compilation') |
| 158 |
mlog.warning(' --> Ignoring this option') |
| 159 |
continue |
| 160 |
res += [i] |
| 161 |
return res |
| 162 |
|
| 163 |
class CMakeInclude: |
| 164 |
def __init__(self, path: Path, isSystem: bool = False): |
| 165 |
self.path = path |
| 166 |
self.isSystem = isSystem |
| 167 |
|
| 168 |
def __repr__(self) -> str: |
| 169 |
return f'<CMakeInclude: {self.path} -- isSystem = {self.isSystem}>' |
| 170 |
|
| 171 |
class CMakeFileGroup: |
| 172 |
def __init__(self, data: T.Dict[str, T.Any]) -> None: |
| 173 |
self.defines: str = data.get('defines', '') |
| 174 |
self.flags = _flags_to_list(data.get('compileFlags', '')) |
| 175 |
self.is_generated: bool = data.get('isGenerated', False) |
| 176 |
self.language: str = data.get('language', 'C') |
| 177 |
self.sources = [Path(x) for x in data.get('sources', [])] |
| 178 |
|
| 179 |
# Fix the include directories |
| 180 |
self.includes: T.List[CMakeInclude] = [] |
| 181 |
for i in data.get('includePath', []): |
| 182 |
if isinstance(i, dict) and 'path' in i: |
| 183 |
isSystem = i.get('isSystem', False) |
| 184 |
assert isinstance(isSystem, bool) |
| 185 |
assert isinstance(i['path'], str) |
| 186 |
self.includes += [CMakeInclude(Path(i['path']), isSystem)] |
| 187 |
elif isinstance(i, str): |
| 188 |
self.includes += [CMakeInclude(Path(i))] |
| 189 |
|
| 190 |
def log(self) -> None: |
| 191 |
mlog.log('flags =', mlog.bold(', '.join(self.flags))) |
| 192 |
mlog.log('defines =', mlog.bold(', '.join(self.defines))) |
| 193 |
mlog.log('includes =', mlog.bold(', '.join([str(x) for x in self.includes]))) |
| 194 |
mlog.log('is_generated =', mlog.bold('true' if self.is_generated else 'false')) |
| 195 |
mlog.log('language =', mlog.bold(self.language)) |
| 196 |
mlog.log('sources:') |
| 197 |
for i in self.sources: |
| 198 |
with mlog.nested(): |
| 199 |
mlog.log(i.as_posix()) |
| 200 |
|
| 201 |
class CMakeTarget: |
| 202 |
def __init__(self, data: T.Dict[str, T.Any]) -> None: |
| 203 |
self.artifacts = [Path(x) for x in data.get('artifacts', [])] |
| 204 |
self.src_dir = Path(data.get('sourceDirectory', '')) |
| 205 |
self.build_dir = Path(data.get('buildDirectory', '')) |
| 206 |
self.name: str = data.get('name', '') |
| 207 |
self.full_name: str = data.get('fullName', '') |
| 208 |
self.install: bool = data.get('hasInstallRule', False) |
| 209 |
self.install_paths = [Path(x) for x in set(data.get('installPaths', []))] |
| 210 |
self.link_lang: str = data.get('linkerLanguage', '') |
| 211 |
self.link_libraries = _flags_to_list(data.get('linkLibraries', '')) |
| 212 |
self.link_flags = _flags_to_list(data.get('linkFlags', '')) |
| 213 |
self.link_lang_flags = _flags_to_list(data.get('linkLanguageFlags', '')) |
| 214 |
# self.link_path = Path(data.get('linkPath', '')) |
| 215 |
self.type: str = data.get('type', 'EXECUTABLE') |
| 216 |
# self.is_generator_provided: bool = data.get('isGeneratorProvided', False) |
| 217 |
self.files: T.List[CMakeFileGroup] = [] |
| 218 |
|
| 219 |
for i in data.get('fileGroups', []): |
| 220 |
self.files += [CMakeFileGroup(i)] |
| 221 |
|
| 222 |
def log(self) -> None: |
| 223 |
mlog.log('artifacts =', mlog.bold(', '.join([x.as_posix() for x in self.artifacts]))) |
| 224 |
mlog.log('src_dir =', mlog.bold(self.src_dir.as_posix())) |
| 225 |
mlog.log('build_dir =', mlog.bold(self.build_dir.as_posix())) |
| 226 |
mlog.log('name =', mlog.bold(self.name)) |
| 227 |
mlog.log('full_name =', mlog.bold(self.full_name)) |
| 228 |
mlog.log('install =', mlog.bold('true' if self.install else 'false')) |
| 229 |
mlog.log('install_paths =', mlog.bold(', '.join([x.as_posix() for x in self.install_paths]))) |
| 230 |
mlog.log('link_lang =', mlog.bold(self.link_lang)) |
| 231 |
mlog.log('link_libraries =', mlog.bold(', '.join(self.link_libraries))) |
| 232 |
mlog.log('link_flags =', mlog.bold(', '.join(self.link_flags))) |
| 233 |
mlog.log('link_lang_flags =', mlog.bold(', '.join(self.link_lang_flags))) |
| 234 |
# mlog.log('link_path =', mlog.bold(self.link_path)) |
| 235 |
mlog.log('type =', mlog.bold(self.type)) |
| 236 |
# mlog.log('is_generator_provided =', mlog.bold('true' if self.is_generator_provided else 'false')) |
| 237 |
for idx, i in enumerate(self.files): |
| 238 |
mlog.log(f'Files {idx}:') |
| 239 |
with mlog.nested(): |
| 240 |
i.log() |
| 241 |
|
| 242 |
class CMakeProject: |
| 243 |
def __init__(self, data: T.Dict[str, T.Any]) -> None: |
| 244 |
self.src_dir = Path(data.get('sourceDirectory', '')) |
| 245 |
self.build_dir = Path(data.get('buildDirectory', '')) |
| 246 |
self.name: str = data.get('name', '') |
| 247 |
self.targets: T.List[CMakeTarget] = [] |
| 248 |
|
| 249 |
for i in data.get('targets', []): |
| 250 |
self.targets += [CMakeTarget(i)] |
| 251 |
|
| 252 |
def log(self) -> None: |
| 253 |
mlog.log('src_dir =', mlog.bold(self.src_dir.as_posix())) |
| 254 |
mlog.log('build_dir =', mlog.bold(self.build_dir.as_posix())) |
| 255 |
mlog.log('name =', mlog.bold(self.name)) |
| 256 |
for idx, i in enumerate(self.targets): |
| 257 |
mlog.log(f'Target {idx}:') |
| 258 |
with mlog.nested(): |
| 259 |
i.log() |
| 260 |
|
| 261 |
class CMakeConfiguration: |
| 262 |
def __init__(self, data: T.Dict[str, T.Any]) -> None: |
| 263 |
self.name: str = data.get('name', '') |
| 264 |
self.projects: T.List[CMakeProject] = [] |
| 265 |
for i in data.get('projects', []): |
| 266 |
self.projects += [CMakeProject(i)] |
| 267 |
|
| 268 |
def log(self) -> None: |
| 269 |
mlog.log('name =', mlog.bold(self.name)) |
| 270 |
for idx, i in enumerate(self.projects): |
| 271 |
mlog.log(f'Project {idx}:') |
| 272 |
with mlog.nested(): |
| 273 |
i.log() |
| 274 |
|
| 275 |
class SingleTargetOptions: |
| 276 |
def __init__(self) -> None: |
| 277 |
self.opts: T.Dict[str, str] = {} |
| 278 |
self.lang_args: T.Dict[str, T.List[str]] = {} |
| 279 |
self.link_args: T.List[str] = [] |
| 280 |
self.install = 'preserve' |
| 281 |
|
| 282 |
def set_opt(self, opt: str, val: str) -> None: |
| 283 |
self.opts[opt] = val |
| 284 |
|
| 285 |
def append_args(self, lang: str, args: T.List[str]) -> None: |
| 286 |
if lang not in self.lang_args: |
| 287 |
self.lang_args[lang] = [] |
| 288 |
self.lang_args[lang] += args |
| 289 |
|
| 290 |
def append_link_args(self, args: T.List[str]) -> None: |
| 291 |
self.link_args += args |
| 292 |
|
| 293 |
def set_install(self, install: bool) -> None: |
| 294 |
self.install = 'true' if install else 'false' |
| 295 |
|
| 296 |
def get_override_options(self, initial: T.List[str]) -> T.List[str]: |
| 297 |
res: T.List[str] = [] |
| 298 |
for i in initial: |
| 299 |
opt = i[:i.find('=')] |
| 300 |
if opt not in self.opts: |
| 301 |
res += [i] |
| 302 |
res += [f'{k}={v}' for k, v in self.opts.items()] |
| 303 |
return res |
| 304 |
|
| 305 |
def get_compile_args(self, lang: str, initial: T.List[str]) -> T.List[str]: |
| 306 |
if lang in self.lang_args: |
| 307 |
return initial + self.lang_args[lang] |
| 308 |
return initial |
| 309 |
|
| 310 |
def get_link_args(self, initial: T.List[str]) -> T.List[str]: |
| 311 |
return initial + self.link_args |
| 312 |
|
| 313 |
def get_install(self, initial: bool) -> bool: |
| 314 |
return {'preserve': initial, 'true': True, 'false': False}[self.install] |
| 315 |
|
| 316 |
class TargetOptions: |
| 317 |
def __init__(self) -> None: |
| 318 |
self.global_options = SingleTargetOptions() |
| 319 |
self.target_options: T.Dict[str, SingleTargetOptions] = {} |
| 320 |
|
| 321 |
def __getitem__(self, tgt: str) -> SingleTargetOptions: |
| 322 |
if tgt not in self.target_options: |
| 323 |
self.target_options[tgt] = SingleTargetOptions() |
| 324 |
return self.target_options[tgt] |
| 325 |
|
| 326 |
def get_override_options(self, tgt: str, initial: T.List[str]) -> T.List[str]: |
| 327 |
initial = self.global_options.get_override_options(initial) |
| 328 |
if tgt in self.target_options: |
| 329 |
initial = self.target_options[tgt].get_override_options(initial) |
| 330 |
return initial |
| 331 |
|
| 332 |
def get_compile_args(self, tgt: str, lang: str, initial: T.List[str]) -> T.List[str]: |
| 333 |
initial = self.global_options.get_compile_args(lang, initial) |
| 334 |
if tgt in self.target_options: |
| 335 |
initial = self.target_options[tgt].get_compile_args(lang, initial) |
| 336 |
return initial |
| 337 |
|
| 338 |
def get_link_args(self, tgt: str, initial: T.List[str]) -> T.List[str]: |
| 339 |
initial = self.global_options.get_link_args(initial) |
| 340 |
if tgt in self.target_options: |
| 341 |
initial = self.target_options[tgt].get_link_args(initial) |
| 342 |
return initial |
| 343 |
|
| 344 |
def get_install(self, tgt: str, initial: bool) -> bool: |
| 345 |
initial = self.global_options.get_install(initial) |
| 346 |
if tgt in self.target_options: |
| 347 |
initial = self.target_options[tgt].get_install(initial) |
| 348 |
return initial |