Skip to contentSkip to conversation

kunit: tool: Autodetect a default architecture

PATCH New David Gow 1 participant 1 message Nov 24

Subsystems (2)
lore
Share & tools
lore

David Gow

author maintainer
#0 lore raw

PATCH 1/1 kunit: tool: Autodetect a default architecture

kunit.py always defaults to using UML (--arch=um) for building / running tests, but it is only available on x86 architectures. Instead, attempt to select a better default: UML on x86, or the host architecture otherwise. A cut-down version of the map from uname -m -> Linux kernel architecture from scripts/subarch.include is used to detect this, but it only includes the most common ones for now.

Ultimately, this should be replaced by something in the architecture config, which we'll be replacing anyway, so something simple like this should suffice for now.

---
 tools/testing/kunit/kunit.py           | 27 +++++++++++++++++++++++++-
 tools/testing/kunit/kunit_tool_test.py |  3 +++
 2 files changed, 29 insertions(+), 1 deletion(-)
tools/testing/kunit/kunit.py +26 -1
index 91d234ac3b57..41ca3d32bdc5 100755
@@ -9,6 +9,7 @@
 
 import argparse
 import os
+import platform
 import re
 import shlex
 import sys
@@ -340,6 +341,30 @@ def get_default_jobs() -> int:
 		return ncpu
 	raise RuntimeError("os.cpu_count() returned None")
 
+# Map from host architecture (uname -m) -> default KUnit architecture
+# Should be 'um' for architectures supporting UML, or the name of the
+# qemu_config otherwise (typically the kernel arch name).
+# See scripts/subarch.include for a reference.
+default_arch_map = {
+		'i386': 'um',
+		'i486': 'um',
+		'i586': 'um',
+		'i686': 'um',
+		'x86_64': 'um',
+		'aarch64': 'arm64',
+		'sun4u': 'sparc64',
+		's390x': 's390',
+		'loongarch64': 'loongarch',
+		'ppc64': 'powerpc64',
+}
+
+def get_default_arch() -> str:
+	host_uname_arch = platform.machine()
+	if host_uname_arch in default_arch_map:
+		return default_arch_map[host_uname_arch]
+
+	return host_uname_arch
+
 def get_default_build_dir() -> str:
 	if 'KBUILD_OUTPUT' in os.environ:
 		return os.path.join(os.environ['KBUILD_OUTPUT'], '.kunit')
@@ -384,7 +409,7 @@ def add_common_opts(parser: argparse.ArgumentParser) -> None:
 				  'string passed to the ARCH make param, '
 				  'e.g. i386, x86_64, arm, um, etc. Non-UML '
 				  'architectures run on QEMU.'),
-			    type=str, default='um', metavar='ARCH')
+				  type=str, default=get_default_arch(), metavar='ARCH')
 
 	parser.add_argument('--cross_compile',
 			    help=('Sets make\'s CROSS_COMPILE variable; it should '
tools/testing/kunit/kunit_tool_test.py +3 -0
index 85ae21754bdf..6b5f32ed25f6 100755
@@ -742,6 +742,9 @@ class KUnitMainTest(unittest.TestCase):
 		self.linux_source_mock.build_kernel.return_value = True
 		self.linux_source_mock.run_kernel.return_value = all_passed_log
 
+		self.default_arch_mock = mock.patch('kunit.get_default_arch').start()
+		self.default_arch_mock.return_value = 'um'
+
 	def test_config_passes_args_pass(self):
 		kunit.main(['config', '--build_dir=.kunit'])
 		self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
Signature · 1 line
2.56.1