2013-08-06 13:38:00 +00:00
|
|
|
"""
|
|
|
|
mbed SDK
|
|
|
|
Copyright (c) 2011-2013 ARM Limited
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
"""
|
2013-06-24 13:32:08 +00:00
|
|
|
import re
|
2016-06-09 22:50:03 +00:00
|
|
|
from os.path import join, basename, splitext, dirname, exists
|
2013-06-24 13:32:08 +00:00
|
|
|
|
2016-06-09 20:34:53 +00:00
|
|
|
from tools.toolchains import mbedToolchain
|
|
|
|
from tools.settings import GCC_ARM_PATH, GCC_CR_PATH
|
|
|
|
from tools.settings import GOANNA_PATH
|
|
|
|
from tools.hooks import hook_tool
|
2013-06-24 13:32:08 +00:00
|
|
|
|
|
|
|
class GCC(mbedToolchain):
|
|
|
|
LINKER_EXT = '.ld'
|
|
|
|
LIBRARY_EXT = '.a'
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
STD_LIB_NAME = "lib%s.a"
|
2016-06-10 14:19:02 +00:00
|
|
|
DIAGNOSTIC_PATTERN = re.compile('((?P<file>[^:]+):(?P<line>\d+):)(\d+:)? (?P<severity>warning|error): (?P<message>.+)')
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2015-11-12 18:16:10 +00:00
|
|
|
def __init__(self, target, options=None, notify=None, macros=None, silent=False, tool_path="", extra_verbose=False):
|
|
|
|
mbedToolchain.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
if target.core == "Cortex-M0+":
|
2015-01-01 19:02:46 +00:00
|
|
|
cpu = "cortex-m0plus"
|
2013-11-27 20:02:37 +00:00
|
|
|
elif target.core == "Cortex-M4F":
|
|
|
|
cpu = "cortex-m4"
|
2015-12-07 15:40:20 +00:00
|
|
|
elif target.core == "Cortex-M7F":
|
|
|
|
cpu = "cortex-m7"
|
2013-06-24 13:32:08 +00:00
|
|
|
else:
|
|
|
|
cpu = target.core.lower()
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
self.cpu = ["-mcpu=%s" % cpu]
|
|
|
|
if target.core.startswith("Cortex"):
|
|
|
|
self.cpu.append("-mthumb")
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-11-27 20:02:37 +00:00
|
|
|
if target.core == "Cortex-M4F":
|
2013-06-24 13:32:08 +00:00
|
|
|
self.cpu.append("-mfpu=fpv4-sp-d16")
|
2014-03-21 12:16:52 +00:00
|
|
|
self.cpu.append("-mfloat-abi=softfp")
|
2015-12-07 15:40:20 +00:00
|
|
|
elif target.core == "Cortex-M7F":
|
|
|
|
self.cpu.append("-mfpu=fpv5-d16")
|
|
|
|
self.cpu.append("-mfloat-abi=softfp")
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2015-02-26 07:28:58 +00:00
|
|
|
if target.core == "Cortex-A9":
|
|
|
|
self.cpu.append("-mthumb-interwork")
|
|
|
|
self.cpu.append("-marm")
|
|
|
|
self.cpu.append("-march=armv7-a")
|
2015-09-08 07:26:40 +00:00
|
|
|
self.cpu.append("-mfpu=vfpv3")
|
2015-02-26 07:28:58 +00:00
|
|
|
self.cpu.append("-mfloat-abi=hard")
|
|
|
|
self.cpu.append("-mno-unaligned-access")
|
|
|
|
|
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
# Note: We are using "-O2" instead of "-Os" to avoid this known GCC bug:
|
|
|
|
# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46762
|
2014-05-03 15:08:45 +00:00
|
|
|
common_flags = ["-c", "-Wall", "-Wextra",
|
2013-08-23 00:45:45 +00:00
|
|
|
"-Wno-unused-parameter", "-Wno-missing-field-initializers",
|
2013-06-24 13:32:08 +00:00
|
|
|
"-fmessage-length=0", "-fno-exceptions", "-fno-builtin",
|
|
|
|
"-ffunction-sections", "-fdata-sections",
|
2016-06-09 22:50:03 +00:00
|
|
|
"-fno-delete-null-pointer-checks", "-fomit-frame-pointer"
|
2013-06-24 13:32:08 +00:00
|
|
|
] + self.cpu
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-07-02 15:43:29 +00:00
|
|
|
if "save-asm" in self.options:
|
2013-07-02 16:04:40 +00:00
|
|
|
common_flags.append("-save-temps")
|
2013-07-03 12:20:08 +00:00
|
|
|
|
2015-09-25 08:16:24 +00:00
|
|
|
if "debug-info" in self.options:
|
2016-06-10 12:12:21 +00:00
|
|
|
common_flags.append("-g")
|
2015-09-25 08:16:24 +00:00
|
|
|
common_flags.append("-O0")
|
|
|
|
else:
|
|
|
|
common_flags.append("-O2")
|
2013-10-14 14:32:41 +00:00
|
|
|
|
|
|
|
main_cc = join(tool_path, "arm-none-eabi-gcc")
|
|
|
|
main_cppc = join(tool_path, "arm-none-eabi-g++")
|
2013-11-08 13:32:31 +00:00
|
|
|
self.asm = [main_cc, "-x", "assembler-with-cpp"] + common_flags
|
2013-10-14 14:32:41 +00:00
|
|
|
if not "analyze" in self.options:
|
|
|
|
self.cc = [main_cc, "-std=gnu99"] + common_flags
|
2014-06-21 09:04:04 +00:00
|
|
|
self.cppc =[main_cppc, "-std=gnu++98", "-fno-rtti"] + common_flags
|
2013-10-14 14:32:41 +00:00
|
|
|
else:
|
|
|
|
self.cc = [join(GOANNA_PATH, "goannacc"), "--with-cc=" + main_cc.replace('\\', '/'), "-std=gnu99", "--dialect=gnu", '--output-format="%s"' % self.GOANNA_FORMAT] + common_flags
|
2014-06-21 09:04:04 +00:00
|
|
|
self.cppc= [join(GOANNA_PATH, "goannac++"), "--with-cxx=" + main_cppc.replace('\\', '/'), "-std=gnu++98", "-fno-rtti", "--dialect=gnu", '--output-format="%s"' % self.GOANNA_FORMAT] + common_flags
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-08-07 11:51:30 +00:00
|
|
|
self.ld = [join(tool_path, "arm-none-eabi-gcc"), "-Wl,--gc-sections", "-Wl,--wrap,main"] + self.cpu
|
2013-06-24 13:32:08 +00:00
|
|
|
self.sys_libs = ["stdc++", "supc++", "m", "c", "gcc"]
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
self.ar = join(tool_path, "arm-none-eabi-ar")
|
|
|
|
self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
def parse_dependencies(self, dep_path):
|
|
|
|
dependencies = []
|
2016-06-09 22:50:03 +00:00
|
|
|
buff = open(dep_path).readlines()
|
|
|
|
buff[0] = re.sub('^(.*?)\: ', '', buff[0])
|
|
|
|
for line in buff:
|
2013-06-24 13:32:08 +00:00
|
|
|
file = line.replace('\\\n', '').strip()
|
|
|
|
if file:
|
2013-09-05 12:29:13 +00:00
|
|
|
# GCC might list more than one dependency on a single line, in this case
|
|
|
|
# the dependencies are separated by a space. However, a space might also
|
|
|
|
# indicate an actual space character in a dependency path, but in this case
|
|
|
|
# the space character is prefixed by a backslash.
|
|
|
|
# Temporary replace all '\ ' with a special char that is not used (\a in this
|
|
|
|
# case) to keep them from being interpreted by 'split' (they will be converted
|
|
|
|
# back later to a space char)
|
|
|
|
file = file.replace('\\ ', '\a')
|
|
|
|
if file.find(" ") == -1:
|
|
|
|
dependencies.append(file.replace('\a', ' '))
|
|
|
|
else:
|
|
|
|
dependencies = dependencies + [f.replace('\a', ' ') for f in file.split(" ")]
|
2013-06-24 13:32:08 +00:00
|
|
|
return dependencies
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2016-02-25 22:29:26 +00:00
|
|
|
def is_not_supported_error(self, output):
|
|
|
|
return "error: #error [NOT_SUPPORTED]" in output
|
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
def parse_output(self, output):
|
|
|
|
# The warning/error notification is multiline
|
|
|
|
WHERE, WHAT = 0, 1
|
|
|
|
state, file, message = WHERE, None, None
|
|
|
|
for line in output.splitlines():
|
2013-10-14 14:32:41 +00:00
|
|
|
match = self.goanna_parse_line(line)
|
|
|
|
if match is not None:
|
|
|
|
self.cc_info(
|
|
|
|
match.group('severity').lower(),
|
|
|
|
match.group('file'),
|
|
|
|
match.group('line'),
|
2014-06-02 14:44:45 +00:00
|
|
|
match.group('message'),
|
|
|
|
target_name=self.target.name,
|
|
|
|
toolchain_name=self.name
|
2013-10-14 14:32:41 +00:00
|
|
|
)
|
|
|
|
continue
|
2014-05-29 13:42:03 +00:00
|
|
|
|
|
|
|
|
2016-06-10 14:19:02 +00:00
|
|
|
match = GCC.DIAGNOSTIC_PATTERN.match(line)
|
|
|
|
if match is not None:
|
2013-06-24 13:32:08 +00:00
|
|
|
self.cc_info(
|
2016-06-10 14:19:02 +00:00
|
|
|
match.group('severity').lower(),
|
|
|
|
match.group('file'),
|
|
|
|
match.group('line'),
|
|
|
|
match.group('message'),
|
|
|
|
target_name=self.target.name,
|
|
|
|
toolchain_name=self.name
|
2013-06-24 13:32:08 +00:00
|
|
|
)
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2016-06-09 22:50:03 +00:00
|
|
|
def get_dep_option(self, object):
|
|
|
|
base, _ = splitext(object)
|
|
|
|
dep_path = base + '.d'
|
|
|
|
return ["-MD", "-MF", dep_path]
|
|
|
|
|
|
|
|
def get_compile_options(self, defines, includes):
|
|
|
|
return ['-D%s' % d for d in defines] + ['@%s' % self.get_inc_file(includes)]
|
|
|
|
|
|
|
|
@hook_tool
|
|
|
|
def assemble(self, source, object, includes):
|
|
|
|
# Build assemble command
|
|
|
|
cmd = self.asm + self.get_compile_options(self.get_symbols(), includes) + ["-o", object, source]
|
|
|
|
|
|
|
|
# Call cmdline hook
|
|
|
|
cmd = self.hook.get_cmdline_assembler(cmd)
|
|
|
|
|
|
|
|
# Return command array, don't execute
|
|
|
|
return [cmd]
|
|
|
|
|
|
|
|
@hook_tool
|
|
|
|
def compile(self, cc, source, object, includes):
|
|
|
|
# Build compile command
|
|
|
|
cmd = cc + self.get_compile_options(self.get_symbols(), includes)
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2016-06-09 22:50:03 +00:00
|
|
|
cmd.extend(self.get_dep_option(object))
|
|
|
|
|
|
|
|
cmd.extend(["-o", object, source])
|
|
|
|
|
|
|
|
# Call cmdline hook
|
|
|
|
cmd = self.hook.get_cmdline_compiler(cmd)
|
|
|
|
|
|
|
|
return [cmd]
|
|
|
|
|
|
|
|
def compile_c(self, source, object, includes):
|
|
|
|
return self.compile(self.cc, source, object, includes)
|
|
|
|
|
|
|
|
def compile_cpp(self, source, object, includes):
|
|
|
|
return self.compile(self.cppc, source, object, includes)
|
|
|
|
|
|
|
|
@hook_tool
|
2013-06-24 13:32:08 +00:00
|
|
|
def link(self, output, objects, libraries, lib_dirs, mem_map):
|
|
|
|
libs = []
|
|
|
|
for l in libraries:
|
|
|
|
name, _ = splitext(basename(l))
|
|
|
|
libs.append("-l%s" % name[3:])
|
|
|
|
libs.extend(["-l%s" % l for l in self.sys_libs])
|
2016-06-09 22:50:03 +00:00
|
|
|
|
|
|
|
# Build linker command
|
|
|
|
map_file = splitext(output)[0] + ".map"
|
|
|
|
cmd = self.ld + ["-o", output, "-Wl,-Map=%s" % map_file] + objects + ["-Wl,--start-group"] + libs + ["-Wl,--end-group"]
|
|
|
|
if mem_map:
|
|
|
|
cmd.extend(['-T', mem_map])
|
|
|
|
|
|
|
|
for L in lib_dirs:
|
|
|
|
cmd.extend(['-L', L])
|
|
|
|
cmd.extend(libs)
|
|
|
|
|
|
|
|
# Call cmdline hook
|
|
|
|
cmd = self.hook.get_cmdline_linker(cmd)
|
|
|
|
|
|
|
|
# Split link command to linker executable + response file
|
|
|
|
link_files = join(dirname(output), ".link_files.txt")
|
|
|
|
with open(link_files, "wb") as f:
|
|
|
|
cmd_linker = cmd[0]
|
|
|
|
cmd_list = []
|
|
|
|
for c in cmd[1:]:
|
|
|
|
if c:
|
|
|
|
cmd_list.append(('"%s"' % c) if not c.startswith('-') else c)
|
|
|
|
string = " ".join(cmd_list).replace("\\", "/")
|
|
|
|
f.write(string)
|
|
|
|
|
|
|
|
# Exec command
|
|
|
|
self.default_cmd([cmd_linker, "@%s" % link_files])
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2016-06-09 22:50:03 +00:00
|
|
|
@hook_tool
|
|
|
|
def archive(self, objects, lib_path):
|
|
|
|
archive_files = join(dirname(lib_path), ".archive_files.txt")
|
|
|
|
with open(archive_files, "wb") as f:
|
|
|
|
o_list = []
|
|
|
|
for o in objects:
|
|
|
|
o_list.append('"%s"' % o)
|
|
|
|
string = " ".join(o_list).replace("\\", "/")
|
|
|
|
f.write(string)
|
|
|
|
|
|
|
|
# Exec command
|
|
|
|
self.default_cmd([self.ar, 'rcs', lib_path, "@%s" % archive_files])
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2014-08-12 13:59:50 +00:00
|
|
|
@hook_tool
|
2014-02-07 12:10:39 +00:00
|
|
|
def binary(self, resources, elf, bin):
|
2016-06-09 22:50:03 +00:00
|
|
|
# Build binary command
|
|
|
|
cmd = [self.elf2bin, "-O", "binary", elf, bin]
|
|
|
|
|
|
|
|
# Call cmdline hook
|
|
|
|
cmd = self.hook.get_cmdline_binary(cmd)
|
|
|
|
|
|
|
|
# Exec command
|
|
|
|
self.default_cmd(cmd)
|
2013-06-24 13:32:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GCC_ARM(GCC):
|
2015-11-12 18:16:10 +00:00
|
|
|
def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
|
|
|
|
GCC.__init__(self, target, options, notify, macros, silent, GCC_ARM_PATH, extra_verbose=extra_verbose)
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
# Use latest gcc nanolib
|
2016-06-11 21:05:54 +00:00
|
|
|
if "big-build" in self.options:
|
|
|
|
use_nano = False
|
|
|
|
elif "small-build" in self.options:
|
|
|
|
use_nano = True
|
|
|
|
elif target.default_build == "standard":
|
|
|
|
use_nano = False
|
|
|
|
elif target.default_build == "small":
|
|
|
|
use_nano = True
|
|
|
|
else:
|
|
|
|
use_nano = False
|
|
|
|
|
|
|
|
if use_nano:
|
2016-05-24 01:59:02 +00:00
|
|
|
self.ld.append("--specs=nano.specs")
|
2016-06-11 21:05:54 +00:00
|
|
|
self.cc += ["-DMBED_RTOS_SINGLE_THREAD"]
|
|
|
|
self.cppc += ["-DMBED_RTOS_SINGLE_THREAD"]
|
|
|
|
|
2015-02-08 10:56:39 +00:00
|
|
|
if target.name in ["LPC1768", "LPC4088", "LPC4088_DM", "LPC4330", "UBLOX_C027", "LPC2368"]:
|
2014-10-15 18:09:06 +00:00
|
|
|
self.ld.extend(["-u _printf_float", "-u _scanf_float"])
|
2016-03-31 07:28:43 +00:00
|
|
|
elif target.name in ["RZ_A1H", "VK_RZ_A1H", "ARCH_MAX", "DISCO_F407VG", "DISCO_F429ZI", "DISCO_F469NI", "NUCLEO_F401RE", "NUCLEO_F410RB", "NUCLEO_F411RE", "NUCLEO_F446RE", "ELMO_F411RE", "MTS_MDOT_F411RE", "MTS_DRAGONFLY_F411RE", "DISCO_F746NG"]:
|
2015-02-26 07:28:58 +00:00
|
|
|
self.ld.extend(["-u_printf_float", "-u_scanf_float"])
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
self.sys_libs.append("nosys")
|
|
|
|
|
|
|
|
|
|
|
|
class GCC_CR(GCC):
|
2015-11-12 18:16:10 +00:00
|
|
|
def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
|
|
|
|
GCC.__init__(self, target, options, notify, macros, silent, GCC_CR_PATH, extra_verbose=extra_verbose)
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2013-06-24 13:32:08 +00:00
|
|
|
additional_compiler_flags = [
|
|
|
|
"-D__NEWLIB__", "-D__CODE_RED", "-D__USE_CMSIS", "-DCPP_USE_HEAP",
|
|
|
|
]
|
|
|
|
self.cc += additional_compiler_flags
|
|
|
|
self.cppc += additional_compiler_flags
|
2014-05-29 13:42:03 +00:00
|
|
|
|
2014-10-25 15:51:05 +00:00
|
|
|
# Use latest gcc nanolib
|
|
|
|
self.ld.append("--specs=nano.specs")
|
2015-02-08 10:56:39 +00:00
|
|
|
if target.name in ["LPC1768", "LPC4088", "LPC4088_DM", "LPC4330", "UBLOX_C027", "LPC2368"]:
|
2014-10-25 15:51:05 +00:00
|
|
|
self.ld.extend(["-u _printf_float", "-u _scanf_float"])
|
2013-06-24 13:32:08 +00:00
|
|
|
self.ld += ["-nostdlib"]
|
|
|
|
|