From 95bec0c43f7adc7b8dae974a3427630fa34a66b1 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 5 Sep 2024 12:44:11 -0500 Subject: [PATCH] Improve interop testing (#3253) --- .gitignore | 1 - .../json_validator_winrt_client_cpp/build.rs | 5 ++- crates/tests/misc/noexcept/build.rs | 5 ++- .../winrt/constructors_client/Cargo.toml | 6 ++++ .../tests/winrt/constructors_client/build.rs | 25 +++++++++++++ .../winrt/constructors_client/src/interop.cpp | 35 +++++++++++++++++++ .../winrt/constructors_client/src/lib.rs | 7 ++++ 7 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 crates/tests/winrt/constructors_client/src/interop.cpp diff --git a/.gitignore b/.gitignore index cb705afd42..4c6fca839a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ /target *.lock *.winmd -**/src/winrt diff --git a/crates/samples/components/json_validator_winrt_client_cpp/build.rs b/crates/samples/components/json_validator_winrt_client_cpp/build.rs index 6293927b07..d8d3f5a9d8 100644 --- a/crates/samples/components/json_validator_winrt_client_cpp/build.rs +++ b/crates/samples/components/json_validator_winrt_client_cpp/build.rs @@ -6,12 +6,14 @@ fn main() { println!("cargo:rerun-if-changed=src/client.cpp"); println!("cargo:rustc-link-lib=windows.0.52.0"); + let include = std::env::var("OUT_DIR").unwrap(); + cppwinrt::cppwinrt([ "-in", "../json_validator_winrt/sample.winmd", &format!("{}\\System32\\WinMetadata", env!("windir")), "-out", - "src", + &include, ]) .unwrap(); @@ -20,5 +22,6 @@ fn main() { .std("c++20") .flag("/EHsc") .file("src/client.cpp") + .include(include) .compile("client"); } diff --git a/crates/tests/misc/noexcept/build.rs b/crates/tests/misc/noexcept/build.rs index 3cf9fbfe49..74b3fa8ad2 100644 --- a/crates/tests/misc/noexcept/build.rs +++ b/crates/tests/misc/noexcept/build.rs @@ -41,12 +41,14 @@ fn main() { panic!("{error}"); } + let include = std::env::var("OUT_DIR").unwrap(); + cppwinrt::cppwinrt([ "-in", "test.winmd", &format!("{}\\System32\\WinMetadata", env!("windir")), "-out", - "src", + &include, ]) .unwrap(); @@ -55,5 +57,6 @@ fn main() { .std("c++20") .flag("/EHsc") .file("src/interop.cpp") + .include(include) .compile("interop"); } diff --git a/crates/tests/winrt/constructors_client/Cargo.toml b/crates/tests/winrt/constructors_client/Cargo.toml index 960400e76f..91387cea02 100644 --- a/crates/tests/winrt/constructors_client/Cargo.toml +++ b/crates/tests/winrt/constructors_client/Cargo.toml @@ -26,3 +26,9 @@ features = [ # Cargo doesn't understand cdylib targets. https://github.com/rust-lang/cargo/issues/7825 [dependencies.test_constructors] path = "../constructors" + +[build-dependencies] +cc = "1.0" + +[build-dependencies.cppwinrt] +workspace = true diff --git a/crates/tests/winrt/constructors_client/build.rs b/crates/tests/winrt/constructors_client/build.rs index 01851a480c..f6765dbb7c 100644 --- a/crates/tests/winrt/constructors_client/build.rs +++ b/crates/tests/winrt/constructors_client/build.rs @@ -1,4 +1,10 @@ fn main() { + if !cfg!(target_env = "msvc") { + return; + } + + println!("cargo:rerun-if-changed=src/interop.cpp"); + windows_bindgen::bindgen([ "--in", "../constructors/metadata.winmd", @@ -10,4 +16,23 @@ fn main() { "no-bindgen-comment", ]) .unwrap(); + + let include = std::env::var("OUT_DIR").unwrap(); + + cppwinrt::cppwinrt([ + "-in", + "../constructors/metadata.winmd", + &format!("{}\\System32\\WinMetadata", env!("windir")), + "-out", + &include, + ]) + .unwrap(); + + cc::Build::new() + .cpp(true) + .std("c++20") + .flag("/EHsc") + .file("src/interop.cpp") + .include(include) + .compile("interop"); } diff --git a/crates/tests/winrt/constructors_client/src/interop.cpp b/crates/tests/winrt/constructors_client/src/interop.cpp new file mode 100644 index 0000000000..b594d1648d --- /dev/null +++ b/crates/tests/winrt/constructors_client/src/interop.cpp @@ -0,0 +1,35 @@ +#include +#include +#include "winrt/test_constructors.h" + +using namespace winrt; +using namespace test_constructors; + +void test() +{ + Activatable activatable; + assert(activatable.Property() == 0); + + Activatable activatable2(123); + assert(activatable2.Property() == 123); + + Composable composable; + assert(composable.Property() == 0); + + Composable composable2(456); + assert(composable2.Property() == 456); +} + +extern "C" +{ + HRESULT __stdcall interop() noexcept + try + { + test(); + return S_OK; + } + catch (...) + { + return to_hresult(); + } +} diff --git a/crates/tests/winrt/constructors_client/src/lib.rs b/crates/tests/winrt/constructors_client/src/lib.rs index 875d7f3903..5f1b02d2b6 100644 --- a/crates/tests/winrt/constructors_client/src/lib.rs +++ b/crates/tests/winrt/constructors_client/src/lib.rs @@ -1,11 +1,18 @@ +#![cfg(target_env = "msvc")] #![cfg(test)] mod bindings; use bindings::*; use windows::core::*; +extern "system" { + fn interop() -> HRESULT; +} + #[test] fn test() -> Result<()> { + unsafe { interop().ok()? }; + let activatable = Activatable::new()?; assert_eq!(activatable.Property()?, 0);