swift.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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
  13. from .compilers import Compiler, swift_buildtype_args
  14. class SwiftCompiler(Compiler):
  15. def __init__(self, exelist, version):
  16. self.language = 'swift'
  17. super().__init__(exelist, version)
  18. self.version = version
  19. self.id = 'llvm'
  20. self.is_cross = False
  21. def get_linker_exelist(self):
  22. return self.exelist[:]
  23. def name_string(self):
  24. return ' '.join(self.exelist)
  25. def needs_static_linker(self):
  26. return True
  27. def get_werror_args(self):
  28. return ['--fatal-warnings']
  29. def get_dependency_gen_args(self, outtarget, outfile):
  30. return ['-emit-dependencies']
  31. def depfile_for_object(self, objfile):
  32. return os.path.splitext(objfile)[0] + '.' + self.get_depfile_suffix()
  33. def get_depfile_suffix(self):
  34. return 'd'
  35. def get_output_args(self, target):
  36. return ['-o', target]
  37. def get_linker_output_args(self, target):
  38. return ['-o', target]
  39. def get_header_import_args(self, headername):
  40. return ['-import-objc-header', headername]
  41. def get_warn_args(self, level):
  42. return []
  43. def get_buildtype_args(self, buildtype):
  44. return swift_buildtype_args[buildtype]
  45. def get_buildtype_linker_args(self, buildtype):
  46. return []
  47. def get_std_exe_link_args(self):
  48. return ['-emit-executable']
  49. def get_module_args(self, modname):
  50. return ['-module-name', modname]
  51. def get_mod_gen_args(self):
  52. return ['-emit-module']
  53. def build_rpath_args(self, *args):
  54. return [] # FIXME
  55. def get_include_args(self, dirname):
  56. return ['-I' + dirname]
  57. def get_compile_only_args(self):
  58. return ['-c']
  59. def sanity_check(self, work_dir, environment):
  60. src = 'swifttest.swift'
  61. source_name = os.path.join(work_dir, src)
  62. output_name = os.path.join(work_dir, 'swifttest')
  63. with open(source_name, 'w') as ofile:
  64. ofile.write('''print("Swift compilation is working.")
  65. ''')
  66. extra_flags = self.get_cross_extra_flags(environment, link=True)
  67. pc = subprocess.Popen(self.exelist + extra_flags + ['-emit-executable', '-o', output_name, src], cwd=work_dir)
  68. pc.wait()
  69. if pc.returncode != 0:
  70. raise EnvironmentException('Swift compiler %s can not compile programs.' % self.name_string())
  71. if subprocess.call(output_name) != 0:
  72. raise EnvironmentException('Executables created by Swift compiler %s are not runnable.' % self.name_string())