gdextension_export_plugin.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #pragma once
  31. #include "core/extension/gdextension_library_loader.h"
  32. #include "editor/export/editor_export.h"
  33. class GDExtensionExportPlugin : public EditorExportPlugin {
  34. protected:
  35. virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features);
  36. virtual String get_name() const { return "GDExtension"; }
  37. };
  38. void GDExtensionExportPlugin::_export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) {
  39. if (p_type != "GDExtension") {
  40. return;
  41. }
  42. Ref<ConfigFile> config;
  43. config.instantiate();
  44. Error err = config->load(p_path);
  45. ERR_FAIL_COND_MSG(err, "Failed to load GDExtension file: " + p_path);
  46. // Check whether this GDExtension should be exported.
  47. bool android_aar_plugin = config->get_value("configuration", "android_aar_plugin", false);
  48. if (android_aar_plugin && p_features.has("android")) {
  49. // The gdextension configuration and Android .so files will be provided by the Android aar
  50. // plugin it's part of, so we abort here.
  51. skip();
  52. return;
  53. }
  54. ERR_FAIL_COND_MSG(!config->has_section_key("configuration", "entry_symbol"), "Failed to export GDExtension file, missing entry symbol: " + p_path);
  55. String entry_symbol = config->get_value("configuration", "entry_symbol");
  56. HashSet<String> all_archs;
  57. all_archs.insert("x86_32");
  58. all_archs.insert("x86_64");
  59. all_archs.insert("arm32");
  60. all_archs.insert("arm64");
  61. all_archs.insert("rv64");
  62. all_archs.insert("ppc64");
  63. all_archs.insert("wasm32");
  64. all_archs.insert("loongarch64");
  65. all_archs.insert("universal");
  66. HashSet<String> archs;
  67. HashSet<String> features_wo_arch;
  68. Vector<String> features_vector;
  69. for (const String &tag : p_features) {
  70. if (all_archs.has(tag)) {
  71. archs.insert(tag);
  72. } else {
  73. features_wo_arch.insert(tag);
  74. }
  75. features_vector.append(tag);
  76. }
  77. if (archs.is_empty()) {
  78. archs.insert("unknown_arch"); // Not archs specified, still try to match.
  79. }
  80. HashSet<String> libs_added;
  81. struct FoundLibInfo {
  82. int count = 0;
  83. Vector<String> libs;
  84. };
  85. HashMap<String, FoundLibInfo> libs_found;
  86. for (const String &arch_tag : archs) {
  87. if (arch_tag != "universal") {
  88. libs_found[arch_tag] = FoundLibInfo();
  89. }
  90. }
  91. for (const String &arch_tag : archs) {
  92. PackedStringArray tags;
  93. String library_path = GDExtensionLibraryLoader::find_extension_library(
  94. p_path, config, [features_wo_arch, arch_tag](const String &p_feature) { return features_wo_arch.has(p_feature) || (p_feature == arch_tag); }, &tags);
  95. if (libs_added.has(library_path)) {
  96. continue; // Universal library, already added for another arch, do not duplicate.
  97. }
  98. if (!library_path.is_empty()) {
  99. libs_added.insert(library_path);
  100. add_shared_object(library_path, tags);
  101. if (p_features.has("apple_embedded") && (library_path.ends_with(".a") || library_path.ends_with(".xcframework"))) {
  102. String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"
  103. "extern void add_apple_embedded_platform_init_callback(void (*cb)());\n"
  104. "\n"
  105. "extern \"C\" void $ENTRY();\n"
  106. "void $ENTRY_init() {\n"
  107. " if (&$ENTRY) register_dynamic_symbol((char *)\"$ENTRY\", (void *)$ENTRY);\n"
  108. "}\n"
  109. "struct $ENTRY_struct {\n"
  110. " $ENTRY_struct() {\n"
  111. " add_apple_embedded_platform_init_callback($ENTRY_init);\n"
  112. " }\n"
  113. "};\n"
  114. "$ENTRY_struct $ENTRY_struct_instance;\n\n";
  115. additional_code = additional_code.replace("$ENTRY", entry_symbol);
  116. add_apple_embedded_platform_cpp_code(additional_code);
  117. String linker_flags = "-Wl,-U,_" + entry_symbol;
  118. add_apple_embedded_platform_linker_flags(linker_flags);
  119. }
  120. // Update found library info.
  121. if (arch_tag == "universal") {
  122. for (const String &sub_arch_tag : archs) {
  123. if (sub_arch_tag != "universal") {
  124. libs_found[sub_arch_tag].count++;
  125. libs_found[sub_arch_tag].libs.push_back(library_path);
  126. }
  127. }
  128. } else {
  129. libs_found[arch_tag].count++;
  130. libs_found[arch_tag].libs.push_back(library_path);
  131. }
  132. }
  133. Vector<SharedObject> dependencies_shared_objects = GDExtensionLibraryLoader::find_extension_dependencies(p_path, config, [p_features](String p_feature) { return p_features.has(p_feature); });
  134. for (const SharedObject &shared_object : dependencies_shared_objects) {
  135. _add_shared_object(shared_object);
  136. }
  137. }
  138. for (const KeyValue<String, FoundLibInfo> &E : libs_found) {
  139. if (E.value.count == 0) {
  140. if (get_export_platform().is_valid()) {
  141. 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)));
  142. }
  143. } else if (E.value.count > 1) {
  144. if (get_export_platform().is_valid()) {
  145. 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)));
  146. }
  147. }
  148. }
  149. }