Debian Codegraph @

mesonbuild/compilers/cs.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2012-2017 The Meson development team
3
4 from __future__ import annotations
5
6 import os.path
7 import textwrap
8 import typing as T
9
10 from ..linkers import RSPFileSyntax
11
12 from .compilers import Compiler
13 from .mixins.islinker import BasicLinkerIsCompilerMixin
14
15 if T.TYPE_CHECKING:
16 from ..dependencies import Dependency
17 from ..environment import Environment
18 from ..mesonlib import MachineChoice
19
20 cs_optimization_args: T.Dict[str, T.List[str]] = {
21 'plain': [],
22 '0': [],
23 'g': [],
24 '1': ['-optimize+'],
25 '2': ['-optimize+'],
26 '3': ['-optimize+'],
27 's': ['-optimize+'],
28 }
29
30
31 class CsCompiler(BasicLinkerIsCompilerMixin, Compiler):
32
33 language = 'cs'
34
35 def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice,
36 env: Environment, runner: T.Optional[str] = None):
37 super().__init__([], exelist, version, for_machine, env)
38 self.runner = runner
39
40 @classmethod
41 def get_display_language(cls) -> str:
42 return 'C sharp'
43
44 def get_always_args(self) -> T.List[str]:
45 return ['/nologo']
46
47 def get_linker_always_args(self) -> T.List[str]:
48 return ['/nologo']
49
50 def get_output_args(self, fname: str) -> T.List[str]:
51 return ['-out:' + fname]
52
53 def get_link_args(self, fname: str) -> T.List[str]:
54 return ['-r:' + fname]
55
56 def get_werror_args(self) -> T.List[str]:
57 return ['-warnaserror']
58
59 def get_pic_args(self) -> T.List[str]:
60 return []
61
62 def get_dependency_compile_args(self, dep: Dependency) -> T.List[str]:
63 # Historically we ignored all compile args. Accept what we can, but
64 # filter out -I arguments, which are in some pkg-config files and
65 # aren't accepted by mcs.
66 return [a for a in dep.get_compile_args() if not a.startswith('-I')]
67
68 def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
69 build_dir: str) -> T.List[str]:
70 for idx, i in enumerate(parameter_list):
71 if i[:2] == '-L':
72 parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:]))
73 if i[:5] == '-lib:':
74 parameter_list[idx] = i[:5] + os.path.normpath(os.path.join(build_dir, i[5:]))
75
76 return parameter_list
77
78 def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]:
79 return []
80
81 def get_pch_name(self, header_name: str) -> str:
82 return ''
83
84 def _sanity_check_source_code(self) -> str:
85 return textwrap.dedent('''
86 public class Sanity {
87 static public void Main () {
88 }
89 }
90 ''')
91
92 def _sanity_check_run_with_exe_wrapper(self, command: T.List[str]) -> T.List[str]:
93 if self.runner:
94 return [self.runner] + command
95 return command
96
97 def needs_static_linker(self) -> bool:
98 return False
99
100 def get_debug_args(self, is_debug: bool) -> T.List[str]:
101 return ['-debug'] if is_debug else []
102
103 def get_optimization_args(self, optimization_level: str) -> T.List[str]:
104 return cs_optimization_args[optimization_level]
105
106
107 class MonoCompiler(CsCompiler):
108
109 id = 'mono'
110
111 def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice,
112 env: Environment):
113 super().__init__(exelist, version, for_machine, env, runner='mono')
114
115 def rsp_file_syntax(self) -> 'RSPFileSyntax':
116 return RSPFileSyntax.GCC
117
118
119 class VisualStudioCsCompiler(CsCompiler):
120
121 id = 'csc'
122
123 def get_debug_args(self, is_debug: bool) -> T.List[str]:
124 if is_debug:
125 return ['-debug'] if self.info.is_windows() else ['-debug:portable']
126 else:
127 return []
128
129 def rsp_file_syntax(self) -> 'RSPFileSyntax':
130 return RSPFileSyntax.MSVC