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    from ...compilers.compilers import Compiler
25else:
26    # This is a bit clever, for mypy we pretend that these mixins descend from
27    # Compiler, so we get all of the methods and attributes defined for us, but
28    # for runtime we make them descend from object (which all classes normally
29    # do). This gives up DRYer type checking, with no runtime impact
30    Compiler = object
31
32c2000_buildtype_args = {
33    'plain': [],
34    'debug': [],
35    'debugoptimized': [],
36    'release': [],
37    'minsize': [],
38    'custom': [],
39}  # type: T.Dict[str, T.List[str]]
40
41c2000_optimization_args = {
42    '0': ['-O0'],
43    'g': ['-Ooff'],
44    '1': ['-O1'],
45    '2': ['-O2'],
46    '3': ['-O3'],
47    's': ['-04']
48}  # type: T.Dict[str, T.List[str]]
49
50c2000_debug_args = {
51    False: [],
52    True: []
53}  # type: T.Dict[bool, T.List[str]]
54
55
56class C2000Compiler(Compiler):
57
58    def __init__(self) -> None:
59        if not self.is_cross:
60            raise EnvironmentException('c2000 supports only cross-compilation.')
61        self.id = 'c2000'
62        # Assembly
63        self.can_compile_suffixes.add('asm')
64        default_warn_args = []  # type: T.List[str]
65        self.warn_args = {'0': [],
66                          '1': default_warn_args,
67                          '2': default_warn_args + [],
68                          '3': default_warn_args + []}  # type: T.Dict[str, T.List[str]]
69
70    def get_pic_args(self) -> T.List[str]:
71        # PIC support is not enabled by default for c2000,
72        # if users want to use it, they need to add the required arguments explicitly
73        return []
74
75    def get_buildtype_args(self, buildtype: str) -> T.List[str]:
76        return c2000_buildtype_args[buildtype]
77
78    def get_pch_suffix(self) -> str:
79        return 'pch'
80
81    def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]:
82        return []
83
84    def thread_flags(self, env: 'Environment') -> T.List[str]:
85        return []
86
87    def get_coverage_args(self) -> T.List[str]:
88        return []
89
90    def get_no_stdinc_args(self) -> T.List[str]:
91        return []
92
93    def get_no_stdlib_link_args(self) -> T.List[str]:
94        return []
95
96    def get_optimization_args(self, optimization_level: str) -> T.List[str]:
97        return c2000_optimization_args[optimization_level]
98
99    def get_debug_args(self, is_debug: bool) -> T.List[str]:
100        return c2000_debug_args[is_debug]
101
102    @classmethod
103    def unix_args_to_native(cls, args: T.List[str]) -> T.List[str]:
104        result = []
105        for i in args:
106            if i.startswith('-D'):
107                i = '-define=' + i[2:]
108            if i.startswith('-I'):
109                i = '-include=' + i[2:]
110            if i.startswith('-Wl,-rpath='):
111                continue
112            elif i == '--print-search-dirs':
113                continue
114            elif i.startswith('-L'):
115                continue
116            result.append(i)
117        return result
118
119    def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], build_dir: str) -> T.List[str]:
120        for idx, i in enumerate(parameter_list):
121            if i[:9] == '-include=':
122                parameter_list[idx] = i[:9] + os.path.normpath(os.path.join(build_dir, i[9:]))
123
124        return parameter_list
125