SergiusTheBest / FindWDK

CMake module for building drivers with Windows Development Kit (WDK)
BSD 3-Clause "New" or "Revised" License
251 stars 53 forks source link

[Troubleshooting] Cannot build kmdf driver #34

Closed Ty3r0X closed 2 months ago

Ty3r0X commented 2 months ago

Hello! I am attempting to build a personal project of mine https://github.com/Ty3r0X/centipede using just the Visual Studio build tools 2022 installation. I decided to replace my non-functional batch build script with cmake, however when compiling I get:

-- The C compiler identification is MSVC 19.41.34120.0
-- The CXX compiler identification is MSVC 19.41.34120.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.30/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: 'Z:/build/CMakeFiles/CMakeScratch/TryCompile-9ou4ph'

    Run Build Command(s): C:/Users/ty3r0x/AppData/Local/Microsoft/WinGet/Links/ninja.exe -v cmTC_eac2c
    [1/2] C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\cl.exe  /nologo   /DWIN32 /D_WINDOWS  /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\cmTC_eac2c.dir\testCCompiler.c.obj /FdCMakeFiles\cmTC_eac2c.dir\ /FS -c Z:\build\CMakeFiles\CMakeScratch\TryCompile-9ou4ph\testCCompiler.c
    [2/2] C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_eac2c.dir --rc="C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\rc.exe" --mt="C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\mt.exe" --manifests  -- C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_eac2c.dir\testCCompiler.c.obj  /out:cmTC_eac2c.exe /implib:cmTC_eac2c.lib /pdb:cmTC_eac2c.pdb /version:0.0 /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    FAILED: cmTC_eac2c.exe
    C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_eac2c.dir --rc="C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\rc.exe" --mt="C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\mt.exe" --manifests  -- C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_eac2c.dir\testCCompiler.c.obj  /out:cmTC_eac2c.exe /implib:cmTC_eac2c.lib /pdb:cmTC_eac2c.pdb /version:0.0 /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    RC Pass 1: command "C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\rc.exe /fo CMakeFiles\cmTC_eac2c.dir/manifest.res CMakeFiles\cmTC_eac2c.dir/manifest.rc" failed (exit code 0) with the following output:
    no such file or directory
    ninja: build stopped: subcommand failed.

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)

-- Configuring incomplete, errors occurred!

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

project(centipede)

set_property(GLOBAL PROPERTY USER_FOLDERS ON)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4 /WX")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake")
find_package(WDK REQUIRED)

add_subdirectory(src)

src/CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

wdk_add_driver(centipede
  KMDF 1.33
  entrypoint.c
)

Apologies for my rather unprofessional issue, but I kept looking on other resources over to no avail. Thank you!

SergiusTheBest commented 2 months ago

@Ty3r0X I guess it's not compatible with ninja. Try building without it. For example, the samples are built with the following command line:

cmake -Bbuild samples
cmake --build build --parallel --config Release

Also you can install Visual Studio 2022 Community. It's free for driver development or opensource projects.

Ty3r0X commented 2 months ago

You're right, ninja was the culprit! I managed to advance in the setup process, however now I'm getting Unsupported architecture on FindWDK.cmake. Full build log:

Z:\build>cmake ..
-- Selecting Windows SDK version 10.0.26100.0 to target Windows 10.0.19044.
-- The C compiler identification is MSVC 19.41.34120.0
-- The CXX compiler identification is MSVC 19.41.34120.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
[Debug] CMAKE_CURRENT_LIST_DIR: Z:/, CMAKE_MODULE_PATH: Z:/cmake, CMAKE_SIZEOF_VOID_P:
-- WDK_ROOT: C:/Program Files (x86)/Windows Kits/10
-- WDK_VERSION: 10.0.26100.0
CMake Error at make/FindWDK.cmake:112 (message):
  Unsupported architecture
Call Stack (most recent call first):
  CMakeLists.txt:13 (find_package)

-- Configuring incomplete, errors occurred!

This is where I think the problem occours:

if(CMAKE_SIZEOF_VOID_P EQUAL 4)
    list(APPEND WDK_COMPILE_DEFINITIONS "_X86_=1;i386=1;STD_CALL")
    set(WDK_PLATFORM "x86")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
    list(APPEND WDK_COMPILE_DEFINITIONS "_WIN64;_AMD64_;AMD64")
    set(WDK_PLATFORM "x64")
else()
    message(FATAL_ERROR "Unsupported architecture")
endif()

I tried manually displaying CMAKE_SIZEOF_VOID_P's value using message() however it doesn't contain anything :/ which is why I guess it breaks the logic. I have installed cmake 3.30.2 from the official website.

SergiusTheBest commented 2 months ago

Try cleaning cmake caches (delete folders generated by cmake). Also can you build the FindWDK sampes?

Ty3r0X commented 2 months ago

Try cleaning cmake caches (delete folders generated by cmake). Also can you build the FindWDK sampes?

Looks like I can build your samples without any issues:

C:\Users\ty3r0x\Desktop\FindWDK-master>cmake -Bbuild samples
-- Building for: Visual Studio 17 2022
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.

-- Selecting Windows SDK version 10.0.26100.0 to target Windows 10.0.19044.
-- The C compiler identification is MSVC 19.41.34120.0
-- The CXX compiler identification is MSVC 19.41.34120.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found WDK: C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/km/ntddk.h
-- WDK_ROOT: C:/Program Files (x86)/Windows Kits/10
-- WDK_VERSION: 10.0.26100.0
-- Configuring done (3.1s)
-- Generating done (0.1s)
-- Build files have been written to: C:/Users/ty3r0x/Desktop/FindWDK-master/build

C:\Users\ty3r0x\Desktop\FindWDK-master>cmake --build build --parallel --config Release
MSBuild version 17.11.2+c078802d4 for .NET Framework

  1>Checking Build System
  Building Custom Rule C:/Users/ty3r0x/Desktop/FindWDK-master/samples/WdmCppLib/CMakeLists.txt
  WdmCppLib.cpp
  Building Custom Rule C:/Users/ty3r0x/Desktop/FindWDK-master/samples/WdmLib/CMakeLists.txt
  Building Custom Rule C:/Users/ty3r0x/Desktop/FindWDK-master/samples/KmdfCppLib/CMakeLists.txt
  Building Custom Rule C:/Users/ty3r0x/Desktop/FindWDK-master/samples/MinifilterCppDriver/CMakeLists.txt
  WdmLib.c
  KmdfCppLib.cpp
  Main.cpp
  WdmCppLib.vcxproj -> C:\Users\ty3r0x\Desktop\FindWDK-master\build\WdmCppLib\Release\WdmCppLib.lib
  WdmLib.vcxproj -> C:\Users\ty3r0x\Desktop\FindWDK-master\build\WdmLib\Release\WdmLib.lib
  Building Custom Rule C:/Users/ty3r0x/Desktop/FindWDK-master/samples/WdmDriver/CMakeLists.txt
  Main.c
  KmdfCppLib.vcxproj -> C:\Users\ty3r0x\Desktop\FindWDK-master\build\KmdfCppLib\Release\KmdfCppLib.lib
  Building Custom Rule C:/Users/ty3r0x/Desktop/FindWDK-master/samples/WdmCppDriver/CMakeLists.txt
  Building Custom Rule C:/Users/ty3r0x/Desktop/FindWDK-master/samples/WdmIntrinsicFunctions/CMakeLists.txt
  MinifilterCppDriver.vcxproj -> C:\Users\ty3r0x\Desktop\FindWDK-master\build\MinifilterCppDriver\Release\MinifilterCpp
  Driver.sys
  WdmDriver.vcxproj -> C:\Users\ty3r0x\Desktop\FindWDK-master\build\WdmDriver\Release\WdmDriver.sys
  Building Custom Rule C:/Users/ty3r0x/Desktop/FindWDK-master/samples/KmdfCppDriver/CMakeLists.txt
  Main.cpp
  Main.cpp
  Main.cpp
  WdmCppDriver.vcxproj -> C:\Users\ty3r0x\Desktop\FindWDK-master\build\WdmCppDriver\Release\WdmCppDriver.sys
  KmdfCppDriver.vcxproj -> C:\Users\ty3r0x\Desktop\FindWDK-master\build\KmdfCppDriver\Release\KmdfCppDriver.sys
  WdmIntrinsicFunctions.vcxproj -> C:\Users\ty3r0x\Desktop\FindWDK-master\build\WdmIntrinsicFunctions\Release\WdmIntrin
  sicFunctions.sys
  Building Custom Rule C:/Users/ty3r0x/Desktop/FindWDK-master/samples/CMakeLists.txt

Deleting the cache files on my project yields no result. This is my project structure: Z:\CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

project(centipede)

set_property(GLOBAL PROPERTY USER_FOLDERS ON)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4 /WX")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}cmake") #I had to modify this variable so CMAKE_MODULE_PATH is generated properly
message("[Debug] CMAKE_CURRENT_LIST_DIR: ${CMAKE_CURRENT_LIST_DIR}, CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}, CMAKE_SIZEOF_VOID_P: ${CMAKE_SIZEOF_VOID_P}")

find_package(WDK REQUIRED)

add_subdirectory(src)

Z:\src\CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

wdk_add_driver(centipede
  entrypoint.c
)

FindWDK is located in Z:\cmake\FindWDK.cmake. Thank you for your help, I will continue to investigate!

Ty3r0X commented 1 month ago

I found the issue! For some bizzare reason, transferring my project from my mounted Z: drive to my desktop solved the CMAKE_SIZEOF_VOID_P issue. So for those who have this niche build enviroment of mounting an external folder (host to vm), bear that in mind!