浏览代码

Compiles on Windows UWP and made unit tests work (#118)

Jorrit Rouwe 3 年之前
父节点
当前提交
71c7199905
共有 7 个文件被更改,包括 95 次插入31 次删除
  1. 35 27
      Build/CMakeLists.txt
  2. 5 0
      Build/cmake_vs2022_uwp.bat
  3. 4 0
      Jolt/Core/Core.h
  4. 1 1
      Jolt/Core/Profiler.cpp
  5. 16 1
      Jolt/Core/TickCounter.cpp
  6. 10 1
      Jolt/Core/TickCounter.h
  7. 24 1
      UnitTests/UnitTestFramework.cpp

+ 35 - 27
Build/CMakeLists.txt

@@ -25,7 +25,7 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}
 	set(CMAKE_CONFIGURATION_TYPES "Debug;Release;ReleaseASAN;ReleaseUBSAN;ReleaseCoverage;Distribution")
 endif()
 
-if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
+if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
 	# Fill in the path to the asan libraries
 	set(CLANG_LIB_PATH "\"$(VSInstallDir)\\VC\\Tools\\Llvm\\x64\\lib\\clang\\${CMAKE_CXX_COMPILER_VERSION}\\lib\\windows\"")
 	
@@ -33,10 +33,16 @@ if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
 	set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE "x64")
 
 	# Set runtime library
-	set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
+	if ("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
+		# Windows store only supports multithreaded DLL
+		set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
+	else()
+		# Otherwise use static library
+		set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
+	endif()
 
 	# Set general compiler flags
-	set(CMAKE_CXX_FLAGS "/std:c++17 /Zc:__cplusplus /GR- /Gm- /Wall /WX /EHsc /nologo /diagnostics:classic /FC /fp:except- /Zc:inline /Zi /DWIN32 /D_WINDOWS /DUNICODE /D_UNICODE")
+	set(CMAKE_CXX_FLAGS "/std:c++17 /Zc:__cplusplus /GR- /Gm- /Wall /WX /EHsc /MP /nologo /diagnostics:classic /FC /fp:except- /Zc:inline /Zi /DWIN32 /D_WINDOWS /DUNICODE /D_UNICODE")
 	
 	# Set compiler flags for various configurations
 	set(CMAKE_CXX_FLAGS_DEBUG "/GS /Od /Ob0 /RTC1")
@@ -49,7 +55,7 @@ if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
 	# Set linker flags
 	set(CMAKE_EXE_LINKER_FLAGS "/machine:x64 /SUBSYSTEM:WINDOWS /ignore:4221 /DEBUG:FASTLINK")
 	if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
-		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /fp:fast") # Clang doesn't use fast math because it cannot be turned off inside a single compilation unit
+		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:fast") # Clang doesn't use fast math because it cannot be turned off inside a single compilation unit
 		if (USE_AVX2)
 			set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")
 		elseif (USE_AVX)
@@ -181,12 +187,12 @@ if (TARGET_UNIT_TESTS)
         target_link_options(UnitTests PUBLIC "/SUBSYSTEM:CONSOLE")
     endif()
     if (IOS)
-	# Set the bundle information
-        set_property(TARGET UnitTests PROPERTY MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/iOS/UnitTestsInfo.plist")
-        set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.joltphysics.unittests")
+		# Set the bundle information
+		set_property(TARGET UnitTests PROPERTY MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/iOS/UnitTestsInfo.plist")
+		set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.joltphysics.unittests")
 
-	# Ensure that we enable SSE4.2 for the x86_64 build, CMAKE_SYSTEM_PROCESSOR is not set for iOS
-	set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
+		# Ensure that we enable SSE4.2 for the x86_64 build, CMAKE_SYSTEM_PROCESSOR is not set for iOS
+		set_property(TARGET UnitTests PROPERTY XCODE_ATTRIBUTE_OTHER_CPLUSPLUSFLAGS[arch=x86_64] "$(inherited) -msse4.2 -mpopcnt")
     endif()
 
     # Register unit tests as a test so that it can be run with:
@@ -195,27 +201,29 @@ if (TARGET_UNIT_TESTS)
     add_test(UnitTests UnitTests)
 endif()
 
-if (TARGET_HELLO_WORLD)
-	# Example 'Hello World' application
-	include(${PHYSICS_REPO_ROOT}/HelloWorld/HelloWorld.cmake)
-	add_executable(HelloWorld ${HELLO_WORLD_SRC_FILES})
-	target_include_directories(HelloWorld PUBLIC ${HELLO_WORLD_ROOT})
-	target_link_libraries (HelloWorld LINK_PUBLIC Jolt)
-	if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
-		target_link_options(HelloWorld PUBLIC "/SUBSYSTEM:CONSOLE")
+if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
+	if (TARGET_HELLO_WORLD)
+		# Example 'Hello World' application
+		include(${PHYSICS_REPO_ROOT}/HelloWorld/HelloWorld.cmake)
+		add_executable(HelloWorld ${HELLO_WORLD_SRC_FILES})
+		target_include_directories(HelloWorld PUBLIC ${HELLO_WORLD_ROOT})
+		target_link_libraries (HelloWorld LINK_PUBLIC Jolt)
+		if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
+			target_link_options(HelloWorld PUBLIC "/SUBSYSTEM:CONSOLE")
+		endif()
 	endif()
-endif()
 
-if (TARGET_PERFORMANCE_TEST)
-	# Performance Test application
-	include(${PHYSICS_REPO_ROOT}/PerformanceTest/PerformanceTest.cmake)
-	add_executable(PerformanceTest ${PERFORMANCE_TEST_SRC_FILES})
-	target_include_directories(PerformanceTest PUBLIC ${PERFORMANCE_TEST_ROOT})
-	target_link_libraries (PerformanceTest LINK_PUBLIC Jolt)
-	if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
-		target_link_options(PerformanceTest PUBLIC "/SUBSYSTEM:CONSOLE")
+	if (TARGET_PERFORMANCE_TEST)
+		# Performance Test application
+		include(${PHYSICS_REPO_ROOT}/PerformanceTest/PerformanceTest.cmake)
+		add_executable(PerformanceTest ${PERFORMANCE_TEST_SRC_FILES})
+		target_include_directories(PerformanceTest PUBLIC ${PERFORMANCE_TEST_ROOT})
+		target_link_libraries (PerformanceTest LINK_PUBLIC Jolt)
+		if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
+			target_link_options(PerformanceTest PUBLIC "/SUBSYSTEM:CONSOLE")
+		endif()
+		set_property(TARGET PerformanceTest PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${PHYSICS_REPO_ROOT}")
 	endif()
-	set_property(TARGET PerformanceTest PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${PHYSICS_REPO_ROOT}")
 endif()
 
 if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")

+ 5 - 0
Build/cmake_vs2022_uwp.bat

@@ -0,0 +1,5 @@
+@echo off
+cmake -S . -B VS2022_UWP -G "Visual Studio 17 2022" -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION=10.0
+echo If cmake failed then be sure to check the "Universal Windows Platform development" checkbox in the Visual Studio Installer
+echo Open VS2022_UWP\JoltPhysics.sln to build the project. 
+echo Note that none of the sample applications are available for the Universal Windows Platform (use cmake_vs2022_cl.bat instead).

+ 4 - 0
Jolt/Core/Core.h

@@ -7,6 +7,10 @@
 #if defined(JPH_PLATFORM_BLUE)
 	// Correct define already defined, this overrides everything else
 #elif defined(_WIN32) || defined(_WIN64)
+	#include <winapifamily.h>
+	#if WINAPI_FAMILY == WINAPI_FAMILY_APP
+		#define JPH_PLATFORM_WINDOWS_UWP // Building for Universal Windows Platform
+	#endif
 	#define JPH_PLATFORM_WINDOWS
 #elif defined(__ANDROID__) // Android is linux too, so that's why we check it first
 	#define JPH_PLATFORM_ANDROID

+ 1 - 1
Jolt/Core/Profiler.cpp

@@ -110,7 +110,7 @@ void Profiler::sAggregate(int inDepth, uint32 inColor, ProfileSample *&ioSample,
 	aggregator->AccumulateMeasurement(cycles_this_with_children, cycles_in_children);
 
 	// Update ioSample to the last child of ioSample
-	JPH_ASSERT(sample[-1].mStartCycle < ioSample->mEndCycle);
+	JPH_ASSERT(sample[-1].mStartCycle <= ioSample->mEndCycle);
 	JPH_ASSERT(sample >= inEnd || sample->mStartCycle >= ioSample->mEndCycle);
 	ioSample = sample - 1;
 }

+ 16 - 1
Jolt/Core/TickCounter.cpp

@@ -20,8 +20,23 @@
 
 JPH_NAMESPACE_BEGIN
 
+#ifdef JPH_PLATFORM_WINDOWS_UWP
+
+uint64 GetProcessorTickCount()
+{
+	LARGE_INTEGER count;
+	QueryPerformanceCounter(&count);
+	return uint64(count.QuadPart);
+}
+
+#endif // JPH_PLATFORM_WINDOWS_UWP
+
 static const uint64 sProcessorTicksPerSecond = []() {
-#if defined(JPH_PLATFORM_WINDOWS)
+#if defined(JPH_PLATFORM_WINDOWS_UWP)
+	LARGE_INTEGER frequency { };
+	QueryPerformanceFrequency(&frequency);
+	return uint64(frequency.QuadPart);
+#elif defined(JPH_PLATFORM_WINDOWS)
 	// Open the key where the processor speed is stored
 	HKEY hkey;
 	RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, 1, &hkey);

+ 10 - 1
Jolt/Core/TickCounter.h

@@ -12,7 +12,14 @@
 
 JPH_NAMESPACE_BEGIN
 
-/// Functionality to get the processors cycle counter 
+#ifdef JPH_PLATFORM_WINDOWS_UWP
+
+/// Functionality to get the processors cycle counter
+uint64 GetProcessorTickCount(); // Not inline to avoid having to include Windows.h
+
+#else
+
+/// Functionality to get the processors cycle counter
 JPH_INLINE uint64 GetProcessorTickCount()
 {
 #if defined(JPH_PLATFORM_BLUE)
@@ -28,6 +35,8 @@ JPH_INLINE uint64 GetProcessorTickCount()
 #endif
 }
 
+#endif // JPH_PLATFORM_WINDOWS_UWP
+
 /// Get the amount of ticks per second, note that this number will never be fully accurate as the amound of ticks per second may vary with CPU load, so this number is only to be used to give an indication of time for profiling purposes
 uint64 GetProcessorTicksPerSecond();
 

+ 24 - 1
UnitTests/UnitTestFramework.cpp

@@ -56,7 +56,30 @@ static bool AssertFailedImpl(const char *inExpression, const char *inMessage, co
 
 #endif // JPH_ENABLE_ASSERTS
 
-#ifndef JPH_PLATFORM_ANDROID
+#ifdef JPH_PLATFORM_WINDOWS_UWP
+
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
+{
+	// Install callbacks
+	Trace = TraceImpl;
+	JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
+
+#ifdef _DEBUG
+	// Enable leak detection
+	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
+#endif
+
+	// Enable floating point exceptions
+	FPExceptionsEnable enable_exceptions;
+	JPH_UNUSED(enable_exceptions);
+
+	// Register physics types
+	RegisterTypes();
+
+	return Context().run(); 
+}
+
+#elif !defined(JPH_PLATFORM_ANDROID)
 
 // Generic entry point
 int main(int argc, char** argv)