This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbuild.rs
92 lines (82 loc) · 2.51 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
extern crate bindgen;
extern crate cc;
extern crate shlex;
use std::env;
use std::path::PathBuf;
use std::process::Command;
const INCLUDED_TYPES: &[&str] = &["file_operations", "ctl_table", "spinlock_t", "mutex", "usb_driver", "usb_device_id", "driver_info"];
const INCLUDED_FUNCTIONS: &[&str] = &[
"__register_chrdev",
"__unregister_chrdev",
"_copy_to_user",
"register_sysctl",
"unregister_sysctl_table",
"proc_dointvec_minmax",
"spin_lock",
"usbnet_probe",
"usbnet_disconnect",
"usb_register_driver",
"usb_deregister",
"usbnet_get_endpoints",
"of_get_mac_address",
"skb_pull",
"skb_push",
"skb_trim",
"skb_clone",
"usbnet_skb_return",
"usbnet_read_cmd",
"call_usermodehelper",
"schedule",
//"__purge_module",
//"__rust_delete_module",
];
const INCLUDED_VARS: &[&str] = &["__this_module", "THIS_MODULE"];
fn main() {
let target = env::var("TARGET").unwrap();
println!("Target={}", target);
let mut builder = bindgen::Builder::default()
.use_core()
.ctypes_prefix("c_types")
.no_copy(".*")
.derive_default(true)
.rustfmt_bindings(true)
.clang_arg(format!("--target={}", target));
let output = String::from_utf8(
Command::new("make")
.arg("-C")
.arg("kernel-cflags-finder")
.arg("-s")
.output()
.unwrap()
.stdout,
)
.unwrap();
Command::new("make")
.arg("-C")
.arg("kernel-cflags-finder")
.arg("clean");
println!("get output:{}", output);
// These three arguments are not supported by clang
// output = output.replace("-mapcs", "");
// output = output.replace("-mno-sched-prolog", "");
// output = output.replace("-mno-thumb-interwork", "");
for arg in shlex::split(&output).unwrap() {
builder = builder.clang_arg(arg.to_string());
}
println!("cargo:rerun-if-changed=src/bindgen_helper.h");
builder = builder.header("src/bindgen_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);
}
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!");
}