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 0:66f3b5499f7f 18 from os import remove
screamer 0:66f3b5499f7f 19 from os.path import join, exists, dirname, splitext, exists
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
screamer 0:66f3b5499f7f 24 class IAR(mbedToolchain):
screamer 0:66f3b5499f7f 25 LIBRARY_EXT = '.a'
screamer 0:66f3b5499f7f 26 LINKER_EXT = '.icf'
screamer 0:66f3b5499f7f 27 STD_LIB_NAME = "%s.a"
screamer 0:66f3b5499f7f 28
screamer 0:66f3b5499f7f 29 DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)",(?P<line>[\d]+)\s+(?P<severity>Warning|Error)(?P<message>.+)')
screamer 21:4fdf0dd04f6f 30 INDEX_PATTERN = re.compile('(?P<col>\s*)\^')
screamer 0:66f3b5499f7f 31
screamer 13:ab47a20b66f0 32 DEFAULT_FLAGS = {
screamer 13:ab47a20b66f0 33 'common': [
screamer 0:66f3b5499f7f 34 "--no_wrap_diagnostics",
screamer 0:66f3b5499f7f 35 # Pa050: No need to be notified about "non-native end of line sequence"
screamer 0:66f3b5499f7f 36 # Pa084: Pointless integer comparison -> checks for the values of an enum, but we use values outside of the enum to notify errors (ie: NC).
screamer 0:66f3b5499f7f 37 # Pa093: Implicit conversion from float to integer (ie: wait_ms(85.4) -> wait_ms(85))
screamer 0:66f3b5499f7f 38 # Pa082: Operation involving two values from two registers (ie: (float)(*obj->MR)/(float)(LPC_PWM1->MR0))
screamer 13:ab47a20b66f0 39 "-e", # Enable IAR language extension
screamer 13:ab47a20b66f0 40 "--diag_suppress=Pa050,Pa084,Pa093,Pa082"],
screamer 13:ab47a20b66f0 41 'asm': [],
screamer 13:ab47a20b66f0 42 'c': ["--vla"],
screamer 13:ab47a20b66f0 43 'cxx': ["--guard_calls"],
screamer 13:ab47a20b66f0 44 'ld': ["--skip_dynamic_initialization", "--threaded_lib"],
screamer 13:ab47a20b66f0 45 }
screamer 0:66f3b5499f7f 46
screamer 13:ab47a20b66f0 47 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 13:ab47a20b66f0 48 mbedToolchain.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 13:ab47a20b66f0 49 if target.core == "Cortex-M7F" or target.core == "Cortex-M7FD":
screamer 13:ab47a20b66f0 50 cpuchoice = "Cortex-M7"
screamer 13:ab47a20b66f0 51 else:
screamer 13:ab47a20b66f0 52 cpuchoice = target.core
screamer 13:ab47a20b66f0 53 # flags_cmd are used only by our scripts, the project files have them already defined,
screamer 13:ab47a20b66f0 54 # using this flags results in the errors (duplication)
screamer 13:ab47a20b66f0 55 # asm accepts --cpu Core or --fpu FPU, not like c/c++ --cpu=Core
screamer 13:ab47a20b66f0 56 if target.core == "Cortex-M4F":
screamer 13:ab47a20b66f0 57 asm_flags_cmd = [
screamer 13:ab47a20b66f0 58 "--cpu", "Cortex-M4F"
screamer 13:ab47a20b66f0 59 ]
screamer 13:ab47a20b66f0 60 else:
screamer 13:ab47a20b66f0 61 asm_flags_cmd = [
screamer 13:ab47a20b66f0 62 "--cpu", cpuchoice
screamer 13:ab47a20b66f0 63 ]
screamer 13:ab47a20b66f0 64 # custom c flags
screamer 13:ab47a20b66f0 65 if target.core == "Cortex-M4F":
screamer 13:ab47a20b66f0 66 c_flags_cmd = [
screamer 13:ab47a20b66f0 67 "--cpu", "Cortex-M4F",
screamer 20:835f6355470d 68 "--thumb", "--dlib_config", join(TOOLCHAIN_PATHS['IAR'], "inc", "c", "DLib_Config_Full.h")
screamer 13:ab47a20b66f0 69 ]
screamer 13:ab47a20b66f0 70 else:
screamer 13:ab47a20b66f0 71 c_flags_cmd = [
screamer 13:ab47a20b66f0 72 "--cpu", cpuchoice,
screamer 20:835f6355470d 73 "--thumb", "--dlib_config", join(TOOLCHAIN_PATHS['IAR'], "inc", "c", "DLib_Config_Full.h")
screamer 13:ab47a20b66f0 74 ]
screamer 13:ab47a20b66f0 75 # custom c++ cmd flags
screamer 13:ab47a20b66f0 76 cxx_flags_cmd = [
screamer 13:ab47a20b66f0 77 "--c++", "--no_rtti", "--no_exceptions"
screamer 13:ab47a20b66f0 78 ]
screamer 13:ab47a20b66f0 79 if target.core == "Cortex-M7FD":
screamer 13:ab47a20b66f0 80 asm_flags_cmd += ["--fpu", "VFPv5"]
screamer 13:ab47a20b66f0 81 c_flags_cmd.append("--fpu=VFPv5")
screamer 13:ab47a20b66f0 82 elif target.core == "Cortex-M7F":
screamer 13:ab47a20b66f0 83 asm_flags_cmd += ["--fpu", "VFPv5_sp"]
screamer 13:ab47a20b66f0 84 c_flags_cmd.append("--fpu=VFPv5_sp")
screamer 0:66f3b5499f7f 85
screamer 0:66f3b5499f7f 86 if "debug-info" in self.options:
screamer 13:ab47a20b66f0 87 c_flags_cmd.append("-r")
screamer 13:ab47a20b66f0 88 c_flags_cmd.append("-On")
screamer 0:66f3b5499f7f 89 else:
screamer 13:ab47a20b66f0 90 c_flags_cmd.append("-Oh")
screamer 0:66f3b5499f7f 91
screamer 20:835f6355470d 92 IAR_BIN = join(TOOLCHAIN_PATHS['IAR'], "bin")
screamer 0:66f3b5499f7f 93 main_cc = join(IAR_BIN, "iccarm")
screamer 13:ab47a20b66f0 94
screamer 13:ab47a20b66f0 95 self.asm = [join(IAR_BIN, "iasmarm")] + asm_flags_cmd + self.flags["asm"]
screamer 21:4fdf0dd04f6f 96 self.cc = [main_cc]
screamer 21:4fdf0dd04f6f 97 self.cppc = [main_cc]
screamer 13:ab47a20b66f0 98 self.cc += self.flags["common"] + c_flags_cmd + self.flags["c"]
screamer 13:ab47a20b66f0 99 self.cppc += self.flags["common"] + c_flags_cmd + cxx_flags_cmd + self.flags["cxx"]
screamer 0:66f3b5499f7f 100 self.ld = join(IAR_BIN, "ilinkarm")
screamer 0:66f3b5499f7f 101 self.ar = join(IAR_BIN, "iarchive")
screamer 0:66f3b5499f7f 102 self.elf2bin = join(IAR_BIN, "ielftool")
screamer 0:66f3b5499f7f 103
screamer 0:66f3b5499f7f 104 def parse_dependencies(self, dep_path):
screamer 21:4fdf0dd04f6f 105 return [(self.CHROOT if self.CHROOT else '')+path.strip() for path in open(dep_path).readlines()
screamer 0:66f3b5499f7f 106 if (path and not path.isspace())]
screamer 0:66f3b5499f7f 107
screamer 0:66f3b5499f7f 108 def parse_output(self, output):
screamer 21:4fdf0dd04f6f 109 msg = None
screamer 0:66f3b5499f7f 110 for line in output.splitlines():
screamer 0:66f3b5499f7f 111 match = IAR.DIAGNOSTIC_PATTERN.match(line)
screamer 0:66f3b5499f7f 112 if match is not None:
screamer 21:4fdf0dd04f6f 113 if msg is not None:
screamer 21:4fdf0dd04f6f 114 self.cc_info(msg)
screamer 21:4fdf0dd04f6f 115 msg = {
screamer 21:4fdf0dd04f6f 116 'severity': match.group('severity').lower(),
screamer 21:4fdf0dd04f6f 117 'file': match.group('file'),
screamer 21:4fdf0dd04f6f 118 'line': match.group('line'),
screamer 21:4fdf0dd04f6f 119 'col': 0,
screamer 21:4fdf0dd04f6f 120 'message': match.group('message'),
screamer 21:4fdf0dd04f6f 121 'text': '',
screamer 21:4fdf0dd04f6f 122 'target_name': self.target.name,
screamer 21:4fdf0dd04f6f 123 'toolchain_name': self.name
screamer 21:4fdf0dd04f6f 124 }
screamer 21:4fdf0dd04f6f 125 elif msg is not None:
screamer 24:25bff2709c20 126 # Determine the warning/error column by calculating the ^ position
screamer 21:4fdf0dd04f6f 127 match = IAR.INDEX_PATTERN.match(line)
screamer 21:4fdf0dd04f6f 128 if match is not None:
screamer 21:4fdf0dd04f6f 129 msg['col'] = len(match.group('col'))
screamer 21:4fdf0dd04f6f 130 self.cc_info(msg)
screamer 21:4fdf0dd04f6f 131 msg = None
screamer 21:4fdf0dd04f6f 132 else:
screamer 21:4fdf0dd04f6f 133 msg['text'] += line+"\n"
screamer 0:66f3b5499f7f 134
screamer 0:66f3b5499f7f 135 def get_dep_option(self, object):
screamer 0:66f3b5499f7f 136 base, _ = splitext(object)
screamer 0:66f3b5499f7f 137 dep_path = base + '.d'
screamer 0:66f3b5499f7f 138 return ["--dependencies", dep_path]
screamer 0:66f3b5499f7f 139
screamer 0:66f3b5499f7f 140 def cc_extra(self, object):
screamer 0:66f3b5499f7f 141 base, _ = splitext(object)
screamer 13:ab47a20b66f0 142 return ["-l", base + '.s.txt']
screamer 13:ab47a20b66f0 143
screamer 13:ab47a20b66f0 144 def get_config_option(self, config_header):
screamer 13:ab47a20b66f0 145 return ['--preinclude=' + config_header]
screamer 0:66f3b5499f7f 146
screamer 13:ab47a20b66f0 147 def get_compile_options(self, defines, includes, for_asm=False):
screamer 21:4fdf0dd04f6f 148 opts = ['-D%s' % d for d in defines]
screamer 21:4fdf0dd04f6f 149 if self.RESPONSE_FILES:
screamer 21:4fdf0dd04f6f 150 opts += ['-f', self.get_inc_file(includes)]
screamer 21:4fdf0dd04f6f 151 else:
screamer 21:4fdf0dd04f6f 152 opts += ["-I%s" % i for i in includes]
screamer 21:4fdf0dd04f6f 153
screamer 29:1210849dba19 154 if not for_asm:
screamer 13:ab47a20b66f0 155 config_header = self.get_config_header()
screamer 13:ab47a20b66f0 156 if config_header is not None:
screamer 13:ab47a20b66f0 157 opts = opts + self.get_config_option(config_header)
screamer 13:ab47a20b66f0 158 return opts
screamer 0:66f3b5499f7f 159
screamer 0:66f3b5499f7f 160 @hook_tool
screamer 0:66f3b5499f7f 161 def assemble(self, source, object, includes):
screamer 0:66f3b5499f7f 162 # Build assemble command
screamer 29:1210849dba19 163 cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes, True) + ["-o", object, source]
screamer 0:66f3b5499f7f 164
screamer 0:66f3b5499f7f 165 # Call cmdline hook
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]
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(self.cc_extra(object))
screamer 0:66f3b5499f7f 179
screamer 0:66f3b5499f7f 180 cmd.extend(["-o", object, source])
screamer 0:66f3b5499f7f 181
screamer 0:66f3b5499f7f 182 # Call cmdline hook
screamer 0:66f3b5499f7f 183 cmd = self.hook.get_cmdline_compiler(cmd)
screamer 0:66f3b5499f7f 184
screamer 0:66f3b5499f7f 185 return [cmd]
screamer 0:66f3b5499f7f 186
screamer 0:66f3b5499f7f 187 def compile_c(self, source, object, includes):
screamer 0:66f3b5499f7f 188 return self.compile(self.cc, source, object, includes)
screamer 0:66f3b5499f7f 189
screamer 0:66f3b5499f7f 190 def compile_cpp(self, source, object, includes):
screamer 0:66f3b5499f7f 191 return self.compile(self.cppc, source, object, includes)
screamer 0:66f3b5499f7f 192
screamer 0:66f3b5499f7f 193 @hook_tool
screamer 0:66f3b5499f7f 194 def link(self, output, objects, libraries, lib_dirs, mem_map):
screamer 0:66f3b5499f7f 195 # Build linker command
screamer 7:5af61d55adbe 196 map_file = splitext(output)[0] + ".map"
screamer 13:ab47a20b66f0 197 cmd = [self.ld, "-o", output, "--map=%s" % map_file] + objects + libraries + self.flags['ld']
screamer 0:66f3b5499f7f 198
screamer 0:66f3b5499f7f 199 if mem_map:
screamer 7:5af61d55adbe 200 cmd.extend(["--config", mem_map])
screamer 0:66f3b5499f7f 201
screamer 0:66f3b5499f7f 202 # Call cmdline hook
screamer 0:66f3b5499f7f 203 cmd = self.hook.get_cmdline_linker(cmd)
screamer 0:66f3b5499f7f 204
screamer 21:4fdf0dd04f6f 205 if self.RESPONSE_FILES:
screamer 21:4fdf0dd04f6f 206 # Split link command to linker executable + response file
screamer 21:4fdf0dd04f6f 207 cmd_linker = cmd[0]
screamer 21:4fdf0dd04f6f 208 link_files = self.get_link_file(cmd[1:])
screamer 21:4fdf0dd04f6f 209 cmd = [cmd_linker, '-f', link_files]
screamer 0:66f3b5499f7f 210
screamer 0:66f3b5499f7f 211 # Exec command
screamer 21:4fdf0dd04f6f 212 self.cc_verbose("Link: %s" % ' '.join(cmd))
screamer 21:4fdf0dd04f6f 213 self.default_cmd(cmd)
screamer 0:66f3b5499f7f 214
screamer 0:66f3b5499f7f 215 @hook_tool
screamer 0:66f3b5499f7f 216 def archive(self, objects, lib_path):
screamer 21:4fdf0dd04f6f 217 if self.RESPONSE_FILES:
screamer 22:9e85236d8716 218 param = ['-f', self.get_arch_file(objects)]
screamer 21:4fdf0dd04f6f 219 else:
screamer 21:4fdf0dd04f6f 220 param = objects
screamer 0:66f3b5499f7f 221
screamer 0:66f3b5499f7f 222 if exists(lib_path):
screamer 0:66f3b5499f7f 223 remove(lib_path)
screamer 0:66f3b5499f7f 224
screamer 21:4fdf0dd04f6f 225 self.default_cmd([self.ar, lib_path] + param)
screamer 0:66f3b5499f7f 226
screamer 0:66f3b5499f7f 227 @hook_tool
screamer 0:66f3b5499f7f 228 def binary(self, resources, elf, bin):
screamer 0:66f3b5499f7f 229 # Build binary command
screamer 0:66f3b5499f7f 230 cmd = [self.elf2bin, "--bin", elf, bin]
screamer 0:66f3b5499f7f 231
screamer 0:66f3b5499f7f 232 # Call cmdline hook
screamer 0:66f3b5499f7f 233 cmd = self.hook.get_cmdline_binary(cmd)
screamer 0:66f3b5499f7f 234
screamer 0:66f3b5499f7f 235 # Exec command
screamer 21:4fdf0dd04f6f 236 self.cc_verbose("FromELF: %s" % ' '.join(cmd))
screamer 0:66f3b5499f7f 237 self.default_cmd(cmd)