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