5

I have this file structure:

myProject
|
└── FolderToInclude
|          |
|          └── somebatfile.bat
|
└── src
|    |
|    └── main.rs
|
└── target
       |
       └── debug
             |
             └── myProject.exe // and other stuff

Is it possible in rust to include a folder in the build directory? I want to end up with this file structure:

myProject
|
└── FolderToInclude
|          |
|          └── somebatfile.bat
|
└── src
|    |
|    └── main.rs
|
└── target
       |
       └── debug
             |
             └── myProject.exe // and other stuff
             |
             └── FolderToInclude
                       |
                       └── somebatfile.bat
E_net4
  • 27,810
  • 13
  • 101
  • 139
roguemacro
  • 199
  • 2
  • 16
  • Why would you want that? – mcarton Aug 17 '19 at 11:36
  • I have a folder with a bat file that runs some executables, so I need to call it through the command line, so in the build it will not find it. – roguemacro Aug 17 '19 at 12:17
  • Sadly, no. You cannot do that with build scripts or cargo without any hacks. If you want this, you'll have to roll your own script, that both runs `cargo build`, and does everything else you want it to do, because build scripts allow to do things only before the build, mainly for compiling something like a C library... And there's no way to run code after the build finishes, because cargo is apparently not supposed to be a general build system for rust. I think we are regressing back to "too lazy" stage which is how languages like C++ with no defined way to do something exist. –  Aug 17 '19 at 12:22
  • Comment, too long, here's the source: https://github.com/rust-lang/cargo/issues/545 In other words, I'd suggest using your bash file to actually perform cargo build and whatever else you want it to do, or use make if you're a fan of that. –  Aug 17 '19 at 12:23
  • You can embed files into your executable though. See `std::include_bytes!`. – SOFe Aug 17 '19 at 13:55
  • @mcarton They want that because they use these files at runtime or need the user to have them. – tomer zeitune Aug 17 '19 at 15:36
  • Possible duplicate of https://stackoverflow.com/questions/31080757/copy-files-to-the-target-directory-after-build – tomer zeitune Aug 17 '19 at 15:55

1 Answers1

3

When cargo is compiling a project, it checks for a build script, which is just pure Rust. You can use this to wrangle C code or bundle assets. A number of helpful environment variables are available to this script. The following build.rs file should work for your use case:

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

const COPY_DIR: &'static str = "FolderToInclude";

/// A helper function for recursively copying a directory.
fn copy_dir<P, Q>(from: P, to: Q)
where
    P: AsRef<Path>,
    Q: AsRef<Path>,
{
    let to = to.as_ref().to_path_buf();

    for path in fs::read_dir(from).unwrap() {
        let path = path.unwrap().path();
        let to = to.clone().join(path.file_name().unwrap());

        if path.is_file() {
            fs::copy(&path, to).unwrap();
        } else if path.is_dir() {
            if !to.exists() {
                fs::create_dir(&to).unwrap();
            }

            copy_dir(&path, to);
        } else { /* Skip other content */
        }
    }
}

fn main() {
    // Request the output directory
    let out = env::var("PROFILE").unwrap();
    let out = PathBuf::from(format!("target/{}/{}", out, COPY_DIR));

    // If it is already in the output directory, delete it and start over
    if out.exists() {
        fs::remove_dir_all(&out).unwrap();
    }

    // Create the out directory
    fs::create_dir(&out).unwrap();

    // Copy the directory
    copy_dir(COPY_DIR, &out);
}
ouflak
  • 2,458
  • 10
  • 44
  • 49
TrickyPR
  • 128
  • 2
  • 9