@@ -154,6 +154,42 @@ def can_use_kvm(can_test_for_kvm, guest_arch):
154154 return False
155155
156156
157+ def get_config_val (kernel_arg , config_key ):
158+ """
159+ Attempt to get configuration value from a .config relative to
160+ kernel_location.
161+
162+ Parameters:
163+ kernel_arg (str): The value of the '--kernel' argument.
164+
165+ Returns:
166+ The configuration value if it can be found, None if not.
167+ """
168+ # kernel_arg is either a path to the kernel source or a full kernel
169+ # location. If it is a file, we need to strip off the basename so that we
170+ # can easily navigate around with '..'.
171+ if (kernel_dir := Path (kernel_arg )).is_file ():
172+ kernel_dir = kernel_dir .parent
173+
174+ # If kernel_location is the kernel source, the configuration will be at
175+ # <kernel_dir>/.config
176+ #
177+ # If kernel_location is a full kernel location, it could either be:
178+ # * <kernel_dir>/.config (if the image is vmlinux)
179+ # * <kernel_dir>/../../../.config (if the image is in arch/*/boot/)
180+ # * <kernel_dir>/config (if the image is in a TuxMake folder)
181+ config_locations = [".config" , "../../../.config" , "config" ]
182+ if (config_file := utils .find_first_file (kernel_dir ,
183+ config_locations ,
184+ required = False )):
185+ config_txt = config_file .read_text (encoding = 'utf-8' )
186+ if (match := re .search (f"^{ config_key } =(.*)$" , config_txt ,
187+ flags = re .M )):
188+ return match .groups ()[0 ]
189+
190+ return None
191+
192+
157193def get_smp_value (args ):
158194 """
159195 Get the value of '-smp' based on user input and kernel configuration.
@@ -178,41 +214,16 @@ def get_smp_value(args):
178214 if args .smp :
179215 return args .smp
180216
181- # kernel_location is either a path to the kernel source or a full kernel
182- # location. If it is a file, we need to strip off the basename so that we
183- # can easily navigate around with '..'.
184- kernel_dir = Path (args .kernel_location )
185- if kernel_dir .is_file ():
186- kernel_dir = kernel_dir .parent
187-
188- # If kernel_location is the kernel source, the configuration will be at
189- # <kernel_dir>/.config
190- #
191- # If kernel_location is a full kernel location, it could either be:
192- # * <kernel_dir>/.config (if the image is vmlinux)
193- # * <kernel_dir>/../../../.config (if the image is in arch/*/boot/)
194- # * <kernel_dir>/config (if the image is in a TuxMake folder)
195- config_file = None
196- for config_name in [".config" , "../../../.config" , "config" ]:
197- config_path = kernel_dir .joinpath (config_name )
198- if config_path .is_file ():
199- config_file = config_path
200- break
201-
202217 # Choose a sensible default value based on treewide defaults for
203218 # CONFIG_NR_CPUS then get the actual value if possible.
204- config_nr_cpus = 8
205- if config_file :
206- with open (config_file , encoding = 'utf-8' ) as file :
207- for line in file :
208- if "CONFIG_NR_CPUS=" in line :
209- config_nr_cpus = int (line .split ("=" , 1 )[1 ])
210- break
219+ if not (config_nr_cpus := get_config_val (args .kernel_location ,
220+ 'CONFIG_NR_CPUS' )):
221+ config_nr_cpus = 8
211222
212223 # Use the minimum of the number of usable processors for the script or
213224 # CONFIG_NR_CPUS.
214225 usable_cpus = os .cpu_count ()
215- return min (usable_cpus , config_nr_cpus )
226+ return min (usable_cpus , int ( config_nr_cpus ) )
216227
217228
218229def setup_cfg (args ):
0 commit comments