samwoodhams / copy_to_output

A small rust library to copy files/folders from the project directory to the output directory
Other
15 stars 5 forks source link

Handle cross complation targets #3

Closed themadcreator closed 10 months ago

themadcreator commented 1 year ago

When cross compiling, the target output directory looks something like target/x86_64-pc-windows-msvc/release or target/{target}/{profile}

Currently this library only considers the PROFILE env var.

daym commented 1 year ago

When doing this, it would also be nice to introduce yet another function which still doesn't put the file into target/{target}/{profile} but rather still puts the file into target/{profile} as before (intended for host tools) even when cross compiling. ({profile} being the build type, really)

LittleBoxOfSunshine commented 10 months ago

FWIW, I needed this supported for work and just forked the implementation to be:

pub fn copy_to_output_for_build_type(path: &str, build_type: &str) -> Result<()> {
    let mut options = CopyOptions::new();
    let mut out_path = get_project_root()?;
    out_path.push("target");

    // TODO: This is a hack, ideally, we would plug into https://docs.rs/cargo/latest/cargo/core/compiler/enum.CompileKind.html
    let triple = build_target::target_triple().unwrap();
    if env::var_os("OUT_DIR").unwrap().to_str().unwrap().contains(&triple) {
        out_path.push(triple)
    }

    out_path.push(build_type);

    // Overwrite existing files with same name
    options.overwrite = true;
    options.copy_inside = true;

    copy_items(&[path], &out_path, &options)?;

    Ok(())
}

This works by inspecting to see if the target appears in the path, which is the hacky way of seeing which compilekind cargo is running with. Ideally it would hook into the cargo library, but looking into it I didn't see an immediately obvious path. Might upgrade the hack later, but it met my needs as is for now.

Note it also uses project_root::get_project_root() which is an option to address #6 , similar to what another commenter mentioned there.

I need to use the forked version for work for other reasons as well, but happy to contribute it back upstream if this is still maintained / isn't too hacky for others.

prenwyn commented 10 months ago

Looks like a good solution that will close both issues, and I'm happy to merge it to the main branch if you submit a pull request. Thanks very much for the help.

LittleBoxOfSunshine commented 10 months ago

Will do :) I'll clean it up a bit first

prenwyn commented 10 months ago

Closed in pr #8 by @LittleBoxOfSunshine. Will look into using get_project_root and rewriting the tests appropriately