Cargo: Copy files to target directory after build, before run

Created on 27 Jun 2015  路  5Comments  路  Source: rust-lang/cargo

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

/src
/resources
Cargo.toml

I would like cargo build (and cargo run) to copy the files in the resources directory and paste them in the same directory as the executable file. This way I could run the game directly, without having to move things myself or using a custom script.

I think this situation can be pretty common for other projects as well and I have seen it implemented for other languages (for example, Visual Studio can do this for C# projects). Do you think it would be a good idea for Cargo?

Maybe something like

[resources]
path = [
    "resources_dir_1",
    "resources_dir_2"
]

Most helpful comment

It seems Cargo is designed to be "code-only" build system. IMO, such resource packaging are intended to be yielded to "outer" build system which uses output of Cargo.

Maybe ideal intended way to use resources is codifying and embedding them into executable binary. This disallows loading/unloading control, but simple.


I found one more reason for "copy resources" feature. Provisioning of testing data files. But I am not sure that this is really necessary at Cargo level.

All 5 comments

Would build scripts suffice for this? I feel like adding one-off features like this doesn't interoperate as well if we've got build scripts already which can perform arbitrary actions like this.

My primary concern is that many people will reinvent the wheel in a build script to achieve the same result. However, I realize now that there are good alternatives to this feature.

I think I am going to write a little library to copy files from a directory to the target directory. People could use it from their custom build script with a one-liner. With that in mind, I think this issue can be closed.

The biggest issue here is that if you have to do your own build script for this, you're going to have to track files that changed yourself, which is an annoyance considering what cargo wants to achieve, or you're going to just prolong your build times because of files being moved around all the time...

It seems Cargo is designed to be "code-only" build system. IMO, such resource packaging are intended to be yielded to "outer" build system which uses output of Cargo.

Maybe ideal intended way to use resources is codifying and embedding them into executable binary. This disallows loading/unloading control, but simple.


I found one more reason for "copy resources" feature. Provisioning of testing data files. But I am not sure that this is really necessary at Cargo level.

@aochagavia What library is this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sdroege picture sdroege  路  3Comments

ehuss picture ehuss  路  3Comments

alilleybrinker picture alilleybrinker  路  3Comments

japaric picture japaric  路  3Comments

aturon picture aturon  路  3Comments