-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Find winmd files without reading
PATH
nor copying to target/
Non-Windows platforms - which are supported for cross-compiling - do not set the output directory in `PATH` nor use semicolons to separate this variable, resulting in errors Split out `winmd` "discovery" by emitting the path to these files in a build step, that is subsequently compiled into `windows_gen`. This is more efficient than `include_bytes!` which was used prior to the `PATH` system as no copies or binary includes are required at all. Copying of DLL targets to the `target/` dir for easy consumption and running of crates is still performed, but only on Windows for the same `PATH` reason. In the future, if a chain of crates is required to export `winmd` files for downstream crates to consume, this can be extended to also export their `$CARGO_MANIFEST_DIR/.winmd/windows` in such a way that the next crate can pick it up, again without copying any files around.
- Loading branch information
Showing
3 changed files
with
70 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,24 @@ | ||
#[cfg(windows)] | ||
fn main() { | ||
let mut source: ::std::path::PathBuf = ::std::env::var("CARGO_MANIFEST_DIR") | ||
.expect("No `CARGO_MANIFEST_DIR` env var") | ||
.into(); | ||
let path_file = | ||
::std::path::Path::new(&::std::env::var("OUT_DIR").expect("No `OUT_DIR` env var")) | ||
.join("winmd_path"); | ||
|
||
source.push(".windows"); | ||
source.push("winmd"); | ||
println!("cargo:rerun-if-changed={}", path_file.display()); | ||
println!("cargo:rerun-if-env-changed=CARGO_MANIFEST_DIR"); | ||
|
||
let destination = std::env::var("PATH").expect("No `PATH` env variable set"); | ||
let end = destination.find(';').expect("Path not ending in `;`"); | ||
let mut destination: std::path::PathBuf = destination[..end].into(); | ||
destination.pop(); | ||
destination.pop(); | ||
destination.push(".windows"); | ||
destination.push("winmd"); | ||
// Compute and store the absolute path to this crates' CARGO_MANIFEST_DIR, in which | ||
// winmd files reside. This allows dependent crates - which otherwise have no idea | ||
// about the existence of our source directory - to find and use these files. | ||
let source = ::std::path::Path::new( | ||
&::std::env::var("CARGO_MANIFEST_DIR").expect("No `CARGO_MANIFEST_DIR` env var"), | ||
) | ||
.join(".windows/winmd"); | ||
|
||
if let ::std::result::Result::Ok(files) = ::std::fs::read_dir(source) { | ||
for file in files.filter_map(|file| file.ok()) { | ||
if let ::std::result::Result::Ok(file_type) = file.file_type() { | ||
if file_type.is_file() { | ||
let path = file.path(); | ||
if let ::std::option::Option::Some(filename) = path.file_name() { | ||
let _ = std::fs::create_dir_all(&destination); | ||
destination.push(filename); | ||
let _ = ::std::fs::copy(path, &destination); | ||
destination.pop(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
::std::fs::write( | ||
path_file, | ||
source | ||
.to_str() | ||
.expect("CARGO_MANIFEST_DIR is not a valid UTF-8 string"), | ||
) | ||
.expect("Failed to write winmd path to src/winmd_path.rs"); | ||
} | ||
|
||
#[cfg(not(windows))] | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters