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