From b0b7b736fa33be1e363d20c134793082e42e7047 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sun, 14 Nov 2021 02:04:39 -0700 Subject: holy shit did something just fucking work --- Cargo.toml | 1 + build.rs | 101 ++---------------------------------------------- examples/hello-world.rs | 2 +- src/lib.rs | 2 +- src/wx-sys.cpp | 4 +- src/wx-sys.h | 2 +- 6 files changed, 9 insertions(+), 103 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9adf0f2..b2e599f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ anyhow = "1.0.44" cxx-build = "1.0" ureq = "2.3.0" url = "2.2.2" +vcpkg = "0.2.15" diff --git a/build.rs b/build.rs index adc18c6..381597f 100644 --- a/build.rs +++ b/build.rs @@ -1,59 +1,6 @@ use std::env; -use std::ffi::{OsStr, OsString}; -use std::fs; -use std::io; -use std::path::{Path, PathBuf}; -use anyhow::{anyhow, ensure, Result}; -use url::Url; - -// there's probably a crate for this somewhere -fn os_concat(a: impl AsRef, b: impl AsRef) -> OsString { - let mut result = OsString::from(&a); - result.push(b); - result -} - -fn out_dir() -> PathBuf { - PathBuf::from(env::var_os("OUT_DIR").expect("no OUT_DIR found")) -} - -fn download(url: &str) -> Result { - let url = Url::parse(url)?; - let file_name = url - .path_segments() - .and_then(|x| x.last()) - .ok_or_else(|| anyhow!("Weird URL downloaded"))?; - let out_path = out_dir().join(file_name); - if !out_path.exists() { - let mut body_reader = ureq::request_url("GET", &url).call()?.into_reader(); - let mut out_file = fs::File::create(&out_path)?; - io::copy(&mut body_reader, &mut out_file)?; - } - Ok(out_path) -} - -fn un7z(archive: PathBuf) -> Result<()> { - // WARNING: This code is horrifying and also bad. - // It should never be used by anyone. - // TODO port the 7zip LZMA SDK to Rust - use std::process::{Command, Stdio}; - let my_7z_exe = Path::new(r"C:\Program Files\7-Zip\7z.exe"); - ensure!(my_7z_exe.exists()); - let output_folder = archive - .parent() - .ok_or_else(|| anyhow!("archive with no parent"))?; - let output_folder_arg = os_concat("-o", &output_folder); - let extract_result = Command::new(my_7z_exe) - .args(["x", "-aos"]) - .arg(output_folder_arg) - .arg(&archive) - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .status()?; - ensure!(extract_result.success()); - Ok(()) -} +use anyhow::Result; fn main() -> Result<()> { println!("cargo:rerun-if-changed=build.rs"); @@ -65,49 +12,12 @@ fn main() -> Result<()> { } } -/* - - - - 1 - 1 - lib\vc14x_x64_dll - - - - - - 1 - 1 - lib\vc14x_x64_lib - - - -*/ - fn build_x64_windows_msvc() -> Result<()> { - let headers = download("https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.5/wxWidgets-3.1.5-headers.7z")?; - un7z(headers)?; - - // TODO make sure VS2015+ is actually the right thing - let libs = download("https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.5/wxMSW-3.1.5_vc14x_x64_Dev.7z")?; - un7z(libs)?; - - let out_dir = out_dir(); - - // TODO consider actually parsing wxwidgets.props at build time instead of doing it manually - // TODO find a static build instead bc fuck DLLs - let libs_folder = out_dir.join("lib/vc14x_x64_dll"); - let debug = env::var("PROFILE")? == "release"; - let wx_suffix = if debug { "u" } else { "ud" }; - - let mut include_dirs = vec![out_dir.join("include/msvc"), out_dir.join("include")]; - // TODO this is only in ResourceCompile in wxwidgets.props, is that bad - include_dirs.push(libs_folder.join(format!("msw{}", wx_suffix))); + let wx = vcpkg::find_package("wxwidgets").unwrap(); cxx_build::bridge("src/lib.rs") .file("src/wx-sys.cpp") - .includes(include_dirs) + .includes(wx.include_paths) .flag_if_supported("-Wno-invalid-token-paste") .define("__WXMSW__", None) .define("wxMSVC_VERSION_AUTO", None) @@ -131,10 +41,5 @@ fn build_x64_windows_msvc() -> Result<()> { // TODO dylib or static println!("cargo:rustc-link-lib={}", lib); } - // TODO is "native=" the right thing there (or display() for that matter) - println!("cargo:rustc-link-search=native={}", libs_folder.display()); - - // TODO which libs do we actually want to link - println!("cargo:rustc-link-lib=static=wxmsw31{}_core", wx_suffix); Ok(()) } diff --git a/examples/hello-world.rs b/examples/hello-world.rs index 9928016..835de22 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -1,5 +1,5 @@ use shit_wx_sys as wx; fn main() { - wx::message_box(); + wx::message_box("sample text"); } diff --git a/src/lib.rs b/src/lib.rs index 44af2fe..2fdd6f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ mod ffi { unsafe extern "C++" { include!("shit-wx-sys/src/wx-sys.h"); - fn message_box(); + fn message_box(message: &str); } } diff --git a/src/wx-sys.cpp b/src/wx-sys.cpp index 8e07962..229b018 100644 --- a/src/wx-sys.cpp +++ b/src/wx-sys.cpp @@ -1,6 +1,6 @@ #include "shit-wx-sys/src/wx-sys.h" #include -void message_box() { - wxMessageBox("Hello World!!!"); +void message_box(rust::Str message) { + wxMessageBox(std::string(message)); } diff --git a/src/wx-sys.h b/src/wx-sys.h index 68c4484..fa649f3 100644 --- a/src/wx-sys.h +++ b/src/wx-sys.h @@ -1,4 +1,4 @@ #include "rust/cxx.h" #include -void message_box(); +void message_box(rust::Str message); -- cgit v1.2.3