Skip to content

Commit dfd1d69

Browse files
agattidpgeorge
authored andcommitted
tests/run-natmodtests.py: Autodetect the test target architecture.
This commit lets the natmod tests runner to automatically detect the architecture of the test target. This allows to avoid to explicitly pass the architecture name to the runner in test scripts. However, the ability to manually specify a target was not removed but it was made optional. This way the user is able to override the architecture name if needed (like if one wants to test an armv6 MPY on an armv7 board). Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent b603fa3 commit dfd1d69

3 files changed

Lines changed: 55 additions & 9 deletions

File tree

ports/qemu/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ can also be tested with this command (this is currently supported only for the
105105
$ make test_natmod
106106

107107
The same remarks about manually running the tests apply for native modules, but
108-
`run-natmodtests.py` should be run instead of `run-tests.py`. In this case you
109-
also have to explicitly pass the architecture you are running native modules to
110-
`run-natmodtests.py` ("--arch rv32imc" for the `VIRT_RV32` board).
108+
`run-natmodtests.py` should be run instead of `run-tests.py`.
111109

112110
Extra make options
113111
------------------

ports/qemu/boards/VIRT_RV32/mpconfigboard.mk

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ LDSCRIPT = mcu/rv32/virt.ld
99
SRC_BOARD_O += shared/runtime/gchelper_native.o shared/runtime/gchelper_rv32i.o
1010

1111
MPY_CROSS_FLAGS += -march=rv32imc
12-
13-
RUN_NATMODTESTS_ARGS = --arch rv32imc

tests/run-natmodtests.py

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@
2828
"re": "re/re_$(ARCH).mpy",
2929
}
3030

31+
# Supported architectures for native mpy modules
32+
AVAILABLE_ARCHS = (
33+
"x86",
34+
"x64",
35+
"armv6",
36+
"armv6m",
37+
"armv7m",
38+
"armv7em",
39+
"armv7emsp",
40+
"armv7emdp",
41+
"xtensa",
42+
"xtensawin",
43+
"rv32imc",
44+
)
45+
46+
ARCH_MAPPINGS = {"armv7em": "armv7m"}
47+
3148
# Code to allow a target MicroPython to import an .mpy from RAM
3249
injected_import_hook_code = """\
3350
import sys, io, vfs
@@ -96,14 +113,33 @@ def run_script(self, script):
96113
return b"", er
97114

98115

99-
def run_tests(target_truth, target, args, stats):
116+
def detect_architecture(target):
117+
with open("./feature_check/target_info.py", "rb") as f:
118+
target_info_data = f.read()
119+
result_out, error = target.run_script(target_info_data)
120+
if error is not None:
121+
return None, None, error
122+
info = result_out.split(b" ")
123+
if len(info) < 2:
124+
return None, None, "unexpected target info: {}".format(info)
125+
platform = info[0].strip().decode()
126+
arch = info[1].strip().decode()
127+
if arch not in AVAILABLE_ARCHS:
128+
if arch == "None":
129+
return None, None, "the target does not support dynamic modules"
130+
else:
131+
return None, None, "{} is not a supported architecture".format(arch)
132+
return platform, arch, None
133+
134+
135+
def run_tests(target_truth, target, args, stats, resolved_arch):
100136
for test_file in args.files:
101137
# Find supported test
102138
test_file_basename = os.path.basename(test_file)
103139
for k, v in TEST_MAPPINGS.items():
104140
if test_file_basename.startswith(k):
105141
test_module = k
106-
test_mpy = v.replace("$(ARCH)", args.arch)
142+
test_mpy = v.replace("$(ARCH)", resolved_arch)
107143
break
108144
else:
109145
print("---- {} - no matching mpy".format(test_file))
@@ -174,7 +210,7 @@ def main():
174210
"-d", "--device", default="/dev/ttyACM0", help="the device for pyboard.py"
175211
)
176212
cmd_parser.add_argument(
177-
"-a", "--arch", default="x64", help="native architecture of the target"
213+
"-a", "--arch", choices=AVAILABLE_ARCHS, help="override native architecture of the target"
178214
)
179215
cmd_parser.add_argument("files", nargs="*", help="input test files")
180216
args = cmd_parser.parse_args()
@@ -186,8 +222,22 @@ def main():
186222
else:
187223
target = TargetSubprocess([MICROPYTHON])
188224

225+
if hasattr(args, "arch") and args.arch is not None:
226+
target_arch = args.arch
227+
target_platform = None
228+
else:
229+
target_platform, target_arch, error = detect_architecture(target)
230+
if error:
231+
print("Cannot run tests: {}".format(error))
232+
sys.exit(1)
233+
target_arch = ARCH_MAPPINGS.get(target_arch, target_arch)
234+
235+
if target_platform:
236+
print("platform={} ".format(target_platform), end="")
237+
print("arch={}".format(target_arch))
238+
189239
stats = {"total": 0, "pass": 0, "fail": 0, "skip": 0}
190-
run_tests(target_truth, target, args, stats)
240+
run_tests(target_truth, target, args, stats, target_arch)
191241

192242
target.close()
193243
target_truth.close()

0 commit comments

Comments
 (0)