2
0

gdextension_export_plugin.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*************************************************************************/
  2. /* gdextension_export_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. };
  37. void GDExtensionExportPlugin::_export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) {
  38. if (p_type != "NativeExtension") {
  39. return;
  40. }
  41. Ref<ConfigFile> config;
  42. config.instantiate();
  43. Error err = config->load(p_path);
  44. ERR_FAIL_COND_MSG(err, "Failed to load GDExtension file: " + p_path);
  45. ERR_FAIL_COND_MSG(!config->has_section_key("configuration", "entry_symbol"), "Failed to export GDExtension file, missing entry symbol: " + p_path);
  46. String entry_symbol = config->get_value("configuration", "entry_symbol");
  47. List<String> libraries;
  48. config->get_section_keys("libraries", &libraries);
  49. bool could_export = false;
  50. for (const String &E : libraries) {
  51. Vector<String> tags = E.split(".");
  52. bool all_tags_met = true;
  53. for (int i = 0; i < tags.size(); i++) {
  54. String tag = tags[i].strip_edges();
  55. if (!p_features.has(tag)) {
  56. all_tags_met = false;
  57. break;
  58. }
  59. }
  60. if (all_tags_met) {
  61. String library_path = config->get_value("libraries", E);
  62. if (!library_path.begins_with("res://")) {
  63. print_line("Skipping export of out-of-project library " + library_path);
  64. continue;
  65. }
  66. add_shared_object(library_path, tags);
  67. if (p_features.has("iOS") && (library_path.ends_with(".a") || library_path.ends_with(".xcframework"))) {
  68. String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"
  69. "extern void add_ios_init_callback(void (*cb)());\n"
  70. "\n"
  71. "extern \"C\" void $ENTRY();\n"
  72. "void $ENTRY_init() {\n"
  73. " if (&$ENTRY) register_dynamic_symbol((char *)\"$ENTRY\", (void *)$ENTRY);\n"
  74. "}\n"
  75. "struct $ENTRY_struct {\n"
  76. " $ENTRY_struct() {\n"
  77. " add_ios_init_callback($ENTRY_init);\n"
  78. " }\n"
  79. "};\n"
  80. "$ENTRY_struct $ENTRY_struct_instance;\n\n";
  81. additional_code = additional_code.replace("$ENTRY", entry_symbol);
  82. add_ios_cpp_code(additional_code);
  83. String linker_flags = "-Wl,-U,_" + entry_symbol;
  84. add_ios_linker_flags(linker_flags);
  85. }
  86. could_export = true;
  87. break;
  88. }
  89. }
  90. if (!could_export) {
  91. Vector<String> tags;
  92. for (const String &E : p_features) {
  93. tags.append(E);
  94. }
  95. ERR_FAIL_MSG(vformat("Couldn't export extension: %s. No suitable library found for export flags: %s", p_path, String(", ").join(tags)));
  96. }
  97. List<String> dependencies;
  98. if (config->has_section("dependencies")) {
  99. config->get_section_keys("dependencies", &dependencies);
  100. }
  101. for (const String &E : libraries) {
  102. Vector<String> tags = E.split(".");
  103. bool all_tags_met = true;
  104. for (int i = 0; i < tags.size(); i++) {
  105. String tag = tags[i].strip_edges();
  106. if (!p_features.has(tag)) {
  107. all_tags_met = false;
  108. break;
  109. }
  110. }
  111. if (all_tags_met) {
  112. Dictionary dependency = config->get_value("dependencies", E);
  113. for (const Variant *key = dependency.next(nullptr); key; key = dependency.next(key)) {
  114. String library_path = *key;
  115. String target_path = dependency[*key];
  116. if (!library_path.begins_with("res://")) {
  117. print_line("Skipping export of out-of-project library " + library_path);
  118. continue;
  119. }
  120. add_shared_object(library_path, tags, target_path);
  121. }
  122. break;
  123. }
  124. }
  125. }
  126. #endif // GDEXTENSION_EXPORT_PLUGIN_H