From 82f2431eb81c0654804852bf19b3363be5995bd6 Mon Sep 17 00:00:00 2001 From: gengteng Date: Tue, 22 Mar 2022 16:19:01 +0800 Subject: [PATCH] use vcpkg to find package --- Cargo.lock | 7 +++++ libs/scrap/Cargo.toml | 1 + libs/scrap/build.rs | 61 ++++++++++++++++++++----------------------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33bc04259..32b8edc38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3574,6 +3574,7 @@ dependencies = [ "serde 1.0.136", "target_build_utils", "tracing", + "vcpkg", "webm", "winapi 0.3.9", ] @@ -4163,6 +4164,12 @@ dependencies = [ "getrandom", ] +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "vec_map" version = "0.8.2" diff --git a/libs/scrap/Cargo.toml b/libs/scrap/Cargo.toml index aaee9b394..eb3faed24 100644 --- a/libs/scrap/Cargo.toml +++ b/libs/scrap/Cargo.toml @@ -33,6 +33,7 @@ quest = "0.3" [build-dependencies] target_build_utils = "0.3" bindgen = "0.59" +vcpkg = "0.2" [target.'cfg(target_os = "linux")'.dependencies] dbus = { version = "0.9", optional = true } diff --git a/libs/scrap/build.rs b/libs/scrap/build.rs index a2707d561..1f7aafe7e 100644 --- a/libs/scrap/build.rs +++ b/libs/scrap/build.rs @@ -4,40 +4,35 @@ use std::{ }; fn find_package(name: &str) -> Vec { - let vcpkg_root = std::env::var("VCPKG_ROOT").unwrap(); - let mut path: PathBuf = vcpkg_root.into(); - let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); - let mut target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); - if target_arch == "x86_64" { - target_arch = "x64".to_owned(); - } else if target_arch == "aarch64" { - target_arch = "arm64".to_owned(); + let library = vcpkg::find_package(name).expect("Failed to find package"); + println!("cargo:info={}", library.vcpkg_triplet); //TODO + let lib_name = name.trim_start_matches("lib").to_string(); + println!("{}", format!("cargo:rustc-link-lib=static={}", lib_name)); + + match ( + library.link_paths.as_slice(), + library.include_paths.as_slice(), + ) { + ([link_search, ..], [include, ..]) => { + println!( + "{}", + format!("cargo:rustc-link-search={}", link_search.display()) + ); + println!("{}", format!("cargo:include={}", include.display())); + } + _ => { + panic!( + "{}", + if library.link_paths.is_empty() { + "link path not found" + } else { + "include path not found" + } + ) + } } - let mut target = if target_os == "macos" { - "x64-osx".to_owned() - } else if target_os == "windows" { - "x64-windows-static".to_owned() - } else { - format!("{}-{}", target_arch, target_os) - }; - if target_arch == "x86" { - target = target.replace("x64", "x86"); - } - println!("cargo:info={}", target); - path.push("installed"); - path.push(target); - let lib = name.trim_start_matches("lib").to_string(); - println!("{}", format!("cargo:rustc-link-lib=static={}", lib)); - println!( - "{}", - format!( - "cargo:rustc-link-search={}", - path.join("lib").to_str().unwrap() - ) - ); - let include = path.join("include"); - println!("{}", format!("cargo:include={}", include.to_str().unwrap())); - vec![include] + + library.include_paths } fn generate_bindings(