"unknown type name" when binding TPM Based Services' tbs.h using bindgen

425 Views Asked by At

I'm trying to bind tbs.h (TPM Based Services) via rust-bindgen.

I have the following header:

#ifndef TPM_WRAPPER_H
#define TPM_WRAPPER_H
#include <tbs.h>
#endif

The build.rs file contains the path to the include directory of the Windows SDK:

use std::env;
use std::path::PathBuf;

fn main() {
    println!("cargo:rustc-link-lib=tbs");
    println!("cargo:rerun-if-changed=include/tpm_wrapper.h");

    let bindings = bindgen::Builder::default()
        .header("include/tpm_wrapper.h")
        .clang_arg("-IC:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared")
        .clang_arg("-v")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");

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

When I try to create the bindings via cargo build and run the build script, I get the following errors:

C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared\tbs.h:50:9: error: unknown type name 'UINT8'
C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared\tbs.h:99:5: error: unknown type name '_In_'
C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared\tbs.h:99:31: error: expected ')'
C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared\tbs.h:98:20: note: to match this '('

Is there some clang configuration missing?

1

There are 1 best solutions below

0
nomad On

The resulting errors are part of the windows-gnu toolchain. Switching to stable-x86_64-pc-windows-msvc did the trick.

rustup toolchain add stable-x86_64-pc-windows-msvc
rustup default stable-x86_64-pc-windows-msvc

The stable-x86_64-pc-windows-msvc requires the windows build tools available from the visual studio installer, or even as standalone.

update

tbs.h obviously needs windows.h for all windows defined types. Due to a bug in bindgen, there is a workaround blacklisting certain functions