From 6826848d55bfc21990b561e4500f3a935395cff5 Mon Sep 17 00:00:00 2001 From: Hugues Kamba Date: Thu, 14 Nov 2019 11:58:01 +0000 Subject: [PATCH] minimal-printf: Enable using a target configuration parameter --- targets/targets.json | 1 + tools/test/toolchains/test_toolchains.py | 87 ++++++++++++++++++++++++ tools/toolchains/arm.py | 12 ++++ tools/toolchains/gcc.py | 19 ++++++ tools/toolchains/iar.py | 6 ++ 5 files changed, 125 insertions(+) create mode 100644 tools/test/toolchains/test_toolchains.py diff --git a/targets/targets.json b/targets/targets.json index e7c1ab980d8..837114e612e 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -15,6 +15,7 @@ "default_lib": "std", "bootloader_supported": false, "static_memory_defines": true, + "printf_lib": "std", "config": { "console-uart": { "help": "Target has UART console on pins STDIO_UART_TX, STDIO_UART_RX. Value is only significant if target has SERIAL device.", diff --git a/tools/test/toolchains/test_toolchains.py b/tools/test/toolchains/test_toolchains.py new file mode 100644 index 00000000000..bc8cc952c00 --- /dev/null +++ b/tools/test/toolchains/test_toolchains.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +# Copyright (c) 2019 Arm Limited and Contributors. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 + +"""Test the arm toolchain.""" + +import os +import sys + +import mock + + +ROOT = os.path.abspath( + os.path.join(os.path.dirname(__file__), "..", "..", "..") +) +sys.path.insert(0, ROOT) + +from tools.toolchains.arm import ARM_STD, ARM_MICRO, ARMC6 +from tools.toolchains.gcc import GCC_ARM +from tools.toolchains.iar import IAR + + +class TestArmToolchain: + """Test Arm classes.""" + + def test_arm_minimal_printf(self): + """Test that linker flags are correctly added to an instance of ARM.""" + mock_target = mock.MagicMock() + mock_target.core = "Cortex-M4" + mock_target.printf_lib = "minimal-printf" + mock_target.supported_toolchains = ["ARM", "uARM", "ARMC5"] + + arm_std_obj = ARM_STD(mock_target) + arm_micro_obj = ARM_MICRO(mock_target) + arm_c6_obj = ARMC6(mock_target) + + assert "-DMBED_MINIMAL_PRINTF" in arm_std_obj.flags["common"] + assert "-DMBED_MINIMAL_PRINTF" in arm_micro_obj.flags["common"] + assert "-DMBED_MINIMAL_PRINTF" in arm_c6_obj.flags["common"] + + +class TestGccToolchain: + """Test the GCC class.""" + + def test_gcc_minimal_printf(self): + """Test that linker flags are correctly added to an instance of GCC_ARM.""" + mock_target = mock.MagicMock() + mock_target.core = "Cortex-M4" + mock_target.printf_lib = "minimal-printf" + mock_target.supported_toolchains = ["GCC_ARM"] + mock_target.is_TrustZone_secure_target = False + + gcc_obj = GCC_ARM(mock_target) + + assert "-DMBED_MINIMAL_PRINTF" in gcc_obj.flags["common"] + + minimal_printf_wraps = [ + "-Wl,--wrap,printf", + "-Wl,--wrap,sprintf", + "-Wl,--wrap,snprintf", + "-Wl,--wrap,vprintf", + "-Wl,--wrap,vsprintf", + "-Wl,--wrap,vsnprintf", + "-Wl,--wrap,fprintf", + "-Wl,--wrap,vfprintf", + ] + + assert all( + elem in gcc_obj.flags["ld"] for elem in minimal_printf_wraps + ) + + +class TestIarToolchain: + """Test the IAR class.""" + + def test_iar_minimal_printf(self): + """Test that linker flags are correctly added to an instance of GCC_ARM.""" + mock_target = mock.MagicMock() + mock_target.core = "Cortex-M4" + mock_target.printf_lib = "minimal-printf" + mock_target.supported_toolchains = ["IAR"] + mock_target.is_TrustZone_secure_target = False + + iar_obj = IAR(mock_target) + + assert "-DMBED_MINIMAL_PRINTF" in iar_obj.flags["common"] diff --git a/tools/toolchains/arm.py b/tools/toolchains/arm.py index 8aed902faf9..d3e229474c5 100644 --- a/tools/toolchains/arm.py +++ b/tools/toolchains/arm.py @@ -81,6 +81,12 @@ def __init__(self, target, notify=None, macros=None, if "--library_type=microlib" not in self.flags['common']: self.flags['common'].append("--library_type=microlib") + if ( + target.printf_lib == "minimal-printf" + and "-DMBED_MINIMAL_PRINTF" not in self.flags['common'] + ): + self.flags["common"].append("-DMBED_MINIMAL_PRINTF") + cpu = { "Cortex-M0+": "Cortex-M0plus", "Cortex-M4F": "Cortex-M4.fp.sp", @@ -568,6 +574,12 @@ def __init__(self, target, *args, **kwargs): if "--library_type=microlib" not in self.flags['asm']: self.flags['asm'].append("--library_type=microlib") + if ( + target.printf_lib == "minimal-printf" + and "-DMBED_MINIMAL_PRINTF" not in self.flags["common"] + ): + self.flags["common"].append("-DMBED_MINIMAL_PRINTF") + if target.is_TrustZone_secure_target: if kwargs.get('build_dir', False): # Output secure import library diff --git a/tools/toolchains/gcc.py b/tools/toolchains/gcc.py index 24568d7c528..8f5315fc466 100644 --- a/tools/toolchains/gcc.py +++ b/tools/toolchains/gcc.py @@ -58,6 +58,25 @@ def __init__(self, target, notify=None, macros=None, build_profile=None, self.flags["common"].append("-DMBED_RTOS_SINGLE_THREAD") self.flags["ld"].append("--specs=nano.specs") + if target.printf_lib == "minimal-printf": + if "-DMBED_MINIMAL_PRINTF" not in self.flags['common']: + self.flags["common"].append("-DMBED_MINIMAL_PRINTF") + + minimal_printf_wraps = [ + "-Wl,--wrap,printf", + "-Wl,--wrap,sprintf", + "-Wl,--wrap,snprintf", + "-Wl,--wrap,vprintf", + "-Wl,--wrap,vsprintf", + "-Wl,--wrap,vsnprintf", + "-Wl,--wrap,fprintf", + "-Wl,--wrap,vfprintf", + ] + + for minimal_printf_wrap in minimal_printf_wraps: + if minimal_printf_wrap not in self.flags["ld"]: + self.flags["ld"].append(minimal_printf_wrap) + self.cpu = [] if target.is_TrustZone_secure_target: # Enable compiler security extensions diff --git a/tools/toolchains/iar.py b/tools/toolchains/iar.py index b2a5d4d8fca..eb47b2871db 100644 --- a/tools/toolchains/iar.py +++ b/tools/toolchains/iar.py @@ -69,6 +69,12 @@ def __init__(self, target, notify=None, macros=None, build_profile=None, define_string = self.make_ld_define("DOMAIN_NS", "0x1") self.flags["ld"].append(define_string) + if ( + target.printf_lib == "minimal-printf" + and "-DMBED_MINIMAL_PRINTF" not in self.flags["common"] + ): + self.flags["common"].append("-DMBED_MINIMAL_PRINTF") + core = target.core_without_NS cpu = { "Cortex-M7F": "Cortex-M7.fp.sp",