rust.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright 2012-2017 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import subprocess, os.path
  12. from ..mesonlib import EnvironmentException, Popen_safe
  13. from .compilers import Compiler, rust_buildtype_args
  14. class RustCompiler(Compiler):
  15. def __init__(self, exelist, version, is_cross, exe_wrapper=None):
  16. self.language = 'rust'
  17. super().__init__(exelist, version)
  18. self.is_cross = is_cross
  19. self.exe_wrapper = exe_wrapper
  20. self.id = 'rustc'
  21. def needs_static_linker(self):
  22. return False
  23. def name_string(self):
  24. return ' '.join(self.exelist)
  25. def sanity_check(self, work_dir, environment):
  26. source_name = os.path.join(work_dir, 'sanity.rs')
  27. output_name = os.path.join(work_dir, 'rusttest')
  28. with open(source_name, 'w') as ofile:
  29. ofile.write('''fn main() {
  30. }
  31. ''')
  32. pc = subprocess.Popen(self.exelist + ['-o', output_name, source_name], cwd=work_dir)
  33. pc.wait()
  34. if pc.returncode != 0:
  35. raise EnvironmentException('Rust compiler %s can not compile programs.' % self.name_string())
  36. if self.is_cross:
  37. if self.exe_wrapper is None:
  38. # Can't check if the binaries run so we have to assume they do
  39. return
  40. cmdlist = self.exe_wrapper + [output_name]
  41. else:
  42. cmdlist = [output_name]
  43. pe = subprocess.Popen(cmdlist, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
  44. pe.wait()
  45. if pe.returncode != 0:
  46. raise EnvironmentException('Executables created by Rust compiler %s are not runnable.' % self.name_string())
  47. def get_dependency_gen_args(self, outfile):
  48. return ['--dep-info', outfile]
  49. def get_buildtype_args(self, buildtype):
  50. return rust_buildtype_args[buildtype]
  51. def build_rpath_args(self, build_dir, from_dir, rpath_paths, build_rpath, install_rpath):
  52. return self.build_unix_rpath_args(build_dir, from_dir, rpath_paths, build_rpath, install_rpath)
  53. def get_sysroot(self):
  54. cmd = self.exelist + ['--print', 'sysroot']
  55. p, stdo, stde = Popen_safe(cmd)
  56. return stdo.split('\n')[0]