use std::fmt::Write as FmtWrite;
use std::path::PathBuf;
const BACKOFF_INFO: &[(&str, &str)] = &[
("crossbeam", "backoff::Crossbeam"),
("yield", "backoff::Yield"),
];
fn main() {
println!("cargo:rerun-if-changed=build.rs");
if std::env::var("CARGO_FEATURE_BENCH_MOODYCAMEL").is_ok() {
println!("cargo:rerun-if-changed=third_party/moodycamel/shim.cpp");
println!("cargo:rerun-if-changed=third_party/moodycamel/concurrentqueue.h");
cc::Build::new()
.cpp(true)
.file("third_party/moodycamel/shim.cpp")
.include("third_party/moodycamel")
.std("c++17")
.compile("moodycamel_shim");
}
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set");
let path = PathBuf::from(&out_dir).join("bench_registry.rs");
let feature_enabled = std::env::var("CARGO_FEATURE_BENCH_REGISTRY").is_ok();
if !feature_enabled {
std::fs::write(
path,
"// bench_registry feature not enabled — stub only.\n\
fn lookup_ubq_job_factory(\n\
\x20\x20\x20\x20_label: &str,\n\
\x20\x20\x20\x20_scenario: ScenarioConfig,\n\
\x20\x20\x20\x20_repeat_index: usize,\n\
\x20\x20\x20\x20_mode: Mode,\n\
\x20\x20\x20\x20_items_per_producer: u64,\n\
\x20\x20\x20\x20_batch_size: Option<usize>,\n\
) -> Option<JobFactory> { None }\n\
fn lookup_ubq_handoff_profile(\n\
\x20\x20\x20\x20_label: &str,\n\
\x20\x20\x20\x20_config: &HandoffProfileConfig,\n\
) -> Option<Result<HandoffProfileResult, String>> { None }\n",
)
.expect("failed to write bench_registry.rs stub");
return;
}
let mut code = String::with_capacity(256 * 1024);
writeln!(code, "// Auto-generated by build.rs — do not edit.").unwrap();
writeln!(code, "fn lookup_ubq_job_factory(").unwrap();
writeln!(code, " label: &str,").unwrap();
writeln!(code, " scenario: ScenarioConfig,").unwrap();
writeln!(code, " repeat_index: usize,").unwrap();
writeln!(code, " mode: Mode,").unwrap();
writeln!(code, " items_per_producer: u64,").unwrap();
writeln!(code, " batch_size: Option<usize>,").unwrap();
writeln!(code, ") -> Option<JobFactory> {{").unwrap();
writeln!(code, " match label {{").unwrap();
for &(backoff_name, backoff_ty) in BACKOFF_INFO {
let label_str = format!("balanced,1,page,{backoff_name}");
let value_type_expr = format!("UBQ<u64, {backoff_ty}>");
let log_type_expr = format!("UBQ<LogRecord, {backoff_ty}>");
writeln!(
code,
" {:?} => Some(make_ubq_job_factory::<{value_type_expr}, {log_type_expr}>(label, scenario, repeat_index, mode, items_per_producer, batch_size)),",
label_str,
)
.unwrap();
}
writeln!(code, " _ => None,").unwrap();
writeln!(code, " }}").unwrap();
writeln!(code, "}}").unwrap();
writeln!(code, "fn lookup_ubq_handoff_profile(").unwrap();
writeln!(code, " label: &str,").unwrap();
writeln!(code, " config: &HandoffProfileConfig,").unwrap();
writeln!(code, ") -> Option<Result<HandoffProfileResult, String>> {{").unwrap();
writeln!(code, " match label {{").unwrap();
for &(backoff_name, backoff_ty) in BACKOFF_INFO {
let label_str = format!("balanced,1,page,{backoff_name}");
let value_type_expr = format!("UBQ<u64, {backoff_ty}>");
writeln!(
code,
" {:?} => Some(profile_handoff_for::<{value_type_expr}>(\"ubq\", Some(label), config)),",
label_str,
)
.unwrap();
}
writeln!(code, " _ => None,").unwrap();
writeln!(code, " }}").unwrap();
writeln!(code, "}}").unwrap();
std::fs::write(path, code).expect("failed to write bench_registry.rs");
}