Raise an informative error when running on macOS under Rosetta 2.

Current users only get a cryptic "Illegal instruction" crash.

Context: https://github.com/deepmind/mujoco/issues/611
PiperOrigin-RevId: 491705647
Change-Id: I1285897a36e858300ee3d125957e90754be9a7f7
This commit is contained in:
Saran Tunyasuvunakool
2022-11-29 12:17:58 -08:00
committed by Copybara-Service
parent 5f7d9f4f02
commit df25d7d602
9 changed files with 93 additions and 9 deletions
+1 -1
View File
@@ -122,7 +122,7 @@ target_include_directories(libsimulate PUBLIC $<TARGET_PROPERTY:glfw,INTERFACE_I
target_link_options(libsimulate PRIVATE ${MUJOCO_SIMULATE_LINK_OPTIONS})
if(APPLE)
target_sources(libsimulate PRIVATE macos_save.mm)
target_sources(libsimulate PRIVATE macos_gui.mm)
target_link_libraries(libsimulate PUBLIC "-framework Cocoa")
endif()
+2 -2
View File
@@ -11,8 +11,8 @@ CXXFLAGS=$(CFLAGS) -std=c++17 -stdlib=libc++
ALLFLAGS=$(CXXFLAGS) -L$(GLFWROOT)/lib -Wl,-rpath,$(MUJOCOPATH)
all:
clang++ $(CXXFLAGS) -c macos_save.mm
clang++ $(CXXFLAGS) -c macos_gui.mm
clang++ $(CXXFLAGS) -c simulate.cc
clang $(CFLAGS) -std=c11 -c uitools.c
clang++ $(CXXFLAGS) main.cc macos_save.o simulate.o uitools.o -framework mujoco -framework Cocoa -lglfw -o simulate
clang++ $(CXXFLAGS) main.cc macos_gui.o simulate.o uitools.o -framework mujoco -framework Cocoa -lglfw -o simulate
rm *.o
@@ -17,7 +17,7 @@
#include <Cocoa/Cocoa.h>
std::string getSavePath(const char* filename) {
std::string GetSavePath(const char* filename) {
NSSavePanel* panel = [NSSavePanel savePanel];
NSURL* userDocumentsDir = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask].firstObject;
@@ -31,3 +31,14 @@ std::string getSavePath(const char* filename) {
return "";
}
}
#ifdef __AVX__
void DisplayErrorDialogBox(const char* title, const char* msg) {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:[NSString stringWithUTF8String:title]];
[alert setInformativeText:[NSString stringWithUTF8String:msg]];
[alert setAlertStyle:NSAlertStyleCritical];
[alert addButtonWithTitle:@"Exit"];
[alert runModal];
}
#endif
+17
View File
@@ -437,8 +437,25 @@ void PhysicsThread(mj::Simulate* sim, const char* filename) {
//------------------------------------------ main --------------------------------------------------
// machinery for replacing command line error by a macOS dialog box when running under Rosetta
#if defined(__APPLE__) && defined(__AVX__)
extern void DisplayErrorDialogBox(const char* title, const char* msg);
static const char* rosetta_error_msg = nullptr;
__attribute__((used, visibility("default"))) extern "C" void _mj_rosettaError(const char* msg) {
rosetta_error_msg = msg;
}
#endif
// run event loop
int main(int argc, const char** argv) {
// display an error if running on macOS under Rosetta 2
#if defined(__APPLE__) && defined(__AVX__)
if (rosetta_error_msg) {
DisplayErrorDialogBox("Rosetta 2 is not supported", rosetta_error_msg);
std::exit(1);
}
#endif
// print version, check compatibility
std::printf("MuJoCo version %s\n", mj_versionString());
if (mjVERSION_HEADER!=mj_version()) {
+5 -5
View File
@@ -40,9 +40,9 @@
// Since the dialog box logic needs to be written in Objective-C, we separate it into a different
// source file.
#ifdef __APPLE__
std::string getSavePath(const char* filename);
std::string GetSavePath(const char* filename);
#else
static std::string getSavePath(const char* filename) {
static std::string GetSavePath(const char* filename) {
return filename;
}
#endif
@@ -1077,7 +1077,7 @@ void uiEvent(mjuiState* state) {
switch (it->itemid) {
case 0: // Save xml
{
const std::string path = getSavePath("mjmodel.xml");
const std::string path = GetSavePath("mjmodel.xml");
if (!path.empty() && !mj_saveLastXML(path.c_str(), m, err, 200)) {
std::printf("Save XML error: %s", err);
}
@@ -1086,7 +1086,7 @@ void uiEvent(mjuiState* state) {
case 1: // Save mjb
{
const std::string path = getSavePath("mjmodel.mjb");
const std::string path = GetSavePath("mjmodel.mjb");
if (!path.empty()) {
mj_saveModel(m, path.c_str(), nullptr, 0);
}
@@ -1850,7 +1850,7 @@ void Simulate::render() {
// Unfortunately, if we just yank ".xml"/".mjb" from the filename and append .PNG, the macOS
// file dialog does not automatically open that location. Thus, we defer to a default
// "screenshot.png" for now.
const std::string path = getSavePath("screenshot.png");
const std::string path = GetSavePath("screenshot.png");
if (!path.empty()) {
if (lodepng::encode(path, rgb.get(), w, h, LCT_RGB)) {
mju_error("could not save screenshot");