diff --git a/README.md b/README.md index 3c0b9a6..a9242ea 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,8 @@ cd sk33p && cargo build --release `build.rs` runs the engine's own CMake superbuild (which fetches RapidJSON, RapidXML, pugixml and libzip), compiles the C ABI shim in `shim/`, and links it all statically. `patches/` holds two small engine fixes applied at build time. -The engine is a heavy C++ compile: set `SK33P_JOBS=1` on a small machine. +The engine is a heavy C++ compile: the build runs one job per 4 GB of available +RAM (at most 4) so cc1plus is not OOM-killed; set `SK33P_JOBS=N` to override. ## License diff --git a/build.rs b/build.rs index 66412b1..4768e7c 100644 --- a/build.rs +++ b/build.rs @@ -14,6 +14,27 @@ fn run(cmd: &mut Command) { assert!(status.success(), "command failed: {cmd:?}"); } +/// Cargo's NUM_JOBS is the CPU count, but an engine translation unit can take +/// 1-2 GB in cc1plus, so 32 at once gets OOM-killed ("Terminated signal +/// terminated program cc1plus", the SIGTERM coming from earlyoom). Each job is +/// only ~350 MB, but on a shared box other services spike, so stay well clear: +/// one job per 4 GB of MemAvailable, at most 4. +fn default_jobs() -> usize { + let cpus: usize = env::var("NUM_JOBS").ok().and_then(|v| v.parse().ok()).unwrap_or(4); + let avail_kb: Option = std::fs::read_to_string("/proc/meminfo").ok().and_then(|m| { + m.lines() + .find(|l| l.starts_with("MemAvailable:")) + .and_then(|l| l.split_whitespace().nth(1)) + .and_then(|v| v.parse().ok()) + }); + let jobs = match avail_kb { + Some(kb) => cpus.min(4).min(kb / (4 * 1024 * 1024)).max(1), + None => cpus.min(4), + }; + println!("cargo:warning=sk33p: building engine with {jobs} jobs (set SK33P_JOBS to override)"); + jobs +} + fn main() { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let engine = root.join("engine"); @@ -51,7 +72,7 @@ fn main() { // 2. engine superbuild. GCC 15 no longer pulls in transitively, // and the engine uses the 4-argument std::lower_bound without including it. - let jobs = env::var("SK33P_JOBS").unwrap_or_else(|_| env::var("NUM_JOBS").unwrap_or("4".into())); + let jobs = env::var("SK33P_JOBS").unwrap_or_else(|_| default_jobs().to_string()); let msvc = env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc"); let force = if msvc { "/FIalgorithm" } else { "-include algorithm" }; let build_dir = out.join("engine-build");