Skip to content
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 22 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> = 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");
Expand Down Expand Up @@ -51,7 +72,7 @@ fn main() {

// 2. engine superbuild. GCC 15 no longer pulls <algorithm> 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");
Expand Down
Loading