Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions monero-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<usize>().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();
Expand All @@ -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");

Expand Down Expand Up @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions swap-asb/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 . .

Expand Down
Loading