2
0

gdextension_export_plugin.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**************************************************************************/
  2. /* gdextension_export_plugin.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef GDEXTENSION_EXPORT_PLUGIN_H
  31. #define GDEXTENSION_EXPORT_PLUGIN_H
  32. #include "core/extension/gdextension_library_loader.h"
  33. #include "editor/export/editor_export.h"
  34. class GDExtensionExportPlugin : public EditorExportPlugin {
  35. protected:
  36. virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features);
  37. virtual String get_name() const { return "GDExtension"; }
  38. };
  39. void GDExtensionExportPlugin::_export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) {
  40. if (p_type != "GDExtension") {
  41. return;
  42. }
  43. Ref<ConfigFile> config;
  44. config.instantiate();
  45. Error err = config->load(p_path);
  46. ERR_FAIL_COND_MSG(err, "Failed to load GDExtension file: " + p_path);
  47. // Check whether this GDExtension should be exported.
  48. bool android_aar_plugin = config->get_value("configuration", "android_aar_plugin", false);
  49. if (android_aar_plugin && p_features.has("android")) {
  50. // The gdextension configuration and Android .so files will be provided by the Android aar
  51. // plugin it's part of, so we abort here.
  52. skip();
  53. return;
  54. }
  55. ERR_FAIL_COND_MSG(!config->has_section_key("configuration", "entry_symbol"), "Failed to export GDExtension file, missing entry symbol: " + p_path);
  56. String entry_symbol = config->get_value("configuration", "entry_symbol");
  57. HashSet<String> all_archs;
  58. all_archs.insert("x86_32");
  59. all_archs.insert("x86_64");
  60. all_archs.insert("arm32");
  61. all_archs.insert("arm64");
  62. all_archs.insert("rv64");
  63. all_archs.insert("ppc32");
  64. all_archs.insert("ppc64");
  65. all_archs.insert("wasm32");
  66. all_archs.insert("loongarch64");
  67. all_archs.insert("universal");
  68. HashSet<String> archs;
  69. HashSet<String> features_wo_arch;
  70. Vector<String> features_vector;
  71. for (const String &tag : p_features) {
  72. if (all_archs.has(tag)) {
  73. archs.insert(tag);
  74. } else {
  75. features_wo_arch.insert(tag);
  76. }
  77. features_vector.append(tag);
  78. }
  79. if (archs.is_empty()) {
  80. archs.insert("unknown_arch"); // Not archs specified, still try to match.
  81. }
  82. HashSet<String> libs_added;
  83. struct FoundLibInfo {
  84. int count = 0;
  85. Vector<String> libs;
  86. };
  87. HashMap<String, FoundLibInfo> libs_found;
  88. for (const String &arch_tag : archs) {
  89. if (arch_tag != "universal") {
  90. libs_found[arch_tag] = FoundLibInfo();
  91. }
  92. }
  93. for (const String &arch_tag : archs) {
  94. PackedStringArray tags;
  95. String library_path = GDExtensionLibraryLoader::find_extension_library(
  96. p_path, config, [features_wo_arch, arch_tag](const String &p_feature) { return features_wo_arch.has(p_feature) || (p_feature == arch_tag); }, &tags);
  97. if (libs_added.has(library_path)) {
  98. continue; // Universal library, already added for another arch, do not duplicate.
  99. }
  100. if (!library_path.is_empty()) {
  101. libs_added.insert(library_path);
  102. add_shared_object(library_path, tags);
  103. if (p_features.has("ios") && (library_path.ends_with(".a") || library_path.ends_with(".xcframework"))) {
  104. String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"
  105. "extern void add_ios_init_callback(void (*cb)());\n"
  106. "\n"
  107. "extern \"C\" void $ENTRY();\n"
  108. "void $ENTRY_init() {\n"
  109. " if (&$ENTRY) register_dynamic_symbol((char *)\"$ENTRY\", (void *)$ENTRY);\n"
  110. "}\n"
  111. "struct $ENTRY_struct {\n"
  112. " $ENTRY_struct() {\n"
  113. " add_ios_init_callback($ENTRY_init);\n"
  114. " }\n"
  115. "};\n"
  116. "$ENTRY_struct $ENTRY_struct_instance;\n\n";
  117. additional_code = additional_code.replace("$ENTRY", entry_symbol);
  118. add_ios_cpp_code(additional_code);
  119. String linker_flags = "-Wl,-U,_" + entry_symbol;
  120. add_ios_linker_flags(linker_flags);
  121. }
  122. // Update found library info.
  123. if (arch_tag == "universal") {
  124. for (const String &sub_arch_tag : archs) {
  125. if (sub_arch_tag != "universal") {
  126. libs_found[sub_arch_tag].count++;
  127. libs_found[sub_arch_tag].libs.push_back(library_path);
  128. }
  129. }
  130. } else {
  131. libs_found[arch_tag].count++;
  132. libs_found[arch_tag].libs.push_back(library_path);
  133. }
  134. }
  135. Vector<SharedObject> dependencies_shared_objects = GDExtensionLibraryLoader::find_extension_dependencies(p_path, config, [p_features](String p_feature) { return p_features.has(p_feature); });
  136. for (const SharedObject &shared_object : dependencies_shared_objects) {
  137. _add_shared_object(shared_object);
  138. }
  139. }
  140. for (const KeyValue<String, FoundLibInfo> &E : libs_found) {
  141. if (E.value.count == 0) {
  142. if (get_export_platform().is_valid()) {
  143. 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)));
  144. }
  145. } else if (E.value.count > 1) {
  146. if (get_export_platform().is_valid()) {
  147. 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)));
  148. }
  149. }
  150. }
  151. }
  152. #endif // GDEXTENSION_EXPORT_PLUGIN_H