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.path import join, basename, splitext
switches 0:0e018d759a2a 19
switches 0:0e018d759a2a 20 from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS
switches 0:0e018d759a2a 21 from tools.hooks import hook_tool
switches 0:0e018d759a2a 22
switches 0:0e018d759a2a 23 class GCC(mbedToolchain):
switches 0:0e018d759a2a 24 LINKER_EXT = '.ld'
switches 0:0e018d759a2a 25 LIBRARY_EXT = '.a'
switches 0:0e018d759a2a 26
switches 0:0e018d759a2a 27 STD_LIB_NAME = "lib%s.a"
switches 0:0e018d759a2a 28 DIAGNOSTIC_PATTERN = re.compile('((?P<file>[^:]+):(?P<line>\d+):)(\d+:)? (?P<severity>warning|error): (?P<message>.+)')
switches 0:0e018d759a2a 29 INDEX_PATTERN = re.compile('(?P<col>\s*)\^')
switches 0:0e018d759a2a 30
switches 0:0e018d759a2a 31 def __init__(self, target, notify=None, macros=None,
switches 0:0e018d759a2a 32 silent=False, tool_path="", extra_verbose=False,
switches 0:0e018d759a2a 33 build_profile=None):
switches 0:0e018d759a2a 34 mbedToolchain.__init__(self, target, notify, macros, silent,
switches 0:0e018d759a2a 35 extra_verbose=extra_verbose,
switches 0:0e018d759a2a 36 build_profile=build_profile)
switches 0:0e018d759a2a 37
switches 0:0e018d759a2a 38 # Add flags for current size setting
switches 0:0e018d759a2a 39 default_lib = "std"
switches 0:0e018d759a2a 40 if hasattr(target, "default_lib"):
switches 0:0e018d759a2a 41 default_lib = target.default_lib
switches 0:0e018d759a2a 42 elif hasattr(target, "default_build"): # Legacy
switches 0:0e018d759a2a 43 default_lib = target.default_build
switches 0:0e018d759a2a 44
switches 0:0e018d759a2a 45 if default_lib == "small":
switches 0:0e018d759a2a 46 self.flags["common"].append("-DMBED_RTOS_SINGLE_THREAD")
switches 0:0e018d759a2a 47 self.flags["ld"].append("--specs=nano.specs")
switches 0:0e018d759a2a 48
switches 0:0e018d759a2a 49 if target.core == "Cortex-M0+":
switches 0:0e018d759a2a 50 cpu = "cortex-m0plus"
switches 0:0e018d759a2a 51 elif target.core == "Cortex-M4F":
switches 0:0e018d759a2a 52 cpu = "cortex-m4"
switches 0:0e018d759a2a 53 elif target.core == "Cortex-M7F":
switches 0:0e018d759a2a 54 cpu = "cortex-m7"
switches 0:0e018d759a2a 55 elif target.core == "Cortex-M7FD":
switches 0:0e018d759a2a 56 cpu = "cortex-m7"
switches 0:0e018d759a2a 57 else:
switches 0:0e018d759a2a 58 cpu = target.core.lower()
switches 0:0e018d759a2a 59
switches 0:0e018d759a2a 60 self.cpu = ["-mcpu=%s" % cpu]
switches 0:0e018d759a2a 61 if target.core.startswith("Cortex"):
switches 0:0e018d759a2a 62 self.cpu.append("-mthumb")
switches 0:0e018d759a2a 63
switches 0:0e018d759a2a 64 # FPU handling, M7 possibly to have double FPU
switches 0:0e018d759a2a 65 if target.core == "Cortex-M4F":
switches 0:0e018d759a2a 66 self.cpu.append("-mfpu=fpv4-sp-d16")
switches 0:0e018d759a2a 67 self.cpu.append("-mfloat-abi=softfp")
switches 0:0e018d759a2a 68 elif target.core == "Cortex-M7F":
switches 0:0e018d759a2a 69 self.cpu.append("-mfpu=fpv5-sp-d16")
switches 0:0e018d759a2a 70 self.cpu.append("-mfloat-abi=softfp")
switches 0:0e018d759a2a 71 elif target.core == "Cortex-M7FD":
switches 0:0e018d759a2a 72 self.cpu.append("-mfpu=fpv5-d16")
switches 0:0e018d759a2a 73 self.cpu.append("-mfloat-abi=softfp")
switches 0:0e018d759a2a 74
switches 0:0e018d759a2a 75 if target.core == "Cortex-A9":
switches 0:0e018d759a2a 76 self.cpu.append("-mthumb-interwork")
switches 0:0e018d759a2a 77 self.cpu.append("-marm")
switches 0:0e018d759a2a 78 self.cpu.append("-march=armv7-a")
switches 0:0e018d759a2a 79 self.cpu.append("-mfpu=vfpv3")
switches 0:0e018d759a2a 80 self.cpu.append("-mfloat-abi=hard")
switches 0:0e018d759a2a 81 self.cpu.append("-mno-unaligned-access")
switches 0:0e018d759a2a 82
switches 0:0e018d759a2a 83 self.flags["common"] += self.cpu
switches 0:0e018d759a2a 84
switches 0:0e018d759a2a 85 main_cc = join(tool_path, "arm-none-eabi-gcc")
switches 0:0e018d759a2a 86 main_cppc = join(tool_path, "arm-none-eabi-g++")
switches 0:0e018d759a2a 87 self.asm = [main_cc] + self.flags['asm'] + self.flags["common"]
switches 0:0e018d759a2a 88 self.cc = [main_cc]
switches 0:0e018d759a2a 89 self.cppc =[main_cppc]
switches 0:0e018d759a2a 90 self.cc += self.flags['c'] + self.flags['common']
switches 0:0e018d759a2a 91 self.cppc += self.flags['cxx'] + self.flags['common']
switches 0:0e018d759a2a 92
switches 0:0e018d759a2a 93 self.flags['ld'] += self.cpu
switches 0:0e018d759a2a 94 self.ld = [join(tool_path, "arm-none-eabi-gcc")] + self.flags['ld']
switches 0:0e018d759a2a 95 self.sys_libs = ["stdc++", "supc++", "m", "c", "gcc"]
switches 0:0e018d759a2a 96
switches 0:0e018d759a2a 97 self.ar = join(tool_path, "arm-none-eabi-ar")
switches 0:0e018d759a2a 98 self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")
switches 0:0e018d759a2a 99
switches 0:0e018d759a2a 100 def parse_dependencies(self, dep_path):
switches 0:0e018d759a2a 101 dependencies = []
switches 0:0e018d759a2a 102 buff = open(dep_path).readlines()
switches 0:0e018d759a2a 103 buff[0] = re.sub('^(.*?)\: ', '', buff[0])
switches 0:0e018d759a2a 104 for line in buff:
switches 0:0e018d759a2a 105 file = line.replace('\\\n', '').strip()
switches 0:0e018d759a2a 106 if file:
switches 0:0e018d759a2a 107 # GCC might list more than one dependency on a single line, in this case
switches 0:0e018d759a2a 108 # the dependencies are separated by a space. However, a space might also
switches 0:0e018d759a2a 109 # indicate an actual space character in a dependency path, but in this case
switches 0:0e018d759a2a 110 # the space character is prefixed by a backslash.
switches 0:0e018d759a2a 111 # Temporary replace all '\ ' with a special char that is not used (\a in this
switches 0:0e018d759a2a 112 # case) to keep them from being interpreted by 'split' (they will be converted
switches 0:0e018d759a2a 113 # back later to a space char)
switches 0:0e018d759a2a 114 file = file.replace('\\ ', '\a')
switches 0:0e018d759a2a 115 if file.find(" ") == -1:
switches 0:0e018d759a2a 116 dependencies.append((self.CHROOT if self.CHROOT else '') + file.replace('\a', ' '))
switches 0:0e018d759a2a 117 else:
switches 0:0e018d759a2a 118 dependencies = dependencies + [(self.CHROOT if self.CHROOT else '') + f.replace('\a', ' ') for f in file.split(" ")]
switches 0:0e018d759a2a 119 return dependencies
switches 0:0e018d759a2a 120
switches 0:0e018d759a2a 121 def is_not_supported_error(self, output):
switches 0:0e018d759a2a 122 return "error: #error [NOT_SUPPORTED]" in output
switches 0:0e018d759a2a 123
switches 0:0e018d759a2a 124 def parse_output(self, output):
switches 0:0e018d759a2a 125 # The warning/error notification is multiline
switches 0:0e018d759a2a 126 msg = None
switches 0:0e018d759a2a 127 for line in output.splitlines():
switches 0:0e018d759a2a 128 match = GCC.DIAGNOSTIC_PATTERN.search(line)
switches 0:0e018d759a2a 129 if match is not None:
switches 0:0e018d759a2a 130 if msg is not None:
switches 0:0e018d759a2a 131 self.cc_info(msg)
switches 0:0e018d759a2a 132 msg = None
switches 0:0e018d759a2a 133 msg = {
switches 0:0e018d759a2a 134 'severity': match.group('severity').lower(),
switches 0:0e018d759a2a 135 'file': match.group('file'),
switches 0:0e018d759a2a 136 'line': match.group('line'),
switches 0:0e018d759a2a 137 'col': 0,
switches 0:0e018d759a2a 138 'message': match.group('message'),
switches 0:0e018d759a2a 139 'text': '',
switches 0:0e018d759a2a 140 'target_name': self.target.name,
switches 0:0e018d759a2a 141 'toolchain_name': self.name
switches 0:0e018d759a2a 142 }
switches 0:0e018d759a2a 143 elif msg is not None:
switches 0:0e018d759a2a 144 # Determine the warning/error column by calculating the ^ position
switches 0:0e018d759a2a 145 match = GCC.INDEX_PATTERN.match(line)
switches 0:0e018d759a2a 146 if match is not None:
switches 0:0e018d759a2a 147 msg['col'] = len(match.group('col'))
switches 0:0e018d759a2a 148 self.cc_info(msg)
switches 0:0e018d759a2a 149 msg = None
switches 0:0e018d759a2a 150 else:
switches 0:0e018d759a2a 151 msg['text'] += line+"\n"
switches 0:0e018d759a2a 152
switches 0:0e018d759a2a 153 if msg is not None:
switches 0:0e018d759a2a 154 self.cc_info(msg)
switches 0:0e018d759a2a 155
switches 0:0e018d759a2a 156 def get_dep_option(self, object):
switches 0:0e018d759a2a 157 base, _ = splitext(object)
switches 0:0e018d759a2a 158 dep_path = base + '.d'
switches 0:0e018d759a2a 159 return ["-MD", "-MF", dep_path]
switches 0:0e018d759a2a 160
switches 0:0e018d759a2a 161 def get_config_option(self, config_header):
switches 0:0e018d759a2a 162 return ['-include', config_header]
switches 0:0e018d759a2a 163
switches 0:0e018d759a2a 164 def get_compile_options(self, defines, includes, for_asm=False):
switches 0:0e018d759a2a 165 opts = ['-D%s' % d for d in defines]
switches 0:0e018d759a2a 166 if self.RESPONSE_FILES:
switches 0:0e018d759a2a 167 opts += ['@%s' % self.get_inc_file(includes)]
switches 0:0e018d759a2a 168 else:
switches 0:0e018d759a2a 169 opts += ["-I%s" % i for i in includes]
switches 0:0e018d759a2a 170
switches 0:0e018d759a2a 171 if not for_asm:
switches 0:0e018d759a2a 172 config_header = self.get_config_header()
switches 0:0e018d759a2a 173 if config_header is not None:
switches 0:0e018d759a2a 174 opts = opts + self.get_config_option(config_header)
switches 0:0e018d759a2a 175 return opts
switches 0:0e018d759a2a 176
switches 0:0e018d759a2a 177 @hook_tool
switches 0:0e018d759a2a 178 def assemble(self, source, object, includes):
switches 0:0e018d759a2a 179 # Build assemble command
switches 0:0e018d759a2a 180 cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes) + ["-o", object, source]
switches 0:0e018d759a2a 181
switches 0:0e018d759a2a 182 # Call cmdline hook
switches 0:0e018d759a2a 183 cmd = self.hook.get_cmdline_assembler(cmd)
switches 0:0e018d759a2a 184
switches 0:0e018d759a2a 185 # Return command array, don't execute
switches 0:0e018d759a2a 186 return [cmd]
switches 0:0e018d759a2a 187
switches 0:0e018d759a2a 188 @hook_tool
switches 0:0e018d759a2a 189 def compile(self, cc, source, object, includes):
switches 0:0e018d759a2a 190 # Build compile command
switches 0:0e018d759a2a 191 cmd = cc + self.get_compile_options(self.get_symbols(), includes)
switches 0:0e018d759a2a 192
switches 0:0e018d759a2a 193 cmd.extend(self.get_dep_option(object))
switches 0:0e018d759a2a 194
switches 0:0e018d759a2a 195 cmd.extend(["-o", object, source])
switches 0:0e018d759a2a 196
switches 0:0e018d759a2a 197 # Call cmdline hook
switches 0:0e018d759a2a 198 cmd = self.hook.get_cmdline_compiler(cmd)
switches 0:0e018d759a2a 199
switches 0:0e018d759a2a 200 return [cmd]
switches 0:0e018d759a2a 201
switches 0:0e018d759a2a 202 def compile_c(self, source, object, includes):
switches 0:0e018d759a2a 203 return self.compile(self.cc, source, object, includes)
switches 0:0e018d759a2a 204
switches 0:0e018d759a2a 205 def compile_cpp(self, source, object, includes):
switches 0:0e018d759a2a 206 return self.compile(self.cppc, source, object, includes)
switches 0:0e018d759a2a 207
switches 0:0e018d759a2a 208 @hook_tool
switches 0:0e018d759a2a 209 def link(self, output, objects, libraries, lib_dirs, mem_map):
switches 0:0e018d759a2a 210 libs = []
switches 0:0e018d759a2a 211 for l in libraries:
switches 0:0e018d759a2a 212 name, _ = splitext(basename(l))
switches 0:0e018d759a2a 213 libs.append("-l%s" % name[3:])
switches 0:0e018d759a2a 214 libs.extend(["-l%s" % l for l in self.sys_libs])
switches 0:0e018d759a2a 215
switches 0:0e018d759a2a 216 # Build linker command
switches 0:0e018d759a2a 217 map_file = splitext(output)[0] + ".map"
switches 0:0e018d759a2a 218 cmd = self.ld + ["-o", output, "-Wl,-Map=%s" % map_file] + objects + ["-Wl,--start-group"] + libs + ["-Wl,--end-group"]
switches 0:0e018d759a2a 219 if mem_map:
switches 0:0e018d759a2a 220 cmd.extend(['-T', mem_map])
switches 0:0e018d759a2a 221
switches 0:0e018d759a2a 222 for L in lib_dirs:
switches 0:0e018d759a2a 223 cmd.extend(['-L', L])
switches 0:0e018d759a2a 224 cmd.extend(libs)
switches 0:0e018d759a2a 225
switches 0:0e018d759a2a 226 # Call cmdline hook
switches 0:0e018d759a2a 227 cmd = self.hook.get_cmdline_linker(cmd)
switches 0:0e018d759a2a 228
switches 0:0e018d759a2a 229 if self.RESPONSE_FILES:
switches 0:0e018d759a2a 230 # Split link command to linker executable + response file
switches 0:0e018d759a2a 231 cmd_linker = cmd[0]
switches 0:0e018d759a2a 232 link_files = self.get_link_file(cmd[1:])
switches 0:0e018d759a2a 233 cmd = [cmd_linker, "@%s" % link_files]
switches 0:0e018d759a2a 234
switches 0:0e018d759a2a 235 # Exec command
switches 0:0e018d759a2a 236 self.cc_verbose("Link: %s" % ' '.join(cmd))
switches 0:0e018d759a2a 237 self.default_cmd(cmd)
switches 0:0e018d759a2a 238
switches 0:0e018d759a2a 239 @hook_tool
switches 0:0e018d759a2a 240 def archive(self, objects, lib_path):
switches 0:0e018d759a2a 241 if self.RESPONSE_FILES:
switches 0:0e018d759a2a 242 param = ["@%s" % self.get_arch_file(objects)]
switches 0:0e018d759a2a 243 else:
switches 0:0e018d759a2a 244 param = objects
switches 0:0e018d759a2a 245
switches 0:0e018d759a2a 246 # Exec command
switches 0:0e018d759a2a 247 self.default_cmd([self.ar, 'rcs', lib_path] + param)
switches 0:0e018d759a2a 248
switches 0:0e018d759a2a 249 @hook_tool
switches 0:0e018d759a2a 250 def binary(self, resources, elf, bin):
switches 0:0e018d759a2a 251 # Build binary command
switches 0:0e018d759a2a 252 cmd = [self.elf2bin, "-O", "binary", elf, bin]
switches 0:0e018d759a2a 253
switches 0:0e018d759a2a 254 # Call cmdline hook
switches 0:0e018d759a2a 255 cmd = self.hook.get_cmdline_binary(cmd)
switches 0:0e018d759a2a 256
switches 0:0e018d759a2a 257 # Exec command
switches 0:0e018d759a2a 258 self.cc_verbose("FromELF: %s" % ' '.join(cmd))
switches 0:0e018d759a2a 259 self.default_cmd(cmd)
switches 0:0e018d759a2a 260
switches 0:0e018d759a2a 261
switches 0:0e018d759a2a 262 class GCC_ARM(GCC):
switches 0:0e018d759a2a 263 @staticmethod
switches 0:0e018d759a2a 264 def check_executable():
switches 0:0e018d759a2a 265 """Returns True if the executable (arm-none-eabi-gcc) location
switches 0:0e018d759a2a 266 specified by the user exists OR the executable can be found on the PATH.
switches 0:0e018d759a2a 267 Returns False otherwise."""
switches 0:0e018d759a2a 268 return mbedToolchain.generic_check_executable("GCC_ARM", 'arm-none-eabi-gcc', 1)
switches 0:0e018d759a2a 269
switches 0:0e018d759a2a 270 def __init__(self, target, notify=None, macros=None,
switches 0:0e018d759a2a 271 silent=False, extra_verbose=False, build_profile=None):
switches 0:0e018d759a2a 272 GCC.__init__(self, target, notify, macros, silent,
switches 0:0e018d759a2a 273 TOOLCHAIN_PATHS['GCC_ARM'], extra_verbose=extra_verbose,
switches 0:0e018d759a2a 274 build_profile=build_profile)
switches 0:0e018d759a2a 275
switches 0:0e018d759a2a 276 self.sys_libs.append("nosys")
switches 0:0e018d759a2a 277
switches 0:0e018d759a2a 278
switches 0:0e018d759a2a 279 class GCC_CR(GCC):
switches 0:0e018d759a2a 280 @staticmethod
switches 0:0e018d759a2a 281 def check_executable():
switches 0:0e018d759a2a 282 """Returns True if the executable (arm-none-eabi-gcc) location
switches 0:0e018d759a2a 283 specified by the user exists OR the executable can be found on the PATH.
switches 0:0e018d759a2a 284 Returns False otherwise."""
switches 0:0e018d759a2a 285 return mbedToolchain.generic_check_executable("GCC_CR", 'arm-none-eabi-gcc', 1)
switches 0:0e018d759a2a 286
switches 0:0e018d759a2a 287 def __init__(self, target, notify=None, macros=None,
switches 0:0e018d759a2a 288 silent=False, extra_verbose=False, build_profile=None):
switches 0:0e018d759a2a 289 GCC.__init__(self, target, notify, macros, silent,
switches 0:0e018d759a2a 290 TOOLCHAIN_PATHS['GCC_CR'], extra_verbose=extra_verbose,
switches 0:0e018d759a2a 291 build_profile=build_profile)
switches 0:0e018d759a2a 292
switches 0:0e018d759a2a 293 additional_compiler_flags = [
switches 0:0e018d759a2a 294 "-D__NEWLIB__", "-D__CODE_RED", "-D__USE_CMSIS", "-DCPP_USE_HEAP",
switches 0:0e018d759a2a 295 ]
switches 0:0e018d759a2a 296 self.cc += additional_compiler_flags
switches 0:0e018d759a2a 297 self.cppc += additional_compiler_flags
switches 0:0e018d759a2a 298
switches 0:0e018d759a2a 299 # Use latest gcc nanolib
switches 0:0e018d759a2a 300 self.ld.append("--specs=nano.specs")
switches 0:0e018d759a2a 301 if target.name in ["LPC1768", "LPC4088", "LPC4088_DM", "LPC4330", "UBLOX_C027", "LPC2368"]:
switches 0:0e018d759a2a 302 self.ld.extend(["-u _printf_float", "-u _scanf_float"])
switches 0:0e018d759a2a 303 self.ld += ["-nostdlib"]