1# Copyright 2012-2017 The Meson development team 2 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6 7# http://www.apache.org/licenses/LICENSE-2.0 8 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import subprocess, os.path 16import typing as T 17 18from ..mesonlib import EnvironmentException, MachineChoice 19 20from .compilers import Compiler, swift_buildtype_args, clike_debug_args 21 22if T.TYPE_CHECKING: 23 from ..envconfig import MachineInfo 24 25swift_optimization_args = {'0': [], 26 'g': [], 27 '1': ['-O'], 28 '2': ['-O'], 29 '3': ['-O'], 30 's': ['-O'], 31 } 32 33class SwiftCompiler(Compiler): 34 35 LINKER_PREFIX = ['-Xlinker'] 36 language = 'swift' 37 38 def __init__(self, exelist, version, for_machine: MachineChoice, 39 is_cross, info: 'MachineInfo', **kwargs): 40 super().__init__(exelist, version, for_machine, info, **kwargs) 41 self.version = version 42 self.id = 'llvm' 43 self.is_cross = is_cross 44 45 def name_string(self): 46 return ' '.join(self.exelist) 47 48 def needs_static_linker(self): 49 return True 50 51 def get_werror_args(self): 52 return ['--fatal-warnings'] 53 54 def get_dependency_gen_args(self, outtarget, outfile): 55 return ['-emit-dependencies'] 56 57 def depfile_for_object(self, objfile): 58 return os.path.splitext(objfile)[0] + '.' + self.get_depfile_suffix() 59 60 def get_depfile_suffix(self): 61 return 'd' 62 63 def get_output_args(self, target): 64 return ['-o', target] 65 66 def get_header_import_args(self, headername): 67 return ['-import-objc-header', headername] 68 69 def get_warn_args(self, level): 70 return [] 71 72 def get_buildtype_args(self, buildtype): 73 return swift_buildtype_args[buildtype] 74 75 def get_std_exe_link_args(self): 76 return ['-emit-executable'] 77 78 def get_module_args(self, modname): 79 return ['-module-name', modname] 80 81 def get_mod_gen_args(self): 82 return ['-emit-module'] 83 84 def get_include_args(self, dirname): 85 return ['-I' + dirname] 86 87 def get_compile_only_args(self): 88 return ['-c'] 89 90 def compute_parameters_with_absolute_paths(self, parameter_list, build_dir): 91 for idx, i in enumerate(parameter_list): 92 if i[:2] == '-I' or i[:2] == '-L': 93 parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:])) 94 95 return parameter_list 96 97 def sanity_check(self, work_dir, environment): 98 src = 'swifttest.swift' 99 source_name = os.path.join(work_dir, src) 100 output_name = os.path.join(work_dir, 'swifttest') 101 extra_flags = [] 102 extra_flags += environment.coredata.get_external_args(self.for_machine, self.language) 103 if self.is_cross: 104 extra_flags += self.get_compile_only_args() 105 else: 106 extra_flags += environment.coredata.get_external_link_args(self.for_machine, self.language) 107 with open(source_name, 'w') as ofile: 108 ofile.write('''print("Swift compilation is working.") 109''') 110 pc = subprocess.Popen(self.exelist + extra_flags + ['-emit-executable', '-o', output_name, src], cwd=work_dir) 111 pc.wait() 112 if pc.returncode != 0: 113 raise EnvironmentException('Swift compiler %s can not compile programs.' % self.name_string()) 114 if self.is_cross: 115 # Can't check if the binaries run so we have to assume they do 116 return 117 if subprocess.call(output_name) != 0: 118 raise EnvironmentException('Executables created by Swift compiler %s are not runnable.' % self.name_string()) 119 120 def get_debug_args(self, is_debug): 121 return clike_debug_args[is_debug] 122 123 def get_optimization_args(self, optimization_level): 124 return swift_optimization_args[optimization_level] 125