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

Commit eb2af6f

Browse files
committed
Fixes #177 -- let the kernel build process drive cargo
1 parent 26eacda commit eb2af6f

File tree

7 files changed

+36
-30
lines changed

7 files changed

+36
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Module.symvers
99
*.mod.c
1010
*.o
1111
modules.order
12+
dummy.c

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ environment variable appropriately, e.g., `CLANG=clang-9`.
5757

5858
## Building hello-world
5959

60-
1. Install clang, kernel headers, and the `rust-src` and `rustfmt` components from `rustup`:
60+
1. Install clang, kernel headers, and the `rust-src` and `rustfmt` components
61+
from `rustup`:
6162

6263
```
6364
apt-get install llvm clang linux-headers-"$(uname -r)" # or the equivalent for your OS
@@ -70,19 +71,14 @@ rustup component add --toolchain=nightly rust-src rustfmt
7071
cd hello-world
7172
```
7273

73-
3. Build the static object with cargo build, cross-compiling for the kernel target
74-
75-
```
76-
cargo build -Z build-std=core,alloc --target x86_64-linux-kernel
77-
```
78-
79-
4. Build the kernel module using the Linux kernel build system (kbuild)
74+
3. Build the kernel module using the Linux kernel build system (kbuild), this
75+
will invoke `cargo` to build the Rust code
8076

8177
```
8278
make
8379
```
8480

85-
5. Load and unload the module!
81+
4. Load and unload the module!
8682

8783
```
8884
sudo insmod helloworld.ko

build.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ fn handle_kernel_symbols_cfg(symvers_path: &PathBuf) {
9898
}
9999
}
100100

101+
fn add_env_if_present(cmd: &mut Command, var: &str) {
102+
if let Ok(val) = env::var(var) {
103+
cmd.env(var, val);
104+
}
105+
}
106+
101107
fn main() {
102108
println!("cargo:rerun-if-env-changed=KDIR");
103109
let kdir = env::var("KDIR").unwrap_or(format!(
@@ -109,12 +115,15 @@ fn main() {
109115

110116
println!("cargo:rerun-if-env-changed=CLANG");
111117
println!("cargo:rerun-if-changed=kernel-cflags-finder/Makefile");
112-
let output = Command::new("make")
113-
.arg("-C")
118+
let mut cmd = Command::new("make");
119+
cmd.arg("-C")
114120
.arg("kernel-cflags-finder")
115121
.arg("-s")
116-
.output()
117-
.unwrap();
122+
.env_clear();
123+
add_env_if_present(&mut cmd, "KDIR");
124+
add_env_if_present(&mut cmd, "CLANG");
125+
add_env_if_present(&mut cmd, "PATH");
126+
let output = cmd.output().unwrap();
118127
if !output.status.success() {
119128
eprintln!("kernel-cflags-finder did not succeed");
120129
eprintln!("stdout: {}", std::str::from_utf8(&output.stdout).unwrap());
@@ -161,7 +170,6 @@ fn main() {
161170
handle_kernel_symbols_cfg(&PathBuf::from(&kdir).join("Module.symvers"));
162171

163172
let mut builder = cc::Build::new();
164-
println!("cargo:rerun-if-env-changed=CLANG");
165173
builder.compiler(env::var("CLANG").unwrap_or("clang".to_string()));
166174
builder.target(&target);
167175
builder.warnings(false);

hello-world/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
ifneq ($(KERNELRELEASE),)
12
obj-m := helloworld.o
23
helloworld-objs := hello_world.rust.o
3-
KDIR ?= /lib/modules/$(shell uname -r)/build
4+
5+
$(src)/target/x86_64-linux-kernel/debug/libhello_world.a: $(src)/Cargo.toml $(wildcard $(src)/src/*.c)
6+
cd $(src); env -u MAKE -u MAKEFLAGS cargo build -Z build-std=core,alloc --target=x86_64-linux-kernel
47

58
%.rust.o: target/x86_64-linux-kernel/debug/lib%.a
69
$(LD) -r -o $@ --whole-archive $<
710

11+
else
12+
KDIR ?= /lib/modules/$(shell uname -r)/build
13+
814
all:
915
$(MAKE) -C $(KDIR) M=$(CURDIR)
1016

1117
clean:
1218
$(MAKE) -C $(KDIR) M=$(CURDIR) clean
19+
endif

kernel-cflags-finder/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
.tmp_versions/
66
Module.symvers
77
built-in.o
8-
dummy.c
98
dummy.ko
109
dummy.mod.c
1110
dummy.mod.o

tests/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
ifneq ($(KERNELRELEASE),)
12
obj-m := testmodule.o
23
testmodule-objs := $(TEST_NAME).rust.o
3-
KDIR ?= /lib/modules/$(shell uname -r)/build
4+
5+
$(src)/target/x86_64-linux-kernel/debug/lib%.a: $(src)/$(TEST_PATH)/Cargo.toml $(wildcard $(src)/$(TEST_PATH)/src/*.rs)
6+
cd $(src)/$(TEST_PATH); env -u MAKE -u MAKEFLAGS RUSTFLAGS="-Dwarnings" CARGO_TARGET_DIR=../target cargo build -Z build-std=core,alloc --target=x86_64-linux-kernel
47

58
%.rust.o: target/x86_64-linux-kernel/debug/lib%.a
69
$(LD) -r -o $@ --whole-archive $<
710

11+
else
12+
KDIR ?= /lib/modules/$(shell uname -r)/build
13+
814
all:
915
$(MAKE) -C $(KDIR) M=$(CURDIR)
1016

1117
clean:
1218
$(MAKE) -C $(KDIR) M=$(CURDIR) clean
19+
endif

tests/run_tests.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,11 @@ def main(argv):
2727
continue
2828

2929
print("+ [{}]".format(path))
30-
run(
31-
"cargo", "build", "-Zbuild-std=core,alloc",
32-
"--target", target,
33-
cwd=os.path.join(BASE_DIR, path),
34-
environ=dict(
35-
os.environ,
36-
RUSTFLAGS="-Dwarnings",
37-
CARGO_TARGET_DIR=os.path.relpath(
38-
os.path.join(BASE_DIR, "target"),
39-
os.path.join(BASE_DIR, path)
40-
),
41-
)
42-
)
4330

4431
run(
4532
"make", "-C", BASE_DIR,
4633
"TEST_NAME={}_tests".format(path.replace("-", "_")),
34+
"TEST_PATH={}".format(path),
4735
)
4836
# TODO: qemu
4937
run(

0 commit comments

Comments
 (0)