# Add WindowsKernel platform support

**URL:** https://discourse.cmake.org/t/add-windowskernel-platform-support/2833
**Category:** Development
**Tags:** os:windows
**Created:** [February 24, 2021, 4:53pm UTC](https://discourse.cmake.org/t/add-windowskernel-platform-support/2833 "2021-02-24T16:53:44Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://discourse.cmake.org/u/sean)
#### Post date: [February 24, 2021, 4:53pm UTC](https://discourse.cmake.org/t/add-windowskernel-platform-support/2833/1 "2021-02-24T16:53:45Z")

</div>

I’m utilizing CMake to build Windows kernel drivers, but currently I have to drop a `WindowsKernel.cmake` into the Modules/Platforms dir to make it work. Could the following be added to CMake to support his natively?

Modules/Platforms/WindowsKernel.cmake:

```auto
set(WIN32 1)

if(CMAKE_VERBOSE_MAKEFILE)
  set(CMAKE_CL_NOLOGO)
else()
  set(CMAKE_CL_NOLOGO "/nologo")
endif()

set(WINDOWS_KERNEL 1)

set(CMAKE_STATIC_LIBRARY_PREFIX "")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".lib")
set(CMAKE_SHARED_LIBRARY_PREFIX "")
set(CMAKE_SHARED_LIBRARY_SUFFIX ".dll")
set(CMAKE_IMPORT_LIBRARY_PREFIX "")
set(CMAKE_IMPORT_LIBRARY_SUFFIX ".lib")
set(CMAKE_EXECUTABLE_SUFFIX ".sys")
set(CMAKE_LINK_LIBRARY_SUFFIX ".lib")
set(CMAKE_DL_LIBS "")
set(CMAKE_EXTRA_LINK_EXTENSIONS ".targets")

set(CMAKE_FIND_LIBRARY_PREFIXES "")
set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")

set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "/LIB")
set(CMAKE_LIBRARY_PATH_FLAG "-LIBPATH:")
set(CMAKE_LINK_LIBRARY_FLAG)

set(CMAKE_C_STANDARD_LIBRARIES_INIT "BufferOverflowK.lib ntoskrnl.lib hal.lib wmilib.lib")
set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "${CMAKE_C_STANDARD_LIBRARIES_INIT}")

set(CMAKE_C_CREATE_SHARED_MODULE
    "<CMAKE_C_COMPILER> ${CMAKE_CL_NOLOGO} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> <SONAME_FLAG><TARGET_SONAME> /OUT:<TARGET> <OBJECTS> <LINK_LIBRARIES>")

set(CMAKE_C_CREATE_SHARED_LIBRARY
    "<CMAKE_LINKER> ${CMAKE_CL_NOLOGO} <OBJECTS> /OUT:<TARGET> /IMPLIB:<TARGET_IMPLIB> /PDB:<TARGET_PDB> /VERSION:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <LINK_FLAGS> <LINK_LIBRARIES>")

set(CMAKE_C_CREATE_STATIC_LIBRARY
    "<CMAKE_LINKER> /LIB ${CMAKE_CL_NOLOGO} <LINK_FLAGS> /OUT:<TARGET> <OBJECTS>")

set(CMAKE_C_COMPILE_OBJECT
    "<CMAKE_C_COMPILER> ${CMAKE_CL_NOLOGO} /Fo<OBJECT> /c <SOURCE> <DEFINES> <INCLUDES> <FLAGS>")

set(CMAKE_C_LINK_EXECUTABLE
    "<CMAKE_LINKER> ${CMAKE_CL_NOLOGO} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> /OUT:<TARGET> <LINK_LIBRARIES>")

include(Platform/WindowsPaths)

```

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [February 24, 2021, 6:48pm UTC](https://discourse.cmake.org/t/add-windowskernel-platform-support/2833/2 "2021-02-24T18:48:46Z")

</div>

I think the fundamental problem is I don’t believe CMake doesn’t registers WDK like it does with the SDK.

I’m not really sure what your code is even trying to address.

WDK = Windows Driver Kit

> **[Download the Windows Driver Kit (WDK) - Windows drivers](https://docs.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk)**
>
> Download instructions for the latest released version of the Windows Driver Kit (WDK)

---

<div class="post-metadata">

### Author: ![sean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://discourse.cmake.org/u/sean)
#### Post date: [February 24, 2021, 7:15pm UTC](https://discourse.cmake.org/t/add-windowskernel-platform-support/2833/3 "2021-02-24T19:15:23Z")

</div>

I decouple toolchain specs in my projects. For example, this is a platform toolchain spec for `x64-winkernel`:

```auto
# Platform
set(CMAKE_SYSTEM_PROCESSOR x86_64)
set(CMAKE_SYSTEM_NAME WindowsKernel)

# WDK path
set(WINDOWS_WDK_PATH "C:/path/to/toolchains/WDK/7.1")

# Tools
set(CMAKE_C_COMPILER "${WINDOWS_WDK_PATH}/bin/x86/amd64/cl.exe" CACHE PATH "")
set(CMAKE_CXX_COMPILER "${WINDOWS_WDK_PATH}/bin/x86/amd64/cl.exe" CACHE PATH "")
set(CMAKE_ASM_COMPILER "${WINDOWS_WDK_PATH}/bin/x86/amd64/ml64.exe" CACHE PATH "")
set(CMAKE_LINKER "${WINDOWS_WDK_PATH}/bin/x86/amd64/link.exe" CACHE PATH "")

set(CMAKE_C_COMPILER_WORKS True)
set(CMAKE_CXX_COMPILER_WORKS True)

# Clear envar that influence toolchain behaviour
set(ENV{INCLUDE})
set(ENV{LIB})
set(ENV{LIBPATH})

# Include paths
include_directories(
    "${WINDOWS_WDK_PATH}/inc/api"
    "${WINDOWS_WDK_PATH}/inc/mfc42"
    "${WINDOWS_WDK_PATH}/inc/ddk"
    "${WINDOWS_WDK_PATH}/inc/crt"
    "${WINDOWS_WDK_PATH}/inc/win7"
    )

# Lib paths
link_directories(
    "${WINDOWS_WDK_PATH}/lib/crt/amd64"
    "${WINDOWS_WDK_PATH}/lib/win7/amd64"
    )

```

The compiler and linker flags are further decoupled, but above provides the interface you noted.

---

<div class="post-metadata">

### Author: ![sean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://discourse.cmake.org/u/sean)
#### Post date: [February 24, 2021, 7:33pm UTC](https://discourse.cmake.org/t/add-windowskernel-platform-support/2833/4 "2021-02-24T19:33:32Z")

</div>

> [@YMba9g8j9CJp0wLoQf5y](#):
>
> I’m not really sure what your code is even trying to address.

To answer this: I may be wrong, but as you said CMake does not have any inbuilt registration for WDK like it does the SDK. Extrapolate this, ignoring my toolchain spec to make this work, to how CMake handles platform types. Unless you explicitly add a `WindowsKernel.cmake` file to Modules/Platform there is no way to specify a platform target. This will result in what ultimately led me here:

```auto
System is unknown to cmake, create:
Platform/WindowsKernel to use this system, please post your config file on discourse.cmake.org so it can be added to cmake

```

I’ve been manually patching `WindowsKernel.cmake` into Modules/Platform but this is cumbersome as we have multiple users and it often gets forgotten.

Alternatively, is there any way to specify platform systems outside of CMake? I tried multiple approaches, but it seems CMake is hardcoded to only consider Modules/Platform.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 24, 2021, 9:39pm UTC](https://discourse.cmake.org/t/add-windowskernel-platform-support/2833/5 "2021-02-24T21:39:36Z")

</div>

This should probably be filed as [an issue](https://gitlab.kitware.com/cmake/cmake/-/issues) for consideration of integration and testing. [This discussion](https://discourse.cmake.org/t/unable-to-fully-build-windows-umdf-driver-using-cmake/2368) is probably relevant too.

---

<div class="post-metadata">

### Author: ![sean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://discourse.cmake.org/u/sean)
#### Post date: [February 25, 2021, 2:49am UTC](https://discourse.cmake.org/t/add-windowskernel-platform-support/2833/6 "2021-02-25T02:49:28Z")

</div>

Sorry @ben.boeckel I wasn’t sure where to post this.

Thanks for posting the UMDF discussion. This would effectively be covered by adding `inc/wdf/kmdf` and `inc/wdf/umdf` to the `include_directories` and `lib/wdf/kmdf/amd64/1.9` to the `link_directories` in my example above, or alternatively using the `i386`/`ia64` targets if a non-`amd64` target is desired.

The `FindWDK.cmake` from that discussion is relevant as well, it handles UMDF includes if `WDK_KMDF` is defined in a similar fashion to what I described above. I’m specifically using WDK 7.1 for legacy OS coverage, there have been changes to the directory layout of newer WDKs.

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [February 25, 2021, 5:01pm UTC](https://discourse.cmake.org/t/add-windowskernel-platform-support/2833/7 "2021-02-25T17:01:06Z")

</div>

At our workplace we have custom toolchain files as well in order to build against the WDK.

And we don’t build for legacy OS coverage we always need to be using latest or close to latest.

---

<div class="post-metadata">

### Author: ![leha-bot](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leha-bot/32/921_2.png) [@leha-bot](https://discourse.cmake.org/u/leha-bot)
#### Post date: [January 16, 2022, 3:02pm UTC](https://discourse.cmake.org/t/add-windowskernel-platform-support/2833/8 "2022-01-16T15:02:25Z")

</div>

Hello, could you try to contribute it into the upstream CMake (avoiding the NDA code)? Thanks!
