meson/mesonbuild/compilers/rust.py

176 lines
6.8 KiB
Python
Raw Normal View History

2017-06-23 07:42:41 +08:00
# Copyright 2012-2017 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import subprocess, os.path
2020-09-29 01:15:03 +08:00
import textwrap
import typing as T
2017-06-23 07:42:41 +08:00
from .. import coredata
from ..mesonlib import (
EnvironmentException, MachineChoice, MesonException, Popen_safe,
OptionKey,
)
from .compilers import Compiler, rust_buildtype_args, clike_debug_args
if T.TYPE_CHECKING:
2020-12-03 08:02:03 +08:00
from ..coredata import KeyedOptionDictType
from ..envconfig import MachineInfo
from ..environment import Environment # noqa: F401
2020-09-29 01:15:03 +08:00
from ..linkers import DynamicLinker
from ..programs import ExternalProgram
2020-09-29 01:15:03 +08:00
rust_optimization_args = {
'0': [],
'g': ['-C', 'opt-level=0'],
'1': ['-C', 'opt-level=1'],
'2': ['-C', 'opt-level=2'],
'3': ['-C', 'opt-level=3'],
's': ['-C', 'opt-level=s'],
} # type: T.Dict[str, T.List[str]]
2017-06-23 07:42:41 +08:00
class RustCompiler(Compiler):
# rustc doesn't invoke the compiler itself, it doesn't need a LINKER_PREFIX
language = 'rust'
2020-09-29 01:15:03 +08:00
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice,
is_cross: bool, info: 'MachineInfo',
exe_wrapper: T.Optional['ExternalProgram'] = None,
full_version: T.Optional[str] = None,
linker: T.Optional['DynamicLinker'] = None):
super().__init__(exelist, version, for_machine, info,
is_cross=is_cross, full_version=full_version,
linker=linker)
self.exe_wrapper = exe_wrapper
2017-06-23 07:42:41 +08:00
self.id = 'rustc'
self.base_options.add(OptionKey('b_colorout'))
if 'link' in self.linker.id:
self.base_options.add(OptionKey('b_vscrt'))
2017-06-23 07:42:41 +08:00
2020-09-29 01:15:03 +08:00
def needs_static_linker(self) -> bool:
2017-06-23 07:42:41 +08:00
return False
2020-09-29 01:15:03 +08:00
def sanity_check(self, work_dir: str, environment: 'Environment') -> None:
2017-06-23 07:42:41 +08:00
source_name = os.path.join(work_dir, 'sanity.rs')
output_name = os.path.join(work_dir, 'rusttest')
with open(source_name, 'w', encoding='utf-8') as ofile:
2020-09-29 01:15:03 +08:00
ofile.write(textwrap.dedent(
'''fn main() {
}
'''))
2019-06-21 21:40:18 +08:00
pc = subprocess.Popen(self.exelist + ['-o', output_name, source_name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=work_dir)
2020-09-29 01:15:03 +08:00
_stdo, _stde = pc.communicate()
assert isinstance(_stdo, bytes)
assert isinstance(_stde, bytes)
stdo = _stdo.decode('utf-8', errors='replace')
stde = _stde.decode('utf-8', errors='replace')
2017-06-23 07:42:41 +08:00
if pc.returncode != 0:
raise EnvironmentException('Rust compiler {} can not compile programs.\n{}\n{}'.format(
2019-06-21 21:40:18 +08:00
self.name_string(),
stdo,
stde))
if self.is_cross:
if self.exe_wrapper is None:
# Can't check if the binaries run so we have to assume they do
return
2020-09-29 01:15:03 +08:00
cmdlist = self.exe_wrapper.get_command() + [output_name]
else:
cmdlist = [output_name]
pe = subprocess.Popen(cmdlist, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
pe.wait()
if pe.returncode != 0:
2017-06-23 07:42:41 +08:00
raise EnvironmentException('Executables created by Rust compiler %s are not runnable.' % self.name_string())
2020-09-29 01:15:03 +08:00
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
2017-06-23 07:42:41 +08:00
return ['--dep-info', outfile]
2020-09-29 01:15:03 +08:00
def get_buildtype_args(self, buildtype: str) -> T.List[str]:
2017-06-23 07:42:41 +08:00
return rust_buildtype_args[buildtype]
2020-09-29 01:15:03 +08:00
def get_sysroot(self) -> str:
2017-06-23 07:42:41 +08:00
cmd = self.exelist + ['--print', 'sysroot']
p, stdo, stde = Popen_safe(cmd)
return stdo.split('\n')[0]
2020-09-29 01:15:03 +08:00
def get_debug_args(self, is_debug: bool) -> T.List[str]:
return clike_debug_args[is_debug]
2020-09-29 01:15:03 +08:00
def get_optimization_args(self, optimization_level: str) -> T.List[str]:
return rust_optimization_args[optimization_level]
2018-12-30 20:37:41 +08:00
2020-09-29 01:15:03 +08:00
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
build_dir: str) -> T.List[str]:
2018-12-31 06:28:28 +08:00
for idx, i in enumerate(parameter_list):
if i[:2] == '-L':
for j in ['dependency', 'crate', 'native', 'framework', 'all']:
combined_len = len(j) + 3
if i[:combined_len] == f'-L{j}=':
2018-12-31 06:28:28 +08:00
parameter_list[idx] = i[:combined_len] + os.path.normpath(os.path.join(build_dir, i[combined_len:]))
break
2018-12-30 20:37:41 +08:00
return parameter_list
def get_output_args(self, outputname: str) -> T.List[str]:
return ['-o', outputname]
@classmethod
def use_linker_args(cls, linker: str) -> T.List[str]:
return ['-C', f'linker={linker}']
# Rust does not have a use_linker_args because it dispatches to a gcc-like
# C compiler for dynamic linking, as such we invoke the C compiler's
# use_linker_args method instead.
2020-12-03 08:02:03 +08:00
def get_options(self) -> 'KeyedOptionDictType':
key = OptionKey('std', machine=self.for_machine, lang=self.language)
return {
2020-12-03 08:02:03 +08:00
key: coredata.UserComboOption(
'Rust Eddition to use',
['none', '2015', '2018'],
'none',
),
}
2020-12-03 08:02:03 +08:00
def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
args = []
2020-12-03 08:02:03 +08:00
key = OptionKey('std', machine=self.for_machine, lang=self.language)
std = options[key]
if std.value != 'none':
args.append('--edition=' + std.value)
return args
def get_crt_compile_args(self, crt_val: str, buildtype: str) -> T.List[str]:
# Rust handles this for us, we don't need to do anything
return []
def get_colorout_args(self, colortype: str) -> T.List[str]:
if colortype in {'always', 'never', 'auto'}:
return [f'--color={colortype}']
raise MesonException(f'Invalid color type for rust {colortype}')
def get_linker_always_args(self) -> T.List[str]:
args: T.List[str] = []
for a in super().get_linker_always_args():
args.extend(['-C', f'link-arg={a}'])
return args
2021-05-18 02:44:25 +08:00
def get_werror_args(self) -> T.List[str]:
# Use -D warnings, which makes every warning not explicitly allowed an
# error
return ['-D', 'warnings']