Clone of official tools

Committer:
screamer
Date:
Sat Jul 16 00:34:03 2016 +0100
Revision:
22:9e85236d8716
Parent:
21:4fdf0dd04f6f
Child:
24:25bff2709c20
Sync with the mbedmicro/mbed tools

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