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