Clone of official tools
toolchains/arm.py@36:96847d42f010, 2017-06-22 (annotated)
- Committer:
- The Other Jimmy
- Date:
- Thu Jun 22 11:12:28 2017 -0500
- Revision:
- 36:96847d42f010
- Parent:
- 35:da9c89f8be7d
- Child:
- 40:7d3fa6b99b2b
Tools release 5.5.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:66f3b5499f7f | 1 | """ |
screamer | 0:66f3b5499f7f | 2 | mbed SDK |
screamer | 0:66f3b5499f7f | 3 | Copyright (c) 2011-2013 ARM Limited |
screamer | 0:66f3b5499f7f | 4 | |
screamer | 0:66f3b5499f7f | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:66f3b5499f7f | 6 | you may not use this file except in compliance with the License. |
screamer | 0:66f3b5499f7f | 7 | You may obtain a copy of the License at |
screamer | 0:66f3b5499f7f | 8 | |
screamer | 0:66f3b5499f7f | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:66f3b5499f7f | 10 | |
screamer | 0:66f3b5499f7f | 11 | Unless required by applicable law or agreed to in writing, software |
screamer | 0:66f3b5499f7f | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:66f3b5499f7f | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:66f3b5499f7f | 14 | See the License for the specific language governing permissions and |
screamer | 0:66f3b5499f7f | 15 | limitations under the License. |
screamer | 0:66f3b5499f7f | 16 | """ |
screamer | 0:66f3b5499f7f | 17 | import re |
The Other Jimmy |
36:96847d42f010 | 18 | from os.path import join, dirname, splitext, basename, exists |
The Other Jimmy |
36:96847d42f010 | 19 | from os import makedirs, write |
The Other Jimmy |
36:96847d42f010 | 20 | from tempfile import mkstemp |
screamer | 0:66f3b5499f7f | 21 | |
screamer | 20:835f6355470d | 22 | from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS |
screamer | 0:66f3b5499f7f | 23 | from tools.hooks import hook_tool |
screamer | 0:66f3b5499f7f | 24 | from tools.utils import mkdir |
screamer | 0:66f3b5499f7f | 25 | |
screamer | 0:66f3b5499f7f | 26 | class ARM(mbedToolchain): |
screamer | 0:66f3b5499f7f | 27 | LINKER_EXT = '.sct' |
screamer | 0:66f3b5499f7f | 28 | LIBRARY_EXT = '.ar' |
screamer | 0:66f3b5499f7f | 29 | |
screamer | 0:66f3b5499f7f | 30 | STD_LIB_NAME = "%s.ar" |
The Other Jimmy |
35:da9c89f8be7d | 31 | DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)", line (?P<line>\d+)( \(column (?P<column>\d+)\)|): (?P<severity>Warning|Error|Fatal error): (?P<message>.+)') |
screamer | 21:4fdf0dd04f6f | 32 | INDEX_PATTERN = re.compile('(?P<col>\s*)\^') |
screamer | 0:66f3b5499f7f | 33 | DEP_PATTERN = re.compile('\S+:\s(?P<file>.+)\n') |
screamer | 0:66f3b5499f7f | 34 | |
The Other Jimmy |
31:8ea194f6145b | 35 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 36 | def check_executable(): |
The Other Jimmy |
31:8ea194f6145b | 37 | """Returns True if the executable (armcc) location specified by the |
The Other Jimmy |
31:8ea194f6145b | 38 | user exists OR the executable can be found on the PATH. |
The Other Jimmy |
31:8ea194f6145b | 39 | Returns False otherwise.""" |
The Other Jimmy |
31:8ea194f6145b | 40 | return mbedToolchain.generic_check_executable("ARM", 'armcc', 2, 'bin') |
screamer | 13:ab47a20b66f0 | 41 | |
The Other Jimmy |
31:8ea194f6145b | 42 | def __init__(self, target, notify=None, macros=None, |
The Other Jimmy |
36:96847d42f010 | 43 | silent=False, extra_verbose=False, build_profile=None, |
The Other Jimmy |
36:96847d42f010 | 44 | build_dir=None): |
The Other Jimmy |
31:8ea194f6145b | 45 | mbedToolchain.__init__(self, target, notify, macros, silent, |
The Other Jimmy |
36:96847d42f010 | 46 | build_dir=build_dir, |
The Other Jimmy |
31:8ea194f6145b | 47 | extra_verbose=extra_verbose, |
The Other Jimmy |
31:8ea194f6145b | 48 | build_profile=build_profile) |
screamer | 0:66f3b5499f7f | 49 | |
screamer | 0:66f3b5499f7f | 50 | if target.core == "Cortex-M0+": |
screamer | 0:66f3b5499f7f | 51 | cpu = "Cortex-M0" |
screamer | 0:66f3b5499f7f | 52 | elif target.core == "Cortex-M4F": |
screamer | 0:66f3b5499f7f | 53 | cpu = "Cortex-M4.fp" |
screamer | 13:ab47a20b66f0 | 54 | elif target.core == "Cortex-M7FD": |
screamer | 13:ab47a20b66f0 | 55 | cpu = "Cortex-M7.fp.dp" |
screamer | 0:66f3b5499f7f | 56 | elif target.core == "Cortex-M7F": |
screamer | 0:66f3b5499f7f | 57 | cpu = "Cortex-M7.fp.sp" |
screamer | 0:66f3b5499f7f | 58 | else: |
screamer | 0:66f3b5499f7f | 59 | cpu = target.core |
screamer | 0:66f3b5499f7f | 60 | |
screamer | 20:835f6355470d | 61 | ARM_BIN = join(TOOLCHAIN_PATHS['ARM'], "bin") |
screamer | 20:835f6355470d | 62 | ARM_INC = join(TOOLCHAIN_PATHS['ARM'], "include") |
screamer | 20:835f6355470d | 63 | |
screamer | 0:66f3b5499f7f | 64 | main_cc = join(ARM_BIN, "armcc") |
screamer | 0:66f3b5499f7f | 65 | |
screamer | 13:ab47a20b66f0 | 66 | self.flags['common'] += ["--cpu=%s" % cpu] |
screamer | 0:66f3b5499f7f | 67 | |
The Other Jimmy |
31:8ea194f6145b | 68 | self.asm = [main_cc] + self.flags['common'] + self.flags['asm'] |
The Other Jimmy |
31:8ea194f6145b | 69 | self.cc = [main_cc] + self.flags['common'] + self.flags['c'] |
The Other Jimmy |
31:8ea194f6145b | 70 | self.cppc = [main_cc] + self.flags['common'] + self.flags['c'] + self.flags['cxx'] |
screamer | 0:66f3b5499f7f | 71 | |
screamer | 13:ab47a20b66f0 | 72 | self.ld = [join(ARM_BIN, "armlink")] |
screamer | 0:66f3b5499f7f | 73 | |
screamer | 0:66f3b5499f7f | 74 | self.ar = join(ARM_BIN, "armar") |
screamer | 0:66f3b5499f7f | 75 | self.elf2bin = join(ARM_BIN, "fromelf") |
screamer | 0:66f3b5499f7f | 76 | |
screamer | 0:66f3b5499f7f | 77 | def parse_dependencies(self, dep_path): |
screamer | 0:66f3b5499f7f | 78 | dependencies = [] |
screamer | 0:66f3b5499f7f | 79 | for line in open(dep_path).readlines(): |
screamer | 0:66f3b5499f7f | 80 | match = ARM.DEP_PATTERN.match(line) |
screamer | 0:66f3b5499f7f | 81 | if match is not None: |
screamer | 21:4fdf0dd04f6f | 82 | #we need to append chroot, because when the .d files are generated the compiler is chrooted |
screamer | 21:4fdf0dd04f6f | 83 | dependencies.append((self.CHROOT if self.CHROOT else '') + match.group('file')) |
screamer | 0:66f3b5499f7f | 84 | return dependencies |
screamer | 21:4fdf0dd04f6f | 85 | |
screamer | 0:66f3b5499f7f | 86 | def parse_output(self, output): |
screamer | 21:4fdf0dd04f6f | 87 | msg = None |
screamer | 0:66f3b5499f7f | 88 | for line in output.splitlines(): |
screamer | 0:66f3b5499f7f | 89 | match = ARM.DIAGNOSTIC_PATTERN.match(line) |
screamer | 0:66f3b5499f7f | 90 | if match is not None: |
screamer | 21:4fdf0dd04f6f | 91 | if msg is not None: |
screamer | 21:4fdf0dd04f6f | 92 | self.cc_info(msg) |
The Other Jimmy |
31:8ea194f6145b | 93 | msg = None |
screamer | 21:4fdf0dd04f6f | 94 | msg = { |
screamer | 21:4fdf0dd04f6f | 95 | 'severity': match.group('severity').lower(), |
screamer | 21:4fdf0dd04f6f | 96 | 'file': match.group('file'), |
screamer | 21:4fdf0dd04f6f | 97 | 'line': match.group('line'), |
screamer | 21:4fdf0dd04f6f | 98 | 'col': match.group('column') if match.group('column') else 0, |
screamer | 21:4fdf0dd04f6f | 99 | 'message': match.group('message'), |
screamer | 21:4fdf0dd04f6f | 100 | 'text': '', |
screamer | 21:4fdf0dd04f6f | 101 | 'target_name': self.target.name, |
screamer | 21:4fdf0dd04f6f | 102 | 'toolchain_name': self.name |
screamer | 21:4fdf0dd04f6f | 103 | } |
screamer | 21:4fdf0dd04f6f | 104 | elif msg is not None: |
screamer | 24:25bff2709c20 | 105 | # Determine the warning/error column by calculating the ^ position |
screamer | 21:4fdf0dd04f6f | 106 | match = ARM.INDEX_PATTERN.match(line) |
screamer | 21:4fdf0dd04f6f | 107 | if match is not None: |
screamer | 21:4fdf0dd04f6f | 108 | msg['col'] = len(match.group('col')) |
screamer | 21:4fdf0dd04f6f | 109 | self.cc_info(msg) |
screamer | 21:4fdf0dd04f6f | 110 | msg = None |
screamer | 21:4fdf0dd04f6f | 111 | else: |
screamer | 21:4fdf0dd04f6f | 112 | msg['text'] += line+"\n" |
screamer | 21:4fdf0dd04f6f | 113 | |
screamer | 21:4fdf0dd04f6f | 114 | if msg is not None: |
screamer | 21:4fdf0dd04f6f | 115 | self.cc_info(msg) |
screamer | 0:66f3b5499f7f | 116 | |
screamer | 0:66f3b5499f7f | 117 | def get_dep_option(self, object): |
screamer | 0:66f3b5499f7f | 118 | base, _ = splitext(object) |
screamer | 0:66f3b5499f7f | 119 | dep_path = base + '.d' |
screamer | 0:66f3b5499f7f | 120 | return ["--depend", dep_path] |
screamer | 0:66f3b5499f7f | 121 | |
screamer | 20:835f6355470d | 122 | def get_config_option(self, config_header): |
screamer | 13:ab47a20b66f0 | 123 | return ['--preinclude=' + config_header] |
screamer | 13:ab47a20b66f0 | 124 | |
screamer | 29:1210849dba19 | 125 | def get_compile_options(self, defines, includes, for_asm=False): |
screamer | 21:4fdf0dd04f6f | 126 | opts = ['-D%s' % d for d in defines] |
screamer | 21:4fdf0dd04f6f | 127 | if self.RESPONSE_FILES: |
screamer | 21:4fdf0dd04f6f | 128 | opts += ['--via', self.get_inc_file(includes)] |
screamer | 21:4fdf0dd04f6f | 129 | else: |
screamer | 21:4fdf0dd04f6f | 130 | opts += ["-I%s" % i for i in includes] |
screamer | 21:4fdf0dd04f6f | 131 | |
screamer | 29:1210849dba19 | 132 | if not for_asm: |
screamer | 29:1210849dba19 | 133 | config_header = self.get_config_header() |
screamer | 29:1210849dba19 | 134 | if config_header is not None: |
screamer | 29:1210849dba19 | 135 | opts = opts + self.get_config_option(config_header) |
screamer | 13:ab47a20b66f0 | 136 | return opts |
screamer | 0:66f3b5499f7f | 137 | |
screamer | 0:66f3b5499f7f | 138 | @hook_tool |
screamer | 0:66f3b5499f7f | 139 | def assemble(self, source, object, includes): |
screamer | 0:66f3b5499f7f | 140 | # Preprocess first, then assemble |
screamer | 0:66f3b5499f7f | 141 | dir = join(dirname(object), '.temp') |
screamer | 0:66f3b5499f7f | 142 | mkdir(dir) |
screamer | 0:66f3b5499f7f | 143 | tempfile = join(dir, basename(object) + '.E.s') |
screamer | 0:66f3b5499f7f | 144 | |
screamer | 0:66f3b5499f7f | 145 | # Build preprocess assemble command |
screamer | 29:1210849dba19 | 146 | cmd_pre = self.asm + self.get_compile_options(self.get_symbols(True), includes) + ["-E", "-o", tempfile, source] |
screamer | 0:66f3b5499f7f | 147 | |
screamer | 0:66f3b5499f7f | 148 | # Build main assemble command |
screamer | 0:66f3b5499f7f | 149 | cmd = self.asm + ["-o", object, tempfile] |
screamer | 0:66f3b5499f7f | 150 | |
screamer | 0:66f3b5499f7f | 151 | # Call cmdline hook |
screamer | 0:66f3b5499f7f | 152 | cmd_pre = self.hook.get_cmdline_assembler(cmd_pre) |
screamer | 0:66f3b5499f7f | 153 | cmd = self.hook.get_cmdline_assembler(cmd) |
screamer | 0:66f3b5499f7f | 154 | |
screamer | 0:66f3b5499f7f | 155 | # Return command array, don't execute |
screamer | 0:66f3b5499f7f | 156 | return [cmd_pre, cmd] |
screamer | 0:66f3b5499f7f | 157 | |
screamer | 0:66f3b5499f7f | 158 | @hook_tool |
screamer | 0:66f3b5499f7f | 159 | def compile(self, cc, source, object, includes): |
screamer | 0:66f3b5499f7f | 160 | # Build compile command |
screamer | 0:66f3b5499f7f | 161 | cmd = cc + self.get_compile_options(self.get_symbols(), includes) |
screamer | 0:66f3b5499f7f | 162 | |
screamer | 0:66f3b5499f7f | 163 | cmd.extend(self.get_dep_option(object)) |
screamer | 0:66f3b5499f7f | 164 | |
screamer | 0:66f3b5499f7f | 165 | cmd.extend(["-o", object, source]) |
screamer | 0:66f3b5499f7f | 166 | |
screamer | 0:66f3b5499f7f | 167 | # Call cmdline hook |
screamer | 0:66f3b5499f7f | 168 | cmd = self.hook.get_cmdline_compiler(cmd) |
screamer | 0:66f3b5499f7f | 169 | |
screamer | 0:66f3b5499f7f | 170 | return [cmd] |
screamer | 0:66f3b5499f7f | 171 | |
screamer | 0:66f3b5499f7f | 172 | def compile_c(self, source, object, includes): |
screamer | 0:66f3b5499f7f | 173 | return self.compile(self.cc, source, object, includes) |
screamer | 0:66f3b5499f7f | 174 | |
screamer | 0:66f3b5499f7f | 175 | def compile_cpp(self, source, object, includes): |
screamer | 0:66f3b5499f7f | 176 | return self.compile(self.cppc, source, object, includes) |
screamer | 0:66f3b5499f7f | 177 | |
screamer | 0:66f3b5499f7f | 178 | @hook_tool |
screamer | 0:66f3b5499f7f | 179 | def link(self, output, objects, libraries, lib_dirs, mem_map): |
screamer | 7:5af61d55adbe | 180 | map_file = splitext(output)[0] + ".map" |
screamer | 0:66f3b5499f7f | 181 | if len(lib_dirs): |
screamer | 7:5af61d55adbe | 182 | args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--map", "--list=%s" % map_file] |
screamer | 0:66f3b5499f7f | 183 | else: |
screamer | 7:5af61d55adbe | 184 | args = ["-o", output, "--info=totals", "--map", "--list=%s" % map_file] |
screamer | 0:66f3b5499f7f | 185 | |
The Other Jimmy |
36:96847d42f010 | 186 | args.extend(self.flags['ld']) |
The Other Jimmy |
36:96847d42f010 | 187 | |
screamer | 0:66f3b5499f7f | 188 | if mem_map: |
screamer | 0:66f3b5499f7f | 189 | args.extend(["--scatter", mem_map]) |
screamer | 0:66f3b5499f7f | 190 | |
screamer | 0:66f3b5499f7f | 191 | # Build linker command |
screamer | 0:66f3b5499f7f | 192 | cmd = self.ld + args + objects + libraries + self.sys_libs |
screamer | 0:66f3b5499f7f | 193 | |
screamer | 0:66f3b5499f7f | 194 | # Call cmdline hook |
screamer | 0:66f3b5499f7f | 195 | cmd = self.hook.get_cmdline_linker(cmd) |
screamer | 0:66f3b5499f7f | 196 | |
screamer | 21:4fdf0dd04f6f | 197 | if self.RESPONSE_FILES: |
screamer | 21:4fdf0dd04f6f | 198 | # Split link command to linker executable + response file |
screamer | 21:4fdf0dd04f6f | 199 | cmd_linker = cmd[0] |
screamer | 21:4fdf0dd04f6f | 200 | link_files = self.get_link_file(cmd[1:]) |
screamer | 21:4fdf0dd04f6f | 201 | cmd = [cmd_linker, '--via', link_files] |
screamer | 0:66f3b5499f7f | 202 | |
screamer | 0:66f3b5499f7f | 203 | # Exec command |
screamer | 21:4fdf0dd04f6f | 204 | self.cc_verbose("Link: %s" % ' '.join(cmd)) |
screamer | 21:4fdf0dd04f6f | 205 | self.default_cmd(cmd) |
screamer | 0:66f3b5499f7f | 206 | |
screamer | 0:66f3b5499f7f | 207 | @hook_tool |
screamer | 0:66f3b5499f7f | 208 | def archive(self, objects, lib_path): |
screamer | 21:4fdf0dd04f6f | 209 | if self.RESPONSE_FILES: |
screamer | 22:9e85236d8716 | 210 | param = ['--via', self.get_arch_file(objects)] |
screamer | 21:4fdf0dd04f6f | 211 | else: |
screamer | 21:4fdf0dd04f6f | 212 | param = objects |
screamer | 0:66f3b5499f7f | 213 | |
screamer | 0:66f3b5499f7f | 214 | # Exec command |
screamer | 21:4fdf0dd04f6f | 215 | self.default_cmd([self.ar, '-r', lib_path] + param) |
screamer | 0:66f3b5499f7f | 216 | |
screamer | 0:66f3b5499f7f | 217 | @hook_tool |
screamer | 0:66f3b5499f7f | 218 | def binary(self, resources, elf, bin): |
The Other Jimmy |
36:96847d42f010 | 219 | _, fmt = splitext(bin) |
The Other Jimmy |
36:96847d42f010 | 220 | bin_arg = {".bin": "--bin", ".hex": "--i32"}[fmt] |
screamer | 0:66f3b5499f7f | 221 | # Build binary command |
The Other Jimmy |
36:96847d42f010 | 222 | cmd = [self.elf2bin, bin_arg, '-o', bin, elf] |
screamer | 0:66f3b5499f7f | 223 | |
screamer | 0:66f3b5499f7f | 224 | # Call cmdline hook |
screamer | 0:66f3b5499f7f | 225 | cmd = self.hook.get_cmdline_binary(cmd) |
screamer | 0:66f3b5499f7f | 226 | |
screamer | 0:66f3b5499f7f | 227 | # Exec command |
screamer | 21:4fdf0dd04f6f | 228 | self.cc_verbose("FromELF: %s" % ' '.join(cmd)) |
screamer | 0:66f3b5499f7f | 229 | self.default_cmd(cmd) |
screamer | 0:66f3b5499f7f | 230 | |
The Other Jimmy |
36:96847d42f010 | 231 | @staticmethod |
The Other Jimmy |
36:96847d42f010 | 232 | def name_mangle(name): |
The Other Jimmy |
36:96847d42f010 | 233 | return "_Z%i%sv" % (len(name), name) |
The Other Jimmy |
36:96847d42f010 | 234 | |
The Other Jimmy |
36:96847d42f010 | 235 | @staticmethod |
The Other Jimmy |
36:96847d42f010 | 236 | def make_ld_define(name, value): |
The Other Jimmy |
36:96847d42f010 | 237 | return "--predefine=\"-D%s=0x%x\"" % (name, value) |
The Other Jimmy |
36:96847d42f010 | 238 | |
The Other Jimmy |
36:96847d42f010 | 239 | @staticmethod |
The Other Jimmy |
36:96847d42f010 | 240 | def redirect_symbol(source, sync, build_dir): |
The Other Jimmy |
36:96847d42f010 | 241 | if not exists(build_dir): |
The Other Jimmy |
36:96847d42f010 | 242 | makedirs(build_dir) |
The Other Jimmy |
36:96847d42f010 | 243 | handle, filename = mkstemp(prefix=".redirect-symbol.", dir=build_dir) |
The Other Jimmy |
36:96847d42f010 | 244 | write(handle, "RESOLVE %s AS %s\n" % (source, sync)) |
The Other Jimmy |
36:96847d42f010 | 245 | return "--edit=%s" % filename |
The Other Jimmy |
36:96847d42f010 | 246 | |
screamer | 0:66f3b5499f7f | 247 | |
screamer | 0:66f3b5499f7f | 248 | class ARM_STD(ARM): |
The Other Jimmy |
31:8ea194f6145b | 249 | pass |
screamer | 0:66f3b5499f7f | 250 | |
screamer | 0:66f3b5499f7f | 251 | |
screamer | 0:66f3b5499f7f | 252 | class ARM_MICRO(ARM): |
screamer | 0:66f3b5499f7f | 253 | PATCHED_LIBRARY = False |