Clone of official tools

Committer:
screamer
Date:
Thu May 19 19:44:41 2016 +0100
Revision:
0:66f3b5499f7f
Child:
2:07730b0f452c
Initial revision

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.path import join, dirname, splitext, basename, exists
screamer 0:66f3b5499f7f 19
screamer 0:66f3b5499f7f 20 from tools.toolchains import mbedToolchain
screamer 0:66f3b5499f7f 21 from tools.settings import ARM_BIN, ARM_INC, ARM_LIB, MY_ARM_CLIB, ARM_CPPLIB, GOANNA_PATH
screamer 0:66f3b5499f7f 22 from tools.hooks import hook_tool
screamer 0:66f3b5499f7f 23 from tools.utils import mkdir
screamer 0:66f3b5499f7f 24
screamer 0:66f3b5499f7f 25 class ARM(mbedToolchain):
screamer 0:66f3b5499f7f 26 LINKER_EXT = '.sct'
screamer 0:66f3b5499f7f 27 LIBRARY_EXT = '.ar'
screamer 0:66f3b5499f7f 28
screamer 0:66f3b5499f7f 29 STD_LIB_NAME = "%s.ar"
screamer 0:66f3b5499f7f 30 DIAGNOSTIC_PATTERN = re.compile('"(?P<file>[^"]+)", line (?P<line>\d+)( \(column (?P<column>\d+)\)|): (?P<severity>Warning|Error): (?P<message>.+)')
screamer 0:66f3b5499f7f 31 DEP_PATTERN = re.compile('\S+:\s(?P<file>.+)\n')
screamer 0:66f3b5499f7f 32
screamer 0:66f3b5499f7f 33 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 0:66f3b5499f7f 34 mbedToolchain.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 0:66f3b5499f7f 35
screamer 0:66f3b5499f7f 36 if target.core == "Cortex-M0+":
screamer 0:66f3b5499f7f 37 cpu = "Cortex-M0"
screamer 0:66f3b5499f7f 38 elif target.core == "Cortex-M4F":
screamer 0:66f3b5499f7f 39 cpu = "Cortex-M4.fp"
screamer 0:66f3b5499f7f 40 elif target.core == "Cortex-M7F":
screamer 0:66f3b5499f7f 41 cpu = "Cortex-M7.fp.sp"
screamer 0:66f3b5499f7f 42 else:
screamer 0:66f3b5499f7f 43 cpu = target.core
screamer 0:66f3b5499f7f 44
screamer 0:66f3b5499f7f 45 main_cc = join(ARM_BIN, "armcc")
screamer 0:66f3b5499f7f 46 common = ["-c",
screamer 0:66f3b5499f7f 47 "--cpu=%s" % cpu, "--gnu",
screamer 0:66f3b5499f7f 48 "-Otime", "--split_sections", "--apcs=interwork",
screamer 0:66f3b5499f7f 49 "--brief_diagnostics", "--restrict", "--multibyte_chars"
screamer 0:66f3b5499f7f 50 ]
screamer 0:66f3b5499f7f 51
screamer 0:66f3b5499f7f 52 if "save-asm" in self.options:
screamer 0:66f3b5499f7f 53 common.extend(["--asm", "--interleave"])
screamer 0:66f3b5499f7f 54
screamer 0:66f3b5499f7f 55 if "debug-info" in self.options:
screamer 0:66f3b5499f7f 56 common.append("-O0")
screamer 0:66f3b5499f7f 57 else:
screamer 0:66f3b5499f7f 58 common.append("-O3")
screamer 0:66f3b5499f7f 59 # add debug symbols for all builds
screamer 0:66f3b5499f7f 60 common.append("-g")
screamer 0:66f3b5499f7f 61
screamer 0:66f3b5499f7f 62 common_c = [
screamer 0:66f3b5499f7f 63 "--md", "--no_depend_system_headers",
screamer 0:66f3b5499f7f 64 '-I%s' % ARM_INC
screamer 0:66f3b5499f7f 65 ]
screamer 0:66f3b5499f7f 66
screamer 0:66f3b5499f7f 67 self.asm = [main_cc] + common + ['-I%s' % ARM_INC]
screamer 0:66f3b5499f7f 68 if not "analyze" in self.options:
screamer 0:66f3b5499f7f 69 self.cc = [main_cc] + common + common_c + ["--c99"]
screamer 0:66f3b5499f7f 70 self.cppc = [main_cc] + common + common_c + ["--cpp", "--no_rtti"]
screamer 0:66f3b5499f7f 71 else:
screamer 0:66f3b5499f7f 72 self.cc = [join(GOANNA_PATH, "goannacc"), "--with-cc=" + main_cc.replace('\\', '/'), "--dialect=armcc", '--output-format="%s"' % self.GOANNA_FORMAT] + common + common_c + ["--c99"]
screamer 0:66f3b5499f7f 73 self.cppc= [join(GOANNA_PATH, "goannac++"), "--with-cxx=" + main_cc.replace('\\', '/'), "--dialect=armcc", '--output-format="%s"' % self.GOANNA_FORMAT] + common + common_c + ["--cpp", "--no_rtti"]
screamer 0:66f3b5499f7f 74
screamer 0:66f3b5499f7f 75 self.ld = [join(ARM_BIN, "armlink")]
screamer 0:66f3b5499f7f 76 self.sys_libs = []
screamer 0:66f3b5499f7f 77
screamer 0:66f3b5499f7f 78 self.ar = join(ARM_BIN, "armar")
screamer 0:66f3b5499f7f 79 self.elf2bin = join(ARM_BIN, "fromelf")
screamer 0:66f3b5499f7f 80
screamer 0:66f3b5499f7f 81 def parse_dependencies(self, dep_path):
screamer 0:66f3b5499f7f 82 dependencies = []
screamer 0:66f3b5499f7f 83 for line in open(dep_path).readlines():
screamer 0:66f3b5499f7f 84 match = ARM.DEP_PATTERN.match(line)
screamer 0:66f3b5499f7f 85 if match is not None:
screamer 0:66f3b5499f7f 86 dependencies.append(match.group('file'))
screamer 0:66f3b5499f7f 87 return dependencies
screamer 0:66f3b5499f7f 88
screamer 0:66f3b5499f7f 89 def parse_output(self, output):
screamer 0:66f3b5499f7f 90 for line in output.splitlines():
screamer 0:66f3b5499f7f 91 match = ARM.DIAGNOSTIC_PATTERN.match(line)
screamer 0:66f3b5499f7f 92 if match is not None:
screamer 0:66f3b5499f7f 93 self.cc_info(
screamer 0:66f3b5499f7f 94 match.group('severity').lower(),
screamer 0:66f3b5499f7f 95 match.group('file'),
screamer 0:66f3b5499f7f 96 match.group('line'),
screamer 0:66f3b5499f7f 97 match.group('message'),
screamer 0:66f3b5499f7f 98 target_name=self.target.name,
screamer 0:66f3b5499f7f 99 toolchain_name=self.name
screamer 0:66f3b5499f7f 100 )
screamer 0:66f3b5499f7f 101 match = self.goanna_parse_line(line)
screamer 0:66f3b5499f7f 102 if match is not None:
screamer 0:66f3b5499f7f 103 self.cc_info(
screamer 0:66f3b5499f7f 104 match.group('severity').lower(),
screamer 0:66f3b5499f7f 105 match.group('file'),
screamer 0:66f3b5499f7f 106 match.group('line'),
screamer 0:66f3b5499f7f 107 match.group('message')
screamer 0:66f3b5499f7f 108 )
screamer 0:66f3b5499f7f 109
screamer 0:66f3b5499f7f 110 def get_dep_option(self, object):
screamer 0:66f3b5499f7f 111 base, _ = splitext(object)
screamer 0:66f3b5499f7f 112 dep_path = base + '.d'
screamer 0:66f3b5499f7f 113 return ["--depend", dep_path]
screamer 0:66f3b5499f7f 114
screamer 0:66f3b5499f7f 115 def get_compile_options(self, defines, includes):
screamer 0:66f3b5499f7f 116 return ['-D%s' % d for d in defines] + ['--via', self.get_inc_file(includes)]
screamer 0:66f3b5499f7f 117
screamer 0:66f3b5499f7f 118 @hook_tool
screamer 0:66f3b5499f7f 119 def assemble(self, source, object, includes):
screamer 0:66f3b5499f7f 120 # Preprocess first, then assemble
screamer 0:66f3b5499f7f 121 dir = join(dirname(object), '.temp')
screamer 0:66f3b5499f7f 122 mkdir(dir)
screamer 0:66f3b5499f7f 123 tempfile = join(dir, basename(object) + '.E.s')
screamer 0:66f3b5499f7f 124
screamer 0:66f3b5499f7f 125 # Build preprocess assemble command
screamer 0:66f3b5499f7f 126 cmd_pre = self.asm + self.get_compile_options(self.get_symbols(), includes) + ["-E", "-o", tempfile, source]
screamer 0:66f3b5499f7f 127
screamer 0:66f3b5499f7f 128 # Build main assemble command
screamer 0:66f3b5499f7f 129 cmd = self.asm + ["-o", object, tempfile]
screamer 0:66f3b5499f7f 130
screamer 0:66f3b5499f7f 131 # Call cmdline hook
screamer 0:66f3b5499f7f 132 cmd_pre = self.hook.get_cmdline_assembler(cmd_pre)
screamer 0:66f3b5499f7f 133 cmd = self.hook.get_cmdline_assembler(cmd)
screamer 0:66f3b5499f7f 134
screamer 0:66f3b5499f7f 135 # Return command array, don't execute
screamer 0:66f3b5499f7f 136 return [cmd_pre, cmd]
screamer 0:66f3b5499f7f 137
screamer 0:66f3b5499f7f 138 @hook_tool
screamer 0:66f3b5499f7f 139 def compile(self, cc, source, object, includes):
screamer 0:66f3b5499f7f 140 # Build compile command
screamer 0:66f3b5499f7f 141 cmd = cc + self.get_compile_options(self.get_symbols(), includes)
screamer 0:66f3b5499f7f 142
screamer 0:66f3b5499f7f 143 cmd.extend(self.get_dep_option(object))
screamer 0:66f3b5499f7f 144
screamer 0:66f3b5499f7f 145 cmd.extend(["-o", object, source])
screamer 0:66f3b5499f7f 146
screamer 0:66f3b5499f7f 147 # Call cmdline hook
screamer 0:66f3b5499f7f 148 cmd = self.hook.get_cmdline_compiler(cmd)
screamer 0:66f3b5499f7f 149
screamer 0:66f3b5499f7f 150 return [cmd]
screamer 0:66f3b5499f7f 151
screamer 0:66f3b5499f7f 152 def compile_c(self, source, object, includes):
screamer 0:66f3b5499f7f 153 return self.compile(self.cc, source, object, includes)
screamer 0:66f3b5499f7f 154
screamer 0:66f3b5499f7f 155 def compile_cpp(self, source, object, includes):
screamer 0:66f3b5499f7f 156 return self.compile(self.cppc, source, object, includes)
screamer 0:66f3b5499f7f 157
screamer 0:66f3b5499f7f 158 @hook_tool
screamer 0:66f3b5499f7f 159 def link(self, output, objects, libraries, lib_dirs, mem_map):
screamer 0:66f3b5499f7f 160 if len(lib_dirs):
screamer 0:66f3b5499f7f 161 args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt"]
screamer 0:66f3b5499f7f 162 else:
screamer 0:66f3b5499f7f 163 args = ["-o", output, "--info=totals", "--list=.link_totals.txt"]
screamer 0:66f3b5499f7f 164
screamer 0:66f3b5499f7f 165 if mem_map:
screamer 0:66f3b5499f7f 166 args.extend(["--scatter", mem_map])
screamer 0:66f3b5499f7f 167
screamer 0:66f3b5499f7f 168 # Build linker command
screamer 0:66f3b5499f7f 169 cmd = self.ld + args + objects + libraries + self.sys_libs
screamer 0:66f3b5499f7f 170
screamer 0:66f3b5499f7f 171 # Call cmdline hook
screamer 0:66f3b5499f7f 172 cmd = self.hook.get_cmdline_linker(cmd)
screamer 0:66f3b5499f7f 173
screamer 0:66f3b5499f7f 174 # Split link command to linker executable + response file
screamer 0:66f3b5499f7f 175 link_files = join(dirname(output), ".link_files.txt")
screamer 0:66f3b5499f7f 176 with open(link_files, "wb") as f:
screamer 0:66f3b5499f7f 177 cmd_linker = cmd[0]
screamer 0:66f3b5499f7f 178 cmd_list = []
screamer 0:66f3b5499f7f 179 for c in cmd[1:]:
screamer 0:66f3b5499f7f 180 if c:
screamer 0:66f3b5499f7f 181 cmd_list.append(('"%s"' % c) if not c.startswith('-') else c)
screamer 0:66f3b5499f7f 182 string = " ".join(cmd_list).replace("\\", "/")
screamer 0:66f3b5499f7f 183 f.write(string)
screamer 0:66f3b5499f7f 184
screamer 0:66f3b5499f7f 185 # Exec command
screamer 0:66f3b5499f7f 186 self.default_cmd([cmd_linker, '--via', link_files])
screamer 0:66f3b5499f7f 187
screamer 0:66f3b5499f7f 188 @hook_tool
screamer 0:66f3b5499f7f 189 def archive(self, objects, lib_path):
screamer 0:66f3b5499f7f 190 archive_files = join(dirname(lib_path), ".archive_files.txt")
screamer 0:66f3b5499f7f 191 with open(archive_files, "wb") as f:
screamer 0:66f3b5499f7f 192 o_list = []
screamer 0:66f3b5499f7f 193 for o in objects:
screamer 0:66f3b5499f7f 194 o_list.append('"%s"' % o)
screamer 0:66f3b5499f7f 195 string = " ".join(o_list).replace("\\", "/")
screamer 0:66f3b5499f7f 196 f.write(string)
screamer 0:66f3b5499f7f 197
screamer 0:66f3b5499f7f 198 # Exec command
screamer 0:66f3b5499f7f 199 self.default_cmd([self.ar, '-r', lib_path, '--via', archive_files])
screamer 0:66f3b5499f7f 200
screamer 0:66f3b5499f7f 201 @hook_tool
screamer 0:66f3b5499f7f 202 def binary(self, resources, elf, bin):
screamer 0:66f3b5499f7f 203 # Build binary command
screamer 0:66f3b5499f7f 204 cmd = [self.elf2bin, '--bin', '-o', bin, elf]
screamer 0:66f3b5499f7f 205
screamer 0:66f3b5499f7f 206 # Call cmdline hook
screamer 0:66f3b5499f7f 207 cmd = self.hook.get_cmdline_binary(cmd)
screamer 0:66f3b5499f7f 208
screamer 0:66f3b5499f7f 209 # Exec command
screamer 0:66f3b5499f7f 210 self.default_cmd(cmd)
screamer 0:66f3b5499f7f 211
screamer 0:66f3b5499f7f 212
screamer 0:66f3b5499f7f 213 class ARM_STD(ARM):
screamer 0:66f3b5499f7f 214 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 0:66f3b5499f7f 215 ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 0:66f3b5499f7f 216 self.cc += ["-D__ASSERT_MSG"]
screamer 0:66f3b5499f7f 217 self.cppc += ["-D__ASSERT_MSG"]
screamer 0:66f3b5499f7f 218 self.ld.extend(["--libpath", ARM_LIB])
screamer 0:66f3b5499f7f 219
screamer 0:66f3b5499f7f 220
screamer 0:66f3b5499f7f 221 class ARM_MICRO(ARM):
screamer 0:66f3b5499f7f 222 PATCHED_LIBRARY = False
screamer 0:66f3b5499f7f 223
screamer 0:66f3b5499f7f 224 def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
screamer 0:66f3b5499f7f 225 ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
screamer 0:66f3b5499f7f 226
screamer 0:66f3b5499f7f 227 # Compiler
screamer 0:66f3b5499f7f 228 self.asm += ["-D__MICROLIB"]
screamer 0:66f3b5499f7f 229 self.cc += ["--library_type=microlib", "-D__MICROLIB", "-D__ASSERT_MSG"]
screamer 0:66f3b5499f7f 230 self.cppc += ["--library_type=microlib", "-D__MICROLIB", "-D__ASSERT_MSG"]
screamer 0:66f3b5499f7f 231
screamer 0:66f3b5499f7f 232 # Linker
screamer 0:66f3b5499f7f 233 self.ld.append("--library_type=microlib")
screamer 0:66f3b5499f7f 234
screamer 0:66f3b5499f7f 235 # We had to patch microlib to add C++ support
screamer 0:66f3b5499f7f 236 # In later releases this patch should have entered mainline
screamer 0:66f3b5499f7f 237 if ARM_MICRO.PATCHED_LIBRARY:
screamer 0:66f3b5499f7f 238 self.ld.append("--noscanlib")
screamer 0:66f3b5499f7f 239
screamer 0:66f3b5499f7f 240 # System Libraries
screamer 0:66f3b5499f7f 241 self.sys_libs.extend([join(MY_ARM_CLIB, lib+".l") for lib in ["mc_p", "mf_p", "m_ps"]])
screamer 0:66f3b5499f7f 242
screamer 0:66f3b5499f7f 243 if target.core == "Cortex-M3":
screamer 0:66f3b5499f7f 244 self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ws", "cpprt_w"]])
screamer 0:66f3b5499f7f 245
screamer 0:66f3b5499f7f 246 elif target.core in ["Cortex-M0", "Cortex-M0+"]:
screamer 0:66f3b5499f7f 247 self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ps", "cpprt_p"]])
screamer 0:66f3b5499f7f 248 else:
screamer 0:66f3b5499f7f 249 self.ld.extend(["--libpath", ARM_LIB])