Cargo: Add support for resource files

Created on 6 Apr 2018  路  5Comments  路  Source: rust-lang/cargo

This is similar to #2729, however, it's a bit different.

The main problem I currently have is that I cannot place configs near my executable. It's common practice in many languages and IDE's that you have some file that gets copied to output directory. For example, let's look at csproj configuration for C# project:

  <ItemGroup>
    <None Update="appsettings.json">
      <Generator>SettingsSingleFileGenerator</Generator>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Production.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

It literaly says "copy this file to output directory (wherever it is) if file in project is newer". It's common, it's convinient.

Currently in Rust i have following build.rs script:

use std::path::Path;
use std::{env, fs};


const SETTINGS_FILE: &str = "Settings.toml";
const LOG4RS_FILE: &str = "log4rs.toml";
fn main() {
    let target_dir_path = env::var("OUT_DIR").unwrap();
    copy(&target_dir_path, LOG4RS_FILE);
    copy(&target_dir_path, SETTINGS_FILE);
}

fn copy<S: AsRef<std::ffi::OsStr> + ?Sized, P: Copy + AsRef<Path>>(target_dir_path: &S, file_name: P) {
    fs::copy(file_name, Path::new(&target_dir_path).join("../../..").join(file_name)).unwrap();
}

It actually _does_ work, but it's not really convinient and we'd like ot have more declarative description.

We'd probably like to specify in cargo.toml as:

[resources]
preserve_newest = ["log4rs.toml", "Settings.toml"]

or even

[resources]
preserve_newest = {file = "log4rs.toml", target_path = "configs/log4rs/log4rs.toml"}

And be sure that when cargo build an application, resource files gets its place near executable (or at some relative path).

This feature should be limited to executables, so it doesn't work for libraries so we don't care about requiring copying these files transitively.

A-build-execution C-feature-request

Most helpful comment

Upvote. I need to include image files, data files, etc. with my application and would like to do so in a clean and standard way.

All 5 comments

Finally I'm not the only one on the whole rusternet with the Problem of copying associated files to binary output dir.

p.s. sorry for off-topic, but I lost whole day in hope that this problems must already been solved.

I would really like this as well

Upvote. I need to include image files, data files, etc. with my application and would like to do so in a clean and standard way.

This would allow us to take a step forward for the development of GUI apps in which resources are used most often.

I nice clean solution to add an icon/resource to our binaries would be great!

Was this page helpful?
0 / 5 - 0 ratings