Skip to content

Commit 95c5e85

Browse files
authored
Merge pull request Mbed-TLS#118 from daverodgman/mtest-iar
2 parents 0b65b27 + bab5d56 commit 95c5e85

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

tools/bin/mtest

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import sys
1111
import shutil
1212
from collections import namedtuple
1313

14-
ALL_COMPILERS = [ "gcc", "clang", "armclang", "arm-none-eabi-gcc" ]
14+
ALL_COMPILERS = [ "gcc", "clang", "armclang", "arm-none-eabi-gcc", "iar" ]
1515
NATIVE_TARGET = None
1616

1717
ARCHS = [ "mips", "mipsel", "mips64", "mips64el", "powerpc64le", "s390x", "riscv64", "x86_64", "x86" ]
@@ -42,6 +42,7 @@ NON_NATIVE_SUPPORT = {
4242
"armclang": [ "armv6-thumb1", "armv7-arm", "armv8-thumb2", "armv8-aarch64" ],
4343
"gcc": [ "armv5-thumb1", "armv5-arm", "armv6-thumb1", "armv6-thumb2", "armv6-arm", "armv7-thumb2", "armv7-arm", "armv8-thumb2", "armv8-arm", "armv8-aarch64" ] + ARCHS,
4444
"clang": [ "armv5-thumb1", "armv5-arm", "armv6-thumb1", "armv6-thumb2", "armv6-arm", "armv7-thumb2", "armv7-arm", "armv8-thumb2", "armv8-arm", "armv8-aarch64" ] + ARCHS,
45+
"iar": [ "armv5-thumb1", "armv5-arm", "armv6-thumb1", "armv6-thumb2", "armv6-arm", "armv7-thumb2", "armv7-arm", "armv8-thumb2", "armv8-arm", "armv8-aarch64" ]
4546
}
4647

4748
QEMU_NAME_MAP = {
@@ -53,20 +54,28 @@ MEMSAN_OPTIONS = [ "MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN", "-MBEDTLS_USE_PSA_CRYPTO
5354
MEMSAN_CFLAGS = ['-fsanitize=memory', '-O1', '-g3', '-fno-omit-frame-pointer', '-fno-optimize-sibling-calls', '-fsanitize-memory-track-origins=2']
5455
MEMSAN_CC = "clang"
5556

57+
# TODO: can some of the IAR limitations be removed?
5658
COMPILER_CONFIGS = {
5759
# "armclang": [ "MBEDTLS_NO_UDBL_DIVISION" ]
60+
"iar": [ "MBEDTLS_NO_PLATFORM_ENTROPY", "-MBEDTLS_HAVE_TIME_DATE", "-MBEDTLS_HAVE_TIME", "-MBEDTLS_TIMING_C", "-MBEDTLS_FS_IO", "-MBEDTLS_PSA_ITS_FILE_C", "-MBEDTLS_PSA_CRYPTO_STORAGE_C", "-MBEDTLS_NET_C" ]
5861
}
5962

6063
ASAN_CFLAGS = ['-fsanitize=address', '-fno-common', '-fsanitize=undefined', '-fno-sanitize-recover=all', '-O1', '-g3', '-fno-omit-frame-pointer', '-fno-optimize-sibling-calls']
6164
ASAN_LDFLAGS = ASAN_CFLAGS
6265

6366
WARNING_FLAGS = {
67+
"iar" : ['--warnings_are_errors'], # cannot enable --strict because then IAR will not recognise #warning
6468
"clang": ['-Werror', '-Wall', '-Wextra', '-Wwrite-strings', '-Wpointer-arith', '-Wimplicit-fallthrough', '-Wshadow', '-Wvla', '-Wformat=2', '-Wno-format-nonliteral', '-Wshadow', '-Wasm-operand-widths', '-Wunreachable-code'],
6569
"gcc" : ['-Werror', '-Wall', '-Wextra', '-Wwrite-strings', '-Wpointer-arith', '-Wimplicit-fallthrough', '-Wshadow', '-Wvla', '-Wformat=2', '-Wno-format-nonliteral', '-Wshadow', '-Wformat-signedness', '-Wformat-overflow=2', '-Wformat-truncation', '-Wlogical-op']
6670
}
6771
WARNING_FLAGS["arm-none-eabi-gcc"] = WARNING_FLAGS["gcc"]
6872
WARNING_FLAGS["armclang"] = WARNING_FLAGS["clang"]
6973

74+
# Use full system libraries for IAR
75+
COMPILER_FLAGS = {
76+
"iar": ["--dlib_config=full"]
77+
}
78+
7079
TFM_ARMCLANG_TARGET_FLAGS = ["--target=arm-arm-none-eabi", "-mcpu=cortex-m33+nodsp+nofp", "-mthumb"]
7180
TFM_CONFIG_FILES = [("config-tfm.h", None), ("tfm_mbedcrypto_config_profile_medium.h", "crypto_config_profile_medium.h")]
7281
COPY_TFM_CONFIG = "//TFM-CONFIG//"
@@ -136,6 +145,7 @@ def clean():
136145

137146
def toolchain_exes(target):
138147
compiler_exe = target.compiler
148+
archiver_exe = None
139149

140150
# construct triple needed in gcc and objdump executable name
141151
triple_abi = "gnu"
@@ -180,6 +190,10 @@ def toolchain_exes(target):
180190
compiler_exe = c
181191
break
182192

193+
elif target.compiler_family == "iar":
194+
compiler_exe = "iccarm"
195+
archiver_exe = "iarchive"
196+
183197
if target.compiler == "gcc":
184198
compiler_error = f"could not locate gcc executable {compiler_exe} or {compiler_exe}-<majorversion>"
185199
elif target.compiler_family == "gcc":
@@ -226,6 +240,7 @@ def toolchain_exes(target):
226240

227241
return {
228242
"compiler": compiler_exe,
243+
"archiver": archiver_exe,
229244
"objdump": objdump_exe,
230245
"size": size_exe,
231246
"triple": triple,
@@ -239,19 +254,28 @@ def toolchain_exes(target):
239254

240255
def toolchain(target, exit_on_failure=True):
241256
# basic CFLAGS setup
242-
# target_c_flags may get overridden by the user (and dropped from the resulting set of flags);
243-
# c_flags is always applied
257+
# target_c_flags are options generated by mtest, which may get overridden by the user (and dropped from the resulting set of flags)
244258

245-
target_c_flags = ["-std=c99", f"-I{ROOT}/include"]
259+
target_c_flags = [f"-I{ROOT}/include"] + COMPILER_FLAGS.get(target.compiler_family, [])
260+
261+
if target.compiler_family != "iar":
262+
# IAR defaults to C99 and doesn't have an option for it to be explicitly specified
263+
target_c_flags += ["-std=c99"]
246264

247265
if args.size_tfm and "clang" in target.compiler:
248266
target_c_flags += ["-Oz"]
267+
elif target.compiler_family == "iar":
268+
# -Ohz = high optimization, favouring size
269+
# -Ol = fastest compile time (about 3x better than Ohz)
270+
target_c_flags += ["-Ol"]
249271
else:
250272
target_c_flags += ["-Os"]
251273

252274
if args.no_warnings:
253275
if target.compiler_family == "clang":
254276
target_c_flags += ["-Wno-everything"]
277+
elif target.compiler_family == "iar":
278+
target_c_flags += ["--no_warnings"]
255279
else:
256280
target_c_flags += ["-Wno-all", "-Wno-extra"]
257281
else:
@@ -344,6 +368,28 @@ def toolchain(target, exit_on_failure=True):
344368
else:
345369
error(f"unsupported target {target.arch} {target.isa} for clang")
346370

371+
elif target.compiler_family == "iar":
372+
if target.arch == "armv5" and target.isa == "thumb1":
373+
target_c_flags += ["--cpu=ARM926EJ-S", "--thumb"]
374+
elif target.arch == "armv5" and target.isa == "arm":
375+
target_c_flags += ["--cpu=ARM968E-S", "--arm"]
376+
elif target.arch == "armv6" and target.isa == "thumb1":
377+
target_c_flags += ["--cpu=ARM1136J-S", "--thumb"]
378+
elif target.arch == "armv6" and target.isa == "thumb2":
379+
target_c_flags += ["--cpu=ARM1156T2-S", "--thumb"]
380+
elif target.arch == "armv6" and target.isa == "arm":
381+
target_c_flags += ["--cpu=ARM1156T2-S", "--arm"]
382+
elif target.arch == "armv7" and target.isa == "thumb2":
383+
target_c_flags += ["--cpu=Cortex-A17", "--thumb"]
384+
elif target.arch == "armv7" and target.isa == "arm":
385+
target_c_flags += ["--cpu=Cortex-A17", "--arm"]
386+
elif target.arch == "armv8" and target.isa == "thumb2":
387+
target_c_flags += ["--cpu=Cortex-A32", "--thumb"]
388+
elif target.arch == "armv8" and target.isa == "arm":
389+
target_c_flags += ["--cpu=Cortex-A32", "--arm"]
390+
elif target.arch == "armv8" and target.isa == "aarch64":
391+
target_c_flags += ["--cpu=Cortex-A72", "--aarch64"]
392+
347393
# unconditionally add user-specified flags
348394
c_flags = target.cflags.split(" ")
349395

@@ -379,11 +425,20 @@ def toolchain(target, exit_on_failure=True):
379425
if not target.is_native:
380426
ld_flags += " --static"
381427

428+
if target.compiler_family == "iar":
429+
# Specify AR_FLAGS for IAR
430+
ar_flags = "--create"
431+
else:
432+
# Do not specify AR_FLAGS - the Makefile will generate some IAR-incompatible flags
433+
ar_flags = None
434+
382435
exes = toolchain_exes(target)
383436
return {
384437
"CFLAGS": " ".join(x for x in c_flags if x != "").strip(),
385438
"LDFLAGS": ld_flags.strip(),
439+
"ARFLAGS": ar_flags,
386440
"CC": exes["compiler"],
441+
"AR": exes["archiver"],
387442
"OBJDUMP": exes["objdump"],
388443
"SIZE": exes["size"]
389444
}
@@ -425,7 +480,7 @@ def configure_options(target):
425480
# target-specific options
426481
options = target.config
427482
# compiler-specific options
428-
options += COMPILER_CONFIGS.get(target.compiler, [])
483+
options += COMPILER_CONFIGS.get(target.compiler_family, [])
429484
# memsan options
430485
if args.memsan:
431486
options += MEMSAN_OPTIONS
@@ -553,7 +608,21 @@ def build(target, tests):
553608
if not args.no_clean:
554609
clean()
555610
t = toolchain(target)
556-
flags = ["CFLAGS=" + t['CFLAGS'], "LDFLAGS=" + t['LDFLAGS'], "CC=" + t['CC']]
611+
612+
flags = ["CFLAGS=" + t['CFLAGS'], "CC=" + t['CC']]
613+
614+
if target.compiler_family == "iar":
615+
# Clear WARNING_CFLAGS so that the Makefile doesn't automatically add -Wall etc, which IAR doesn't understand
616+
flags += ["WARNING_CFLAGS="]
617+
618+
if t['LDFLAGS'] != "":
619+
flags += ["LDFLAGS=" + t['LDFLAGS']]
620+
621+
if t['AR'] is not None:
622+
flags += ['AR=' + t['AR']]
623+
624+
if t['ARFLAGS'] is not None:
625+
flags += ['ARFLAGS=' + t['ARFLAGS']]
557626

558627
configure_options(target)
559628

0 commit comments

Comments
 (0)