Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GDExtension] Improve macOS library loading/export. #98809

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ bool OS::has_feature(const String &p_feature) {
}
#if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
#if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
#if defined(MACOS_ENABLED)
if (p_feature == "universal") {
return true;
}
#endif
if (p_feature == "x86_64") {
return true;
}
Expand All @@ -452,6 +457,11 @@ bool OS::has_feature(const String &p_feature) {
}
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
#if defined(__aarch64__) || defined(_M_ARM64)
#if defined(MACOS_ENABLED)
if (p_feature == "universal") {
return true;
}
#endif
if (p_feature == "arm64") {
return true;
}
Expand Down
45 changes: 37 additions & 8 deletions editor/plugins/gdextension_export_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,37 @@ void GDExtensionExportPlugin::_export_file(const String &p_path, const String &p

HashSet<String> archs;
HashSet<String> features_wo_arch;
Vector<String> features_vector;
for (const String &tag : p_features) {
if (all_archs.has(tag)) {
archs.insert(tag);
} else {
features_wo_arch.insert(tag);
}
features_vector.append(tag);
}

if (archs.is_empty()) {
archs.insert("unknown_arch"); // Not archs specified, still try to match.
}

HashSet<String> libs_added;
struct FoundLibInfo {
int count = 0;
Vector<String> libs;
};
HashMap<String, FoundLibInfo> libs_found;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a comment would be good that libs_found only exists for warning purposes.

for (const String &arch_tag : archs) {
if (arch_tag != "universal") {
libs_found[arch_tag] = FoundLibInfo();
}
}

for (const String &arch_tag : archs) {
PackedStringArray tags;
String library_path = GDExtensionLibraryLoader::find_extension_library(
p_path, config, [features_wo_arch, arch_tag](const String &p_feature) { return features_wo_arch.has(p_feature) || (p_feature == arch_tag); }, &tags);

if (libs_added.has(library_path)) {
continue; // Universal library, already added for another arch, do not duplicate.
}
Expand Down Expand Up @@ -122,22 +135,38 @@ void GDExtensionExportPlugin::_export_file(const String &p_path, const String &p
String linker_flags = "-Wl,-U,_" + entry_symbol;
add_ios_linker_flags(linker_flags);
}
} else {
Vector<String> features_vector;
for (const String &E : p_features) {
features_vector.append(E);
}
if (get_export_platform().is_valid()) {
get_export_platform()->add_message(EditorExportPlatform::EXPORT_MESSAGE_WARNING, TTR("GDExtension"), vformat(TTR("No suitable library found for GDExtension: \"%s\". Possible feature flags for your platform: %s"), p_path, String(", ").join(features_vector)));

// Update found library info.
if (arch_tag == "universal") {
for (const String &sub_arch_tag : archs) {
if (sub_arch_tag != "universal") {
libs_found[sub_arch_tag].count++;
libs_found[sub_arch_tag].libs.push_back(library_path);
}
}
} else {
libs_found[arch_tag].count++;
libs_found[arch_tag].libs.push_back(library_path);
}
return;
}

Vector<SharedObject> dependencies_shared_objects = GDExtensionLibraryLoader::find_extension_dependencies(p_path, config, [p_features](String p_feature) { return p_features.has(p_feature); });
for (const SharedObject &shared_object : dependencies_shared_objects) {
_add_shared_object(shared_object);
}
}

for (const KeyValue<String, FoundLibInfo> &E : libs_found) {
if (E.value.count == 0) {
if (get_export_platform().is_valid()) {
get_export_platform()->add_message(EditorExportPlatform::EXPORT_MESSAGE_WARNING, TTR("GDExtension"), vformat(TTR("No \"%s\" library found for GDExtension: \"%s\". Possible feature flags for your platform: %s"), E.key, p_path, String(", ").join(features_vector)));
}
} else if (E.value.count > 1) {
if (get_export_platform().is_valid()) {
get_export_platform()->add_message(EditorExportPlatform::EXPORT_MESSAGE_WARNING, TTR("GDExtension"), vformat(TTR("Multiple \"%s\" libraries found for GDExtension: \"%s\": \"%s\"."), E.key, p_path, String(", ").join(E.value.libs)));
}
}
}
}

#endif // GDEXTENSION_EXPORT_PLUGIN_H
5 changes: 5 additions & 0 deletions platform/macos/export/export_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ void EditorExportPlatformMacOS::get_preset_features(const Ref<EditorExportPreset
} else {
ERR_PRINT("Invalid architecture");
}

if (architecture == "universal") {
r_features->push_back("x86_64");
r_features->push_back("arm64");
}
}

String EditorExportPlatformMacOS::get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const {
Expand Down
Loading