0

While I am learning rust, I have a crude means of building a non-rust c/c++ library that lives in a submodule. My build script (build.rs) now looks like:

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");
}

This is working nicely, the makefile within cadd/ sorts out any build/re-build deps etc. The only thing I can't do at the moment is hook in make -C cadd clean when I run cargo clean. Ideally I would like it to run the clean make target at the same time. The command would look like:

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

But I don't know how to get such a command to run during cargo clean. Is there a "clean script" like there is a "build script" - or another method?

Eventually I will get around to learning how to wrap up my makefile project into a cargo crate (I think that's the right terminology) - so I know this is not the optimal way to do this, but I want to get this working in a basic way first (so my head does not explode!).

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • generated file are expected to be in `CARGO_TARGET_DIR` directory https://doc.rust-lang.org/cargo/reference/config.html#buildtarget-dir – Stargateur May 10 '21 at 10:08
  • Does this answer your question? [Out of source builds (external build directory) with Cargo?](https://stackoverflow.com/questions/41274901/out-of-source-builds-external-build-directory-with-cargo) – Stargateur May 10 '21 at 10:11
  • I don't think this is a good dup, more or a "see also". In particular it mentions nothing about `cargo clean` or what `cargo clean` does (merely removing the target folder). – mcarton May 11 '21 at 09:20

1 Answers1

1

The cargo clean command simply deletes the cargo target directory.

One solution is for your Makefile to output its compilation artifacts(all files it generates) into the target directory.

You could also change the directory cargo outputs its artifacts to, either via the --target-dir CLI option or by adding the following to the .cargo/config:

[build]
target-dir = "some/path"
Misty
  • 416
  • 3
  • 8
  • ah interesting - I did not realise it was as simple as it just deleting the target dir. This adds weight to wrapping my c/c++ lib in a cargo crate - then I can use `cagro clean-recursive`. Thanks - not quite the answer I was hoping for, but its clear what I have to do. – code_fodder May 11 '21 at 06:18