Nathan Yonkee / Mbed 2 deprecated Nucleo_sinewave_output_copy

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gcc.py Source File

gcc.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, basename, splitext, dirname, exists
00019 from distutils.spawn import find_executable
00020 
00021 from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS
00022 from tools.hooks import hook_tool
00023 
00024 class GCC(mbedToolchain):
00025     LINKER_EXT = '.ld'
00026     LIBRARY_EXT = '.a'
00027 
00028     STD_LIB_NAME = "lib%s.a"
00029     DIAGNOSTIC_PATTERN = re.compile('((?P<file>[^:]+):(?P<line>\d+):)(?P<col>\d+):? (?P<severity>warning|[eE]rror|fatal error): (?P<message>.+)')
00030 
00031     def __init__(self, target,  notify=None, macros=None,
00032                  silent=False, extra_verbose=False, build_profile=None,
00033                  build_dir=None):
00034         mbedToolchain.__init__(self, target, notify, macros, silent,
00035                                extra_verbose=extra_verbose,
00036                                build_profile=build_profile, build_dir=build_dir)
00037 
00038         tool_path=TOOLCHAIN_PATHS['GCC_ARM']
00039         # Add flags for current size setting
00040         default_lib = "std"
00041         if hasattr(target, "default_lib"):
00042             default_lib = target.default_lib
00043         elif hasattr(target, "default_build"): # Legacy
00044             default_lib = target.default_build
00045 
00046         if default_lib == "small":
00047             self.flags["common"].append("-DMBED_RTOS_SINGLE_THREAD")
00048             self.flags["ld"].append("--specs=nano.specs")
00049 
00050         if target.core == "Cortex-M0+":
00051             cpu = "cortex-m0plus"
00052         elif target.core == "Cortex-M4F":
00053             cpu = "cortex-m4"
00054         elif target.core == "Cortex-M7F":
00055             cpu = "cortex-m7"
00056         elif target.core == "Cortex-M7FD":
00057             cpu = "cortex-m7"
00058         elif target.core == "Cortex-M23-NS":
00059             cpu = "cortex-m23"
00060         elif target.core == "Cortex-M33-NS":
00061             cpu = "cortex-m33"
00062         else:
00063             cpu = target.core.lower()
00064 
00065         self.cpu = ["-mcpu=%s" % cpu]
00066         if target.core.startswith("Cortex-M"):
00067             self.cpu.append("-mthumb")
00068 
00069         # FPU handling, M7 possibly to have double FPU
00070         if target.core == "Cortex-M4F":
00071             self.cpu.append("-mfpu=fpv4-sp-d16")
00072             self.cpu.append("-mfloat-abi=softfp")
00073         elif target.core == "Cortex-M7F":
00074             self.cpu.append("-mfpu=fpv5-sp-d16")
00075             self.cpu.append("-mfloat-abi=softfp")
00076         elif target.core == "Cortex-M7FD":
00077             self.cpu.append("-mfpu=fpv5-d16")
00078             self.cpu.append("-mfloat-abi=softfp")
00079 
00080         if target.core == "Cortex-A9":
00081             self.cpu.append("-mthumb-interwork")
00082             self.cpu.append("-marm")
00083             self.cpu.append("-march=armv7-a")
00084             self.cpu.append("-mfpu=vfpv3")
00085             self.cpu.append("-mfloat-abi=hard")
00086             self.cpu.append("-mno-unaligned-access")
00087 
00088         if target.core == "Cortex-M23" or target.core == "Cortex-M33":
00089             self.cpu.append("-mcmse")
00090         elif target.core == "Cortex-M23-NS" or target.core == "Cortex-M33-NS":
00091              self.flags["ld"].append("-D__DOMAIN_NS=1")
00092 
00093         self.flags["common"] += self.cpu
00094 
00095         main_cc = join(tool_path, "arm-none-eabi-gcc")
00096         main_cppc = join(tool_path, "arm-none-eabi-g++")
00097         self.asm = [main_cc] + self.flags['asm'] + self.flags["common"]
00098         self.cc  = [main_cc]
00099         self.cppc =[main_cppc]
00100         self.cc += self.flags['c'] + self.flags['common']
00101         self.cppc += self.flags['cxx'] + self.flags['common']
00102 
00103         self.flags['ld'] += self.cpu
00104         self.ld = [join(tool_path, "arm-none-eabi-gcc")] + self.flags['ld']
00105         self.sys_libs = ["stdc++", "supc++", "m", "c", "gcc", "nosys"]
00106         self.preproc = [join(tool_path, "arm-none-eabi-cpp"), "-E", "-P"]
00107 
00108         self.ar = join(tool_path, "arm-none-eabi-ar")
00109         self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")
00110 
00111     def is_not_supported_error(self, output):
00112         return "error: #error [NOT_SUPPORTED]" in output
00113 
00114     def parse_output(self, output):
00115         # The warning/error notification is multiline
00116         msg = None
00117         for line in output.splitlines():
00118             match = self.DIAGNOSTIC_PATTERN.search(line)
00119             if match is not None:
00120                 if msg is not None:
00121                     self.cc_info(msg)
00122                     msg = None
00123                 msg = {
00124                     'severity': match.group('severity').lower(),
00125                     'file': match.group('file'),
00126                     'line': match.group('line'),
00127                     'col': match.group('col'),
00128                     'message': match.group('message'),
00129                     'text': '',
00130                     'target_name': self.target.name,
00131                     'toolchain_name': self.name
00132                 }
00133 
00134         if msg is not None:
00135             self.cc_info(msg)
00136 
00137     def get_dep_option(self, object):
00138         base, _ = splitext(object)
00139         dep_path = base + '.d'
00140         return ["-MD", "-MF", dep_path]
00141 
00142     def get_config_option(self, config_header):
00143         return ['-include', config_header]
00144 
00145     def get_compile_options(self, defines, includes, for_asm=False):
00146         opts = ['-D%s' % d for d in defines]
00147         if self.RESPONSE_FILES:
00148             opts += ['@%s' % self.get_inc_file(includes)]
00149         else:
00150             opts += ["-I%s" % i for i in includes]
00151 
00152         if not for_asm:
00153             config_header = self.get_config_header()
00154             if config_header is not None:
00155                 opts = opts + self.get_config_option(config_header)
00156         return opts
00157 
00158     @hook_tool
00159     def assemble(self, source, object, includes):
00160         # Build assemble command
00161         cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes) + ["-o", object, source]
00162 
00163         # Call cmdline hook
00164         cmd = self.hook.get_cmdline_assembler(cmd)
00165 
00166         # Return command array, don't execute
00167         return [cmd]
00168 
00169     @hook_tool
00170     def compile(self, cc, source, object, includes):
00171         # Build compile command
00172         cmd = cc + self.get_compile_options(self.get_symbols(), includes)
00173 
00174         cmd.extend(self.get_dep_option(object))
00175 
00176         cmd.extend(["-o", object, source])
00177 
00178         # Call cmdline hook
00179         cmd = self.hook.get_cmdline_compiler(cmd)
00180 
00181         return [cmd]
00182 
00183     def compile_c(self, source, object, includes):
00184         return self.compile(self.cc, source, object, includes)
00185 
00186     def compile_cpp(self, source, object, includes):
00187         return self.compile(self.cppc, source, object, includes)
00188 
00189     @hook_tool
00190     def link(self, output, objects, libraries, lib_dirs, mem_map):
00191         libs = []
00192         for l in libraries:
00193             name, _ = splitext(basename(l))
00194             libs.append("-l%s" % name[3:])
00195         libs.extend(["-l%s" % l for l in self.sys_libs])
00196 
00197         # Preprocess
00198         if mem_map:
00199             preproc_output = join(dirname(output), ".link_script.ld")
00200             cmd = (self.preproc + [mem_map] + self.ld[1:] +
00201                    [ "-o", preproc_output])
00202             self.cc_verbose("Preproc: %s" % ' '.join(cmd))
00203             self.default_cmd(cmd)
00204             mem_map = preproc_output
00205 
00206         # Build linker command
00207         map_file = splitext(output)[0] + ".map"
00208         cmd = self.ld + ["-o", output, "-Wl,-Map=%s" % map_file] + objects + ["-Wl,--start-group"] + libs + ["-Wl,--end-group"]
00209         # Create Secure library
00210         if self.target.core == "Cortex-M23" or self.target.core == "Cortex-M33":
00211             secure_file = join(dirname(output), "cmse_lib.o")
00212             cmd.extend(["-Wl,--cmse-implib"])
00213             cmd.extend(["-Wl,--out-implib=%s" % secure_file])
00214 
00215         if mem_map:
00216             cmd.extend(['-T', mem_map])
00217 
00218         for L in lib_dirs:
00219             cmd.extend(['-L', L])
00220         cmd.extend(libs)
00221 
00222         # Call cmdline hook
00223         cmd = self.hook.get_cmdline_linker(cmd)
00224 
00225         if self.RESPONSE_FILES:
00226             # Split link command to linker executable + response file
00227             cmd_linker = cmd[0]
00228             link_files = self.get_link_file(cmd[1:])
00229             cmd = [cmd_linker, "@%s" % link_files]
00230 
00231         # Exec command
00232         self.cc_verbose("Link: %s" % ' '.join(cmd))
00233         self.default_cmd(cmd)
00234         if self.target.core == "Cortex-M23" or self.target.core == "Cortex-M33":
00235             self.info("Secure Library Object %s" %secure_file)
00236 
00237     @hook_tool
00238     def archive(self, objects, lib_path):
00239         if self.RESPONSE_FILES:
00240             param = ["@%s" % self.get_arch_file(objects)]
00241         else:
00242             param = objects
00243 
00244         # Exec command
00245         self.default_cmd([self.ar, 'rcs', lib_path] + param)
00246 
00247     @hook_tool
00248     def binary(self, resources, elf, bin):
00249         # Build binary command
00250         _, fmt = splitext(bin)
00251         bin_arg = {'.bin': 'binary', '.hex': 'ihex'}[fmt]
00252         cmd = [self.elf2bin, "-O", bin_arg, elf, bin]
00253 
00254         # Call cmdline hook
00255         cmd = self.hook.get_cmdline_binary(cmd)
00256 
00257         # Exec command
00258         self.cc_verbose("FromELF: %s" % ' '.join(cmd))
00259         self.default_cmd(cmd)
00260 
00261     @staticmethod
00262     def name_mangle(name):
00263         return "_Z%i%sv" % (len(name), name)
00264 
00265     @staticmethod
00266     def make_ld_define(name, value):
00267         return "-D%s=0x%x" % (name, value)
00268 
00269     @staticmethod
00270     def redirect_symbol(source, sync, build_dir):
00271         return "-Wl,--defsym=%s=%s" % (source, sync)
00272 
00273     @staticmethod
00274     def check_executable():
00275         """Returns True if the executable (arm-none-eabi-gcc) location
00276         specified by the user exists OR the executable can be found on the PATH.
00277         Returns False otherwise."""
00278         if not TOOLCHAIN_PATHS['GCC_ARM'] or not exists(TOOLCHAIN_PATHS['GCC_ARM']):
00279             if find_executable('arm-none-eabi-gcc'):
00280                 TOOLCHAIN_PATHS['GCC_ARM'] = ''
00281                 return True
00282             else:
00283                 return False
00284         else:
00285             exec_name = join(TOOLCHAIN_PATHS['GCC_ARM'], 'arm-none-eabi-gcc')
00286             return exists(exec_name) or exists(exec_name + '.exe')
00287 
00288 class GCC_ARM(GCC):
00289     pass