Godot Engine – Multi-platform 2D and 3D game engine
[macOS] Support exporting with debug symbols.
Pāvels Nadtočajevs5c29df869569
diff --git a/platform/macos/crash_handler_macos.mm b/platform/macos/crash_handler_macos.mmindex ee303bc8e92..ac1093c805a 100644--- a/platform/macos/crash_handler_macos.mm+++ b/platform/macos/crash_handler_macos.mm@@ -30,10 +30,12 @@#import "crash_handler_macos.h"+#include "os_macos.h"+#include "core/config/project_settings.h"+#include "core/io/dir_access.h"#include "core/object/script_language.h"#include "core/os/main_loop.h"-#include "core/os/os.h"#include "core/string/print_string.h"#include "core/version.h"#include "main/main.h"@@ -52,6 +54,20 @@#include <cxxabi.h>#include <execinfo.h>+static String find_debugsymbols(const String &p_exec_path) {+ const String base_dir = p_exec_path.get_base_dir() + "/";+ const String exec_bundle = p_exec_path.get_file() + ".dSYM";++ // First try the current executable name plus `.dSYM`, possibly in a `.debug` folder.+ if (DirAccess::exists(p_exec_path + ".dSYM")) {+ return p_exec_path + ".dSYM";+ } else if (DirAccess::exists(base_dir + ".debug/" + exec_bundle)) {+ return base_dir + ".debug/" + exec_bundle;+ }++ return p_exec_path;+}+static void handle_crash(int sig) {signal(SIGSEGV, SIG_DFL);signal(SIGFPE, SIG_DFL);@@ -68,7 +84,7 @@ static void handle_crash(int sig) {void *bt_buffer[256];size_t size = backtrace(bt_buffer, 256);- String exec_path = OS::get_singleton()->get_executable_path();+ String exec_path = find_debugsymbols(static_cast<OS_MacOS *>(OS::get_singleton())->get_bundle_or_executable_path());String msg;if (ProjectSettings::get_singleton()) {diff --git a/platform/macos/doc_classes/EditorExportPlatformMacOS.xml b/platform/macos/doc_classes/EditorExportPlatformMacOS.xmlindex 9a0e14385c9..076f7070886 100644--- a/platform/macos/doc_classes/EditorExportPlatformMacOS.xml+++ b/platform/macos/doc_classes/EditorExportPlatformMacOS.xml@@ -181,6 +181,11 @@<member name="debug/export_console_wrapper" type="int" setter="" getter="">If enabled, a wrapper that can be used to run the application with console output is created alongside the exported application.</member>+ <member name="debug/export_debug_symbols" type="int" setter="" getter="">+ If [code]true[/code], a debug symbol bundle ([code].dSYM[/code]) is exported alongside the main executable.+ [b]Note:[/b] This requires a separate debug symbols bundle. This bundle must have the same name as the export template archive and an additional [code].dSYM[/code] extension, and must be present in the same directory as the export template binary, or in a ZIP archive with a [code].dSYM.zip[/code] extension.+ [b]Note:[/b] If debug symbols are embedded in the export template binary, they will remain embedded in the exported project binary regardless of this option, so it should be disabled in that case.+ </member><member name="display/high_res" type="bool" setter="" getter="">If [code]true[/code], the application is rendered at native display resolution, otherwise it is always rendered at loDPI resolution and upscaled by OS when required.</member>diff --git a/platform/macos/export/export_plugin.cpp b/platform/macos/export/export_plugin.cppindex 0f9e34aff88..a86d76d628c 100644--- a/platform/macos/export/export_plugin.cpp+++ b/platform/macos/export/export_plugin.cpp@@ -480,6 +480,7 @@ void EditorExportPlatformMacOS::get_export_options(List<ExportOption> *r_optionsr_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "debug/export_console_wrapper", PROPERTY_HINT_ENUM, "No,Debug Only,Debug and Release"), 1));+ r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "debug/export_debug_symbols", PROPERTY_HINT_ENUM, "No,Debug Only,Debug and Release"), 1));r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/liquid_glass_icon", PROPERTY_HINT_FILE, "*.icon"), ""));r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.icns,*.png,*.webp,*.svg"), ""));r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/icon_interpolation", PROPERTY_HINT_ENUM, "Nearest neighbor,Bilinear,Cubic,Trilinear,Lanczos"), 4));@@ -1802,6 +1803,143 @@ Error EditorExportPlatformMacOS::_export_debug_script(const Ref<EditorExportPresreturn OK;}+Error EditorExportPlatformMacOS::_unzip_debugsymbols(Ref<DirAccess> &p_da, const String &p_path, const String &p_zip_path, const String &p_app_name, bool p_debug) {+ Ref<FileAccess> io_fa;+ zlib_filefunc_def io = zipio_create_io(&io_fa);++ unzFile src_pkg_zip = unzOpen2(p_zip_path.utf8().get_data(), &io);+ if (!src_pkg_zip) {+ add_message(EXPORT_MESSAGE_WARNING, TTR("Prepare Templates"), vformat(TTR("Could not find debug symbols to export: \"%s\"."), p_zip_path));+ return ERR_FILE_NOT_FOUND;+ }++ String template_name = p_debug ? "template_debug" : "template_release";+ int ret = unzGoToFirstFile(src_pkg_zip);+ while (ret == UNZ_OK) {+ // Get filename.+ unz_file_info info;+ char fname[16384];+ ret = unzGetCurrentFileInfo(src_pkg_zip, &info, fname, 16384, nullptr, 0, nullptr, 0);+ if (ret != UNZ_OK) {+ break;+ }+ String file = String::utf8(fname);+ file = file.trim_prefix("out/macos/templates/");+ if (file.begins_with(vformat("godot.macos.%s.universal.dSYM/", template_name))) {+ file = file.trim_prefix(vformat("godot.macos.%s.universal.dSYM/", template_name));+ file = file.replace(vformat("godot.macos.%s.universal", template_name), p_app_name);++ Vector<uint8_t> data;+ data.resize(info.uncompressed_size);++ // Read.+ unzOpenCurrentFile(src_pkg_zip);+ unzReadCurrentFile(src_pkg_zip, data.ptrw(), data.size());+ unzCloseCurrentFile(src_pkg_zip);++ if (data.size() > 0) {+ print_verbose("ADDING: " + file + " size: " + itos(data.size()));++ file = p_path.path_join(file);+ Error err = p_da->make_dir_recursive(file.get_base_dir());+ if (err != OK) {+ add_message(EXPORT_MESSAGE_WARNING, TTR("Export"), vformat(TTR("Could not create directory \"%s\"."), file.get_base_dir()));+ }+ if (err == OK) {+ Ref<FileAccess> f = FileAccess::open(file, FileAccess::WRITE);+ if (f.is_valid()) {+ f->store_buffer(data.ptr(), data.size());+ f.unref();+ } else {+ add_message(EXPORT_MESSAGE_WARNING, TTR("Export"), vformat(TTR("Could not open \"%s\"."), file));+ err = ERR_CANT_CREATE;+ }+ }+ }+ }++ ret = unzGoToNextFile(src_pkg_zip);+ }++ // We're done with our source zip.+ unzClose(src_pkg_zip);++ return OK;+}++Error EditorExportPlatformMacOS::_copy_debugsymbols_recursively(Ref<DirAccess> &p_da, const String &p_path, const String &p_src_path, const String &p_app_name, bool p_debug) {+ List<String> dirs;++ Ref<DirAccess> src_da = DirAccess::create_for_path(p_src_path);+ if (!p_da->dir_exists(p_path)) {+ Error err = p_da->make_dir(p_path);+ if (err != OK) {+ add_message(EXPORT_MESSAGE_WARNING, TTR("Export"), vformat(TTR("Cannot create directory \"%s\"."), p_path));+ return err;+ }+ }++ Error err = src_da->change_dir(p_src_path);+ if (err != OK) {+ add_message(EXPORT_MESSAGE_WARNING, TTR("Export"), vformat(TTR("Cannot change current directory to \"%s\"."), p_src_path));+ return err;+ }++ String template_name = p_debug ? "template_debug" : "template_release";+ src_da->list_dir_begin();+ String n = src_da->get_next();+ while (!n.is_empty()) {+ if (n != "." && n != "..") {+ if (src_da->current_is_dir()) {+ dirs.push_back(n);+ } else {+ const String &rel_path = n.replace(vformat("godot.macos.%s.universal", template_name), p_app_name);+ if (!n.is_relative_path()) {+ src_da->list_dir_end();+ if (err != OK) {+ add_message(EXPORT_MESSAGE_WARNING, TTR("Export"), vformat(TTR("\"%s\" is not a relative path."), n));+ return err;+ }+ }+ print_verbose("ADDING: " + rel_path);++ err = p_da->copy(src_da->get_current_dir().path_join(n), p_path.path_join(rel_path));+ if (err) {+ src_da->list_dir_end();+ if (err != OK) {+ add_message(EXPORT_MESSAGE_WARNING, TTR("Export"), vformat(TTR("Failed to copy file \"%s\"."), n));+ return err;+ }+ }+ }+ }++ n = src_da->get_next();+ }++ src_da->list_dir_end();++ for (const String &rel_path : dirs) {+ err = _copy_debugsymbols_recursively(p_da, p_path.path_join(rel_path), p_src_path.path_join(rel_path), p_app_name, p_debug);+ if (err != OK) {+ return err;+ }+ }++ return OK;+}++bool EditorExportPlatformMacOS::_copy_debugsymbols(Ref<DirAccess> &p_da, const String &p_path, const String &p_symbols_path, const String &p_app_name, bool p_debug, Error &r_err) {+ if (DirAccess::exists(p_symbols_path.get_basename() + ".dSYM")) {+ r_err = _copy_debugsymbols_recursively(p_da, p_path.simplify_path(), p_symbols_path.simplify_path().get_basename() + ".dSYM", p_app_name, p_debug);+ } else if (FileAccess::exists(p_symbols_path.get_basename() + ".dSYM.zip")) {+ r_err = _unzip_debugsymbols(p_da, p_path.simplify_path(), p_symbols_path.simplify_path().get_basename() + ".dSYM.zip", p_app_name, p_debug);+ } else {+ return false;+ }+ return r_err == OK;+}+Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags, bool p_notify) {ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags, p_notify);@@ -1878,14 +2016,17 @@ Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &pString tmp_base_path_name;String tmp_app_path_name;String scr_path;+ String dbg_name;if (export_format == "app") {tmp_base_path_name = p_path.get_base_dir();tmp_app_path_name = p_path;scr_path = p_path.get_basename() + ".command";+ dbg_name = p_path.get_file().get_basename();} else {tmp_base_path_name = EditorPaths::get_singleton()->get_temp_dir().path_join(pkg_name);tmp_app_path_name = tmp_base_path_name.path_join(tmp_app_dir_name);scr_path = tmp_base_path_name.path_join(pkg_name + ".command");+ dbg_name = pkg_name;}print_verbose("Exporting to " + tmp_app_path_name);@@ -1909,6 +2050,14 @@ Error EditorExportPlatformMacOS::export_project(const Ref<EditorExportPreset> &p}}+ int debug_symbols_mode = p_preset->get("debug/export_debug_symbols");+ bool copy_debug_symbols = (debug_symbols_mode == 1 && p_debug) || (debug_symbols_mode == 2);++ if (copy_debug_symbols) {+ print_verbose("Exporting debug symbols to " + tmp_app_path_name + ".dSYM");+ _copy_debugsymbols(tmp_app_dir, tmp_app_path_name + ".dSYM", src_pkg_name, dbg_name, p_debug, err);+ }+Array helpers = p_preset->get("codesign/entitlements/app_sandbox/helper_executables");// Create our folder structure.diff --git a/platform/macos/export/export_plugin.h b/platform/macos/export/export_plugin.hindex 4a076203d67..9ccc2bada51 100644--- a/platform/macos/export/export_plugin.h+++ b/platform/macos/export/export_plugin.h@@ -84,6 +84,10 @@ class EditorExportPlatformMacOS : public EditorExportPlatform {void _fix_plist(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &plist, const String &p_binary, bool p_lg_icon_exported, const String &p_lg_icon);void _make_icon(const Ref<EditorExportPreset> &p_preset, const Ref<Image> &p_icon, Vector<uint8_t> &p_data);+ Error _unzip_debugsymbols(Ref<DirAccess> &p_da, const String &p_path, const String &p_zip_path, const String &p_app_name, bool p_debug);+ Error _copy_debugsymbols_recursively(Ref<DirAccess> &p_da, const String &p_path, const String &p_src_path, const String &p_app_name, bool p_debug);+ bool _copy_debugsymbols(Ref<DirAccess> &p_da, const String &p_path, const String &p_symbols_path, const String &p_app_name, bool p_debug, Error &r_err);+Error _export_liquid_glass_icon(const Ref<EditorExportPreset> &p_preset, const String &p_app_path, const String &p_icon_path);Error _notarize(const Ref<EditorExportPreset> &p_preset, const String &p_path);Error _copy_dir_and_validate_arch(const Ref<EditorExportPreset> &p_preset, const Ref<DirAccess> &p_dir_access, const String &p_from, String p_to, int p_chmod_flags, bool p_copy_links);diff --git a/platform/macos/os_macos.h b/platform/macos/os_macos.hindex a3b578fb585..978a500be10 100644--- a/platform/macos/os_macos.h+++ b/platform/macos/os_macos.h@@ -123,6 +123,7 @@ public:virtual String get_bundle_icon_path() const override;virtual String get_bundle_icon_name() const override;virtual String get_godot_dir_name() const override;+ String get_bundle_or_executable_path() const;virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const override;virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) override;diff --git a/platform/macos/os_macos.mm b/platform/macos/os_macos.mmindex 120a14408cf..10e92d0e8b1 100644--- a/platform/macos/os_macos.mm+++ b/platform/macos/os_macos.mm@@ -461,6 +461,20 @@ String OS_MacOS::get_bundle_resource_dir() const {return ret;}+String OS_MacOS::get_bundle_or_executable_path() const {+ String ret;++ NSBundle *main = [NSBundle mainBundle];+ if (main) {+ NSString *resource_path = [main bundlePath];+ ret.append_utf8([resource_path UTF8String]);+ }+ if (ret.is_empty()) {+ ret = get_executable_path();+ }+ return ret;+}+String OS_MacOS::get_bundle_icon_path() const {String ret;