pmpx-plugin 0.0.0

Plugin contract for pmpx: the PackageManager trait and the stable C ABI that carries it across dlopen.
Documentation
//! 把编译期的 rustc 版本与 target triple 注入进去,供 [`pmpx_plugin::abi`] 填进
//! `PmpxPluginV1`。
//!
//! # 为什么这件事必须由 build.rs 做
//!
//! 宿主在加载插件时想知道"这个 .so 是谁编的"。但 `rustc --version` 是**运行期**才知道的
//! 东西 —— 编译出来之后就没地方问了。所以在编译期问一次,把答案固化进二进制。
//!
//! 顺带一提:这个 crate 编译出的常量会被链接进**每一个插件**,所以插件不需要自己的
//! build.rs,也不需要知道 `PMPX_BUILD_*` 这些名字 —— 见 `export!` 宏。

use std::process::Command;

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    // 换工具链要重新跑,否则会把上一个工具链的版本号固化进去。
    println!("cargo:rerun-if-env-changed=RUSTC");

    let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());

    // 问不到就写 "unknown" —— 这个字段**只用于诊断展示**,拿不到不该让构建失败。
    let version = Command::new(&rustc)
        .arg("--version")
        .output()
        .ok()
        .and_then(|out| String::from_utf8(out.stdout).ok())
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| "unknown".to_string());

    let target = std::env::var("TARGET").unwrap_or_else(|_| "unknown".to_string());

    println!("cargo:rustc-env=PMPX_BUILD_RUSTC={version}");
    println!("cargo:rustc-env=PMPX_BUILD_TARGET={target}");
}