From 732c6c9beb7c3b605a58f0b5f86b8046ec11f222 Mon Sep 17 00:00:00 2001 From: Losses Don <1384036+Losses@users.noreply.github.com> Date: Sat, 14 Sep 2024 20:02:23 +0800 Subject: [PATCH 1/3] fix: Unable to build plugins with ffi --- lib/elinux_plugins.dart | 92 ++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 15 deletions(-) diff --git a/lib/elinux_plugins.dart b/lib/elinux_plugins.dart index 946d77b..9c1a41f 100644 --- a/lib/elinux_plugins.dart +++ b/lib/elinux_plugins.dart @@ -99,6 +99,21 @@ class ELinuxPlugin extends PluginPlatform implements NativeOrDartPlugin { } String get path => directory.parent.path; + + /// Method to return a detailed debug string for the plugin. + @override + String toString() { + return ''' +ELinuxPlugin: + name: $name + directory: ${directory.path} + pluginClass: $pluginClass + dartPluginClass: $dartPluginClass + ffiPlugin: $ffiPlugin + defaultPackage: $defaultPackage + dependencies: ${dependencies?.join(', ')} +'''; + } } /// Source: [_internalCapitalLetterRegex] in `platform_plugins.dart` (exact copy) @@ -426,12 +441,20 @@ Future> findELinuxPlugins( final ELinuxPlugin? plugin = _pluginFromPackage(package.name, packageRoot); if (plugin == null) { continue; - } else if (nativeOnly && - (plugin.pluginClass == null || plugin.pluginClass == 'none')) { + } + + final bool isFfi = plugin.ffiPlugin ?? false; + + if (nativeOnly && + ((plugin.pluginClass == null || plugin.pluginClass == 'none') && + !isFfi)) { continue; - } else if (dartOnly && plugin.dartPluginClass == null) { + } + + if (dartOnly && (plugin.dartPluginClass == null || isFfi)) { continue; } + plugins.add(plugin); } return plugins; @@ -513,18 +536,45 @@ void registerPlugins() { ); } +/// Filters out any plugins that don't use method channels, and thus shouldn't be added to the native generated registrants. +List _filterMethodChannelPlugins(List plugins) { + return plugins.where((ELinuxPlugin plugin) { + return (plugin as NativeOrDartPlugin).hasMethodChannel(); + }).toList(); +} + +/// Filters out Dart-only and method channel plugins. +/// +/// FFI plugins do not need native code registration, but their binaries need to be bundled. +List _filterFfiPlugins(List plugins) { + return plugins.where((ELinuxPlugin plugin) { + final NativeOrDartPlugin plugin_ = plugin as NativeOrDartPlugin; + return plugin_.hasFfi(); + }).toList(); +} + /// See: [_writeWindowsPluginFiles] in `plugins.dart` void _writePluginCmakefileTemplate( ELinuxProject eLinuxProject, Directory registryDirectory, List plugins, ) { - final List> pluginConfigs = - plugins.map((ELinuxPlugin plugin) => plugin.toMap()).toList(); + final List methodChannelPlugins = + _filterMethodChannelPlugins(plugins); + final List ffiPlugins = _filterFfiPlugins(plugins) + ..removeWhere(methodChannelPlugins.contains); + + final List> methodChannelPluginsMap = + methodChannelPlugins.map((ELinuxPlugin plugin) => plugin.toMap()).toList(); + final List> ffiPluginsMap = + ffiPlugins.map((ELinuxPlugin plugin) => plugin.toMap()).toList(); + final Map context = { - 'plugins': pluginConfigs, + 'methodChannelPlugins': methodChannelPluginsMap, + 'ffiPlugins': ffiPluginsMap, 'pluginsDir': _cmakeRelativePluginSymlinkDirectoryPath(eLinuxProject), }; + _renderTemplateToFile( ''' // @@ -547,22 +597,23 @@ void RegisterPlugins(flutter::PluginRegistry* registry); _renderTemplateToFile( ''' // -// Generated file. Do not edit. +// Generated file. Do not edit. // // clang-format off #include "generated_plugin_registrant.h" -{{#plugins}} +{{#methodChannelPlugins}} #include <{{name}}/{{filename}}.h> -{{/plugins}} +{{/methodChannelPlugins}} void RegisterPlugins(flutter::PluginRegistry* registry) { -{{#plugins}} - {{class}}RegisterWithRegistrar( - registry->GetRegistrarForPlugin("{{class}}")); -{{/plugins}} +{{#methodChannelPlugins}} + g_autoptr(FlPluginRegistrar) {{name}}_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "{{class}}"); + {{filename}}_register_with_registrar({{name}}_registrar); +{{/methodChannelPlugins}} } ''', context, @@ -575,9 +626,15 @@ void RegisterPlugins(flutter::PluginRegistry* registry) { # list(APPEND FLUTTER_PLUGIN_LIST -{{#plugins}} +{{#methodChannelPlugins}} {{name}} -{{/plugins}} +{{/methodChannelPlugins}} +) + +list(APPEND FLUTTER_FFI_PLUGIN_LIST +{{#ffiPlugins}} + {{name}} +{{/ffiPlugins}} ) set(PLUGIN_BUNDLED_LIBRARIES) @@ -588,6 +645,11 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST}) list(APPEND PLUGIN_BUNDLED_LIBRARIES $) list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries}) endforeach(plugin) + +foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST}) + add_subdirectory({{pluginsDir}}/${ffi_plugin}/elinux plugins/${ffi_plugin}) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries}) +endforeach(ffi_plugin) ''', context, registryDirectory.childFile('generated_plugins.cmake').path, From be9854e9c88d778db77801369d70da9da33927ff Mon Sep 17 00:00:00 2001 From: LOSSES Don <1384036+Losses@users.noreply.github.com> Date: Sun, 15 Sep 2024 00:48:00 +0000 Subject: [PATCH 2/3] fix: Format issue --- lib/elinux_plugins.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/elinux_plugins.dart b/lib/elinux_plugins.dart index 9c1a41f..5d4e64b 100644 --- a/lib/elinux_plugins.dart +++ b/lib/elinux_plugins.dart @@ -565,7 +565,9 @@ void _writePluginCmakefileTemplate( ..removeWhere(methodChannelPlugins.contains); final List> methodChannelPluginsMap = - methodChannelPlugins.map((ELinuxPlugin plugin) => plugin.toMap()).toList(); + methodChannelPlugins + .map((ELinuxPlugin plugin) => plugin.toMap()) + .toList(); final List> ffiPluginsMap = ffiPlugins.map((ELinuxPlugin plugin) => plugin.toMap()).toList(); From a351b5b6e00ccd605a75489a5a02af20b868d32e Mon Sep 17 00:00:00 2001 From: LOSSES Don <1384036+Losses@users.noreply.github.com> Date: Sun, 15 Sep 2024 05:33:53 +0000 Subject: [PATCH 3/3] fix: Fix plugin interfaces issue --- lib/elinux_plugins.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/elinux_plugins.dart b/lib/elinux_plugins.dart index 5d4e64b..2463dad 100644 --- a/lib/elinux_plugins.dart +++ b/lib/elinux_plugins.dart @@ -612,9 +612,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry); void RegisterPlugins(flutter::PluginRegistry* registry) { {{#methodChannelPlugins}} - g_autoptr(FlPluginRegistrar) {{name}}_registrar = - fl_plugin_registry_get_registrar_for_plugin(registry, "{{class}}"); - {{filename}}_register_with_registrar({{name}}_registrar); + {{class}}RegisterWithRegistrar( + registry->GetRegistrarForPlugin("{{class}}")); {{/methodChannelPlugins}} } ''',