use std::{env, path::Path, process::Command};
#[cfg(all(feature = "static", feature = "dylib"))]
compile_error!("static and dylib are mutually exclusive and cannot be enabled together");
fn main() {
build_library();
}
fn build_library() {
let manifest_path = env::var("CARGO_MANIFEST_DIR").expect("failed to get manifest directory");
let out_path = env::var("OUT_DIR").expect("missing OUT_DIR");
let manifest_path = Path::new(&manifest_path);
let out_path = Path::new(&out_path);
let project_path = manifest_path.join("lhm-bridge");
let intermediate_path = out_path.join("lhm-bridge-build");
let publish_path = out_path.join("lhm-bridge");
let nuget_path = out_path.join(".nuget");
let sdk_path = nuget_path
.join("runtime.win-x64.microsoft.dotnet.ilcompiler")
.join("7.0.0")
.join("sdk");
unsafe {
env::set_var("NUGET_PACKAGES", nuget_path.clone());
}
println!(
"cargo:rerun-if-changed={}",
project_path.join("src/FFI.cs").display()
);
println!(
"cargo:rerun-if-changed={}",
project_path.join("lhm-bridge.csproj").display()
);
let mut command = Command::new("dotnet");
command
.arg("publish")
.arg(project_path.join("lhm-bridge.csproj"))
.arg("-c")
.arg("Release")
.arg("-r")
.arg("win-x64")
.arg("-o")
.arg(&publish_path)
.arg(format!("/p:OutputPath={}/", publish_path.display()))
.arg(format!(
"/p:BaseIntermediateOutputPath={}/",
intermediate_path.display()
));
#[cfg(feature = "dylib")]
command.arg("/p:NativeLib=Shared");
#[cfg(feature = "static")]
command.arg("/p:NativeLib=Static");
let status = command
.status()
.expect("failed to run dotnet publish command");
if !status.success() {
panic!("failed to build binding library");
}
let native_path = publish_path.join("native");
println!("cargo:rustc-link-search=native={}", native_path.display());
println!("cargo:rustc-link-search=native={}", sdk_path.display());
#[cfg(feature = "dylib")]
link_dylib();
#[cfg(feature = "static")]
link_static();
}
#[cfg(feature = "dylib")]
fn link_dylib() {
println!("cargo:rustc-link-lib=dylib=lhm-bridge");
}
#[cfg(feature = "static")]
fn link_static() {
println!("cargo:rustc-link-lib=static=bootstrapperdll");
println!("cargo:rustc-link-lib=static=Runtime.WorkstationGC");
println!("cargo:rustc-link-lib=static=System.Globalization.Native.Aot");
println!("cargo:rustc-link-lib=static=System.IO.Compression.Native.Aot");
println!("cargo:rustc-link-lib=dylib=Iphlpapi");
println!("cargo:rustc-link-lib=dylib=Crypt32");
println!("cargo:rustc-link-lib=dylib=advapi32");
println!("cargo:rustc-link-lib=dylib=bcrypt");
println!("cargo:rustc-link-lib=dylib=ole32");
println!("cargo:rustc-link-lib=dylib=oleaut32");
println!("cargo:rustc-link-lib=static=lhm-bridge");
}