Driver compilation Issue

Hi guys!

I'm new in Rust and in the device drivers in particular. I'm following some examples for a device driver in Rust but I'm facing a problem that cannot understand completely.

This is my error:

thread 'main' panicked at 'Unable to generate bindings: ()', build.rs:86:39
stack backtrace:
0: rust_begin_unwind
at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/panicking.rs:493:5
1: core::panicking::panic_fmt
at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/core/src/panicking.rs:92:14
2: core::option::expect_none_failed
at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/core/src/option.rs:1300:5
3: core::result::Result<T,E>::expect
at /home/kuni/snap/rustup/common/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:997:23
4: build_script_build::main
at ./build.rs:86:20
5: core::ops::function::FnOnce::call_once
at /home/kuni/snap/rustup/common/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with RUST_BACKTRACE=full for a verbose backtrace.

My build.rs file is this:

extern crate bindgen;
extern crate cc;
extern crate shlex;

use std::env;
use std::path::PathBuf;
use std::process::Command;

const INCLUDED_TYPES: &[&str] = &["file_operations", "ctl_table", "spinlock_t", "mutex", "usb_driver", "usb_device_id", "driver_info"];
const INCLUDED_FUNCTIONS: &[&str] = &[
"__register_chrdev",
"__unregister_chrdev",
"_copy_to_user",
"register_sysctl",
"unregister_sysctl_table",
"proc_dointvec_minmax",
"spin_lock",
"usbnet_probe",
"usbnet_disconnect",
"usb_register_driver",
"usb_deregister",
"usbnet_get_endpoints",
"of_get_mac_address",
"skb_pull",
"skb_push",
"skb_trim",
"skb_clone",
"usbnet_skb_return",
"usbnet_read_cmd",
"call_usermodehelper",
"schedule",
//"__purge_module",
//"__rust_delete_module",
];
const INCLUDED_VARS: &[&str] = &["__this_module", "THIS_MODULE"];

fn main() {
let target = env::var("TARGET").unwrap();
println!("Target={}", target);
let mut builder = bindgen::Builder::default()
.use_core()
.ctypes_prefix("c_types")
.no_copy(".*")
.derive_default(true)
.rustfmt_bindings(true)
.clang_arg(format!("--target={}", target));

let output = String::from_utf8(
Command::new("make")
.arg("-C")
.arg("kernel-cflags-finder")
.arg("-s")
.output()
.unwrap()
.stdout,
)
.unwrap();

Command::new("make")
.arg("-C")
.arg("kernel-cflags-finder")
.arg("clean");

println!("get output:{}", output);
// These three arguments are not supported by clang
// output = output.replace("-mapcs", "");
// output = output.replace("-mno-sched-prolog", "");
// output = output.replace("-mno-thumb-interwork", "");

for arg in shlex::split(&output).unwrap() {
builder = builder.clang_arg(arg.to_string());
}

println!("cargo:rerun-if-changed=src/bindgen_helper.h");
builder = builder.header("src/bindgen_helper.h");

for t in INCLUDED_TYPES {
builder = builder.whitelist_type(t);
}
for f in INCLUDED_FUNCTIONS {
builder = builder.whitelist_function(f);
}
for v in INCLUDED_VARS {
builder = builder.whitelist_var(v);
}
let bindings = builder.generate().expect("Unable to generate bindings");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}

The line tha fails is this

let bindings = builder.generate().expect("Unable to generate bindings");

I'm working on Ubuntu 20.04. Have installed clang, llvm-dev, llvm , libclang-dev, xbuild, nightly toolchain.

Can some help me where to look up in order to understand this error better?

Thanks!

I don't know if this is what's causing your problem, but these lines

    Command::new("make")
        .arg("-C")
        .arg("kernel-cflags-finder")
        .arg("clean");

don't execute anything, they just build up a Command and then throw it away. If you want to run the command and ignore its output and exit status you can do

    let _ = Command::new("make")
        .arg("-C")
        .arg("kernel-cflags-finder")
        .arg("clean")
        .status();

Please use code blocks instead of quote blocks.

1 Like

Thanks for the reply!

I kept exploring possible causes. Seems like the linux-headers have some files missing.

Anyway, I comment some parts and there is a new error around.

thread 'main' panicked at 'libclang error;
possible causes include:

  • Invalid flag syntax`
  • Unrecognized flags
  • Invalid flag arguments
  • File I/O errors
  • Host vs. target architecture mismatch
    If you encounter an error missing from this list, please file an issue or a PR!

How do I format in code style? Thanks!

It's in the pinned post, on the main page of this forum.

Since the pinned posts have a tendency to disappear after having been marked read, here's a link:

Hi guys!

Well, my problem was fixed changing the clang compiler. Seems like the one that I had installed, was a version that have some kind of conflict. So I'm using clang-11 and this was working fine until today.

Now, without change a thing in my repo, I'm facing this issue:

Finished dev [unoptimized + debuginfo] target(s) in 0.34s
ld -r -o /path/to/my/dir/hello-world/hello_world.rust.o --whole-archive /path/to/my/dir/hello-world/target/x86_64-linux-kernel/debug/libhello_world.a
  LD [M]  /path/to/my/dir/hello-world/helloworld.o
  MODPOST /path/to/my/dir/hello-world/Module.symvers
/path/to/my/dir/hello-world/.hello_world.rust.o.cmd: No such file or directory
make[2]: *** [scripts/Makefile.modpost:111: /path/to/my/dir/hello-world/Module.symvers] Error 1
make[1]: *** [Makefile:1693: modules] Error 2
make[1]: se sale del directorio '/usr/src/linux-headers-5.8.0-43-generic'
make: *** [Makefile:9: all] Error 2

Funny thing is my configuration was working few days ago. And today, it doesn't work anymore.

Some suggestions?

Btw, I updated nightly version. There is a new build from yesteday (2021-02-12). Do you know if this could be the issue?

Regards!

Seem like a need the file

.hello_world.rust.o.cmd

Just creating it with touch command, works.