Skip to content

Commit efb7cbe

Browse files
committed
Generate Hypervisor framework bindings
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
1 parent a0c38e0 commit efb7cbe

5 files changed

Lines changed: 59 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[workspace]
2+
members = [
3+
"hv-sys"
4+
]

hv-sys/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "hv-sys"
3+
version = "0.1.0"
4+
edition = "2018"
5+
description = "Low level Rust binding for Hypervisor Framework generated with bindgen"
6+
7+
[dependencies]
8+
9+
[build-dependencies]
10+
bindgen = "0.58"

hv-sys/build.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
use std::process::Command;
4+
5+
fn main() {
6+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
7+
8+
bindgen::builder()
9+
.header("wrapper.h")
10+
.clang_arg(format!("-F{}/System/Library/Frameworks", show_sdk_path())) // -F<directory> Add framework to the search path
11+
.allowlist_function("hv_.*")
12+
.generate()
13+
.expect("Failed to generate bindings")
14+
.write_to_file(out_path.join("bindings.rs"))
15+
.expect("Failed to write bindings file");
16+
}
17+
18+
/// Execute `xcrun --sdk macosx --show-sdk-path` to locate MacOS SDK
19+
fn show_sdk_path() -> String {
20+
let output = Command::new("xcrun")
21+
.arg("--sdk")
22+
.arg("macosx")
23+
.arg("--show-sdk-path")
24+
.output()
25+
.expect("Failed to execute xcrun");
26+
27+
if !output.stderr.is_empty() {
28+
panic!("ERROR: {}", String::from_utf8(output.stderr).unwrap());
29+
}
30+
31+
let mut path = output.stdout;
32+
33+
// Remove new line character ('\n' == 0x0A == 10)
34+
if path.ends_with(&[10]) {
35+
path.swap_remove(path.len() - 1);
36+
}
37+
38+
String::from_utf8(path).unwrap()
39+
}

hv-sys/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![allow(non_upper_case_globals)]
2+
#![allow(non_camel_case_types)]
3+
4+
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

hv-sys/wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "Hypervisor/hv.h"
2+
#include "Hypervisor/hv_vmx.h"

0 commit comments

Comments
 (0)