microsoft / windows-drivers-rs

Platform that enables Windows driver development in Rust. Developed by Surface.
Apache License 2.0
1.49k stars 65 forks source link

Bindgen Clang compile error in wdk-sys - missing preprocessor defines #107

Closed polloloco closed 8 months ago

polloloco commented 8 months ago

I had trouble compiling my own test driver, so I wanted to compile the sample-kmdf-driver crate to see if the problem is with my driver.

But I get the same error from that project (full output below):

Bindings should succeed to generate: ClangDiagnostic("E:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/ntifs.h:25:2: error: Compiler version not supported by Windows DDK...

I'm running Windows 10 LTSC, with the latest (10.0.22621) EWDK iso mounted to E: It looks like the compiler doesn't set preprocessor defines such as _MSC_VER for some reason

C:\dev\rust-kernel\windows-drivers-rs\crates\sample-kmdf-driver>cargo make
[cargo-make] INFO - cargo make 0.37.9
[cargo-make] INFO - Calling cargo metadata to extract project info
[cargo-make] INFO - Cargo metadata done
[cargo-make] INFO - Project: sample-kmdf-driver
[cargo-make] INFO - Build File: Makefile.toml
[cargo-make] INFO - Task: default
[cargo-make] INFO - Profile: development
[cargo-make] INFO - Running Task: legacy-migration
Building and packaging driver: sample-kmdf-driver...
[cargo-make] INFO - Running Task: default
[cargo-make] INFO - Running Task: build
[cargo-make] INFO - Execute Command: "cargo" "build"
   Compiling wdk-sys v0.2.0 (C:\dev\rust-kernel\windows-drivers-rs\crates\wdk-sys)
error: failed to run custom build command for `wdk-sys v0.2.0 (C:\dev\rust-kernel\windows-drivers-rs\crates\wdk-sys)`
note: To improve backtraces for build dependencies, set the CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.

Caused by:
  process didn't exit successfully: `C:\dev\rust-kernel\windows-drivers-rs\target\debug\build\wdk-sys-83dd066b1c411ae7\build-script-build` (exit code: 101)
  --- stdout
  cargo:rerun-if-env-changed=TARGET
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64-pc-windows-msvc
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64_pc_windows_msvc
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS
  cargo:rerun-if-changed=src/ntddk-input.h
  cargo:rerun-if-changed=src/wdf-input.h

  --- stderr
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/ntifs.h:25:2: error: Compiler version not supported by Windows DDK
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/ntddk.h:28:2: error: Compiler version not supported by Windows DDK
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:38:2: error: Compiler version not supported by Windows DDK
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\shared/basetsd.h:318:33: warning: cast to smaller integer type 'unsigned long' from 'const void *' [-Wvoid-pointer-to-int-cast]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\shared/basetsd.h:327:32: warning: cast to smaller integer type 'long' from 'const void *' [-Wvoid-pointer-to-int-cast]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\shared/basetsd.h:336:12: warning: cast to 'void *' from smaller integer type 'unsigned long' [-Wint-to-void-pointer-cast]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\shared/ntdef.h:1390:2: error: Must define a target architecture.
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:4864:19: error: call to undeclared function 'ReadAcquire8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:4875:19: error: call to undeclared function 'ReadNoFence8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:4886:21: error: call to undeclared function 'ReadAcquire8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:4897:21: error: call to undeclared function 'ReadNoFence8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:4930:5: error: call to undeclared function 'WriteRelease8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:4943:5: error: call to undeclared function 'WriteNoFence8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:4956:5: error: call to undeclared function 'WriteRelease8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:4969:5: error: call to undeclared function 'WriteNoFence8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:4994:20: error: call to undeclared function 'ReadAcquire16'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:5005:20: error: call to undeclared function 'ReadNoFence16'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:5028:5: error: call to undeclared function 'WriteRelease16'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:5041:5: error: call to undeclared function 'WriteNoFence16'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:5066:19: error: call to undeclared function 'ReadAcquire'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:5077:19: error: call to undeclared function 'ReadNoFence'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  E:\Program Files\Windows Kits\10\Include\10.0.22621.0\km/wdm.h:5100:5: error: call to undeclared function 'WriteRelease'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  fatal error: too many errors emitted, stopping now [-ferror-limit=]
  clang diag: E:\Program Files\Windows Kits\10\Include\10.0.22621.0\shared/basetsd.h:318:33: warning: cast to smaller integer type 'unsigned long' from 'const void *' [-Wvoid-pointer-to-int-cast]
  clang diag: E:\Program Files\Windows Kits\10\Include\10.0.22621.0\shared/basetsd.h:327:32: warning: cast to smaller integer type 'long' from 'const void *' [-Wvoid-pointer-to-int-cast]
  clang diag: E:\Program Files\Windows Kits\10\Include\10.0.22621.0\shared/basetsd.h:336:12: warning: cast to 'void *' from smaller integer type 'unsigned long' [-Wint-to-void-pointer-cast]
  thread 'main' panicked at crates\wdk-sys\build.rs:27:14:
  Bindings should succeed to generate: ClangDiagnostic("E:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/ntifs.h:25:2: error: Compiler version not supported by Windows DDK\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/ntddk.h:28:2: error: Compiler version not supported by Windows DDK\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:38:2: error: Compiler version not supported by Windows DDK\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\shared/ntdef.h:1390:2: error: Must define a target architecture.\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:4864:19: error: call to undeclared function 'ReadAcquire8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:4875:19: error: call to undeclared function 'ReadNoFence8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:4886:21: error: call to undeclared function 'ReadAcquire8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:4897:21: error: call to undeclared function 'ReadNoFence8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:4930:5: error: call to undeclared function 'WriteRelease8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:4943:5: error: call to undeclared function 'WriteNoFence8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:4956:5: error: call to undeclared function 'WriteRelease8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:4969:5: error: call to undeclared function 'WriteNoFence8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:4994:20: error: call to undeclared function 'ReadAcquire16'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:5005:20: error: call to undeclared function 'ReadNoFence16'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:5028:5: error: call to undeclared function 'WriteRelease16'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:5041:5: error: call to undeclared function 'WriteNoFence16'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:5066:19: error: call to undeclared function 'ReadAcquire'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:5077:19: error: call to undeclared function 'ReadNoFence'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nE:\\Program Files\\Windows Kits\\10\\Include\\10.0.22621.0\\km/wdm.h:5100:5: error: call to undeclared function 'WriteRelease'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]\nfatal error: too many errors emitted, stopping now [-ferror-limit=]\n")
  stack backtrace:
     0:     0x7ff68772b1b3 - std::sys_common::backtrace::_print::impl$0::fmt
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\sys_common\backtrace.rs:44
     1:     0x7ff68774c49d - core::fmt::rt::Argument::fmt
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\core\src\fmt\rt.rs:142
     2:     0x7ff68774c49d - core::fmt::write
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\core\src\fmt\mod.rs:1120
     3:     0x7ff6877268a1 - std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\io\mod.rs:1762
     4:     0x7ff68772afba - std::sys_common::backtrace::_print
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\sys_common\backtrace.rs:47
     5:     0x7ff68772afba - std::sys_common::backtrace::print
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\sys_common\backtrace.rs:34
     6:     0x7ff68772d509 - std::panicking::default_hook::closure$1
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\panicking.rs:272
     7:     0x7ff68772d1cb - std::panicking::default_hook
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\panicking.rs:292
     8:     0x7ff68772d9f4 - std::panicking::rust_panic_with_hook
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\panicking.rs:779
     9:     0x7ff68772d8c5 - std::panicking::begin_panic_handler::closure$0
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\panicking.rs:657
    10:     0x7ff68772baa9 - std::sys_common::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\sys_common\backtrace.rs:170
    11:     0x7ff68772d5d4 - std::panicking::begin_panic_handler
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\panicking.rs:645
    12:     0x7ff687755147 - core::panicking::panic_fmt
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\core\src\panicking.rs:72
    13:     0x7ff687755703 - core::result::unwrap_failed
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\core\src\result.rs:1653
    14:     0x7ff6870b78b2 - core::result::Result<T,E>::expect::h0d737977ad35a758
    15:     0x7ff6870b7e1c - <tracing_core::span::Id as core::clone::Clone>::clone::h6924d960d20cc670
    16:     0x7ff6870b9651 - <tracing_core::span::Id as core::clone::Clone>::clone::h6924d960d20cc670
    17:     0x7ff6870b25a6 - core::ops::function::FnOnce::call_once::h2363bccbde1a8686
    18:     0x7ff6870b5259 - std::sys_common::backtrace::__rust_begin_short_backtrace::h01d5257eb604a13c
    19:     0x7ff6870b330c - std::rt::lang_start::{{closure}}::h0741405682e202fc
    20:     0x7ff6877216a8 - std::rt::lang_start_internal::closure$2
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\rt.rs:148
    21:     0x7ff6877216a8 - std::panicking::try::do_call
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\panicking.rs:552
    22:     0x7ff6877216a8 - std::panicking::try
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\panicking.rs:516
    23:     0x7ff6877216a8 - std::panic::catch_unwind
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\panic.rs:142
    24:     0x7ff6877216a8 - std::rt::lang_start_internal
                                 at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\rt.rs:148
    25:     0x7ff6870b32e7 - std::rt::lang_start::hdc2616c860b3953a
    26:     0x7ff6870ba069 - main
    27:     0x7ff687752a60 - invoke_main
                                 at d:\a01\_work\43\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    28:     0x7ff687752a60 - __scrt_common_main_seh
                                 at d:\a01\_work\43\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    29:     0x7ffde0ab7344 - BaseThreadInitThunk
    30:     0x7ffde28226b1 - RtlUserThreadStart
[cargo-make] ERROR - Error while executing command, exit code: 101
[cargo-make] WARN - Build Failed.
wmmc88 commented 8 months ago

What is the output of rustup show? Are you using a msvc rust toolchain?

polloloco commented 8 months ago

Yes, msvc

Default host: x86_64-pc-windows-msvc
rustup home:  C:\Users\User\.rustup

installed toolchains
--------------------

stable-x86_64-pc-windows-msvc (default)
nightly-x86_64-pc-windows-msvc
esp

installed targets for active toolchain
--------------------------------------

riscv32imc-unknown-none-elf
x86_64-pc-windows-msvc

active toolchain
----------------

stable-x86_64-pc-windows-msvc (default)
rustc 1.75.0 (82e1608df 2023-12-21)
wmmc88 commented 8 months ago

I'm unable to repro this error locally. I suspect it has to do with your installation of msvc. A couple questions:

polloloco commented 8 months ago

cargo build is failing the same way. Setting the environment variable doesn't provide any additional details.

Yes I'm running from the ewdk developer prompt. I have Visual Studio 2022 Enterprise (version 17.8.6) installed.

My visual studio install configuration:

{
  "version": "1.0",
  "components": [
    "Microsoft.VisualStudio.Component.Roslyn.Compiler",
    "Microsoft.Component.MSBuild",
    "Microsoft.VisualStudio.Component.SQL.LocalDB.Runtime",
    "Microsoft.VisualStudio.Component.CoreEditor",
    "Microsoft.VisualStudio.Workload.CoreEditor",
    "Microsoft.VisualStudio.Component.TextTemplating",
    "Microsoft.VisualStudio.Component.IntelliTrace.FrontEnd",
    "Microsoft.VisualStudio.Component.Debugger.JustInTime",
    "Microsoft.VisualStudio.Component.ClassDesigner",
    "Microsoft.VisualStudio.Component.GraphDocument",
    "Microsoft.VisualStudio.Component.CodeMap",
    "Microsoft.VisualStudio.Component.VC.CoreIde",
    "Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
    "Microsoft.VisualStudio.Component.Graphics.Tools",
    "Microsoft.VisualStudio.Component.VC.DiagnosticTools",
    "Microsoft.VisualStudio.Component.Windows11SDK.22621",
    "Microsoft.VisualStudio.Component.VC.ATL",
    "Microsoft.VisualStudio.ComponentGroup.ArchitectureTools.Native",
    "Microsoft.VisualStudio.Component.VC.Redist.14.Latest",
    "Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core",
    "Microsoft.VisualStudio.ComponentGroup.WebToolsExtensions.CMake",
    "Microsoft.VisualStudio.Component.VC.CMake.Project",
    "Microsoft.VisualStudio.Component.VC.ASAN",
    "Microsoft.VisualStudio.Component.Vcpkg",
    "Microsoft.VisualStudio.Component.VC.Llvm.ClangToolset",
    "Microsoft.VisualStudio.Component.VC.Llvm.Clang",
    "Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Llvm.Clang",
    "Microsoft.VisualStudio.Workload.NativeDesktop",
    "Microsoft.VisualStudio.Component.VC.ATL.Spectre",
    "Microsoft.VisualStudio.Component.VC.ATLMFC.Spectre",
    "Microsoft.VisualStudio.Component.VC.Runtimes.x86.x64.Spectre"
  ]
}

I'll try to reproduce the error in a VM tomorrow

polloloco commented 8 months ago

I figured it out. The first time I ran it inside my VM, it didn't work because I forgot to install llvm. But it gave me an interesting error: Unable to find libclang: "couldn't find any valid shared libraries matching: ['clang.dll', 'libclang.dll'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: [])"

After installing llvm it compiled in the VM.

So now I went to my machine and checked the LIBCLANG_PATH, and to my surprise it didn't point to my llvm installation, but rather to C:\Users\User\.rustup\toolchains\esp\xtensa-esp32-elf-clang\esp-clang\bin\libclang.dll Looks like the esp32 rust toolchain sets that env var to their custom (patched) clang version.

I changed the env var in my terminal to point to my llvm installation like this set LIBCLANG_PATH=C:\Program Files\LLVM\bin\libclang.dll and tried to run cargo make again, and now it works :)

Yesterday I had a feeling the esp toolchain might be interfering so I checked PATH to make sure the regular llvm installation comes first. That didn't help though. Also removing all esp related things from PATH didn't help, looks like I also have to make sure LIBCLANG_PATH is correct.

wmmc88 commented 8 months ago

Good to hear you solved your issue!