macos: Add support for OpenGOAL on Apple Silicon via Rosetta 2 on Sequoia+

This commit is contained in:
Tyler Wilding
2024-11-09 11:59:24 -05:00
parent 431508aab1
commit baea81efff
7 changed files with 70 additions and 2 deletions
+8 -1
View File
@@ -5,6 +5,9 @@ set(CMAKE_CXX_STANDARD 20)
project(jak)
include(CTest)
# Options
option(BUILD_X86_ON_MACOS "Builds the project for macOS for Rosetta 2" OFF)
# Include third-party modules
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/third-party/cmake/modules/)
@@ -119,7 +122,11 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
# pin to AVX for macOS, hopefully all macOS runners have atleast this architecture
# technically speaking, SSE4 is the cutoff for Apple Silicon so...only a matter of time!
if(NOT CMAKE_CXX_COMPILER_TARGET STREQUAL "arm64-apple-darwin")
if (BUILD_X86_ON_MACOS)
message("Targetting x86 on macOS via Rosetta 2 (requires Sequoia)")
set(CMAKE_OSX_ARCHITECTURES "x86_64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx")
elseif(NOT CMAKE_CXX_COMPILER_TARGET STREQUAL "arm64-apple-darwin")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx")
endif()
+11
View File
@@ -174,6 +174,17 @@
"ZYDIS_BUILD_SHARED_LIB": "OFF"
}
},
{
"name": "Release-macos-rosetta-clang-static",
"displayName": "MacOS Rosetta Static Release (clang)",
"description": "Build with Clang as Release without Debug Symbols but statically linked, requires MacOS Sequoia",
"inherits": ["base-macos-release", "base-clang"],
"cacheVariables": {
"BUILD_X86_ON_MACOS": "ON",
"STATICALLY_LINK": "true",
"ZYDIS_BUILD_SHARED_LIB": "OFF"
}
},
{
"name": "Release-linux-clang-asan",
"displayName": "Linux Release (clang-asan)",
+8
View File
@@ -67,6 +67,14 @@ tasks:
cmds:
- "{{.GK_BIN_RELEASE_DIR}}/gk -v --game {{.GAME}} -- -fakeiso -debug"
# DEVELOPMENT
gen-cmake:
desc: "Generate the CMake"
cmds:
- "cmake -B build --preset={{.CMAKE_PRESET}}"
build:
desc: "Build the project using the generated CMake"
cmds:
- "cmake --build build --parallel {{.CMAKE_NUM_THREADS}}"
repl:
desc: "Start the REPL"
preconditions:
+25
View File
@@ -3,6 +3,12 @@
#include "common/common_types.h"
#include "common/log/log.h"
#ifdef __APPLE__
#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdio.h>
#endif
#ifdef _WIN32
// clang-format off
#define NOMINMAX
@@ -104,3 +110,22 @@ void setup_cpu_info() {
CpuInfo& get_cpu_info() {
return gCpuInfo;
}
std::optional<double> get_macos_version() {
#ifndef __APPLE__
return {};
#endif
char buffer[128];
size_t bufferlen = 128;
auto ok = sysctlbyname("kern.osproductversion",&buffer,&bufferlen,NULL,0);
if (ok != 0) {
lg::warn("Unable to check for `kern.osproductversion` to determine macOS version");
return {};
}
try {
return std::stod(buffer);
} catch (std::exception e) {
lg::error("Error occured when attempting to convert sysctl value {} to number", buffer);
return {};
}
}
+2
View File
@@ -16,3 +16,5 @@ struct CpuInfo {
};
CpuInfo& get_cpu_info();
std::optional<double> get_macos_version();
+4 -1
View File
@@ -1,7 +1,10 @@
# Set a more convenient ARM flag
if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64" AND (NOT BUILD_X86_ON_MACOS))
set(ARM64_ARCH TRUE)
message(STATUS "ARM64 architecture detected")
elseif()
set(ARM64_ARCH FALSE)
message(STATUS "Forcefully targetting x86 via Rosetta 2")
else()
set(ARM64_ARCH FALSE)
message(STATUS "Non-ARM64 architecture detected")
+12
View File
@@ -179,10 +179,22 @@ int main(int argc, char** argv) {
setup_cpu_info();
// If the CPU doesn't have AVX, GOAL code won't work and we exit.
if (!get_cpu_info().has_avx) {
// Check if we are on a modern enough version of macOS so that AVX can be
// emulated via rosetta
#ifdef __APPLE__
auto macos_version = get_macos_version();
if (macos_version < 15.0) {
lg::info("Your CPU does not support AVX. But the newer version of Rosetta supports it, update to atleast Sequoia to run OpenGOAL!");
dialogs::create_error_message_dialog(
"Unmet Requirements", "Your CPU does not support AVX. But the newer version of Rosetta supports it, update to atleast Sequoia to run OpenGOAL!");
return -1;
}
#else
lg::info("Your CPU does not support AVX, which is required for OpenGOAL.");
dialogs::create_error_message_dialog(
"Unmet Requirements", "Your CPU does not support AVX, which is required for OpenGOAL.");
return -1;
#endif
}
// set up file paths for resources. This is the full repository when developing, and the data