From c85fac315ad367877423ebf2f92a3bdaa92e83b5 Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Wed, 15 Feb 2023 22:04:23 -0800 Subject: [PATCH] CMake updated to support building for Xbox (#87) --- CMakeLists.txt | 47 +++++++++++++++++++++++++++++++++++++++++++++-- CMakePresets.json | 28 +++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3155a8a..fde2a4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ endif() if(DEFINED XBOX_CONSOLE_TARGET) set(BUILD_DX12 ON) + set(BUILD_TOOLS OFF) endif() include(GNUInstallDirs) @@ -104,7 +105,32 @@ endif() if(directx-headers_FOUND) message(STATUS "Using DirectX-Headers package") target_link_libraries(${PROJECT_NAME} PUBLIC Microsoft::DirectX-Headers) - target_compile_definitions(${PROJECT_NAME} PRIVATE USING_DIRECTX_HEADERS) + target_compile_definitions(${PROJECT_NAME} PUBLIC USING_DIRECTX_HEADERS) +endif() + +include(CheckIncludeFileCXX) + +if(DEFINED XBOX_CONSOLE_TARGET) + message(STATUS "Building for Xbox Console Target: ${XBOX_CONSOLE_TARGET}") + set(CMAKE_REQUIRED_QUIET ON) + CHECK_INCLUDE_FILE_CXX(gxdk.h GXDK_HEADER) + if(NOT GXDK_HEADER) + message(FATAL_ERROR "Microsoft GDK with Xbox Extensions required to build for Xbox. See https://aka.ms/gdkx") + endif() + target_compile_definitions(${PROJECT_NAME} PUBLIC WINAPI_FAMILY=WINAPI_FAMILY_GAMES) + if(XBOX_CONSOLE_TARGET STREQUAL "scarlett") + CHECK_INCLUDE_FILE_CXX(d3d12_xs.h D3D12XS_HEADER) + if(NOT D3D12XS_HEADER) + message(FATAL_ERROR "Microsoft GDK with Xbox Extensions environment needs to be set for Xbox Series X|S.") + endif() + target_compile_definitions(${PROJECT_NAME} PUBLIC _GAMING_XBOX _GAMING_XBOX_SCARLETT) + elseif(XBOX_CONSOLE_TARGET STREQUAL "xboxone") + CHECK_INCLUDE_FILE_CXX(d3d12_x.h D3D12X_HEADER) + if(NOT D3D12X_HEADER) + message(FATAL_ERROR "Microsoft GDK with Xbox Extensions environment needs to be set for Xbox One.") + endif() + target_compile_definitions(${PROJECT_NAME} PUBLIC _GAMING_XBOX _GAMING_XBOX_XBOXONE) + endif() endif() #--- Utilities @@ -225,7 +251,11 @@ else() endforeach() endif() -if(NOT ${DIRECTX_ARCH} MATCHES "^arm") +if(XBOX_CONSOLE_TARGET STREQUAL "scarlett") + target_compile_options(${PROJECT_NAME} PRIVATE $,/favor:AMD64 /arch:AVX2,-march=znver2>) +elseif(XBOX_CONSOLE_TARGET STREQUAL "xboxone") + target_compile_options(${PROJECT_NAME} PRIVATE $,/favor:AMD64 /arch:AVX,-march=btver2) +elseif(NOT ${DIRECTX_ARCH} MATCHES "^arm") if(${CMAKE_SIZEOF_VOID_P} EQUAL "4") set(ARCH_SSE2 $<$:/arch:SSE2> $<$>:-msse2>) else() @@ -261,6 +291,13 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") endforeach() endif() + if(CMAKE_INTERPROCEDURAL_OPTIMIZATION) + message(STATUS "Building using Whole Program Optimization") + foreach(t IN LISTS TOOL_EXES ITEMS ${PROJECT_NAME}) + target_compile_options(${t} PRIVATE /Gy /Gw) + endforeach() + endif() + if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.24) foreach(t IN LISTS TOOL_EXES ITEMS ${PROJECT_NAME}) target_compile_options(${t} PRIVATE /ZH:SHA_256) @@ -285,6 +322,12 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") endforeach() endif() + if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.35) + foreach(t IN LISTS TOOL_EXES ITEMS ${PROJECT_NAME}) + target_compile_options(${t} PRIVATE /Zc:templateScope /Zc:checkGwOdr) + endforeach() + endif() + set(WarningsEXE "/wd4061" "/wd4365" "/wd4514" "/wd4625" "/wd4626" "/wd4627" "/wd4668" "/wd4710" "/wd4751" "/wd4820" "/wd5026" "/wd5027" "/wd5039" "/wd5045" "/wd5219") if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.34) list(APPEND WarningsEXE "/wd5262" "/wd5264") diff --git a/CMakePresets.json b/CMakePresets.json index 4ab8b69..66b0f6a 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -55,7 +55,11 @@ }, { "name": "Release", - "cacheVariables": { "CMAKE_BUILD_TYPE": "RelWithDebInfo" }, + "cacheVariables": + { + "CMAKE_BUILD_TYPE": "RelWithDebInfo", + "CMAKE_INTERPROCEDURAL_OPTIMIZATION": true + }, "hidden": true }, @@ -109,6 +113,22 @@ }, "hidden": true }, + { + "name": "Scarlett", + "cacheVariables": { + "XBOX_CONSOLE_TARGET": "scarlett", + "BUILD_TESTING": false + }, + "hidden": true + }, + { + "name": "XboxOne", + "cacheVariables": { + "XBOX_CONSOLE_TARGET": "xboxone", + "BUILD_TESTING": false + }, + "hidden": true + }, { "name": "VCPKG", "cacheVariables": { @@ -163,6 +183,12 @@ { "name": "x86-Debug-Win7" , "description": "MSVC for x86 (Debug) for Windows 7", "inherits": [ "base", "x86", "Debug", "MSVC", "Win7" ] }, { "name": "x86-Release-Win7" , "description": "MSVC for x86 (Release) for Windows 7", "inherits": [ "base", "x86", "Release", "MSVC", "Win7" ] }, + { "name": "x64-Debug-Scarlett" , "description": "MSVC for x64 (Debug) for Xbox Series X|S", "inherits": [ "base", "x64", "Debug", "MSVC", "Scarlett" ] }, + { "name": "x64-Release-Scarlett" , "description": "MSVC for x64 (Release) for Xbox Series X|S", "inherits": [ "base", "x64", "Release", "MSVC", "Scarlett" ] }, + + { "name": "x64-Debug-XboxOne" , "description": "MSVC for x64 (Debug) for Xbox One", "inherits": [ "base", "x64", "Debug", "MSVC", "XboxOne" ] }, + { "name": "x64-Release-XboxOne" , "description": "MSVC for x64 (Release) for Xbox One", "inherits": [ "base", "x64", "Release", "MSVC", "XboxOne" ] }, + { "name": "x64-Debug-VCPKG" , "description": "MSVC for x64 (Debug) using VCPKG", "inherits": [ "base", "x64", "Debug", "MSVC", "VCPKG" ] }, { "name": "x64-Release-VCPKG" , "description": "MSVC for x64 (Release) using VCPKG", "inherits": [ "base", "x64", "Release", "MSVC", "VCPKG" ] }, { "name": "x86-Debug-VCPKG" , "description": "MSVC for x86 (Debug) using VCPKG", "inherits": [ "base", "x86", "Debug", "MSVC", "VCPKG" ] },