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