From 60604a2595c7ce11ddd258ae26f549ac722358f0 Mon Sep 17 00:00:00 2001 From: Moritz Date: Wed, 8 May 2024 10:21:58 +0200 Subject: [PATCH] Refactor add link asset test --- .../test_data/add_asset_link/assets/test.txt | 1 - .../add_asset_link/bin/add_asset_link.dart | 4 +-- .../test_data/add_asset_link/hook/build.dart | 31 +++++++++++++++++++ .../test_data/add_asset_link/hook/link.dart | 10 ++++-- .../add_asset_link/lib/add_asset_link.dart | 5 +++ .../lib/src/add_asset_link.dart | 9 ++++++ .../lib/src/add_asset_link_bindings.dart | 12 +++++++ .../test_data/add_asset_link/pubspec.yaml | 3 ++ .../test_data/add_asset_link/src/native_add.c | 9 ++++++ .../test_data/add_asset_link/src/native_add.h | 13 ++++++++ .../test_data/manifest.yaml | 8 +++-- 11 files changed, 97 insertions(+), 8 deletions(-) delete mode 100644 pkgs/native_assets_builder/test_data/add_asset_link/assets/test.txt create mode 100644 pkgs/native_assets_builder/test_data/add_asset_link/hook/build.dart create mode 100644 pkgs/native_assets_builder/test_data/add_asset_link/lib/add_asset_link.dart create mode 100644 pkgs/native_assets_builder/test_data/add_asset_link/lib/src/add_asset_link.dart create mode 100644 pkgs/native_assets_builder/test_data/add_asset_link/lib/src/add_asset_link_bindings.dart create mode 100644 pkgs/native_assets_builder/test_data/add_asset_link/src/native_add.c create mode 100644 pkgs/native_assets_builder/test_data/add_asset_link/src/native_add.h diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/assets/test.txt b/pkgs/native_assets_builder/test_data/add_asset_link/assets/test.txt deleted file mode 100644 index 74158754f..000000000 --- a/pkgs/native_assets_builder/test_data/add_asset_link/assets/test.txt +++ /dev/null @@ -1 +0,0 @@ -My test text \ No newline at end of file diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/bin/add_asset_link.dart b/pkgs/native_assets_builder/test_data/add_asset_link/bin/add_asset_link.dart index a7b90cc9e..c56edc742 100644 --- a/pkgs/native_assets_builder/test_data/add_asset_link/bin/add_asset_link.dart +++ b/pkgs/native_assets_builder/test_data/add_asset_link/bin/add_asset_link.dart @@ -2,8 +2,8 @@ // 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 'dart:io'; +import 'package:add_asset_link/add_asset_link.dart'; void main(List arguments) { - print(File('assets/test.txt').readAsStringSync()); + print('Hello world: ${MyMath.add(3, 4)}!'); } diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/hook/build.dart b/pkgs/native_assets_builder/test_data/add_asset_link/hook/build.dart new file mode 100644 index 000000000..74908a340 --- /dev/null +++ b/pkgs/native_assets_builder/test_data/add_asset_link/hook/build.dart @@ -0,0 +1,31 @@ +// 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: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 logger = Logger('') + ..level = Level.ALL + ..onRecord.listen((record) { + print('${record.level.name}: ${record.time}: ${record.message}'); + }); + await CBuilder.library( + name: 'add', + assetName: 'dylib_add_build', + sources: [ + 'src/native_add.c', + ], + dartBuildFiles: ['hook/build.dart'], + linkModePreference: LinkModePreference.dynamic, + ).run( + buildConfig: config, + buildOutput: output, + logger: logger, + linkInPackage: 'add_asset_link', + ); + }); +} diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/hook/link.dart b/pkgs/native_assets_builder/test_data/add_asset_link/hook/link.dart index 84a3bbcf6..c5999f9c9 100644 --- a/pkgs/native_assets_builder/test_data/add_asset_link/hook/link.dart +++ b/pkgs/native_assets_builder/test_data/add_asset_link/hook/link.dart @@ -6,12 +6,16 @@ import 'package:native_assets_cli/native_assets_cli.dart'; void main(List arguments) async { await link(arguments, (config, output) async { + final built_dylib = config.assets.first as NativeCodeAsset; output ..addAsset( - DataAsset( + NativeCodeAsset( package: 'add_asset_link', - name: 'test_text_file', - file: config.packageRoot.resolve('assets/test.txt'), + name: 'dylib_add_link', + linkMode: built_dylib.linkMode, + os: built_dylib.os, + architecture: built_dylib.architecture, + file: built_dylib.file, ), ) ..addDependency(config.packageRoot.resolve('hook/link.dart')); diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/lib/add_asset_link.dart b/pkgs/native_assets_builder/test_data/add_asset_link/lib/add_asset_link.dart new file mode 100644 index 000000000..8c0d3965a --- /dev/null +++ b/pkgs/native_assets_builder/test_data/add_asset_link/lib/add_asset_link.dart @@ -0,0 +1,5 @@ +// 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. + +export 'src/add_asset_link.dart'; diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/lib/src/add_asset_link.dart b/pkgs/native_assets_builder/test_data/add_asset_link/lib/src/add_asset_link.dart new file mode 100644 index 000000000..71651a7b4 --- /dev/null +++ b/pkgs/native_assets_builder/test_data/add_asset_link/lib/src/add_asset_link.dart @@ -0,0 +1,9 @@ +// 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 'add_asset_link_bindings.dart' as bindings; + +class MyMath { + static int add(int a, int b) => bindings.add(a, b); +} diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/lib/src/add_asset_link_bindings.dart b/pkgs/native_assets_builder/test_data/add_asset_link/lib/src/add_asset_link_bindings.dart new file mode 100644 index 000000000..1d1d71837 --- /dev/null +++ b/pkgs/native_assets_builder/test_data/add_asset_link/lib/src/add_asset_link_bindings.dart @@ -0,0 +1,12 @@ +// 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 'dart:ffi' as ffi; + +@ffi.Native( + assetId: 'package:add_asset_link/dylib_add_link') +external int add( + int a, + int b, +); diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/pubspec.yaml b/pkgs/native_assets_builder/test_data/add_asset_link/pubspec.yaml index 9ffc4e934..be1090386 100644 --- a/pkgs/native_assets_builder/test_data/add_asset_link/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/add_asset_link/pubspec.yaml @@ -13,6 +13,9 @@ dependencies: meta: ^1.12.0 native_assets_cli: path: ../../../native_assets_cli/ + # native_toolchain_c: ^0.4.0 + native_toolchain_c: + path: ../../../native_toolchain_c/ dev_dependencies: lints: ^3.0.0 diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/src/native_add.c b/pkgs/native_assets_builder/test_data/add_asset_link/src/native_add.c new file mode 100644 index 000000000..570e754fe --- /dev/null +++ b/pkgs/native_assets_builder/test_data/add_asset_link/src/native_add.c @@ -0,0 +1,9 @@ +// 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. + +#include "native_add.h" + +MYLIB_EXPORT int32_t add(int32_t a, int32_t b) { + return a + b; +} diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/src/native_add.h b/pkgs/native_assets_builder/test_data/add_asset_link/src/native_add.h new file mode 100644 index 000000000..275878fe8 --- /dev/null +++ b/pkgs/native_assets_builder/test_data/add_asset_link/src/native_add.h @@ -0,0 +1,13 @@ +// 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. + +#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/manifest.yaml b/pkgs/native_assets_builder/test_data/manifest.yaml index 6ed7566be..60b4a0d85 100644 --- a/pkgs/native_assets_builder/test_data/manifest.yaml +++ b/pkgs/native_assets_builder/test_data/manifest.yaml @@ -69,7 +69,11 @@ - drop_dylib_link/hook/build.dart - drop_dylib_link/bin/drop_dylib_link.dart - add_asset_link/pubspec.yaml -- add_asset_link/assets/test.txt -- add_asset_link/README.md - add_asset_link/hook/link.dart - add_asset_link/bin/add_asset_link.dart +- add_asset_link/lib/add_asset_link.dart +- add_asset_link/lib/src/add_asset_link.dart +- add_asset_link/lib/src/add_asset_link_bindings.dart +- add_asset_link/src/native_add.h +- add_asset_link/src/native_add.c +- add_asset_link/hook/build.dart \ No newline at end of file