Clone of official tools

Committer:
theotherjimmy
Date:
Tue Sep 25 13:43:09 2018 -0500
Revision:
43:2a7da56ebd24
Parent:
41:2a77626a4c21
Release 5.10.0

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
The Other Jimmy 31:8ea194f6145b 19 from os.path import join, splitext, exists
theotherjimmy 43:2a7da56ebd24 20 from distutils.version import LooseVersion
screamer 0:66f3b5499f7f 21
screamer 20:835f6355470d 22 from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS
screamer 0:66f3b5499f7f 23 from tools.hooks import hook_tool
theotherjimmy 43:2a7da56ebd24 24 from tools.utils import run_cmd, NotSupportedException
screamer 0:66f3b5499f7f 25
screamer 0:66f3b5499f7f 26 class IAR(mbedToolchain):
screamer 0:66f3b5499f7f 27 LIBRARY_EXT = '.a'
screamer 0:66f3b5499f7f 28 LINKER_EXT = '.icf'
screamer 0:66f3b5499f7f 29 STD_LIB_NAME = "%s.a"
screamer 0:66f3b5499f7f 30
The Other Jimmy 35:da9c89f8be7d 31 DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)",(?P<line>[\d]+)\s+(?P<severity>Warning|Error|Fatal error)(?P<message>.+)')
screamer 21:4fdf0dd04f6f 32 INDEX_PATTERN = re.compile('(?P<col>\s*)\^')
theotherjimmy 43:2a7da56ebd24 33 IAR_VERSION_RE = re.compile(b"IAR ANSI C/C\+\+ Compiler V(\d+\.\d+)")
theotherjimmy 43:2a7da56ebd24 34 IAR_VERSION = LooseVersion("7.80")
screamer 0:66f3b5499f7f 35
The Other Jimmy 31:8ea194f6145b 36 @staticmethod
The Other Jimmy 31:8ea194f6145b 37 def check_executable():
The Other Jimmy 31:8ea194f6145b 38 """Returns True if the executable (arm-none-eabi-gcc) location
The Other Jimmy 31:8ea194f6145b 39 specified by the user exists OR the executable can be found on the PATH.
The Other Jimmy 31:8ea194f6145b 40 Returns False otherwise."""
The Other Jimmy 31:8ea194f6145b 41 return mbedToolchain.generic_check_executable("IAR", 'iccarm', 2, "bin")
screamer 0:66f3b5499f7f 42
theotherjimmy 43:2a7da56ebd24 43 def __init__(self, target, notify=None, macros=None, build_profile=None,
The Other Jimmy 36:96847d42f010 44 build_dir=None):
theotherjimmy 43:2a7da56ebd24 45 mbedToolchain.__init__(self, target, notify, macros, build_dir=build_dir,
The Other Jimmy 31:8ea194f6145b 46 build_profile=build_profile)
screamer 13:ab47a20b66f0 47 if target.core == "Cortex-M7F" or target.core == "Cortex-M7FD":
screamer 13:ab47a20b66f0 48 cpuchoice = "Cortex-M7"
theotherjimmy 40:7d3fa6b99b2b 49 elif target.core.startswith("Cortex-M23"):
theotherjimmy 40:7d3fa6b99b2b 50 cpuchoice = "8-M.baseline"
theotherjimmy 40:7d3fa6b99b2b 51 elif target.core.startswith("Cortex-M33"):
theotherjimmy 40:7d3fa6b99b2b 52 cpuchoice = "8-M.mainline"
screamer 13:ab47a20b66f0 53 else:
screamer 13:ab47a20b66f0 54 cpuchoice = target.core
The Other Jimmy 31:8ea194f6145b 55
screamer 13:ab47a20b66f0 56 # flags_cmd are used only by our scripts, the project files have them already defined,
screamer 13:ab47a20b66f0 57 # using this flags results in the errors (duplication)
screamer 13:ab47a20b66f0 58 # asm accepts --cpu Core or --fpu FPU, not like c/c++ --cpu=Core
theotherjimmy 40:7d3fa6b99b2b 59 asm_flags_cmd = ["--cpu", cpuchoice]
screamer 13:ab47a20b66f0 60 # custom c flags
theotherjimmy 40:7d3fa6b99b2b 61 c_flags_cmd = ["--cpu", cpuchoice]
The Other Jimmy 31:8ea194f6145b 62
The Other Jimmy 31:8ea194f6145b 63 c_flags_cmd.extend([
The Other Jimmy 31:8ea194f6145b 64 "--thumb", "--dlib_config", "DLib_Config_Full.h"
The Other Jimmy 31:8ea194f6145b 65 ])
screamer 13:ab47a20b66f0 66 # custom c++ cmd flags
screamer 13:ab47a20b66f0 67 cxx_flags_cmd = [
screamer 13:ab47a20b66f0 68 "--c++", "--no_rtti", "--no_exceptions"
screamer 13:ab47a20b66f0 69 ]
screamer 13:ab47a20b66f0 70 if target.core == "Cortex-M7FD":
screamer 13:ab47a20b66f0 71 asm_flags_cmd += ["--fpu", "VFPv5"]
screamer 13:ab47a20b66f0 72 c_flags_cmd.append("--fpu=VFPv5")
screamer 13:ab47a20b66f0 73 elif target.core == "Cortex-M7F":
screamer 13:ab47a20b66f0 74 asm_flags_cmd += ["--fpu", "VFPv5_sp"]
screamer 13:ab47a20b66f0 75 c_flags_cmd.append("--fpu=VFPv5_sp")
theotherjimmy 40:7d3fa6b99b2b 76 elif target.core == "Cortex-M23" or target.core == "Cortex-M33":
theotherjimmy 40:7d3fa6b99b2b 77 self.flags["asm"] += ["--cmse"]
theotherjimmy 43:2a7da56ebd24 78 self.flags["common"] += ["--cmse"]
theotherjimmy 43:2a7da56ebd24 79
theotherjimmy 43:2a7da56ebd24 80 # Create Secure library
theotherjimmy 43:2a7da56ebd24 81 if target.core == "Cortex-M23" or self.target.core == "Cortex-M33":
theotherjimmy 43:2a7da56ebd24 82 secure_file = join(build_dir, "cmse_lib.o")
theotherjimmy 43:2a7da56ebd24 83 self.flags["ld"] += ["--import_cmse_lib_out=%s" % secure_file]
screamer 0:66f3b5499f7f 84
screamer 20:835f6355470d 85 IAR_BIN = join(TOOLCHAIN_PATHS['IAR'], "bin")
screamer 0:66f3b5499f7f 86 main_cc = join(IAR_BIN, "iccarm")
screamer 13:ab47a20b66f0 87
screamer 13:ab47a20b66f0 88 self.asm = [join(IAR_BIN, "iasmarm")] + asm_flags_cmd + self.flags["asm"]
screamer 21:4fdf0dd04f6f 89 self.cc = [main_cc]
screamer 21:4fdf0dd04f6f 90 self.cppc = [main_cc]
screamer 13:ab47a20b66f0 91 self.cc += self.flags["common"] + c_flags_cmd + self.flags["c"]
screamer 13:ab47a20b66f0 92 self.cppc += self.flags["common"] + c_flags_cmd + cxx_flags_cmd + self.flags["cxx"]
The Other Jimmy 36:96847d42f010 93
theotherjimmy 41:2a77626a4c21 94 self.ld = [join(IAR_BIN, "ilinkarm")] + self.flags['ld']
screamer 0:66f3b5499f7f 95 self.ar = join(IAR_BIN, "iarchive")
screamer 0:66f3b5499f7f 96 self.elf2bin = join(IAR_BIN, "ielftool")
screamer 0:66f3b5499f7f 97
theotherjimmy 43:2a7da56ebd24 98 def version_check(self):
theotherjimmy 43:2a7da56ebd24 99 stdout, _, retcode = run_cmd([self.cc[0], "--version"], redirect=True)
theotherjimmy 43:2a7da56ebd24 100 msg = None
theotherjimmy 43:2a7da56ebd24 101 match = self.IAR_VERSION_RE.search(stdout)
theotherjimmy 43:2a7da56ebd24 102 found_version = match.group(1).decode("utf-8") if match else None
theotherjimmy 43:2a7da56ebd24 103 if found_version and LooseVersion(found_version) != self.IAR_VERSION:
theotherjimmy 43:2a7da56ebd24 104 msg = "Compiler version mismatch: Have {}; expected {}".format(
theotherjimmy 43:2a7da56ebd24 105 found_version, self.IAR_VERSION)
theotherjimmy 43:2a7da56ebd24 106 elif not match or len(match.groups()) != 1:
theotherjimmy 43:2a7da56ebd24 107 msg = ("Compiler version mismatch: Could Not detect compiler "
theotherjimmy 43:2a7da56ebd24 108 "version; expected {}".format(self.IAR_VERSION))
theotherjimmy 43:2a7da56ebd24 109 if msg:
theotherjimmy 43:2a7da56ebd24 110 self.notify.cc_info({
theotherjimmy 43:2a7da56ebd24 111 "message": msg,
theotherjimmy 43:2a7da56ebd24 112 "file": "",
theotherjimmy 43:2a7da56ebd24 113 "line": "",
theotherjimmy 43:2a7da56ebd24 114 "col": "",
theotherjimmy 43:2a7da56ebd24 115 "severity": "ERROR",
theotherjimmy 43:2a7da56ebd24 116 })
theotherjimmy 43:2a7da56ebd24 117
theotherjimmy 43:2a7da56ebd24 118
screamer 0:66f3b5499f7f 119 def parse_dependencies(self, dep_path):
screamer 21:4fdf0dd04f6f 120 return [(self.CHROOT if self.CHROOT else '')+path.strip() for path in open(dep_path).readlines()
screamer 0:66f3b5499f7f 121 if (path and not path.isspace())]
screamer 0:66f3b5499f7f 122
screamer 0:66f3b5499f7f 123 def parse_output(self, output):
screamer 21:4fdf0dd04f6f 124 msg = None
screamer 0:66f3b5499f7f 125 for line in output.splitlines():
screamer 0:66f3b5499f7f 126 match = IAR.DIAGNOSTIC_PATTERN.match(line)
screamer 0:66f3b5499f7f 127 if match is not None:
screamer 21:4fdf0dd04f6f 128 if msg is not None:
theotherjimmy 43:2a7da56ebd24 129 self.notify.cc_info(msg)
The Other Jimmy 31:8ea194f6145b 130 msg = None
screamer 21:4fdf0dd04f6f 131 msg = {
screamer 21:4fdf0dd04f6f 132 'severity': match.group('severity').lower(),
screamer 21:4fdf0dd04f6f 133 'file': match.group('file'),
screamer 21:4fdf0dd04f6f 134 'line': match.group('line'),
screamer 21:4fdf0dd04f6f 135 'col': 0,
screamer 21:4fdf0dd04f6f 136 'message': match.group('message'),
screamer 21:4fdf0dd04f6f 137 'text': '',
screamer 21:4fdf0dd04f6f 138 'target_name': self.target.name,
screamer 21:4fdf0dd04f6f 139 'toolchain_name': self.name
screamer 21:4fdf0dd04f6f 140 }
screamer 21:4fdf0dd04f6f 141 elif msg is not None:
screamer 24:25bff2709c20 142 # Determine the warning/error column by calculating the ^ position
screamer 21:4fdf0dd04f6f 143 match = IAR.INDEX_PATTERN.match(line)
screamer 21:4fdf0dd04f6f 144 if match is not None:
screamer 21:4fdf0dd04f6f 145 msg['col'] = len(match.group('col'))
theotherjimmy 43:2a7da56ebd24 146 self.notify.cc_info(msg)
screamer 21:4fdf0dd04f6f 147 msg = None
screamer 21:4fdf0dd04f6f 148 else:
screamer 21:4fdf0dd04f6f 149 msg['text'] += line+"\n"
screamer 0:66f3b5499f7f 150
The Other Jimmy 31:8ea194f6145b 151 if msg is not None:
theotherjimmy 43:2a7da56ebd24 152 self.notify.cc_info(msg)
The Other Jimmy 31:8ea194f6145b 153
screamer 0:66f3b5499f7f 154 def get_dep_option(self, object):
screamer 0:66f3b5499f7f 155 base, _ = splitext(object)
screamer 0:66f3b5499f7f 156 dep_path = base + '.d'
screamer 0:66f3b5499f7f 157 return ["--dependencies", dep_path]
screamer 0:66f3b5499f7f 158
screamer 0:66f3b5499f7f 159 def cc_extra(self, object):
screamer 0:66f3b5499f7f 160 base, _ = splitext(object)
screamer 13:ab47a20b66f0 161 return ["-l", base + '.s.txt']
screamer 13:ab47a20b66f0 162
screamer 13:ab47a20b66f0 163 def get_config_option(self, config_header):
screamer 13:ab47a20b66f0 164 return ['--preinclude=' + config_header]
screamer 0:66f3b5499f7f 165
screamer 13:ab47a20b66f0 166 def get_compile_options(self, defines, includes, for_asm=False):
screamer 21:4fdf0dd04f6f 167 opts = ['-D%s' % d for d in defines]
theotherjimmy 43:2a7da56ebd24 168 if for_asm:
theotherjimmy 43:2a7da56ebd24 169 config_macros = self.config.get_config_data_macros()
theotherjimmy 43:2a7da56ebd24 170 macros_cmd = ['"-D%s"' % d.replace('"', '').replace('//','/\/') for d in config_macros]
theotherjimmy 43:2a7da56ebd24 171 if self.RESPONSE_FILES:
theotherjimmy 43:2a7da56ebd24 172 via_file = self.make_option_file(
theotherjimmy 43:2a7da56ebd24 173 macros_cmd, "asm_macros_{}.xcl")
theotherjimmy 43:2a7da56ebd24 174 opts += ['-f', via_file]
theotherjimmy 43:2a7da56ebd24 175 else:
theotherjimmy 43:2a7da56ebd24 176 opts += macros_cmd
theotherjimmy 40:7d3fa6b99b2b 177 return opts
screamer 21:4fdf0dd04f6f 178 else:
theotherjimmy 43:2a7da56ebd24 179 if self.RESPONSE_FILES:
theotherjimmy 43:2a7da56ebd24 180 opts += ['-f', self.get_inc_file(includes)]
theotherjimmy 43:2a7da56ebd24 181 else:
theotherjimmy 43:2a7da56ebd24 182 opts += ["-I%s" % i for i in includes]
theotherjimmy 43:2a7da56ebd24 183 config_header = self.get_config_header()
theotherjimmy 43:2a7da56ebd24 184 if config_header is not None:
theotherjimmy 43:2a7da56ebd24 185 opts = opts + self.get_config_option(config_header)
screamer 21:4fdf0dd04f6f 186
theotherjimmy 43:2a7da56ebd24 187 return opts
screamer 0:66f3b5499f7f 188
screamer 0:66f3b5499f7f 189 @hook_tool
screamer 0:66f3b5499f7f 190 def assemble(self, source, object, includes):
screamer 0:66f3b5499f7f 191 # Build assemble command
screamer 29:1210849dba19 192 cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes, True) + ["-o", object, source]
screamer 0:66f3b5499f7f 193
screamer 0:66f3b5499f7f 194 # Call cmdline hook
screamer 0:66f3b5499f7f 195 cmd = self.hook.get_cmdline_assembler(cmd)
screamer 0:66f3b5499f7f 196
screamer 0:66f3b5499f7f 197 # Return command array, don't execute
screamer 0:66f3b5499f7f 198 return [cmd]
screamer 0:66f3b5499f7f 199
screamer 0:66f3b5499f7f 200 @hook_tool
screamer 0:66f3b5499f7f 201 def compile(self, cc, source, object, includes):
screamer 0:66f3b5499f7f 202 # Build compile command
screamer 0:66f3b5499f7f 203 cmd = cc + self.get_compile_options(self.get_symbols(), includes)
screamer 0:66f3b5499f7f 204
screamer 0:66f3b5499f7f 205 cmd.extend(self.get_dep_option(object))
screamer 0:66f3b5499f7f 206
screamer 0:66f3b5499f7f 207 cmd.extend(self.cc_extra(object))
screamer 0:66f3b5499f7f 208
screamer 0:66f3b5499f7f 209 cmd.extend(["-o", object, source])
screamer 0:66f3b5499f7f 210
screamer 0:66f3b5499f7f 211 # Call cmdline hook
screamer 0:66f3b5499f7f 212 cmd = self.hook.get_cmdline_compiler(cmd)
screamer 0:66f3b5499f7f 213
screamer 0:66f3b5499f7f 214 return [cmd]
screamer 0:66f3b5499f7f 215
screamer 0:66f3b5499f7f 216 def compile_c(self, source, object, includes):
screamer 0:66f3b5499f7f 217 return self.compile(self.cc, source, object, includes)
screamer 0:66f3b5499f7f 218
screamer 0:66f3b5499f7f 219 def compile_cpp(self, source, object, includes):
screamer 0:66f3b5499f7f 220 return self.compile(self.cppc, source, object, includes)
screamer 0:66f3b5499f7f 221
screamer 0:66f3b5499f7f 222 @hook_tool
screamer 0:66f3b5499f7f 223 def link(self, output, objects, libraries, lib_dirs, mem_map):
screamer 0:66f3b5499f7f 224 # Build linker command
screamer 7:5af61d55adbe 225 map_file = splitext(output)[0] + ".map"
theotherjimmy 41:2a77626a4c21 226 cmd = self.ld + [ "-o", output, "--map=%s" % map_file] + objects + libraries
screamer 0:66f3b5499f7f 227
screamer 0:66f3b5499f7f 228 if mem_map:
screamer 7:5af61d55adbe 229 cmd.extend(["--config", mem_map])
screamer 0:66f3b5499f7f 230
screamer 0:66f3b5499f7f 231 # Call cmdline hook
screamer 0:66f3b5499f7f 232 cmd = self.hook.get_cmdline_linker(cmd)
screamer 0:66f3b5499f7f 233
screamer 21:4fdf0dd04f6f 234 if self.RESPONSE_FILES:
screamer 21:4fdf0dd04f6f 235 # Split link command to linker executable + response file
screamer 21:4fdf0dd04f6f 236 cmd_linker = cmd[0]
screamer 21:4fdf0dd04f6f 237 link_files = self.get_link_file(cmd[1:])
screamer 21:4fdf0dd04f6f 238 cmd = [cmd_linker, '-f', link_files]
screamer 0:66f3b5499f7f 239
screamer 0:66f3b5499f7f 240 # Exec command
theotherjimmy 43:2a7da56ebd24 241 self.notify.cc_verbose("Link: %s" % ' '.join(cmd))
screamer 21:4fdf0dd04f6f 242 self.default_cmd(cmd)
screamer 0:66f3b5499f7f 243
screamer 0:66f3b5499f7f 244 @hook_tool
screamer 0:66f3b5499f7f 245 def archive(self, objects, lib_path):
screamer 21:4fdf0dd04f6f 246 if self.RESPONSE_FILES:
screamer 22:9e85236d8716 247 param = ['-f', self.get_arch_file(objects)]
screamer 21:4fdf0dd04f6f 248 else:
screamer 21:4fdf0dd04f6f 249 param = objects
screamer 0:66f3b5499f7f 250
screamer 0:66f3b5499f7f 251 if exists(lib_path):
screamer 0:66f3b5499f7f 252 remove(lib_path)
screamer 0:66f3b5499f7f 253
screamer 21:4fdf0dd04f6f 254 self.default_cmd([self.ar, lib_path] + param)
screamer 0:66f3b5499f7f 255
screamer 0:66f3b5499f7f 256 @hook_tool
screamer 0:66f3b5499f7f 257 def binary(self, resources, elf, bin):
The Other Jimmy 36:96847d42f010 258 _, fmt = splitext(bin)
The Other Jimmy 36:96847d42f010 259 bin_arg = {".bin": "--bin", ".hex": "--ihex"}[fmt]
screamer 0:66f3b5499f7f 260 # Build binary command
The Other Jimmy 36:96847d42f010 261 cmd = [self.elf2bin, bin_arg, elf, bin]
screamer 0:66f3b5499f7f 262
screamer 0:66f3b5499f7f 263 # Call cmdline hook
screamer 0:66f3b5499f7f 264 cmd = self.hook.get_cmdline_binary(cmd)
screamer 0:66f3b5499f7f 265
screamer 0:66f3b5499f7f 266 # Exec command
theotherjimmy 43:2a7da56ebd24 267 self.notify.cc_verbose("FromELF: %s" % ' '.join(cmd))
screamer 0:66f3b5499f7f 268 self.default_cmd(cmd)
The Other Jimmy 36:96847d42f010 269
The Other Jimmy 36:96847d42f010 270 @staticmethod
The Other Jimmy 36:96847d42f010 271 def name_mangle(name):
The Other Jimmy 36:96847d42f010 272 return "_Z%i%sv" % (len(name), name)
The Other Jimmy 36:96847d42f010 273
The Other Jimmy 36:96847d42f010 274 @staticmethod
The Other Jimmy 36:96847d42f010 275 def make_ld_define(name, value):
theotherjimmy 43:2a7da56ebd24 276 return "--config_def %s=%s" % (name, value)
The Other Jimmy 36:96847d42f010 277
The Other Jimmy 36:96847d42f010 278 @staticmethod
The Other Jimmy 36:96847d42f010 279 def redirect_symbol(source, sync, build_dir):
The Other Jimmy 36:96847d42f010 280 return "--redirect %s=%s" % (source, sync)