Clone of official tools

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