]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/workspace_tools/toolchains/arm.py
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / workspace_tools / toolchains / arm.py
1 """
2 mbed SDK
3 Copyright (c) 2011-2013 ARM Limited
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 """
17 import re
18 from os.path import join
19
20 from workspace_tools.toolchains import mbedToolchain
21 from workspace_tools.settings import ARM_BIN, ARM_INC, ARM_LIB, MY_ARM_CLIB, ARM_CPPLIB
22 from workspace_tools.hooks import hook_tool
23 from workspace_tools.settings import GOANNA_PATH
24
25 class ARM(mbedToolchain):
26     LINKER_EXT = '.sct'
27     LIBRARY_EXT = '.ar'
28
29     STD_LIB_NAME = "%s.ar"
30     DIAGNOSTIC_PATTERN  = re.compile('"(?P<file>[^"]+)", line (?P<line>\d+): (?P<severity>Warning|Error): (?P<message>.+)')
31     DEP_PATTERN = re.compile('\S+:\s(?P<file>.+)\n')
32
33     def __init__(self, target, options=None, notify=None, macros=None, silent=False):
34         mbedToolchain.__init__(self, target, options, notify, macros, silent)
35
36         if target.core == "Cortex-M0+":
37             cpu = "Cortex-M0"
38         elif target.core == "Cortex-M4F":
39             cpu = "Cortex-M4.fp"
40         elif target.core == "Cortex-M7F":
41             cpu = "Cortex-M7.fp.sp"
42         else:
43             cpu = target.core
44
45         main_cc = join(ARM_BIN, "armcc")
46         common = ["-c",
47             "--cpu=%s" % cpu, "--gnu",
48             "-Otime", "--split_sections", "--apcs=interwork",
49             "--brief_diagnostics", "--restrict", "--multibyte_chars"
50         ]
51
52         if "save-asm" in self.options:
53             common.extend(["--asm", "--interleave"])
54
55         if "debug-info" in self.options:
56             common.append("-g")
57             common.append("-O0")
58         else:
59             common.append("-O3")
60
61         common_c = [
62             "--md", "--no_depend_system_headers",
63             '-I%s' % ARM_INC
64         ]
65
66         self.asm = [main_cc] + common + ['-I%s' % ARM_INC]
67         if not "analyze" in self.options:
68             self.cc = [main_cc] + common + common_c + ["--c99"]
69             self.cppc = [main_cc] + common + common_c + ["--cpp", "--no_rtti"]
70         else:
71             self.cc  = [join(GOANNA_PATH, "goannacc"), "--with-cc=" + main_cc.replace('\\', '/'), "--dialect=armcc", '--output-format="%s"' % self.GOANNA_FORMAT] + common + common_c + ["--c99"]
72             self.cppc= [join(GOANNA_PATH, "goannac++"), "--with-cxx=" + main_cc.replace('\\', '/'), "--dialect=armcc", '--output-format="%s"' % self.GOANNA_FORMAT] + common + common_c + ["--cpp", "--no_rtti"]
73
74         self.ld = [join(ARM_BIN, "armlink")]
75         self.sys_libs = []
76
77         self.ar = join(ARM_BIN, "armar")
78         self.elf2bin = join(ARM_BIN, "fromelf")
79
80     def remove_option(self, option):
81         for tool in [self.asm, self.cc, self.cppc]:
82             if option in tool:
83                 tool.remove(option)
84
85     def assemble(self, source, object, includes):
86         # Preprocess first, then assemble
87         tempfile = object + '.E.s'
88         return [
89             self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-E", "-o", tempfile, source],
90             self.hook.get_cmdline_assembler(self.asm + ["-o", object, tempfile])
91         ]
92
93     def parse_dependencies(self, dep_path):
94         dependencies = []
95         for line in open(dep_path).readlines():
96             match = ARM.DEP_PATTERN.match(line)
97             if match is not None:
98                 dependencies.append(match.group('file'))
99         return dependencies
100
101     def parse_output(self, output):
102         for line in output.splitlines():
103             match = ARM.DIAGNOSTIC_PATTERN.match(line)
104             if match is not None:
105                 self.cc_info(
106                     match.group('severity').lower(),
107                     match.group('file'),
108                     match.group('line'),
109                     match.group('message'),
110                     target_name=self.target.name,
111                     toolchain_name=self.name
112                 )
113             match = self.goanna_parse_line(line)
114             if match is not None:
115                 self.cc_info(
116                     match.group('severity').lower(),
117                     match.group('file'),
118                     match.group('line'),
119                     match.group('message')
120                 )
121                 
122     def get_dep_opt(self, dep_path):
123         return ["--depend", dep_path]
124         
125     def archive(self, objects, lib_path):
126         self.default_cmd([self.ar, '-r', lib_path] + objects)
127
128     def link(self, output, objects, libraries, lib_dirs, mem_map):
129         if len(lib_dirs):
130             args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt"]
131         else:
132             args = ["-o", output, "--info=totals", "--list=.link_totals.txt"]
133
134         if mem_map:
135             args.extend(["--scatter", mem_map])
136
137         if hasattr(self.target, "link_cmdline_hook"):
138             args = self.target.link_cmdline_hook(self.__class__.__name__, args)
139
140         self.default_cmd(self.ld + args + objects + libraries + self.sys_libs)
141
142     @hook_tool
143     def binary(self, resources, elf, bin):
144         args = [self.elf2bin, '--bin', '-o', bin, elf]
145
146         if hasattr(self.target, "binary_cmdline_hook"):
147             args = self.target.binary_cmdline_hook(self.__class__.__name__, args)
148
149         self.default_cmd(args)
150
151 class ARM_STD(ARM):
152     def __init__(self, target, options=None, notify=None, macros=None, silent=False):
153         ARM.__init__(self, target, options, notify, macros, silent)
154         self.cc   += ["-D__ASSERT_MSG"]
155         self.cppc += ["-D__ASSERT_MSG"]
156         self.ld.append("--libpath=%s" % ARM_LIB)
157
158
159 class ARM_MICRO(ARM):
160     PATCHED_LIBRARY = False
161
162     def __init__(self, target, options=None, notify=None, macros=None, silent=False):
163         ARM.__init__(self, target, options, notify, macros, silent)
164
165         # Compiler
166         self.asm  += ["-D__MICROLIB"]
167         self.cc   += ["--library_type=microlib", "-D__MICROLIB", "-D__ASSERT_MSG"]
168         self.cppc += ["--library_type=microlib", "-D__MICROLIB", "-D__ASSERT_MSG"]
169
170         # Linker
171         self.ld.append("--library_type=microlib")
172
173         # We had to patch microlib to add C++ support
174         # In later releases this patch should have entered mainline
175         if ARM_MICRO.PATCHED_LIBRARY:
176             self.ld.append("--noscanlib")
177
178             # System Libraries
179             self.sys_libs.extend([join(MY_ARM_CLIB, lib+".l") for lib in ["mc_p", "mf_p", "m_ps"]])
180
181             if target.core == "Cortex-M3":
182                 self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ws", "cpprt_w"]])
183
184             elif target.core in ["Cortex-M0", "Cortex-M0+"]:
185                 self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ps", "cpprt_p"]])
186         else:
187             self.ld.append("--libpath=%s" % ARM_LIB)