Path: blob/master/editor/export/gdextension_export_plugin.h
20952 views
/**************************************************************************/1/* gdextension_export_plugin.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/extension/gdextension_library_loader.h"33#include "editor/export/editor_export.h"3435class GDExtensionExportPlugin : public EditorExportPlugin {36GDSOFTCLASS(GDExtensionExportPlugin, EditorExportPlugin);3738protected:39virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) override;40virtual String get_name() const override { return "GDExtension"; }41};4243void GDExtensionExportPlugin::_export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) {44if (p_type != "GDExtension") {45return;46}4748Ref<ConfigFile> config;49config.instantiate();5051Error err = config->load(p_path);52ERR_FAIL_COND_MSG(err, "Failed to load GDExtension file: " + p_path);5354// Check whether this GDExtension should be exported.55bool android_aar_plugin = config->get_value("configuration", "android_aar_plugin", false);56if (android_aar_plugin && p_features.has("android")) {57// The gdextension configuration and Android .so files will be provided by the Android aar58// plugin it's part of, so we abort here.59skip();60return;61}6263ERR_FAIL_COND_MSG(!config->has_section_key("configuration", "entry_symbol"), "Failed to export GDExtension file, missing entry symbol: " + p_path);6465String entry_symbol = config->get_value("configuration", "entry_symbol");6667HashSet<String> all_archs;68all_archs.insert("x86_32");69all_archs.insert("x86_64");70all_archs.insert("arm32");71all_archs.insert("arm64");72all_archs.insert("rv64");73all_archs.insert("ppc64");74all_archs.insert("wasm32");75all_archs.insert("loongarch64");76all_archs.insert("universal");7778HashSet<String> archs;79HashSet<String> features_wo_arch;80Vector<String> features_vector;81for (const String &tag : p_features) {82if (all_archs.has(tag)) {83archs.insert(tag);84} else {85features_wo_arch.insert(tag);86}87features_vector.append(tag);88}8990if (archs.is_empty()) {91archs.insert("unknown_arch"); // Not archs specified, still try to match.92}9394HashSet<String> libs_added;95struct FoundLibInfo {96int count = 0;97Vector<String> libs;98};99HashMap<String, FoundLibInfo> libs_found;100for (const String &arch_tag : archs) {101if (arch_tag != "universal") {102libs_found[arch_tag] = FoundLibInfo();103}104}105106for (const String &arch_tag : archs) {107PackedStringArray tags;108String library_path = GDExtensionLibraryLoader::find_extension_library(109p_path, config, [features_wo_arch, arch_tag](const String &p_feature) { return features_wo_arch.has(p_feature) || (p_feature == arch_tag); }, &tags);110111if (libs_added.has(library_path)) {112continue; // Universal library, already added for another arch, do not duplicate.113}114if (!library_path.is_empty()) {115libs_added.insert(library_path);116add_shared_object(library_path, tags);117118if (p_features.has("apple_embedded") && (library_path.ends_with(".a") || library_path.ends_with(".xcframework"))) {119String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"120"extern void add_apple_embedded_platform_init_callback(void (*cb)());\n"121"\n"122"extern \"C\" void $ENTRY();\n"123"void $ENTRY_init() {\n"124" if (&$ENTRY) register_dynamic_symbol((char *)\"$ENTRY\", (void *)$ENTRY);\n"125"}\n"126"struct $ENTRY_struct {\n"127" $ENTRY_struct() {\n"128" add_apple_embedded_platform_init_callback($ENTRY_init);\n"129" }\n"130"};\n"131"$ENTRY_struct $ENTRY_struct_instance;\n\n";132additional_code = additional_code.replace("$ENTRY", entry_symbol);133add_apple_embedded_platform_cpp_code(additional_code);134135String linker_flags = "-Wl,-U,_" + entry_symbol;136add_apple_embedded_platform_linker_flags(linker_flags);137}138139// Update found library info.140if (arch_tag == "universal") {141for (const String &sub_arch_tag : archs) {142if (sub_arch_tag != "universal") {143libs_found[sub_arch_tag].count++;144libs_found[sub_arch_tag].libs.push_back(library_path);145}146}147} else {148libs_found[arch_tag].count++;149libs_found[arch_tag].libs.push_back(library_path);150}151}152153Vector<SharedObject> dependencies_shared_objects = GDExtensionLibraryLoader::find_extension_dependencies(154p_path, config, [features_wo_arch, arch_tag](String p_feature) { return features_wo_arch.has(p_feature) || (p_feature == arch_tag); });155for (const SharedObject &shared_object : dependencies_shared_objects) {156_add_shared_object(shared_object);157}158}159160for (const KeyValue<String, FoundLibInfo> &E : libs_found) {161if (E.value.count == 0) {162if (get_export_platform().is_valid()) {163get_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)));164}165} else if (E.value.count > 1) {166if (get_export_platform().is_valid()) {167get_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)));168}169}170}171}172173174