From 8174acb3c040336f59cac8bdc9e259a53b91cfce Mon Sep 17 00:00:00 2001 From: Mohan <86064887+binarybaron@users.noreply.github.com> Date: Mon, 22 Jun 2026 13:40:50 +0200 Subject: [PATCH] monero-sys: make Monero C++ build parallelism configurable via MONERO_BUILD_JOBS The Monero C++ compile in build.rs was hardcoded to -j1 in CI/Docker (and all cores locally) with no way to tune it. Monero's translation units are RAM-hungry, so serial is a safe default on memory-constrained runners, but builders with enough memory have no escape hatch and pay for a fully serial compile. Add an optional MONERO_BUILD_JOBS env var that overrides the default job count, and plumb it through swap-asb/Dockerfile as a build arg so the image build can be tuned with `--build-arg MONERO_BUILD_JOBS=N`. Behavior is unchanged when the variable is unset. Co-Authored-By: Claude Opus 4.8 (1M context) --- monero-sys/build.rs | 35 +++++++++++++++++++++++++++-------- swap-asb/Dockerfile | 6 ++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/monero-sys/build.rs b/monero-sys/build.rs index 12ca0153e7..1d857c8b13 100644 --- a/monero-sys/build.rs +++ b/monero-sys/build.rs @@ -110,6 +110,29 @@ fn find_workspace_target_dir() -> std::path::PathBuf { panic!("Could not find target directory from OUT_DIR: {out_dir}"); } +/// Number of parallel jobs (`-j`) for the Monero C++ build. +/// +/// Monero's C++ compilation is very RAM-hungry, so we default to serial (`-j1`) +/// in CI/Docker where memory is constrained, and to all logical cores locally. +/// An explicit `MONERO_BUILD_JOBS` overrides both, allowing the Docker/CI build +/// to be tuned without editing this file (mind the peak memory: each Monero +/// translation unit can take hundreds of MB, so `-j2`/`-j4` is the safe range). +fn monero_build_jobs(is_github_actions: bool, is_docker_build: bool) -> usize { + if let Some(jobs) = std::env::var("MONERO_BUILD_JOBS") + .ok() + .and_then(|v| v.parse::().ok()) + .filter(|&jobs| jobs > 0) + { + return jobs; + } + + if is_github_actions || is_docker_build { + 1 + } else { + num_cpus::get() + } +} + fn main() { let is_github_actions: bool = std::env::var("GITHUB_ACTIONS").is_ok(); let is_docker_build: bool = std::env::var("DOCKER_BUILD").is_ok(); @@ -124,6 +147,9 @@ fn main() { // Rerun if the patches directory or any patch files change println!("cargo:rerun-if-changed=patches"); + // Rerun if the Monero build parallelism override changes + println!("cargo:rerun-if-env-changed=MONERO_BUILD_JOBS"); + // Apply embedded patches before building apply_patches().expect("Failed to apply our patches"); @@ -196,14 +222,7 @@ fn main() { .to_string(), ) // This is needed for libsodium.a to be found on mingw-w64 .build_arg("-Wno-dev") // Disable warnings we can't fix anyway - .build_arg(format!( - "-j{}", - if is_github_actions || is_docker_build { - 1 - } else { - num_cpus::get() - } - )) + .build_arg(format!("-j{}", monero_build_jobs(is_github_actions, is_docker_build))) .build_arg("-I.") .build(); diff --git a/swap-asb/Dockerfile b/swap-asb/Dockerfile index 3d7b3cd61b..10a91a7980 100644 --- a/swap-asb/Dockerfile +++ b/swap-asb/Dockerfile @@ -88,6 +88,12 @@ RUN --mount=type=cache,target=/root/.cargo/registry,sharing=locked \ # Act as if we are in a GitHub Actions environment ENV DOCKER_BUILD=true +# Number of parallel jobs for the Monero C++ build (monero-sys/build.rs). +# Defaults to serial (-j1) when unset because Monero's compilation is RAM-hungry. +# Override with `--build-arg MONERO_BUILD_JOBS=N` on builders with enough memory. +ARG MONERO_BUILD_JOBS +ENV MONERO_BUILD_JOBS=${MONERO_BUILD_JOBS} + # Now we build our binaries COPY . .