compilers/rust: add type annotations

pull/7795/head
Dylan Baker 4 years ago
parent e437bdd0a9
commit 4430269d4e
  1. 53
      mesonbuild/compilers/rust.py
  2. 1
      run_mypy.py

@ -13,51 +13,63 @@
# limitations under the License.
import subprocess, os.path
import textwrap
import typing as T
from ..mesonlib import EnvironmentException, MachineChoice, Popen_safe
from .compilers import Compiler, rust_buildtype_args, clike_debug_args
if T.TYPE_CHECKING:
from ..dependencies import ExternalProgram
from ..envconfig import MachineInfo
from ..environment import Environment # noqa: F401
from ..linkers import DynamicLinker
rust_optimization_args = {'0': [],
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]]
class RustCompiler(Compiler):
# rustc doesn't invoke the compiler itself, it doesn't need a LINKER_PREFIX
language = 'rust'
def __init__(self, exelist, version, for_machine: MachineChoice,
is_cross: bool, info: 'MachineInfo', exe_wrapper=None, **kwargs):
super().__init__(exelist, version, for_machine, info, is_cross=is_cross, **kwargs)
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
self.id = 'rustc'
def needs_static_linker(self):
def needs_static_linker(self) -> bool:
return False
def sanity_check(self, work_dir, environment):
def sanity_check(self, work_dir: str, environment: 'Environment') -> None:
source_name = os.path.join(work_dir, 'sanity.rs')
output_name = os.path.join(work_dir, 'rusttest')
with open(source_name, 'w') as ofile:
ofile.write('''fn main() {
}
''')
ofile.write(textwrap.dedent(
'''fn main() {
}
'''))
pc = subprocess.Popen(self.exelist + ['-o', output_name, source_name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=work_dir)
stdo, stde = pc.communicate()
stdo = stdo.decode('utf-8', errors='replace')
stde = stde.decode('utf-8', errors='replace')
_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')
if pc.returncode != 0:
raise EnvironmentException('Rust compiler %s can not compile programs.\n%s\n%s' % (
self.name_string(),
@ -67,7 +79,7 @@ class RustCompiler(Compiler):
if self.exe_wrapper is None:
# Can't check if the binaries run so we have to assume they do
return
cmdlist = self.exe_wrapper + [output_name]
cmdlist = self.exe_wrapper.get_command() + [output_name]
else:
cmdlist = [output_name]
pe = subprocess.Popen(cmdlist, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
@ -75,24 +87,25 @@ class RustCompiler(Compiler):
if pe.returncode != 0:
raise EnvironmentException('Executables created by Rust compiler %s are not runnable.' % self.name_string())
def get_dependency_gen_args(self, outfile):
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
return ['--dep-info', outfile]
def get_buildtype_args(self, buildtype):
def get_buildtype_args(self, buildtype: str) -> T.List[str]:
return rust_buildtype_args[buildtype]
def get_sysroot(self):
def get_sysroot(self) -> str:
cmd = self.exelist + ['--print', 'sysroot']
p, stdo, stde = Popen_safe(cmd)
return stdo.split('\n')[0]
def get_debug_args(self, is_debug):
def get_debug_args(self, is_debug: bool) -> T.List[str]:
return clike_debug_args[is_debug]
def get_optimization_args(self, optimization_level):
def get_optimization_args(self, optimization_level: str) -> T.List[str]:
return rust_optimization_args[optimization_level]
def compute_parameters_with_absolute_paths(self, parameter_list, build_dir):
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
build_dir: str) -> T.List[str]:
for idx, i in enumerate(parameter_list):
if i[:2] == '-L':
for j in ['dependency', 'crate', 'native', 'framework', 'all']:

@ -23,6 +23,7 @@ modules = [
'mesonbuild/compilers/cuda.py',
'mesonbuild/compilers/objc.py',
'mesonbuild/compilers/objcpp.py',
'mesonbuild/compilers/rust.py',
'mesonbuild/compilers/swift.py',
'mesonbuild/compilers/vala.py',
# 'mesonbuild/coredata.py',

Loading…
Cancel
Save