Clone of official tools

Committer:
screamer
Date:
Mon Aug 29 11:56:59 2016 +0100
Revision:
30:f12ce67666d0
Parent:
29:1210849dba19
Child:
31:8ea194f6145b
Update tools from the mbed OS 5.1.2 release (instead of master)

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 30:f12ce67666d0 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 30:f12ce67666d0 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 29:1210849dba19 131 def get_compile_options(self, defines, includes, for_asm=False):
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 29:1210849dba19 138 if not for_asm:
screamer 29:1210849dba19 139 config_header = self.get_config_header()
screamer 29:1210849dba19 140 if config_header is not None:
screamer 29:1210849dba19 141 opts = opts + self.get_config_option(config_header)
screamer 13:ab47a20b66f0 142 return opts
screamer 0:66f3b5499f7f 143
screamer 0:66f3b5499f7f 144 @hook_tool
screamer 0:66f3b5499f7f 145 def assemble(self, source, object, includes):
screamer 0:66f3b5499f7f 146 # Preprocess first, then assemble
screamer 0:66f3b5499f7f 147 dir = join(dirname(object), '.temp')
screamer 0:66f3b5499f7f 148 mkdir(dir)
screamer 0:66f3b5499f7f 149 tempfile = join(dir, basename(object) + '.E.s')
screamer 0:66f3b5499f7f 150
screamer 0:66f3b5499f7f 151 # Build preprocess assemble command
screamer 29:1210849dba19 152 cmd_pre = self.asm + self.get_compile_options(self.get_symbols(True), includes) + ["-E", "-o", tempfile, source]
screamer 0:66f3b5499f7f 153
screamer 0:66f3b5499f7f 154 # Build main assemble command
screamer 0:66f3b5499f7f 155 cmd = self.asm + ["-o", object, tempfile]
screamer 0:66f3b5499f7f 156
screamer 0:66f3b5499f7f 157 # Call cmdline hook
screamer 0:66f3b5499f7f 158 cmd_pre = self.hook.get_cmdline_assembler(cmd_pre)
screamer 0:66f3b5499f7f 159 cmd = self.hook.get_cmdline_assembler(cmd)
screamer 0:66f3b5499f7f 160
screamer 0:66f3b5499f7f 161 # Return command array, don't execute
screamer 0:66f3b5499f7f 162 return [cmd_pre, cmd]
screamer 0:66f3b5499f7f 163
screamer 0:66f3b5499f7f 164 @hook_tool
screamer 0:66f3b5499f7f 165 def compile(self, cc, source, object, includes):
screamer 0:66f3b5499f7f 166 # Build compile command
screamer 0:66f3b5499f7f 167 cmd = cc + self.get_compile_options(self.get_symbols(), includes)
screamer 0:66f3b5499f7f 168
screamer 0:66f3b5499f7f 169 cmd.extend(self.get_dep_option(object))
screamer 0:66f3b5499f7f 170
screamer 0:66f3b5499f7f 171 cmd.extend(["-o", object, source])
screamer 0:66f3b5499f7f 172
screamer 0:66f3b5499f7f 173 # Call cmdline hook
screamer 0:66f3b5499f7f 174 cmd = self.hook.get_cmdline_compiler(cmd)
screamer 0:66f3b5499f7f 175
screamer 0:66f3b5499f7f 176 return [cmd]
screamer 0:66f3b5499f7f 177
screamer 0:66f3b5499f7f 178 def compile_c(self, source, object, includes):
screamer 0:66f3b5499f7f 179 return self.compile(self.cc, source, object, includes)
screamer 0:66f3b5499f7f 180
screamer 0:66f3b5499f7f 181 def compile_cpp(self, source, object, includes):
screamer 0:66f3b5499f7f 182 return self.compile(self.cppc, source, object, includes)
screamer 0:66f3b5499f7f 183
screamer 0:66f3b5499f7f 184 @hook_tool
screamer 0:66f3b5499f7f 185 def link(self, output, objects, libraries, lib_dirs, mem_map):
screamer 7:5af61d55adbe 186 map_file = splitext(output)[0] + ".map"
screamer 0:66f3b5499f7f 187 if len(lib_dirs):
screamer 7:5af61d55adbe 188 args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--map", "--list=%s" % map_file]
screamer 0:66f3b5499f7f 189 else:
screamer 7:5af61d55adbe 190 args = ["-o", output, "--info=totals", "--map", "--list=%s" % map_file]
screamer 0:66f3b5499f7f 191
screamer 0:66f3b5499f7f 192 if mem_map:
screamer 0:66f3b5499f7f 193 args.extend(["--scatter", mem_map])
screamer 0:66f3b5499f7f 194
screamer 0:66f3b5499f7f 195 # Build linker command
screamer 0:66f3b5499f7f 196 cmd = self.ld + args + objects + libraries + self.sys_libs
screamer 0:66f3b5499f7f 197
screamer 0:66f3b5499f7f 198 # Call cmdline hook
screamer 0:66f3b5499f7f 199 cmd = self.hook.get_cmdline_linker(cmd)
screamer 0:66f3b5499f7f 200
screamer 21:4fdf0dd04f6f 201 if self.RESPONSE_FILES:
screamer 21:4fdf0dd04f6f 202 # Split link command to linker executable + response file
screamer 21:4fdf0dd04f6f 203 cmd_linker = cmd[0]
screamer 21:4fdf0dd04f6f 204 link_files = self.get_link_file(cmd[1:])
screamer 21:4fdf0dd04f6f 205 cmd = [cmd_linker, '--via', link_files]
screamer 0:66f3b5499f7f 206
screamer 0:66f3b5499f7f 207 # Exec command
screamer 21:4fdf0dd04f6f 208 self.cc_verbose("Link: %s" % ' '.join(cmd))
screamer 21:4fdf0dd04f6f 209 self.default_cmd(cmd)
screamer 0:66f3b5499f7f 210
screamer 0:66f3b5499f7f 211 @hook_tool
screamer 0:66f3b5499f7f 212 def archive(self, objects, lib_path):
screamer 21:4fdf0dd04f6f 213 if self.RESPONSE_FILES:
screamer 22:9e85236d8716 214 param = ['--via', self.get_arch_file(objects)]
screamer 21:4fdf0dd04f6f 215 else:
screamer 21:4fdf0dd04f6f 216 param = objects
screamer 0:66f3b5499f7f 217
screamer 0:66f3b5499f7f 218 # Exec command
screamer 21:4fdf0dd04f6f 219 self.default_cmd([self.ar, '-r', lib_path] + param)
screamer 0:66f3b5499f7f 220
screamer 0:66f3b5499f7f 221 @hook_tool
screamer 0:66f3b5499f7f 222 def binary(self, resources, elf, bin):
screamer 0:66f3b5499f7f 223 # Build binary command
screamer 0:66f3b5499f7f 224 cmd = [self.elf2bin, '--bin', '-o', bin, elf]
screamer 0:66f3b5499f7f 225
screamer 0:66f3b5499f7f 226 # Call cmdline hook
screamer 0:66f3b5499f7f 227 cmd = self.hook.get_cmdline_binary(cmd)
screamer 0:66f3b5499f7f 228
screamer 0:66f3b5499f7f 229 # Exec command
screamer 21:4fdf0dd04f6f 230 self.cc_verbose("FromELF: %s" % ' '.join(cmd))
screamer 0:66f3b5499f7f 231 self.default_cmd(cmd)
screamer 0:66f3b5499f7f 232
screamer 0:66f3b5499f7f 233
screamer 0:66f3b5499f7f 234 class ARM_STD(ARM):
screamer 13:ab47a20b66f0 235 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 13:ab47a20b66f0 236 ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 13:ab47a20b66f0 237
screamer 13:ab47a20b66f0 238 # Run-time values
screamer 21:4fdf0dd04f6f 239 self.ld.extend(["--libpath", join(TOOLCHAIN_PATHS['ARM'], "lib")])
screamer 0:66f3b5499f7f 240
screamer 0:66f3b5499f7f 241
screamer 0:66f3b5499f7f 242 class ARM_MICRO(ARM):
screamer 0:66f3b5499f7f 243 PATCHED_LIBRARY = False
screamer 0:66f3b5499f7f 244
screamer 13:ab47a20b66f0 245 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 13:ab47a20b66f0 246 ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 0:66f3b5499f7f 247
screamer 13:ab47a20b66f0 248 # Extend flags
screamer 13:ab47a20b66f0 249 self.flags['common'].extend(["-D__MICROLIB"])
screamer 13:ab47a20b66f0 250 self.flags['c'].extend(["--library_type=microlib"])
screamer 13:ab47a20b66f0 251 self.flags['ld'].extend(["--library_type=microlib"])
screamer 13:ab47a20b66f0 252
screamer 13:ab47a20b66f0 253 # Run-time values
screamer 0:66f3b5499f7f 254 self.asm += ["-D__MICROLIB"]
screamer 13:ab47a20b66f0 255 self.cc += ["-D__MICROLIB", "--library_type=microlib"]
screamer 13:ab47a20b66f0 256 self.cppc += ["-D__MICROLIB", "--library_type=microlib"]
screamer 13:ab47a20b66f0 257 self.ld += ["--library_type=microlib"]
screamer 0:66f3b5499f7f 258
screamer 13:ab47a20b66f0 259 # Only allow a single thread
screamer 13:ab47a20b66f0 260 self.cc += ["-DMBED_RTOS_SINGLE_THREAD"]
screamer 13:ab47a20b66f0 261 self.cppc += ["-DMBED_RTOS_SINGLE_THREAD"]
screamer 0:66f3b5499f7f 262
screamer 0:66f3b5499f7f 263 # We had to patch microlib to add C++ support
screamer 0:66f3b5499f7f 264 # In later releases this patch should have entered mainline
screamer 0:66f3b5499f7f 265 if ARM_MICRO.PATCHED_LIBRARY:
screamer 13:ab47a20b66f0 266 # Run-time values
screamer 13:ab47a20b66f0 267 self.flags['ld'].extend(["--noscanlib"])
screamer 13:ab47a20b66f0 268 # Run-time values
screamer 13:ab47a20b66f0 269 self.ld += ["--noscanlib"]
screamer 0:66f3b5499f7f 270
screamer 0:66f3b5499f7f 271 # System Libraries
screamer 20:835f6355470d 272 self.sys_libs.extend([join(TOOLCHAIN_PATHS['ARM'], "lib", "microlib", lib+".l") for lib in ["mc_p", "mf_p", "m_ps"]])
screamer 0:66f3b5499f7f 273
screamer 0:66f3b5499f7f 274 if target.core == "Cortex-M3":
screamer 20:835f6355470d 275 self.sys_libs.extend([join(TOOLCHAIN_PATHS['ARM'], "lib", "cpplib", lib+".l") for lib in ["cpp_ws", "cpprt_w"]])
screamer 0:66f3b5499f7f 276
screamer 0:66f3b5499f7f 277 elif target.core in ["Cortex-M0", "Cortex-M0+"]:
screamer 20:835f6355470d 278 self.sys_libs.extend([join(TOOLCHAIN_PATHS['ARM'], "lib", "cpplib", lib+".l") for lib in ["cpp_ps", "cpprt_p"]])
screamer 0:66f3b5499f7f 279 else:
screamer 13:ab47a20b66f0 280 # Run-time values
screamer 21:4fdf0dd04f6f 281 self.ld.extend(["--libpath", join(TOOLCHAIN_PATHS['ARM'], "lib")])