Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions examples/dart_only/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import 'package:native_toolchain_rust/native_toolchain_rust.dart';

void main(List<String> args) async {
await build(args, (input, output) async {
await const RustBuilder(assetName: 'src/ffi.g.dart').run(
input: input,
output: output,
);
await const RustBuilder(
assetName: 'src/ffi.g.dart',
).run(input: input, output: output);
});
}
7 changes: 2 additions & 5 deletions examples/dart_only/lib/src/ffi.g.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint, unused_import
// ignore_for_file: type=lint, unused_import, unused_element, deprecated_member_use_from_same_package
import 'dart:ffi' as ffi;

@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>()
external int rust_add(
int a,
int b,
);
external int rust_add(int a, int b);
2 changes: 1 addition & 1 deletion examples/dart_only/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ dependencies:
native_toolchain_rust: ^1.0.6

dev_dependencies:
ffigen: ^21.0.0
ffigen: ^22.0.0
test: ^1.29.0
19 changes: 14 additions & 5 deletions examples/dart_only/tool/ffigen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ import 'dart:io';

import 'package:ffigen/ffigen.dart';

void main() {
Future<void> main() async {
final packageRoot = Platform.script.resolve('../');
FfiGenerator(
headers: Headers(entryPoints: [packageRoot.resolve('rust/bindings.h')]),
output: Output(dartFile: packageRoot.resolve('lib/src/ffi.g.dart')),
functions: Functions.includeSet({'rust_add'}),
await FfiGenerator(
input: Input(entryPoints: [packageRoot.resolve('rust/bindings.h')]),
output: Output(
dart: DartOutput(path: packageRoot.resolve('lib/src/ffi.g.dart')),
),
visitors: [
Visitor(
func: (node) {
const toInclude = {'rust_add'};
node.isIncluded = toInclude.contains(node.originalName);
},
),
],
).generate();
}
7 changes: 3 additions & 4 deletions examples/flutter/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import 'package:native_toolchain_rust/native_toolchain_rust.dart';

void main(List<String> args) async {
await build(args, (input, output) async {
await const RustBuilder(assetName: 'src/ffi.g.dart').run(
input: input,
output: output,
);
await const RustBuilder(
assetName: 'src/ffi.g.dart',
).run(input: input, output: output);
});
}
10 changes: 5 additions & 5 deletions examples/flutter/lib/src/ffi.g.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint, unused_import
// ignore_for_file: type=lint, unused_import, unused_element, deprecated_member_use_from_same_package
import 'dart:ffi' as ffi;

@ffi.Native<ffi.Void Function()>()
external void reset_count();
@ffi.Native<ffi.Uint64 Function()>()
external int get_count();

@ffi.Native<ffi.Void Function()>()
external void increase_count();

@ffi.Native<ffi.Uint64 Function()>()
external int get_count();
@ffi.Native<ffi.Void Function()>()
external void reset_count();
2 changes: 1 addition & 1 deletion examples/flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
native_toolchain_rust: ^1.0.6

dev_dependencies:
ffigen: ^21.0.0
ffigen: ^22.0.0
flutter_test:
sdk: flutter
integration_test:
Expand Down
23 changes: 14 additions & 9 deletions examples/flutter/tool/ffigen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import 'dart:io';

import 'package:ffigen/ffigen.dart';

void main() {
Future<void> main() async {
final packageRoot = Platform.script.resolve('../');
FfiGenerator(
headers: Headers(entryPoints: [packageRoot.resolve('rust/bindings.h')]),
output: Output(dartFile: packageRoot.resolve('lib/src/ffi.g.dart')),
functions: Functions.includeSet({
'reset_count',
'increase_count',
'get_count',
}),
await FfiGenerator(
input: Input(entryPoints: [packageRoot.resolve('rust/bindings.h')]),
output: Output(
dart: DartOutput(path: packageRoot.resolve('lib/src/ffi.g.dart')),
),
visitors: [
Visitor(
func: (node) {
const toInclude = {'reset_count', 'increase_count', 'get_count'};
node.isIncluded = toInclude.contains(node.originalName);
},
),
],
).generate();
}
4 changes: 1 addition & 3 deletions native_toolchain_rust/lib/src/build_environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ interface class BuildEnvironmentFactory {
interface class AndroidBuildEnvironmentFactory {
const AndroidBuildEnvironmentFactory();

Map<String, String> createBuildEnvVars({
required CodeConfig codeConfig,
}) {
Map<String, String> createBuildEnvVars({required CodeConfig codeConfig}) {
final CodeConfig(:targetTriple, :cCompiler) = codeConfig;
if (cCompiler == null) {
throw UnsupportedError(
Expand Down
13 changes: 3 additions & 10 deletions native_toolchain_rust/lib/src/crate_info_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ interface class CrateInfoValidator {
required String toolchainTomlPath,
required String targetTriple,
}) {
final manifest = cargoManifestParser.parseManifest(
manifestPath,
);
final manifest = cargoManifestParser.parseManifest(manifestPath);

final [
String? libName,
Expand All @@ -29,13 +27,8 @@ interface class CrateInfoValidator {
] = RustValidationException.compose<dynamic>([
() => manifest.libName,
() {
final CargoManifest(
:libName,
:crateName,
:libCrateTypes,
) = cargoManifestParser.parseManifest(
manifestPath,
);
final CargoManifest(:libName, :crateName, :libCrateTypes) =
cargoManifestParser.parseManifest(manifestPath);

const requiredTypes = ['staticlib', 'cdylib'];
if (!requiredTypes.every(libCrateTypes.contains)) {
Expand Down
4 changes: 1 addition & 3 deletions native_toolchain_rust/lib/src/toml_parsing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ interface class CargoManifestParser {
final Logger logger;
final TomlDocumentWrapperFactory tomlDocumentFactory;

CargoManifest parseManifest(
String manifestPath,
) {
CargoManifest parseManifest(String manifestPath) {
logger.info('Looking for Cargo.toml');
if (!File(manifestPath).existsSync()) {
throw RustValidationException([
Expand Down
2 changes: 1 addition & 1 deletion native_toolchain_rust/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ environment:
sdk: ^3.9.0

dependencies:
code_assets: ^1.2.1
code_assets: ^2.0.0
hooks: ^2.1.0
logging: ^1.3.0
meta: ^1.17.0
Expand Down
14 changes: 6 additions & 8 deletions native_toolchain_rust/test/crate_info_validator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ void main() {
libCrateTypes: ['staticlib', 'cdylib'],
),
);
when(() => mockToolchainTomlParser.parseToolchainToml(any())).thenReturn((
channel: '1.90.0',
targets: {'aarch64-linux-android'},
));
when(
() => mockToolchainTomlParser.parseToolchainToml(any()),
).thenReturn((channel: '1.90.0', targets: {'aarch64-linux-android'}));

final result = validator.fetchAndValidateCrateInfo(
manifestPath: 'dummy_manifest_path',
Expand All @@ -52,10 +51,9 @@ void main() {
libCrateTypes: ['staticlib'],
),
);
when(() => mockToolchainTomlParser.parseToolchainToml(any())).thenReturn((
channel: 'stable',
targets: {'x86_64-linux-gnu'},
));
when(
() => mockToolchainTomlParser.parseToolchainToml(any()),
).thenReturn((channel: 'stable', targets: {'x86_64-linux-gnu'}));

expect(
() => validator.fetchAndValidateCrateInfo(
Expand Down
23 changes: 10 additions & 13 deletions native_toolchain_rust/test/crate_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,16 @@ void main() {
expect(result.path, equals(crateDir.path));
});

test(
'resolveCrateDirectory throws RustValidationException '
'if no directory exists',
() {
expect(
() => resolver.resolveCrateDirectory(
rootPath: tempDir.path,
cratePathOptions: ['non_existent_crate'],
),
throwsA(isA<RustValidationException>()),
);
},
);
test('resolveCrateDirectory throws RustValidationException '
'if no directory exists', () {
expect(
() => resolver.resolveCrateDirectory(
rootPath: tempDir.path,
cratePathOptions: ['non_existent_crate'],
),
throwsA(isA<RustValidationException>()),
);
});

test('resolveCrateDirectory returns the first existing directory', () {
final crateDir1 = Directory(path.join(tempDir.path, 'crate1'));
Expand Down
27 changes: 12 additions & 15 deletions native_toolchain_rust/test/dependency_discoverer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,20 @@ target/debug/librust_lib.a: /path/to/some/other/file.rs
expect(dependencies, isEmpty);
});

test(
'discover does not include the dependency file itself',
() {
// Cargo refreshes the mtime of the dep-info file on every invocation,
// even for no-op rebuilds. Including it in the dependency list causes
// Flutter's build graph to emit "File modified during build. Build
// must be rerun." warnings. See issue #73.
final dependencyFilePath = path.join(tempDir.path, 'test.d');
File(dependencyFilePath).writeAsStringSync(
'target/debug/librust_lib.a: src/lib.rs',
);
test('discover does not include the dependency file itself', () {
// Cargo refreshes the mtime of the dep-info file on every invocation,
// even for no-op rebuilds. Including it in the dependency list causes
// Flutter's build graph to emit "File modified during build. Build
// must be rerun." warnings. See issue #73.
final dependencyFilePath = path.join(tempDir.path, 'test.d');
File(
dependencyFilePath,
).writeAsStringSync('target/debug/librust_lib.a: src/lib.rs');

final dependencies = dependencyDiscoverer.discover(dependencyFilePath);
final dependencies = dependencyDiscoverer.discover(dependencyFilePath);

expect(dependencies, isNot(contains(path.toUri(dependencyFilePath))));
},
);
expect(dependencies, isNot(contains(path.toUri(dependencyFilePath))));
});

test(
'discover throws an exception if the dependency file is not found',
Expand Down
5 changes: 1 addition & 4 deletions native_toolchain_rust/test/exception_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import 'package:test/test.dart';
void main() {
group('RustValidationException', () {
test('compose returns results when no exceptions are thrown', () {
final results = RustValidationException.compose([
() => 1,
() => 2,
]);
final results = RustValidationException.compose([() => 1, () => 2]);
expect(results, equals([1, 2]));
});

Expand Down
Loading
Loading