From b411d12909124e66229e3d84e9c70c28cb71cf45 Mon Sep 17 00:00:00 2001 From: Lockie Richter Date: Thu, 16 Jul 2026 11:58:43 +0930 Subject: [PATCH] mixin_logger: support Swift Package Manager on iOS Add an iOS Package.swift so Swift Package Manager projects no longer fall back to CocoaPods for this plugin (fixes the "does not support Swift Package Manager" warning). mixin_logger is an FFI plugin, so the shared C++ sources in src/ are reused via a forwarding source file under Sources/ (mirroring the existing CocoaPods Classes/ forwarder), and a copy of the public header is placed under include/ so the module header search path resolves mixin_logger/mixin_logger.h. The product is built as a dynamic library. Because Flutter references plugin products with underscores replaced by hyphens, the product is named mixin-logger and SPM produces mixin-logger.framework. The Dart library loader now accepts both the SPM framework name and the CocoaPods mixin_logger.framework name. Verified by building and running the example on the iOS simulator with Swift Package Manager enabled: the plugin builds via SPM, the app launches, and the FFI logger writes to file. Closes #486 Claude-Session: https://claude.ai/code/session_01HUSXFyjL8e4u3nQCvynuod --- packages/mixin_logger/CHANGELOG.md | 3 ++ .../ios/mixin_logger/Package.swift | 29 +++++++++++++++ .../include/mixin_logger/mixin_logger.h | 35 +++++++++++++++++++ .../Sources/mixin_logger/mixin_logger.cpp | 8 +++++ .../lib/src/write_to_file_ffi.dart | 14 +++++++- packages/mixin_logger/pubspec.yaml | 2 +- 6 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 packages/mixin_logger/ios/mixin_logger/Package.swift create mode 100644 packages/mixin_logger/ios/mixin_logger/Sources/mixin_logger/include/mixin_logger/mixin_logger.h create mode 100644 packages/mixin_logger/ios/mixin_logger/Sources/mixin_logger/mixin_logger.cpp diff --git a/packages/mixin_logger/CHANGELOG.md b/packages/mixin_logger/CHANGELOG.md index bb55e158..526b54a7 100644 --- a/packages/mixin_logger/CHANGELOG.md +++ b/packages/mixin_logger/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.1.4 +* support Swift Package Manager on iOS + ## 0.1.3 * support android 15 16k page size diff --git a/packages/mixin_logger/ios/mixin_logger/Package.swift b/packages/mixin_logger/ios/mixin_logger/Package.swift new file mode 100644 index 00000000..9669d852 --- /dev/null +++ b/packages/mixin_logger/ios/mixin_logger/Package.swift @@ -0,0 +1,29 @@ +// swift-tools-version: 5.9 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "mixin_logger", + platforms: [ + .iOS("12.0") + ], + products: [ + // `mixin_logger` is an FFI plugin whose Dart bindings load the native + // code from a dynamic framework, so the product is built as a dynamic + // library rather than being statically linked into the app binary. + // + // The product is named `mixin-logger` (with a hyphen) because Flutter's + // generated `FlutterGeneratedPluginSwiftPackage` references plugin + // products by the package name with underscores replaced by hyphens. + // The framework SPM builds is therefore `mixin-logger.framework`; the + // Dart loader in `lib/src/write_to_file_ffi.dart` accepts both this name + // and the CocoaPods `mixin_logger.framework` name. + .library(name: "mixin-logger", type: .dynamic, targets: ["mixin_logger"]) + ], + targets: [ + .target( + name: "mixin_logger" + ) + ] +) diff --git a/packages/mixin_logger/ios/mixin_logger/Sources/mixin_logger/include/mixin_logger/mixin_logger.h b/packages/mixin_logger/ios/mixin_logger/Sources/mixin_logger/include/mixin_logger/mixin_logger.h new file mode 100644 index 00000000..fa2dd5df --- /dev/null +++ b/packages/mixin_logger/ios/mixin_logger/Sources/mixin_logger/include/mixin_logger/mixin_logger.h @@ -0,0 +1,35 @@ +#include +#include +#include + +#if _WIN32 +#include +#else + +#include +#include + +#endif + +#if _WIN32 +#define FFI_PLUGIN_EXPORT __declspec(dllexport) +#else +#define FFI_PLUGIN_EXPORT +#endif + +#ifdef __cplusplus +extern "C" { // only need to export C interface if +// used by C++ source code +#endif + + +FFI_PLUGIN_EXPORT intptr_t +mixin_logger_init(const char *dir, intptr_t max_file_size, intptr_t max_file_count, const char *file_leading); + +FFI_PLUGIN_EXPORT intptr_t mixin_logger_set_file_leading(const char *file_leading); + +FFI_PLUGIN_EXPORT intptr_t mixin_logger_write_log(const char *log); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/packages/mixin_logger/ios/mixin_logger/Sources/mixin_logger/mixin_logger.cpp b/packages/mixin_logger/ios/mixin_logger/Sources/mixin_logger/mixin_logger.cpp new file mode 100644 index 00000000..0fcc59f1 --- /dev/null +++ b/packages/mixin_logger/ios/mixin_logger/Sources/mixin_logger/mixin_logger.cpp @@ -0,0 +1,8 @@ +// Relative import to be able to reuse the C sources. +// +// Swift Package Manager (like CocoaPods) does not support referencing source +// files outside of the package directory, so this forwarder relatively imports +// the shared implementation in `../src` so that the C sources can be shared +// among all target platforms. See the comment in ../mixin_logger.podspec for +// more information about the equivalent CocoaPods setup. +#include "../../../../src/mixin_logger.cpp" diff --git a/packages/mixin_logger/lib/src/write_to_file_ffi.dart b/packages/mixin_logger/lib/src/write_to_file_ffi.dart index 0fe1525c..deedef9c 100644 --- a/packages/mixin_logger/lib/src/write_to_file_ffi.dart +++ b/packages/mixin_logger/lib/src/write_to_file_ffi.dart @@ -11,7 +11,19 @@ const String _libName = 'mixin_logger'; /// The dynamic library in which the symbols for [MixinLoggerBindings] can be found. final DynamicLibrary _dylib = () { if (Platform.isMacOS || Platform.isIOS) { - return DynamicLibrary.open('$_libName.framework/$_libName'); + // The native code ships as a dynamic framework. With CocoaPods the + // framework is named after the pod (`mixin_logger`); with Swift Package + // Manager it is named after the SPM product (`mixin-logger`, because + // Flutter references plugin products with underscores replaced by + // hyphens). Try both so the plugin loads with either integration. + for (final framework in const ['mixin_logger', 'mixin-logger']) { + try { + return DynamicLibrary.open('$framework.framework/$framework'); + } catch (_) { + continue; + } + } + throw UnsupportedError('Failed to load the $_libName dynamic library.'); } if (Platform.isAndroid || Platform.isLinux) { return DynamicLibrary.open('lib$_libName.so'); diff --git a/packages/mixin_logger/pubspec.yaml b/packages/mixin_logger/pubspec.yaml index 0610d4ce..09403c0e 100644 --- a/packages/mixin_logger/pubspec.yaml +++ b/packages/mixin_logger/pubspec.yaml @@ -1,7 +1,7 @@ name: mixin_logger resolution: workspace description: Simple logger tool for flutter, make it easy to save your app log to file. -version: 0.1.3 +version: 0.1.4 homepage: https://github.com/MixinNetwork/flutter-plugins environment: