gdextension_export_plugin.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "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. ERR_FAIL_COND_MSG(!config->has_section_key("configuration", "entry_symbol"), "Failed to export GDExtension file, missing entry symbol: " + p_path);
  47. String entry_symbol = config->get_value("configuration", "entry_symbol");
  48. HashSet<String> all_archs;
  49. all_archs.insert("x86_32");
  50. all_archs.insert("x86_64");
  51. all_archs.insert("arm32");
  52. all_archs.insert("arm64");
  53. all_archs.insert("rv64");
  54. all_archs.insert("ppc32");
  55. all_archs.insert("ppc64");
  56. all_archs.insert("wasm32");
  57. all_archs.insert("universal");
  58. HashSet<String> archs;
  59. HashSet<String> features_wo_arch;
  60. for (const String &tag : p_features) {
  61. if (all_archs.has(tag)) {
  62. archs.insert(tag);
  63. } else {
  64. features_wo_arch.insert(tag);
  65. }
  66. }
  67. if (archs.is_empty()) {
  68. archs.insert("unknown_arch"); // Not archs specified, still try to match.
  69. }
  70. for (const String &arch_tag : archs) {
  71. PackedStringArray tags;
  72. String library_path = GDExtension::find_extension_library(
  73. p_path, config, [features_wo_arch, arch_tag](String p_feature) { return features_wo_arch.has(p_feature) || (p_feature == arch_tag); }, &tags);
  74. if (!library_path.is_empty()) {
  75. add_shared_object(library_path, tags);
  76. if (p_features.has("iOS") && (library_path.ends_with(".a") || library_path.ends_with(".xcframework"))) {
  77. String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"
  78. "extern void add_ios_init_callback(void (*cb)());\n"
  79. "\n"
  80. "extern \"C\" void $ENTRY();\n"
  81. "void $ENTRY_init() {\n"
  82. " if (&$ENTRY) register_dynamic_symbol((char *)\"$ENTRY\", (void *)$ENTRY);\n"
  83. "}\n"
  84. "struct $ENTRY_struct {\n"
  85. " $ENTRY_struct() {\n"
  86. " add_ios_init_callback($ENTRY_init);\n"
  87. " }\n"
  88. "};\n"
  89. "$ENTRY_struct $ENTRY_struct_instance;\n\n";
  90. additional_code = additional_code.replace("$ENTRY", entry_symbol);
  91. add_ios_cpp_code(additional_code);
  92. String linker_flags = "-Wl,-U,_" + entry_symbol;
  93. add_ios_linker_flags(linker_flags);
  94. }
  95. } else {
  96. Vector<String> features_vector;
  97. for (const String &E : p_features) {
  98. features_vector.append(E);
  99. }
  100. ERR_FAIL_MSG(vformat("No suitable library found for GDExtension: %s. Possible feature flags for your platform: %s", p_path, String(", ").join(features_vector)));
  101. }
  102. List<String> dependencies;
  103. if (config->has_section("dependencies")) {
  104. config->get_section_keys("dependencies", &dependencies);
  105. }
  106. for (const String &E : dependencies) {
  107. Vector<String> dependency_tags = E.split(".");
  108. bool all_tags_met = true;
  109. for (int i = 0; i < dependency_tags.size(); i++) {
  110. String tag = dependency_tags[i].strip_edges();
  111. if (!p_features.has(tag)) {
  112. all_tags_met = false;
  113. break;
  114. }
  115. }
  116. if (all_tags_met) {
  117. Dictionary dependency = config->get_value("dependencies", E);
  118. for (const Variant *key = dependency.next(nullptr); key; key = dependency.next(key)) {
  119. String dependency_path = *key;
  120. String target_path = dependency[*key];
  121. if (dependency_path.is_relative_path()) {
  122. dependency_path = p_path.get_base_dir().path_join(dependency_path);
  123. }
  124. add_shared_object(dependency_path, dependency_tags, target_path);
  125. }
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. #endif // GDEXTENSION_EXPORT_PLUGIN_H