├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── build.rs └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "kernel-build" 3 | version = "0.1.0" 4 | description = "Helper crate for building kernel drivers" 5 | authors = ["not-matthias <26800596+not-matthias@users.noreply.github.com>"] 6 | edition = "2021" 7 | readme = "README.md" 8 | repository = "https://github.com/not-matthias/kernel-build-rs" 9 | license-file = "LICENSE" 10 | 11 | [build-dependencies] 12 | winreg = "0.10.1" 13 | failure = "0.1.8" 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 not-matthias 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![crates.io](https://img.shields.io/crates/v/kernel-build.svg)](https://crates.io/crates/kernel-build) 2 | 3 | # kernel-build-rs 4 | 5 | Helper crate for building Windows Kernel Drivers. 6 | 7 | ## What does it do? 8 | 9 | It sets all the linker search paths in [`build.rs`](./build.rs) which are needed for the kernel driver. 10 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | extern crate winreg; 2 | #[macro_use] extern crate failure; 3 | 4 | use failure::Error; 5 | use std::{ 6 | env::var, 7 | path::{Path, PathBuf}, 8 | }; 9 | use winreg::{enums::*, RegKey}; 10 | 11 | fn get_windows_kits_dir() -> Result { 12 | let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); 13 | let key = r"SOFTWARE\Microsoft\Windows Kits\Installed Roots"; 14 | let dir: String = hklm.open_subkey(key)?.get_value("KitsRoot10")?; 15 | 16 | Ok(dir.into()) 17 | } 18 | 19 | fn get_km_dir(windows_kits_dir: &Path) -> Result { 20 | let readdir = Path::new(windows_kits_dir).join("lib").read_dir()?; 21 | 22 | let max_libdir = readdir 23 | .filter_map(|dir| dir.ok()) 24 | .map(|dir| dir.path()) 25 | .filter(|dir| { 26 | dir.components() 27 | .last() 28 | .and_then(|c| c.as_os_str().to_str()) 29 | .map(|c| c.starts_with("10.") && dir.join("km").is_dir()) 30 | .unwrap_or(false) 31 | }) 32 | .max() 33 | .ok_or_else(|| format_err!("Can not find a valid km dir in `{:?}`", windows_kits_dir))?; 34 | 35 | Ok(max_libdir.join("km")) 36 | } 37 | 38 | fn internal_link_search() { 39 | let windows_kits_dir = get_windows_kits_dir().unwrap(); 40 | 41 | let km_dir = get_km_dir(&windows_kits_dir).unwrap(); 42 | 43 | let target = var("TARGET").unwrap(); 44 | 45 | let arch = if target.contains("x86_64") { 46 | "x64" 47 | } else if target.contains("i686") { 48 | "x86" 49 | } else { 50 | panic!("Only support x86_64 and i686!"); 51 | }; 52 | 53 | let lib_dir = km_dir.join(arch); 54 | println!( 55 | "cargo:rustc-link-search=native={}", 56 | lib_dir.to_str().unwrap() 57 | ); 58 | } 59 | 60 | fn extra_link_search() {} 61 | 62 | fn main() { 63 | if var(format!( 64 | "CARGO_FEATURE_{}", 65 | "extra_link_search".to_uppercase() 66 | )) 67 | .is_ok() 68 | { 69 | extra_link_search() 70 | } else { 71 | internal_link_search() 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] --------------------------------------------------------------------------------