From 8ae13659e3375c35982f490b14789f1f09f4997a Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 19 Aug 2021 12:29:55 -0700 Subject: [PATCH] workspace --- .../winmd/Microsoft.Web.WebView2.winmd | Bin .../winmd/TestComponent.winmd | Bin .../x64/TestComponent.dll | Bin .../x64/WebView2LoaderStatic.lib | Bin .../x86/TestComponent.dll | Bin .../x86/WebView2LoaderStatic.lib | Bin crates/gen/build.rs | 36 ------- crates/macros/src/lib.rs | 13 +-- .../default}/Windows.Win32.winmd | Bin .../default}/Windows.WinRT.winmd | Bin .../winmd => reader/default}/readme.md | 0 crates/reader/src/type_reader.rs | 2 +- crates/reader/src/workspace.rs | 89 ++++++++---------- 13 files changed, 44 insertions(+), 96 deletions(-) rename {tests/static_lib/.windows => .windows}/winmd/Microsoft.Web.WebView2.winmd (100%) rename {tests/winrt/.windows => .windows}/winmd/TestComponent.winmd (100%) rename {tests/winrt/.windows => .windows}/x64/TestComponent.dll (100%) rename {tests/static_lib/.windows => .windows}/x64/WebView2LoaderStatic.lib (100%) rename {tests/winrt/.windows => .windows}/x86/TestComponent.dll (100%) rename {tests/static_lib/.windows => .windows}/x86/WebView2LoaderStatic.lib (100%) delete mode 100644 crates/gen/build.rs rename crates/{gen/.windows/winmd => reader/default}/Windows.Win32.winmd (100%) rename crates/{gen/.windows/winmd => reader/default}/Windows.WinRT.winmd (100%) rename crates/{gen/.windows/winmd => reader/default}/readme.md (100%) diff --git a/tests/static_lib/.windows/winmd/Microsoft.Web.WebView2.winmd b/.windows/winmd/Microsoft.Web.WebView2.winmd similarity index 100% rename from tests/static_lib/.windows/winmd/Microsoft.Web.WebView2.winmd rename to .windows/winmd/Microsoft.Web.WebView2.winmd diff --git a/tests/winrt/.windows/winmd/TestComponent.winmd b/.windows/winmd/TestComponent.winmd similarity index 100% rename from tests/winrt/.windows/winmd/TestComponent.winmd rename to .windows/winmd/TestComponent.winmd diff --git a/tests/winrt/.windows/x64/TestComponent.dll b/.windows/x64/TestComponent.dll similarity index 100% rename from tests/winrt/.windows/x64/TestComponent.dll rename to .windows/x64/TestComponent.dll diff --git a/tests/static_lib/.windows/x64/WebView2LoaderStatic.lib b/.windows/x64/WebView2LoaderStatic.lib similarity index 100% rename from tests/static_lib/.windows/x64/WebView2LoaderStatic.lib rename to .windows/x64/WebView2LoaderStatic.lib diff --git a/tests/winrt/.windows/x86/TestComponent.dll b/.windows/x86/TestComponent.dll similarity index 100% rename from tests/winrt/.windows/x86/TestComponent.dll rename to .windows/x86/TestComponent.dll diff --git a/tests/static_lib/.windows/x86/WebView2LoaderStatic.lib b/.windows/x86/WebView2LoaderStatic.lib similarity index 100% rename from tests/static_lib/.windows/x86/WebView2LoaderStatic.lib rename to .windows/x86/WebView2LoaderStatic.lib diff --git a/crates/gen/build.rs b/crates/gen/build.rs deleted file mode 100644 index 45e9c8be5a..0000000000 --- a/crates/gen/build.rs +++ /dev/null @@ -1,36 +0,0 @@ -#[cfg(windows)] -fn main() { - let mut source: ::std::path::PathBuf = ::std::env::var("CARGO_MANIFEST_DIR") - .expect("No `CARGO_MANIFEST_DIR` env var") - .into(); - - source.push(".windows"); - source.push("winmd"); - - 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"); - - 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(); - } - } - } - } - } -} - -#[cfg(not(windows))] -fn main() {} diff --git a/crates/macros/src/lib.rs b/crates/macros/src/lib.rs index 89e9900c7c..eff2c86916 100644 --- a/crates/macros/src/lib.rs +++ b/crates/macros/src/lib.rs @@ -46,9 +46,8 @@ impl ToTokens for RawString { pub fn build(stream: proc_macro::TokenStream) -> proc_macro::TokenStream { let build = parse_macro_input!(stream as BuildMacro); let tokens = RawString(build.to_tokens_string()); - let target_dir = std::env::var("PATH").expect("No `PATH` env variable set"); - let end = target_dir.find(';').expect("Path not ending in `;`"); - let target_dir = RawString(target_dir[..end].to_string()); + let target_dir = RawString(target_dir()); + let workspace_dir = RawString(workspace_dir()); let tokens = quote! { { @@ -107,7 +106,7 @@ pub fn build(stream: proc_macro::TokenStream) -> proc_macro::TokenStream { } } - let mut source : ::std::path::PathBuf = ::std::env::var("CARGO_MANIFEST_DIR").expect("No `CARGO_MANIFEST_DIR` env variable set").into(); + let mut source : ::std::path::PathBuf = #workspace_dir.into(); source.push(".windows"); if source.exists() { @@ -132,12 +131,6 @@ pub fn build(stream: proc_macro::TokenStream) -> proc_macro::TokenStream { let profile = ::std::env::var("PROFILE").expect("No `PROFILE` env variable set"); copy_to_profile(&source, &destination, &profile); - - destination.push(".windows"); - destination.push("winmd"); - source.pop(); - source.push("winmd"); - copy(&source, &mut destination); } } }; diff --git a/crates/gen/.windows/winmd/Windows.Win32.winmd b/crates/reader/default/Windows.Win32.winmd similarity index 100% rename from crates/gen/.windows/winmd/Windows.Win32.winmd rename to crates/reader/default/Windows.Win32.winmd diff --git a/crates/gen/.windows/winmd/Windows.WinRT.winmd b/crates/reader/default/Windows.WinRT.winmd similarity index 100% rename from crates/gen/.windows/winmd/Windows.WinRT.winmd rename to crates/reader/default/Windows.WinRT.winmd diff --git a/crates/gen/.windows/winmd/readme.md b/crates/reader/default/readme.md similarity index 100% rename from crates/gen/.windows/winmd/readme.md rename to crates/reader/default/readme.md diff --git a/crates/reader/src/type_reader.rs b/crates/reader/src/type_reader.rs index 5507059ae5..f64fbe820e 100644 --- a/crates/reader/src/type_reader.rs +++ b/crates/reader/src/type_reader.rs @@ -34,7 +34,7 @@ impl TypeReader { /// /// This function panics if the if the files where the windows metadata are stored cannot be read. fn new() -> Self { - let files = crate_winmds(); + let files = workspace_winmds(); let mut nested = HashMap::>::new(); let mut types = TypeTree::from_namespace(""); types.include = true; diff --git a/crates/reader/src/workspace.rs b/crates/reader/src/workspace.rs index f88e179466..070e277879 100644 --- a/crates/reader/src/workspace.rs +++ b/crates/reader/src/workspace.rs @@ -1,41 +1,12 @@ use super::*; -pub fn crate_winmds() -> &'static [File] { +pub fn workspace_winmds() -> &'static [File] { use std::{mem::MaybeUninit, sync::Once}; static ONCE: Once = Once::new(); static mut VALUE: MaybeUninit> = MaybeUninit::uninit(); - ONCE.call_once(|| { - // This is safe because `Once` provides thread-safe one-time initialization - unsafe { VALUE = MaybeUninit::new(get_crate_winmds()) } - }); - - // This is safe because `call_once` has already been called. - unsafe { &*VALUE.as_ptr() } -} - -fn cargo_metadata() -> &'static str { - use std::{mem::MaybeUninit, sync::Once}; - static ONCE: Once = Once::new(); - static mut VALUE: MaybeUninit = MaybeUninit::uninit(); - - ONCE.call_once(|| { - let output = std::process::Command::new(env!("CARGO")) - .arg("metadata") - .arg("--format-version=1") - .arg("--no-deps") - .arg("--offline") - .output() - .expect("Failed to run `cargo metadata`"); + ONCE.call_once(|| unsafe { VALUE = MaybeUninit::new(get_workspace_winmds()) }); - unsafe { - VALUE = MaybeUninit::new( - String::from_utf8(output.stdout).expect("Cargo metadata is not utf-8"), - ) - } - }); - - // This is safe because `call_once` has already been called. unsafe { &*VALUE.as_ptr() } } @@ -56,7 +27,7 @@ pub fn workspace_dir() -> String { json[beginning_index..beginning_index + ending_index].replace("\\\\", "\\") } -fn target_dir() -> String { +pub fn target_dir() -> String { const JSON_KEY: &str = r#""target_directory":"#; let json = cargo_metadata(); @@ -73,7 +44,32 @@ fn target_dir() -> String { json[beginning_index..beginning_index + ending_index].replace("\\\\", "\\") } -fn get_crate_winmds() -> Vec { +fn cargo_metadata() -> &'static str { + use std::{mem::MaybeUninit, sync::Once}; + static ONCE: Once = Once::new(); + static mut VALUE: MaybeUninit = MaybeUninit::uninit(); + + ONCE.call_once(|| { + let output = std::process::Command::new(env!("CARGO")) + .arg("metadata") + .arg("--format-version=1") + .arg("--no-deps") + .arg("--offline") + .output() + .expect("Failed to run `cargo metadata`"); + + unsafe { + VALUE = MaybeUninit::new( + String::from_utf8(output.stdout).expect("Cargo metadata is not utf-8"), + ) + } + }); + + // This is safe because `call_once` has already been called. + unsafe { &*VALUE.as_ptr() } +} + +fn get_workspace_winmds() -> Vec { fn push_dir(result: &mut Vec, dir: &std::path::Path) { if let Ok(files) = std::fs::read_dir(&dir) { for file in files.filter_map(|file| file.ok()) { @@ -93,26 +89,21 @@ fn get_crate_winmds() -> Vec { let mut result = vec![]; - if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") { - let mut dir: std::path::PathBuf = dir.into(); - dir.push(".windows"); - dir.push("winmd"); - push_dir(&mut result, &dir); - } - - let dir = std::env::var("PATH").expect("No `PATH` env variable set"); - let end = dir.find(';').expect("Path not ending in `;`"); - let mut dir: std::path::PathBuf = dir[..end].into(); - dir.pop(); - dir.pop(); + let mut dir: std::path::PathBuf = workspace_dir().into(); dir.push(".windows"); dir.push("winmd"); push_dir(&mut result, &dir); - let mut dir: std::path::PathBuf = target_dir().into(); - dir.push(".windows"); - dir.push("winmd"); - push_dir(&mut result, &dir); + if !result.iter().any(|file| file.name.starts_with("Windows.")) { + result.push(File::from_bytes( + "Windows.Win32.winmd".to_string(), + include_bytes!("../default/Windows.Win32.winmd").to_vec(), + )); + result.push(File::from_bytes( + "Windows.WinRT.winmd".to_string(), + include_bytes!("../default/Windows.WinRT.winmd").to_vec(), + )); + } result }