From e4b16e2d71fad873611a8219405a398af328fd6e Mon Sep 17 00:00:00 2001 From: Andreia Gaita Date: Fri, 19 Jul 2024 14:56:59 +0200 Subject: [PATCH] Add num_jobs as an explicit option so it can be set from other sources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `-j` flag is only settable via the command line, which makes it hard to configure when running builds from places like VS, where the flag isn't easily exposed or configurable. This lets users configure the number of jobs to be used by default if `-j` isn't specified, instead of always defaulting to number of cores - 1. Co-authored-by: RĂ©mi Verschelde --- SConstruct | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/SConstruct b/SConstruct index f3331f3b0d8..e0ba0bdb3cc 100644 --- a/SConstruct +++ b/SConstruct @@ -234,6 +234,11 @@ opts.Add(BoolVariable("tests", "Build the unit tests", False)) opts.Add(BoolVariable("fast_unsafe", "Enable unsafe options for faster rebuilds", False)) opts.Add(BoolVariable("ninja", "Use the ninja backend for faster rebuilds", 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("progress", "Show a progress indicator during compilation", True)) opts.Add(EnumVariable("warnings", "Level of compilation warnings", "all", ("extra", "all", "moderate", "no"))) @@ -537,16 +542,22 @@ initial_num_jobs = env.GetOption("num_jobs") altered_num_jobs = initial_num_jobs + 1 env.SetOption("num_jobs", altered_num_jobs) if env.GetOption("num_jobs") == altered_num_jobs: - cpu_count = os.cpu_count() - if cpu_count is None: - print_warning("Couldn't auto-detect CPU count to configure build parallelism. Specify it with the -j argument.") + num_jobs = env.get("num_jobs", "") + if str(num_jobs).isdigit() and int(num_jobs) > 0: + env.SetOption("num_jobs", num_jobs) 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 argument." - % (cpu_count, safer_cpu_count) - ) - env.SetOption("num_jobs", safer_cpu_count) + cpu_count = os.cpu_count() + if cpu_count is None: + print_warning( + "Couldn't auto-detect CPU count to configure build parallelism. Specify it with the `-j` or `num_jobs` arguments." + ) + 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 = ""