0

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
nomad
  • 131
  • 1
  • 8
  • Unfortunately, the [winapi](https://github.com/retep998/winapi-rs/issues/787) crate is still missing the binding. – nomad Jul 28 '20 at 09:47
  • Try to `#include ` ahead of ``. That should take care of the primitive type-defs, though I'm not sure it will also include the respective header(s) for the SAL annotations (`_In_`, etc.). – IInspectable Jul 28 '20 at 10:39
  • I tried it, but received new errors. According to [rust ffi](https://anssi-fr.github.io/rust-guide/07_ffi.html) the SAL annotated code shall be handled accordingly. – nomad Jul 28 '20 at 11:05

1 Answers1

0

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

nomad
  • 131
  • 1
  • 8