This repository was archived by the owner on Mar 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathbuild.rs
150 lines (138 loc) · 4.61 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use bindgen;
use cc;
use shlex;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::process::Command;
use std::{env, fs};
const INCLUDED_TYPES: &[&str] = &["file_system_type", "mode_t", "umode_t", "ctl_table"];
const INCLUDED_FUNCTIONS: &[&str] = &[
"cdev_add",
"cdev_init",
"cdev_del",
"register_filesystem",
"unregister_filesystem",
"krealloc",
"kfree",
"mount_nodev",
"kill_litter_super",
"register_sysctl",
"unregister_sysctl_table",
"access_ok",
"_copy_to_user",
"_copy_from_user",
"alloc_chrdev_region",
"unregister_chrdev_region",
];
const INCLUDED_VARS: &[&str] = &[
"EINVAL",
"ENOMEM",
"ESPIPE",
"EFAULT",
"__this_module",
"FS_REQUIRES_DEV",
"FS_BINARY_MOUNTDATA",
"FS_HAS_SUBTYPE",
"FS_USERNS_MOUNT",
"FS_RENAME_DOES_D_MOVE",
"BINDINGS_GFP_KERNEL",
"KERN_INFO",
"VERIFY_WRITE",
"LINUX_VERSION_CODE",
"SEEK_SET",
"SEEK_CUR",
"SEEK_END",
];
const OPAQUE_TYPES: &[&str] = &[
// These need to be opaque because they're both packed and aligned, which rustc
// doesn't support yet. See https://github.com/rust-lang/rust/issues/59154
// and https://github.com/rust-lang/rust-bindgen/issues/1538
"desc_struct",
"xregs_state",
];
fn kernel_version_code(major: u8, minor: u8, patch: u8) -> u64 {
((major as u64) << 16) | ((minor as u64) << 8) | (patch as u64)
}
fn handle_kernel_version_cfg(bindings_path: &PathBuf) {
let f = BufReader::new(fs::File::open(bindings_path).unwrap());
let mut version = None;
for line in f.lines() {
let line = line.unwrap();
if let Some(type_and_value) = line.split("pub const LINUX_VERSION_CODE").nth(1) {
if let Some(value) = type_and_value.split("=").nth(1) {
let raw_version = value.split(";").next().unwrap();
version = Some(raw_version.trim().parse::<u64>().unwrap());
break;
}
}
}
let version = version.expect("Couldn't find kernel version");
if version >= kernel_version_code(4, 15, 0) {
println!("cargo:rustc-cfg=kernel_4_15_0_or_greater")
}
if version >= kernel_version_code(4, 19, 0) {
println!("cargo:rustc-cfg=kernel_4_19_0_or_greater")
}
if version >= kernel_version_code(4, 20, 0) {
println!("cargo:rustc-cfg=kernel_4_20_0_or_greater")
}
if version >= kernel_version_code(5, 1, 0) {
println!("cargo:rustc-cfg=kernel_5_1_0_or_greater")
}
}
fn main() {
println!("cargo:rerun-if-env-changed=KDIR");
println!("cargo:rerun-if-env-changed=CLANG");
println!("cargo:rerun-if-changed=kernel-cflags-finder/Makefile");
let output = Command::new("make")
.arg("-C")
.arg("kernel-cflags-finder")
.arg("-s")
.output()
.unwrap();
if !output.status.success() {
eprintln!("kernel-cflags-finder did not succeed");
eprintln!("stdout: {}", std::str::from_utf8(&output.stdout).unwrap());
eprintln!("stderr: {}", std::str::from_utf8(&output.stderr).unwrap());
std::process::exit(1);
}
let mut builder = bindgen::Builder::default()
.use_core()
.ctypes_prefix("c_types")
.derive_default(true)
.rustfmt_bindings(true);
builder = builder.clang_arg("--target=x86_64-linux-kernel");
for arg in shlex::split(std::str::from_utf8(&output.stdout).unwrap()).unwrap() {
builder = builder.clang_arg(arg.to_string());
}
println!("cargo:rerun-if-changed=src/bindings_helper.h");
builder = builder.header("src/bindings_helper.h");
for t in INCLUDED_TYPES {
builder = builder.whitelist_type(t);
}
for f in INCLUDED_FUNCTIONS {
builder = builder.whitelist_function(f);
}
for v in INCLUDED_VARS {
builder = builder.whitelist_var(v);
}
for t in OPAQUE_TYPES {
builder = builder.opaque_type(t);
}
let bindings = builder.generate().expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
handle_kernel_version_cfg(&out_path.join("bindings.rs"));
let mut builder = cc::Build::new();
println!("cargo:rerun-if-env-changed=CLANG");
builder.compiler(env::var("CLANG").unwrap_or("clang".to_string()));
builder.target("x86_64-linux-kernel");
builder.warnings(false);
builder.file("src/helpers.c");
for arg in shlex::split(std::str::from_utf8(&output.stdout).unwrap()).unwrap() {
builder.flag(&arg);
}
builder.compile("helpers");
}