From 3558a0a65cc5e6ae4df8ae4c1d95c6003d50af05 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Sun, 28 Nov 2021 23:51:41 +0100 Subject: Add integration tests for injector --- zluda_inject/build.rs | 68 ++++++++++++++++++++++++++++ zluda_inject/tests/helpers/direct_cuinit.rs | 9 ++++ zluda_inject/tests/helpers/nvcuda.lib | Bin 0 -> 71050 bytes zluda_inject/tests/inject.rs | 13 ++++++ 4 files changed, 90 insertions(+) create mode 100644 zluda_inject/build.rs create mode 100644 zluda_inject/tests/helpers/direct_cuinit.rs create mode 100644 zluda_inject/tests/helpers/nvcuda.lib create mode 100644 zluda_inject/tests/inject.rs diff --git a/zluda_inject/build.rs b/zluda_inject/build.rs new file mode 100644 index 0000000..c9146e0 --- /dev/null +++ b/zluda_inject/build.rs @@ -0,0 +1,68 @@ +use std::{ + env::{self, VarError}, + fs::{self, DirEntry}, + io, + path::{self, PathBuf}, + process::Command, +}; + +fn main() -> Result<(), VarError> { + println!("cargo:rerun-if-changed=build.rs"); + if env::var("PROFILE")? != "debug" { + return Ok(()); + } + let rustc_exe = env::var("RUSTC")?; + let out_dir = env::var("OUT_DIR")?; + let target = env::var("TARGET")?; + let is_msvc = env::var("CARGO_CFG_TARGET_ENV")? == "msvc"; + let opt_level = env::var("OPT_LEVEL")?; + let debug = str::parse::(env::var("DEBUG")?.as_str()).unwrap(); + let mut helpers_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?); + helpers_dir.push("tests"); + helpers_dir.push("helpers"); + let helpers_dir_as_string = helpers_dir.to_string_lossy(); + for rust_file in fs::read_dir(&helpers_dir).unwrap().filter_map(rust_file) { + let full_file_path = format!( + "{}{}{}", + helpers_dir_as_string, + path::MAIN_SEPARATOR, + rust_file + ); + println!("cargo:rerun-if-changed={}", full_file_path); + let mut rustc_cmd = Command::new(&*rustc_exe); + if debug { + rustc_cmd.arg("-g"); + } + rustc_cmd.arg(format!("-Lnative={}", helpers_dir_as_string)); + if !is_msvc { + // HACK ALERT + // I have no idea why the extra library below have to be linked + rustc_cmd.arg(r"-lucrt"); + } + rustc_cmd + .arg("-ldylib=nvcuda") + .arg("-C") + .arg(format!("opt-level={}", opt_level)) + .arg("--out-dir") + .arg(format!("{}", out_dir)) + .arg("--target") + .arg(format!("{}", target)) + .arg(full_file_path); + assert!(rustc_cmd.status().unwrap().success()); + } + println!("cargo:rustc-env=HELPERS_OUT_DIR={}", &out_dir); + Ok(()) +} + +fn rust_file(entry: io::Result) -> Option { + entry.ok().and_then(|e| { + let os_file_name = e.file_name(); + let file_name = os_file_name.to_string_lossy(); + let is_file = e.file_type().ok().map(|t| t.is_file()).unwrap_or(false); + if is_file && file_name.ends_with(".rs") { + Some(file_name.to_string()) + } else { + None + } + }) +} diff --git a/zluda_inject/tests/helpers/direct_cuinit.rs b/zluda_inject/tests/helpers/direct_cuinit.rs new file mode 100644 index 0000000..8341a60 --- /dev/null +++ b/zluda_inject/tests/helpers/direct_cuinit.rs @@ -0,0 +1,9 @@ +#![crate_type = "bin"] + +extern "system" { + fn cuInit(flags: u32) -> u32; +} + +fn main() { + unsafe { cuInit(0) }; +} diff --git a/zluda_inject/tests/helpers/nvcuda.lib b/zluda_inject/tests/helpers/nvcuda.lib new file mode 100644 index 0000000..b793c56 Binary files /dev/null and b/zluda_inject/tests/helpers/nvcuda.lib differ diff --git a/zluda_inject/tests/inject.rs b/zluda_inject/tests/inject.rs new file mode 100644 index 0000000..f63186a --- /dev/null +++ b/zluda_inject/tests/inject.rs @@ -0,0 +1,13 @@ +use std::{env, io, process::Command}; + +#[test] +fn direct_cuinit() -> io::Result<()> { + let helpers_dir = env!("HELPERS_OUT_DIR"); + let mut main_exe = Command::new(format!( + "{}{}direct_cuinit.exe", + helpers_dir, + std::path::MAIN_SEPARATOR + )); + assert!(main_exe.status()?.success()); + Ok(()) +} -- cgit v1.2.3