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

Commit 1ac31ba

Browse files
alexgeofft
andcommitted
Remove kernel-cflags-finder
This is #185 minus #188, but with the CONFIG_CC_IS_CLANG fix from #227. Co-authored-by: Geoffrey Thomas <[email protected]>
1 parent 59110dc commit 1ac31ba

File tree

9 files changed

+46
-101
lines changed

9 files changed

+46
-101
lines changed

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ various other examples in the tests/ directory.
1919
We run [bindgen](https://github.com/rust-lang/rust-bindgen) on the
2020
kernel headers to generate automatic Rust FFI bindings. bindgen is
2121
powered by [Clang](https://clang.llvm.org), so we use the kernel's
22-
own build system to determine the appropriate CFLAGS (see
23-
`kernel-cflags-finder`) and pass them to bindgen (see `build.rs`). Then we
24-
write safe bindings to these types (see the various files inside `src/`).
22+
own build system to determine the appropriate CFLAGS. Then we write safe
23+
bindings to these types (see the various files inside `src/`).
2524

2625
Each kernel module in Rust lives in a `staticlib` crate, which generates
2726
a `.a` file. We pass this object to the Linux kernel's own module build
@@ -52,8 +51,8 @@ properly](https://github.com/rust-lang/rust-bindgen/issues/1316). If
5251
you're running kernel 5.0 or newer, [you'll need Clang
5352
9](https://github.com/fishinabarrel/linux-kernel-module-rust/issues/123)
5453
(released September 2019), which adds support for `asm goto`.
55-
You may need to set the `CLANG` environment variable appropriately,
56-
e.g., `CLANG=clang-9`.
54+
You may need to set the `CC` environment variable appropriately, e.g.,
55+
`CC=clang-9`.
5756

5857
## Building hello-world
5958

build.rs

+34-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::io::{BufRead, BufReader};
22
use std::path::PathBuf;
3-
use std::process::Command;
43
use std::{env, fs};
54

65
const INCLUDED_TYPES: &[&str] = &["file_system_type", "mode_t", "umode_t", "ctl_table"];
@@ -100,38 +99,39 @@ fn handle_kernel_symbols_cfg(symvers_path: &PathBuf) {
10099
}
101100
}
102101

103-
fn add_env_if_present(cmd: &mut Command, var: &str) {
104-
if let Ok(val) = env::var(var) {
105-
cmd.env(var, val);
102+
// Takes the CFLAGS from the kernel Makefile and changes all the include paths to be absolute
103+
// instead of relative.
104+
fn prepare_cflags(cflags: &str, kernel_dir: &str) -> Vec<String> {
105+
let cflag_parts = shlex::split(&cflags).unwrap();
106+
let mut cflag_iter = cflag_parts.iter();
107+
let mut kernel_args = vec![];
108+
while let Some(arg) = cflag_iter.next() {
109+
if arg.starts_with("-I") && !arg.starts_with("-I/") {
110+
kernel_args.push(format!("-I{}/{}", kernel_dir, &arg[2..]));
111+
} else if arg == "-include" {
112+
kernel_args.push(arg.to_string());
113+
let include_path = cflag_iter.next().unwrap();
114+
if include_path.starts_with("/") {
115+
kernel_args.push(include_path.to_string());
116+
} else {
117+
kernel_args.push(format!("{}/{}", kernel_dir, include_path));
118+
}
119+
} else {
120+
kernel_args.push(arg.to_string());
121+
}
106122
}
123+
return kernel_args;
107124
}
108125

109126
fn main() {
110-
println!("cargo:rerun-if-env-changed=KDIR");
111-
let kdir = env::var("KDIR").unwrap_or(format!(
112-
"/lib/modules/{}/build",
113-
std::str::from_utf8(&(Command::new("uname").arg("-r").output().unwrap().stdout))
114-
.unwrap()
115-
.trim()
116-
));
117-
118-
println!("cargo:rerun-if-env-changed=CLANG");
119-
println!("cargo:rerun-if-changed=kernel-cflags-finder/Makefile");
120-
let mut cmd = Command::new("make");
121-
cmd.arg("-C")
122-
.arg("kernel-cflags-finder")
123-
.arg("-s")
124-
.env_clear();
125-
add_env_if_present(&mut cmd, "KDIR");
126-
add_env_if_present(&mut cmd, "CLANG");
127-
add_env_if_present(&mut cmd, "PATH");
128-
let output = cmd.output().unwrap();
129-
if !output.status.success() {
130-
eprintln!("kernel-cflags-finder did not succeed");
131-
eprintln!("stdout: {}", std::str::from_utf8(&output.stdout).unwrap());
132-
eprintln!("stderr: {}", std::str::from_utf8(&output.stderr).unwrap());
133-
std::process::exit(1);
134-
}
127+
println!("cargo:rerun-if-env-changed=CC");
128+
println!("cargo:rerun-if-env-changed=abs_srctree");
129+
println!("cargo:rerun-if-env-changed=c_flags");
130+
131+
let kernel_dir = env::var("abs_srctree").expect("Must be invoked from kernel makefile");
132+
let kernel_cflags = env::var("c_flags").expect("Add 'export c_flags' to Kbuild");
133+
134+
let kernel_args = prepare_cflags(&kernel_cflags, &kernel_dir);
135135

136136
let target = env::var("TARGET").unwrap();
137137

@@ -142,8 +142,8 @@ fn main() {
142142
.rustfmt_bindings(true);
143143

144144
builder = builder.clang_arg(format!("--target={}", target));
145-
for arg in shlex::split(std::str::from_utf8(&output.stdout).unwrap()).unwrap() {
146-
builder = builder.clang_arg(arg.to_string());
145+
for arg in kernel_args.iter() {
146+
builder = builder.clang_arg(arg.clone());
147147
}
148148

149149
println!("cargo:rerun-if-changed=src/bindings_helper.h");
@@ -169,14 +169,14 @@ fn main() {
169169
.expect("Couldn't write bindings!");
170170

171171
handle_kernel_version_cfg(&out_path.join("bindings.rs"));
172-
handle_kernel_symbols_cfg(&PathBuf::from(&kdir).join("Module.symvers"));
172+
handle_kernel_symbols_cfg(&PathBuf::from(&kernel_dir).join("Module.symvers"));
173173

174174
let mut builder = cc::Build::new();
175-
builder.compiler(env::var("CLANG").unwrap_or_else(|_| "clang".to_string()));
175+
builder.compiler(env::var("CC").unwrap_or_else(|_| "clang".to_string()));
176176
builder.target(&target);
177177
builder.warnings(false);
178178
builder.file("src/helpers.c");
179-
for arg in shlex::split(std::str::from_utf8(&output.stdout).unwrap()).unwrap() {
179+
for arg in kernel_args.iter() {
180180
builder.flag(&arg);
181181
}
182182
builder.compile("helpers");

hello-world/Kbuild

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ obj-m := helloworld.o
22
helloworld-objs := hello_world.rust.o
33

44
CARGO ?= cargo
5+
export c_flags
56

67
$(src)/target/x86_64-linux-kernel/debug/libhello_world.a: $(src)/Cargo.toml $(wildcard $(src)/src/*.rs)
78
cd $(src); env -u MAKE -u MAKEFLAGS $(CARGO) build -Z build-std=core,alloc --target=x86_64-linux-kernel

hello-world/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
KDIR ?= /lib/modules/$(shell uname -r)/build
2+
CC ?= clang
23

34
all:
4-
$(MAKE) -C $(KDIR) M=$(CURDIR)
5+
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) CONFIG_CC_IS_CLANG=y
56

67
clean:
7-
$(MAKE) -C $(KDIR) M=$(CURDIR) clean
8+
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) clean

kernel-cflags-finder/.gitignore

-12
This file was deleted.

kernel-cflags-finder/Makefile

-36
This file was deleted.

kernel-cflags-finder/README.md

-10
This file was deleted.

tests/Kbuild

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ obj-m := testmodule.o
22
testmodule-objs := $(TEST_NAME).rust.o
33

44
CARGO ?= cargo
5+
export c_flags
56

67
$(src)/target/x86_64-linux-kernel/debug/lib%.a: $(src)/$(TEST_PATH)/Cargo.toml $(wildcard $(src)/$(TEST_PATH)/src/*.rs)
78
cd $(src)/$(TEST_PATH); env -u MAKE -u MAKEFLAGS CARGO_TARGET_DIR=../target $(CARGO) build -Z build-std=core,alloc --target=x86_64-linux-kernel

tests/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
KDIR ?= /lib/modules/$(shell uname -r)/build
2+
CC ?= clang
23

34
all:
4-
$(MAKE) -C $(KDIR) M=$(CURDIR)
5+
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) CONFIG_CC_IS_CLANG=y
56

67
clean:
7-
$(MAKE) -C $(KDIR) M=$(CURDIR) clean
8+
$(MAKE) -C $(KDIR) M=$(CURDIR) CC=$(CC) clean

0 commit comments

Comments
 (0)