Skip to content

Commit

Permalink
wip: try native assets
Browse files Browse the repository at this point in the history
  • Loading branch information
jjanku committed Mar 30, 2024
1 parent 364be4e commit 51a227c
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 298 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/flutter-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:

steps:
- uses: dart-lang/setup-dart@v1
with:
sdk: main
- uses: actions/checkout@v3
with:
submodules: true
Expand All @@ -45,12 +47,6 @@ jobs:
- name: Install pdfsig
run: sudo apt-get install -y poppler-utils

- name: Install native
run: |
cargo build --release
echo "LD_LIBRARY_PATH=$(pwd)/target/release/" >> "$GITHUB_ENV"
working-directory: meesign_native/native/meesign-crypto

- name: Setup server
run: |
wget https://raw.githubusercontent.com/crocs-muni/meesign-server/main/generate_keys.sh
Expand All @@ -62,7 +58,7 @@ jobs:
crocsmuni/meesign:nightly
- name: Run tests
run: cd meesign_core; dart test
run: cd meesign_core; dart --enable-experiment=native-assets test


build:
Expand Down Expand Up @@ -108,7 +104,7 @@ jobs:
- name: Clone Flutter repository with stable channel
uses: subosito/flutter-action@v2
with:
channel: stable
channel: main
cache: true

- name: Install Android dependencies
Expand Down Expand Up @@ -148,6 +144,9 @@ jobs:
flutter config --enable-linux-desktop
flutter config --enable-windows-desktop
- name: Enable native assets
run: flutter config --enable-native-assets

# Fetch dart packages
- run: flutter pub get

Expand Down
2 changes: 2 additions & 0 deletions meesign_native/ffigen/meesign-crypto.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ headers:
# so specify only the headers we need (most notably avoid system headers)
include-directives:
- '**bindings.h'
ffi-native:
assetId: package:meesign_native/libmeesign_crypto
80 changes: 80 additions & 0 deletions meesign_native/hook/build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'dart:io';

import 'package:native_assets_cli/native_assets_cli.dart';

String selectTarget(OS os, Architecture architecture) {
return switch ((os, architecture)) {
(OS.linux, Architecture.x64) => 'x86_64-unknown-linux-gnu',
(OS.windows, Architecture.x64) => 'x86_64-pc-windows-msvc',
(OS.macOS, Architecture.x64) => 'x86_64-apple-darwin',
(OS.macOS, Architecture.arm64) => 'aarch64-apple-darwin',
(OS.android, Architecture.x64) => 'x86_64-linux-android',
(OS.android, Architecture.arm64) => 'aarch64-linux-android',
(OS.iOS, Architecture.arm64) => 'aarch64-apple-ios',
_ => throw UnsupportedError('($os, $architecture) not supported'),
};
}

String libPrefix(OS os) => switch (os) {
OS.linux || OS.macOS || OS.android || OS.iOS => 'lib',
_ => '',
};

String libSuffix(OS os) => switch (os) {
OS.linux || OS.android => '.so',
OS.macOS || OS.iOS => '.dylib',
OS.windows => '.dll',
_ => throw UnsupportedError(''),
};

void main(List<String> args) async {
await build(args, (config, output) async {
if (config.linkModePreference == LinkModePreference.static) {
// Simulate that this hook only supports dynamic libraries.
throw UnsupportedError(
'LinkModePreference.static is not supported.',
);
}

final target = selectTarget(
config.targetOS,
config.targetArchitecture ?? Architecture.current,
);
String libName =
'${libPrefix(config.targetOS)}meesign_crypto${libSuffix(config.targetOS)}';
final assetPath = config.outputDirectory.resolve(libName);
final cryptoDir = config.packageRoot.resolve('native/meesign-crypto/');

if (!config.dryRun) {
final result = await Process.run(
'cargo',
['build', '--release', '--target', target],
workingDirectory: cryptoDir.toFilePath(),
includeParentEnvironment: true,
runInShell: true,
);
stdout.write(result.stdout);
stderr.write(result.stderr);

final libUri = cryptoDir.resolve('target/$target/release/$libName');
await File.fromUri(libUri).copy(assetPath.toFilePath());

output.addDependencies([
cryptoDir,
config.packageRoot.resolve('hook/build.dart'),
]);
}

output.addAsset(
// TODO: Change to DataAsset once the Dart/Flutter SDK can consume it.
NativeCodeAsset(
package: config.packageName,
name: 'libmeesign_crypto',
file: assetPath,
linkMode: DynamicLoadingBundled(),
os: config.targetOS,
architecture: config.targetArchitecture,
),
);
});
}
13 changes: 0 additions & 13 deletions meesign_native/lib/src/dl_util.dart

This file was deleted.

Loading

0 comments on commit 51a227c

Please sign in to comment.