[PATCH RFC] kunit: respect KBUILD_OUTPUT env variable by default

2 views
Skip to first unread message

Ryota Sakamoto

unread,
Dec 28, 2025, 2:57:25 AM12/28/25
to Brendan Higgins, David Gow, Rae Moar, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org, Ryota Sakamoto
Currently, kunit.py ignores the KBUILD_OUTPUT env variable and always
defaults to .kunit in the working directory. This behavior is inconsistent
with standard Kbuild behavior, where KBUILD_OUTPUT defines the build
artifact location.

This patch modifies kunit.py to respect KBUILD_OUTPUT if set. A .kunit
subdirectory is created inside KBUILD_OUTPUT to avoid polluting the build
directory.

Signed-off-by: Ryota Sakamoto <sakamo...@gmail.com>
---
tools/testing/kunit/kunit.py | 7 ++++++-
tools/testing/kunit/kunit_tool_test.py | 19 +++++++++++++++++++
2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/tools/testing/kunit/kunit.py b/tools/testing/kunit/kunit.py
index cd99c1956331dbbfb06cf4ddf130db3dcf2a7c31..e3d82a038f93df0e86952da92461bc2e02f69ed1 100755
--- a/tools/testing/kunit/kunit.py
+++ b/tools/testing/kunit/kunit.py
@@ -323,11 +323,16 @@ def get_default_jobs() -> int:
return ncpu
raise RuntimeError("os.cpu_count() returned None")

+def get_default_build_dir() -> str:
+ if 'KBUILD_OUTPUT' in os.environ:
+ return os.path.join(os.environ['KBUILD_OUTPUT'], '.kunit')
+ return '.kunit'
+
def add_common_opts(parser: argparse.ArgumentParser) -> None:
parser.add_argument('--build_dir',
help='As in the make command, it specifies the build '
'directory.',
- type=str, default='.kunit', metavar='DIR')
+ type=str, default=get_default_build_dir(), metavar='DIR')
parser.add_argument('--make_options',
help='X=Y make option, can be repeated.',
action='append', metavar='X=Y')
diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py
index bbba921e0eacb18663abfcabb2bccf330d8666f5..a55b5085310d1bc54a549d3f36a83f7697fb8881 100755
--- a/tools/testing/kunit/kunit_tool_test.py
+++ b/tools/testing/kunit/kunit_tool_test.py
@@ -601,6 +601,7 @@ class KUnitMainTest(unittest.TestCase):
all_passed_log = file.readlines()

self.print_mock = mock.patch('kunit_printer.Printer.print').start()
+ mock.patch.dict(os.environ, clear=True).start()
self.addCleanup(mock.patch.stopall)

self.mock_linux_init = mock.patch.object(kunit_kernel, 'LinuxSourceTree').start()
@@ -723,6 +724,24 @@ class KUnitMainTest(unittest.TestCase):
args=None, build_dir=build_dir, filter_glob='', filter='', filter_action=None, timeout=300)
self.print_mock.assert_any_call(StrContains('Testing complete.'))

+ @mock.patch.dict(os.environ, {'KBUILD_OUTPUT': '/tmp'})
+ def test_run_builddir_from_env(self):
+ build_dir = '/tmp/.kunit'
+ kunit.main(['run'])
+ self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
+ self.linux_source_mock.run_kernel.assert_called_once_with(
+ args=None, build_dir=build_dir, filter_glob='', filter='', filter_action=None, timeout=300)
+ self.print_mock.assert_any_call(StrContains('Testing complete.'))
+
+ @mock.patch.dict(os.environ, {'KBUILD_OUTPUT': '/tmp'})
+ def test_run_builddir_override(self):
+ build_dir = '.kunit'
+ kunit.main(['run', '--build_dir=.kunit'])
+ self.assertEqual(self.linux_source_mock.build_reconfig.call_count, 1)
+ self.linux_source_mock.run_kernel.assert_called_once_with(
+ args=None, build_dir=build_dir, filter_glob='', filter='', filter_action=None, timeout=300)
+ self.print_mock.assert_any_call(StrContains('Testing complete.'))
+
def test_config_builddir(self):
build_dir = '.kunit'
kunit.main(['config', '--build_dir', build_dir])

---
base-commit: ccd1cdca5cd433c8a5dff78b69a79b31d9b77ee1
change-id: 20251228-kunit-kbuild_output-5d21a89fa603

Best regards,
--
Ryota Sakamoto <sakamo...@gmail.com>

David Gow

unread,
Jan 5, 2026, 3:53:59 AMJan 5
to Ryota Sakamoto, Brendan Higgins, Rae Moar, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org
On Sun, 28 Dec 2025 at 15:57, Ryota Sakamoto <sakamo...@gmail.com> wrote:
>
> Currently, kunit.py ignores the KBUILD_OUTPUT env variable and always
> defaults to .kunit in the working directory. This behavior is inconsistent
> with standard Kbuild behavior, where KBUILD_OUTPUT defines the build
> artifact location.
>
> This patch modifies kunit.py to respect KBUILD_OUTPUT if set. A .kunit
> subdirectory is created inside KBUILD_OUTPUT to avoid polluting the build
> directory.
>
> Signed-off-by: Ryota Sakamoto <sakamo...@gmail.com>
> ---

Thanks!

Two small thoughts:
- Do we want to make KBUILD_OUTPUT the KUnit output directory
directly, rather than nesting .kunit?
- Do we want to make the --build_dir option relative to KBUILD_OUTPUT,
instead of the current directory?

Personally, I think the answer to both of those is probably no, so I'm
happy to take this as-is.

Reviewed-by: David Gow <davi...@google.com>

Cheers,
-- David

Ryota Sakamoto

unread,
Jan 5, 2026, 11:29:58 AMJan 5
to David Gow, Brendan Higgins, Rae Moar, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org
Hi David,
Thank you for your review.

On Mon, Jan 5, 2026 at 5:53 PM David Gow <davi...@google.com> wrote:
> Two small thoughts:
> - Do we want to make KBUILD_OUTPUT the KUnit output directory
> directly, rather than nesting .kunit?
> - Do we want to make the --build_dir option relative to KBUILD_OUTPUT,
> instead of the current directory?
>
> Personally, I think the answer to both of those is probably no, so I'm
> happy to take this as-is.

I agree with you that the answer for both points is no. Keeping the nesting in
KBUILD_OUTPUT seems cleaner, and keeping --build_dir explicit avoids confusion.

I will send v2 shortly with the Reviewed-by tag and remove RFC.

Regards,

Ryota Sakamoto

unread,
Jan 5, 2026, 11:41:42 AMJan 5
to Brendan Higgins, David Gow, Rae Moar, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org, Ryota Sakamoto
Currently, kunit.py ignores the KBUILD_OUTPUT env variable and always
defaults to .kunit in the working directory. This behavior is inconsistent
with standard Kbuild behavior, where KBUILD_OUTPUT defines the build
artifact location.

This patch modifies kunit.py to respect KBUILD_OUTPUT if set. A .kunit
subdirectory is created inside KBUILD_OUTPUT to avoid polluting the build
directory.

Reviewed-by: David Gow <davi...@google.com>
Signed-off-by: Ryota Sakamoto <sakamo...@gmail.com>
---
Changes in v2:
- Add Reviewed-by tag and remove RFC
- Link to v1: https://lore.kernel.org/r/20251228-kunit-kbuild_o...@gmail.com
---
Reply all
Reply to author
Forward
0 new messages