1# Copyright 2012-2019 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
15"""Representations specific to the Texas Instruments C2000 compiler family."""
16
17import os
18import typing as T
19
20from ...mesonlib import EnvironmentException
21
22if T.TYPE_CHECKING:
23    from ...environment import Environment
24
25c2000_buildtype_args = {
26    'plain': [],
27    'debug': [],
28    'debugoptimized': [],
29    'release': [],
30    'minsize': [],
31    'custom': [],
32}  # type: T.Dict[str, T.List[str]]
33
34c2000_optimization_args = {
35    '0': ['-O0'],
36    'g': ['-Ooff'],
37    '1': ['-O1'],
38    '2': ['-O2'],
39    '3': ['-O3'],
40    's': ['-04']
41}  # type: T.Dict[str, T.List[str]]
42
43c2000_debug_args = {
44    False: [],
45    True: []
46}  # type: T.Dict[bool, T.List[str]]
47
48
49class C2000Compiler:
50    def __init__(self):
51        if not self.is_cross:
52            raise EnvironmentException('c2000 supports only cross-compilation.')
53        self.id = 'c2000'
54        # Assembly
55        self.can_compile_suffixes.add('asm')
56        default_warn_args = []  # type: T.List[str]
57        self.warn_args = {'0': [],
58                          '1': default_warn_args,
59                          '2': default_warn_args + [],
60                          '3': default_warn_args + []}
61
62    def get_pic_args(self) -> T.List[str]:
63        # PIC support is not enabled by default for c2000,
64        # if users want to use it, they need to add the required arguments explicitly
65        return []
66
67    def get_buildtype_args(self, buildtype: str) -> T.List[str]:
68        return c2000_buildtype_args[buildtype]
69
70    def get_pch_suffix(self) -> str:
71        return 'pch'
72
73    def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]:
74        return []
75
76    # Override CCompiler.get_dependency_gen_args
77    def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
78        return []
79
80    def thread_flags(self, env: 'Environment') -> T.List[str]:
81        return []
82
83    def get_coverage_args(self) -> T.List[str]:
84        return []
85
86    def get_no_stdinc_args(self) -> T.List[str]:
87        return []
88
89    def get_no_stdlib_link_args(self) -> T.List[str]:
90        return []
91
92    def get_optimization_args(self, optimization_level: str) -> T.List[str]:
93        return c2000_optimization_args[optimization_level]
94
95    def get_debug_args(self, is_debug: bool) -> T.List[str]:
96        return c2000_debug_args[is_debug]
97
98    @classmethod
99    def unix_args_to_native(cls, args: T.List[str]) -> T.List[str]:
100        result = []
101        for i in args:
102            if i.startswith('-D'):
103                i = '-define=' + i[2:]
104            if i.startswith('-I'):
105                i = '-include=' + i[2:]
106            if i.startswith('-Wl,-rpath='):
107                continue
108            elif i == '--print-search-dirs':
109                continue
110            elif i.startswith('-L'):
111                continue
112            result.append(i)
113        return result
114
115    def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], build_dir: str) -> T.List[str]:
116        for idx, i in enumerate(parameter_list):
117            if i[:9] == '-include=':
118                parameter_list[idx] = i[:9] + os.path.normpath(os.path.join(build_dir, i[9:]))
119
120        return parameter_list
121