2021-04-28 01:18:21 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2024-08-30 01:22:53 +08:00
|
|
|
# Copyright © 2021-2024 Intel Corporation
|
2022-09-14 09:22:35 +08:00
|
|
|
from __future__ import annotations
|
2021-04-28 01:18:21 +08:00
|
|
|
|
|
|
|
"""Abstraction for Cython language compilers."""
|
|
|
|
|
|
|
|
import typing as T
|
|
|
|
|
2024-05-23 04:59:02 +08:00
|
|
|
from .. import options
|
2024-06-08 23:37:17 +08:00
|
|
|
from ..mesonlib import EnvironmentException, version_compare
|
2021-04-28 01:18:21 +08:00
|
|
|
from .compilers import Compiler
|
|
|
|
|
|
|
|
if T.TYPE_CHECKING:
|
2021-10-13 09:45:24 +08:00
|
|
|
from ..coredata import MutableKeyedOptionDictType, KeyedOptionDictType
|
2021-04-28 01:18:21 +08:00
|
|
|
from ..environment import Environment
|
|
|
|
|
|
|
|
|
|
|
|
class CythonCompiler(Compiler):
|
|
|
|
|
|
|
|
"""Cython Compiler."""
|
|
|
|
|
|
|
|
language = 'cython'
|
|
|
|
id = 'cython'
|
|
|
|
|
|
|
|
def needs_static_linker(self) -> bool:
|
|
|
|
# We transpile into C, so we don't need any linker
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_always_args(self) -> T.List[str]:
|
2021-05-20 06:22:55 +08:00
|
|
|
return ['--fast-fail']
|
2021-04-28 01:18:21 +08:00
|
|
|
|
|
|
|
def get_werror_args(self) -> T.List[str]:
|
|
|
|
return ['-Werror']
|
|
|
|
|
|
|
|
def get_output_args(self, outputname: str) -> T.List[str]:
|
|
|
|
return ['-o', outputname]
|
|
|
|
|
|
|
|
def get_optimization_args(self, optimization_level: str) -> T.List[str]:
|
|
|
|
# Cython doesn't have optimization levels itself, the underlying
|
|
|
|
# compiler might though
|
|
|
|
return []
|
|
|
|
|
2022-12-01 13:31:44 +08:00
|
|
|
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
|
|
|
|
if version_compare(self.version, '>=0.29.33'):
|
|
|
|
return ['-M']
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_depfile_suffix(self) -> str:
|
|
|
|
return 'dep'
|
|
|
|
|
2021-04-28 01:18:21 +08:00
|
|
|
def sanity_check(self, work_dir: str, environment: 'Environment') -> None:
|
|
|
|
code = 'print("hello world")'
|
|
|
|
with self.cached_compile(code, environment.coredata) as p:
|
|
|
|
if p.returncode != 0:
|
|
|
|
raise EnvironmentException(f'Cython compiler {self.id!r} cannot compile programs')
|
|
|
|
|
|
|
|
def get_pic_args(self) -> T.List[str]:
|
|
|
|
# We can lie here, it's fine
|
|
|
|
return []
|
|
|
|
|
|
|
|
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
|
|
|
|
build_dir: str) -> T.List[str]:
|
|
|
|
new: T.List[str] = []
|
|
|
|
for i in parameter_list:
|
|
|
|
new.append(i)
|
|
|
|
|
|
|
|
return new
|
2021-05-20 06:22:55 +08:00
|
|
|
|
2021-10-13 09:45:24 +08:00
|
|
|
def get_options(self) -> 'MutableKeyedOptionDictType':
|
2024-08-30 01:22:53 +08:00
|
|
|
opts = super().get_options()
|
|
|
|
|
|
|
|
key = self.form_compileropt_key('version')
|
|
|
|
opts[key] = options.UserComboOption(
|
|
|
|
self.make_option_name(key),
|
|
|
|
'Python version to target',
|
|
|
|
['2', '3'],
|
|
|
|
'3')
|
|
|
|
|
|
|
|
key = self.form_compileropt_key('language')
|
|
|
|
opts[key] = options.UserComboOption(
|
|
|
|
self.make_option_name(key),
|
|
|
|
'Output C or C++ files',
|
|
|
|
['c', 'cpp'],
|
|
|
|
'c')
|
|
|
|
|
|
|
|
return opts
|
2021-05-20 06:22:55 +08:00
|
|
|
|
|
|
|
def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
|
|
|
|
args: T.List[str] = []
|
2024-07-12 18:37:43 +08:00
|
|
|
key = self.form_compileropt_key('version')
|
2024-06-09 06:03:49 +08:00
|
|
|
version = options.get_value(key)
|
|
|
|
args.append(f'-{version}')
|
2024-07-12 18:37:43 +08:00
|
|
|
key = self.form_compileropt_key('language')
|
2024-06-09 06:03:49 +08:00
|
|
|
lang = options.get_value(key)
|
|
|
|
if lang == 'cpp':
|
2021-08-25 00:20:03 +08:00
|
|
|
args.append('--cplus')
|
2021-05-20 06:22:55 +08:00
|
|
|
return args
|