Skip to content

Out-of-tree module failed to build if the kernel source path is different #792

Description

@Leo1003

As most of the Linux distribution install the kernel with their package (.deb, .rpm, ...). I also tried to do the same thing to test Linux kernel with Rust support. However, when I build some out-of-tree Rust kernel module, it failed to compile with the following message:

error[E0461]: couldn't find crate `core` with expected target triple target-13637732820183936185
  |
  = note: the following crate versions were found:
          crate `core`, target triple target-4719486650351723570: /usr/src/linux-headers-5.18.0-rc2-2-rust/rust/libcore.rmeta

error[E0461]: couldn't find crate `compiler_builtins` with expected target triple target-13637732820183936185
  |
  = note: the following crate versions were found:
          crate `compiler_builtins`, target triple target-4719486650351723570: /usr/src/linux-headers-5.18.0-rc2-2-rust/rust/libcompiler_builtins
.rmeta

error[E0461]: couldn't find crate `kernel` with expected target triple target-13637732820183936185
  --> /home/leo/rust-kernel-modules/rust-random/rust_random.rs:11:5
   |
11 | use kernel::{
   |     ^^^^^^
   |
   = note: the following crate versions were found:
           crate `kernel`, target triple target-4719486650351723570: /usr/src/linux-headers-5.18.0-rc2-2-rust/rust/libkernel.rmeta

After digging some codes, I found that rustc record the target triple as the path of target.json file.
https://doc.rust-lang.org/stable/nightly-rustc/rustc_target/spec/enum.TargetTriple.html

And the target triple appears in the error messages can be calculated with the following codes:

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;

#[allow(unused_variables)]
fn main() {
    let mut hasher = DefaultHasher::new();
    // target-4719486650351723570
    let path = PathBuf::from("/home/leo/linux-rust/src/build/rust/target.json");
    // target-13637732820183936185
    let path = PathBuf::from("/usr/src/linux-headers-5.18.0-rc2-2-rust/rust/target.json");
    path.hash(&mut hasher);
    let hash = hasher.finish();
    println!("target-{}", hash)
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=aa52d204a7d4167cfd84076776ab957a

The above codes is reference from rust-lang/rust/compiler/rustc_target/src/spec/mod.rs:2445

P.s. I think that this is the problem of the compiler, however, this issue must be resolved if we want to support out-of-tree Rust kernel module in the future.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    • kbuildRelated to building the kernel, `make`, `Kbuild`, `Kconfig` options...

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions