Brian Daniels / mbed-tools

Fork of mbed-tools by Morpheus

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm.py Source File

arm.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2013 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 import re
00018 from os.path import join
00019 
00020 from tools.toolchains import mbedToolchain
00021 from tools.settings import ARM_BIN, ARM_INC, ARM_LIB, MY_ARM_CLIB, ARM_CPPLIB
00022 from tools.hooks import hook_tool
00023 from tools.settings import GOANNA_PATH
00024 
00025 class ARM(mbedToolchain):
00026     LINKER_EXT = '.sct'
00027     LIBRARY_EXT = '.ar'
00028 
00029     STD_LIB_NAME = "%s.ar"
00030     DIAGNOSTIC_PATTERN  = re.compile('"(?P<file>[^"]+)", line (?P<line>\d+)( \(column (?P<column>\d+)\)|): (?P<severity>Warning|Error): (?P<message>.+)')
00031     DEP_PATTERN = re.compile('\S+:\s(?P<file>.+)\n')
00032 
00033     def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
00034         mbedToolchain.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
00035 
00036         if target.core == "Cortex-M0+":
00037             cpu = "Cortex-M0"
00038         elif target.core == "Cortex-M4F":
00039             cpu = "Cortex-M4.fp"
00040         elif target.core == "Cortex-M7F":
00041             cpu = "Cortex-M7.fp.sp"
00042         else:
00043             cpu = target.core
00044 
00045         main_cc = join(ARM_BIN, "armcc")
00046         common = ["-c",
00047             "--cpu=%s" % cpu, "--gnu",
00048             "-Otime", "--split_sections", "--apcs=interwork",
00049             "--brief_diagnostics", "--restrict", "--multibyte_chars"
00050         ]
00051 
00052         if "save-asm" in self.options:
00053             common.extend(["--asm", "--interleave"])
00054 
00055         if "debug-info" in self.options:
00056             common.append("-g")
00057             common.append("-O0")
00058         else:
00059             common.append("-O3")
00060 
00061         common_c = [
00062             "--md", "--no_depend_system_headers",
00063             '-I%s' % ARM_INC
00064         ]
00065 
00066         self.asm = [main_cc] + common + ['-I%s' % ARM_INC]
00067         if not "analyze" in self.options:
00068             self.cc = [main_cc] + common + common_c + ["--c99"]
00069             self.cppc = [main_cc] + common + common_c + ["--cpp", "--no_rtti"]
00070         else:
00071             self.cc  = [join(GOANNA_PATH, "goannacc"), "--with-cc=" + main_cc.replace('\\', '/'), "--dialect=armcc", '--output-format="%s"' % self.GOANNA_FORMAT] + common + common_c + ["--c99"]
00072             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"]
00073 
00074         self.ld = [join(ARM_BIN, "armlink")]
00075         self.sys_libs = []
00076 
00077         self.ar = join(ARM_BIN, "armar")
00078         self.elf2bin = join(ARM_BIN, "fromelf")
00079 
00080     def remove_option(self, option):
00081         for tool in [self.asm, self.cc, self.cppc]:
00082             if option in tool:
00083                 tool.remove(option)
00084 
00085     def assemble(self, source, object, includes):
00086         # Preprocess first, then assemble
00087         tempfile = object + '.E.s'
00088         return [
00089             self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-E", "-o", tempfile, source],
00090             self.hook.get_cmdline_assembler(self.asm + ["-o", object, tempfile])
00091         ]
00092 
00093     def parse_dependencies(self, dep_path):
00094         dependencies = []
00095         for line in open(dep_path).readlines():
00096             match = ARM.DEP_PATTERN.match(line)
00097             if match is not None:
00098                 dependencies.append(match.group('file'))
00099         return dependencies
00100 
00101     def parse_output(self, output):
00102         for line in output.splitlines():
00103             match = ARM.DIAGNOSTIC_PATTERN.match(line)
00104             if match is not None:
00105                 self.cc_info(
00106                     match.group('severity').lower(),
00107                     match.group('file'),
00108                     match.group('line'),
00109                     match.group('message'),
00110                     target_name=self.target.name,
00111                     toolchain_name=self.name
00112                 )
00113             match = self.goanna_parse_line(line)
00114             if match is not None:
00115                 self.cc_info(
00116                     match.group('severity').lower(),
00117                     match.group('file'),
00118                     match.group('line'),
00119                     match.group('message')
00120                 )
00121 
00122     def get_dep_opt(self, dep_path):
00123         return ["--depend", dep_path]
00124 
00125     def archive(self, objects, lib_path):
00126         self.default_cmd([self.ar, '-r', lib_path] + objects)
00127 
00128     def link(self, output, objects, libraries, lib_dirs, mem_map):
00129         if len(lib_dirs):
00130             args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt"]
00131         else:
00132             args = ["-o", output, "--info=totals", "--list=.link_totals.txt"]
00133 
00134         if mem_map:
00135             args.extend(["--scatter", mem_map])
00136 
00137         if hasattr(self.target, "link_cmdline_hook"):
00138             args = self.target.link_cmdline_hook(self.__class__.__name__, args)
00139 
00140         self.default_cmd(self.ld + args + objects + libraries + self.sys_libs)
00141 
00142     @hook_tool
00143     def binary(self, resources, elf, bin):
00144         args = [self.elf2bin, '--bin', '-o', bin, elf]
00145 
00146         if hasattr(self.target, "binary_cmdline_hook"):
00147             args = self.target.binary_cmdline_hook(self.__class__.__name__, args)
00148 
00149         self.default_cmd(args)
00150 
00151 class ARM_STD(ARM):
00152     def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
00153         ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
00154         self.cc   += ["-D__ASSERT_MSG"]
00155         self.cppc += ["-D__ASSERT_MSG"]
00156         self.ld.append("--libpath=%s" % ARM_LIB)
00157 
00158 
00159 class ARM_MICRO(ARM):
00160     PATCHED_LIBRARY = False
00161 
00162     def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
00163         ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
00164 
00165         # Compiler
00166         self.asm  += ["-D__MICROLIB"]
00167         self.cc   += ["--library_type=microlib", "-D__MICROLIB", "-D__ASSERT_MSG"]
00168         self.cppc += ["--library_type=microlib", "-D__MICROLIB", "-D__ASSERT_MSG"]
00169 
00170         # Linker
00171         self.ld.append("--library_type=microlib")
00172 
00173         # We had to patch microlib to add C++ support
00174         # In later releases this patch should have entered mainline
00175         if ARM_MICRO.PATCHED_LIBRARY:
00176             self.ld.append("--noscanlib")
00177 
00178             # System Libraries
00179             self.sys_libs.extend([join(MY_ARM_CLIB, lib+".l") for lib in ["mc_p", "mf_p", "m_ps"]])
00180 
00181             if target.core == "Cortex-M3":
00182                 self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ws", "cpprt_w"]])
00183 
00184             elif target.core in ["Cortex-M0", "Cortex-M0+"]:
00185                 self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ps", "cpprt_p"]])
00186         else:
00187             self.ld.append("--libpath=%s" % ARM_LIB)