Clone of official tools

Committer:
screamer
Date:
Mon Aug 01 09:10:17 2016 +0100
Revision:
24:25bff2709c20
Parent:
22:9e85236d8716
Child:
29:1210849dba19
Major update to tools from ARMmbed/mbed-os

Who changed what in which revision?

UserRevisionLine numberNew 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
screamer 0:66f3b5499f7f 18 from os.path import join, dirname, splitext, basename, exists
screamer 0:66f3b5499f7f 19
screamer 20:835f6355470d 20 from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS
screamer 0:66f3b5499f7f 21 from tools.hooks import hook_tool
screamer 0:66f3b5499f7f 22 from tools.utils import mkdir
screamer 13:ab47a20b66f0 23 import copy
screamer 0:66f3b5499f7f 24
screamer 0:66f3b5499f7f 25 class ARM(mbedToolchain):
screamer 0:66f3b5499f7f 26 LINKER_EXT = '.sct'
screamer 0:66f3b5499f7f 27 LIBRARY_EXT = '.ar'
screamer 0:66f3b5499f7f 28
screamer 0:66f3b5499f7f 29 STD_LIB_NAME = "%s.ar"
screamer 0:66f3b5499f7f 30 DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)", line (?P<line>\d+)( \(column (?P<column>\d+)\)|): (?P<severity>Warning|Error): (?P<message>.+)')
screamer 21:4fdf0dd04f6f 31 INDEX_PATTERN = re.compile('(?P<col>\s*)\^')
screamer 0:66f3b5499f7f 32 DEP_PATTERN = re.compile('\S+:\s(?P<file>.+)\n')
screamer 0:66f3b5499f7f 33
screamer 13:ab47a20b66f0 34
screamer 13:ab47a20b66f0 35 DEFAULT_FLAGS = {
screamer 13:ab47a20b66f0 36 'common': ["-c", "--gnu",
screamer 13:ab47a20b66f0 37 "-Otime", "--split_sections", "--apcs=interwork",
screamer 20:835f6355470d 38 "--brief_diagnostics", "--restrict", "--multibyte_chars"],
screamer 13:ab47a20b66f0 39 'asm': [],
screamer 13:ab47a20b66f0 40 'c': ["--md", "--no_depend_system_headers", "--c99", "-D__ASSERT_MSG"],
screamer 13:ab47a20b66f0 41 'cxx': ["--cpp", "--no_rtti", "--no_vla"],
screamer 13:ab47a20b66f0 42 'ld': [],
screamer 13:ab47a20b66f0 43 }
screamer 13:ab47a20b66f0 44
screamer 13:ab47a20b66f0 45 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 13:ab47a20b66f0 46 mbedToolchain.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 0:66f3b5499f7f 47
screamer 0:66f3b5499f7f 48 if target.core == "Cortex-M0+":
screamer 0:66f3b5499f7f 49 cpu = "Cortex-M0"
screamer 0:66f3b5499f7f 50 elif target.core == "Cortex-M4F":
screamer 0:66f3b5499f7f 51 cpu = "Cortex-M4.fp"
screamer 13:ab47a20b66f0 52 elif target.core == "Cortex-M7FD":
screamer 13:ab47a20b66f0 53 cpu = "Cortex-M7.fp.dp"
screamer 0:66f3b5499f7f 54 elif target.core == "Cortex-M7F":
screamer 0:66f3b5499f7f 55 cpu = "Cortex-M7.fp.sp"
screamer 0:66f3b5499f7f 56 else:
screamer 0:66f3b5499f7f 57 cpu = target.core
screamer 0:66f3b5499f7f 58
screamer 20:835f6355470d 59 ARM_BIN = join(TOOLCHAIN_PATHS['ARM'], "bin")
screamer 20:835f6355470d 60 ARM_INC = join(TOOLCHAIN_PATHS['ARM'], "include")
screamer 20:835f6355470d 61
screamer 0:66f3b5499f7f 62 main_cc = join(ARM_BIN, "armcc")
screamer 0:66f3b5499f7f 63
screamer 13:ab47a20b66f0 64 self.flags['common'] += ["--cpu=%s" % cpu]
screamer 0:66f3b5499f7f 65 if "save-asm" in self.options:
screamer 13:ab47a20b66f0 66 self.flags['common'].extend(["--asm", "--interleave"])
screamer 0:66f3b5499f7f 67
screamer 0:66f3b5499f7f 68 if "debug-info" in self.options:
screamer 13:ab47a20b66f0 69 self.flags['common'].append("-g")
screamer 13:ab47a20b66f0 70 self.flags['c'].append("-O0")
screamer 0:66f3b5499f7f 71 else:
screamer 13:ab47a20b66f0 72 self.flags['c'].append("-O3")
screamer 0:66f3b5499f7f 73
screamer 20:835f6355470d 74 self.asm = [main_cc] + self.flags['common'] + self.flags['asm'] + ["-I \""+ARM_INC+"\""]
screamer 21:4fdf0dd04f6f 75 self.cc = [main_cc] + self.flags['common'] + self.flags['c'] + ["-I \""+ARM_INC+"\""]
screamer 21:4fdf0dd04f6f 76 self.cppc = [main_cc] + self.flags['common'] + self.flags['c'] + self.flags['cxx'] + ["-I \""+ARM_INC+"\""]
screamer 0:66f3b5499f7f 77
screamer 13:ab47a20b66f0 78 self.ld = [join(ARM_BIN, "armlink")]
screamer 0:66f3b5499f7f 79 self.sys_libs = []
screamer 0:66f3b5499f7f 80
screamer 0:66f3b5499f7f 81 self.ar = join(ARM_BIN, "armar")
screamer 0:66f3b5499f7f 82 self.elf2bin = join(ARM_BIN, "fromelf")
screamer 0:66f3b5499f7f 83
screamer 0:66f3b5499f7f 84 def parse_dependencies(self, dep_path):
screamer 0:66f3b5499f7f 85 dependencies = []
screamer 0:66f3b5499f7f 86 for line in open(dep_path).readlines():
screamer 0:66f3b5499f7f 87 match = ARM.DEP_PATTERN.match(line)
screamer 0:66f3b5499f7f 88 if match is not None:
screamer 21:4fdf0dd04f6f 89 #we need to append chroot, because when the .d files are generated the compiler is chrooted
screamer 21:4fdf0dd04f6f 90 dependencies.append((self.CHROOT if self.CHROOT else '') + match.group('file'))
screamer 0:66f3b5499f7f 91 return dependencies
screamer 21:4fdf0dd04f6f 92
screamer 0:66f3b5499f7f 93 def parse_output(self, output):
screamer 21:4fdf0dd04f6f 94 msg = None
screamer 0:66f3b5499f7f 95 for line in output.splitlines():
screamer 0:66f3b5499f7f 96 match = ARM.DIAGNOSTIC_PATTERN.match(line)
screamer 0:66f3b5499f7f 97 if match is not None:
screamer 21:4fdf0dd04f6f 98 if msg is not None:
screamer 21:4fdf0dd04f6f 99 self.cc_info(msg)
screamer 21:4fdf0dd04f6f 100 msg = {
screamer 21:4fdf0dd04f6f 101 'severity': match.group('severity').lower(),
screamer 21:4fdf0dd04f6f 102 'file': match.group('file'),
screamer 21:4fdf0dd04f6f 103 'line': match.group('line'),
screamer 21:4fdf0dd04f6f 104 'col': match.group('column') if match.group('column') else 0,
screamer 21:4fdf0dd04f6f 105 'message': match.group('message'),
screamer 21:4fdf0dd04f6f 106 'text': '',
screamer 21:4fdf0dd04f6f 107 'target_name': self.target.name,
screamer 21:4fdf0dd04f6f 108 'toolchain_name': self.name
screamer 21:4fdf0dd04f6f 109 }
screamer 21:4fdf0dd04f6f 110 elif msg is not None:
screamer 24:25bff2709c20 111 # Determine the warning/error column by calculating the ^ position
screamer 21:4fdf0dd04f6f 112 match = ARM.INDEX_PATTERN.match(line)
screamer 21:4fdf0dd04f6f 113 if match is not None:
screamer 21:4fdf0dd04f6f 114 msg['col'] = len(match.group('col'))
screamer 21:4fdf0dd04f6f 115 self.cc_info(msg)
screamer 21:4fdf0dd04f6f 116 msg = None
screamer 21:4fdf0dd04f6f 117 else:
screamer 21:4fdf0dd04f6f 118 msg['text'] += line+"\n"
screamer 21:4fdf0dd04f6f 119
screamer 21:4fdf0dd04f6f 120 if msg is not None:
screamer 21:4fdf0dd04f6f 121 self.cc_info(msg)
screamer 0:66f3b5499f7f 122
screamer 0:66f3b5499f7f 123 def get_dep_option(self, object):
screamer 0:66f3b5499f7f 124 base, _ = splitext(object)
screamer 0:66f3b5499f7f 125 dep_path = base + '.d'
screamer 0:66f3b5499f7f 126 return ["--depend", dep_path]
screamer 0:66f3b5499f7f 127
screamer 20:835f6355470d 128 def get_config_option(self, config_header):
screamer 13:ab47a20b66f0 129 return ['--preinclude=' + config_header]
screamer 13:ab47a20b66f0 130
screamer 0:66f3b5499f7f 131 def get_compile_options(self, defines, includes):
screamer 21:4fdf0dd04f6f 132 opts = ['-D%s' % d for d in defines]
screamer 21:4fdf0dd04f6f 133 if self.RESPONSE_FILES:
screamer 21:4fdf0dd04f6f 134 opts += ['--via', self.get_inc_file(includes)]
screamer 21:4fdf0dd04f6f 135 else:
screamer 21:4fdf0dd04f6f 136 opts += ["-I%s" % i for i in includes]
screamer 21:4fdf0dd04f6f 137
screamer 13:ab47a20b66f0 138 config_header = self.get_config_header()
screamer 13:ab47a20b66f0 139 if config_header is not None:
screamer 13:ab47a20b66f0 140 opts = opts + self.get_config_option(config_header)
screamer 13:ab47a20b66f0 141 return opts
screamer 0:66f3b5499f7f 142
screamer 0:66f3b5499f7f 143 @hook_tool
screamer 0:66f3b5499f7f 144 def assemble(self, source, object, includes):
screamer 0:66f3b5499f7f 145 # Preprocess first, then assemble
screamer 0:66f3b5499f7f 146 dir = join(dirname(object), '.temp')
screamer 0:66f3b5499f7f 147 mkdir(dir)
screamer 0:66f3b5499f7f 148 tempfile = join(dir, basename(object) + '.E.s')
screamer 0:66f3b5499f7f 149
screamer 0:66f3b5499f7f 150 # Build preprocess assemble command
screamer 0:66f3b5499f7f 151 cmd_pre = self.asm + self.get_compile_options(self.get_symbols(), includes) + ["-E", "-o", tempfile, source]
screamer 0:66f3b5499f7f 152
screamer 0:66f3b5499f7f 153 # Build main assemble command
screamer 0:66f3b5499f7f 154 cmd = self.asm + ["-o", object, tempfile]
screamer 0:66f3b5499f7f 155
screamer 0:66f3b5499f7f 156 # Call cmdline hook
screamer 0:66f3b5499f7f 157 cmd_pre = self.hook.get_cmdline_assembler(cmd_pre)
screamer 0:66f3b5499f7f 158 cmd = self.hook.get_cmdline_assembler(cmd)
screamer 0:66f3b5499f7f 159
screamer 0:66f3b5499f7f 160 # Return command array, don't execute
screamer 0:66f3b5499f7f 161 return [cmd_pre, cmd]
screamer 0:66f3b5499f7f 162
screamer 0:66f3b5499f7f 163 @hook_tool
screamer 0:66f3b5499f7f 164 def compile(self, cc, source, object, includes):
screamer 0:66f3b5499f7f 165 # Build compile command
screamer 0:66f3b5499f7f 166 cmd = cc + self.get_compile_options(self.get_symbols(), includes)
screamer 0:66f3b5499f7f 167
screamer 0:66f3b5499f7f 168 cmd.extend(self.get_dep_option(object))
screamer 0:66f3b5499f7f 169
screamer 0:66f3b5499f7f 170 cmd.extend(["-o", object, source])
screamer 0:66f3b5499f7f 171
screamer 0:66f3b5499f7f 172 # Call cmdline hook
screamer 0:66f3b5499f7f 173 cmd = self.hook.get_cmdline_compiler(cmd)
screamer 0:66f3b5499f7f 174
screamer 0:66f3b5499f7f 175 return [cmd]
screamer 0:66f3b5499f7f 176
screamer 0:66f3b5499f7f 177 def compile_c(self, source, object, includes):
screamer 0:66f3b5499f7f 178 return self.compile(self.cc, source, object, includes)
screamer 0:66f3b5499f7f 179
screamer 0:66f3b5499f7f 180 def compile_cpp(self, source, object, includes):
screamer 0:66f3b5499f7f 181 return self.compile(self.cppc, source, object, includes)
screamer 0:66f3b5499f7f 182
screamer 0:66f3b5499f7f 183 @hook_tool
screamer 0:66f3b5499f7f 184 def link(self, output, objects, libraries, lib_dirs, mem_map):
screamer 7:5af61d55adbe 185 map_file = splitext(output)[0] + ".map"
screamer 0:66f3b5499f7f 186 if len(lib_dirs):
screamer 7:5af61d55adbe 187 args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--map", "--list=%s" % map_file]
screamer 0:66f3b5499f7f 188 else:
screamer 7:5af61d55adbe 189 args = ["-o", output, "--info=totals", "--map", "--list=%s" % map_file]
screamer 0:66f3b5499f7f 190
screamer 0:66f3b5499f7f 191 if mem_map:
screamer 0:66f3b5499f7f 192 args.extend(["--scatter", mem_map])
screamer 0:66f3b5499f7f 193
screamer 0:66f3b5499f7f 194 # Build linker command
screamer 0:66f3b5499f7f 195 cmd = self.ld + args + objects + libraries + self.sys_libs
screamer 0:66f3b5499f7f 196
screamer 0:66f3b5499f7f 197 # Call cmdline hook
screamer 0:66f3b5499f7f 198 cmd = self.hook.get_cmdline_linker(cmd)
screamer 0:66f3b5499f7f 199
screamer 21:4fdf0dd04f6f 200 if self.RESPONSE_FILES:
screamer 21:4fdf0dd04f6f 201 # Split link command to linker executable + response file
screamer 21:4fdf0dd04f6f 202 cmd_linker = cmd[0]
screamer 21:4fdf0dd04f6f 203 link_files = self.get_link_file(cmd[1:])
screamer 21:4fdf0dd04f6f 204 cmd = [cmd_linker, '--via', link_files]
screamer 0:66f3b5499f7f 205
screamer 0:66f3b5499f7f 206 # Exec command
screamer 21:4fdf0dd04f6f 207 self.cc_verbose("Link: %s" % ' '.join(cmd))
screamer 21:4fdf0dd04f6f 208 self.default_cmd(cmd)
screamer 0:66f3b5499f7f 209
screamer 0:66f3b5499f7f 210 @hook_tool
screamer 0:66f3b5499f7f 211 def archive(self, objects, lib_path):
screamer 21:4fdf0dd04f6f 212 if self.RESPONSE_FILES:
screamer 22:9e85236d8716 213 param = ['--via', self.get_arch_file(objects)]
screamer 21:4fdf0dd04f6f 214 else:
screamer 21:4fdf0dd04f6f 215 param = objects
screamer 0:66f3b5499f7f 216
screamer 0:66f3b5499f7f 217 # Exec command
screamer 21:4fdf0dd04f6f 218 self.default_cmd([self.ar, '-r', lib_path] + param)
screamer 0:66f3b5499f7f 219
screamer 0:66f3b5499f7f 220 @hook_tool
screamer 0:66f3b5499f7f 221 def binary(self, resources, elf, bin):
screamer 0:66f3b5499f7f 222 # Build binary command
screamer 0:66f3b5499f7f 223 cmd = [self.elf2bin, '--bin', '-o', bin, elf]
screamer 0:66f3b5499f7f 224
screamer 0:66f3b5499f7f 225 # Call cmdline hook
screamer 0:66f3b5499f7f 226 cmd = self.hook.get_cmdline_binary(cmd)
screamer 0:66f3b5499f7f 227
screamer 0:66f3b5499f7f 228 # Exec command
screamer 21:4fdf0dd04f6f 229 self.cc_verbose("FromELF: %s" % ' '.join(cmd))
screamer 0:66f3b5499f7f 230 self.default_cmd(cmd)
screamer 0:66f3b5499f7f 231
screamer 0:66f3b5499f7f 232
screamer 0:66f3b5499f7f 233 class ARM_STD(ARM):
screamer 13:ab47a20b66f0 234 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 13:ab47a20b66f0 235 ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 13:ab47a20b66f0 236
screamer 13:ab47a20b66f0 237 # Run-time values
screamer 21:4fdf0dd04f6f 238 self.ld.extend(["--libpath", join(TOOLCHAIN_PATHS['ARM'], "lib")])
screamer 0:66f3b5499f7f 239
screamer 0:66f3b5499f7f 240
screamer 0:66f3b5499f7f 241 class ARM_MICRO(ARM):
screamer 0:66f3b5499f7f 242 PATCHED_LIBRARY = False
screamer 0:66f3b5499f7f 243
screamer 13:ab47a20b66f0 244 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 13:ab47a20b66f0 245 ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 0:66f3b5499f7f 246
screamer 13:ab47a20b66f0 247 # Extend flags
screamer 13:ab47a20b66f0 248 self.flags['common'].extend(["-D__MICROLIB"])
screamer 13:ab47a20b66f0 249 self.flags['c'].extend(["--library_type=microlib"])
screamer 13:ab47a20b66f0 250 self.flags['ld'].extend(["--library_type=microlib"])
screamer 13:ab47a20b66f0 251
screamer 13:ab47a20b66f0 252 # Run-time values
screamer 0:66f3b5499f7f 253 self.asm += ["-D__MICROLIB"]
screamer 13:ab47a20b66f0 254 self.cc += ["-D__MICROLIB", "--library_type=microlib"]
screamer 13:ab47a20b66f0 255 self.cppc += ["-D__MICROLIB", "--library_type=microlib"]
screamer 13:ab47a20b66f0 256 self.ld += ["--library_type=microlib"]
screamer 0:66f3b5499f7f 257
screamer 13:ab47a20b66f0 258 # Only allow a single thread
screamer 13:ab47a20b66f0 259 self.cc += ["-DMBED_RTOS_SINGLE_THREAD"]
screamer 13:ab47a20b66f0 260 self.cppc += ["-DMBED_RTOS_SINGLE_THREAD"]
screamer 0:66f3b5499f7f 261
screamer 0:66f3b5499f7f 262 # We had to patch microlib to add C++ support
screamer 0:66f3b5499f7f 263 # In later releases this patch should have entered mainline
screamer 0:66f3b5499f7f 264 if ARM_MICRO.PATCHED_LIBRARY:
screamer 13:ab47a20b66f0 265 # Run-time values
screamer 13:ab47a20b66f0 266 self.flags['ld'].extend(["--noscanlib"])
screamer 13:ab47a20b66f0 267 # Run-time values
screamer 13:ab47a20b66f0 268 self.ld += ["--noscanlib"]
screamer 0:66f3b5499f7f 269
screamer 0:66f3b5499f7f 270 # System Libraries
screamer 20:835f6355470d 271 self.sys_libs.extend([join(TOOLCHAIN_PATHS['ARM'], "lib", "microlib", lib+".l") for lib in ["mc_p", "mf_p", "m_ps"]])
screamer 0:66f3b5499f7f 272
screamer 0:66f3b5499f7f 273 if target.core == "Cortex-M3":
screamer 20:835f6355470d 274 self.sys_libs.extend([join(TOOLCHAIN_PATHS['ARM'], "lib", "cpplib", lib+".l") for lib in ["cpp_ws", "cpprt_w"]])
screamer 0:66f3b5499f7f 275
screamer 0:66f3b5499f7f 276 elif target.core in ["Cortex-M0", "Cortex-M0+"]:
screamer 20:835f6355470d 277 self.sys_libs.extend([join(TOOLCHAIN_PATHS['ARM'], "lib", "cpplib", lib+".l") for lib in ["cpp_ps", "cpprt_p"]])
screamer 0:66f3b5499f7f 278 else:
screamer 13:ab47a20b66f0 279 # Run-time values
screamer 21:4fdf0dd04f6f 280 self.ld.extend(["--libpath", join(TOOLCHAIN_PATHS['ARM'], "lib")])