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 e0d7a8a commit 34f060c
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 309 deletions.
31 changes: 12 additions & 19 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 @@ -61,8 +57,13 @@ jobs:
--publish 1337:1337 --volume $(pwd)/keys/:/meesign/keys/:Z \
crocsmuni/meesign:nightly
- run: |
for PKG in native network core; do
(cd meesign_$PKG; dart pub get)
done
- name: Run tests
run: cd meesign_core; dart test
run: cd meesign_core; dart --enable-experiment=native-assets test


build:
Expand Down Expand Up @@ -114,7 +115,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 @@ -162,6 +163,9 @@ jobs:
flutter config --enable-windows-desktop
flutter config --enable-macos-desktop
- name: Enable native assets
run: flutter config --enable-native-assets

# Fetch dart packages
- run: flutter pub get

Expand All @@ -171,17 +175,6 @@ jobs:
--release
--dart-define=ALLOW_BAD_CERTS=true
- name: Build native libs for macOS
if: matrix.target == 'macOS'
run: |
cd meesign_native/native/meesign-crypto/
cargo build --release --target x86_64-apple-darwin
cargo build --release --target aarch64-apple-darwin
lipo -create -output libmeesign_crypto.dylib \
target/x86_64-apple-darwin/release/libmeesign_crypto.dylib \
target/aarch64-apple-darwin/release/libmeesign_crypto.dylib
cp libmeesign_crypto.dylib ../../../build/macos/Build/Products/Release/meesign_client.app/Contents/Frameworks/
# Package the build.
- name: Copy VC redistributables to release directory for Windows
if: matrix.target == 'Windows'
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
85 changes: 85 additions & 0 deletions meesign_native/hook/build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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.android, Architecture.arm) => 'armv7-linux-androideabi',
(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,
environment: {
'CC': config.cCompiler.compiler?.toFilePath() ?? '',
'AR': config.cCompiler.archiver?.toFilePath() ?? '',
},
);
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 34f060c

Please sign in to comment.