'rust crate cc is creating unwanted directories
I have a custom build.rs file, which is supposed to download a c++ libary from github and build it. My build.rs file is seperated into three parts:
- cloning the github repository
- compiling all the files using the crate
cc - linking the required libaries
a) If i comment out the second part, the cloning part works just fine and the linking part fails. (just like expected)
b) If i only comment out the cloning part, while already having cloned the repository from "a)" everything works just fine and the application runs.
c) If i delete the ./target directory and use the whole ./build.rs file without commenting anything out, the build fails while trying to clone the repository, with the following error: fatal: destination path '<path to my directory>\target\debug\build\rust_imgui-9999b756f624420c\out/imgui' already exists and is not an empty directory. Looking at the directory it is complaining about, i can see, that it contains a sub directory "backends". Uncommenting line 8 in ./build.rs doesn't change anything.
So the problem is the following:
Somehow the compiling part of the build script is causing the creation of of the directory .\target\debug\build\rust_imgui-9999b756f624420c\out/imgui/backends prior to its execution.
Edit: The creation of the unwanted directory is caused by the lines '''.file()'''.
If you now any way tho disable this behavior or maby a small workaround please leave a comment :)
./build.rs
use std::{process::Command, fs};
use cc;
fn main() {
let imgui_str = format!("{}{}", std::env::var("OUT_DIR").unwrap(), "/imgui/");
let imgui_path = imgui_str.as_str();
// fs::remove_dir_all("target/debug/build/rust_imgui-9999b756f624420c/out/imgui/backends").expect("deleting error");
//clone Dear ImGui
Command::new("git").args(["clone", "https://github.com/ocornut/imgui.git", "--branch", "docking", imgui_path]).spawn().expect("cloning failed");
Command::new("git").args(["-C", imgui_path, "pull",]).spawn().expect("pulling failed");
//compile Dear ImGui
cc::Build::new()
.cpp(true)
.include(format!("{}{}", imgui_path, ""))
.include(format!("{}{}", imgui_path, "backends"))
.include(format!("{}{}", imgui_path, "examples/libs/glfw/include"))
.file("src/gui/gui_lib.cpp")
.file(format!("{}{}", imgui_path, "imgui.cpp"))
.file(format!("{}{}", imgui_path, "imgui_draw.cpp"))
.file(format!("{}{}", imgui_path, "imgui_tables.cpp"))
.file(format!("{}{}", imgui_path, "imgui_widgets.cpp"))
.file(format!("{}{}", imgui_path, "backends/imgui_impl_opengl3.cpp"))
.file(format!("{}{}", imgui_path, "backends/imgui_impl_glfw.cpp"))
.file(format!("{}{}", imgui_path, "imgui_demo.cpp"))
.compile("gui_lib_cc");
//link everything
println!("cargo:rustc-link-lib=glfw3");
println!("{}", format!("cargo:rustc-link-search={}examples/libs/glfw/lib-vc2010-64", imgui_path));
println!("cargo:rustc-link-lib=gdi32");
println!("cargo:rustc-link-lib=opengl32");
println!("cargo:rustc-link-lib=shell32");
}
rust version: 1.59.0
os: Windows 10
Solution 1:[1]
The problem is, that the commands to clone the repository are started but not waited for to finish. Changing the .spawn() to .status() changes that.
Applying that change gets rid of the whole issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Taufiq Rahman |
