Skip to content
This repository has been archived by the owner on Mar 7, 2021. It is now read-only.

Commit

Permalink
build.rs: Build fixes for Centos 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Davydov committed Jan 25, 2021
1 parent b9a4c2d commit 1aad8ea
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ edition = "2018"
bitflags = "1"

[build-dependencies]
bindgen = "0.54"
bindgen = "0.56"
cc = "1.0"
shlex = "0.1"
43 changes: 41 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ const OPAQUE_TYPES: &[&str] = &[
// and https://github.com/rust-lang/rust-bindgen/issues/1538
"desc_struct",
"xregs_state",
"xsave_struct",
];
const CLANG_ARGS_BLACKLIST: [&'static str; 5] = [
"-maccumulate-outgoing-args",
"-mpreferred-stack-boundary=3",
"-mindirect-branch=thunk-extern",
"-mindirect-branch-register",
"-fconserve-stack",
];

fn handle_kernel_version_cfg(bindings_path: &PathBuf) {
Expand Down Expand Up @@ -86,10 +94,25 @@ fn handle_kernel_version_cfg(bindings_path: &PathBuf) {
}
if major >= 4 {
// We don't currently support anything older than 4.4
for x in 4..=if major > 4 { 20 } else { minor } {
for x in 1..=if major > 4 { 20 } else { minor } {
println!("cargo:rustc-cfg=kernel_4_{}_0_or_greater", x);
}
}
if major >= 3 {
for x in 1..=if major > 3 { 19 } else { minor } {
println!("cargo:rustc-cfg=kernel_3_{}_0_or_greater", x);
}
}
}

fn handle_kernel_rhel_kabi(fs_ops_path: &PathBuf) {
let f = BufReader::new(fs::File::open(fs_ops_path).unwrap());
for line in f.lines() {
if line.unwrap().contains("RH_KABI_REPLACE") {
println!("cargo:rustc-cfg=kernel_rh_kabi");
break;
}
}
}

fn handle_kernel_symbols_cfg(symvers_path: &PathBuf) {
Expand Down Expand Up @@ -153,6 +176,12 @@ fn main() {

builder = builder.clang_arg(format!("--target={}", target));
for arg in kernel_args.iter() {
if CLANG_ARGS_BLACKLIST.contains(&arg.as_str()) {
continue;
}
if arg.as_str() == "-DOS_CENTOS" {
println!("cargo:rustc-cfg=os_centos")
}
builder = builder.clang_arg(arg.clone());
}

Expand All @@ -179,16 +208,26 @@ fn main() {
.expect("Couldn't write bindings!");

handle_kernel_version_cfg(&out_path.join("bindings.rs"));
handle_kernel_rhel_kabi(
&PathBuf::from(&kernel_dir)
.parent()
.unwrap()
.join("source/include/linux/fs.h"),
);
handle_kernel_symbols_cfg(&PathBuf::from(&kernel_dir).join("Module.symvers"));

let mut builder = cc::Build::new();
builder.compiler(env::var("CC").unwrap_or_else(|_| "clang".to_string()));
let compiler = env::var("CC").unwrap_or_else(|_| "clang".to_string());
builder.compiler(&compiler);
builder.target(&target);
builder.warnings(false);
println!("cargo:rerun-if-changed=src/helpers.c");
builder.file("src/helpers.c");
for arg in kernel_args.iter() {
builder.flag(&arg);
}
if &compiler == "gcc" {
builder.flag("-fno-pie");
}
builder.compile("helpers");
}
2 changes: 1 addition & 1 deletion hello-world/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGO ?= cargo
export c_flags

$(src)/target/x86_64-linux-kernel/debug/libhello_world.a: cargo_will_determine_dependencies
cd $(src); $(CARGO) build -Z build-std=core,alloc --target=x86_64-linux-kernel
cd $(src); $(CARGO) +nightly build -Z build-std=core,alloc --target=x86_64-linux-kernel

.PHONY: cargo_will_determine_dependencies

Expand Down
13 changes: 12 additions & 1 deletion hello-world/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
export KDIR ?= /lib/modules/$(shell uname -r)/build

CONFIG_CC_IS_CLANG := y
CLANG ?= clang
ifeq ($(origin CC),default)
CC := ${CLANG}
endif

HEADER_SEMVER ?= $(shell uname -r | grep -oG "[0-9]\+\.[0-9]\+\.[0-9]\+" | head -1)
ifeq ($(shell echo $(HEADER_SEMVER) 4.4.0 | tr " " "\n" | sort -V | head -1), $(HEADER_SEMVER))
CC := gcc
CONFIG_CC_IS_CLANG := n
endif

ifneq (,$(wildcard /etc/centos-release))
KCPPFLAGS += -DOS_CENTOS
endif

all:
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) CONFIG_CC_IS_CLANG=y
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) KCPPFLAGS=$(KCPPFLAGS) CONFIG_CC_IS_CLANG=$(CONFIG_CC_IS_CLANG)

clean:
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) clean
22 changes: 16 additions & 6 deletions src/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,24 @@ impl<T: FileOperations> FileOperationsVtable<T> {
} else {
None
},

#[cfg(not(kernel_4_1_0_or_greater))]
aio_read: None,
#[cfg(not(kernel_4_1_0_or_greater))]
aio_write: None,
#[cfg(not(kernel_3_11_0_or_greater))]
readdir: None,
#[cfg(not(kernel_4_9_0_or_greater))]
aio_fsync: None,
check_flags: None,
#[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater)))]
#[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater), not(os_centos)))]
clone_file_range: None,
compat_ioctl: None,
#[cfg(kernel_4_5_0_or_greater)]
copy_file_range: None,
#[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater)))]
#[cfg(all(kernel_4_5_0_or_greater, not(kernel_4_20_0_or_greater), not(os_centos)))]
dedupe_file_range: None,
fallocate: None,
#[cfg(kernel_4_19_0_or_greater)]
#[cfg(any(kernel_4_19_0_or_greater, all(os_centos, kernel_4_18_0_or_greater)))]
fadvise: None,
fasync: None,
flock: None,
Expand All @@ -181,25 +186,30 @@ impl<T: FileOperations> FileOperationsVtable<T> {
iterate: None,
#[cfg(kernel_4_7_0_or_greater)]
iterate_shared: None,
#[cfg(kernel_5_1_0_or_greater)]
#[cfg(any(kernel_5_1_0_or_greater, all(os_centos, kernel_4_18_0_or_greater)))]
iopoll: None,
lock: None,
mmap: None,
#[cfg(kernel_4_15_0_or_greater)]
mmap_supported_flags: 0,
owner: ptr::null_mut(),
poll: None,
#[cfg(kernel_3_16_0_or_greater)]
read_iter: None,
#[cfg(kernel_4_20_0_or_greater)]
#[cfg(any(kernel_4_20_0_or_greater, all(os_centos, kernel_4_18_0_or_greater)))]
remap_file_range: None,
sendpage: None,
#[cfg(kernel_aufs_setfl)]
setfl: None,
#[cfg(kernel_rh_kabi)]
__bindgen_anon_1: bindings::file_operations__bindgen_ty_1 { setlease: None },
#[cfg(not(kernel_rh_kabi))]
setlease: None,
show_fdinfo: None,
splice_read: None,
splice_write: None,
unlocked_ioctl: None,
#[cfg(kernel_3_16_0_or_greater)]
write_iter: None,
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
#include <linux/uaccess.h>
#include <linux/version.h>


void bug_helper(void)
{
BUG();
}

int access_ok_helper(const void __user *addr, unsigned long n)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) /* v5.0-rc1~46 */
#if defined(OS_CENTOS) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
return access_ok(addr, n);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0) /* v5.0-rc1~46 */
return access_ok(addr, n);
#else
return access_ok(0, addr, n);
Expand Down

0 comments on commit 1aad8ea

Please sign in to comment.