Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:0e018d759a2a 1 """
switches 0:0e018d759a2a 2 mbed SDK
switches 0:0e018d759a2a 3 Copyright (c) 2011-2013 ARM Limited
switches 0:0e018d759a2a 4
switches 0:0e018d759a2a 5 Licensed under the Apache License, Version 2.0 (the "License");
switches 0:0e018d759a2a 6 you may not use this file except in compliance with the License.
switches 0:0e018d759a2a 7 You may obtain a copy of the License at
switches 0:0e018d759a2a 8
switches 0:0e018d759a2a 9 http://www.apache.org/licenses/LICENSE-2.0
switches 0:0e018d759a2a 10
switches 0:0e018d759a2a 11 Unless required by applicable law or agreed to in writing, software
switches 0:0e018d759a2a 12 distributed under the License is distributed on an "AS IS" BASIS,
switches 0:0e018d759a2a 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:0e018d759a2a 14 See the License for the specific language governing permissions and
switches 0:0e018d759a2a 15 limitations under the License.
switches 0:0e018d759a2a 16 """
switches 0:0e018d759a2a 17 import re
switches 0:0e018d759a2a 18 from os import remove
switches 0:0e018d759a2a 19 from os.path import join, splitext, exists
switches 0:0e018d759a2a 20
switches 0:0e018d759a2a 21 from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS
switches 0:0e018d759a2a 22 from tools.hooks import hook_tool
switches 0:0e018d759a2a 23
switches 0:0e018d759a2a 24 class IAR(mbedToolchain):
switches 0:0e018d759a2a 25 LIBRARY_EXT = '.a'
switches 0:0e018d759a2a 26 LINKER_EXT = '.icf'
switches 0:0e018d759a2a 27 STD_LIB_NAME = "%s.a"
switches 0:0e018d759a2a 28
switches 0:0e018d759a2a 29 DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)",(?P<line>[\d]+)\s+(?P<severity>Warning|Error)(?P<message>.+)')
switches 0:0e018d759a2a 30 INDEX_PATTERN = re.compile('(?P<col>\s*)\^')
switches 0:0e018d759a2a 31
switches 0:0e018d759a2a 32 @staticmethod
switches 0:0e018d759a2a 33 def check_executable():
switches 0:0e018d759a2a 34 """Returns True if the executable (arm-none-eabi-gcc) location
switches 0:0e018d759a2a 35 specified by the user exists OR the executable can be found on the PATH.
switches 0:0e018d759a2a 36 Returns False otherwise."""
switches 0:0e018d759a2a 37 return mbedToolchain.generic_check_executable("IAR", 'iccarm', 2, "bin")
switches 0:0e018d759a2a 38
switches 0:0e018d759a2a 39 def __init__(self, target, notify=None, macros=None,
switches 0:0e018d759a2a 40 silent=False, extra_verbose=False, build_profile=None):
switches 0:0e018d759a2a 41 mbedToolchain.__init__(self, target, notify, macros, silent,
switches 0:0e018d759a2a 42 extra_verbose=extra_verbose,
switches 0:0e018d759a2a 43 build_profile=build_profile)
switches 0:0e018d759a2a 44 if target.core == "Cortex-M7F" or target.core == "Cortex-M7FD":
switches 0:0e018d759a2a 45 cpuchoice = "Cortex-M7"
switches 0:0e018d759a2a 46 else:
switches 0:0e018d759a2a 47 cpuchoice = target.core
switches 0:0e018d759a2a 48
switches 0:0e018d759a2a 49 # flags_cmd are used only by our scripts, the project files have them already defined,
switches 0:0e018d759a2a 50 # using this flags results in the errors (duplication)
switches 0:0e018d759a2a 51 # asm accepts --cpu Core or --fpu FPU, not like c/c++ --cpu=Core
switches 0:0e018d759a2a 52 if target.core == "Cortex-M4F":
switches 0:0e018d759a2a 53 asm_flags_cmd = [
switches 0:0e018d759a2a 54 "--cpu", "Cortex-M4F"
switches 0:0e018d759a2a 55 ]
switches 0:0e018d759a2a 56 else:
switches 0:0e018d759a2a 57 asm_flags_cmd = [
switches 0:0e018d759a2a 58 "--cpu", cpuchoice
switches 0:0e018d759a2a 59 ]
switches 0:0e018d759a2a 60 # custom c flags
switches 0:0e018d759a2a 61 if target.core == "Cortex-M4F":
switches 0:0e018d759a2a 62 c_flags_cmd = [
switches 0:0e018d759a2a 63 "--cpu", "Cortex-M4F",
switches 0:0e018d759a2a 64 "--thumb", "--dlib_config", join(TOOLCHAIN_PATHS['IAR'], "inc", "c", "DLib_Config_Full.h")
switches 0:0e018d759a2a 65 ]
switches 0:0e018d759a2a 66 else:
switches 0:0e018d759a2a 67 c_flags_cmd = [
switches 0:0e018d759a2a 68 "--cpu", cpuchoice,
switches 0:0e018d759a2a 69 "--thumb", "--dlib_config", join(TOOLCHAIN_PATHS['IAR'], "inc", "c", "DLib_Config_Full.h")
switches 0:0e018d759a2a 70 ]
switches 0:0e018d759a2a 71 # custom c++ cmd flags
switches 0:0e018d759a2a 72 cxx_flags_cmd = [
switches 0:0e018d759a2a 73 "--c++", "--no_rtti", "--no_exceptions"
switches 0:0e018d759a2a 74 ]
switches 0:0e018d759a2a 75 if target.core == "Cortex-M7FD":
switches 0:0e018d759a2a 76 asm_flags_cmd += ["--fpu", "VFPv5"]
switches 0:0e018d759a2a 77 c_flags_cmd.append("--fpu=VFPv5")
switches 0:0e018d759a2a 78 elif target.core == "Cortex-M7F":
switches 0:0e018d759a2a 79 asm_flags_cmd += ["--fpu", "VFPv5_sp"]
switches 0:0e018d759a2a 80 c_flags_cmd.append("--fpu=VFPv5_sp")
switches 0:0e018d759a2a 81
switches 0:0e018d759a2a 82 IAR_BIN = join(TOOLCHAIN_PATHS['IAR'], "bin")
switches 0:0e018d759a2a 83 main_cc = join(IAR_BIN, "iccarm")
switches 0:0e018d759a2a 84
switches 0:0e018d759a2a 85 self.asm = [join(IAR_BIN, "iasmarm")] + asm_flags_cmd + self.flags["asm"]
switches 0:0e018d759a2a 86 self.cc = [main_cc]
switches 0:0e018d759a2a 87 self.cppc = [main_cc]
switches 0:0e018d759a2a 88 self.cc += self.flags["common"] + c_flags_cmd + self.flags["c"]
switches 0:0e018d759a2a 89 self.cppc += self.flags["common"] + c_flags_cmd + cxx_flags_cmd + self.flags["cxx"]
switches 0:0e018d759a2a 90 self.ld = [join(IAR_BIN, "ilinkarm")]
switches 0:0e018d759a2a 91 self.ar = join(IAR_BIN, "iarchive")
switches 0:0e018d759a2a 92 self.elf2bin = join(IAR_BIN, "ielftool")
switches 0:0e018d759a2a 93
switches 0:0e018d759a2a 94 def parse_dependencies(self, dep_path):
switches 0:0e018d759a2a 95 return [(self.CHROOT if self.CHROOT else '')+path.strip() for path in open(dep_path).readlines()
switches 0:0e018d759a2a 96 if (path and not path.isspace())]
switches 0:0e018d759a2a 97
switches 0:0e018d759a2a 98 def parse_output(self, output):
switches 0:0e018d759a2a 99 msg = None
switches 0:0e018d759a2a 100 for line in output.splitlines():
switches 0:0e018d759a2a 101 match = IAR.DIAGNOSTIC_PATTERN.match(line)
switches 0:0e018d759a2a 102 if match is not None:
switches 0:0e018d759a2a 103 if msg is not None:
switches 0:0e018d759a2a 104 self.cc_info(msg)
switches 0:0e018d759a2a 105 msg = None
switches 0:0e018d759a2a 106 msg = {
switches 0:0e018d759a2a 107 'severity': match.group('severity').lower(),
switches 0:0e018d759a2a 108 'file': match.group('file'),
switches 0:0e018d759a2a 109 'line': match.group('line'),
switches 0:0e018d759a2a 110 'col': 0,
switches 0:0e018d759a2a 111 'message': match.group('message'),
switches 0:0e018d759a2a 112 'text': '',
switches 0:0e018d759a2a 113 'target_name': self.target.name,
switches 0:0e018d759a2a 114 'toolchain_name': self.name
switches 0:0e018d759a2a 115 }
switches 0:0e018d759a2a 116 elif msg is not None:
switches 0:0e018d759a2a 117 # Determine the warning/error column by calculating the ^ position
switches 0:0e018d759a2a 118 match = IAR.INDEX_PATTERN.match(line)
switches 0:0e018d759a2a 119 if match is not None:
switches 0:0e018d759a2a 120 msg['col'] = len(match.group('col'))
switches 0:0e018d759a2a 121 self.cc_info(msg)
switches 0:0e018d759a2a 122 msg = None
switches 0:0e018d759a2a 123 else:
switches 0:0e018d759a2a 124 msg['text'] += line+"\n"
switches 0:0e018d759a2a 125
switches 0:0e018d759a2a 126 if msg is not None:
switches 0:0e018d759a2a 127 self.cc_info(msg)
switches 0:0e018d759a2a 128
switches 0:0e018d759a2a 129 def get_dep_option(self, object):
switches 0:0e018d759a2a 130 base, _ = splitext(object)
switches 0:0e018d759a2a 131 dep_path = base + '.d'
switches 0:0e018d759a2a 132 return ["--dependencies", dep_path]
switches 0:0e018d759a2a 133
switches 0:0e018d759a2a 134 def cc_extra(self, object):
switches 0:0e018d759a2a 135 base, _ = splitext(object)
switches 0:0e018d759a2a 136 return ["-l", base + '.s.txt']
switches 0:0e018d759a2a 137
switches 0:0e018d759a2a 138 def get_config_option(self, config_header):
switches 0:0e018d759a2a 139 return ['--preinclude=' + config_header]
switches 0:0e018d759a2a 140
switches 0:0e018d759a2a 141 def get_compile_options(self, defines, includes, for_asm=False):
switches 0:0e018d759a2a 142 opts = ['-D%s' % d for d in defines]
switches 0:0e018d759a2a 143 if self.RESPONSE_FILES:
switches 0:0e018d759a2a 144 opts += ['-f', self.get_inc_file(includes)]
switches 0:0e018d759a2a 145 else:
switches 0:0e018d759a2a 146 opts += ["-I%s" % i for i in includes]
switches 0:0e018d759a2a 147
switches 0:0e018d759a2a 148 if not for_asm:
switches 0:0e018d759a2a 149 config_header = self.get_config_header()
switches 0:0e018d759a2a 150 if config_header is not None:
switches 0:0e018d759a2a 151 opts = opts + self.get_config_option(config_header)
switches 0:0e018d759a2a 152 return opts
switches 0:0e018d759a2a 153
switches 0:0e018d759a2a 154 @hook_tool
switches 0:0e018d759a2a 155 def assemble(self, source, object, includes):
switches 0:0e018d759a2a 156 # Build assemble command
switches 0:0e018d759a2a 157 cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes, True) + ["-o", object, source]
switches 0:0e018d759a2a 158
switches 0:0e018d759a2a 159 # Call cmdline hook
switches 0:0e018d759a2a 160 cmd = self.hook.get_cmdline_assembler(cmd)
switches 0:0e018d759a2a 161
switches 0:0e018d759a2a 162 # Return command array, don't execute
switches 0:0e018d759a2a 163 return [cmd]
switches 0:0e018d759a2a 164
switches 0:0e018d759a2a 165 @hook_tool
switches 0:0e018d759a2a 166 def compile(self, cc, source, object, includes):
switches 0:0e018d759a2a 167 # Build compile command
switches 0:0e018d759a2a 168 cmd = cc + self.get_compile_options(self.get_symbols(), includes)
switches 0:0e018d759a2a 169
switches 0:0e018d759a2a 170 cmd.extend(self.get_dep_option(object))
switches 0:0e018d759a2a 171
switches 0:0e018d759a2a 172 cmd.extend(self.cc_extra(object))
switches 0:0e018d759a2a 173
switches 0:0e018d759a2a 174 cmd.extend(["-o", object, source])
switches 0:0e018d759a2a 175
switches 0:0e018d759a2a 176 # Call cmdline hook
switches 0:0e018d759a2a 177 cmd = self.hook.get_cmdline_compiler(cmd)
switches 0:0e018d759a2a 178
switches 0:0e018d759a2a 179 return [cmd]
switches 0:0e018d759a2a 180
switches 0:0e018d759a2a 181 def compile_c(self, source, object, includes):
switches 0:0e018d759a2a 182 return self.compile(self.cc, source, object, includes)
switches 0:0e018d759a2a 183
switches 0:0e018d759a2a 184 def compile_cpp(self, source, object, includes):
switches 0:0e018d759a2a 185 return self.compile(self.cppc, source, object, includes)
switches 0:0e018d759a2a 186
switches 0:0e018d759a2a 187 @hook_tool
switches 0:0e018d759a2a 188 def link(self, output, objects, libraries, lib_dirs, mem_map):
switches 0:0e018d759a2a 189 # Build linker command
switches 0:0e018d759a2a 190 map_file = splitext(output)[0] + ".map"
switches 0:0e018d759a2a 191 cmd = self.ld + [ "-o", output, "--map=%s" % map_file] + objects + libraries + self.flags['ld']
switches 0:0e018d759a2a 192
switches 0:0e018d759a2a 193 if mem_map:
switches 0:0e018d759a2a 194 cmd.extend(["--config", mem_map])
switches 0:0e018d759a2a 195
switches 0:0e018d759a2a 196 # Call cmdline hook
switches 0:0e018d759a2a 197 cmd = self.hook.get_cmdline_linker(cmd)
switches 0:0e018d759a2a 198
switches 0:0e018d759a2a 199 if self.RESPONSE_FILES:
switches 0:0e018d759a2a 200 # Split link command to linker executable + response file
switches 0:0e018d759a2a 201 cmd_linker = cmd[0]
switches 0:0e018d759a2a 202 link_files = self.get_link_file(cmd[1:])
switches 0:0e018d759a2a 203 cmd = [cmd_linker, '-f', link_files]
switches 0:0e018d759a2a 204
switches 0:0e018d759a2a 205 # Exec command
switches 0:0e018d759a2a 206 self.cc_verbose("Link: %s" % ' '.join(cmd))
switches 0:0e018d759a2a 207 self.default_cmd(cmd)
switches 0:0e018d759a2a 208
switches 0:0e018d759a2a 209 @hook_tool
switches 0:0e018d759a2a 210 def archive(self, objects, lib_path):
switches 0:0e018d759a2a 211 if self.RESPONSE_FILES:
switches 0:0e018d759a2a 212 param = ['-f', self.get_arch_file(objects)]
switches 0:0e018d759a2a 213 else:
switches 0:0e018d759a2a 214 param = objects
switches 0:0e018d759a2a 215
switches 0:0e018d759a2a 216 if exists(lib_path):
switches 0:0e018d759a2a 217 remove(lib_path)
switches 0:0e018d759a2a 218
switches 0:0e018d759a2a 219 self.default_cmd([self.ar, lib_path] + param)
switches 0:0e018d759a2a 220
switches 0:0e018d759a2a 221 @hook_tool
switches 0:0e018d759a2a 222 def binary(self, resources, elf, bin):
switches 0:0e018d759a2a 223 # Build binary command
switches 0:0e018d759a2a 224 cmd = [self.elf2bin, "--bin", elf, bin]
switches 0:0e018d759a2a 225
switches 0:0e018d759a2a 226 # Call cmdline hook
switches 0:0e018d759a2a 227 cmd = self.hook.get_cmdline_binary(cmd)
switches 0:0e018d759a2a 228
switches 0:0e018d759a2a 229 # Exec command
switches 0:0e018d759a2a 230 self.cc_verbose("FromELF: %s" % ' '.join(cmd))
switches 0:0e018d759a2a 231 self.default_cmd(cmd)