Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for windows plugins #4

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions qemu-plugin-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ publish.workspace = true
readme.workspace = true
repository.workspace = true
version.workspace = true

[build-dependencies]
anyhow = "1.0.75"

[lints.rust]
non_snake_case = "allow"
Comment on lines +17 to +18
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to stop it complaining about the generated code.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, nice!

35 changes: 35 additions & 0 deletions qemu-plugin-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{path::PathBuf, str::FromStr, env::var};
use anyhow::{anyhow, Result};

fn out_dir() -> Result<PathBuf> {
Ok(PathBuf::from(
var("OUT_DIR").map_err(|e| anyhow!("OUT_DIR not set: {e}"))?,
))
}
fn main() -> Result<()> {
#[cfg(windows)]
{
let out_dir = out_dir()?;
let def_file = PathBuf::from_str("src/qemu_plugin_api.def")?;
let def_file_str = def_file.to_string_lossy();
let lib_file = out_dir.join("qemu_plugin_api.lib");
let lib_file_str = lib_file.to_string_lossy();
let ch = std::process::Command::new("dlltool")
.args([
"--input-def",
&def_file_str,
"--output-delaylib",
&lib_file_str,
"--dllname",
"qemu.exe",
])
.spawn()?
.wait()?;
if !ch.success() {
return Err(anyhow!("dlltool failed"));
}
println!("cargo:rustc-link-search={}", out_dir.display());
println!("cargo:rustc-link-lib=qemu_plugin_api");
}
Ok(())
}
21 changes: 19 additions & 2 deletions qemu-plugin-sys/generate-bindings.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#!/usr/bin/env -S cargo +nightly -Z script
#!/usr/bin/env -S cargo +nightly-gnu -Z script

//! ```cargo
//! [package]
//! edition = "2021"
//! [dependencies]
//! anyhow = "*"
//! bindgen = "*"
//! cargo_metadata = "*"
//! reqwest = { version = "*", features = ["blocking"] }
//! tar = "*"
//! xz2 = "*"
//![lints.rust]
//!non_snake_case = "allow"
Comment on lines +13 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, to suppress warnings. It seems that this generate-bindings.rs script is intended to run standalone, but it compiles everything under src/ directory anyway. I wonder if there's a way to stop it doing that.

//! ```

use anyhow::{anyhow, Result};
Expand All @@ -25,7 +29,7 @@ use tar::Archive;
use xz2::read::XzDecoder;

const QEMU_SRC_URL_BASE: &str = "https://download.qemu.org/";
const QEMU_VERSION: &str = "8.1.3";
const QEMU_VERSION: &str = "8.2.0";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should really be done in a normal upgrade. Probably before this PR.


fn qemu_src_url() -> String {
format!("{}qemu-{}.tar.xz", QEMU_SRC_URL_BASE, QEMU_VERSION)
Expand Down Expand Up @@ -70,6 +74,14 @@ fn extract_txz(archive: &Path, destination: &Path) -> Result<()> {
})?;
Ok(())
}
fn generate_windows_delaylink_library(qemu_plugin_symbols: &Path, out_dir: &Path) -> Result<()> {
let def_file = out_dir.join("qemu_plugin_api.def");
let all_commands = std::fs::read_to_string(qemu_plugin_symbols)?;
let all_commands = all_commands.replace(|x| "{};".contains(x), "");
std::fs::write(&def_file, format!("EXPORTS\n{all_commands}"))?;

Ok(())
}
tocklime marked this conversation as resolved.
Show resolved Hide resolved

fn generate_bindings(qemu_plugin_header: &Path, destination: &Path) -> Result<()> {
let rust_bindings = builder()
Expand Down Expand Up @@ -139,6 +151,11 @@ fn main() -> Result<()> {
)?;
}

generate_windows_delaylink_library(
&src_dir.join("plugins").join("qemu-plugins.symbols"),
&out_dir,
)?;

generate_bindings(
&src_dir
.join("include")
Expand Down
Loading