diff --git a/pkgs/native_assets_builder/test/build_runner/v1_5_0_test.dart b/pkgs/native_assets_builder/test/build_runner/v1_5_0_test.dart new file mode 100644 index 000000000..114dff39a --- /dev/null +++ b/pkgs/native_assets_builder/test/build_runner/v1_5_0_test.dart @@ -0,0 +1,38 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:test/test.dart'; + +import '../helpers.dart'; +import 'helpers.dart'; + +const Timeout longTimeout = Timeout(Duration(minutes: 5)); + +void main() async { + test('native_add build', timeout: longTimeout, () async { + await inTempDir((tempUri) async { + await copyTestProjects(targetUri: tempUri); + final packageUri = tempUri.resolve('native_add_v1_5_0/'); + + // First, run `pub get`, we need pub to resolve our dependencies. + await runPubGet( + workingDirectory: packageUri, + logger: logger, + ); + + { + final result = await build( + packageUri, + logger, + dartExecutable, + configValidator: validateCodeAssetBuildConfig, + buildAssetTypes: [CodeAsset.type], + buildValidator: validateCodeAssetBuildOutput, + applicationAssetValidator: validateCodeAssetInApplication, + ); + expect(result?.encodedAssets.length, 1); + } + }); + }); +} diff --git a/pkgs/native_assets_builder/test_data/manifest.yaml b/pkgs/native_assets_builder/test_data/manifest.yaml index 9ed41c7c9..e00cc0328 100644 --- a/pkgs/native_assets_builder/test_data/manifest.yaml +++ b/pkgs/native_assets_builder/test_data/manifest.yaml @@ -69,6 +69,15 @@ - native_add/src/native_add.c - native_add/src/native_add.h - native_add/test/native_add_test.dart +- native_add_v1_5_0/ffigen.yaml +- native_add_v1_5_0/hook/build.dart +- native_add_v1_5_0/lib/native_add.dart +- native_add_v1_5_0/lib/src/native_add_bindings_generated.dart +- native_add_v1_5_0/lib/src/native_add.dart +- native_add_v1_5_0/pubspec.yaml +- native_add_v1_5_0/src/native_add.c +- native_add_v1_5_0/src/native_add.h +- native_add_v1_5_0/test/native_add_test.dart - native_dynamic_linking/bin/native_dynamic_linking.dart - native_dynamic_linking/ffigen.yaml - native_dynamic_linking/hook/build.dart diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/ffigen.yaml b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/ffigen.yaml new file mode 100644 index 000000000..b8716bd2d --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/ffigen.yaml @@ -0,0 +1,20 @@ +# Run with `flutter pub run ffigen --config ffigen.yaml`. +name: NativeAddBindings +description: | + Bindings for `src/native_add.h`. + + Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`. +output: "lib/src/native_add_bindings_generated.dart" +headers: + entry-points: + - "src/native_add.h" + include-directives: + - "src/native_add.h" +preamble: | + // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file + // for details. All rights reserved. Use of this source code is governed by a + // BSD-style license that can be found in the LICENSE file. +comments: + style: any + length: full +ffi-native: diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/hook/build.dart b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/hook/build.dart new file mode 100644 index 000000000..320f1d3a8 --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/hook/build.dart @@ -0,0 +1,29 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:logging/logging.dart'; +import 'package:native_assets_cli/native_assets_cli.dart'; +import 'package:native_toolchain_c/native_toolchain_c.dart'; + +void main(List arguments) async { + await build(arguments, (config, output) async { + final packageName = config.packageName; + final cbuilder = CBuilder.library( + name: packageName, + assetName: 'src/${packageName}_bindings_generated.dart', + sources: [ + 'src/$packageName.c', + ], + ); + await cbuilder.run( + config: config, + output: output, + logger: Logger('') + ..level = Level.ALL + ..onRecord.listen((record) { + print('${record.level.name}: ${record.time}: ${record.message}'); + }), + ); + }); +} diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/lib/native_add.dart b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/lib/native_add.dart new file mode 100644 index 000000000..9eda681ab --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/lib/native_add.dart @@ -0,0 +1,5 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +export 'src/native_add.dart'; diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/lib/src/native_add.dart b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/lib/src/native_add.dart new file mode 100644 index 000000000..9c8a43b9e --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/lib/src/native_add.dart @@ -0,0 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'native_add_bindings_generated.dart' as bindings; + +int add(int a, int b) => bindings.add(a, b); diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/lib/src/native_add_bindings_generated.dart b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/lib/src/native_add_bindings_generated.dart new file mode 100644 index 000000000..4d1e803aa --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/lib/src/native_add_bindings_generated.dart @@ -0,0 +1,15 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// AUTO GENERATED FILE, DO NOT EDIT. +// +// Generated by `package:ffigen`. +// ignore_for_file: type=lint +import 'dart:ffi' as ffi; + +@ffi.Native(symbol: 'add') +external int add( + int a, + int b, +); diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/manifest.yaml b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/manifest.yaml new file mode 100644 index 000000000..7f66e87ac --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/manifest.yaml @@ -0,0 +1,9 @@ +- ffigen.yaml +- hook/build.dart +- lib/native_add.dart +- lib/src/native_add_bindings_generated.dart +- lib/src/native_add.dart +- pubspec.yaml +- src/native_add.c +- src/native_add.h +- test/native_add_test.dart diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/pubspec.yaml new file mode 100644 index 000000000..310680f5c --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/pubspec.yaml @@ -0,0 +1,20 @@ +name: native_add +description: Sums two numbers with native code. +version: 0.1.0 + +publish_to: none + +environment: + sdk: '>=3.3.0 <4.0.0' + +dependencies: + logging: ^1.1.1 + native_assets_cli: ^0.9.0 + native_toolchain_c: ^0.6.0 + +dev_dependencies: + ffigen: ^8.0.2 + lints: ^3.0.0 + some_dev_dep: + path: ../some_dev_dep/ + test: ^1.23.1 diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/src/native_add.c b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/src/native_add.c new file mode 100644 index 000000000..cf21076d1 --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/src/native_add.c @@ -0,0 +1,9 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include "native_add.h" + +int32_t add(int32_t a, int32_t b) { + return a + b; +} diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/src/native_add.h b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/src/native_add.h new file mode 100644 index 000000000..5824f98a7 --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/src/native_add.h @@ -0,0 +1,13 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include + +#if _WIN32 +#define MYLIB_EXPORT __declspec(dllexport) +#else +#define MYLIB_EXPORT +#endif + +MYLIB_EXPORT int32_t add(int32_t a, int32_t b); diff --git a/pkgs/native_assets_builder/test_data/native_add_v1_5_0/test/native_add_test.dart b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/test/native_add_test.dart new file mode 100644 index 000000000..7531a6209 --- /dev/null +++ b/pkgs/native_assets_builder/test_data/native_add_v1_5_0/test/native_add_test.dart @@ -0,0 +1,13 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:native_add/native_add.dart'; +import 'package:test/test.dart'; + +void main() { + test('native add test', () { + final result = add(4, 6); + expect(result, equals(10)); + }); +} diff --git a/pkgs/native_assets_cli/lib/src/config.dart b/pkgs/native_assets_cli/lib/src/config.dart index ff1912306..10b19cbb3 100644 --- a/pkgs/native_assets_cli/lib/src/config.dart +++ b/pkgs/native_assets_cli/lib/src/config.dart @@ -268,7 +268,7 @@ sealed class HookOutput { HookOutput(this.json) : version = switch (Version.parse(json.string(_versionKey))) { final Version version => (version.major != latestVersion.major || - version < latestVersion) + version < latestParsableVersion) ? throw FormatException( 'Only compatible versions with $latestVersion are supported ' '(was: $version).') @@ -279,9 +279,6 @@ sealed class HookOutput { @override String toString() => const JsonEncoder.withIndent(' ').convert(json); - - /// The version of [HookOutput]. - static final Version latestVersion = Version(1, 6, 0); } List _parseDependencies(List? list) { @@ -298,7 +295,7 @@ sealed class HookOutputBuilder { final Map json = {}; HookOutputBuilder() { - json[_versionKey] = HookOutput.latestVersion.toString(); + json[_versionKey] = latestVersion.toString(); json[_timestampKey] = DateTime.now().roundDownToSeconds().toString(); json[_dependenciesKey] = []; } diff --git a/pkgs/native_assets_cli/test/build_output_test.dart b/pkgs/native_assets_cli/test/build_output_test.dart index 5c7e78e4f..2b53dc485 100644 --- a/pkgs/native_assets_cli/test/build_output_test.dart +++ b/pkgs/native_assets_cli/test/build_output_test.dart @@ -5,6 +5,7 @@ // ignore_for_file: deprecated_member_use_from_same_package import 'package:native_assets_cli/native_assets_cli_builder.dart'; +import 'package:native_assets_cli/src/config.dart'; import 'package:native_assets_cli/src/utils/datetime.dart'; import 'package:test/test.dart'; @@ -79,7 +80,7 @@ void main() { (e) => e is FormatException && e.message.contains(version) && - e.message.contains(HookOutput.latestVersion.toString()), + e.message.contains(latestVersion.toString()), )), ); }); diff --git a/pkgs/native_assets_cli/test/link_output_test.dart b/pkgs/native_assets_cli/test/link_output_test.dart index 5d3a7d74d..8d87b4aa7 100644 --- a/pkgs/native_assets_cli/test/link_output_test.dart +++ b/pkgs/native_assets_cli/test/link_output_test.dart @@ -3,6 +3,7 @@ // BSD-style license that can be found in the LICENSE file. import 'package:native_assets_cli/native_assets_cli_builder.dart'; +import 'package:native_assets_cli/src/config.dart'; import 'package:native_assets_cli/src/utils/datetime.dart'; import 'package:test/test.dart'; @@ -55,7 +56,7 @@ void main() { (e) => e is FormatException && e.message.contains(version) && - e.message.contains(HookOutput.latestVersion.toString()), + e.message.contains(latestVersion.toString()), )), ); });