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

Commit 5536879

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

File tree

10 files changed

+42
-40
lines changed

10 files changed

+42
-40
lines changed

.gitignore

+1
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

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ install:
1717
- rustup component add rust-src rustfmt clippy
1818

1919
script:
20-
- ./tests/run_tests.py x86_64-linux-kernel
20+
- ./tests/run_tests.py
2121
- |
2222
for p in . hello-world tests/*; do
2323
if [ -d "$p" ]; then

README.md

+5-9
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

+13-5
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/Kbuild

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
obj-m := helloworld.o
2+
helloworld-objs := hello_world.rust.o
3+
4+
CARGO ?= cargo
5+
6+
$(src)/target/x86_64-linux-kernel/debug/libhello_world.a: $(src)/Cargo.toml $(wildcard $(src)/src/*.rs)
7+
cd $(src); env -u MAKE -u MAKEFLAGS $(CARGO) build -Z build-std=core,alloc --target=x86_64-linux-kernel
8+
9+
%.rust.o: target/x86_64-linux-kernel/debug/lib%.a
10+
$(LD) -r -o $@ --whole-archive $<

hello-world/Makefile

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
obj-m := helloworld.o
2-
helloworld-objs := hello_world.rust.o
31
KDIR ?= /lib/modules/$(shell uname -r)/build
42

5-
%.rust.o: target/x86_64-linux-kernel/debug/lib%.a
6-
$(LD) -r -o $@ --whole-archive $<
7-
83
all:
94
$(MAKE) -C $(KDIR) M=$(CURDIR)
105

kernel-cflags-finder/.gitignore

-1
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/Kbuild

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
obj-m := testmodule.o
2+
testmodule-objs := $(TEST_NAME).rust.o
3+
4+
CARGO ?= cargo
5+
6+
$(src)/target/x86_64-linux-kernel/debug/lib%.a: $(src)/$(TEST_PATH)/Cargo.toml $(wildcard $(src)/$(TEST_PATH)/src/*.rs)
7+
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
8+
9+
%.rust.o: target/x86_64-linux-kernel/debug/lib%.a
10+
$(LD) -r -o $@ --whole-archive $<

tests/Makefile

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
obj-m := testmodule.o
2-
testmodule-objs := $(TEST_NAME).rust.o
31
KDIR ?= /lib/modules/$(shell uname -r)/build
42

5-
%.rust.o: target/x86_64-linux-kernel/debug/lib%.a
6-
$(LD) -r -o $@ --whole-archive $<
7-
83
all:
94
$(MAKE) -C $(KDIR) M=$(CURDIR)
105

tests/run_tests.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def run(*args, **kwargs):
1818

1919

2020
def main(argv):
21-
[_, target] = argv
2221
for path in os.listdir(BASE_DIR):
2322
if (
2423
not os.path.isdir(os.path.join(BASE_DIR, path)) or
@@ -27,23 +26,12 @@ def main(argv):
2726
continue
2827

2928
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-
)
4329

4430
run(
4531
"make", "-C", BASE_DIR,
4632
"TEST_NAME={}_tests".format(path.replace("-", "_")),
33+
"TEST_PATH={}".format(path),
34+
"RUSTFLAGS=-Dwarnings",
4735
)
4836
# TODO: qemu
4937
run(

0 commit comments

Comments
 (0)