Clone of official tools

Committer:
screamer
Date:
Thu Jul 14 20:21:19 2016 +0100
Revision:
13:ab47a20b66f0
Parent:
7:5af61d55adbe
Child:
20:835f6355470d
Apply latest 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 0:66f3b5499f7f 20 from tools.toolchains import mbedToolchain
screamer 0:66f3b5499f7f 21 from tools.settings import ARM_BIN, ARM_INC, ARM_LIB, MY_ARM_CLIB, ARM_CPPLIB, GOANNA_PATH
screamer 0:66f3b5499f7f 22 from tools.hooks import hook_tool
screamer 0:66f3b5499f7f 23 from tools.utils import mkdir
screamer 13:ab47a20b66f0 24 import copy
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"
screamer 0:66f3b5499f7f 31 DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)", line (?P<line>\d+)( \(column (?P<column>\d+)\)|): (?P<severity>Warning|Error): (?P<message>.+)')
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 13:ab47a20b66f0 38 "--brief_diagnostics", "--restrict", "--multibyte_chars", "-I \""+ARM_INC+"\""],
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 0:66f3b5499f7f 59 main_cc = join(ARM_BIN, "armcc")
screamer 0:66f3b5499f7f 60
screamer 13:ab47a20b66f0 61 self.flags['common'] += ["--cpu=%s" % cpu]
screamer 0:66f3b5499f7f 62 if "save-asm" in self.options:
screamer 13:ab47a20b66f0 63 self.flags['common'].extend(["--asm", "--interleave"])
screamer 0:66f3b5499f7f 64
screamer 0:66f3b5499f7f 65 if "debug-info" in self.options:
screamer 13:ab47a20b66f0 66 self.flags['common'].append("-g")
screamer 13:ab47a20b66f0 67 self.flags['c'].append("-O0")
screamer 0:66f3b5499f7f 68 else:
screamer 13:ab47a20b66f0 69 self.flags['c'].append("-O3")
screamer 0:66f3b5499f7f 70
screamer 13:ab47a20b66f0 71 self.asm = [main_cc] + self.flags['common'] + self.flags['asm']
screamer 13:ab47a20b66f0 72 if not "analyze" in self.options:
screamer 13:ab47a20b66f0 73 self.cc = [main_cc] + self.flags['common'] + self.flags['c']
screamer 13:ab47a20b66f0 74 self.cppc = [main_cc] + self.flags['common'] + self.flags['c'] + self.flags['cxx']
screamer 13:ab47a20b66f0 75 else:
screamer 13:ab47a20b66f0 76 self.cc = [join(GOANNA_PATH, "goannacc"), "--with-cc=" + main_cc.replace('\\', '/'), "--dialect=armcc", '--output-format="%s"' % self.GOANNA_FORMAT] + self.flags['common'] + self.flags['c']
screamer 13:ab47a20b66f0 77 self.cppc= [join(GOANNA_PATH, "goannac++"), "--with-cxx=" + main_cc.replace('\\', '/'), "--dialect=armcc", '--output-format="%s"' % self.GOANNA_FORMAT] + self.flags['common'] + self.flags['c'] + self.flags['cxx']
screamer 0:66f3b5499f7f 78
screamer 13:ab47a20b66f0 79 self.ld = [join(ARM_BIN, "armlink")]
screamer 0:66f3b5499f7f 80 self.sys_libs = []
screamer 0:66f3b5499f7f 81
screamer 0:66f3b5499f7f 82 self.ar = join(ARM_BIN, "armar")
screamer 0:66f3b5499f7f 83 self.elf2bin = join(ARM_BIN, "fromelf")
screamer 0:66f3b5499f7f 84
screamer 0:66f3b5499f7f 85 def parse_dependencies(self, dep_path):
screamer 0:66f3b5499f7f 86 dependencies = []
screamer 0:66f3b5499f7f 87 for line in open(dep_path).readlines():
screamer 0:66f3b5499f7f 88 match = ARM.DEP_PATTERN.match(line)
screamer 0:66f3b5499f7f 89 if match is not None:
screamer 0:66f3b5499f7f 90 dependencies.append(match.group('file'))
screamer 0:66f3b5499f7f 91 return dependencies
screamer 0:66f3b5499f7f 92
screamer 0:66f3b5499f7f 93 def parse_output(self, output):
screamer 0:66f3b5499f7f 94 for line in output.splitlines():
screamer 0:66f3b5499f7f 95 match = ARM.DIAGNOSTIC_PATTERN.match(line)
screamer 0:66f3b5499f7f 96 if match is not None:
screamer 0:66f3b5499f7f 97 self.cc_info(
screamer 0:66f3b5499f7f 98 match.group('severity').lower(),
screamer 0:66f3b5499f7f 99 match.group('file'),
screamer 0:66f3b5499f7f 100 match.group('line'),
screamer 0:66f3b5499f7f 101 match.group('message'),
screamer 0:66f3b5499f7f 102 target_name=self.target.name,
screamer 0:66f3b5499f7f 103 toolchain_name=self.name
screamer 0:66f3b5499f7f 104 )
screamer 0:66f3b5499f7f 105 match = self.goanna_parse_line(line)
screamer 0:66f3b5499f7f 106 if match is not None:
screamer 0:66f3b5499f7f 107 self.cc_info(
screamer 0:66f3b5499f7f 108 match.group('severity').lower(),
screamer 0:66f3b5499f7f 109 match.group('file'),
screamer 0:66f3b5499f7f 110 match.group('line'),
screamer 0:66f3b5499f7f 111 match.group('message')
screamer 0:66f3b5499f7f 112 )
screamer 0:66f3b5499f7f 113
screamer 0:66f3b5499f7f 114 def get_dep_option(self, object):
screamer 0:66f3b5499f7f 115 base, _ = splitext(object)
screamer 0:66f3b5499f7f 116 dep_path = base + '.d'
screamer 0:66f3b5499f7f 117 return ["--depend", dep_path]
screamer 0:66f3b5499f7f 118
screamer 13:ab47a20b66f0 119 def get_config_option(self, config_header) :
screamer 13:ab47a20b66f0 120 return ['--preinclude=' + config_header]
screamer 13:ab47a20b66f0 121
screamer 0:66f3b5499f7f 122 def get_compile_options(self, defines, includes):
screamer 13:ab47a20b66f0 123 opts = ['-D%s' % d for d in defines] + ['--via', self.get_inc_file(includes)]
screamer 13:ab47a20b66f0 124 config_header = self.get_config_header()
screamer 13:ab47a20b66f0 125 if config_header is not None:
screamer 13:ab47a20b66f0 126 opts = opts + self.get_config_option(config_header)
screamer 13:ab47a20b66f0 127 return opts
screamer 0:66f3b5499f7f 128
screamer 0:66f3b5499f7f 129 @hook_tool
screamer 0:66f3b5499f7f 130 def assemble(self, source, object, includes):
screamer 0:66f3b5499f7f 131 # Preprocess first, then assemble
screamer 0:66f3b5499f7f 132 dir = join(dirname(object), '.temp')
screamer 0:66f3b5499f7f 133 mkdir(dir)
screamer 0:66f3b5499f7f 134 tempfile = join(dir, basename(object) + '.E.s')
screamer 0:66f3b5499f7f 135
screamer 0:66f3b5499f7f 136 # Build preprocess assemble command
screamer 0:66f3b5499f7f 137 cmd_pre = self.asm + self.get_compile_options(self.get_symbols(), includes) + ["-E", "-o", tempfile, source]
screamer 0:66f3b5499f7f 138
screamer 0:66f3b5499f7f 139 # Build main assemble command
screamer 0:66f3b5499f7f 140 cmd = self.asm + ["-o", object, tempfile]
screamer 0:66f3b5499f7f 141
screamer 0:66f3b5499f7f 142 # Call cmdline hook
screamer 0:66f3b5499f7f 143 cmd_pre = self.hook.get_cmdline_assembler(cmd_pre)
screamer 0:66f3b5499f7f 144 cmd = self.hook.get_cmdline_assembler(cmd)
screamer 0:66f3b5499f7f 145
screamer 0:66f3b5499f7f 146 # Return command array, don't execute
screamer 0:66f3b5499f7f 147 return [cmd_pre, cmd]
screamer 0:66f3b5499f7f 148
screamer 0:66f3b5499f7f 149 @hook_tool
screamer 0:66f3b5499f7f 150 def compile(self, cc, source, object, includes):
screamer 0:66f3b5499f7f 151 # Build compile command
screamer 0:66f3b5499f7f 152 cmd = cc + self.get_compile_options(self.get_symbols(), includes)
screamer 0:66f3b5499f7f 153
screamer 0:66f3b5499f7f 154 cmd.extend(self.get_dep_option(object))
screamer 0:66f3b5499f7f 155
screamer 0:66f3b5499f7f 156 cmd.extend(["-o", object, source])
screamer 0:66f3b5499f7f 157
screamer 0:66f3b5499f7f 158 # Call cmdline hook
screamer 0:66f3b5499f7f 159 cmd = self.hook.get_cmdline_compiler(cmd)
screamer 0:66f3b5499f7f 160
screamer 0:66f3b5499f7f 161 return [cmd]
screamer 0:66f3b5499f7f 162
screamer 0:66f3b5499f7f 163 def compile_c(self, source, object, includes):
screamer 0:66f3b5499f7f 164 return self.compile(self.cc, source, object, includes)
screamer 0:66f3b5499f7f 165
screamer 0:66f3b5499f7f 166 def compile_cpp(self, source, object, includes):
screamer 0:66f3b5499f7f 167 return self.compile(self.cppc, source, object, includes)
screamer 0:66f3b5499f7f 168
screamer 0:66f3b5499f7f 169 @hook_tool
screamer 0:66f3b5499f7f 170 def link(self, output, objects, libraries, lib_dirs, mem_map):
screamer 7:5af61d55adbe 171 map_file = splitext(output)[0] + ".map"
screamer 0:66f3b5499f7f 172 if len(lib_dirs):
screamer 7:5af61d55adbe 173 args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--map", "--list=%s" % map_file]
screamer 0:66f3b5499f7f 174 else:
screamer 7:5af61d55adbe 175 args = ["-o", output, "--info=totals", "--map", "--list=%s" % map_file]
screamer 0:66f3b5499f7f 176
screamer 0:66f3b5499f7f 177 if mem_map:
screamer 0:66f3b5499f7f 178 args.extend(["--scatter", mem_map])
screamer 0:66f3b5499f7f 179
screamer 0:66f3b5499f7f 180 # Build linker command
screamer 0:66f3b5499f7f 181 cmd = self.ld + args + objects + libraries + self.sys_libs
screamer 0:66f3b5499f7f 182
screamer 0:66f3b5499f7f 183 # Call cmdline hook
screamer 0:66f3b5499f7f 184 cmd = self.hook.get_cmdline_linker(cmd)
screamer 0:66f3b5499f7f 185
screamer 0:66f3b5499f7f 186 # Split link command to linker executable + response file
screamer 0:66f3b5499f7f 187 link_files = join(dirname(output), ".link_files.txt")
screamer 0:66f3b5499f7f 188 with open(link_files, "wb") as f:
screamer 0:66f3b5499f7f 189 cmd_linker = cmd[0]
screamer 0:66f3b5499f7f 190 cmd_list = []
screamer 0:66f3b5499f7f 191 for c in cmd[1:]:
screamer 0:66f3b5499f7f 192 if c:
screamer 0:66f3b5499f7f 193 cmd_list.append(('"%s"' % c) if not c.startswith('-') else c)
screamer 0:66f3b5499f7f 194 string = " ".join(cmd_list).replace("\\", "/")
screamer 0:66f3b5499f7f 195 f.write(string)
screamer 0:66f3b5499f7f 196
screamer 0:66f3b5499f7f 197 # Exec command
screamer 0:66f3b5499f7f 198 self.default_cmd([cmd_linker, '--via', link_files])
screamer 0:66f3b5499f7f 199
screamer 0:66f3b5499f7f 200 @hook_tool
screamer 0:66f3b5499f7f 201 def archive(self, objects, lib_path):
screamer 0:66f3b5499f7f 202 archive_files = join(dirname(lib_path), ".archive_files.txt")
screamer 0:66f3b5499f7f 203 with open(archive_files, "wb") as f:
screamer 0:66f3b5499f7f 204 o_list = []
screamer 0:66f3b5499f7f 205 for o in objects:
screamer 0:66f3b5499f7f 206 o_list.append('"%s"' % o)
screamer 0:66f3b5499f7f 207 string = " ".join(o_list).replace("\\", "/")
screamer 0:66f3b5499f7f 208 f.write(string)
screamer 0:66f3b5499f7f 209
screamer 0:66f3b5499f7f 210 # Exec command
screamer 0:66f3b5499f7f 211 self.default_cmd([self.ar, '-r', lib_path, '--via', archive_files])
screamer 0:66f3b5499f7f 212
screamer 0:66f3b5499f7f 213 @hook_tool
screamer 0:66f3b5499f7f 214 def binary(self, resources, elf, bin):
screamer 0:66f3b5499f7f 215 # Build binary command
screamer 0:66f3b5499f7f 216 cmd = [self.elf2bin, '--bin', '-o', bin, elf]
screamer 0:66f3b5499f7f 217
screamer 0:66f3b5499f7f 218 # Call cmdline hook
screamer 0:66f3b5499f7f 219 cmd = self.hook.get_cmdline_binary(cmd)
screamer 0:66f3b5499f7f 220
screamer 0:66f3b5499f7f 221 # Exec command
screamer 0:66f3b5499f7f 222 self.default_cmd(cmd)
screamer 0:66f3b5499f7f 223
screamer 0:66f3b5499f7f 224
screamer 0:66f3b5499f7f 225 class ARM_STD(ARM):
screamer 13:ab47a20b66f0 226 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 13:ab47a20b66f0 227 ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 13:ab47a20b66f0 228
screamer 13:ab47a20b66f0 229 # Run-time values
screamer 13:ab47a20b66f0 230 self.ld.extend(["--libpath \"%s\"" % ARM_LIB])
screamer 0:66f3b5499f7f 231
screamer 0:66f3b5499f7f 232
screamer 0:66f3b5499f7f 233 class ARM_MICRO(ARM):
screamer 0:66f3b5499f7f 234 PATCHED_LIBRARY = False
screamer 0:66f3b5499f7f 235
screamer 13:ab47a20b66f0 236 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 13:ab47a20b66f0 237 ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 0:66f3b5499f7f 238
screamer 13:ab47a20b66f0 239 # Extend flags
screamer 13:ab47a20b66f0 240 self.flags['common'].extend(["-D__MICROLIB"])
screamer 13:ab47a20b66f0 241 self.flags['c'].extend(["--library_type=microlib"])
screamer 13:ab47a20b66f0 242 self.flags['ld'].extend(["--library_type=microlib"])
screamer 13:ab47a20b66f0 243
screamer 13:ab47a20b66f0 244 # Run-time values
screamer 0:66f3b5499f7f 245 self.asm += ["-D__MICROLIB"]
screamer 13:ab47a20b66f0 246 self.cc += ["-D__MICROLIB", "--library_type=microlib"]
screamer 13:ab47a20b66f0 247 self.cppc += ["-D__MICROLIB", "--library_type=microlib"]
screamer 13:ab47a20b66f0 248 self.ld += ["--library_type=microlib"]
screamer 0:66f3b5499f7f 249
screamer 13:ab47a20b66f0 250 # Only allow a single thread
screamer 13:ab47a20b66f0 251 self.cc += ["-DMBED_RTOS_SINGLE_THREAD"]
screamer 13:ab47a20b66f0 252 self.cppc += ["-DMBED_RTOS_SINGLE_THREAD"]
screamer 0:66f3b5499f7f 253
screamer 0:66f3b5499f7f 254 # We had to patch microlib to add C++ support
screamer 0:66f3b5499f7f 255 # In later releases this patch should have entered mainline
screamer 0:66f3b5499f7f 256 if ARM_MICRO.PATCHED_LIBRARY:
screamer 13:ab47a20b66f0 257 # Run-time values
screamer 13:ab47a20b66f0 258 self.flags['ld'].extend(["--noscanlib"])
screamer 13:ab47a20b66f0 259 # Run-time values
screamer 13:ab47a20b66f0 260 self.ld += ["--noscanlib"]
screamer 0:66f3b5499f7f 261
screamer 0:66f3b5499f7f 262 # System Libraries
screamer 0:66f3b5499f7f 263 self.sys_libs.extend([join(MY_ARM_CLIB, lib+".l") for lib in ["mc_p", "mf_p", "m_ps"]])
screamer 0:66f3b5499f7f 264
screamer 0:66f3b5499f7f 265 if target.core == "Cortex-M3":
screamer 0:66f3b5499f7f 266 self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ws", "cpprt_w"]])
screamer 0:66f3b5499f7f 267
screamer 0:66f3b5499f7f 268 elif target.core in ["Cortex-M0", "Cortex-M0+"]:
screamer 0:66f3b5499f7f 269 self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ps", "cpprt_p"]])
screamer 0:66f3b5499f7f 270 else:
screamer 13:ab47a20b66f0 271 # Run-time values
screamer 13:ab47a20b66f0 272 self.ld.extend(["--libpath \"%s\"" % ARM_LIB])