diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 8024a15..a5937f3 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -63,7 +63,7 @@ jobs: strategy: fail-fast: false matrix: - msrv: [1.64] + msrv: [1.68] steps: - uses: actions/checkout@v4 with: diff --git a/windivert-sys/Cargo.toml b/windivert-sys/Cargo.toml index e76457a..96aa3c2 100644 --- a/windivert-sys/Cargo.toml +++ b/windivert-sys/Cargo.toml @@ -28,13 +28,11 @@ thiserror = "1" [dependencies.windows] version = "0.48" -features = [ - "Win32_Foundation", - "Win32_System_IO", -] +features = ["Win32_Foundation", "Win32_System_IO"] [build-dependencies] +path_macro2 = "0.1.3" cc = "1.0" [package.metadata.docs.rs] -default-target = "x86_64-pc-windows-msvc" \ No newline at end of file +default-target = "x86_64-pc-windows-msvc" diff --git a/windivert-sys/build/compile/gnu.rs b/windivert-sys/build/compile/gnu.rs index bbe6315..9f7d6a6 100644 --- a/windivert-sys/build/compile/gnu.rs +++ b/windivert-sys/build/compile/gnu.rs @@ -1,4 +1,5 @@ use cc::Build; +use path_macro2::{path, path_const}; use std::process::{Command, Stdio}; use std::{env, fs}; @@ -8,8 +9,8 @@ pub fn lib() { build .out_dir(&out_dir) - .include(r#"vendor\include"#) - .file(r#"vendor\dll\windivert.c"#); + .include(path_const!(vendor / include)) + .file(path_const!(vendor / dll / windivert.c)); build.compile("WinDivert"); } @@ -28,16 +29,16 @@ pub fn dll() { }; cmd.args(DYNAMIC_C_OPTIONS); cmd.arg(format!("-Wl,--entry=${mangle}WinDivertDllEntry")); - cmd.args(["-c", "vendor/dll/windivert.c"]); - cmd.args(["-o", &format!("{out_dir}/WinDivert.o")]); + cmd.args(["-c", path_const!(vendor / dll / windivert.c)]); + cmd.args(["-o", &path!({ out_dir } / WinDivert.o).to_string_lossy()]); cmd.output().expect("Error compiling windivert c library"); let mut cmd = Build::new().get_compiler().to_command(); cmd.args(DYNAMIC_C_OPTIONS); - cmd.args(["-o", &format!("{out_dir}/WinDivert.dll")]); + cmd.args(["-o", &path!({ out_dir } / WinDivert.dll).to_string_lossy()]); cmd.args([ - &format!("{out_dir}/WinDivert.o"), - "vendor/dll/windivert.def", + &path!({ out_dir } / WinDivert.o).to_string_lossy(), + path_const!(vendor / dll / windivert.def), "-nostdlib", ]); cmd.args(DYNAMIC_C_INCLUDES); @@ -52,7 +53,7 @@ pub fn dll() { let mut strip = Command::new(strip); strip.stdout(Stdio::inherit()).stderr(Stdio::inherit()); - strip.arg(&format!("{out_dir}/WinDivert.dll")); + strip.arg(path!({ out_dir } / WinDivert.dll)); let _ = strip.output().expect("Error striping windivert dll"); let dlltool = Build::new() @@ -62,13 +63,19 @@ pub fn dll() { .replace("gcc", "dlltool"); let mut dlltool = Command::new(dlltool); dlltool.stdout(Stdio::inherit()).stderr(Stdio::inherit()); - - dlltool.args(["--dllname", &format!("{out_dir}/WinDivert.dll")]); - dlltool.args(["--def", "vendor/dll/windivert.def"]); - dlltool.args(["--output-lib", &format!("{out_dir}/WinDivert.lib")]); + dlltool.args([ + "--dllname", + &path!({ out_dir } / WinDivert.dll).to_string_lossy(), + ]); + dlltool.args(["--def", path_const!(vendor / dll / windivert.def)]); + dlltool.args([ + "--output-lib", + &path!({ out_dir } / WinDivert.lib).to_string_lossy(), + ]); let _ = dlltool.output().expect("Error building windivert lib"); - let _ = fs::remove_file(format!("{out_dir}/WinDivert.o")); + // Clean up object file + let _ = fs::remove_file(path!({ out_dir } / WinDivert.o)); } const DYNAMIC_C_OPTIONS: &[&str] = &[ @@ -77,7 +84,7 @@ const DYNAMIC_C_OPTIONS: &[&str] = &[ r#"-Wall"#, r#"-Wno-pointer-to-int-cast"#, r#"-Os"#, - r#"-Ivendor/include/"#, + concat!("-I", path_const!(vendor / include /)), r#"-Wl,--enable-stdcall-fixup"#, ]; diff --git a/windivert-sys/build/compile/msvc.rs b/windivert-sys/build/compile/msvc.rs index 950f300..752b3b8 100644 --- a/windivert-sys/build/compile/msvc.rs +++ b/windivert-sys/build/compile/msvc.rs @@ -1,4 +1,5 @@ use cc::Build; +use path_macro2::{path, path_const}; use std::{env, fs}; use crate::DLL_OUTPUT_PATH_ARG; @@ -9,8 +10,8 @@ pub fn lib() { build .out_dir(&out_dir) - .include(r#"vendor\include"#) - .file(r#"vendor\dll\windivert.c"#); + .include(path_const!(vendor / include)) + .file(path_const!(vendor / dll / windivert.c)); for &flag in STATIC_CL_ARGS { build.flag(flag); @@ -34,9 +35,18 @@ pub fn dll() { compiler.arg(&format!("/MACHINE:{arch}")); - compiler.arg(&format!(r#"/PDB:{out_dir}\WinDivertDll.pdb"#)); - compiler.arg(&format!(r#"/OUT:{out_dir}\WinDivert.dll"#)); - compiler.arg(&format!(r#"/IMPLIB:{out_dir}\WinDivert.lib"#)); + compiler.arg(&format!( + "/PDB:{}", + &path!({ out_dir } / WinDivertDll.pdb).to_string_lossy() + )); + compiler.arg(&format!( + "/OUT:{}", + &path!({ out_dir } / WinDivert.dll).to_string_lossy() + )); + compiler.arg(&format!( + "/IMPLIB:{}", + &path!({ out_dir } / WinDivert.lib).to_string_lossy() + )); if let Ok(out) = compiler.output() { if !out.status.success() { @@ -52,12 +62,12 @@ pub fn dll() { if let Ok(dylib_save_dir) = env::var(DLL_OUTPUT_PATH_ARG) { let _ = fs::copy( - format!(r#"{out_dir}\WinDivert.dll"#), - format!(r#"{dylib_save_dir}\WinDivert.dll"#), + path!({ out_dir } / WinDivert.dll), + path!({ dylib_save_dir } / WinDivert.dll), ); let _ = fs::copy( - format!(r#"{out_dir}\WinDivert.lib"#), - format!(r#"{dylib_save_dir}\WinDivert.lib"#), + path!({ out_dir } / WinDivert.lib), + path!({ dylib_save_dir } / WinDivert.lib), ); } else { println!("cargo:warning=Environment variable {DLL_OUTPUT_PATH_ARG} not found, compiled dll & lib files will be stored on {out_dir}"); @@ -65,7 +75,7 @@ pub fn dll() { } const DYNAMIC_CL_ARGS: &[&str] = &[ - r#"/Ivendor\include"#, + concat!("/I", path_const!(vendor / include)), r#"/ZI"#, r#"/JMC"#, r#"/nologo"#, @@ -86,7 +96,7 @@ const DYNAMIC_CL_ARGS: &[&str] = &[ r#"/TC"#, r#"/FC"#, r#"/errorReport:queue"#, - r#"vendor\dll\windivert.c"#, + path_const!(vendor / dll / windivert.c), r#"/link"#, r#"/ERRORREPORT:QUEUE"#, r#"/INCREMENTAL"#, @@ -94,7 +104,7 @@ const DYNAMIC_CL_ARGS: &[&str] = &[ r#"kernel32.lib"#, r#"advapi32.lib"#, r#"/NODEFAULTLIB"#, - r#"/DEF:vendor/dll/windivert.def"#, + concat!("/DEF:", path_const!(vendor / dll / windivert.def)), r#"/MANIFEST"#, r#"/manifest:embed"#, r#"/DEBUG:FASTLINK"#, diff --git a/windivert-sys/build/main.rs b/windivert-sys/build/main.rs index 21468c5..641d0a7 100644 --- a/windivert-sys/build/main.rs +++ b/windivert-sys/build/main.rs @@ -1,5 +1,6 @@ mod compile; +use path_macro2::path; use std::{env, fs}; pub const LIB_PATH_ARG: &str = "WINDIVERT_PATH"; @@ -26,6 +27,14 @@ fn main() { // Prioritize environment variables over feature flags if env::var(STATIC_BUILD_ARG).is_ok() || cfg!(feature = "static") { println!("cargo:rerun-if-changed=wrapper.h"); + + // pull the git submodule + // ensure the submodule exists + std::process::Command::new("git") + .args(["submodule", "update", "--init"]) + .output() + .expect("Failed to pull the git submodule"); + compile::lib(); println!( @@ -55,14 +64,17 @@ fn handle_provided_dll(arch: &str, out_dir: &str, lib_path: &str) { if let Some(name) = file.file_name().to_str() { match name { "WinDivert.dll" | "WinDivert.lib" | "WinDivert32.sys" | "WinDivert64.sys" => { - let _ = fs::copy(file.path(), format!("{out_dir}/{name}")); + let src = file.path(); + let dst = path!({ out_dir } / { name }); + let _ = fs::copy(src, dst); } _ => {} } } } - if fs::metadata(format!("{lib_path}\\WinDivert{arch}.sys")).is_err() { + let sys_file = std::path::PathBuf::from(lib_path).join(format!("WinDivert{arch}.sys")); + if fs::metadata(&sys_file).is_err() { println!("cargo:warning=WinDivert{arch}.sys not found on the same directory as the dll.") } }