Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy files to the target directory after build

Let's assume I have a game with the following directory structure:

/src
/resources
Cargo.toml

I would like cargo build to copy the files in the resources directory and paste them in the same directory as the executable file.

I know it is possible to do this using a custom build script, but this seems to be a common case that deserves special treatment. So the question is: does cargo provide a standard way of copying files to the target directory (using just Cargo.toml)?

like image 765
aochagavia Avatar asked Jun 26 '15 19:06

aochagavia


People also ask

What is copy to output directory C#?

"Copy to Output Directory" is the property of the files within a Visual Studio project, which defines if the file will be copied to the project's built path as it is. Coping the file as it is allows us to use relative path to files within the project.

How do I copy a folder using Xcopy?

Type "xcopy", "source", "destination" /t /e in the Command Prompt window. Instead of “ source ,” type the path of the folder hierarchy you want to copy. Instead of “ destination ,” enter the path where you want to store the copied folder structure. Press “Enter” on your keyboard.


1 Answers

No, it doesn't.

You can move files around with build scripts, but these are run before your crate is built because their sole purpose is to prepare the environment (e.g. compile C libraries and shims).

If you think this is an important feature, you can open a feature request in Cargo issue tracker.

Alternatively, you can write a makefile or a shell script which would forward all arguments to cargo and then copy the directory manually:

#!/bin/bash

DIR="$(dirname "$0")"

if cargo "$@"; then
    [ -d "$DIR/target/debug" ] && cp -r "$DIR/resources" "$DIR/target/debug/resources"
    [ -d "$DIR/target/release" ] && cp -r "$DIR/resources" "$DIR/target/release/resources"
fi

Now you can run cargo like

% ./make.sh build
like image 121
Vladimir Matveev Avatar answered Sep 29 '22 09:09

Vladimir Matveev