[Concept,05/11] buildman: Split _config_and_build() into three methods

Message ID 20260105183030.1487468-6-sjg@u-boot.org
State New
Headers
Series buildman: Refactor control and builderthread |

Commit Message

Simon Glass Jan. 5, 2026, 6:30 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Refactor the _config_and_build() method to improve readability by
splitting it into three separate methods:

- _setup_build(): Set up the build environment and arguments
- _reconfig_if_needed(): Handle reconfiguration if needed
- _build_and_get_result(): Perform the build and finalise the result

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 tools/buildman/builderthread.py | 110 +++++++++++++++++++++++++++-----
 1 file changed, 94 insertions(+), 16 deletions(-)
  

Patch

diff --git a/tools/buildman/builderthread.py b/tools/buildman/builderthread.py
index eda2b0083d4..67a0b486b47 100644
--- a/tools/buildman/builderthread.py
+++ b/tools/buildman/builderthread.py
@@ -443,29 +443,24 @@  class BuilderThread(threading.Thread):
             commit = 'current'
         return commit
 
-    def _config_and_build(self, req, commit_upto, do_config, mrproper,
-                          config_only, commit, out_dir, out_rel_dir, result):
-        """Do the build, configuring first if necessary
+    def _setup_build(self, req, commit_upto, out_dir, out_rel_dir):
+        """Set up the build environment and arguments
 
         Args:
             req (RunRequest): Run request (see RunRequest for details)
             commit_upto (int): Commit number to build (0...n-1)
-            do_config (bool): True to run a make <board>_defconfig on the source
-            mrproper (bool): True to run mrproper first
-            config_only (bool): Only configure the source, do not build it
-            commit (Commit): Commit being built
             out_dir (str): Output directory for the build, or None to use
                current
             out_rel_dir (str): Output directory relative to the current dir
-            result (CommandResult): Previous result
 
         Returns:
             tuple:
-                result (CommandResult): Result of the build
-                do_config (bool): indicates whether 'make config' is needed on
-                    the next incremental build
+                env (dict): Environment variables for the build
+                args (list of str): Arguments to pass to make
+                config_args (list of str): Arguments for configuration
+                cwd (str): Current working directory for the build
+                src_dir (str): Source directory path
         """
-        # Set up the environment and command line
         env = self.builder.make_environment(self.toolchain)
         if out_dir and not os.path.exists(out_dir):
             mkdir(out_dir)
@@ -481,13 +476,37 @@  class BuilderThread(threading.Thread):
             config_args = [f'{req.brd.target}_defconfig']
         if req.fragments is not None:
             config_args.extend(req.fragments.split(','))
-        config_out = io.StringIO()
 
         _remove_old_outputs(out_dir)
 
-        # If we need to reconfigure, do that now
+        return env, args, config_args, cwd, src_dir
+
+    def _reconfig_if_needed(self, req, commit, cwd, args, env, config_args,
+                            config_out, cmd_list, out_dir, do_config, mrproper,
+                            result):
+        """Reconfigure the build if needed
+
+        Args:
+            req (RunRequest): Run request (see RunRequest for details)
+            commit (Commit): Commit being built
+            cwd (str): Current working directory
+            args (list of str): Arguments to pass to make
+            env (dict): Environment strings
+            config_args (list of str): Arguments for configuration
+            config_out (StringIO): Buffer for configuration output
+            cmd_list (list of str): List to add the commands to, for logging
+            out_dir (str): Output directory for the build
+            do_config (bool): True to run a make <board>_defconfig on the source
+            mrproper (bool): True to run mrproper first
+            result (CommandResult): Previous result
+
+        Returns:
+            tuple:
+                result (CommandResult): Result of the reconfiguration
+                do_config (bool): Whether config is still needed
+                cfg_file (str): Path to the .config file
+        """
         cfg_file = os.path.join(out_dir or '', '.config')
-        cmd_list = []
         if do_config or req.adjust_cfg:
             result = self._reconfigure(
                 commit, req.brd, cwd, args, env, config_args, config_out,
@@ -495,8 +514,29 @@  class BuilderThread(threading.Thread):
             do_config = False   # No need to configure next time
             if req.adjust_cfg:
                 cfgutil.adjust_cfg_file(cfg_file, req.adjust_cfg)
+        return result, do_config, cfg_file
+
+    def _build_and_get_result(self, req, commit, cwd, args, env, cmd_list,
+                              config_out, cfg_file, src_dir, config_only,
+                              result):
+        """Perform the build and finalise the result
+
+        Args:
+            req (RunRequest): Run request (see RunRequest for details)
+            commit (Commit): Commit being built
+            cwd (str): Current working directory
+            args (list of str): Arguments to pass to make
+            env (dict): Environment strings
+            cmd_list (list of str): List to add the commands to, for logging
+            config_out (StringIO): Buffer for configuration output
+            cfg_file (str): Path to the .config file
+            src_dir (str): Source directory path
+            config_only (bool): Only configure the source, do not build it
+            result (CommandResult): Previous result
 
-        # Now do the build, if everything looks OK
+        Returns:
+            CommandResult: Result of the build
+        """
         if result.return_code == 0:
             if req.adjust_cfg:
                 oldc_args = list(args) + ['oldconfig']
@@ -515,6 +555,44 @@  class BuilderThread(threading.Thread):
         if self.builder.verbose_build:
             result.stdout = config_out.getvalue() + result.stdout
         result.cmd_list = cmd_list
+        return result
+
+    def _config_and_build(self, req, commit_upto, do_config, mrproper,
+                          config_only, commit, out_dir, out_rel_dir, result):
+        """Do the build, configuring first if necessary
+
+        Args:
+            req (RunRequest): Run request (see RunRequest for details)
+            commit_upto (int): Commit number to build (0...n-1)
+            do_config (bool): True to run a make <board>_defconfig on the source
+            mrproper (bool): True to run mrproper first
+            config_only (bool): Only configure the source, do not build it
+            commit (Commit): Commit being built
+            out_dir (str): Output directory for the build, or None to use
+               current
+            out_rel_dir (str): Output directory relative to the current dir
+            result (CommandResult): Previous result
+
+        Returns:
+            tuple:
+                result (CommandResult): Result of the build
+                do_config (bool): indicates whether 'make config' is needed on
+                    the next incremental build
+        """
+        env, args, config_args, cwd, src_dir = self._setup_build(
+            req, commit_upto, out_dir, out_rel_dir)
+
+        config_out = io.StringIO()
+        cmd_list = []
+
+        result, do_config, cfg_file = self._reconfig_if_needed(
+            req, commit, cwd, args, env, config_args, config_out, cmd_list,
+            out_dir, do_config, mrproper, result)
+
+        result = self._build_and_get_result(
+            req, commit, cwd, args, env, cmd_list, config_out, cfg_file,
+            src_dir, config_only, result)
+
         return result, do_config
 
     def run_commit(self, req, commit_upto, do_config, mrproper, config_only,