//! 两件事: //! //! 1. 计算唯一一个 cfg `mpc_control_plane`(必须放在最前面,见下)。 //! 2. 只在带 `ray` feature 编译时,把 libmpc_ray.so 所在目录写进可执行文件的 //! RUNPATH。`cargo:rustc-link-arg-bins` 只作用于**本包**的 bin 目标, //! 放在 mpc-ray 的 build.rs 里传不过来——症状是链接期能找到 .so、运行期报 //! `libmpc_ray.so: cannot open shared object file`。 //! //! ★ 第 1 件的理由:mpc-hostd / mpc-chain 既是可选依赖(feature //! `control-plane`),又只挂在非 musl 目标上(见 Cargo.toml 的 //! `[target.'cfg(not(target_env = "musl"))'.dependencies]`)。源码必须按 //! **同一个条件**裁掉模块与命令分派;把这个条件在 main.rs 里抄八遍, //! 迟早有一处漏改。 //! //! musl 目标即使开着 feature 也**不**设这个 cfg —— 于是 main.rs 的 //! `compile_error!` 能真正执行到,明确告诉调用方加 `--no-default-features`, //! 而不是先撞上 openssl-sys 那句含混的 pkg-config 报错。 //! ★ 注意下面 ray 分支有提前 return,cfg 计算**必须**在它之前。 fn main() { // ---- 1. 控制面 cfg(不产生构建产物、不引入依赖)---- // // ★ 分成两个 cfg,不是两个 feature —— 因为它们对应的是**依赖差异**: // // mpc_control_plane —— 宿主控制面里**不依赖 OpenSSL** 的那一半: // mpc-hostd(axum/tokio/reqwest)+ EasyTier 链路(mpc_hostd::link)。 // ★ 这一半**可以**在 musl 下静态编译 —— 真机没有 Rust 工具链, // 需要一个自包含二进制把 hostd 跑起来。 // // mpc_chain_plane —— 链上层:mpc-chain 链接 **系统 OpenSSL** // (openssl-sys 要 musl 版 libcrypto,本机没有)。 // ★ 这一半**不能**进 musl:一旦进了,main.rs 的 compile_error! // 守卫会被 pkg-config 抢先,守卫就变成永远测不到的假判据。 println!("cargo:rustc-check-cfg=cfg(mpc_control_plane)"); println!("cargo:rustc-check-cfg=cfg(mpc_chain_plane)"); let target = std::env::var("TARGET").unwrap_or_default(); let control_plane_on = std::env::var_os("CARGO_FEATURE_CONTROL_PLANE").is_some(); let host_only_on = std::env::var_os("CARGO_FEATURE_HOST_ONLY").is_some(); // guest 的静态产物只可能是 musl 目标;宿主(darwin / glibc linux)不受影响。 let is_guest_target = target.contains("musl"); // ★ musl + control-plane 是**明确错误**:那是 guest 产物,边界不许破。 // main.rs 的 compile_error! 会给出可执行的指引。 if control_plane_on && is_guest_target { // 由 main.rs 的 compile_error! 负责报错文案,这里只负责 cfg 不被误设。 } else if control_plane_on || host_only_on { // 宿主(darwin / glibc):control-plane 含 chain,host-only 不含。 println!("cargo:rustc-cfg=mpc_control_plane"); if control_plane_on { println!("cargo:rustc-cfg=mpc_chain_plane"); } } // ---- 2. Ray 的 RUNPATH ---- println!("cargo:rerun-if-env-changed=MPC_RAY_LIB_DIR"); println!("cargo:rerun-if-env-changed=MPC_RAY_LIB_NAME"); println!("cargo:rerun-if-env-changed=MPC_RAY_LIBPYTHON"); // 没开 ray feature 就别管——默认构建是纯静态单二进制,不碰任何 .so。 if std::env::var("CARGO_FEATURE_RAY").is_err() { return; } let lib = std::env::var("MPC_RAY_LIB_NAME").unwrap_or_else(|_| "mpc_ray".to_string()); let mut dirs: Vec = Vec::new(); if let Ok(d) = std::env::var("MPC_RAY_LIB_DIR") { dirs.push(d); } // 与 mpc-ray/build.rs 保持同一套搜索顺序 if let Ok(m) = std::env::var("CARGO_MANIFEST_DIR") { if let Some(root) = std::path::Path::new(&m).parent().and_then(|p| p.parent()) { dirs.push(root.join("target/ray").display().to_string()); } } dirs.push("/usr/local/lib".into()); for d in &dirs { if std::path::Path::new(d).join(format!("lib{lib}.so")).exists() { println!("cargo:rustc-link-arg-bins=-Wl,-rpath,{d}"); break; } } // _raylet.so 自己带 RUNPATH 指向 libpython,但交叉编译的场景不一定,多带一个不亏。 if let Ok(p) = std::env::var("MPC_RAY_LIBPYTHON") { if let Some(dir) = std::path::Path::new(&p).parent() { println!("cargo:rustc-link-arg-bins=-Wl,-rpath,{}", dir.display()); } } }