构建完成后拷贝文件到目标目录 [英] Copy files to the target directory after build
问题描述
假设我有一个具有以下目录结构的游戏:
Let's assume I have a game with the following directory structure:
/src
/resources
Cargo.toml
我希望 cargo build
将 resources
目录中的文件复制到与可执行文件相同的目录中.
I would like cargo build
to copy the files in the resources
directory and paste them in the same directory as the executable file.
我知道可以使用自定义构建脚本来做到这一点,但这似乎是一个值得特殊对待的常见情况.所以问题是:cargo 是否提供了将文件复制到目标目录的标准方式(仅使用 Cargo.toml
)?
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
)?
推荐答案
不,没有.
您可以使用 构建脚本 移动文件,但这些在您的 crate 之前运行之所以构建,是因为它们的唯一目的是准备环境(例如编译 C 库和垫片).
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).
如果你认为这是一个重要的特性,你可以在 Cargo issue tracker/a>.
If you think this is an important feature, you can open a feature request in Cargo issue tracker.
或者,您可以编写一个 makefile 或 shell 脚本,将所有参数转发给货物,然后手动复制目录:
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
这篇关于构建完成后拷贝文件到目标目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!