Reputation: 97
I'm compile the Linux kernel module but I get the follow error
error: target file "./scripts/target.json" does not exist
make[3]: *** [scripts/Makefile.build:292: /home/guilherme/repositories/rust_learn/hello_world_lkm/hello_world.o] Error 1
make[2]: *** [/home/guilherme/repositories/linux/Makefile:1913: /home/guilherme/repositories/rust_learn/hello_world_lkm] Error 2
make[1]: *** [Makefile:234: __sub-make] Error 2
make[1]: Saindo do diretório '/home/guilherme/repositories/linux'
The module is a Rust module, and I'm doing this for a studying purposes. I dont have a lot knowning in Rust, but I study this. I have a short experience in Linux kernel module in C and I starting this in Rust.
I has a Rust installed in my computer:
$ rustc --version
rustc 1.73.0 (cc66ad468 2023-10-03)
and I have a cargo
installed:
$ cargo --version
cargo 1.73.0 (9c4383fb5 2023-08-26)
I has a RUST cofigured in my kernel:
The module that I'm developing is very simple:
use kernel::prelude::*
module! {
type: HelloWorld,
name: b"HelloWorld",
license b"GPL"
};
struct HelloWorld;
impl kernel::Module for HelloWorld {
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
pr_info!("Hello world");
Ok(HelloWorld)
}
}
impl Drop for HelloWorld {
fn drop(&mut self) {
pr_info!("Adeus mundo cruel fdp");
}
}
and my Makefile is very simple too:
PWD := $(shell pwd)
obj-m += hello_world.o
CLANG ?= clang
ifeq ($(origin CC),default)
CC := ${CLANG}
endif
all:
$(MAKE) -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) CC=$(CC) CONFIG_CC_IS_CLANG=y modules
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
I have a clang
installed:
$ clang --version
clang version 16.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
And Rust is available in my kernel source:
$ make LLVM=1 rustavailable
Rust is available!
Upvotes: 0
Views: 419
Reputation: 11
i'm not sure if you are using a distributed kernel with kernel-devel package installed to compile an out-of-tree rust module,but i've got same errors. here's some info may useful for you.
linux distributions supports for rust is not very good,so the kernel-devel package always del some important file.
such as:
ubuntu 24.04 /usr/src/'uname -r/rust directory is empty.
opensuse tumbleweed /usr/src/'uname -r'/scripts/target.json is missing
in fact, ./scripts/target.json missing means /usr/src/'uname -r'/scripts/target.json is missing.
make LLVM=1 V=1 can offer more details,you can see rustc --target=./scripts/target.json.
in kernel compile,target.json should generate with scripts/generate_rust_target.rs,but distributions does not support this,may be their package scripts does not support rust yet.
make LLVM=1 rustavailable means current kernel-build tool-chain is ready,not rust kernel-devel env is ready(in tumbleweed,even rust directory in kernel is missing,rustavailable is still true).
so,if you want to compile an out-of-tree rust module,you still need to compile a kernel by yourself,and change Makefile KDIR to your linux source path.
but even you compiled your own kernel,there are other problems waiting.
module compile is using rustc --target=./scripts/target.json,so the target is incorrect to.
for example,rustc --target=x86_64_unknown.json means rustc is searching a target named x86_64_unknown,the target config is written in json file.the contents like rustc -Z unstable-options --target=x86_64-unknown-linux-gnu --print target-spec-json
and out-of-tree module build needs no-std libcore for target target
(target.json).
i'm not sure if a self compiled kernel can success build libcore,but for me,
cd rustc --print sysroot
/lib/rustlib/src/rust/library/core and execute
cargo build -Z build-std=core --target */scripts/target.json
has failed.
so,out-of-tree module supports is not very good for linux source and linux distributions till now.
Upvotes: 1