Building a binding for Linux kernel header as well as a sample C header fails


#1

I tried to generate header for a selected subset of Linux kernel headers as well as a sample C header files (the code is uploaded to Github and linked above). However, both the attempts fail with two different set of errors.


Error while building Linux kernel bindings

STD_KERNEL_PATH=’/lib/modules/4.2.0-41-generic/build’ cargo build
Compiling kernel-wrapper v0.1.0 (file:///local/mnt/workspace/dipanjan/kwrap)
error: failed to run custom build command for kernel-wrapper v0.1.0 (file:///local/mnt/workspace/dipanjan/kwrap)
process didn’t exit successfully: /local/mnt/workspace/dipanjan/kwrap/target/debug/build/kernel-wrapper-abf16a8eacafefd0/build-script-build (exit code: 101)
— stdout
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:142:6: error: variable has incomplete type ‘void’, err: true
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:142:12: error: expected ‘;’ after top level declarator, err: true
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:143:11: error: expected ‘;’ after top level declarator, err: true
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:281:6: error: variable has incomplete type ‘void’, err: true
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:281:12: error: expected ‘;’ after top level declarator, err: true
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:282:6: error: variable has incomplete type ‘void’, err: true
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:282:12: error: expected ‘;’ after top level declarator, err: true
/lib/modules/4.2.0-41-generic/build/include/linux/linkage.h:7:10: fatal error: ‘asm/linkage.h’ file not found, err: true

— stderr
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:142:6: error: variable has incomplete type ‘void’
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:142:12: error: expected ‘;’ after top level declarator
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:143:11: error: expected ‘;’ after top level declarator
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:281:6: error: variable has incomplete type ‘void’
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:281:12: error: expected ‘;’ after top level declarator
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:282:6: error: variable has incomplete type ‘void’
/lib/modules/4.2.0-41-generic/build/include/linux/init.h:282:12: error: expected ‘;’ after top level declarator
/lib/modules/4.2.0-41-generic/build/include/linux/linkage.h:7:10: fatal error: ‘asm/linkage.h’ file not found
thread ‘main’ panicked at ‘Unable to generate kernel bindings: ()’, /checkout/src/libcore/result.rs:859
note: Run with RUST_BACKTRACE=1 for a backtrace.

make: *** [all] Error 101


Error while building sample C header file

STD_KERNEL_PATH=’/lib/modules/4.2.0-41-generic/build’ cargo build
Compiling kernel-wrapper v0.1.0 (file:///local/mnt/workspace/dipanjan/kwrap)
error: failed to run custom build command for kernel-wrapper v0.1.0 (file:///local/mnt/workspace/dipanjan/kwrap)
process didn’t exit successfully: /local/mnt/workspace/dipanjan/kwrap/target/debug/build/kernel-wrapper-abf16a8eacafefd0/build-script-build (exit code: 101)
— stderr
thread ‘main’ panicked at ‘function not loaded: clang_Type_getNumTemplateArguments’, /usr2/dipanjan/.cargo/registry/src/github.com-1ecc6299db9ec823/clang-sys-0.19.0/src/lib.rs:1356
note: Run with RUST_BACKTRACE=1 for a backtrace.

make: *** [all] Error 101


#2

Looks like the Linux headers are not compatible with Clang and the __init macro was not expanded.

    142 void __init load_default_modules(void);

I think if you want to use a Clang-based bindings generator with the kernel header, you need to fix them first so that they work with Clang.


#3

To build kernel headers, you need to integrate with KConfig first.
Use a kernel Makefile, such as the one for out of tree modules, to drive your build.


#4

Also, your second error with the missing clang_Type_getNumTemplateArguments indicates you don’t have the right version of Clang for rust-bindgen. IIRC you need libclang-3.9-dev.