Skip to content

Commit

Permalink
cppwinrt
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed May 23, 2024
1 parent 40d35fa commit 44b13aa
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
18 changes: 18 additions & 0 deletions crates/libs/cppwinrt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "cppwinrt"
version = "0.1.0"
authors = ["Microsoft"]
edition = "2021"
rust-version = "1.60"
license = "MIT OR Apache-2.0"
description = "C++/WinRT"
repository = "https://github.com/microsoft/windows-rs"
readme = "readme.md"
categories = ["os::windows-apis"]

[lints]
workspace = true

[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-msvc"
targets = []
Binary file added crates/libs/cppwinrt/cppwinrt.exe
Binary file not shown.
31 changes: 31 additions & 0 deletions crates/libs/cppwinrt/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## C++/WinRT

The [cppwinrt](https://crates.io/crates/cppwinrt) crate bundles the C++/WinRT compiler for use in Rust.

* [Getting started](https://kennykerr.ca/rust-getting-started/)
* [Samples](https://github.com/microsoft/windows-rs/tree/0.56.0/crates/samples) <!-- link to samples for upcoming release -->
* [Releases](https://github.com/microsoft/windows-rs/releases)

Start by adding the following to your Cargo.toml file:

```toml
[dependencies.cppwinrt]
version = "0.1"
```

Use `cppwinrt` function as needed:

```rust
fn main() {
match cppwinrt::cppwinrt(["-help"]) {
Ok(output) => println!("{output}"),
Err(error) => println!("{error}"),
};
}
```

Source:

* <https://github.com/microsoft/cppwinrt>
* <https://www.nuget.org/packages/microsoft.windows.cppwinrt>
* Version 2.0.240405.15
43 changes: 43 additions & 0 deletions crates/libs/cppwinrt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*!
Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
*/

const VERSION: &str = "2.0.240405.15";

/// Calls the C++/WinRT compiler with the given arguments.
///
/// Use `cppwinrt["-help"]` for available options.
pub fn cppwinrt<I, S>(args: I) -> Result<String, String>
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
let mut path = std::env::temp_dir();
path.push(format!("cppwinrt-{VERSION}.exe"));

std::fs::write(&path, std::include_bytes!("../cppwinrt.exe")).unwrap();
let mut command = std::process::Command::new(&path);
command.args(args);
let output = command.output().expect("failed to run cppwinrt");
_ = std::fs::remove_file(path);

if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).to_string())
} else {
Err(String::from_utf8_lossy(&output.stderr).to_string())
}
}

#[cfg(test)]
mod tests {
use crate::*;

#[test]
fn test() {
let ok = cppwinrt(["-help"]).unwrap();
assert!(ok.contains(VERSION), "unexpected version");

let err = cppwinrt(["-invalid"]).unwrap_err();
assert!(err.contains("'-invalid' is not supported"));
}
}
3 changes: 3 additions & 0 deletions crates/tests/readme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ path = "../../libs/registry"

[dev-dependencies.windows-version]
path = "../../libs/version"

[dev-dependencies.cppwinrt]
path = "../../libs/cppwinrt"
1 change: 1 addition & 0 deletions crates/tests/readme/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
#![doc = include_str!("../../../../crates/libs/targets/readme.md")]
#![doc = include_str!("../../../../crates/libs/version/readme.md")]
#![doc = include_str!("../../../../crates/libs/windows/readme.md")]
#![doc = include_str!("../../../../crates/libs/cppwinrt/readme.md")]

0 comments on commit 44b13aa

Please sign in to comment.