| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/extension/gdextension_library_loader.h" |
| 34 |
#include "editor/export/editor_export.h" |
| 35 |
|
| 36 |
class GDExtensionExportPlugin : public EditorExportPlugin { |
| 37 |
GDSOFTCLASS(GDExtensionExportPlugin, EditorExportPlugin); |
| 38 |
|
| 39 |
protected: |
| 40 |
virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) override; |
| 41 |
virtual String get_name() const override { return "GDExtension"; } |
| 42 |
}; |
| 43 |
|
| 44 |
void GDExtensionExportPlugin::_export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) { |
| 45 |
if (p_type != "GDExtension") { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
Ref<ConfigFile> config; |
| 50 |
config.instantiate(); |
| 51 |
|
| 52 |
Error err = config->load(p_path); |
| 53 |
ERR_FAIL_COND_MSG(err, "Failed to load GDExtension file: " + p_path); |
| 54 |
|
| 55 |
|
| 56 |
bool android_aar_plugin = config->get_value("configuration", "android_aar_plugin", false); |
| 57 |
if (android_aar_plugin && p_features.has("android")) { |
| 58 |
|
| 59 |
|
| 60 |
skip(); |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
ERR_FAIL_COND_MSG(!config->has_section_key("configuration", "entry_symbol"), "Failed to export GDExtension file, missing entry symbol: " + p_path); |
| 65 |
|
| 66 |
String entry_symbol = config->get_value("configuration", "entry_symbol"); |
| 67 |
|
| 68 |
HashSet<String> all_archs; |
| 69 |
all_archs.insert("x86_32"); |
| 70 |
all_archs.insert("x86_64"); |
| 71 |
all_archs.insert("arm32"); |
| 72 |
all_archs.insert("arm64"); |
| 73 |
all_archs.insert("rv64"); |
| 74 |
all_archs.insert("ppc64"); |
| 75 |
all_archs.insert("wasm32"); |
| 76 |
all_archs.insert("loongarch64"); |
| 77 |
all_archs.insert("universal"); |
| 78 |
|
| 79 |
HashSet<String> archs; |
| 80 |
HashSet<String> features_wo_arch; |
| 81 |
Vector<String> features_vector; |
| 82 |
for (const String &tag : p_features) { |
| 83 |
if (all_archs.has(tag)) { |
| 84 |
archs.insert(tag); |
| 85 |
} else { |
| 86 |
features_wo_arch.insert(tag); |
| 87 |
} |
| 88 |
features_vector.append(tag); |
| 89 |
} |
| 90 |
|
| 91 |
if (archs.is_empty()) { |
| 92 |
archs.insert("unknown_arch"); |
| 93 |
} |
| 94 |
|
| 95 |
HashSet<String> libs_added; |
| 96 |
struct FoundLibInfo { |
| 97 |
int count = 0; |
| 98 |
Vector<String> libs; |
| 99 |
}; |
| 100 |
HashMap<String, FoundLibInfo> libs_found; |
| 101 |
for (const String &arch_tag : archs) { |
| 102 |
if (arch_tag != "universal") { |
| 103 |
libs_found[arch_tag] = FoundLibInfo(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
for (const String &arch_tag : archs) { |
| 108 |
PackedStringArray tags; |
| 109 |
String library_path = GDExtensionLibraryLoader::find_extension_library( |
| 110 |
p_path, config, [features_wo_arch, arch_tag](const String &p_feature) { return features_wo_arch.has(p_feature) || (p_feature == arch_tag); }, &tags); |
| 111 |
|
| 112 |
if (libs_added.has(library_path)) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
if (!library_path.is_empty()) { |
| 116 |
libs_added.insert(library_path); |
| 117 |
add_shared_object(library_path, tags); |
| 118 |
|
| 119 |
if (p_features.has("apple_embedded") && (library_path.ends_with(".a") || library_path.ends_with(".xcframework"))) { |
| 120 |
String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n" |
| 121 |
"extern void add_apple_embedded_platform_init_callback(void (*cb)());\n" |
| 122 |
"\n" |
| 123 |
"extern \"C\" void $ENTRY();\n" |
| 124 |
"void $ENTRY_init() {\n" |
| 125 |
" if (&$ENTRY) register_dynamic_symbol((char *)\"$ENTRY\", (void *)$ENTRY);\n" |
| 126 |
"}\n" |
| 127 |
"struct $ENTRY_struct {\n" |
| 128 |
" $ENTRY_struct() {\n" |
| 129 |
" add_apple_embedded_platform_init_callback($ENTRY_init);\n" |
| 130 |
" }\n" |
| 131 |
"};\n" |
| 132 |
"$ENTRY_struct $ENTRY_struct_instance;\n\n"; |
| 133 |
additional_code = additional_code.replace("$ENTRY", entry_symbol); |
| 134 |
add_apple_embedded_platform_cpp_code(additional_code); |
| 135 |
|
| 136 |
String linker_flags = "-Wl,-U,_" + entry_symbol; |
| 137 |
add_apple_embedded_platform_linker_flags(linker_flags); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if (arch_tag == "universal") { |
| 142 |
for (const String &sub_arch_tag : archs) { |
| 143 |
if (sub_arch_tag != "universal") { |
| 144 |
libs_found[sub_arch_tag].count++; |
| 145 |
libs_found[sub_arch_tag].libs.push_back(library_path); |
| 146 |
} |
| 147 |
} |
| 148 |
} else { |
| 149 |
libs_found[arch_tag].count++; |
| 150 |
libs_found[arch_tag].libs.push_back(library_path); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
Vector<SharedObject> dependencies_shared_objects = GDExtensionLibraryLoader::find_extension_dependencies( |
| 155 |
p_path, config, [features_wo_arch, arch_tag](String p_feature) { return features_wo_arch.has(p_feature) || (p_feature == arch_tag); }); |
| 156 |
for (const SharedObject &shared_object : dependencies_shared_objects) { |
| 157 |
_add_shared_object(shared_object); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
for (const KeyValue<String, FoundLibInfo> &E : libs_found) { |
| 162 |
if (E.value.count == 0) { |
| 163 |
if (get_export_platform().is_valid()) { |
| 164 |
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))); |
| 165 |
} |
| 166 |
} else if (E.value.count > 1) { |
| 167 |
if (get_export_platform().is_valid()) { |
| 168 |
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))); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |