Skip to content

Commit

Permalink
Uses Ninja to build on Windows (obhq#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Mar 31, 2024
1 parent cad411f commit c04768c
Show file tree
Hide file tree
Showing 7 changed files with 508 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Install Ninja
run: choco install ninja
- name: Generate cache keys
run: |
echo "cargo=${{ runner.os }}-cargo" >> $env:GITHUB_OUTPUT
Expand Down
20 changes: 12 additions & 8 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
{
"name": "windows",
"inherits": "default",
"generator": "Visual Studio 17 2022",
"architecture": "x64",
"generator": "Ninja",
"toolchainFile": "cmake/Windows.MSVC.toolchain.cmake",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
Expand All @@ -21,11 +21,17 @@
},
{
"name": "windows-release",
"inherits": "windows"
"inherits": "windows",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "windows-debug",
"inherits": "windows"
"inherits": "windows",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "linux",
Expand Down Expand Up @@ -79,13 +85,11 @@
"buildPresets": [
{
"name": "windows-release",
"configurePreset": "windows-release",
"configuration": "Release"
"configurePreset": "windows-release"
},
{
"name": "windows-debug",
"configurePreset": "windows-debug",
"configuration": "Debug"
"configurePreset": "windows-debug"
},
{
"name": "linux-release",
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Obliteration supports only 4KB/8KB/16KB pages. Most people should not have any p
- Rust on the latest stable channel
- CMake 3.21+
- Make sure you have `Add CMake to the system PATH` selected when installing
- [Ninja](https://ninja-build.org)
- Make sure Ninja is added to `PATH`

### Linux prerequisites

Expand Down
132 changes: 132 additions & 0 deletions cmake/VSWhere.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#----------------------------------------------------------------------------------------------------------------------
# MIT License
#
# Copyright (c) 2021 Mark Schofield
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#----------------------------------------------------------------------------------------------------------------------
include_guard()

#[[====================================================================================================================
toolchain_validate_vs_files
---------------------------

Note: Not supported for consumption outside of the toolchain files.

Validates the the specified folder exists and contains the specified files.

toolchain_validate_vs_files(
<DESCRIPTION <description>>
<FOLDER <folder>>
<FILES <file>...>
)

If the folder or files are missing, then a FATAL_ERROR is reported.
====================================================================================================================]]#
function(toolchain_validate_vs_files)
set(OPTIONS)
set(ONE_VALUE_KEYWORDS FOLDER DESCRIPTION)
set(MULTI_VALUE_KEYWORDS FILES)

cmake_parse_arguments(PARSE_ARGV 0 VS "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")

if(NOT EXISTS ${VS_FOLDER})
message(FATAL_ERROR "Folder not present - ${VS_FOLDER} - ensure that the ${VS_DESCRIPTION} are installed with Visual Studio.")
endif()

foreach(FILE ${VS_FILES})
if(NOT EXISTS "${VS_FOLDER}/${FILE}")
message(FATAL_ERROR "File not present - ${VS_FOLDER}/${FILE} - ensure that the ${VS_DESCRIPTION} are installed with Visual Studio.")
endif()
endforeach()
endfunction()

#[[====================================================================================================================
findVisualStudio
----------------

Finds a Visual Studio instance, and sets CMake variables based on properties of the found instance.

findVisualStudio(
[VERSION <version range>]
[PRERELEASE <ON|OFF>]
[PRODUCTS <products>]
[REQUIRES <vs component>...]
PROPERTIES
<<vswhere property> <cmake variable>>
)
====================================================================================================================]]#
function(findVisualStudio)
set(OPTIONS)
set(ONE_VALUE_KEYWORDS VERSION PRERELEASE PRODUCTS)
set(MULTI_VALUE_KEYWORDS REQUIRES PROPERTIES)

cmake_parse_arguments(PARSE_ARGV 0 FIND_VS "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")

find_program(VSWHERE_PATH
NAMES vswhere vswhere.exe
HINTS "$ENV{ProgramFiles\(x86\)}/Microsoft Visual Studio/Installer"
)

if(VSWHERE_PATH STREQUAL "VSWHERE_PATH-NOTFOUND")
message(FATAL_ERROR "'vswhere' isn't found.")
endif()

set(VSWHERE_COMMAND ${VSWHERE_PATH} -latest)

if(FIND_VS_PRERELEASE)
list(APPEND VSWHERE_COMMAND -prerelease)
endif()

if(FIND_VS_PRODUCTS)
list(APPEND VSWHERE_COMMAND -products ${FIND_VS_PRODUCTS})
endif()

if(FIND_VS_REQUIRES)
list(APPEND VSWHERE_COMMAND -requires ${FIND_VS_REQUIRES})
endif()

if(FIND_VS_VERSION)
list(APPEND VSWHERE_COMMAND -version "${FIND_VS_VERSION}")
endif()

message(VERBOSE "findVisualStudio: VSWHERE_COMMAND = ${VSWHERE_COMMAND}")

execute_process(
COMMAND ${VSWHERE_COMMAND}
OUTPUT_VARIABLE VSWHERE_OUTPUT
)

message(VERBOSE "findVisualStudio: VSWHERE_OUTPUT = ${VSWHERE_OUTPUT}")

# Matches `VSWHERE_PROPERTY` in the `VSWHERE_OUTPUT` text in the format written by vswhere.
# The matched value is assigned to the variable `VARIABLE_NAME` in the parent scope.
function(getVSWhereProperty VSWHERE_OUTPUT VSWHERE_PROPERTY VARIABLE_NAME)
string(REGEX MATCH "${VSWHERE_PROPERTY}: [^\r\n]*" VSWHERE_VALUE "${VSWHERE_OUTPUT}")
string(REPLACE "${VSWHERE_PROPERTY}: " "" VSWHERE_VALUE "${VSWHERE_VALUE}")
set(${VARIABLE_NAME} "${VSWHERE_VALUE}" PARENT_SCOPE)
endfunction()

while(FIND_VS_PROPERTIES)
list(POP_FRONT FIND_VS_PROPERTIES VSWHERE_PROPERTY)
list(POP_FRONT FIND_VS_PROPERTIES VSWHERE_CMAKE_VARIABLE)
getVSWhereProperty("${VSWHERE_OUTPUT}" ${VSWHERE_PROPERTY} VSWHERE_VALUE)
set(${VSWHERE_CMAKE_VARIABLE} ${VSWHERE_VALUE} PARENT_SCOPE)
endwhile()
endfunction()
141 changes: 141 additions & 0 deletions cmake/Windows.Kits.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#----------------------------------------------------------------------------------------------------------------------
# MIT License
#
# Copyright (c) 2021 Mark Schofield
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#----------------------------------------------------------------------------------------------------------------------
#
# | CMake Variable | Description |
# |-----------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
# | CMAKE_SYSTEM_VERSION | The version of the operating system for which CMake is to build. Defaults to the host version. |
# | CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE | The architecture of the tooling to use. Defaults to x64. |
# | CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION | The version of the Windows SDK to use. Defaults to the highest installed, that is no higher than CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM |
# | CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM | The maximum version of the Windows SDK to use, for example '10.0.19041.0'. Defaults to nothing |
# | CMAKE_WINDOWS_KITS_10_DIR | The location of the root of the Windows Kits 10 directory. |
#
# The following variables will be set:
#
# | CMake Variable | Description |
# |---------------------------------------------|-------------------------------------------------------------------------------------------------------|
# | CMAKE_MT | The path to the 'mt' tool. |
# | CMAKE_RC_COMPILER | The path to the 'rc' tool. |
# | CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION | The version of the Windows SDK to be used. |
# | MDMERGE_TOOL | The path to the 'mdmerge' tool. |
# | MIDL_COMPILER | The path to the 'midl' compiler. |
# | WINDOWS_KITS_BIN_PATH | The path to the folder containing the Windows Kits binaries. |
# | WINDOWS_KITS_INCLUDE_PATH | The path to the folder containing the Windows Kits include files. |
# | WINDOWS_KITS_LIB_PATH | The path to the folder containing the Windows Kits library files. |
# | WINDOWS_KITS_REFERENCES_PATH | The path to the folder containing the Windows Kits references. |
#
include_guard()

if(NOT CMAKE_SYSTEM_VERSION)
set(CMAKE_SYSTEM_VERSION ${CMAKE_HOST_SYSTEM_VERSION})
endif()

if(NOT CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE)
set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE x64)
endif()

if(NOT CMAKE_WINDOWS_KITS_10_DIR)
get_filename_component(CMAKE_WINDOWS_KITS_10_DIR "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\v10.0;InstallationFolder]" ABSOLUTE CACHE)
if ("${CMAKE_WINDOWS_KITS_10_DIR}" STREQUAL "/registry")
unset(CMAKE_WINDOWS_KITS_10_DIR)
endif()
endif()

if(NOT CMAKE_WINDOWS_KITS_10_DIR)
message(FATAL_ERROR "Unable to find an installed Windows SDK, and one wasn't specified.")
endif()

# If a CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION wasn't specified, find the highest installed version that is no higher
# than the host version
if(NOT CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
file(GLOB WINDOWS_KITS_VERSIONS RELATIVE "${CMAKE_WINDOWS_KITS_10_DIR}/lib" "${CMAKE_WINDOWS_KITS_10_DIR}/lib/*")
list(FILTER WINDOWS_KITS_VERSIONS INCLUDE REGEX "10\\.0\\.")
list(SORT WINDOWS_KITS_VERSIONS COMPARE NATURAL ORDER DESCENDING)
while(WINDOWS_KITS_VERSIONS)
list(POP_FRONT WINDOWS_KITS_VERSIONS CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
if(NOT CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM)
message(VERBOSE "Windows.Kits: Defaulting version: ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
break()
endif()

if(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION VERSION_LESS_EQUAL CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM)
message(VERBOSE "Windows.Kits: Choosing version: ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
break()
endif()

message(VERBOSE "Windows.Kits: Not suitable: ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
set(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
endwhile()
endif()

if(NOT CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
message(FATAL_ERROR "A Windows SDK could not be found.")
endif()

set(WINDOWS_KITS_BIN_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/bin/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" CACHE PATH "" FORCE)
set(WINDOWS_KITS_INCLUDE_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/include/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" CACHE PATH "" FORCE)
set(WINDOWS_KITS_LIB_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/lib/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" CACHE PATH "" FORCE)
set(WINDOWS_KITS_REFERENCES_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/References" CACHE PATH "" FORCE)
set(WINDOWS_KITS_PLATFORM_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/Platforms/UAP/${CMAKE_SYSTEM_VERSION}/Platform.xml" CACHE PATH "" FORCE)

if(NOT EXISTS ${WINDOWS_KITS_BIN_PATH})
message(FATAL_ERROR "Windows SDK ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} cannot be found: Folder '${WINDOWS_KITS_BIN_PATH}' does not exist.")
endif()

if(NOT EXISTS ${WINDOWS_KITS_INCLUDE_PATH})
message(FATAL_ERROR "Windows SDK ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} cannot be found: Folder '${WINDOWS_KITS_INCLUDE_PATH}' does not exist.")
endif()

if(NOT EXISTS ${WINDOWS_KITS_LIB_PATH})
message(FATAL_ERROR "Windows SDK ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} cannot be found: Folder '${WINDOWS_KITS_LIB_PATH}' does not exist.")
endif()

set(CMAKE_MT "${WINDOWS_KITS_BIN_PATH}/${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}/mt.exe")
set(CMAKE_RC_COMPILER_INIT "${WINDOWS_KITS_BIN_PATH}/${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}/rc.exe")
set(CMAKE_RC_FLAGS_INIT "/nologo")

set(MIDL_COMPILER "${WINDOWS_KITS_BIN_PATH}/${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}/midl.exe")
set(MDMERGE_TOOL "${WINDOWS_KITS_BIN_PATH}/${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}/mdmerge.exe")

# Windows SDK
if((CMAKE_SYSTEM_PROCESSOR STREQUAL AMD64) OR (CMAKE_SYSTEM_PROCESSOR STREQUAL x64))
set(WINDOWS_KITS_TARGET_ARCHITECTURE x64)
elseif((CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
OR (CMAKE_SYSTEM_PROCESSOR STREQUAL arm64)
OR (CMAKE_SYSTEM_PROCESSOR STREQUAL x86))
set(WINDOWS_KITS_TARGET_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR})
else()
message(FATAL_ERROR "Unable identify Windows Kits architecture for CMAKE_SYSTEM_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR}")
endif()

foreach(LANG C CXX RC)
list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/ucrt")
list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/shared")
list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/um")
list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/winrt")
list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/cppwinrt")
endforeach()

link_directories("${WINDOWS_KITS_LIB_PATH}/ucrt/${WINDOWS_KITS_TARGET_ARCHITECTURE}")
link_directories("${WINDOWS_KITS_LIB_PATH}/um/${WINDOWS_KITS_TARGET_ARCHITECTURE}")
link_directories("${WINDOWS_KITS_REFERENCES_PATH}/${WINDOWS_KITS_TARGET_ARCHITECTURE}")
Loading

0 comments on commit c04768c

Please sign in to comment.