Files
breez-sdk-liquid/packages/dart/lib/breez_liquid.dart
Erdem Yerebasmaz f6fa503cb9 Flutter uniFFI (#510)
* Flutter uniffi

* Set on-demand resources

* Do not build non-uniffi libraries

* Change iosLibName

* Add BreezSDKLiquid as on demand resources

* Use downloaded framework

* Add Sources to published flutter package

* Set OTHER_LDFLAGS

* Add logging

* Refactor library initialization logic and throw an error if initialization fails

* Do not statically link framework on production

* Use uniFFI headers to generate FlutterBreezLiquidBindings

* Re add frb header

* Correct the library name

* Remove static_framework

* Move source header files

* Copy iOS podspecs to macOS folder

* Update version of macOS podspecs

* Remove Windows & Linux support

* Remove CMake scripts

* Remove breez_sdk_liquid.podspec from version script

* Cleanup older build scripts used by melos & just recipes

* Remove softlink & copy recipes

Add recipe descriptions

* Rename link-uniffi recipe to link-headers

Make sure headers are linked after uniffi is built
  - Remove just gen recipe
Add recipe descriptions

* Set package versions on production files as well when publishing

* Include bindings project on melos script hooks

* Flutter uniffi

* Set on-demand resources

* Do not build non-uniffi libraries

* Change iosLibName

* Add BreezSDKLiquid as on demand resources

* Use downloaded framework

* Add Sources to published flutter package

* Set OTHER_LDFLAGS

* Add logging

* Refactor library initialization logic and throw an error if initialization fails

* Do not statically link framework on production

* Use uniFFI headers to generate FlutterBreezLiquidBindings

* Re add frb header

* Correct the library name

* Remove static_framework

* Move source header files

* Copy iOS podspecs to macOS folder

* Update version of macOS podspecs

* Remove Windows & Linux support

* Remove CMake scripts

* Remove breez_sdk_liquid.podspec from version script

* Cleanup older build scripts used by melos & just recipes

* Remove softlink & copy recipes

Add recipe descriptions

* Rename link-uniffi recipe to link-headers

Make sure headers are linked after uniffi is built
  - Remove just gen recipe
Add recipe descriptions

* Set package versions on production files as well when publishing

* Include bindings project on melos script hooks

* chore: just version

* fix: remove unused files on "Set package version" step

* copy FFI header files

* [WIP] Add macOS support

* remove example app on Flutter plugin

* Link headers before running ffigen on CI workflow

* macOS: add macos/Sources folder to .gitignore

.

* macOS: Copy iOS sources to macOS sources after downloading bindings

* macOS: copy sources & framework file to macos folder on build-uniffi-swift script

* import breez_sdk_liquidFFI header on plugin file

Update flutter_breez_liquid.c

* cleanup header file artifacts

---------

Co-authored-by: Ross Savage <hello@satimoto.com>
2024-10-01 10:59:11 +03:00

78 lines
2.2 KiB
Dart

/// Dart bindings for the Breez Liquid SDK
library;
export 'src/bindings.dart';
export 'src/model.dart';
export 'src/error.dart';
export 'src/bindings/duplicates.dart';
import 'dart:io';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'package:logging/logging.dart';
import 'src/frb_generated.dart';
typedef BreezLiquid = RustLibApi;
typedef BreezLiquidImpl = RustLibApiImpl;
const libName = 'libbreez_sdk_liquid_bindings.so';
const iosLibName = "breez_sdk_liquidFFI";
final _log = Logger('breez_liquid');
class UnsupportedPlatform implements Exception {
UnsupportedPlatform(String s);
}
Future<void> initialize({ExternalLibrary? dylib}) async {
try {
dylib ??= await _loadPlatformSpecificLibrary();
_log.info(dylib != null
? 'Initializing RustLib with the provided library'
: 'Initializing RustLib with the default ExternalLibrary');
await RustLib.init(externalLibrary: dylib);
} catch (e, stacktrace) {
_log.severe('Initialization failed: $e', e, stacktrace);
rethrow;
}
}
Future<ExternalLibrary?> _loadPlatformSpecificLibrary() async {
switch (Platform.operatingSystem) {
case 'android':
case 'linux':
return _loadAndroidLinuxLibrary();
case 'ios':
case 'macos':
return _loadIOSMacOSLibrary();
default:
_log.severe('${Platform.operatingSystem} is not yet supported!');
throw UnsupportedPlatform('${Platform.operatingSystem} is not supported!');
}
}
Future<ExternalLibrary?> _loadAndroidLinuxLibrary() async {
try {
_log.info('Attempting to load $libName for Android/Linux');
return ExternalLibrary.open(libName);
} catch (e) {
_log.warning('Failed to load $libName for Android/Linux: $e');
return null;
}
}
Future<ExternalLibrary?> _loadIOSMacOSLibrary() async {
try {
_log.info('Attempting to use iOS/MacOS framework');
return ExternalLibrary.open("$iosLibName.framework/$iosLibName");
} catch (e) {
_log.warning('iOS/MacOS framework not found, attempting fallback to ExternalLibrary.process: $e');
try {
return ExternalLibrary.process(iKnowHowToUseIt: true);
} catch (e) {
_log.warning('Failed to initialize ExternalLibrary.process for iOS/MacOS: $e');
return null;
}
}
}