Clone of official tools

Committer:
screamer
Date:
Fri Jul 15 15:28:09 2016 +0100
Revision:
20:835f6355470d
Parent:
13:ab47a20b66f0
Child:
21:4fdf0dd04f6f
Build system related fixes

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