我怎样才能 运行 a command::new(...) 在 cargo clean 期间?

How can I run a command::new(...) during cargo clean?

虽然我正在学习 Rust,但我有一种粗略的方法来构建一个位于子模块中的非 Rust c/c++ 库。我的构建脚本 (build.rs) 现在看起来像:

use std::process::Command;

fn main() {

    // EXTERN_C
    // Build the c-library
    Command::new("make").args(&["-C", "cadd"]).status().unwrap();
    // The name of the library to link to, i.e. like: -l<lib>
    println!("cargo:rustc-link-lib=dylib=add_x64Linuxd");
    // The library search path for linking, i.e. like -L<path>
    println!("cargo:rustc-link-search=native=cadd/lib");
    // The run-time library search path (LD_LIBRARY_PATH)
    println!("cargo:rustc-env=LD_LIBRARY_PATH=cadd/lib");
}

这工作得很好,cadd/ 中的 makefile 整理出所有 build/re-build 依赖项等。我现在唯一不能做的是挂钩 make -C cadd clean 当我运行 cargo clean。理想情况下,我希望它同时 运行 干净的 make 目标。该命令如下所示:

    Command::new("make").args(&["-C", "cadd", "clean"]).status().unwrap();

但我不知道如何在 cargo clean 期间向 运行 发送这样的命令。是否有像“构建脚本”一样的“干净脚本”或其他方法?

最终我将开始学习如何将我的 makefile 项目打包到一个 cargo crate 中(我认为这是正确的术语)——所以我知道这不是最佳方式,但我想得到这首先以基本方式工作(所以我的头不会爆炸!)。

cargo clean命令只是删除了cargo目标目录。

一个解决方案是让您的 Makefile 将其编译工件(它生成的所有文件)输出到目标目录中。

您还可以通过 --target-dir CLI 选项或将以下内容添加到 .cargo/config:

来更改 cargo 输出其工件的目录
[build]
target-dir = "some/path"