Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
strategy:
fail-fast: false
matrix:
msrv: [1.64]
msrv: [1.68]
steps:
- uses: actions/checkout@v4
with:
Expand Down
8 changes: 3 additions & 5 deletions windivert-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
default-target = "x86_64-pc-windows-msvc"
35 changes: 21 additions & 14 deletions windivert-sys/build/compile/gnu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cc::Build;
use path_macro2::{path, path_const};
use std::process::{Command, Stdio};
use std::{env, fs};

Expand All @@ -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");
}
Expand All @@ -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);
Expand All @@ -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()
Expand All @@ -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] = &[
Expand All @@ -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"#,
];

Expand Down
34 changes: 22 additions & 12 deletions windivert-sys/build/compile/msvc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cc::Build;
use path_macro2::{path, path_const};
use std::{env, fs};

use crate::DLL_OUTPUT_PATH_ARG;
Expand All @@ -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);
Expand All @@ -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() {
Expand All @@ -52,20 +62,20 @@ 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}");
};
}

const DYNAMIC_CL_ARGS: &[&str] = &[
r#"/Ivendor\include"#,
concat!("/I", path_const!(vendor / include)),
r#"/ZI"#,
r#"/JMC"#,
r#"/nologo"#,
Expand All @@ -86,15 +96,15 @@ 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"#,
r#"/NOLOGO"#,
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"#,
Expand Down
16 changes: 14 additions & 2 deletions windivert-sys/build/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod compile;

use path_macro2::path;
use std::{env, fs};

pub const LIB_PATH_ARG: &str = "WINDIVERT_PATH";
Expand All @@ -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!(
Expand Down Expand Up @@ -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.")
}
}
Loading