-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Thaumy/fix-build
Support build on Linux 3.12+
- Loading branch information
Showing
44 changed files
with
586 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// [(major, patch_level, is_selected)] | ||
#[rustfmt::skip] | ||
pub const LINUX_FEATURE_VERSIONS: [(usize, usize, bool); 26] = [ | ||
(6, 3, cfg!(feature = "linux-6.3" )), | ||
(6, 0, cfg!(feature = "linux-6.0" )), | ||
(5,16, cfg!(feature = "linux-5.16")), | ||
(5,13, cfg!(feature = "linux-5.13")), | ||
(5,12, cfg!(feature = "linux-5.12")), | ||
(5,11, cfg!(feature = "linux-5.11")), | ||
(5, 9, cfg!(feature = "linux-5.9" )), | ||
(5, 8, cfg!(feature = "linux-5.8" )), | ||
(5, 7, cfg!(feature = "linux-5.7" )), | ||
(5, 5, cfg!(feature = "linux-5.5" )), | ||
(5, 4, cfg!(feature = "linux-5.4" )), | ||
(5, 1, cfg!(feature = "linux-5.1" )), | ||
(4,17, cfg!(feature = "linux-4.17")), | ||
(4,16, cfg!(feature = "linux-4.16")), | ||
(4,14, cfg!(feature = "linux-4.14")), | ||
(4,12, cfg!(feature = "linux-4.12")), | ||
(4, 8, cfg!(feature = "linux-4.8" )), | ||
(4, 7, cfg!(feature = "linux-4.7" )), | ||
(4, 4, cfg!(feature = "linux-4.4" )), | ||
(4, 3, cfg!(feature = "linux-4.3" )), | ||
(4, 2, cfg!(feature = "linux-4.2" )), | ||
(4, 1, cfg!(feature = "linux-4.1" )), | ||
(3,19, cfg!(feature = "linux-3.19")), | ||
(3,16, cfg!(feature = "linux-3.16")), | ||
(3,13, cfg!(feature = "linux-3.13")), | ||
(3,12, cfg!(feature = "linux-3.12")), | ||
]; | ||
|
||
// [(major, patch_level, enum_entry)] | ||
#[rustfmt::skip] | ||
pub const IOCTLS: [(usize, usize, &str); 5] = [ | ||
(3,12, "PERF_EVENT_IOCTL_ID = PERF_EVENT_IOC_ID," ), | ||
(4, 1, "PERF_EVENT_IOCTL_SET_BPF = PERF_EVENT_IOC_SET_BPF," ), | ||
(4, 7, "PERF_EVENT_IOCTL_PAUSE_OUTPUT = PERF_EVENT_IOC_PAUSE_OUTPUT," ), | ||
(4,16, "PERF_EVENT_IOCTL_QUERY_BPF = PERF_EVENT_IOC_QUERY_BPF," ), | ||
(4,17, "PERF_EVENT_IOCTL_MODIFY_ATTRIBUTES = PERF_EVENT_IOC_MODIFY_ATTRIBUTES,"), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use crate::consts::IOCTLS; | ||
use std::fs; | ||
|
||
/// Parse `LINUX_VERSION_CODE` of `linux/version.h` to (major, patch_level, sub_level) | ||
pub fn parse_linux_version_h(path: &str) -> (usize, usize, usize) { | ||
let first_line = fs::read_to_string(path) | ||
.unwrap_or_else(|_| panic!("Failed to read {}", path)) | ||
.lines() | ||
.next() | ||
.unwrap_or_else(|| panic!("No lines in {}", path)) | ||
.to_string(); | ||
let linux_version_code = first_line | ||
.split(' ') | ||
.nth(2) | ||
.unwrap_or_else(|| panic!("Invalid line {}", first_line)) | ||
.to_string(); | ||
let linux_version_code = linux_version_code.parse::<usize>().unwrap_or_else(|e| { | ||
panic!( | ||
"Invalid LINUX_VERSION_CODE `{}` ({})", | ||
linux_version_code, e | ||
) | ||
}); | ||
|
||
let major = linux_version_code >> 16; | ||
let patch_lv = (linux_version_code & 65535) >> 8; | ||
let sub_lv = linux_version_code & 255; | ||
(major, patch_lv, sub_lv) | ||
} | ||
|
||
pub fn bindgen(linux_headers_path: &str, enabled_feature_versions: &[(usize, usize)]) { | ||
let bindings_output_path = "src/syscall/bindings/bindgen.rs"; | ||
// Try delete `bindgen.rs` for every build | ||
let _ = fs::remove_file(bindings_output_path); | ||
|
||
let header_contents = { | ||
let include_linux_bpf_h = if enabled_feature_versions.contains(&(5, 1)) { | ||
"#include <linux/bpf.h>" | ||
} else { | ||
"" | ||
}; | ||
let enum_entries = enabled_feature_versions | ||
.iter() | ||
.fold(String::new(), |mut acc, it| { | ||
IOCTLS.iter().try_for_each(|(m, p, str)| { | ||
if *it == (*m, *p) { | ||
acc.push_str(str); | ||
None | ||
} else { | ||
Some(()) | ||
} | ||
}); | ||
acc | ||
}); | ||
format!( | ||
" | ||
#include <asm/unistd.h> | ||
#include <linux/hw_breakpoint.h> | ||
#include <linux/perf_event.h> | ||
{} | ||
enum perf_event_ioctls {{ | ||
PERF_EVENT_IOCTL_ENABLE = PERF_EVENT_IOC_ENABLE, | ||
PERF_EVENT_IOCTL_DISABLE = PERF_EVENT_IOC_DISABLE, | ||
PERF_EVENT_IOCTL_REFRESH = PERF_EVENT_IOC_REFRESH, | ||
PERF_EVENT_IOCTL_RESET = PERF_EVENT_IOC_RESET, | ||
PERF_EVENT_IOCTL_PERIOD = PERF_EVENT_IOC_PERIOD, | ||
PERF_EVENT_IOCTL_SET_OUTPUT = PERF_EVENT_IOC_SET_OUTPUT, | ||
PERF_EVENT_IOCTL_SET_FILTER = PERF_EVENT_IOC_SET_FILTER, | ||
{} | ||
}};", | ||
include_linux_bpf_h, enum_entries, | ||
) | ||
}; | ||
|
||
let builder = bindgen::Builder::default() | ||
.derive_default(true) | ||
.generate_comments(false) | ||
.prepend_enum_name(false) | ||
.header_contents("wrapper.h", &header_contents); | ||
|
||
if linux_headers_path != "/usr/include" { | ||
builder.clang_arg(format!("-I{}", linux_headers_path)) | ||
} else { | ||
builder | ||
} | ||
.generate() | ||
.unwrap_or_else(|e| panic!("Failed to generate bindings: {}", e)) | ||
.write_to_file(bindings_output_path) | ||
.unwrap_or_else(|e| panic!("Failed to write {}: {}", bindings_output_path, e)); | ||
} |
Oops, something went wrong.