Skip to content

Commit f877b97

Browse files
committed
Add support for Swift Package Manager dependencies in Apple export plugins
1 parent 6d6e822 commit f877b97

5 files changed

Lines changed: 109 additions & 5 deletions

File tree

editor/export/editor_export_platform_apple_embedded.cpp

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,12 @@ String EditorExportPlatformAppleEmbedded::_process_config_file_line(const Ref<Ed
416416
strnew += p_line.replace("$modules_buildphase", p_config.modules_buildphase) + "\n";
417417
} else if (p_line.contains("$modules_buildgrp")) {
418418
strnew += p_line.replace("$modules_buildgrp", p_config.modules_buildgrp) + "\n";
419+
} else if (p_line.contains("$spm_packages")) {
420+
strnew += p_line.replace("$spm_packages", p_config.spm_packages) + "\n";
421+
} else if (p_line.contains("$spm_package_refs")) {
422+
strnew += p_line.replace("$spm_package_refs", p_config.spm_package_refs) + "\n";
423+
} else if (p_line.contains("$spm_package_products")) {
424+
strnew += p_line.replace("$spm_package_products", p_config.spm_package_products) + "\n";
419425
} else if (p_line.contains("$name")) {
420426
strnew += p_line.replace("$name", p_config.pkg_name) + "\n";
421427
} else if (p_line.contains("$bundle_identifier")) {
@@ -1645,6 +1651,67 @@ Error EditorExportPlatformAppleEmbedded::_export_apple_embedded_plugins(const Re
16451651
p_config_data.linker_flags += result_linker_flags;
16461652
}
16471653

1654+
// SPM Packages
1655+
{
1656+
PbxId pbx_id;
1657+
pbx_id.high_bits = 0xDEB00000;
1658+
pbx_id.mid_bits = 0;
1659+
pbx_id.low_bits = 0;
1660+
1661+
HashMap<String, PluginConfigAppleEmbedded::SPMPackage> unique_packages;
1662+
for (int i = 0; i < enabled_plugins.size(); i++) {
1663+
for (const PluginConfigAppleEmbedded::SPMPackage &package : enabled_plugins[i].spm_packages) {
1664+
if (!unique_packages.has(package.url)) {
1665+
unique_packages[package.url] = package;
1666+
} else {
1667+
for (const String &product : package.products) {
1668+
if (unique_packages[package.url].products.find(product) == -1) {
1669+
unique_packages[package.url].products.push_back(product);
1670+
}
1671+
}
1672+
}
1673+
}
1674+
}
1675+
1676+
Vector<String> sorted_package_urls;
1677+
for (const KeyValue<String, PluginConfigAppleEmbedded::SPMPackage> &E : unique_packages) {
1678+
sorted_package_urls.push_back(E.key);
1679+
}
1680+
sorted_package_urls.sort();
1681+
1682+
for (const String &url : sorted_package_urls) {
1683+
const PluginConfigAppleEmbedded::SPMPackage &package = unique_packages[url];
1684+
String package_id = (++pbx_id).str();
1685+
String package_name = package.url.get_file().get_basename();
1686+
1687+
p_config_data.spm_packages += vformat("\t\t\t\t%s /* %s */,\n", package_id, package_name);
1688+
1689+
p_config_data.spm_package_refs += vformat("\t\t%s /* %s */ = {\n", package_id, package_name);
1690+
p_config_data.spm_package_refs += "\t\t\tisa = XCRemoteSwiftPackageReference;\n";
1691+
p_config_data.spm_package_refs += vformat("\t\t\trepositoryURL = \"%s\";\n", package.url);
1692+
p_config_data.spm_package_refs += "\t\t\trequirement = {\n";
1693+
p_config_data.spm_package_refs += "\t\t\t\tkind = exactVersion;\n";
1694+
p_config_data.spm_package_refs += vformat("\t\t\t\tversion = %s;\n", package.version);
1695+
p_config_data.spm_package_refs += "\t\t\t};\n";
1696+
p_config_data.spm_package_refs += "\t\t};\n";
1697+
1698+
for (const String &product : package.products) {
1699+
String product_id = (++pbx_id).str();
1700+
String build_file_id = (++pbx_id).str();
1701+
1702+
p_config_data.spm_package_products += vformat("\t\t%s /* %s */ = {\n", product_id, product);
1703+
p_config_data.spm_package_products += "\t\t\tisa = XCSwiftPackageProductDependency;\n";
1704+
p_config_data.spm_package_products += vformat("\t\t\tpackage = %s /* %s */;\n", package_id, package_name);
1705+
p_config_data.spm_package_products += vformat("\t\t\tproductName = \"%s\";\n", product);
1706+
p_config_data.spm_package_products += "\t\t};\n";
1707+
1708+
p_config_data.modules_buildfile += vformat("\t\t%s /* %s in Frameworks */ = {isa = PBXBuildFile; productRef = %s /* %s */; };\n", build_file_id, product, product_id, product);
1709+
1710+
p_config_data.modules_buildphase += vformat("\t\t\t\t%s /* %s in Frameworks */,\n", build_file_id, product);
1711+
}
1712+
}
1713+
}
1714+
16481715
return OK;
16491716
}
16501717

@@ -1819,11 +1886,14 @@ Error EditorExportPlatformAppleEmbedded::_export_project_helper(const Ref<Editor
18191886
String(" ").join(_get_preset_architectures(p_preset)),
18201887
_get_linker_flags(),
18211888
_get_cpp_code(),
1822-
"",
1823-
"",
1824-
"",
1825-
"",
1826-
Vector<String>(),
1889+
"", // modules_buildfile
1890+
"", // modules_fileref
1891+
"", // modules_buildphase
1892+
"", // modules_buildgrp
1893+
"", // spm_packages
1894+
"", // spm_package_refs
1895+
"", // spm_package_products
1896+
Vector<String>(), // capabilities
18271897
};
18281898

18291899
config_data.plist_content += p_preset->get("application/additional_plist_content").operator String() + "\n";

editor/export/editor_export_platform_apple_embedded.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ class EditorExportPlatformAppleEmbedded : public EditorExportPlatform {
143143
String modules_fileref;
144144
String modules_buildphase;
145145
String modules_buildgrp;
146+
String spm_packages;
147+
String spm_package_refs;
148+
String spm_package_products;
146149
Vector<String> capabilities;
147150
};
148151

editor/export/plugin_config_apple_embedded.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ PluginConfigAppleEmbedded PluginConfigAppleEmbedded::load_plugin_config(Ref<Conf
205205
plugin_config.capabilities = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_CAPABILITIES_KEY, Vector<String>());
206206

207207
plugin_config.linker_flags = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_LINKER_FLAGS, Vector<String>());
208+
209+
Array spm_packages = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_SPM_PACKAGES_KEY, Array());
210+
for (int i = 0; i < spm_packages.size(); i++) {
211+
Dictionary package_config = spm_packages[i];
212+
PluginConfigAppleEmbedded::SPMPackage package;
213+
package.url = package_config.get("url", String());
214+
package.version = package_config.get("version", String());
215+
216+
Array products = package_config.get("products", Array());
217+
for (int j = 0; j < products.size(); j++) {
218+
package.products.push_back(products[j]);
219+
}
220+
plugin_config.spm_packages.push_back(package);
221+
}
208222
}
209223

210224
if (config_file->has_section(PluginConfigAppleEmbedded::PLIST_SECTION)) {

editor/export/plugin_config_apple_embedded.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ The `dependencies` and fields are optional.
4444
- **system**: system dependencies that should be linked.
4545
- **capabilities**: capabilities that would be used for `UIRequiredDeviceCapabilities` options in Info.plist file.
4646
- **files**: files that would be copied into application
47+
- **spm_packages**: array of Swift Package Manager dependencies (url, version, products).
4748
4849
The `plist` section are optional.
4950
- **key**: key and value that would be added in Info.plist file.
@@ -65,6 +66,7 @@ struct PluginConfigAppleEmbedded {
6566
inline static const char *DEPENDENCIES_CAPABILITIES_KEY = "capabilities";
6667
inline static const char *DEPENDENCIES_FILES_KEY = "files";
6768
inline static const char *DEPENDENCIES_LINKER_FLAGS = "linker_flags";
69+
inline static const char *DEPENDENCIES_SPM_PACKAGES_KEY = "spm_packages";
6870

6971
inline static const char *PLIST_SECTION = "plist";
7072

@@ -82,6 +84,12 @@ struct PluginConfigAppleEmbedded {
8284
String value;
8385
};
8486

87+
struct SPMPackage {
88+
String url;
89+
String version;
90+
Vector<String> products;
91+
};
92+
8593
// Set to true when the config file is properly loaded.
8694
bool valid_config = false;
8795
bool supports_targets = false;
@@ -104,6 +112,9 @@ struct PluginConfigAppleEmbedded {
104112

105113
Vector<String> linker_flags;
106114

115+
// Optional spm_dependencies section.
116+
Vector<SPMPackage> spm_packages;
117+
107118
// Optional plist section
108119
// String value is default value.
109120
// Currently supports `string`, `boolean`, `integer`, `raw`, `string_input` types

misc/dist/apple_embedded_xcode/godot_apple_embedded.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@
176176
productRefGroup = D0BCFE3518AEBDA2004A7AAE /* Products */;
177177
projectDirPath = "";
178178
projectRoot = "";
179+
packageReferences = (
180+
$spm_packages
181+
);
179182
targets = (
180183
D0BCFE3318AEBDA2004A7AAE /* $binary */,
181184
);
@@ -407,6 +410,9 @@
407410
defaultConfigurationName = Debug;
408411
};
409412
/* End XCConfigurationList section */
413+
414+
$spm_package_refs
415+
$spm_package_products
410416
};
411417
rootObject = D0BCFE2C18AEBDA2004A7AAE /* Project object */;
412418
}

0 commit comments

Comments
 (0)