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 Microchip XC16 C 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
25xc16_buildtype_args = {
26    'plain': [],
27    'debug': [],
28    'debugoptimized': [],
29    'release': [],
30    'minsize': [],
31    'custom': [],
32}  # type: T.Dict[str, T.List[str]]
33
34xc16_optimization_args = {
35    '0': ['-O0'],
36    'g': ['-O0'],
37    '1': ['-O1'],
38    '2': ['-O2'],
39    '3': ['-O3'],
40    's': ['-Os']
41}  # type: T.Dict[str, T.List[str]]
42
43xc16_debug_args = {
44    False: [],
45    True: []
46}  # type: T.Dict[bool, T.List[str]]
47
48
49class Xc16Compiler:
50    def __init__(self):
51        if not self.is_cross:
52            raise EnvironmentException('xc16 supports only cross-compilation.')
53        self.id = 'xc16'
54        # Assembly
55        self.can_compile_suffixes.add('s')
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_always_args(self) -> T.List[str]:
63        return []
64
65    def get_pic_args(self) -> T.List[str]:
66        # PIC support is not enabled by default for xc16,
67        # if users want to use it, they need to add the required arguments explicitly
68        return []
69
70    def get_buildtype_args(self, buildtype: str) -> T.List[str]:
71        return xc16_buildtype_args[buildtype]
72
73    def get_pch_suffix(self) -> str:
74        return 'pch'
75
76    def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]:
77        return []
78
79    # Override CCompiler.get_dependency_gen_args
80    def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
81        return []
82
83    def thread_flags(self, env: 'Environment') -> T.List[str]:
84        return []
85
86    def get_coverage_args(self) -> T.List[str]:
87        return []
88
89    def get_no_stdinc_args(self) -> T.List[str]:
90        return ['-nostdinc']
91
92    def get_no_stdlib_link_args(self) -> T.List[str]:
93        return ['--nostdlib']
94
95    def get_optimization_args(self, optimization_level: str) -> T.List[str]:
96        return xc16_optimization_args[optimization_level]
97
98    def get_debug_args(self, is_debug: bool) -> T.List[str]:
99        return xc16_debug_args[is_debug]
100
101    @classmethod
102    def unix_args_to_native(cls, args: T.List[str]) -> T.List[str]:
103        result = []
104        for i in args:
105            if i.startswith('-D'):
106                i = '-D' + i[2:]
107            if i.startswith('-I'):
108                i = '-I' + i[2:]
109            if i.startswith('-Wl,-rpath='):
110                continue
111            elif i == '--print-search-dirs':
112                continue
113            elif i.startswith('-L'):
114                continue
115            result.append(i)
116        return result
117
118    def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], build_dir: str) -> T.List[str]:
119        for idx, i in enumerate(parameter_list):
120            if i[:9] == '-I':
121                parameter_list[idx] = i[:9] + os.path.normpath(os.path.join(build_dir, i[9:]))
122
123        return parameter_list
124