Merge pull request #87376 from shana/add-num-jobs-option

SCons: Add `num_jobs` as an explicit option so it can be set from other sources
This commit is contained in:
Rémi Verschelde 2024-08-28 00:10:23 +02:00
commit bd04f687f0
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -237,6 +237,11 @@ opts.Add(BoolVariable("ninja", "Use the ninja backend for faster rebuilds", Fals
opts.Add(BoolVariable("ninja_auto_run", "Run ninja automatically after generating the ninja file", True)) opts.Add(BoolVariable("ninja_auto_run", "Run ninja automatically after generating the ninja file", True))
opts.Add("ninja_file", "Path to the generated ninja file", "build.ninja") opts.Add("ninja_file", "Path to the generated ninja file", "build.ninja")
opts.Add(BoolVariable("compiledb", "Generate compilation DB (`compile_commands.json`) for external tools", False)) opts.Add(BoolVariable("compiledb", "Generate compilation DB (`compile_commands.json`) for external tools", False))
opts.Add(
"num_jobs",
"Use up to N jobs when compiling (equivalent to `-j N`). Defaults to max jobs - 1. Ignored if -j is used.",
"",
)
opts.Add(BoolVariable("verbose", "Enable verbose output for the compilation", False)) opts.Add(BoolVariable("verbose", "Enable verbose output for the compilation", False))
opts.Add(BoolVariable("progress", "Show a progress indicator during compilation", True)) opts.Add(BoolVariable("progress", "Show a progress indicator during compilation", True))
opts.Add(EnumVariable("warnings", "Level of compilation warnings", "all", ("extra", "all", "moderate", "no"))) opts.Add(EnumVariable("warnings", "Level of compilation warnings", "all", ("extra", "all", "moderate", "no")))
@ -540,16 +545,22 @@ initial_num_jobs = env.GetOption("num_jobs")
altered_num_jobs = initial_num_jobs + 1 altered_num_jobs = initial_num_jobs + 1
env.SetOption("num_jobs", altered_num_jobs) env.SetOption("num_jobs", altered_num_jobs)
if env.GetOption("num_jobs") == altered_num_jobs: if env.GetOption("num_jobs") == altered_num_jobs:
cpu_count = os.cpu_count() num_jobs = env.get("num_jobs", "")
if cpu_count is None: if str(num_jobs).isdigit() and int(num_jobs) > 0:
print_warning("Couldn't auto-detect CPU count to configure build parallelism. Specify it with the -j argument.") env.SetOption("num_jobs", num_jobs)
else: else:
safer_cpu_count = cpu_count if cpu_count <= 4 else cpu_count - 1 cpu_count = os.cpu_count()
print( if cpu_count is None:
"Auto-detected %d CPU cores available for build parallelism. Using %d cores by default. You can override it with the -j argument." print_warning(
% (cpu_count, safer_cpu_count) "Couldn't auto-detect CPU count to configure build parallelism. Specify it with the `-j` or `num_jobs` arguments."
) )
env.SetOption("num_jobs", safer_cpu_count) else:
safer_cpu_count = cpu_count if cpu_count <= 4 else cpu_count - 1
print(
"Auto-detected %d CPU cores available for build parallelism. Using %d cores by default. You can override it with the `-j` or `num_jobs` arguments."
% (cpu_count, safer_cpu_count)
)
env.SetOption("num_jobs", safer_cpu_count)
env.extra_suffix = "" env.extra_suffix = ""