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 Renesas CC-RX 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
32ccrx_buildtype_args = {
33    'plain': [],
34    'debug': [],
35    'debugoptimized': [],
36    'release': [],
37    'minsize': [],
38    'custom': [],
39}  # type: T.Dict[str, T.List[str]]
40
41ccrx_optimization_args = {
42    '0': ['-optimize=0'],
43    'g': ['-optimize=0'],
44    '1': ['-optimize=1'],
45    '2': ['-optimize=2'],
46    '3': ['-optimize=max'],
47    's': ['-optimize=2', '-size']
48}  # type: T.Dict[str, T.List[str]]
49
50ccrx_debug_args = {
51    False: [],
52    True: ['-debug']
53}  # type: T.Dict[bool, T.List[str]]
54
55
56class CcrxCompiler(Compiler):
57
58    if T.TYPE_CHECKING:
59        is_cross = True
60        can_compile_suffixes = set()  # type: T.Set[str]
61
62    def __init__(self) -> None:
63        if not self.is_cross:
64            raise EnvironmentException('ccrx supports only cross-compilation.')
65        self.id = 'ccrx'
66        # Assembly
67        self.can_compile_suffixes.add('src')
68        default_warn_args = []  # type: T.List[str]
69        self.warn_args = {'0': [],
70                          '1': default_warn_args,
71                          '2': default_warn_args + [],
72                          '3': default_warn_args + []}  # type: T.Dict[str, T.List[str]]
73
74    def get_pic_args(self) -> T.List[str]:
75        # PIC support is not enabled by default for CCRX,
76        # if users want to use it, they need to add the required arguments explicitly
77        return []
78
79    def get_buildtype_args(self, buildtype: str) -> T.List[str]:
80        return ccrx_buildtype_args[buildtype]
81
82    def get_pch_suffix(self) -> str:
83        return 'pch'
84
85    def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]:
86        return []
87
88    def thread_flags(self, env: 'Environment') -> T.List[str]:
89        return []
90
91    def get_coverage_args(self) -> T.List[str]:
92        return []
93
94    def get_no_stdinc_args(self) -> T.List[str]:
95        return []
96
97    def get_no_stdlib_link_args(self) -> T.List[str]:
98        return []
99
100    def get_optimization_args(self, optimization_level: str) -> T.List[str]:
101        return ccrx_optimization_args[optimization_level]
102
103    def get_debug_args(self, is_debug: bool) -> T.List[str]:
104        return ccrx_debug_args[is_debug]
105
106    @classmethod
107    def unix_args_to_native(cls, args: T.List[str]) -> T.List[str]:
108        result = []
109        for i in args:
110            if i.startswith('-D'):
111                i = '-define=' + i[2:]
112            if i.startswith('-I'):
113                i = '-include=' + i[2:]
114            if i.startswith('-Wl,-rpath='):
115                continue
116            elif i == '--print-search-dirs':
117                continue
118            elif i.startswith('-L'):
119                continue
120            elif not i.startswith('-lib=') and i.endswith(('.a', '.lib')):
121                i = '-lib=' + i
122            result.append(i)
123        return result
124
125    def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], build_dir: str) -> T.List[str]:
126        for idx, i in enumerate(parameter_list):
127            if i[:9] == '-include=':
128                parameter_list[idx] = i[:9] + os.path.normpath(os.path.join(build_dir, i[9:]))
129
130        return parameter_list
131