From adbdd0f82238d9a58e4a1767708464e4d8c15b6b Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 18 Jan 2024 13:58:03 +0100 Subject: [PATCH 01/75] [native_assets_cli] Redesign API --- .../lib/native_assets_cli.dart | 1 + .../lib/src/api/build_config.dart | 24 +++- .../lib/src/api/build_output.dart | 90 +++++++++++++-- .../lib/src/api/build_state.dart | 106 ++++++++++++++++++ .../lib/src/api/dependencies.dart | 1 + .../lib/src/api/metadata.dart | 1 + .../lib/src/model/build_config.dart | 6 + .../lib/src/model/build_output.dart | 30 ++++- .../lib/src/model/dependencies.dart | 1 + .../lib/src/model/metadata.dart | 1 + .../test/api/build_config_test.dart | 21 ++-- .../test/api/build_output_test.dart | 8 +- 12 files changed, 258 insertions(+), 32 deletions(-) create mode 100644 pkgs/native_assets_cli/lib/src/api/build_state.dart diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index b46aa7031..aa0848d20 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -10,6 +10,7 @@ export 'src/api/asset.dart'; export 'src/api/build_config.dart'; export 'src/api/build_mode.dart'; export 'src/api/build_output.dart'; +export 'src/api/build_state.dart'; export 'src/api/dependencies.dart'; export 'src/api/ios_sdk.dart'; export 'src/api/link_mode.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index b9de44e64..0b0e8a41a 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -11,6 +11,7 @@ import '../model/build_config.dart' as model; import '../model/build_mode.dart' as model; import '../model/ios_sdk.dart' as model; import '../model/link_mode_preference.dart' as model; +import '../model/metadata.dart' as model; import '../model/target.dart' as model; import 'build_mode.dart'; import 'ios_sdk.dart'; @@ -18,6 +19,10 @@ import 'link_mode_preference.dart'; import 'metadata.dart'; import 'target.dart'; +/// The configuration for a `build.dart` invocation. +/// +/// The Flutter and Dart SDK invoke `build.dart` with commandline arguments +/// that can be parsed by this class. abstract class BuildConfig { /// The folder in which all output and intermediate artifacts should be /// placed. @@ -74,8 +79,16 @@ abstract class BuildConfig { /// The key in the nested map is the key for the metadata from the dependency. /// /// Not available in [dryRun]. + @Deprecated('Use getMetadata.') Map? get dependencyMetadata; + /// Get the metadata from a direct dependency. + /// + /// The [packageName] of is the package name of the direct dependency. + /// + /// Not available in [dryRun]. + T? getMetadata(String packageName, String key); + /// The configuration for invoking the C compiler. /// /// Not available in [dryRun]. @@ -90,8 +103,6 @@ abstract class BuildConfig { BuildMode get buildMode; /// The underlying config. - /// - /// Can be used for easier access to values on [dependencyMetadata]. Config get config; /// The version of [BuildConfig]. @@ -115,7 +126,9 @@ abstract class BuildConfig { int? targetAndroidNdkApi, CCompilerConfig? cCompiler, required LinkModePreference linkModePreference, + @Deprecated('Use dependencyMetadata2.') Map? dependencyMetadata, + Map>? dependencyMetadata2, }) => model.BuildConfig( outDir: outDir, @@ -128,7 +141,12 @@ abstract class BuildConfig { targetAndroidNdkApi: targetAndroidNdkApi, cCompiler: cCompiler as model.CCompilerConfig?, linkModePreference: linkModePreference as model.LinkModePreference, - dependencyMetadata: dependencyMetadata?.cast(), + dependencyMetadata: dependencyMetadata2 != null + ? { + for (final entry in dependencyMetadata2.entries) + entry.key: model.Metadata(entry.value.cast()) + } + : dependencyMetadata?.cast(), ); factory BuildConfig.dryRun({ diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 8c93ffb11..c87a518c8 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -13,30 +13,99 @@ import '../model/metadata.dart' as model; import 'asset.dart'; import 'dependencies.dart'; import 'metadata.dart'; +import 'target.dart'; +/// The output of a `build.dart` invocation. +/// +/// The Dart and Flutter SDK consume this data. abstract class BuildOutput { - /// Time the build this output belongs to started. + /// The [timestamp] indicates the time the build this output belongs to + /// started. /// - /// Rounded down to whole seconds, because [File.lastModified] is rounded - /// to whole seconds and caching logic compares these timestamps. + /// The [timestamp] is rounded down to whole seconds, because + /// [File.lastModified] is rounded to whole seconds and caching logic compares + /// these timestamps. DateTime get timestamp; + + @Deprecated('Use assets2') List get assets; + + /// The assets produced by this build. + /// + /// In dry runs, the assets for all [Architecture]s for the [OS] specified in + /// the dry run must be provided. + // TODO: Rename to `assets` after removing old one. + Iterable get assets2; + + @Deprecated('Use dependencies2') Dependencies get dependencies; - Metadata get metadata; + /// The files used by this build. + /// + /// If any of the files in [dependencies2] is modified after [timestamp], the + /// build will be re-run. + // TODO: Rename to `dependencies` after removing old one. + Iterable get dependencies2; + + /// Metadata can to be passed to `build.dart` invocations of dependent + /// packages. + // TODO(dacoharkes): Then we also need to make the accessors and setters + // in BuildConfig methods. + T getMetadata(String key); + + /// Create a build output. + /// + /// The [timestamp] indicates the time the build this output belongs to + /// started. If omitted, [timestamp] defaults to the time the build started. + /// The [timestamp] is rounded down to whole seconds, because + /// [File.lastModified] is rounded to whole seconds and caching logic compares + /// these timestamps. + /// + /// The [Asset]s produced by this build or dry run can be passed in [assets] + /// or [addAssets]. In dry runs, the assets for all [Architecture]s for the + /// [OS] specified in the dry run must be provided. + /// + /// The files used by this build must be passed in [dependencies2] or + /// [addDependencies]. If any of these files is modified after [timestamp], + /// the build will be re-run. + /// + /// Metadata can be passed to `build.dart` invocations of dependent packages + /// via [metadata2] or [addMetadata]. factory BuildOutput({ DateTime? timestamp, - List? assets, - Dependencies? dependencies, - Metadata? metadata, + Iterable? assets, + @Deprecated('Use addDependencies.') Dependencies? dependencies, + Iterable? dependencies2, + @Deprecated('Use addMetadata.') Metadata? metadata, + Map? metadata2, }) => model.BuildOutput( timestamp: timestamp, - assets: assets?.map((e) => e as model.Asset).toList(), - dependencies: dependencies as model.Dependencies?, - metadata: metadata as model.Metadata?, + assets: assets?.cast().toList(), + dependencies: dependencies2 != null + ? model.Dependencies(dependencies2.toList()) + : dependencies as model.Dependencies?, + metadata: metadata2 != null + ? model.Metadata(metadata2) + : metadata as model.Metadata?, ); + /// Add [Asset]s produced by this build or dry run. + /// + /// In dry runs, the assets for all [Architecture]s for the [OS] specified in + /// the dry run must be provided. + void addAssets(Iterable assets); + + /// Add files used by this build. + /// + /// If any of the files is modified after [timestamp], the build will be + /// re-run. + void addDependencies(Iterable dependencies); + + /// Add metadata to be passed to `build.dart` invocations of dependent + /// packages. + void addMetadata(String key, Object value); + /// The version of [BuildOutput]. /// /// This class is used in the protocol between the Dart and Flutter SDKs @@ -47,5 +116,6 @@ abstract class BuildOutput { /// representation in the protocol. static Version get version => model.BuildOutput.version; + /// Write out this build output to a file inside [outDir]. Future writeToFile({required Uri outDir}); } diff --git a/pkgs/native_assets_cli/lib/src/api/build_state.dart b/pkgs/native_assets_cli/lib/src/api/build_state.dart new file mode 100644 index 000000000..23ac3dd1c --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/api/build_state.dart @@ -0,0 +1,106 @@ +// 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 'asset.dart'; +import 'build_config.dart'; +import 'build_output.dart'; + +final class BuildState { + /// Run a native assets build. + /// + /// Example using `package:native_toolchain_c`: + /// + /// ```dart + /// void main(List args) async => + /// await BuildState.build(args, (buildState) async { + /// final cbuilder = CBuilder.library( + /// name: packageName, + /// assetId: 'package:$packageName/${packageName}.dart', + /// sources: [ + /// 'src/$packageName.c', + /// ], + /// ); + /// await cbuilder.run( + /// buildState: buildState, + /// logger: Logger('') + /// ..level = Level.ALL + /// ..onRecord.listen((record) => print(record.message)), + /// ); + /// }); + /// ``` + /// + /// Example outputting assets manually: + /// + /// ```dart + /// void main(List args) async => + /// await BuildState.build(args, (buildState) async { + /// final config = buildState.config; + /// if (config.linkModePreference == LinkModePreference.static) { + /// // Simulat that this script only supports dynamic libraries. + /// throw Exception('LinkModePreference.static is not supported.'); + /// } + /// + /// final Iterable targets; + /// final packageName = config.packageName; + /// final assetPath = config.outDir.resolve( + /// config.targetOs.dylibFileName(packageName), + /// ); + /// if (config.dryRun) { + /// // Dry run invocations report assets for all architectures for that OS. + /// targets = Target.values.where( + /// (element) => element.os == config.targetOs, + /// ); + /// } else { + /// targets = [config.target]; + /// + /// // Insert code that downloads or builds the asset to `assetPath`. + /// } + /// + /// for (final target in targets) { + /// buildState.output.addAssets([ + /// Asset( + /// id: 'library:${packageName}/src/some_file.dart', + /// linkMode: LinkMode.dynamic, + /// target: target, + /// path: AssetAbsolutePath(assetPath), + /// ) + /// ]); + /// } + /// }); + /// ``` + static Future build( + List args, + Future Function(BuildState) callback, + ) async { + final config = await BuildConfig.fromArgs(args); + final output = BuildOutput(); + final state = BuildState._(config: config, output: output); + await callback(state); + await output.writeToFile(outDir: config.outDir); + } + + final BuildConfig config; + final BuildOutput output; + + BuildState._({ + required this.config, + required this.output, + }); + + /// Add assets to build output. + /// + /// See [BuildOutput.addAssets] for more info. + void addAssets(Iterable assets) => output.addAssets(assets); + + /// Add dependencies to build output. + /// + /// See [BuildOutput.addDependencies] for more info. + void addDependencies(Iterable dependencies) => + output.addDependencies(dependencies); + + /// Add metadata to build output. + /// + /// See [BuildOutput.addMetadata] for more info. + void addMetadata(String key, Object value) => output.addMetadata(key, value); +} diff --git a/pkgs/native_assets_cli/lib/src/api/dependencies.dart b/pkgs/native_assets_cli/lib/src/api/dependencies.dart index 5c8763b91..f79d0c13d 100644 --- a/pkgs/native_assets_cli/lib/src/api/dependencies.dart +++ b/pkgs/native_assets_cli/lib/src/api/dependencies.dart @@ -4,6 +4,7 @@ import '../model/dependencies.dart' as model; +@Deprecated('Use the methods on BuildOutput.') abstract class Dependencies { /// The dependencies a build relied on. List get dependencies; diff --git a/pkgs/native_assets_cli/lib/src/api/metadata.dart b/pkgs/native_assets_cli/lib/src/api/metadata.dart index 234e653a0..efa8d7ff5 100644 --- a/pkgs/native_assets_cli/lib/src/api/metadata.dart +++ b/pkgs/native_assets_cli/lib/src/api/metadata.dart @@ -4,6 +4,7 @@ import '../model/metadata.dart' as model; +@Deprecated('Use the methods on BuildConfig and BuildOutput.') abstract class Metadata { Map get metadata; diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index a5019d24f..c65ffc822 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -111,6 +111,12 @@ class BuildConfig implements api.BuildConfig { return _dependencyMetadata; } + @override + T? getMetadata(String packageName, String key) { + _ensureNotDryRun(); + return _dependencyMetadata?[packageName]?.metadata[key] as T?; + } + late final Map? _dependencyMetadata; /// The configuration for invoking the C compiler. diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index ccf3795a2..fe5fcdc93 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -8,6 +8,7 @@ import 'package:collection/collection.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:yaml/yaml.dart'; +import '../api/asset.dart' as api; import '../api/build_output.dart' as api; import '../utils/datetime.dart'; import '../utils/file.dart'; @@ -18,17 +19,21 @@ import 'dependencies.dart'; import 'metadata.dart'; class BuildOutput implements api.BuildOutput { - /// Time the build this output belongs to started. - /// - /// Rounded down to whole seconds, because [File.lastModified] is rounded - /// to whole seconds and caching logic compares these timestamps. @override final DateTime timestamp; + @override final List assets; + + @override + Iterable get assets2 => assets; + @override final Dependencies dependencies; + @override + Iterable get dependencies2 => dependencies.dependencies; + final Metadata metadata; BuildOutput({ @@ -43,6 +48,10 @@ class BuildOutput implements api.BuildOutput { // ignore: prefer_const_constructors metadata = metadata ?? Metadata({}); + @override + void addDependencies(Iterable dependencies) => + this.dependencies.dependencies.addAll(dependencies); + static const _assetsKey = 'assets'; static const _dependenciesKey = 'dependencies'; static const _metadataKey = 'metadata'; @@ -141,4 +150,17 @@ class BuildOutput implements api.BuildOutput { dependencies, metadata, ); + + @override + void addMetadata(String key, Object value) { + metadata.metadata[key] = value; + } + + @override + T getMetadata(String key) => metadata.metadata[key] as T; + + @override + void addAssets(Iterable assets) { + this.assets.addAll(assets.cast()); + } } diff --git a/pkgs/native_assets_cli/lib/src/model/dependencies.dart b/pkgs/native_assets_cli/lib/src/model/dependencies.dart index 8cc801a41..6b42a8104 100644 --- a/pkgs/native_assets_cli/lib/src/model/dependencies.dart +++ b/pkgs/native_assets_cli/lib/src/model/dependencies.dart @@ -10,6 +10,7 @@ import '../utils/file.dart'; import '../utils/uri.dart'; import '../utils/yaml.dart'; +// ignore: deprecated_member_use_from_same_package class Dependencies implements api.Dependencies { /// The dependencies a build relied on. @override diff --git a/pkgs/native_assets_cli/lib/src/model/metadata.dart b/pkgs/native_assets_cli/lib/src/model/metadata.dart index db68330d6..f31df0dca 100644 --- a/pkgs/native_assets_cli/lib/src/model/metadata.dart +++ b/pkgs/native_assets_cli/lib/src/model/metadata.dart @@ -9,6 +9,7 @@ import '../api/metadata.dart' as api; import '../utils/map.dart'; import '../utils/yaml.dart'; +// ignore: deprecated_member_use_from_same_package class Metadata implements api.Metadata { @override final Map metadata; diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 93ace7cb5..a44569547 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -89,7 +89,6 @@ void main() async { true); expect(config1.cCompiler != config2.cCompiler, true); expect(config1.linkModePreference, config2.linkModePreference); - expect(config1.dependencyMetadata, config2.dependencyMetadata); }); test('BuildConfig fromConfig', () { @@ -154,14 +153,14 @@ void main() async { targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, - dependencyMetadata: { - 'bar': const Metadata({ + dependencyMetadata2: { + 'bar': { 'key': 'value', 'foo': ['asdf', 'fdsa'], - }), - 'foo': const Metadata({ + }, + 'foo': { 'key': 321, - }), + }, }, ); @@ -174,13 +173,13 @@ void main() async { targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, - dependencyMetadata: { - 'bar': const Metadata({ + dependencyMetadata2: { + 'bar': { 'key': 'value', - }), - 'foo': const Metadata({ + }, + 'foo': { 'key': 123, - }), + }, }, ); diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index 48a2644b7..9ae10ad67 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -35,12 +35,12 @@ void main() { linkMode: LinkMode.dynamic, ), ], - dependencies: Dependencies([ + dependencies2: [ Uri.file('path/to/file.ext'), - ]), - metadata: const Metadata({ + ], + metadata2: { 'key': 'value', - }), + }, ); }); } From 653397262eb54b573befc358e81f6512781ed9f2 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 22 Jan 2024 14:12:52 +0100 Subject: [PATCH 02/75] Surpress deprecation warnings to appease CI --- .../test_data/package_reading_metadata/build.dart | 4 ++++ .../test_data/package_with_metadata/build.dart | 4 ++++ pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart | 4 ++++ pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart index e8009dbd9..03eac1d0d 100644 --- a/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart +++ b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart @@ -2,6 +2,10 @@ // 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. +// TODO(https://github.com/dart-lang/native/issues/882): A new version of +// `package:native_assets_cli` needs to be published first. +// ignore_for_file: deprecated_member_use + import 'package:cli_config/cli_config.dart'; import 'package:native_assets_cli/native_assets_cli.dart'; diff --git a/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart b/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart index a5e908ac7..7fb8b22bf 100644 --- a/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart +++ b/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart @@ -2,6 +2,10 @@ // 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. +// TODO(https://github.com/dart-lang/native/issues/882): A new version of +// `package:native_assets_cli` needs to be published first. +// ignore_for_file: deprecated_member_use + import 'package:native_assets_cli/native_assets_cli.dart'; void main(List args) async { diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index a0a199b0d..a5fafab72 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -2,6 +2,10 @@ // 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. +// TODO(https://github.com/dart-lang/native/issues/882): A new version of +// `package:native_assets_cli` needs to be published first. +// ignore_for_file: deprecated_member_use + import 'dart:io'; import 'package:logging/logging.dart'; diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart index 53b5a965b..56015b5cb 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart @@ -2,6 +2,10 @@ // 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. +// TODO(https://github.com/dart-lang/native/issues/882): A new version of +// `package:native_assets_cli` needs to be published first. +// ignore_for_file: deprecated_member_use + @OnPlatform({ 'mac-os': Timeout.factor(2), 'windows': Timeout.factor(10), From cbdbc1e4abb25345347617fa40ba28385d0f668c Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 22 Jan 2024 14:24:59 +0100 Subject: [PATCH 03/75] changelog entries --- pkgs/native_assets_cli/CHANGELOG.md | 4 ++++ pkgs/native_assets_cli/lib/src/api/build_output.dart | 3 +++ pkgs/native_assets_cli/lib/src/model/build_output.dart | 1 + pkgs/native_assets_cli/pubspec.yaml | 2 +- pkgs/native_toolchain_c/CHANGELOG.md | 1 + 5 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pkgs/native_assets_cli/CHANGELOG.md b/pkgs/native_assets_cli/CHANGELOG.md index 402b91647..53f85b125 100644 --- a/pkgs/native_assets_cli/CHANGELOG.md +++ b/pkgs/native_assets_cli/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.4.2-wip + +- New API. + ## 0.4.1 - **Breaking change** Removed all code not used in `build.dart` scripts out of diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index c87a518c8..faa54d02d 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -47,6 +47,9 @@ abstract class BuildOutput { // TODO: Rename to `dependencies` after removing old one. Iterable get dependencies2; + @Deprecated('Use getMetadata') + Metadata get metadata; + /// Metadata can to be passed to `build.dart` invocations of dependent /// packages. // TODO(dacoharkes): Then we also need to make the accessors and setters diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index fe5fcdc93..13995a13c 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -34,6 +34,7 @@ class BuildOutput implements api.BuildOutput { @override Iterable get dependencies2 => dependencies.dependencies; + @override final Metadata metadata; BuildOutput({ diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index a367c44d8..216e41c7d 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -4,7 +4,7 @@ description: >- native assets CLI. # Note: Bump BuildConfig.version and BuildOutput.version on breaking changes! -version: 0.4.1 +version: 0.4.2-wip repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli topics: diff --git a/pkgs/native_toolchain_c/CHANGELOG.md b/pkgs/native_toolchain_c/CHANGELOG.md index a04137bde..537c8b960 100644 --- a/pkgs/native_toolchain_c/CHANGELOG.md +++ b/pkgs/native_toolchain_c/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.3.5-wip - Bump `package:native_assets_cli` to 0.4.1. +- Temporary deprecation lint suppresion for `package:native_assets_cli`. ## 0.3.4+1 From 499feaeac11cd0f37cf43ce9bc2f805603047545 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 22 Jan 2024 21:40:51 +0100 Subject: [PATCH 04/75] Address comments --- .../lib/src/api/build_config.dart | 31 +++++++------ .../lib/src/api/build_output.dart | 43 ++++++++++--------- .../lib/src/api/build_state.dart | 30 +++++++------ .../lib/src/model/build_config.dart | 18 ++++---- .../lib/src/model/build_output.dart | 2 +- 5 files changed, 68 insertions(+), 56 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 0b0e8a41a..fabbb6876 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -21,8 +21,10 @@ import 'target.dart'; /// The configuration for a `build.dart` invocation. /// -/// The Flutter and Dart SDK invoke `build.dart` with commandline arguments -/// that can be parsed by this class. +/// A package can choose to have a toplevel `build.dart` script. If such a +/// script exists, it will be automatically run, by the Flutter and Dart SDK +/// tools. The script will then be run with specific commandline arguments, +/// which [BuildConfig] can parse and provide more convenient access to. abstract class BuildConfig { /// The folder in which all output and intermediate artifacts should be /// placed. @@ -39,12 +41,12 @@ abstract class BuildConfig { /// The target being compiled for. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. Target get target; /// The architecture being compiled for. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. Architecture get targetArchitecture; /// The operating system being compiled for. @@ -54,7 +56,7 @@ abstract class BuildConfig { /// /// Required when [targetOs] equals [OS.iOS]. /// - /// Not available in [dryRun].s + /// Not available during a [dryRun]. IOSSdk? get targetIOSSdk; /// When compiling for Android, the minimum Android SDK API version to that @@ -62,7 +64,7 @@ abstract class BuildConfig { /// /// Required when [targetOs] equals [OS.android]. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. /// /// For more information about the Android API version, refer to /// [`minSdkVersion`](https://developer.android.com/ndk/guides/sdk-versions#minsdkversion) @@ -78,7 +80,7 @@ abstract class BuildConfig { /// /// The key in the nested map is the key for the metadata from the dependency. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. @Deprecated('Use getMetadata.') Map? get dependencyMetadata; @@ -86,20 +88,25 @@ abstract class BuildConfig { /// /// The [packageName] of is the package name of the direct dependency. /// - /// Not available in [dryRun]. - T? getMetadata(String packageName, String key); + /// Returns `null` if metadata was not provided. + /// + /// Not available during a [dryRun]. + Object? metadata(String packageName, String key); /// The configuration for invoking the C compiler. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. CCompilerConfig get cCompiler; - /// Don't run the build, only report the native assets produced. + /// Whether the current run is a "dry run". + /// + /// If so, the build won't actually be run, but will report the native assets + /// which would have been produced. bool get dryRun; /// The build mode that the code should be compiled in. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. BuildMode get buildMode; /// The underlying config. diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index faa54d02d..7f726ec87 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -17,10 +17,12 @@ import 'target.dart'; /// The output of a `build.dart` invocation. /// -/// The Dart and Flutter SDK consume this data. +/// A package can choose to have a toplevel `build.dart` script. If such a +/// script exists, it will be automatically run, by the Flutter and Dart SDK +/// tools. The script is expect to produce a specific output which [BuildOutput] +/// can produce. abstract class BuildOutput { - /// The [timestamp] indicates the time the build this output belongs to - /// started. + // Start time for the build of this output. /// /// The [timestamp] is rounded down to whole seconds, because /// [File.lastModified] is rounded to whole seconds and caching logic compares @@ -42,7 +44,7 @@ abstract class BuildOutput { /// The files used by this build. /// - /// If any of the files in [dependencies2] is modified after [timestamp], the + /// If any of the files in [dependencies2] are modified after [timestamp], the /// build will be re-run. // TODO: Rename to `dependencies` after removing old one. Iterable get dependencies2; @@ -52,24 +54,25 @@ abstract class BuildOutput { /// Metadata can to be passed to `build.dart` invocations of dependent /// packages. - // TODO(dacoharkes): Then we also need to make the accessors and setters - // in BuildConfig methods. - T getMetadata(String key); + // TODO(dacoharkes): Rename to metadata. + Object? metadata2(String key); /// Create a build output. /// - /// The [timestamp] indicates the time the build this output belongs to - /// started. If omitted, [timestamp] defaults to the time the build started. - /// The [timestamp] is rounded down to whole seconds, because - /// [File.lastModified] is rounded to whole seconds and caching logic compares - /// these timestamps. + /// The [timestamp] must be before any any [dependencies2] are read by the + /// build this output belongs to. If the [BuildOutput] object is created at + /// the beginning of the `build.dart` script, it can be omitted and will + /// default to [DateTime.now]. The [timestamp] is rounded down to whole + /// seconds, because [File.lastModified] is rounded to whole seconds and + /// caching logic compares these timestamps. /// - /// The [Asset]s produced by this build or dry run can be passed in [assets] - /// or [addAssets]. In dry runs, the assets for all [Architecture]s for the - /// [OS] specified in the dry run must be provided. + /// The [Asset]s produced by this build or dry-run can be provided to the + /// constructor as [assets], or can be added later using [addAssets]. In dry + /// runs, the assets for all [Architecture]s for the [OS] specified in the dry + /// run must be provided. /// /// The files used by this build must be passed in [dependencies2] or - /// [addDependencies]. If any of these files is modified after [timestamp], + /// [addDependencies]. If any of these files are modified after [timestamp], /// the build will be re-run. /// /// Metadata can be passed to `build.dart` invocations of dependent packages @@ -93,19 +96,19 @@ abstract class BuildOutput { : metadata as model.Metadata?, ); - /// Add [Asset]s produced by this build or dry run. + /// Adds [Asset]s produced by this build or dry run. /// /// In dry runs, the assets for all [Architecture]s for the [OS] specified in /// the dry run must be provided. void addAssets(Iterable assets); - /// Add files used by this build. + /// Adds files used by this build. /// - /// If any of the files is modified after [timestamp], the build will be + /// If any of the files are modified after [timestamp], the build will be /// re-run. void addDependencies(Iterable dependencies); - /// Add metadata to be passed to `build.dart` invocations of dependent + /// Adds metadata to be passed to `build.dart` invocations of dependent /// packages. void addMetadata(String key, Object value); diff --git a/pkgs/native_assets_cli/lib/src/api/build_state.dart b/pkgs/native_assets_cli/lib/src/api/build_state.dart index 23ac3dd1c..ba295cae6 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_state.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_state.dart @@ -12,11 +12,11 @@ final class BuildState { /// Example using `package:native_toolchain_c`: /// /// ```dart - /// void main(List args) async => - /// await BuildState.build(args, (buildState) async { + /// void main(List args) => + /// BuildState.build(args, (buildState) async { /// final cbuilder = CBuilder.library( /// name: packageName, - /// assetId: 'package:$packageName/${packageName}.dart', + /// assetId: 'package:$packageName/$packageName.dart', /// sources: [ /// 'src/$packageName.c', /// ], @@ -33,12 +33,14 @@ final class BuildState { /// Example outputting assets manually: /// /// ```dart - /// void main(List args) async => - /// await BuildState.build(args, (buildState) async { + /// void main(List args) => + /// BuildState.build(args, (buildState) async { /// final config = buildState.config; /// if (config.linkModePreference == LinkModePreference.static) { - /// // Simulat that this script only supports dynamic libraries. - /// throw Exception('LinkModePreference.static is not supported.'); + /// // Simulate that this script only supports dynamic libraries. + /// throw UnsupportedError( + /// 'LinkModePreference.static is not supported.', + /// ); /// } /// /// final Iterable targets; @@ -70,13 +72,13 @@ final class BuildState { /// }); /// ``` static Future build( - List args, - Future Function(BuildState) callback, + List commandlineArguments, + Future Function(BuildState) builder, ) async { - final config = await BuildConfig.fromArgs(args); + final config = await BuildConfig.fromArgs(commandlineArguments); final output = BuildOutput(); final state = BuildState._(config: config, output: output); - await callback(state); + await builder(state); await output.writeToFile(outDir: config.outDir); } @@ -88,18 +90,18 @@ final class BuildState { required this.output, }); - /// Add assets to build output. + /// Adds assets to build output. /// /// See [BuildOutput.addAssets] for more info. void addAssets(Iterable assets) => output.addAssets(assets); - /// Add dependencies to build output. + /// Adds dependencies to build output. /// /// See [BuildOutput.addDependencies] for more info. void addDependencies(Iterable dependencies) => output.addDependencies(dependencies); - /// Add metadata to build output. + /// Adds metadata to build output. /// /// See [BuildOutput.addMetadata] for more info. void addMetadata(String key, Object value) => output.addMetadata(key, value); diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index c65ffc822..842db9afb 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -41,14 +41,14 @@ class BuildConfig implements api.BuildConfig { /// The target being compiled for. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. @override late final Target target = Target.fromArchitectureAndOs(targetArchitecture, targetOs); /// The architecture being compiled for. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. @override Architecture get targetArchitecture { _ensureNotDryRun(); @@ -66,7 +66,7 @@ class BuildConfig implements api.BuildConfig { /// /// Required when [targetOs] equals [OS.iOS]. /// - /// Not available in [dryRun].s + /// Not available during a [dryRun]. @override IOSSdk? get targetIOSSdk { _ensureNotDryRun(); @@ -80,7 +80,7 @@ class BuildConfig implements api.BuildConfig { /// /// Required when [targetOs] equals [OS.android]. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. /// /// For more information about the Android API version, refer to /// [`minSdkVersion`](https://developer.android.com/ndk/guides/sdk-versions#minsdkversion) @@ -104,7 +104,7 @@ class BuildConfig implements api.BuildConfig { /// /// The key in the nested map is the key for the metadata from the dependency. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. @override Map? get dependencyMetadata { _ensureNotDryRun(); @@ -112,16 +112,16 @@ class BuildConfig implements api.BuildConfig { } @override - T? getMetadata(String packageName, String key) { + Object? metadata(String packageName, String key) { _ensureNotDryRun(); - return _dependencyMetadata?[packageName]?.metadata[key] as T?; + return _dependencyMetadata?[packageName]?.metadata[key]; } late final Map? _dependencyMetadata; /// The configuration for invoking the C compiler. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. @override CCompilerConfig get cCompiler { _ensureNotDryRun(); @@ -137,7 +137,7 @@ class BuildConfig implements api.BuildConfig { /// The build mode that the code should be compiled in. /// - /// Not available in [dryRun]. + /// Not available during a [dryRun]. @override BuildMode get buildMode { _ensureNotDryRun(); diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index 13995a13c..3be0251b8 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -158,7 +158,7 @@ class BuildOutput implements api.BuildOutput { } @override - T getMetadata(String key) => metadata.metadata[key] as T; + Object? metadata2(String key) => metadata.metadata[key]; @override void addAssets(Iterable assets) { From 027fece6dd21f47901013c549e823f6409fd6880 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 23 Jan 2024 12:29:00 +0100 Subject: [PATCH 05/75] Remove `BuildState` --- .../lib/native_assets_cli.dart | 2 +- pkgs/native_assets_cli/lib/src/api/build.dart | 80 +++++++++++++ .../lib/src/api/build_state.dart | 108 ------------------ 3 files changed, 81 insertions(+), 109 deletions(-) create mode 100644 pkgs/native_assets_cli/lib/src/api/build.dart delete mode 100644 pkgs/native_assets_cli/lib/src/api/build_state.dart diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index aa0848d20..331c27b68 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -7,10 +7,10 @@ library native_assets_cli; export 'src/api/asset.dart'; +export 'src/api/build.dart'; export 'src/api/build_config.dart'; export 'src/api/build_mode.dart'; export 'src/api/build_output.dart'; -export 'src/api/build_state.dart'; export 'src/api/dependencies.dart'; export 'src/api/ios_sdk.dart'; export 'src/api/link_mode.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart new file mode 100644 index 000000000..6ee951660 --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -0,0 +1,80 @@ +// 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 'build_config.dart'; +import 'build_output.dart'; + +/// Run a native assets build. +/// +/// Example using `package:native_toolchain_c`: +/// +/// ```dart +/// void main(List args) => +/// build(args, (buildConfig, buildOutput) async { +/// final cbuilder = CBuilder.library( +/// name: packageName, +/// assetId: 'package:$packageName/$packageName.dart', +/// sources: [ +/// 'src/$packageName.c', +/// ], +/// ); +/// await cbuilder.run( +/// buildConfig: buildConfig, +/// buildOutput: buildOutput, +/// logger: Logger('') +/// ..level = Level.ALL +/// ..onRecord.listen((record) => print(record.message)), +/// ); +/// }); +/// ``` +/// +/// Example outputting assets manually: +/// +/// ```dart +/// void main(List args) => +/// build(args, (config, output) async { +/// if (config.linkModePreference == LinkModePreference.static) { +/// // Simulate that this script only supports dynamic libraries. +/// throw UnsupportedError( +/// 'LinkModePreference.static is not supported.', +/// ); +/// } +/// +/// final Iterable targets; +/// final packageName = config.packageName; +/// final assetPath = config.outDir.resolve( +/// config.targetOs.dylibFileName(packageName), +/// ); +/// if (config.dryRun) { +/// // Dry run invocations report assets for all architectures for that OS. +/// targets = Target.values.where( +/// (element) => element.os == config.targetOs, +/// ); +/// } else { +/// targets = [config.target]; +/// +/// // Insert code that downloads or builds the asset to `assetPath`. +/// } +/// +/// for (final target in targets) { +/// output.addAssets([ +/// Asset( +/// id: 'library:${packageName}/src/some_file.dart', +/// linkMode: LinkMode.dynamic, +/// target: target, +/// path: AssetAbsolutePath(assetPath), +/// ) +/// ]); +/// } +/// }); +/// ``` +Future build( + List commandlineArguments, + Future Function(BuildConfig, BuildOutput) builder, +) async { + final config = await BuildConfig.fromArgs(commandlineArguments); + final output = BuildOutput(); + await builder(config, output); + await output.writeToFile(outDir: config.outDir); +} diff --git a/pkgs/native_assets_cli/lib/src/api/build_state.dart b/pkgs/native_assets_cli/lib/src/api/build_state.dart deleted file mode 100644 index ba295cae6..000000000 --- a/pkgs/native_assets_cli/lib/src/api/build_state.dart +++ /dev/null @@ -1,108 +0,0 @@ -// 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 'asset.dart'; -import 'build_config.dart'; -import 'build_output.dart'; - -final class BuildState { - /// Run a native assets build. - /// - /// Example using `package:native_toolchain_c`: - /// - /// ```dart - /// void main(List args) => - /// BuildState.build(args, (buildState) async { - /// final cbuilder = CBuilder.library( - /// name: packageName, - /// assetId: 'package:$packageName/$packageName.dart', - /// sources: [ - /// 'src/$packageName.c', - /// ], - /// ); - /// await cbuilder.run( - /// buildState: buildState, - /// logger: Logger('') - /// ..level = Level.ALL - /// ..onRecord.listen((record) => print(record.message)), - /// ); - /// }); - /// ``` - /// - /// Example outputting assets manually: - /// - /// ```dart - /// void main(List args) => - /// BuildState.build(args, (buildState) async { - /// final config = buildState.config; - /// if (config.linkModePreference == LinkModePreference.static) { - /// // Simulate that this script only supports dynamic libraries. - /// throw UnsupportedError( - /// 'LinkModePreference.static is not supported.', - /// ); - /// } - /// - /// final Iterable targets; - /// final packageName = config.packageName; - /// final assetPath = config.outDir.resolve( - /// config.targetOs.dylibFileName(packageName), - /// ); - /// if (config.dryRun) { - /// // Dry run invocations report assets for all architectures for that OS. - /// targets = Target.values.where( - /// (element) => element.os == config.targetOs, - /// ); - /// } else { - /// targets = [config.target]; - /// - /// // Insert code that downloads or builds the asset to `assetPath`. - /// } - /// - /// for (final target in targets) { - /// buildState.output.addAssets([ - /// Asset( - /// id: 'library:${packageName}/src/some_file.dart', - /// linkMode: LinkMode.dynamic, - /// target: target, - /// path: AssetAbsolutePath(assetPath), - /// ) - /// ]); - /// } - /// }); - /// ``` - static Future build( - List commandlineArguments, - Future Function(BuildState) builder, - ) async { - final config = await BuildConfig.fromArgs(commandlineArguments); - final output = BuildOutput(); - final state = BuildState._(config: config, output: output); - await builder(state); - await output.writeToFile(outDir: config.outDir); - } - - final BuildConfig config; - final BuildOutput output; - - BuildState._({ - required this.config, - required this.output, - }); - - /// Adds assets to build output. - /// - /// See [BuildOutput.addAssets] for more info. - void addAssets(Iterable assets) => output.addAssets(assets); - - /// Adds dependencies to build output. - /// - /// See [BuildOutput.addDependencies] for more info. - void addDependencies(Iterable dependencies) => - output.addDependencies(dependencies); - - /// Adds metadata to build output. - /// - /// See [BuildOutput.addMetadata] for more info. - void addMetadata(String key, Object value) => output.addMetadata(key, value); -} From e4d9b012a061fae811f3e8b73bfcd1d7f2f3c365 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 24 Jan 2024 10:00:09 +0100 Subject: [PATCH 06/75] Sketch an `AssetDownloader` --- .../lib/src/helpers/asset_downloader.dart | 72 +++++++++++++++++++ pkgs/native_assets_cli/pubspec.yaml | 1 + 2 files changed, 73 insertions(+) create mode 100644 pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart new file mode 100644 index 000000000..a67771304 --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -0,0 +1,72 @@ +// 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:io'; + +import 'package:logging/logging.dart'; + +import '../../native_assets_cli.dart'; + +abstract class Builder { + Future run({ + required BuildConfig buildConfig, + required BuildOutput buildOutput, + required Logger? logger, + }); +} + +// TODO(dacoharkes): Should we really add this? It seems too specific. +// E.g. what about varying file names, zipped downloads etc. etc. ? +class AssetDownloader implements Builder { + final Uri Function(Target) downloadUri; + + /// Asset identifier. + /// + /// If omitted, no asset will be added to the build output. + final String assetId; + + AssetDownloader({ + required this.downloadUri, + required this.assetId, + }); + + @override + Future run({ + required BuildConfig buildConfig, + required BuildOutput buildOutput, + required Logger? logger, + }) async { + final assetId = this.assetId.startsWith('package:') + ? this.assetId + : 'package:${buildConfig.packageName}/${this.assetId}'; + final List targets; + if (buildConfig.dryRun) { + targets = [ + for (final target in Target.values) + if (target.os == buildConfig.targetOs) target + ]; + } else { + targets = [buildConfig.target]; + } + for (final target in targets) { + final downloadUri2 = downloadUri(buildConfig.target); + final fileName = + downloadUri2.pathSegments.lastWhere((element) => element.isNotEmpty); + final targetUri = buildConfig.outDir.resolve(fileName); + if (!buildConfig.dryRun) { + final request = await HttpClient().getUrl(downloadUri2); + final response = await request.close(); + await response.pipe(File.fromUri(targetUri).openWrite()); + } + buildOutput.addAssets([ + Asset( + id: assetId, + linkMode: LinkMode.dynamic, + path: AssetAbsolutePath(targetUri), + target: target, + ) + ]); + } + } +} diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index 42fe30873..10226fa16 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -19,6 +19,7 @@ dependencies: cli_config: ^0.1.1 collection: ^1.17.1 crypto: ^3.0.3 + logging: ^1.2.0 pub_semver: ^2.1.3 yaml: ^3.1.1 yaml_edit: ^2.1.0 From 5deb29c9fb0cd13c6ff4d282b1e081a8f9bb6279 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 15 Feb 2024 13:25:29 +0100 Subject: [PATCH 07/75] Remove _2 fields --- .../package_reading_metadata/build.dart | 15 +++--- .../package_with_metadata/build.dart | 8 +--- pkgs/native_assets_cli/CHANGELOG.md | 2 +- .../lib/native_assets_cli.dart | 2 - .../lib/src/api/build_config.dart | 21 ++------- .../lib/src/api/build_output.dart | 40 +++++----------- .../lib/src/api/dependencies.dart | 13 ------ .../lib/src/api/metadata.dart | 12 ----- .../lib/src/model/build_config.dart | 22 ++------- .../lib/src/model/build_output.dart | 46 +++++++++---------- .../lib/src/model/dependencies.dart | 4 +- .../lib/src/model/metadata.dart | 4 +- .../test/api/build_config_test.dart | 4 +- .../test/api/build_output_test.dart | 4 +- .../test/example/native_add_library_test.dart | 4 +- .../test/model/build_config_test.dart | 1 - .../test/model/build_output_test.dart | 3 +- pkgs/native_toolchain_c/CHANGELOG.md | 3 +- .../lib/src/cbuilder/cbuilder.dart | 20 ++++---- .../test/cbuilder/cbuilder_test.dart | 6 +-- 20 files changed, 69 insertions(+), 165 deletions(-) delete mode 100644 pkgs/native_assets_cli/lib/src/api/dependencies.dart delete mode 100644 pkgs/native_assets_cli/lib/src/api/metadata.dart diff --git a/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart index 03eac1d0d..a0749b63d 100644 --- a/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart +++ b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart @@ -2,10 +2,6 @@ // 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. -// TODO(https://github.com/dart-lang/native/issues/882): A new version of -// `package:native_assets_cli` needs to be published first. -// ignore_for_file: deprecated_member_use - import 'package:cli_config/cli_config.dart'; import 'package:native_assets_cli/native_assets_cli.dart'; @@ -13,13 +9,14 @@ void main(List args) async { final config = await Config.fromArgs(args: args); final buildConfig = BuildConfig.fromConfig(config); if (!buildConfig.dryRun) { - final metadata = - buildConfig.dependencyMetadata!['package_with_metadata']!.metadata; - final someValue = metadata['some_key']; + final someValue = buildConfig.metadata('package_with_metadata', 'some_key'); assert(someValue != null); - final someInt = metadata['some_int']; + final someInt = buildConfig.metadata('package_with_metadata', 'some_int'); assert(someInt != null); - print(metadata); + print({ + 'some_int': someInt, + 'some_key': someValue, + }); } else { print('meta data not available in dry run'); } diff --git a/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart b/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart index 7fb8b22bf..d8cdc389c 100644 --- a/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart +++ b/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart @@ -2,19 +2,15 @@ // 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. -// TODO(https://github.com/dart-lang/native/issues/882): A new version of -// `package:native_assets_cli` needs to be published first. -// ignore_for_file: deprecated_member_use - import 'package:native_assets_cli/native_assets_cli.dart'; void main(List args) async { final buildConfig = await BuildConfig.fromArgs(args); final buildOutput = BuildOutput( - metadata: Metadata({ + metadata: { 'some_key': 'some_value', 'some_int': 3, - }), + }, ); await buildOutput.writeToFile(outDir: buildConfig.outDir); } diff --git a/pkgs/native_assets_cli/CHANGELOG.md b/pkgs/native_assets_cli/CHANGELOG.md index 5a1496315..30906ef31 100644 --- a/pkgs/native_assets_cli/CHANGELOG.md +++ b/pkgs/native_assets_cli/CHANGELOG.md @@ -1,7 +1,7 @@ ## 0.4.3-wip - **Breaking change** New API. -- Bump examples dependencies to path dependencies. (2x) +- Bump examples dependencies to path dependencies. ## 0.4.2 diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 331c27b68..7df4c533a 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -11,9 +11,7 @@ export 'src/api/build.dart'; export 'src/api/build_config.dart'; export 'src/api/build_mode.dart'; export 'src/api/build_output.dart'; -export 'src/api/dependencies.dart'; export 'src/api/ios_sdk.dart'; export 'src/api/link_mode.dart'; export 'src/api/link_mode_preference.dart'; -export 'src/api/metadata.dart'; export 'src/api/target.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index fabbb6876..7d52dcf03 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -16,7 +16,6 @@ import '../model/target.dart' as model; import 'build_mode.dart'; import 'ios_sdk.dart'; import 'link_mode_preference.dart'; -import 'metadata.dart'; import 'target.dart'; /// The configuration for a `build.dart` invocation. @@ -74,16 +73,6 @@ abstract class BuildConfig { /// Preferred linkMode method for library. LinkModePreference get linkModePreference; - /// Metadata from direct dependencies. - /// - /// The key in the map is the package name of the dependency. - /// - /// The key in the nested map is the key for the metadata from the dependency. - /// - /// Not available during a [dryRun]. - @Deprecated('Use getMetadata.') - Map? get dependencyMetadata; - /// Get the metadata from a direct dependency. /// /// The [packageName] of is the package name of the direct dependency. @@ -133,9 +122,7 @@ abstract class BuildConfig { int? targetAndroidNdkApi, CCompilerConfig? cCompiler, required LinkModePreference linkModePreference, - @Deprecated('Use dependencyMetadata2.') - Map? dependencyMetadata, - Map>? dependencyMetadata2, + Map>? dependencyMetadata, }) => model.BuildConfig( outDir: outDir, @@ -148,12 +135,12 @@ abstract class BuildConfig { targetAndroidNdkApi: targetAndroidNdkApi, cCompiler: cCompiler as model.CCompilerConfig?, linkModePreference: linkModePreference as model.LinkModePreference, - dependencyMetadata: dependencyMetadata2 != null + dependencyMetadata: dependencyMetadata != null ? { - for (final entry in dependencyMetadata2.entries) + for (final entry in dependencyMetadata.entries) entry.key: model.Metadata(entry.value.cast()) } - : dependencyMetadata?.cast(), + : {}, ); factory BuildConfig.dryRun({ diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 7f726ec87..b4b6840fb 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -11,8 +11,6 @@ import '../model/build_output.dart' as model; import '../model/dependencies.dart' as model; import '../model/metadata.dart' as model; import 'asset.dart'; -import 'dependencies.dart'; -import 'metadata.dart'; import 'target.dart'; /// The output of a `build.dart` invocation. @@ -29,37 +27,27 @@ abstract class BuildOutput { /// these timestamps. DateTime get timestamp; - @Deprecated('Use assets2') - List get assets; - /// The assets produced by this build. /// /// In dry runs, the assets for all [Architecture]s for the [OS] specified in /// the dry run must be provided. - // TODO: Rename to `assets` after removing old one. - Iterable get assets2; - - @Deprecated('Use dependencies2') - Dependencies get dependencies; + Iterable get assets; /// The files used by this build. /// - /// If any of the files in [dependencies2] are modified after [timestamp], the + /// If any of the files in [dependencies] are modified after [timestamp], the /// build will be re-run. // TODO: Rename to `dependencies` after removing old one. - Iterable get dependencies2; - - @Deprecated('Use getMetadata') - Metadata get metadata; + Iterable get dependencies; /// Metadata can to be passed to `build.dart` invocations of dependent /// packages. // TODO(dacoharkes): Rename to metadata. - Object? metadata2(String key); + Object? metadata(String key); /// Create a build output. /// - /// The [timestamp] must be before any any [dependencies2] are read by the + /// The [timestamp] must be before any any [dependencies] are read by the /// build this output belongs to. If the [BuildOutput] object is created at /// the beginning of the `build.dart` script, it can be omitted and will /// default to [DateTime.now]. The [timestamp] is rounded down to whole @@ -71,29 +59,23 @@ abstract class BuildOutput { /// runs, the assets for all [Architecture]s for the [OS] specified in the dry /// run must be provided. /// - /// The files used by this build must be passed in [dependencies2] or + /// The files used by this build must be passed in [dependencies] or /// [addDependencies]. If any of these files are modified after [timestamp], /// the build will be re-run. /// /// Metadata can be passed to `build.dart` invocations of dependent packages - /// via [metadata2] or [addMetadata]. + /// via [metadata] or [addMetadata]. factory BuildOutput({ DateTime? timestamp, Iterable? assets, - @Deprecated('Use addDependencies.') Dependencies? dependencies, - Iterable? dependencies2, - @Deprecated('Use addMetadata.') Metadata? metadata, - Map? metadata2, + Iterable? dependencies, + Map? metadata, }) => model.BuildOutput( timestamp: timestamp, assets: assets?.cast().toList(), - dependencies: dependencies2 != null - ? model.Dependencies(dependencies2.toList()) - : dependencies as model.Dependencies?, - metadata: metadata2 != null - ? model.Metadata(metadata2) - : metadata as model.Metadata?, + dependencies: model.Dependencies([...?dependencies]), + metadata: model.Metadata({...?metadata}), ); /// Adds [Asset]s produced by this build or dry run. diff --git a/pkgs/native_assets_cli/lib/src/api/dependencies.dart b/pkgs/native_assets_cli/lib/src/api/dependencies.dart deleted file mode 100644 index f79d0c13d..000000000 --- a/pkgs/native_assets_cli/lib/src/api/dependencies.dart +++ /dev/null @@ -1,13 +0,0 @@ -// 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 '../model/dependencies.dart' as model; - -@Deprecated('Use the methods on BuildOutput.') -abstract class Dependencies { - /// The dependencies a build relied on. - List get dependencies; - - const factory Dependencies(List dependencies) = model.Dependencies; -} diff --git a/pkgs/native_assets_cli/lib/src/api/metadata.dart b/pkgs/native_assets_cli/lib/src/api/metadata.dart deleted file mode 100644 index efa8d7ff5..000000000 --- a/pkgs/native_assets_cli/lib/src/api/metadata.dart +++ /dev/null @@ -1,12 +0,0 @@ -// 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 '../model/metadata.dart' as model; - -@Deprecated('Use the methods on BuildConfig and BuildOutput.') -abstract class Metadata { - Map get metadata; - - const factory Metadata(Map metadata) = model.Metadata; -} diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 842db9afb..5493e89d6 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -98,19 +98,6 @@ class BuildConfig implements api.BuildConfig { LinkModePreference get linkModePreference => _linkModePreference; late final LinkModePreference _linkModePreference; - /// Metadata from direct dependencies. - /// - /// The key in the map is the package name of the dependency. - /// - /// The key in the nested map is the key for the metadata from the dependency. - /// - /// Not available during a [dryRun]. - @override - Map? get dependencyMetadata { - _ensureNotDryRun(); - return _dependencyMetadata; - } - @override Object? metadata(String packageName, String key) { _ensureNotDryRun(); @@ -146,9 +133,6 @@ class BuildConfig implements api.BuildConfig { late final BuildMode _buildMode; - /// The underlying config. - /// - /// Can be used for easier access to values on [dependencyMetadata]. @override Config get config => _config; late final Config _config; @@ -519,7 +503,7 @@ class BuildConfig implements api.BuildConfig { if (_targetAndroidNdkApi != null) targetAndroidNdkApiConfigKey: _targetAndroidNdkApi!, if (cCompilerYaml.isNotEmpty) CCompilerConfig.configKey: cCompilerYaml, - if (_dependencyMetadata != null) + if (_dependencyMetadata != null && _dependencyMetadata!.isNotEmpty) dependencyMetadataConfigKey: { for (final entry in _dependencyMetadata!.entries) entry.key: entry.value.toYaml(), @@ -548,7 +532,7 @@ class BuildConfig implements api.BuildConfig { if (other.targetAndroidNdkApi != targetAndroidNdkApi) return false; if (other.cCompiler != cCompiler) return false; if (!const DeepCollectionEquality() - .equals(other.dependencyMetadata, _dependencyMetadata)) return false; + .equals(other._dependencyMetadata, _dependencyMetadata)) return false; } return true; } @@ -563,7 +547,7 @@ class BuildConfig implements api.BuildConfig { dryRun, if (!dryRun) ...[ buildMode, - const DeepCollectionEquality().hash(dependencyMetadata), + const DeepCollectionEquality().hash(_dependencyMetadata), targetArchitecture, targetIOSSdk, targetAndroidNdkApi, diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index 3be0251b8..eef3770bb 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -22,20 +22,17 @@ class BuildOutput implements api.BuildOutput { @override final DateTime timestamp; - @override - final List assets; + final List _assets; @override - Iterable get assets2 => assets; + Iterable get assets => _assets; - @override - final Dependencies dependencies; + final Dependencies _dependencies; @override - Iterable get dependencies2 => dependencies.dependencies; + Iterable get dependencies => _dependencies.dependencies; - @override - final Metadata metadata; + final Metadata _metadata; BuildOutput({ DateTime? timestamp, @@ -43,15 +40,15 @@ class BuildOutput implements api.BuildOutput { Dependencies? dependencies, Metadata? metadata, }) : timestamp = (timestamp ?? DateTime.now()).roundDownToSeconds(), - assets = assets ?? [], + _assets = assets ?? [], // ignore: prefer_const_constructors - dependencies = dependencies ?? Dependencies([]), + _dependencies = dependencies ?? Dependencies([]), // ignore: prefer_const_constructors - metadata = metadata ?? Metadata({}); + _metadata = metadata ?? Metadata({}); @override void addDependencies(Iterable dependencies) => - this.dependencies.dependencies.addAll(dependencies); + _dependencies.dependencies.addAll(dependencies); static const _assetsKey = 'assets'; static const _dependenciesKey = 'dependencies'; @@ -92,9 +89,10 @@ class BuildOutput implements api.BuildOutput { Map toYaml() => { _timestampKey: timestamp.toString(), - _assetsKey: assets.toYaml(), - _dependenciesKey: dependencies.toYaml(), - _metadataKey: metadata.toYaml(), + _assetsKey: _assets.toYaml(), + if (_dependencies.dependencies.isNotEmpty) + _dependenciesKey: _dependencies.toYaml(), + _metadataKey: _metadata.toYaml(), _versionKey: version.toString(), }..sortOnKey(); @@ -139,29 +137,29 @@ class BuildOutput implements api.BuildOutput { return false; } return other.timestamp == timestamp && - const ListEquality().equals(other.assets, assets) && - other.dependencies == dependencies && - other.metadata == metadata; + const ListEquality().equals(other._assets, _assets) && + other._dependencies == _dependencies && + other._metadata == _metadata; } @override int get hashCode => Object.hash( timestamp.hashCode, - const ListEquality().hash(assets), - dependencies, - metadata, + const ListEquality().hash(_assets), + _dependencies, + _metadata, ); @override void addMetadata(String key, Object value) { - metadata.metadata[key] = value; + _metadata.metadata[key] = value; } @override - Object? metadata2(String key) => metadata.metadata[key]; + Object? metadata(String key) => _metadata.metadata[key]; @override void addAssets(Iterable assets) { - this.assets.addAll(assets.cast()); + _assets.addAll(assets.cast()); } } diff --git a/pkgs/native_assets_cli/lib/src/model/dependencies.dart b/pkgs/native_assets_cli/lib/src/model/dependencies.dart index 6b42a8104..e67ebfc91 100644 --- a/pkgs/native_assets_cli/lib/src/model/dependencies.dart +++ b/pkgs/native_assets_cli/lib/src/model/dependencies.dart @@ -5,15 +5,13 @@ import 'package:collection/collection.dart'; import 'package:yaml/yaml.dart'; -import '../api/dependencies.dart' as api; import '../utils/file.dart'; import '../utils/uri.dart'; import '../utils/yaml.dart'; // ignore: deprecated_member_use_from_same_package -class Dependencies implements api.Dependencies { +class Dependencies { /// The dependencies a build relied on. - @override final List dependencies; const Dependencies(this.dependencies); diff --git a/pkgs/native_assets_cli/lib/src/model/metadata.dart b/pkgs/native_assets_cli/lib/src/model/metadata.dart index f31df0dca..ed4f8916c 100644 --- a/pkgs/native_assets_cli/lib/src/model/metadata.dart +++ b/pkgs/native_assets_cli/lib/src/model/metadata.dart @@ -5,13 +5,11 @@ import 'package:collection/collection.dart'; import 'package:yaml/yaml.dart'; -import '../api/metadata.dart' as api; import '../utils/map.dart'; import '../utils/yaml.dart'; // ignore: deprecated_member_use_from_same_package -class Metadata implements api.Metadata { - @override +class Metadata { final Map metadata; const Metadata(this.metadata); diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index a44569547..82a0c5712 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -153,7 +153,7 @@ void main() async { targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, - dependencyMetadata2: { + dependencyMetadata: { 'bar': { 'key': 'value', 'foo': ['asdf', 'fdsa'], @@ -173,7 +173,7 @@ void main() async { targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, - dependencyMetadata2: { + dependencyMetadata: { 'bar': { 'key': 'value', }, diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index 9ae10ad67..0307911cf 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -35,10 +35,10 @@ void main() { linkMode: LinkMode.dynamic, ), ], - dependencies2: [ + dependencies: [ Uri.file('path/to/file.ext'), ], - metadata2: { + metadata: { 'key': 'value', }, ); diff --git a/pkgs/native_assets_cli/test/example/native_add_library_test.dart b/pkgs/native_assets_cli/test/example/native_add_library_test.dart index d7a0b3f84..2c4f22f91 100644 --- a/pkgs/native_assets_cli/test/example/native_add_library_test.dart +++ b/pkgs/native_assets_cli/test/example/native_add_library_test.dart @@ -78,12 +78,12 @@ void main() async { await File.fromUri((assets.first.path as AssetAbsolutePath).uri) .exists(), false); - expect(dependencies.dependencies, []); + expect(dependencies, []); } else { expect(assets.length, 1); expect(await assets.allExist(), true); expect( - dependencies.dependencies, + dependencies, [ testPackageUri.resolve('src/$name.c'), testPackageUri.resolve('build.dart'), diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index df9df7c6a..d8fb8fec3 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -90,7 +90,6 @@ void main() async { true); expect(config1.cCompiler != config2.cCompiler, true); expect(config1.linkModePreference, config2.linkModePreference); - expect(config1.dependencyMetadata, config2.dependencyMetadata); }); test('BuildConfig fromConfig', () { diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 8ac830685..8aa2ab65f 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -165,8 +165,7 @@ version: ${BuildOutput.version}'''), // Remove once dependencies are made immutable. final buildOutput = BuildOutput(); expect( - () => buildOutput.dependencies.dependencies - .add(Uri.file('path/to/file.ext')), + () => buildOutput.addDependencies([Uri.file('path/to/file.ext')]), returnsNormally, ); }); diff --git a/pkgs/native_toolchain_c/CHANGELOG.md b/pkgs/native_toolchain_c/CHANGELOG.md index 537c8b960..dd97f5c7b 100644 --- a/pkgs/native_toolchain_c/CHANGELOG.md +++ b/pkgs/native_toolchain_c/CHANGELOG.md @@ -1,7 +1,6 @@ ## 0.3.5-wip -- Bump `package:native_assets_cli` to 0.4.1. -- Temporary deprecation lint suppresion for `package:native_assets_cli`. +- Bump `package:native_assets_cli` to path dependency. ## 0.3.4+1 diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index a5fafab72..1e9142427 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -2,10 +2,6 @@ // 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. -// TODO(https://github.com/dart-lang/native/issues/882): A new version of -// `package:native_assets_cli` needs to be published first. -// ignore_for_file: deprecated_member_use - import 'dart:io'; import 'package:logging/logging.dart'; @@ -259,12 +255,14 @@ class CBuilder implements Builder { if (target.os == buildConfig.targetOs) target ]; for (final target in targets) { - buildOutput.assets.add(Asset( - id: assetId!, - linkMode: linkMode, - target: target, - path: AssetAbsolutePath(libUri), - )); + buildOutput.addAssets([ + Asset( + id: assetId!, + linkMode: linkMode, + target: target, + path: AssetAbsolutePath(libUri), + ) + ]); } } if (!buildConfig.dryRun) { @@ -277,7 +275,7 @@ class CBuilder implements Builder { ) .toList(); - buildOutput.dependencies.dependencies.addAll({ + buildOutput.addDependencies({ // Note: We use a Set here to deduplicate the dependencies. ...sources, ...includeFiles, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart index 56015b5cb..aa98ed978 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart @@ -2,10 +2,6 @@ // 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. -// TODO(https://github.com/dart-lang/native/issues/882): A new version of -// `package:native_assets_cli` needs to be published first. -// ignore_for_file: deprecated_member_use - @OnPlatform({ 'mac-os': Timeout.factor(2), 'windows': Timeout.factor(10), @@ -305,7 +301,7 @@ void main() { logger: logger, ); - expect(buildOutput.dependencies.dependencies, contains(includesHUri)); + expect(buildOutput.dependencies, contains(includesHUri)); final dylibUri = tempUri.resolve(Target.current.os.dylibFileName(name)); final dylib = openDynamicLibraryForTest(dylibUri.toFilePath()); From a580dfb8698ccf12d32c0355bd00fa64dd3e2c39 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 15 Feb 2024 13:39:21 +0100 Subject: [PATCH 08/75] changelogs --- pkgs/native_assets_builder/CHANGELOG.md | 4 ++++ pkgs/native_assets_builder/pubspec.yaml | 10 ++++++---- pkgs/native_assets_cli/lib/src/model/dependencies.dart | 1 - pkgs/native_assets_cli/lib/src/model/metadata.dart | 1 - 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pkgs/native_assets_builder/CHANGELOG.md b/pkgs/native_assets_builder/CHANGELOG.md index 900457b42..c68610946 100644 --- a/pkgs/native_assets_builder/CHANGELOG.md +++ b/pkgs/native_assets_builder/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.1-wip + +- Bump `package:native_assets_cli` to path dependency. + ## 0.5.0 - **Breaking change**: Hide implementation of `KernelAssets`. diff --git a/pkgs/native_assets_builder/pubspec.yaml b/pkgs/native_assets_builder/pubspec.yaml index 37199f6a1..c65f56cee 100644 --- a/pkgs/native_assets_builder/pubspec.yaml +++ b/pkgs/native_assets_builder/pubspec.yaml @@ -1,18 +1,20 @@ name: native_assets_builder description: >- This package is the backend that invokes top-level `build.dart` scripts. -version: 0.5.0 +version: 0.5.1-wip repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_builder environment: sdk: '>=3.0.0 <4.0.0' +publish_to: none + dependencies: graphs: ^2.3.1 logging: ^1.2.0 - native_assets_cli: ^0.4.2 - # native_assets_cli: - # path: ../native_assets_cli/ + # native_assets_cli: ^0.4.2 + native_assets_cli: + path: ../native_assets_cli/ package_config: ^2.1.0 yaml: ^3.1.2 yaml_edit: ^2.1.0 diff --git a/pkgs/native_assets_cli/lib/src/model/dependencies.dart b/pkgs/native_assets_cli/lib/src/model/dependencies.dart index e67ebfc91..9ecc2cb91 100644 --- a/pkgs/native_assets_cli/lib/src/model/dependencies.dart +++ b/pkgs/native_assets_cli/lib/src/model/dependencies.dart @@ -9,7 +9,6 @@ import '../utils/file.dart'; import '../utils/uri.dart'; import '../utils/yaml.dart'; -// ignore: deprecated_member_use_from_same_package class Dependencies { /// The dependencies a build relied on. final List dependencies; diff --git a/pkgs/native_assets_cli/lib/src/model/metadata.dart b/pkgs/native_assets_cli/lib/src/model/metadata.dart index ed4f8916c..34949b1d4 100644 --- a/pkgs/native_assets_cli/lib/src/model/metadata.dart +++ b/pkgs/native_assets_cli/lib/src/model/metadata.dart @@ -8,7 +8,6 @@ import 'package:yaml/yaml.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; -// ignore: deprecated_member_use_from_same_package class Metadata { final Map metadata; From b0d4cdcb32e76486aaf87cf84556816f23b0e351 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 15 Feb 2024 13:47:23 +0100 Subject: [PATCH 09/75] cleanup --- .../lib/src/api/build_output.dart | 2 -- .../native_assets_cli/lib/src/api/target.dart | 25 +------------------ .../lib/src/model/target.dart | 2 +- 3 files changed, 2 insertions(+), 27 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index b4b6840fb..4735c245e 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -37,12 +37,10 @@ abstract class BuildOutput { /// /// If any of the files in [dependencies] are modified after [timestamp], the /// build will be re-run. - // TODO: Rename to `dependencies` after removing old one. Iterable get dependencies; /// Metadata can to be passed to `build.dart` invocations of dependent /// packages. - // TODO(dacoharkes): Rename to metadata. Object? metadata(String key); /// Create a build output. diff --git a/pkgs/native_assets_cli/lib/src/api/target.dart b/pkgs/native_assets_cli/lib/src/api/target.dart index 721a52fb3..dbbec8a88 100644 --- a/pkgs/native_assets_cli/lib/src/api/target.dart +++ b/pkgs/native_assets_cli/lib/src/api/target.dart @@ -98,30 +98,7 @@ abstract class Target implements Comparable { /// /// Note that for some of these a Dart SDK is not available and they are only /// used as target architectures for Flutter apps. - static const values = { - androidArm, - androidArm64, - androidIA32, - androidX64, - androidRiscv64, - fuchsiaArm64, - fuchsiaX64, - iOSArm, - iOSArm64, - iOSX64, - linuxArm, - linuxArm64, - linuxIA32, - linuxRiscv32, - linuxRiscv64, - linuxX64, - macOSArm64, - macOSX64, - windowsArm64, - windowsIA32, - windowsX64, - // TODO(dacoharkes): Add support for `wasm`. - }; + static const Set values = model.Target.values; /// The current [Target]. /// diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index 8492e3992..de9cfbb63 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -298,7 +298,7 @@ class Target implements api.Target { /// /// Note that for some of these a Dart SDK is not available and they are only /// used as target architectures for Flutter apps. - static const values = { + static const Set values = { androidArm, androidArm64, androidIA32, From 12108ad2e74332943fc62278390b035887a2df61 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 15 Feb 2024 14:14:39 +0100 Subject: [PATCH 10/75] fix wrong deps version --- .../lib/src/build_runner/build_runner.dart | 16 +++---- .../example/native_add_library/build.dart | 43 +++++++------------ .../lib/src/model/build_output.dart | 4 ++ 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 5a35967fd..963155f3d 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -204,7 +204,7 @@ class NativeAssetsBuildRunner { final buildOutput = await BuildOutput.readFromFile(outDir: outDir); final lastBuilt = buildOutput?.timestamp.roundDownToSeconds() ?? DateTime.fromMillisecondsSinceEpoch(0); - final dependencies = buildOutput?.dependencies; + final dependencies = buildOutput?.dependenciesModel; final lastChange = await dependencies?.lastModified() ?? DateTime.now(); if (lastBuilt.isAfter(lastChange)) { @@ -213,8 +213,8 @@ class NativeAssetsBuildRunner { // All build flags go into [outDir]. Therefore we do not have to check // here whether the config is equal. final assets = buildOutput!.assets; - final dependencies = buildOutput.dependencies.dependencies; - final metadata = buildOutput.metadata; + final dependencies = buildOutput.dependencies; + final metadata = buildOutput.metadataModel; return (assets, dependencies, metadata, true); } @@ -285,8 +285,8 @@ ${result.stdout} final buildOutput = await BuildOutput.readFromFile(outDir: outDir); final assets = buildOutput?.assets ?? []; success &= validateAssetsPackage(assets, config.packageName); - final dependencies = buildOutput?.dependencies.dependencies ?? []; - final metadata = dryRun ? null : buildOutput?.metadata; + final dependencies = buildOutput?.dependencies ?? []; + final metadata = dryRun ? null : buildOutput?.metadataModel; return (assets, dependencies, metadata, success); } on FormatException catch (e) { logger.severe(''' @@ -400,7 +400,7 @@ build_output.yaml contained a format error. }; } - bool validateAssetsPackage(List assets, String packageName) { + bool validateAssetsPackage(Iterable assets, String packageName) { final invalidAssetIds = assets .map((a) => a.id) .where((n) => !n.startsWith('package:$packageName/')) @@ -419,8 +419,8 @@ build_output.yaml contained a format error. } typedef _PackageBuildRecord = ( - List, - List dependencies, + Iterable, + Iterable dependencies, Metadata?, bool success, ); diff --git a/pkgs/native_assets_cli/example/native_add_library/build.dart b/pkgs/native_assets_cli/example/native_add_library/build.dart index ae39cdc1a..ea3121d52 100644 --- a/pkgs/native_assets_cli/example/native_add_library/build.dart +++ b/pkgs/native_assets_cli/example/native_add_library/build.dart @@ -8,32 +8,21 @@ import 'package:native_toolchain_c/native_toolchain_c.dart'; const packageName = 'native_add_library'; -/// Implements the protocol from `package:native_assets_cli` by building -/// the C code in `src/` and reporting what native assets it built. void main(List args) async { - // Parse the build configuration passed to this CLI from Dart or Flutter. - final buildConfig = await BuildConfig.fromArgs(args); - final buildOutput = BuildOutput(); - - // Configure `package:native_toolchain_c` to build the C code for us. - final cbuilder = CBuilder.library( - name: packageName, - assetId: 'package:$packageName/${packageName}.dart', - sources: [ - 'src/$packageName.c', - ], - ); - await cbuilder.run( - buildConfig: buildConfig, - // `package:native_toolchain_c` will output the dynamic or static libraries it built, - // what files it accessed (for caching the build), etc. - buildOutput: buildOutput, - logger: Logger('') - ..level = Level.ALL - ..onRecord.listen((record) => print(record.message)), - ); - - // Write the output according to the native assets protocol so that Dart or - // Flutter can find the native assets produced by this script. - await buildOutput.writeToFile(outDir: buildConfig.outDir); + await build(args, (buildConfig, buildOutput) async { + final cbuilder = CBuilder.library( + name: packageName, + assetId: 'package:$packageName/$packageName.dart', + sources: [ + 'src/$packageName.c', + ], + ); + await cbuilder.run( + buildConfig: buildConfig, + buildOutput: buildOutput, + logger: Logger('') + ..level = Level.ALL + ..onRecord.listen((record) => print(record.message)), + ); + }); } diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index eef3770bb..ba8acc895 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -29,6 +29,8 @@ class BuildOutput implements api.BuildOutput { final Dependencies _dependencies; + Dependencies get dependenciesModel => _dependencies; + @override Iterable get dependencies => _dependencies.dependencies; @@ -158,6 +160,8 @@ class BuildOutput implements api.BuildOutput { @override Object? metadata(String key) => _metadata.metadata[key]; + Metadata get metadataModel => _metadata; + @override void addAssets(Iterable assets) { _assets.addAll(assets.cast()); From b0bc231577517726a7d1d89aa92e603b1055b43e Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 15 Feb 2024 14:43:54 +0100 Subject: [PATCH 11/75] Add local asset example --- .../example/local_asset/README.md | 5 + .../example/local_asset/build.dart | 52 ++++++++++ .../example/local_asset/data/asset.txt | 1 + .../example/local_asset/pubspec.yaml | 20 ++++ .../test/example/local_asset_test.dart | 95 +++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 pkgs/native_assets_cli/example/local_asset/README.md create mode 100644 pkgs/native_assets_cli/example/local_asset/build.dart create mode 100644 pkgs/native_assets_cli/example/local_asset/data/asset.txt create mode 100644 pkgs/native_assets_cli/example/local_asset/pubspec.yaml create mode 100644 pkgs/native_assets_cli/test/example/local_asset_test.dart diff --git a/pkgs/native_assets_cli/example/local_asset/README.md b/pkgs/native_assets_cli/example/local_asset/README.md new file mode 100644 index 000000000..3ffe34caf --- /dev/null +++ b/pkgs/native_assets_cli/example/local_asset/README.md @@ -0,0 +1,5 @@ +This example shows two use cases: + +* Data assets that are part of the package source code. +* Code assets which are downloaded from the cloud. + (It doesn't show how to download, but shows how to implement the protocol correctly.) diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart new file mode 100644 index 000000000..f6e8eaa52 --- /dev/null +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -0,0 +1,52 @@ +// 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:io'; + +import 'package:native_assets_cli/native_assets_cli.dart'; + +const assetName = 'asset.txt'; +final packageAssetPath = Uri.file('data/$assetName'); + +void main(List args) async { + await build(args, (config, output) async { + if (config.linkModePreference == LinkModePreference.static) { + // Simulate that this script only supports dynamic libraries. + throw UnsupportedError( + 'LinkModePreference.static is not supported.', + ); + } + + final Iterable targets; + final packageName = config.packageName; + final assetPath = config.outDir.resolve(assetName); + final assetSourcePath = config.packageRoot.resolveUri(packageAssetPath); + if (config.dryRun) { + // Dry run invocations report assets for all architectures for that OS. + targets = Target.values.where( + (element) => element.os == config.targetOs, + ); + } else { + targets = [config.target]; + + // Insert code that downloads or builds the asset to `assetPath`. + await File.fromUri(assetSourcePath).copy(assetPath.toFilePath()); + + output.addDependencies([ + assetSourcePath, + config.packageRoot.resolve('build.dart'), + ]); + } + + output.addAssets([ + for (final target in targets) + Asset( + id: 'library:$packageName/asset.txt', + linkMode: LinkMode.dynamic, + target: target, + path: AssetAbsolutePath(assetPath), + ) + ]); + }); +} diff --git a/pkgs/native_assets_cli/example/local_asset/data/asset.txt b/pkgs/native_assets_cli/example/local_asset/data/asset.txt new file mode 100644 index 000000000..0cdca5885 --- /dev/null +++ b/pkgs/native_assets_cli/example/local_asset/data/asset.txt @@ -0,0 +1 @@ +Some arbitrary contents diff --git a/pkgs/native_assets_cli/example/local_asset/pubspec.yaml b/pkgs/native_assets_cli/example/local_asset/pubspec.yaml new file mode 100644 index 000000000..06d2d89b9 --- /dev/null +++ b/pkgs/native_assets_cli/example/local_asset/pubspec.yaml @@ -0,0 +1,20 @@ +publish_to: none + +name: local_asset +description: Uses an asset local to the package. +version: 0.1.0 +repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli/example/native_add_library + +environment: + sdk: '>=3.0.0 <4.0.0' + +dependencies: + cli_config: ^0.1.1 + logging: ^1.1.1 + native_assets_cli: + path: ../../../native_assets_cli/ + +dev_dependencies: + ffigen: ^8.0.2 + lints: ^3.0.0 + test: ^1.21.0 diff --git a/pkgs/native_assets_cli/test/example/local_asset_test.dart b/pkgs/native_assets_cli/test/example/local_asset_test.dart new file mode 100644 index 000000000..2179c0866 --- /dev/null +++ b/pkgs/native_assets_cli/test/example/local_asset_test.dart @@ -0,0 +1,95 @@ +// 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. + +@OnPlatform({ + 'mac-os': Timeout.factor(2), + 'windows': Timeout.factor(10), +}) +library; + +import 'dart:io'; + +import 'package:native_assets_cli/native_assets_cli_internal.dart'; +import 'package:test/test.dart'; + +import '../helpers.dart'; + +void main() async { + late Uri tempUri; + const name = 'local_asset'; + + setUp(() async { + tempUri = (await Directory.systemTemp.createTemp()).uri; + }); + + tearDown(() async { + await Directory.fromUri(tempUri).delete(recursive: true); + }); + + for (final dryRun in [true, false]) { + final testSuffix = dryRun ? ' dry_run' : ''; + test('local_asset build$testSuffix', () async { + final testTempUri = tempUri.resolve('test1/'); + await Directory.fromUri(testTempUri).create(); + final testPackageUri = packageUri.resolve('example/$name/'); + final dartUri = Uri.file(Platform.resolvedExecutable); + + final processResult = await Process.run( + dartUri.toFilePath(), + [ + 'build.dart', + '-Dout_dir=${tempUri.toFilePath()}', + '-Dpackage_name=$name', + '-Dpackage_root=${testPackageUri.toFilePath()}', + '-Dtarget_os=${OS.current}', + '-Dversion=${BuildConfig.version}', + '-Dlink_mode_preference=dynamic', + '-Ddry_run=$dryRun', + if (!dryRun) ...[ + '-Dtarget_architecture=${Architecture.current}', + '-Dbuild_mode=debug', + if (cc != null) '-Dcc=${cc!.toFilePath()}', + if (envScript != null) + '-D${CCompilerConfig.envScriptConfigKeyFull}=' + '${envScript!.toFilePath()}', + if (envScriptArgs != null) + '-D${CCompilerConfig.envScriptArgsConfigKeyFull}=' + '${envScriptArgs!.join(' ')}', + ], + ], + workingDirectory: testPackageUri.toFilePath(), + ); + if (processResult.exitCode != 0) { + print(processResult.stdout); + print(processResult.stderr); + print(processResult.exitCode); + } + expect(processResult.exitCode, 0); + + final buildOutputUri = tempUri.resolve('build_output.yaml'); + final buildOutput = BuildOutput.fromYamlString( + await File.fromUri(buildOutputUri).readAsString()); + final assets = buildOutput.assets; + final dependencies = buildOutput.dependencies; + if (dryRun) { + expect(assets.length, greaterThanOrEqualTo(1)); + expect( + await File.fromUri((assets.first.path as AssetAbsolutePath).uri) + .exists(), + false); + expect(dependencies, []); + } else { + expect(assets.length, 1); + expect(await assets.allExist(), true); + expect( + dependencies, + [ + testPackageUri.resolve('data/asset.txt'), + testPackageUri.resolve('build.dart'), + ], + ); + } + }); + } +} From 37de6cfb04de0eb79b5a43676068cd031f62fadc Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 15 Feb 2024 16:48:26 +0100 Subject: [PATCH 12/75] Add both singular and plural API --- .../package_reading_metadata/build.dart | 5 ++-- .../lib/src/api/build_config.dart | 2 +- .../lib/src/api/build_output.dart | 29 ++++++++++++++----- .../lib/src/model/build_config.dart | 2 +- .../lib/src/model/build_output.dart | 15 ++++++++-- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart index a0749b63d..e03f24cde 100644 --- a/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart +++ b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart @@ -9,9 +9,10 @@ void main(List args) async { final config = await Config.fromArgs(args: args); final buildConfig = BuildConfig.fromConfig(config); if (!buildConfig.dryRun) { - final someValue = buildConfig.metadata('package_with_metadata', 'some_key'); + final someValue = + buildConfig.metadatum('package_with_metadata', 'some_key'); assert(someValue != null); - final someInt = buildConfig.metadata('package_with_metadata', 'some_int'); + final someInt = buildConfig.metadatum('package_with_metadata', 'some_int'); assert(someInt != null); print({ 'some_int': someInt, diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 7d52dcf03..3b49b8c32 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -80,7 +80,7 @@ abstract class BuildConfig { /// Returns `null` if metadata was not provided. /// /// Not available during a [dryRun]. - Object? metadata(String packageName, String key); + Object? metadatum(String packageName, String key); /// The configuration for invoking the C compiler. /// diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 4735c245e..2fc9a0384 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -39,16 +39,12 @@ abstract class BuildOutput { /// build will be re-run. Iterable get dependencies; - /// Metadata can to be passed to `build.dart` invocations of dependent - /// packages. - Object? metadata(String key); - /// Create a build output. /// /// The [timestamp] must be before any any [dependencies] are read by the /// build this output belongs to. If the [BuildOutput] object is created at - /// the beginning of the `build.dart` script, it can be omitted and will - /// default to [DateTime.now]. The [timestamp] is rounded down to whole + /// the beginning of the `build.dart` script, [timestamp] can be omitted and + /// will default to [DateTime.now]. The [timestamp] is rounded down to whole /// seconds, because [File.lastModified] is rounded to whole seconds and /// caching logic compares these timestamps. /// @@ -59,7 +55,8 @@ abstract class BuildOutput { /// /// The files used by this build must be passed in [dependencies] or /// [addDependencies]. If any of these files are modified after [timestamp], - /// the build will be re-run. + /// the build will be re-run. Typically these dependencies contain the + /// `build.dart` script itself, and the source files used in the build. /// /// Metadata can be passed to `build.dart` invocations of dependent packages /// via [metadata] or [addMetadata]. @@ -76,12 +73,24 @@ abstract class BuildOutput { metadata: model.Metadata({...?metadata}), ); + /// Adds [Asset]s produced by this build or dry run. + /// + /// In dry runs, the assets for all [Architecture]s for the [OS] specified in + /// the dry run must be provided. + void addAsset(Asset asset); + /// Adds [Asset]s produced by this build or dry run. /// /// In dry runs, the assets for all [Architecture]s for the [OS] specified in /// the dry run must be provided. void addAssets(Iterable assets); + /// Adds file used by this build. + /// + /// If any of the files are modified after [timestamp], the build will be + /// re-run. + void addDependency(Uri dependency); + /// Adds files used by this build. /// /// If any of the files are modified after [timestamp], the build will be @@ -90,7 +99,11 @@ abstract class BuildOutput { /// Adds metadata to be passed to `build.dart` invocations of dependent /// packages. - void addMetadata(String key, Object value); + void addMetadatum(String key, Object value); + + /// Adds metadata to be passed to `build.dart` invocations of dependent + /// packages. + void addMetadata(Map metadata); /// The version of [BuildOutput]. /// diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 5493e89d6..cc476ad26 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -99,7 +99,7 @@ class BuildConfig implements api.BuildConfig { late final LinkModePreference _linkModePreference; @override - Object? metadata(String packageName, String key) { + Object? metadatum(String packageName, String key) { _ensureNotDryRun(); return _dependencyMetadata?[packageName]?.metadata[key]; } diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index ba8acc895..a5cae1cf8 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -48,6 +48,10 @@ class BuildOutput implements api.BuildOutput { // ignore: prefer_const_constructors _metadata = metadata ?? Metadata({}); + @override + void addDependency(Uri dependency) => + _dependencies.dependencies.add(dependency); + @override void addDependencies(Iterable dependencies) => _dependencies.dependencies.addAll(dependencies); @@ -153,15 +157,22 @@ class BuildOutput implements api.BuildOutput { ); @override - void addMetadata(String key, Object value) { + void addMetadatum(String key, Object value) { _metadata.metadata[key] = value; } @override - Object? metadata(String key) => _metadata.metadata[key]; + void addMetadata(Map metadata) { + _metadata.metadata.addAll(metadata); + } Metadata get metadataModel => _metadata; + @override + void addAsset(api.Asset asset) { + _assets.add(asset as Asset); + } + @override void addAssets(Iterable assets) { _assets.addAll(assets.cast()); From cef032fe330e365fb8ed405d3cde9de89d06d40c Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 16 Feb 2024 12:11:20 +0100 Subject: [PATCH 13/75] update examples --- .../example/native_add_library/build.dart | 9 +- pkgs/native_assets_cli/lib/src/api/build.dart | 108 ++++++++++-------- 2 files changed, 63 insertions(+), 54 deletions(-) diff --git a/pkgs/native_assets_cli/example/native_add_library/build.dart b/pkgs/native_assets_cli/example/native_add_library/build.dart index ea3121d52..f05fe779b 100644 --- a/pkgs/native_assets_cli/example/native_add_library/build.dart +++ b/pkgs/native_assets_cli/example/native_add_library/build.dart @@ -6,10 +6,9 @@ import 'package:logging/logging.dart'; import 'package:native_assets_cli/native_assets_cli.dart'; import 'package:native_toolchain_c/native_toolchain_c.dart'; -const packageName = 'native_add_library'; - void main(List args) async { - await build(args, (buildConfig, buildOutput) async { + await build(args, (config, output) async { + final packageName = config.packageName; final cbuilder = CBuilder.library( name: packageName, assetId: 'package:$packageName/$packageName.dart', @@ -18,8 +17,8 @@ void main(List args) async { ], ); await cbuilder.run( - buildConfig: buildConfig, - buildOutput: buildOutput, + buildConfig: config, + buildOutput: output, logger: Logger('') ..level = Level.ALL ..onRecord.listen((record) => print(record.message)), diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 6ee951660..60833cc55 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -10,64 +10,74 @@ import 'build_output.dart'; /// Example using `package:native_toolchain_c`: /// /// ```dart -/// void main(List args) => -/// build(args, (buildConfig, buildOutput) async { -/// final cbuilder = CBuilder.library( -/// name: packageName, -/// assetId: 'package:$packageName/$packageName.dart', -/// sources: [ -/// 'src/$packageName.c', -/// ], -/// ); -/// await cbuilder.run( -/// buildConfig: buildConfig, -/// buildOutput: buildOutput, -/// logger: Logger('') -/// ..level = Level.ALL -/// ..onRecord.listen((record) => print(record.message)), -/// ); -/// }); +/// void main(List args) async { +/// await build(args, (config, output) async { +/// final packageName = config.packageName; +/// final cbuilder = CBuilder.library( +/// name: packageName, +/// assetId: 'package:$packageName/$packageName.dart', +/// sources: [ +/// 'src/$packageName.c', +/// ], +/// ); +/// await cbuilder.run( +/// buildConfig: config, +/// buildOutput: output, +/// logger: Logger('') +/// ..level = Level.ALL +/// ..onRecord.listen((record) => print(record.message)), +/// ); +/// }); +/// } /// ``` /// /// Example outputting assets manually: /// /// ```dart -/// void main(List args) => -/// build(args, (config, output) async { -/// if (config.linkModePreference == LinkModePreference.static) { -/// // Simulate that this script only supports dynamic libraries. -/// throw UnsupportedError( -/// 'LinkModePreference.static is not supported.', -/// ); -/// } +/// const assetName = 'asset.txt'; +/// final packageAssetPath = Uri.file('data/$assetName'); /// -/// final Iterable targets; -/// final packageName = config.packageName; -/// final assetPath = config.outDir.resolve( -/// config.targetOs.dylibFileName(packageName), +/// void main(List args) async { +/// await build(args, (config, output) async { +/// if (config.linkModePreference == LinkModePreference.static) { +/// // Simulate that this script only supports dynamic libraries. +/// throw UnsupportedError( +/// 'LinkModePreference.static is not supported.', /// ); -/// if (config.dryRun) { -/// // Dry run invocations report assets for all architectures for that OS. -/// targets = Target.values.where( -/// (element) => element.os == config.targetOs, -/// ); -/// } else { -/// targets = [config.target]; +/// } +/// +/// final Iterable targets; +/// final packageName = config.packageName; +/// final assetPath = config.outDir.resolve(assetName); +/// final assetSourcePath = config.packageRoot.resolveUri(packageAssetPath); +/// if (config.dryRun) { +/// // Dry run invocations report assets for all architectures for that OS. +/// targets = Target.values.where( +/// (element) => element.os == config.targetOs, +/// ); +/// } else { +/// targets = [config.target]; +/// +/// // Insert code that downloads or builds the asset to `assetPath`. +/// await File.fromUri(assetSourcePath).copy(assetPath.toFilePath()); /// -/// // Insert code that downloads or builds the asset to `assetPath`. -/// } +/// output.addDependencies([ +/// assetSourcePath, +/// config.packageRoot.resolve('build.dart'), +/// ]); +/// } /// -/// for (final target in targets) { -/// output.addAssets([ -/// Asset( -/// id: 'library:${packageName}/src/some_file.dart', -/// linkMode: LinkMode.dynamic, -/// target: target, -/// path: AssetAbsolutePath(assetPath), -/// ) -/// ]); -/// } -/// }); +/// output.addAssets([ +/// for (final target in targets) +/// Asset( +/// id: 'library:$packageName/asset.txt', +/// linkMode: LinkMode.dynamic, +/// target: target, +/// path: AssetAbsolutePath(assetPath), +/// ) +/// ]); +/// }); +/// } /// ``` Future build( List commandlineArguments, From 09e5a3e17c6fa09266e994858286b008453126fa Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 16 Feb 2024 12:49:51 +0100 Subject: [PATCH 14/75] Use parts file for API --- .../lib/src/build_runner/build_runner.dart | 71 ++-- .../lib/src/model/kernel_assets.dart | 4 +- .../build_runner_dry_run_test.dart | 4 +- .../build_runner_reusability_test.dart | 20 +- .../build_runner_run_in_isolation_test.dart | 12 +- .../test/build_runner/helpers.dart | 22 +- .../packaging_preference_test.dart | 16 +- .../test/model/kernel_assets_test.dart | 14 +- .../test_data/wrong_build_output/build.dart | 4 +- .../test_data/wrong_build_output_2/build.dart | 4 +- .../test_data/wrong_build_output_3/build.dart | 4 +- .../lib/native_assets_cli.dart | 22 +- .../lib/native_assets_cli_internal.dart | 22 +- pkgs/native_assets_cli/lib/src/api/asset.dart | 36 +- .../lib/src/api/build_config.dart | 46 +-- .../lib/src/api/build_mode.dart | 6 +- .../lib/src/api/build_output.dart | 24 +- .../lib/src/api/ios_sdk.dart | 6 +- .../lib/src/api/link_mode.dart | 6 +- .../lib/src/api/link_mode_preference.dart | 11 +- .../native_assets_cli/lib/src/api/target.dart | 78 ++-- .../lib/src/model/asset.dart | 107 +++--- .../lib/src/model/build_config.dart | 199 +++++----- .../lib/src/model/build_mode.dart | 12 +- .../lib/src/model/build_output.dart | 56 ++- .../lib/src/model/ios_sdk.dart | 12 +- .../lib/src/model/link_mode.dart | 16 +- .../lib/src/model/link_mode_preference.dart | 37 +- .../lib/src/model/target.dart | 346 +++++++++--------- .../test/api/build_config_test.dart | 5 +- .../test/example/local_asset_test.dart | 14 +- .../test/example/native_add_library_test.dart | 14 +- pkgs/native_assets_cli/test/helpers.dart | 12 +- .../test/model/asset_test.dart | 59 +-- .../test/model/build_config_test.dart | 224 ++++++------ .../test/model/build_output_test.dart | 48 +-- .../test/model/target_test.dart | 39 +- 37 files changed, 813 insertions(+), 819 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 963155f3d..11ff277e7 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -36,12 +36,12 @@ class NativeAssetsBuildRunner { /// If provided, only native assets of all transitive dependencies of /// [runPackageName] are built. Future build({ - required LinkModePreference linkModePreference, - required Target target, + required LinkModePreferenceImpl linkModePreference, + required TargetImpl target, required Uri workingDirectory, - required BuildMode buildMode, - CCompilerConfig? cCompilerConfig, - IOSSdk? targetIOSSdk, + required BuildModeImpl buildMode, + CCompilerConfigImpl? cCompilerConfig, + IOSSdkImpl? targetIOSSdk, int? targetAndroidNdkApi, required bool includeParentEnvironment, PackageLayout? packageLayout, @@ -77,7 +77,7 @@ class NativeAssetsBuildRunner { buildPlan = plan; packageGraph = planner.packageGraph; } - final assets = []; + final assets = []; final dependencies = []; final metadata = {}; var success = true; @@ -132,8 +132,8 @@ class NativeAssetsBuildRunner { /// If provided, only native assets of all transitive dependencies of /// [runPackageName] are built. Future dryRun({ - required LinkModePreference linkModePreference, - required OS targetOs, + required LinkModePreferenceImpl linkModePreference, + required OSImpl targetOs, required Uri workingDirectory, required bool includeParentEnvironment, PackageLayout? packageLayout, @@ -163,7 +163,7 @@ class NativeAssetsBuildRunner { } buildPlan = plan; } - final assets = []; + final assets = []; var success = true; for (final package in buildPlan) { final config = await _cliConfigDryRun( @@ -190,7 +190,7 @@ class NativeAssetsBuildRunner { } Future<_PackageBuildRecord> _buildPackageCached( - BuildConfig config, + BuildConfigImpl config, Uri packageConfigUri, Uri workingDirectory, bool includeParentEnvironment, @@ -201,7 +201,7 @@ class NativeAssetsBuildRunner { await Directory.fromUri(outDir).create(recursive: true); } - final buildOutput = await BuildOutput.readFromFile(outDir: outDir); + final buildOutput = await BuildOutputImpl.readFromFile(outDir: outDir); final lastBuilt = buildOutput?.timestamp.roundDownToSeconds() ?? DateTime.fromMillisecondsSinceEpoch(0); final dependencies = buildOutput?.dependenciesModel; @@ -228,7 +228,7 @@ class NativeAssetsBuildRunner { } Future<_PackageBuildRecord> _buildPackage( - BuildConfig config, + BuildConfigImpl config, Uri packageConfigUri, Uri workingDirectory, bool includeParentEnvironment, { @@ -240,7 +240,8 @@ class NativeAssetsBuildRunner { final configFileContents = config.toYamlString(); logger.info('config.yaml contents: $configFileContents'); await File.fromUri(configFile).writeAsString(configFileContents); - final buildOutputFile = File.fromUri(outDir.resolve(BuildOutput.fileName)); + final buildOutputFile = + File.fromUri(outDir.resolve(BuildOutputImpl.fileName)); if (await buildOutputFile.exists()) { // Ensure we'll never read outdated build results. await buildOutputFile.delete(); @@ -282,7 +283,7 @@ ${result.stdout} } try { - final buildOutput = await BuildOutput.readFromFile(outDir: outDir); + final buildOutput = await BuildOutputImpl.readFromFile(outDir: outDir); final assets = buildOutput?.assets ?? []; success &= validateAssetsPackage(assets, config.packageName); final dependencies = buildOutput?.dependencies ?? []; @@ -295,7 +296,7 @@ build_output.yaml contained a format error. ${e.message} '''); success = false; - return ([], [], const Metadata({}), false); + return ([], [], const Metadata({}), false); // TODO(https://github.com/dart-lang/native/issues/109): Stop throwing // type errors in native_assets_cli, release a new version of that package // and then remove this. @@ -306,11 +307,11 @@ Building native assets for package:${config.packageName} failed. build_output.yaml contained a format error. '''); success = false; - return ([], [], const Metadata({}), false); + return ([], [], const Metadata({}), false); } finally { if (!success) { final buildOutputFile = - File.fromUri(outDir.resolve(BuildOutput.fileName)); + File.fromUri(outDir.resolve(BuildOutputImpl.fileName)); if (await buildOutputFile.exists()) { await buildOutputFile.delete(); } @@ -318,19 +319,19 @@ build_output.yaml contained a format error. } } - static Future _cliConfig({ + static Future _cliConfig({ required String packageName, required Uri packageRoot, - required Target target, - IOSSdk? targetIOSSdk, + required TargetImpl target, + IOSSdkImpl? targetIOSSdk, int? targetAndroidNdkApi, - required BuildMode buildMode, - required LinkModePreference linkMode, + required BuildModeImpl buildMode, + required LinkModePreferenceImpl linkMode, required Uri buildParentDir, - CCompilerConfig? cCompilerConfig, + CCompilerConfigImpl? cCompilerConfig, DependencyMetadata? dependencyMetadata, }) async { - final buildDirName = BuildConfig.checksum( + final buildDirName = BuildConfigImpl.checksum( packageName: packageName, packageRoot: packageRoot, targetOs: target.os, @@ -348,7 +349,7 @@ build_output.yaml contained a format error. // TODO(https://dartbug.com/50565): Purge old or unused folders. await outDir.create(recursive: true); } - return BuildConfig( + return BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: packageRoot, @@ -363,11 +364,11 @@ build_output.yaml contained a format error. ); } - static Future _cliConfigDryRun({ + static Future _cliConfigDryRun({ required String packageName, required Uri packageRoot, - required OS targetOs, - required LinkModePreference linkMode, + required OSImpl targetOs, + required LinkModePreferenceImpl linkMode, required Uri buildParentDir, }) async { final buildDirName = 'dry_run_${targetOs}_$linkMode'; @@ -376,7 +377,7 @@ build_output.yaml contained a format error. if (!await outDir.exists()) { await outDir.create(recursive: true); } - return BuildConfig.dryRun( + return BuildConfigImpl.dryRun( outDir: outDirUri, packageName: packageName, packageRoot: packageRoot, @@ -400,7 +401,7 @@ build_output.yaml contained a format error. }; } - bool validateAssetsPackage(Iterable assets, String packageName) { + bool validateAssetsPackage(Iterable assets, String packageName) { final invalidAssetIds = assets .map((a) => a.id) .where((n) => !n.startsWith('package:$packageName/')) @@ -419,7 +420,7 @@ build_output.yaml contained a format error. } typedef _PackageBuildRecord = ( - Iterable, + Iterable, Iterable dependencies, Metadata?, bool success, @@ -427,8 +428,8 @@ typedef _PackageBuildRecord = ( /// The result from a [NativeAssetsBuildRunner.dryRun]. abstract interface class DryRunResult { - /// The native assets for all [Target]s for the build or dry run. - List get assets; + /// The native assets for all [TargetImpl]s for the build or dry run. + List get assets; /// Whether all builds completed without errors. /// @@ -438,7 +439,7 @@ abstract interface class DryRunResult { final class _DryRunResultImpl implements DryRunResult { @override - final List assets; + final List assets; @override final bool success; @@ -462,7 +463,7 @@ abstract class BuildResult implements DryRunResult { final class _BuildResultImpl implements BuildResult { @override - final List assets; + final List assets; @override final List dependencies; diff --git a/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart b/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart index 14418e8a5..fad307de1 100644 --- a/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart +++ b/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart @@ -24,7 +24,7 @@ class KernelAssets { KernelAssets([Iterable? assets]) : _assets = [...?assets]; String toNativeAssetsFile() { - final assetsPerTarget = >{}; + final assetsPerTarget = >{}; for (final asset in _assets) { final assets = assetsPerTarget[asset.target] ?? []; assets.add(asset); @@ -47,7 +47,7 @@ class KernelAssets { class KernelAsset { final String id; - final Target target; + final TargetImpl target; final KernelAssetPath path; KernelAsset({ diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart index 057751469..b98659753 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart @@ -26,7 +26,7 @@ void main() async { final dryRunAssets = (await dryRun(packageUri, logger, dartExecutable)) .assets - .where((element) => element.target == Target.current) + .where((element) => element.target == TargetImpl.current) .toList(); final result = await build(packageUri, logger, dartExecutable); @@ -41,7 +41,7 @@ void main() async { } final dryRunDir = packageUri.resolve( - '.dart_tool/native_assets_builder/dry_run_${Target.current.os}_dynamic/'); + '.dart_tool/native_assets_builder/dry_run_${TargetImpl.current.os}_dynamic/'); expect(File.fromUri(dryRunDir.resolve('config.yaml')), exists); expect(File.fromUri(dryRunDir.resolve('out/build_output.yaml')), exists); // diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart index 0ee48ab31..5659ac16c 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart @@ -29,28 +29,28 @@ void main() async { ); await buildRunner.dryRun( - targetOs: Target.current.os, - linkModePreference: LinkModePreference.dynamic, + targetOs: TargetImpl.current.os, + linkModePreference: LinkModePreferenceImpl.dynamic, workingDirectory: packageUri, includeParentEnvironment: true, ); await buildRunner.dryRun( - targetOs: Target.current.os, - linkModePreference: LinkModePreference.dynamic, + targetOs: TargetImpl.current.os, + linkModePreference: LinkModePreferenceImpl.dynamic, workingDirectory: packageUri, includeParentEnvironment: true, ); await buildRunner.build( - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.dynamic, - target: Target.current, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.dynamic, + target: TargetImpl.current, workingDirectory: packageUri, includeParentEnvironment: true, ); await buildRunner.build( - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.dynamic, - target: Target.current, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.dynamic, + target: TargetImpl.current, workingDirectory: packageUri, includeParentEnvironment: true, ); diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_run_in_isolation_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_run_in_isolation_test.dart index ea7b05e3f..84e7b5264 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_run_in_isolation_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_run_in_isolation_test.dart @@ -14,12 +14,12 @@ const Timeout longTimeout = Timeout(Duration(minutes: 5)); void main() async { String unparseKey(String key) => key.replaceAll('.', '__').toUpperCase(); - final arKey = unparseKey(CCompilerConfig.arConfigKeyFull); - final ccKey = unparseKey(CCompilerConfig.ccConfigKeyFull); - final ldKey = unparseKey(CCompilerConfig.ldConfigKeyFull); - final envScriptKey = unparseKey(CCompilerConfig.envScriptConfigKeyFull); + final arKey = unparseKey(CCompilerConfigImpl.arConfigKeyFull); + final ccKey = unparseKey(CCompilerConfigImpl.ccConfigKeyFull); + final ldKey = unparseKey(CCompilerConfigImpl.ldConfigKeyFull); + final envScriptKey = unparseKey(CCompilerConfigImpl.envScriptConfigKeyFull); final envScriptArgsKey = - unparseKey(CCompilerConfig.envScriptArgsConfigKeyFull); + unparseKey(CCompilerConfigImpl.envScriptArgsConfigKeyFull); final cc = Platform.environment[ccKey]?.fileUri; @@ -52,7 +52,7 @@ void main() async { logger, dartExecutable, // Manually pass in a compiler. - cCompilerConfig: CCompilerConfig( + cCompilerConfig: CCompilerConfigImpl( ar: Platform.environment[arKey]?.fileUri, cc: cc, envScript: Platform.environment[envScriptKey]?.fileUri, diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index c21ec033e..498b92824 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -34,8 +34,8 @@ Future build( Uri packageUri, Logger logger, Uri dartExecutable, { - LinkModePreference linkModePreference = LinkModePreference.dynamic, - CCompilerConfig? cCompilerConfig, + LinkModePreferenceImpl linkModePreference = LinkModePreferenceImpl.dynamic, + CCompilerConfigImpl? cCompilerConfig, bool includeParentEnvironment = true, List? capturedLogs, PackageLayout? packageLayout, @@ -51,9 +51,9 @@ Future build( logger: logger, dartExecutable: dartExecutable, ).build( - buildMode: BuildMode.release, + buildMode: BuildModeImpl.release, linkModePreference: linkModePreference, - target: Target.current, + target: TargetImpl.current, workingDirectory: packageUri, cCompilerConfig: cCompilerConfig, includeParentEnvironment: includeParentEnvironment, @@ -75,8 +75,8 @@ Future dryRun( Uri packageUri, Logger logger, Uri dartExecutable, { - LinkModePreference linkModePreference = LinkModePreference.dynamic, - CCompilerConfig? cCompilerConfig, + LinkModePreferenceImpl linkModePreference = LinkModePreferenceImpl.dynamic, + CCompilerConfigImpl? cCompilerConfig, bool includeParentEnvironment = true, List? capturedLogs, PackageLayout? packageLayout, @@ -92,7 +92,7 @@ Future dryRun( dartExecutable: dartExecutable, ).dryRun( linkModePreference: linkModePreference, - targetOs: Target.current.os, + targetOs: TargetImpl.current.os, workingDirectory: packageUri, includeParentEnvironment: includeParentEnvironment, packageLayout: packageLayout, @@ -105,9 +105,9 @@ Future dryRun( return result; } -Future expectAssetsExist(List assets) async { +Future expectAssetsExist(List assets) async { for (final asset in assets) { - final uri = (asset.path as AssetAbsolutePath).uri; + final uri = (asset.path as AssetAbsolutePathImpl).uri; expect( uri.toFilePath(), contains('${Platform.pathSeparator}.dart_tool${Platform.pathSeparator}' @@ -118,11 +118,11 @@ Future expectAssetsExist(List assets) async { } Future expectSymbols({ - required Asset asset, + required AssetImpl asset, required List symbols, }) async { if (Platform.isLinux) { - final assetUri = (asset.path as AssetAbsolutePath).uri; + final assetUri = (asset.path as AssetAbsolutePathImpl).uri; final nmResult = await runProcess( executable: Uri(path: 'nm'), arguments: [ diff --git a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart index 7db076c08..461bf5fc8 100644 --- a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart @@ -26,35 +26,35 @@ void main() async { packageUri, logger, dartExecutable, - linkModePreference: LinkModePreference.dynamic, + linkModePreference: LinkModePreferenceImpl.dynamic, ); final resultPreferDynamic = await build( packageUri, logger, dartExecutable, - linkModePreference: LinkModePreference.preferDynamic, + linkModePreference: LinkModePreferenceImpl.preferDynamic, ); final resultStatic = await build( packageUri, logger, dartExecutable, - linkModePreference: LinkModePreference.static, + linkModePreference: LinkModePreferenceImpl.static, ); final resultPreferStatic = await build( packageUri, logger, dartExecutable, - linkModePreference: LinkModePreference.preferStatic, + linkModePreference: LinkModePreferenceImpl.preferStatic, ); // This package honors preferences. - expect(resultDynamic.assets.single.linkMode, LinkMode.dynamic); - expect(resultPreferDynamic.assets.single.linkMode, LinkMode.dynamic); - expect(resultStatic.assets.single.linkMode, LinkMode.static); - expect(resultPreferStatic.assets.single.linkMode, LinkMode.static); + expect(resultDynamic.assets.single.linkMode, LinkModeImpl.dynamic); + expect(resultPreferDynamic.assets.single.linkMode, LinkModeImpl.dynamic); + expect(resultStatic.assets.single.linkMode, LinkModeImpl.static); + expect(resultPreferStatic.assets.single.linkMode, LinkModeImpl.static); }); }); } diff --git a/pkgs/native_assets_builder/test/model/kernel_assets_test.dart b/pkgs/native_assets_builder/test/model/kernel_assets_test.dart index 8b8e92b6b..a2a951b0f 100644 --- a/pkgs/native_assets_builder/test/model/kernel_assets_test.dart +++ b/pkgs/native_assets_builder/test/model/kernel_assets_test.dart @@ -18,37 +18,37 @@ void main() { KernelAsset( id: 'foo', path: KernelAssetAbsolutePath(fooUri), - target: Target.androidX64, + target: TargetImpl.androidX64, ), KernelAsset( id: 'foo2', path: KernelAssetRelativePath(foo2Uri), - target: Target.androidX64, + target: TargetImpl.androidX64, ), KernelAsset( id: 'foo3', path: KernelAssetSystemPath(foo3Uri), - target: Target.androidX64, + target: TargetImpl.androidX64, ), KernelAsset( id: 'foo4', path: KernelAssetInExecutable(), - target: Target.androidX64, + target: TargetImpl.androidX64, ), KernelAsset( id: 'foo5', path: KernelAssetInProcess(), - target: Target.androidX64, + target: TargetImpl.androidX64, ), KernelAsset( id: 'bar', path: KernelAssetAbsolutePath(barUri), - target: Target.linuxArm64, + target: TargetImpl.linuxArm64, ), KernelAsset( id: 'bla', path: KernelAssetAbsolutePath(blaUri), - target: Target.windowsX64, + target: TargetImpl.windowsX64, ), ]); diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart index 8e19fd899..dd16fa64d 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart @@ -7,8 +7,8 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - await File.fromUri(buildConfig.outDir.resolve(BuildOutput.fileName)) + final buildConfig = await BuildConfigImpl.fromArgs(args); + await File.fromUri(buildConfig.outDir.resolve(BuildOutputImpl.fileName)) .writeAsString(_wrongContents); } diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart index de6e65daf..f09a82313 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart @@ -7,8 +7,8 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - await File.fromUri(buildConfig.outDir.resolve(BuildOutput.fileName)) + final buildConfig = await BuildConfigImpl.fromArgs(args); + await File.fromUri(buildConfig.outDir.resolve(BuildOutputImpl.fileName)) .writeAsString(_wrongContents); } diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart index 33f9dfc01..4483cf8ce 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart @@ -7,8 +7,8 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - await File.fromUri(buildConfig.outDir.resolve(BuildOutput.fileName)) + final buildConfig = await BuildConfigImpl.fromArgs(args); + await File.fromUri(buildConfig.outDir.resolve(BuildOutputImpl.fileName)) .writeAsString(_rightContents); exit(1); } diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 7df4c533a..e5d7fe8c9 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -6,12 +6,18 @@ /// native assets CLI, e.g. a `build.dart` script. library native_assets_cli; -export 'src/api/asset.dart'; +export 'src/api/asset.dart' + show + Asset, + AssetAbsolutePath, + AssetInExecutable, + AssetInProcess, + AssetSystemPath; export 'src/api/build.dart'; -export 'src/api/build_config.dart'; -export 'src/api/build_mode.dart'; -export 'src/api/build_output.dart'; -export 'src/api/ios_sdk.dart'; -export 'src/api/link_mode.dart'; -export 'src/api/link_mode_preference.dart'; -export 'src/api/target.dart'; +export 'src/api/build_config.dart' show BuildConfig, CCompilerConfig; +export 'src/api/build_mode.dart' show BuildMode; +export 'src/api/build_output.dart' show BuildOutput; +export 'src/api/ios_sdk.dart' show IOSSdk; +export 'src/api/link_mode.dart' show LinkMode; +export 'src/api/link_mode_preference.dart' show LinkModePreference; +export 'src/api/target.dart' show Architecture, OS, Target; diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index 9bf58c114..74f790ec5 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -8,13 +8,19 @@ /// @nodoc library native_assets_cli_internal; -export 'src/model/asset.dart'; -export 'src/model/build_config.dart'; -export 'src/model/build_mode.dart'; -export 'src/model/build_output.dart'; +export 'src/api/asset.dart' + show + AssetAbsolutePathImpl, + AssetImpl, + AssetInExecutableImpl, + AssetInProcessImpl, + AssetSystemPathImpl; +export 'src/api/build_config.dart' show BuildConfigImpl, CCompilerConfigImpl; +export 'src/api/build_mode.dart' show BuildModeImpl; +export 'src/api/build_output.dart' show BuildOutputImpl; +export 'src/api/ios_sdk.dart' show IOSSdkImpl; +export 'src/api/link_mode.dart' show LinkModeImpl; +export 'src/api/link_mode_preference.dart' show LinkModePreferenceImpl; +export 'src/api/target.dart' show ArchitectureImpl, OSImpl, TargetImpl; export 'src/model/dependencies.dart'; -export 'src/model/ios_sdk.dart'; -export 'src/model/link_mode.dart'; -export 'src/model/link_mode_preference.dart'; export 'src/model/metadata.dart'; -export 'src/model/target.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index 1d10d644c..83a9984c6 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -2,43 +2,45 @@ // 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 '../model/asset.dart' as model; -import '../model/link_mode.dart' as model; -import '../model/target.dart' as model; +import 'package:yaml/yaml.dart'; + +import '../utils/yaml.dart'; import 'link_mode.dart'; import 'target.dart'; -abstract class AssetPath {} +part '../model/asset.dart'; + +abstract final class AssetPath {} /// Asset at absolute path [uri]. -abstract class AssetAbsolutePath implements AssetPath { +abstract final class AssetAbsolutePath implements AssetPath { Uri get uri; - factory AssetAbsolutePath(Uri uri) = model.AssetAbsolutePath; + factory AssetAbsolutePath(Uri uri) = AssetAbsolutePathImpl; } /// Asset is avaliable on the system `PATH`. /// /// [uri] only contains a file name. -abstract class AssetSystemPath implements AssetPath { +abstract final class AssetSystemPath implements AssetPath { Uri get uri; - factory AssetSystemPath(Uri uri) = model.AssetSystemPath; + factory AssetSystemPath(Uri uri) = AssetSystemPathImpl; } /// Asset is loaded in the process and symbols are available through /// `DynamicLibrary.process()`. -abstract class AssetInProcess implements AssetPath { - factory AssetInProcess() = model.AssetInProcess; +abstract final class AssetInProcess implements AssetPath { + factory AssetInProcess() = AssetInProcessImpl; } /// Asset is embedded in executable and symbols are available through /// `DynamicLibrary.executable()`. -abstract class AssetInExecutable implements AssetPath { - factory AssetInExecutable() = model.AssetInExecutable; +abstract final class AssetInExecutable implements AssetPath { + factory AssetInExecutable() = AssetInExecutableImpl; } -abstract class Asset { +abstract final class Asset { LinkMode get linkMode; String get id; Target get target; @@ -50,10 +52,10 @@ abstract class Asset { required Target target, required AssetPath path, }) => - model.Asset( + AssetImpl( id: id, - linkMode: linkMode as model.LinkMode, - target: target as model.Target, - path: path as model.AssetPath, + linkMode: linkMode as LinkModeImpl, + target: target as TargetImpl, + path: path as AssetPathImpl, ); } diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 3b49b8c32..1165abf65 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -2,29 +2,31 @@ // 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:convert'; import 'dart:io'; import 'package:cli_config/cli_config.dart'; +import 'package:collection/collection.dart'; +import 'package:crypto/crypto.dart'; import 'package:pub_semver/pub_semver.dart'; -import '../model/build_config.dart' as model; -import '../model/build_mode.dart' as model; -import '../model/ios_sdk.dart' as model; -import '../model/link_mode_preference.dart' as model; -import '../model/metadata.dart' as model; -import '../model/target.dart' as model; +import '../model/metadata.dart'; +import '../utils/map.dart'; +import '../utils/yaml.dart'; import 'build_mode.dart'; import 'ios_sdk.dart'; import 'link_mode_preference.dart'; import 'target.dart'; +part '../model/build_config.dart'; + /// The configuration for a `build.dart` invocation. /// /// A package can choose to have a toplevel `build.dart` script. If such a /// script exists, it will be automatically run, by the Flutter and Dart SDK /// tools. The script will then be run with specific commandline arguments, /// which [BuildConfig] can parse and provide more convenient access to. -abstract class BuildConfig { +abstract final class BuildConfig { /// The folder in which all output and intermediate artifacts should be /// placed. Uri get outDir; @@ -109,7 +111,7 @@ abstract class BuildConfig { /// If we ever were to make breaking changes, it would be useful to give /// proper error messages rather than just fail to parse the YAML /// representation in the protocol. - static Version get version => model.BuildConfig.version; + static Version get version => BuildConfigImpl.version; factory BuildConfig({ required Uri outDir, @@ -124,21 +126,21 @@ abstract class BuildConfig { required LinkModePreference linkModePreference, Map>? dependencyMetadata, }) => - model.BuildConfig( + BuildConfigImpl( outDir: outDir, packageName: packageName, packageRoot: packageRoot, - buildMode: buildMode as model.BuildMode, - targetArchitecture: targetArchitecture as model.Architecture, - targetOs: targetOs as model.OS, - targetIOSSdk: targetIOSSdk as model.IOSSdk?, + buildMode: buildMode as BuildModeImpl, + targetArchitecture: targetArchitecture as ArchitectureImpl, + targetOs: targetOs as OSImpl, + targetIOSSdk: targetIOSSdk as IOSSdkImpl?, targetAndroidNdkApi: targetAndroidNdkApi, - cCompiler: cCompiler as model.CCompilerConfig?, - linkModePreference: linkModePreference as model.LinkModePreference, + cCompiler: cCompiler as CCompilerConfigImpl?, + linkModePreference: linkModePreference as LinkModePreferenceImpl, dependencyMetadata: dependencyMetadata != null ? { for (final entry in dependencyMetadata.entries) - entry.key: model.Metadata(entry.value.cast()) + entry.key: Metadata(entry.value.cast()) } : {}, ); @@ -150,16 +152,16 @@ abstract class BuildConfig { required OS targetOs, required LinkModePreference linkModePreference, }) => - model.BuildConfig.dryRun( + BuildConfigImpl.dryRun( outDir: outDir, packageName: packageName, packageRoot: packageRoot, - targetOs: targetOs as model.OS, - linkModePreference: linkModePreference as model.LinkModePreference, + targetOs: targetOs as OSImpl, + linkModePreference: linkModePreference as LinkModePreferenceImpl, ); factory BuildConfig.fromConfig(Config config) => - model.BuildConfig.fromConfig(config); + BuildConfigImpl.fromConfig(config); /// Constructs a config by parsing CLI arguments and loading the config file. /// @@ -178,7 +180,7 @@ abstract class BuildConfig { Map? environment, Uri? workingDirectory, }) => - model.BuildConfig.fromArgs( + BuildConfigImpl.fromArgs( args, environment: environment, workingDirectory: workingDirectory, @@ -207,5 +209,5 @@ abstract class CCompilerConfig { Uri? ld, Uri? envScript, List? envScriptArgs, - }) = model.CCompilerConfig; + }) = CCompilerConfigImpl; } diff --git a/pkgs/native_assets_cli/lib/src/api/build_mode.dart b/pkgs/native_assets_cli/lib/src/api/build_mode.dart index f3efc3efb..7a7f5e68a 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_mode.dart @@ -2,13 +2,13 @@ // 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 '../model/build_mode.dart' as model; +part '../model/build_mode.dart'; abstract class BuildMode { String get name; - static const BuildMode debug = model.BuildMode.debug; - static const BuildMode release = model.BuildMode.release; + static const BuildMode debug = BuildModeImpl.debug; + static const BuildMode release = BuildModeImpl.release; static const values = [ debug, diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 2fc9a0384..c0adb8c9c 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -4,15 +4,21 @@ import 'dart:io'; +import 'package:collection/collection.dart'; import 'package:pub_semver/pub_semver.dart'; +import 'package:yaml/yaml.dart'; -import '../model/asset.dart' as model; -import '../model/build_output.dart' as model; -import '../model/dependencies.dart' as model; -import '../model/metadata.dart' as model; +import '../model/dependencies.dart'; +import '../model/metadata.dart'; +import '../utils/datetime.dart'; +import '../utils/file.dart'; +import '../utils/map.dart'; +import '../utils/yaml.dart'; import 'asset.dart'; import 'target.dart'; +part '../model/build_output.dart'; + /// The output of a `build.dart` invocation. /// /// A package can choose to have a toplevel `build.dart` script. If such a @@ -66,11 +72,11 @@ abstract class BuildOutput { Iterable? dependencies, Map? metadata, }) => - model.BuildOutput( + BuildOutputImpl( timestamp: timestamp, - assets: assets?.cast().toList(), - dependencies: model.Dependencies([...?dependencies]), - metadata: model.Metadata({...?metadata}), + assets: assets?.cast().toList(), + dependencies: Dependencies([...?dependencies]), + metadata: Metadata({...?metadata}), ); /// Adds [Asset]s produced by this build or dry run. @@ -113,7 +119,7 @@ abstract class BuildOutput { /// If we ever were to make breaking changes, it would be useful to give /// proper error messages rather than just fail to parse the YAML /// representation in the protocol. - static Version get version => model.BuildOutput.version; + static Version get version => BuildOutputImpl.version; /// Write out this build output to a file inside [outDir]. Future writeToFile({required Uri outDir}); diff --git a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart index b76afbd09..8b1679000 100644 --- a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart +++ b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart @@ -2,14 +2,14 @@ // 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 '../model/ios_sdk.dart' as model; +part '../model/ios_sdk.dart'; /// For an iOS target, a build is either done for the device or the simulator. /// /// Only fat binaries or xcframeworks can contain both targets. abstract class IOSSdk { - static const IOSSdk iPhoneOs = model.IOSSdk.iPhoneOs; - static const IOSSdk iPhoneSimulator = model.IOSSdk.iPhoneSimulator; + static const IOSSdk iPhoneOs = IOSSdkImpl.iPhoneOs; + static const IOSSdk iPhoneSimulator = IOSSdkImpl.iPhoneSimulator; static const values = [ iPhoneOs, diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode.dart b/pkgs/native_assets_cli/lib/src/api/link_mode.dart index d389cf751..701471880 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode.dart @@ -2,11 +2,11 @@ // 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 '../model/link_mode.dart' as model; +part '../model/link_mode.dart'; abstract class LinkMode { - static const LinkMode dynamic = model.LinkMode.dynamic; - static const LinkMode static = model.LinkMode.static; + static const LinkMode dynamic = LinkModeImpl.dynamic; + static const LinkMode static = LinkModeImpl.static; /// Known values for [LinkMode]. static const List values = [ diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart index 8224e4d40..a7ce492e5 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart @@ -2,9 +2,10 @@ // 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 '../model/link_mode_preference.dart' as model; import 'link_mode.dart'; +part '../model/link_mode_preference.dart'; + abstract class LinkModePreference { String get name; @@ -16,20 +17,20 @@ abstract class LinkModePreference { /// /// Fails if not all native assets can only be provided as static library. /// Required to run Dart in JIT mode. - static const LinkModePreference dynamic = model.LinkModePreference.dynamic; + static const LinkModePreference dynamic = LinkModePreferenceImpl.dynamic; /// Provide native assets as static libraries. /// /// Fails if not all native assets can only be provided as dynamic library. /// Required for potential link-time tree-shaking of native code. /// Therefore, preferred to in Dart AOT mode. - static const LinkModePreference static = model.LinkModePreference.static; + static const LinkModePreference static = LinkModePreferenceImpl.static; /// Provide native assets as dynamic libraries, if possible. /// /// Otherwise, build native assets as static libraries static const LinkModePreference preferDynamic = - model.LinkModePreference.preferDynamic; + LinkModePreferenceImpl.preferDynamic; /// Provide native assets as static libraries, if possible. /// @@ -37,7 +38,7 @@ abstract class LinkModePreference { /// compilation, if there are any native assets which can only be provided as /// dynamic libraries. static const LinkModePreference preferStatic = - model.LinkModePreference.preferStatic; + LinkModePreferenceImpl.preferStatic; static const values = [ dynamic, diff --git a/pkgs/native_assets_cli/lib/src/api/target.dart b/pkgs/native_assets_cli/lib/src/api/target.dart index dbbec8a88..72ad1ef7e 100644 --- a/pkgs/native_assets_cli/lib/src/api/target.dart +++ b/pkgs/native_assets_cli/lib/src/api/target.dart @@ -2,19 +2,21 @@ // 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' show Abi; import 'dart:io'; -import '../model/target.dart' as model; import 'link_mode.dart'; +part '../model/target.dart'; + /// The hardware architectures the Dart VM runs on. abstract class Architecture { - static const Architecture arm = model.Architecture.arm; - static const Architecture arm64 = model.Architecture.arm64; - static const Architecture ia32 = model.Architecture.ia32; - static const Architecture riscv32 = model.Architecture.riscv32; - static const Architecture riscv64 = model.Architecture.riscv64; - static const Architecture x64 = model.Architecture.x64; + static const Architecture arm = ArchitectureImpl.arm; + static const Architecture arm64 = ArchitectureImpl.arm64; + static const Architecture ia32 = ArchitectureImpl.ia32; + static const Architecture riscv32 = ArchitectureImpl.riscv32; + static const Architecture riscv64 = ArchitectureImpl.riscv64; + static const Architecture x64 = ArchitectureImpl.x64; /// Known values for [Architecture]. static const List values = [ @@ -29,17 +31,17 @@ abstract class Architecture { /// The current [Architecture]. /// /// Read from the [Platform.version] string. - static Architecture get current => model.Architecture.current; + static Architecture get current => ArchitectureImpl.current; } /// The operating systems the Dart VM runs on. abstract class OS { - static const OS android = model.OS.android; - static const OS fuchsia = model.OS.fuchsia; - static const OS iOS = model.OS.iOS; - static const OS linux = model.OS.linux; - static const OS macOS = model.OS.macOS; - static const OS windows = model.OS.windows; + static const OS android = OSImpl.android; + static const OS fuchsia = OSImpl.fuchsia; + static const OS iOS = OSImpl.iOS; + static const OS linux = OSImpl.linux; + static const OS macOS = OSImpl.macOS; + static const OS windows = OSImpl.windows; /// Known values for [OS]. static const List values = [ @@ -65,45 +67,45 @@ abstract class OS { /// The current [OS]. /// /// Read from the [Platform.version] string. - static OS get current => model.OS.current; + static OS get current => OSImpl.current; } /// Application binary interface. /// /// The Dart VM can run on a variety of [Target]s, see [Target.values]. abstract class Target implements Comparable { - static const Target androidArm = model.Target.androidArm; - static const Target androidArm64 = model.Target.androidArm64; - static const Target androidIA32 = model.Target.androidIA32; - static const Target androidX64 = model.Target.androidX64; - static const Target androidRiscv64 = model.Target.androidRiscv64; - static const Target fuchsiaArm64 = model.Target.fuchsiaArm64; - static const Target fuchsiaX64 = model.Target.fuchsiaX64; - static const Target iOSArm = model.Target.iOSArm; - static const Target iOSArm64 = model.Target.iOSArm64; - static const Target iOSX64 = model.Target.iOSX64; - static const Target linuxArm = model.Target.linuxArm; - static const Target linuxArm64 = model.Target.linuxArm64; - static const Target linuxIA32 = model.Target.linuxIA32; - static const Target linuxRiscv32 = model.Target.linuxRiscv32; - static const Target linuxRiscv64 = model.Target.linuxRiscv64; - static const Target linuxX64 = model.Target.linuxX64; - static const Target macOSArm64 = model.Target.macOSArm64; - static const Target macOSX64 = model.Target.macOSX64; - static const Target windowsArm64 = model.Target.windowsArm64; - static const Target windowsIA32 = model.Target.windowsIA32; - static const Target windowsX64 = model.Target.windowsX64; + static const Target androidArm = TargetImpl.androidArm; + static const Target androidArm64 = TargetImpl.androidArm64; + static const Target androidIA32 = TargetImpl.androidIA32; + static const Target androidX64 = TargetImpl.androidX64; + static const Target androidRiscv64 = TargetImpl.androidRiscv64; + static const Target fuchsiaArm64 = TargetImpl.fuchsiaArm64; + static const Target fuchsiaX64 = TargetImpl.fuchsiaX64; + static const Target iOSArm = TargetImpl.iOSArm; + static const Target iOSArm64 = TargetImpl.iOSArm64; + static const Target iOSX64 = TargetImpl.iOSX64; + static const Target linuxArm = TargetImpl.linuxArm; + static const Target linuxArm64 = TargetImpl.linuxArm64; + static const Target linuxIA32 = TargetImpl.linuxIA32; + static const Target linuxRiscv32 = TargetImpl.linuxRiscv32; + static const Target linuxRiscv64 = TargetImpl.linuxRiscv64; + static const Target linuxX64 = TargetImpl.linuxX64; + static const Target macOSArm64 = TargetImpl.macOSArm64; + static const Target macOSX64 = TargetImpl.macOSX64; + static const Target windowsArm64 = TargetImpl.windowsArm64; + static const Target windowsIA32 = TargetImpl.windowsIA32; + static const Target windowsX64 = TargetImpl.windowsX64; /// All Targets that native assets can be built for. /// /// Note that for some of these a Dart SDK is not available and they are only /// used as target architectures for Flutter apps. - static const Set values = model.Target.values; + static const Set values = TargetImpl.values; /// The current [Target]. /// /// Read from the [Platform.version] string. - static Target get current => model.Target.current; + static Target get current => TargetImpl.current; Architecture get architecture; diff --git a/pkgs/native_assets_cli/lib/src/model/asset.dart b/pkgs/native_assets_cli/lib/src/model/asset.dart index babbed31a..53bfd508f 100644 --- a/pkgs/native_assets_cli/lib/src/model/asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/asset.dart @@ -2,33 +2,28 @@ // 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:yaml/yaml.dart'; +part of '../api/asset.dart'; -import '../api/asset.dart' as api; -import '../utils/yaml.dart'; -import 'link_mode.dart'; -import 'target.dart'; - -abstract class AssetPath implements api.AssetPath { - factory AssetPath(String pathType, Uri? uri) { +abstract final class AssetPathImpl implements AssetPath { + factory AssetPathImpl(String pathType, Uri? uri) { switch (pathType) { - case AssetAbsolutePath._pathTypeValue: - return AssetAbsolutePath(uri!); - case AssetSystemPath._pathTypeValue: - return AssetSystemPath(uri!); - case AssetInExecutable._pathTypeValue: - return AssetInExecutable(); - case AssetInProcess._pathTypeValue: - return AssetInProcess(); + case AssetAbsolutePathImpl._pathTypeValue: + return AssetAbsolutePathImpl(uri!); + case AssetSystemPathImpl._pathTypeValue: + return AssetSystemPathImpl(uri!); + case AssetInExecutableImpl._pathTypeValue: + return AssetInExecutableImpl(); + case AssetInProcessImpl._pathTypeValue: + return AssetInProcessImpl(); } throw FormatException('Unknown pathType: $pathType.'); } - factory AssetPath.fromYaml(YamlMap yamlMap) { + factory AssetPathImpl.fromYaml(YamlMap yamlMap) { final pathType = as(yamlMap[_pathTypeKey]); final uriString = as(yamlMap[_uriKey]); final uri = uriString != null ? Uri(path: uriString) : null; - return AssetPath(pathType, uri); + return AssetPathImpl(pathType, uri); } Map toYaml(); @@ -38,18 +33,18 @@ abstract class AssetPath implements api.AssetPath { } /// Asset at absolute path [uri]. -class AssetAbsolutePath implements AssetPath, api.AssetAbsolutePath { +final class AssetAbsolutePathImpl implements AssetPathImpl, AssetAbsolutePath { @override final Uri uri; - AssetAbsolutePath(this.uri); + AssetAbsolutePathImpl(this.uri); static const _pathTypeValue = 'absolute'; @override Map toYaml() => { - AssetPath._pathTypeKey: _pathTypeValue, - AssetPath._uriKey: uri.toFilePath(), + AssetPathImpl._pathTypeKey: _pathTypeValue, + AssetPathImpl._uriKey: uri.toFilePath(), }; @override @@ -57,7 +52,7 @@ class AssetAbsolutePath implements AssetPath, api.AssetAbsolutePath { @override bool operator ==(Object other) { - if (other is! AssetAbsolutePath) { + if (other is! AssetAbsolutePathImpl) { return false; } return uri == other.uri; @@ -67,18 +62,18 @@ class AssetAbsolutePath implements AssetPath, api.AssetAbsolutePath { /// Asset is avaliable on the system `PATH`. /// /// [uri] only contains a file name. -class AssetSystemPath implements AssetPath, api.AssetSystemPath { +final class AssetSystemPathImpl implements AssetPathImpl, AssetSystemPath { @override final Uri uri; - AssetSystemPath(this.uri); + AssetSystemPathImpl(this.uri); static const _pathTypeValue = 'system'; @override Map toYaml() => { - AssetPath._pathTypeKey: _pathTypeValue, - AssetPath._uriKey: uri.toFilePath(), + AssetPathImpl._pathTypeKey: _pathTypeValue, + AssetPathImpl._uriKey: uri.toFilePath(), }; @override @@ -86,7 +81,7 @@ class AssetSystemPath implements AssetPath, api.AssetSystemPath { @override bool operator ==(Object other) { - if (other is! AssetSystemPath) { + if (other is! AssetSystemPathImpl) { return false; } return uri == other.uri; @@ -95,85 +90,85 @@ class AssetSystemPath implements AssetPath, api.AssetSystemPath { /// Asset is loaded in the process and symbols are available through /// `DynamicLibrary.process()`. -class AssetInProcess implements AssetPath, api.AssetInProcess { - AssetInProcess._(); +final class AssetInProcessImpl implements AssetPathImpl, AssetInProcess { + AssetInProcessImpl._(); - static final AssetInProcess _singleton = AssetInProcess._(); + static final AssetInProcessImpl _singleton = AssetInProcessImpl._(); - factory AssetInProcess() => _singleton; + factory AssetInProcessImpl() => _singleton; static const _pathTypeValue = 'process'; @override Map toYaml() => { - AssetPath._pathTypeKey: _pathTypeValue, + AssetPathImpl._pathTypeKey: _pathTypeValue, }; } /// Asset is embedded in executable and symbols are available through /// `DynamicLibrary.executable()`. -class AssetInExecutable implements AssetPath, api.AssetInExecutable { - AssetInExecutable._(); +final class AssetInExecutableImpl implements AssetPathImpl, AssetInExecutable { + AssetInExecutableImpl._(); - static final AssetInExecutable _singleton = AssetInExecutable._(); + static final AssetInExecutableImpl _singleton = AssetInExecutableImpl._(); - factory AssetInExecutable() => _singleton; + factory AssetInExecutableImpl() => _singleton; static const _pathTypeValue = 'executable'; @override Map toYaml() => { - AssetPath._pathTypeKey: _pathTypeValue, + AssetPathImpl._pathTypeKey: _pathTypeValue, }; } -class Asset implements api.Asset { +final class AssetImpl implements Asset { @override - final LinkMode linkMode; + final LinkModeImpl linkMode; @override final String id; @override final Target target; @override - final AssetPath path; + final AssetPathImpl path; - Asset({ + AssetImpl({ required this.id, required this.linkMode, required this.target, required this.path, }); - factory Asset.fromYaml(YamlMap yamlMap) => Asset( + factory AssetImpl.fromYaml(YamlMap yamlMap) => AssetImpl( id: as(yamlMap[_idKey]), - path: AssetPath.fromYaml(as(yamlMap[_pathKey])), - target: Target.fromString(as(yamlMap[_targetKey])), - linkMode: LinkMode.fromName(as(yamlMap[_linkModeKey])), + path: AssetPathImpl.fromYaml(as(yamlMap[_pathKey])), + target: TargetImpl.fromString(as(yamlMap[_targetKey])), + linkMode: LinkModeImpl.fromName(as(yamlMap[_linkModeKey])), ); - static List listFromYamlString(String yaml) { + static List listFromYamlString(String yaml) { final yamlObject = loadYaml(yaml); if (yamlObject == null) { return []; } return [ for (final yamlElement in as(yamlObject)) - Asset.fromYaml(as(yamlElement)), + AssetImpl.fromYaml(as(yamlElement)), ]; } - static List listFromYamlList(YamlList yamlList) => [ + static List listFromYamlList(YamlList yamlList) => [ for (final yamlElement in yamlList) - Asset.fromYaml(as(yamlElement)), + AssetImpl.fromYaml(as(yamlElement)), ]; - Asset copyWith({ - LinkMode? linkMode, + AssetImpl copyWith({ + LinkModeImpl? linkMode, String? id, Target? target, - AssetPath? path, + AssetPathImpl? path, }) => - Asset( + AssetImpl( id: id ?? this.id, linkMode: linkMode ?? this.linkMode, target: target ?? this.target, @@ -182,7 +177,7 @@ class Asset implements api.Asset { @override bool operator ==(Object other) { - if (other is! Asset) { + if (other is! AssetImpl) { return false; } return other.id == id && @@ -212,7 +207,7 @@ class Asset implements api.Asset { String toString() => 'Asset(${toYaml()})'; } -extension AssetIterable on Iterable { +extension AssetIterable on Iterable { List toYaml() => [for (final item in this) item.toYaml()]; String toYamlString() => yamlEncode(toYaml()); diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index cc476ad26..a962c4d55 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -2,24 +2,9 @@ // 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:convert'; -import 'dart:io'; - -import 'package:cli_config/cli_config.dart'; -import 'package:collection/collection.dart'; -import 'package:crypto/crypto.dart'; -import 'package:pub_semver/pub_semver.dart'; - -import '../api/build_config.dart' as api; -import '../utils/map.dart'; -import '../utils/yaml.dart'; -import 'build_mode.dart'; -import 'ios_sdk.dart'; -import 'link_mode_preference.dart'; -import 'metadata.dart'; -import 'target.dart'; - -class BuildConfig implements api.BuildConfig { +part of '../api/build_config.dart'; + +final class BuildConfigImpl implements BuildConfig { /// The folder in which all output and intermediate artifacts should be /// placed. @override @@ -43,42 +28,42 @@ class BuildConfig implements api.BuildConfig { /// /// Not available during a [dryRun]. @override - late final Target target = - Target.fromArchitectureAndOs(targetArchitecture, targetOs); + late final TargetImpl target = + TargetImpl.fromArchitectureAndOs(targetArchitecture, targetOs); /// The architecture being compiled for. /// /// Not available during a [dryRun]. @override - Architecture get targetArchitecture { + ArchitectureImpl get targetArchitecture { _ensureNotDryRun(); return _targetArchitecture; } - late final Architecture _targetArchitecture; + late final ArchitectureImpl _targetArchitecture; /// The operating system being compiled for. @override - OS get targetOs => _targetOs; - late final OS _targetOs; + OSImpl get targetOs => _targetOs; + late final OSImpl _targetOs; /// When compiling for iOS, whether to target device or simulator. /// - /// Required when [targetOs] equals [OS.iOS]. + /// Required when [targetOs] equals [OSImpl.iOS]. /// /// Not available during a [dryRun]. @override - IOSSdk? get targetIOSSdk { + IOSSdkImpl? get targetIOSSdk { _ensureNotDryRun(); return _targetIOSSdk; } - late final IOSSdk? _targetIOSSdk; + late final IOSSdkImpl? _targetIOSSdk; /// When compiling for Android, the minimum Android SDK API version to that /// the compiled code will be compatible with. /// - /// Required when [targetOs] equals [OS.android]. + /// Required when [targetOs] equals [OSImpl.android]. /// /// Not available during a [dryRun]. /// @@ -95,8 +80,8 @@ class BuildConfig implements api.BuildConfig { /// Preferred linkMode method for library. @override - LinkModePreference get linkModePreference => _linkModePreference; - late final LinkModePreference _linkModePreference; + LinkModePreferenceImpl get linkModePreference => _linkModePreference; + late final LinkModePreferenceImpl _linkModePreference; @override Object? metadatum(String packageName, String key) { @@ -110,12 +95,12 @@ class BuildConfig implements api.BuildConfig { /// /// Not available during a [dryRun]. @override - CCompilerConfig get cCompiler { + CCompilerConfigImpl get cCompiler { _ensureNotDryRun(); return _cCompiler; } - late final CCompilerConfig _cCompiler; + late final CCompilerConfigImpl _cCompiler; /// Don't run the build, only report the native assets produced. @override @@ -126,31 +111,31 @@ class BuildConfig implements api.BuildConfig { /// /// Not available during a [dryRun]. @override - BuildMode get buildMode { + BuildModeImpl get buildMode { _ensureNotDryRun(); return _buildMode; } - late final BuildMode _buildMode; + late final BuildModeImpl _buildMode; @override Config get config => _config; late final Config _config; - factory BuildConfig({ + factory BuildConfigImpl({ required Uri outDir, required String packageName, required Uri packageRoot, - required BuildMode buildMode, - required Architecture targetArchitecture, - required OS targetOs, - IOSSdk? targetIOSSdk, + required BuildModeImpl buildMode, + required ArchitectureImpl targetArchitecture, + required OSImpl targetOs, + IOSSdkImpl? targetIOSSdk, int? targetAndroidNdkApi, - CCompilerConfig? cCompiler, - required LinkModePreference linkModePreference, + CCompilerConfigImpl? cCompiler, + required LinkModePreferenceImpl linkModePreference, Map? dependencyMetadata, }) { - final nonValidated = BuildConfig._() + final nonValidated = BuildConfigImpl._() .._outDir = outDir .._packageName = packageName .._packageRoot = packageRoot @@ -159,36 +144,36 @@ class BuildConfig implements api.BuildConfig { .._targetOs = targetOs .._targetIOSSdk = targetIOSSdk .._targetAndroidNdkApi = targetAndroidNdkApi - .._cCompiler = cCompiler ?? CCompilerConfig() + .._cCompiler = cCompiler ?? CCompilerConfigImpl() .._linkModePreference = linkModePreference .._dependencyMetadata = dependencyMetadata .._dryRun = false; final parsedConfigFile = nonValidated.toYaml(); final config = Config(fileParsed: parsedConfigFile); - return BuildConfig.fromConfig(config); + return BuildConfigImpl.fromConfig(config); } - factory BuildConfig.dryRun({ + factory BuildConfigImpl.dryRun({ required Uri outDir, required String packageName, required Uri packageRoot, - required OS targetOs, - required LinkModePreference linkModePreference, + required OSImpl targetOs, + required LinkModePreferenceImpl linkModePreference, }) { - final nonValidated = BuildConfig._() + final nonValidated = BuildConfigImpl._() .._outDir = outDir .._packageName = packageName .._packageRoot = packageRoot .._targetOs = targetOs .._linkModePreference = linkModePreference - .._cCompiler = CCompilerConfig() + .._cCompiler = CCompilerConfigImpl() .._dryRun = true; final parsedConfigFile = nonValidated.toYaml(); final config = Config(fileParsed: parsedConfigFile); - return BuildConfig.fromConfig(config); + return BuildConfigImpl.fromConfig(config); } - /// Constructs a checksum for a [BuildConfig] based on the fields + /// Constructs a checksum for a [BuildConfigImpl] based on the fields /// of a buildconfig that influence the build. /// /// This can be used for an [outDir], but should not be used for dry-runs. @@ -198,13 +183,13 @@ class BuildConfig implements api.BuildConfig { static String checksum({ required String packageName, required Uri packageRoot, - required Architecture targetArchitecture, - required OS targetOs, - required BuildMode buildMode, - IOSSdk? targetIOSSdk, + required ArchitectureImpl targetArchitecture, + required OSImpl targetOs, + required BuildModeImpl buildMode, + IOSSdkImpl? targetIOSSdk, int? targetAndroidNdkApi, - CCompilerConfig? cCompiler, - required LinkModePreference linkModePreference, + CCompilerConfigImpl? cCompiler, + required LinkModePreferenceImpl linkModePreference, Map? dependencyMetadata, }) { final input = [ @@ -234,9 +219,9 @@ class BuildConfig implements api.BuildConfig { return sha256String.substring(0, nameLength); } - BuildConfig._(); + BuildConfigImpl._(); - /// The version of [BuildConfig]. + /// The version of [BuildConfigImpl]. /// /// This class is used in the protocol between the Dart and Flutter SDKs /// and packages through `build.dart` invocations. @@ -246,8 +231,8 @@ class BuildConfig implements api.BuildConfig { /// representation in the protocol. static Version version = Version(1, 0, 0); - factory BuildConfig.fromConfig(Config config) { - final result = BuildConfig._().._cCompiler = CCompilerConfig._(); + factory BuildConfigImpl.fromConfig(Config config) { + final result = BuildConfigImpl._().._cCompiler = CCompilerConfigImpl._(); final configExceptions = []; for (final f in result._readFieldsFromConfig()) { try { @@ -278,7 +263,7 @@ class BuildConfig implements api.BuildConfig { /// If not provided, [workingDirectory] defaults to [Directory.current]. /// /// This async constructor is intended to be used directly in CLI files. - static Future fromArgs( + static Future fromArgs( List args, { Map? environment, Uri? workingDirectory, @@ -288,7 +273,7 @@ class BuildConfig implements api.BuildConfig { environment: environment, workingDirectory: workingDirectory, ); - return BuildConfig.fromConfig(config); + return BuildConfigImpl.fromConfig(config); } static const outDirConfigKey = 'out_dir'; @@ -328,39 +313,39 @@ class BuildConfig implements api.BuildConfig { _packageRoot = config.path(packageRootConfigKey, mustExist: true), (config) { if (dryRun) { - _throwIfNotNullInDryRun(BuildMode.configKey); + _throwIfNotNullInDryRun(BuildModeImpl.configKey); } else { - _buildMode = BuildMode.fromString( + _buildMode = BuildModeImpl.fromString( config.string( - BuildMode.configKey, - validValues: BuildMode.values.map((e) => '$e'), + BuildModeImpl.configKey, + validValues: BuildModeImpl.values.map((e) => '$e'), ), ); } }, (config) { - _targetOs = OS.fromString( + _targetOs = OSImpl.fromString( config.string( - OS.configKey, - validValues: OS.values.map((e) => '$e'), + OSImpl.configKey, + validValues: OSImpl.values.map((e) => '$e'), ), ); osSet = true; }, (config) { if (dryRun) { - _throwIfNotNullInDryRun(Architecture.configKey); + _throwIfNotNullInDryRun(ArchitectureImpl.configKey); } else { final validArchitectures = [ if (!osSet) - ...Architecture.values + ...ArchitectureImpl.values else - for (final target in Target.values) + for (final target in TargetImpl.values) if (target.os == _targetOs) target.architecture ]; - _targetArchitecture = Architecture.fromString( + _targetArchitecture = ArchitectureImpl.fromString( config.string( - Architecture.configKey, + ArchitectureImpl.configKey, validValues: validArchitectures.map((e) => '$e'), ), ); @@ -368,13 +353,13 @@ class BuildConfig implements api.BuildConfig { }, (config) { if (dryRun) { - _throwIfNotNullInDryRun(IOSSdk.configKey); + _throwIfNotNullInDryRun(IOSSdkImpl.configKey); } else { - _targetIOSSdk = (osSet && _targetOs == OS.iOS) - ? IOSSdk.fromString( + _targetIOSSdk = (osSet && _targetOs == OSImpl.iOS) + ? IOSSdkImpl.fromString( config.string( - IOSSdk.configKey, - validValues: IOSSdk.values.map((e) => '$e'), + IOSSdkImpl.configKey, + validValues: IOSSdkImpl.values.map((e) => '$e'), ), ) : null; @@ -384,27 +369,27 @@ class BuildConfig implements api.BuildConfig { if (dryRun) { _throwIfNotNullInDryRun(targetAndroidNdkApiConfigKey); } else { - _targetAndroidNdkApi = (osSet && _targetOs == OS.android) + _targetAndroidNdkApi = (osSet && _targetOs == OSImpl.android) ? config.int(targetAndroidNdkApiConfigKey) : null; } }, (config) { if (dryRun) { - _throwIfNotNullInDryRun(CCompilerConfig.arConfigKeyFull); + _throwIfNotNullInDryRun(CCompilerConfigImpl.arConfigKeyFull); } else { cCompiler._ar = config.optionalPath( - CCompilerConfig.arConfigKeyFull, + CCompilerConfigImpl.arConfigKeyFull, mustExist: true, ); } }, (config) { if (dryRun) { - _throwIfNotNullInDryRun(CCompilerConfig.ccConfigKeyFull); + _throwIfNotNullInDryRun(CCompilerConfigImpl.ccConfigKeyFull); } else { cCompiler._cc = config.optionalPath( - CCompilerConfig.ccConfigKeyFull, + CCompilerConfigImpl.ccConfigKeyFull, mustExist: true, ); ccSet = true; @@ -412,41 +397,41 @@ class BuildConfig implements api.BuildConfig { }, (config) { if (dryRun) { - _throwIfNotNullInDryRun(CCompilerConfig.ccConfigKeyFull); + _throwIfNotNullInDryRun(CCompilerConfigImpl.ccConfigKeyFull); } else { cCompiler._ld = config.optionalPath( - CCompilerConfig.ldConfigKeyFull, + CCompilerConfigImpl.ldConfigKeyFull, mustExist: true, ); } }, (config) { if (dryRun) { - _throwIfNotNullInDryRun(CCompilerConfig.ccConfigKeyFull); + _throwIfNotNullInDryRun(CCompilerConfigImpl.ccConfigKeyFull); } else { cCompiler._envScript = (ccSet && cCompiler.cc != null && cCompiler.cc!.toFilePath().endsWith('cl.exe')) - ? config.path(CCompilerConfig.envScriptConfigKeyFull, + ? config.path(CCompilerConfigImpl.envScriptConfigKeyFull, mustExist: true) : null; } }, (config) { if (dryRun) { - _throwIfNotNullInDryRun(CCompilerConfig.ccConfigKeyFull); + _throwIfNotNullInDryRun(CCompilerConfigImpl.ccConfigKeyFull); } else { cCompiler._envScriptArgs = config.optionalStringList( - CCompilerConfig.envScriptArgsConfigKeyFull, + CCompilerConfigImpl.envScriptArgsConfigKeyFull, splitEnvironmentPattern: ' ', ); } }, (config) { - _linkModePreference = LinkModePreference.fromString( + _linkModePreference = LinkModePreferenceImpl.fromString( config.string( - LinkModePreference.configKey, - validValues: LinkModePreference.values.map((e) => '$e'), + LinkModePreferenceImpl.configKey, + validValues: LinkModePreferenceImpl.values.map((e) => '$e'), ), ); }, @@ -492,17 +477,19 @@ class BuildConfig implements api.BuildConfig { outDirConfigKey: _outDir.toFilePath(), packageNameConfigKey: _packageName, packageRootConfigKey: _packageRoot.toFilePath(), - OS.configKey: _targetOs.toString(), - LinkModePreference.configKey: _linkModePreference.toString(), + OSImpl.configKey: _targetOs.toString(), + LinkModePreferenceImpl.configKey: _linkModePreference.toString(), _versionKey: version.toString(), if (dryRun) dryRunConfigKey: dryRun, if (!dryRun) ...{ - BuildMode.configKey: _buildMode.toString(), - Architecture.configKey: _targetArchitecture.toString(), - if (_targetIOSSdk != null) IOSSdk.configKey: _targetIOSSdk.toString(), + BuildModeImpl.configKey: _buildMode.toString(), + ArchitectureImpl.configKey: _targetArchitecture.toString(), + if (_targetIOSSdk != null) + IOSSdkImpl.configKey: _targetIOSSdk.toString(), if (_targetAndroidNdkApi != null) targetAndroidNdkApiConfigKey: _targetAndroidNdkApi!, - if (cCompilerYaml.isNotEmpty) CCompilerConfig.configKey: cCompilerYaml, + if (cCompilerYaml.isNotEmpty) + CCompilerConfigImpl.configKey: cCompilerYaml, if (_dependencyMetadata != null && _dependencyMetadata!.isNotEmpty) dependencyMetadataConfigKey: { for (final entry in _dependencyMetadata!.entries) @@ -516,7 +503,7 @@ class BuildConfig implements api.BuildConfig { @override bool operator ==(Object other) { - if (other is! BuildConfig) { + if (other is! BuildConfigImpl) { return false; } if (other.outDir != outDir) return false; @@ -578,7 +565,7 @@ can _only_ depend on OS.'''); } } -class CCompilerConfig implements api.CCompilerConfig { +class CCompilerConfigImpl implements CCompilerConfig { /// Path to a C compiler. @override Uri? get cc => _cc; @@ -604,21 +591,21 @@ class CCompilerConfig implements api.CCompilerConfig { List? get envScriptArgs => _envScriptArgs; late final List? _envScriptArgs; - factory CCompilerConfig({ + factory CCompilerConfigImpl({ Uri? ar, Uri? cc, Uri? ld, Uri? envScript, List? envScriptArgs, }) => - CCompilerConfig._() + CCompilerConfigImpl._() .._ar = ar .._cc = cc .._ld = ld .._envScript = envScript .._envScriptArgs = envScriptArgs; - CCompilerConfig._(); + CCompilerConfigImpl._(); static const configKey = 'c_compiler'; static const arConfigKey = 'ar'; @@ -643,7 +630,7 @@ class CCompilerConfig implements api.CCompilerConfig { @override bool operator ==(Object other) { - if (other is! CCompilerConfig) { + if (other is! CCompilerConfigImpl) { return false; } if (other.ar != ar) return false; diff --git a/pkgs/native_assets_cli/lib/src/model/build_mode.dart b/pkgs/native_assets_cli/lib/src/model/build_mode.dart index 472569bce..d44cf14e5 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_mode.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_mode.dart @@ -2,23 +2,23 @@ // 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 '../api/build_mode.dart' as api; +part of '../api/build_mode.dart'; -class BuildMode implements api.BuildMode { +class BuildModeImpl implements BuildMode { @override final String name; - const BuildMode._(this.name); + const BuildModeImpl._(this.name); - static const debug = BuildMode._('debug'); - static const release = BuildMode._('release'); + static const debug = BuildModeImpl._('debug'); + static const release = BuildModeImpl._('release'); static const values = [ debug, release, ]; - factory BuildMode.fromString(String target) => + factory BuildModeImpl.fromString(String target) => values.firstWhere((e) => e.name == target); /// The `package:config` key preferably used. diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index a5cae1cf8..711658325 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -2,30 +2,16 @@ // 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:collection/collection.dart'; -import 'package:pub_semver/pub_semver.dart'; -import 'package:yaml/yaml.dart'; - -import '../api/asset.dart' as api; -import '../api/build_output.dart' as api; -import '../utils/datetime.dart'; -import '../utils/file.dart'; -import '../utils/map.dart'; -import '../utils/yaml.dart'; -import 'asset.dart'; -import 'dependencies.dart'; -import 'metadata.dart'; - -class BuildOutput implements api.BuildOutput { +part of '../api/build_output.dart'; + +class BuildOutputImpl implements BuildOutput { @override final DateTime timestamp; - final List _assets; + final List _assets; @override - Iterable get assets => _assets; + Iterable get assets => _assets; final Dependencies _dependencies; @@ -36,9 +22,9 @@ class BuildOutput implements api.BuildOutput { final Metadata _metadata; - BuildOutput({ + BuildOutputImpl({ DateTime? timestamp, - List? assets, + List? assets, Dependencies? dependencies, Metadata? metadata, }) : timestamp = (timestamp ?? DateTime.now()).roundDownToSeconds(), @@ -62,12 +48,12 @@ class BuildOutput implements api.BuildOutput { static const _timestampKey = 'timestamp'; static const _versionKey = 'version'; - factory BuildOutput.fromYamlString(String yaml) { + factory BuildOutputImpl.fromYamlString(String yaml) { final yamlObject = loadYaml(yaml); - return BuildOutput.fromYaml(as(yamlObject)); + return BuildOutputImpl.fromYaml(as(yamlObject)); } - factory BuildOutput.fromYaml(YamlMap yamlMap) { + factory BuildOutputImpl.fromYaml(YamlMap yamlMap) { final outputVersion = Version.parse(as(yamlMap['version'])); if (outputVersion.major > version.major) { throw FormatException( @@ -84,9 +70,9 @@ class BuildOutput implements api.BuildOutput { ); } - return BuildOutput( + return BuildOutputImpl( timestamp: DateTime.parse(as(yamlMap[_timestampKey])), - assets: Asset.listFromYamlList(as(yamlMap[_assetsKey])), + assets: AssetImpl.listFromYamlList(as(yamlMap[_assetsKey])), dependencies: Dependencies.fromYaml(as(yamlMap[_dependenciesKey])), metadata: Metadata.fromYaml(as(yamlMap[_metadataKey])), @@ -104,7 +90,7 @@ class BuildOutput implements api.BuildOutput { String toYamlString() => yamlEncode(toYaml()); - /// The version of [BuildOutput]. + /// The version of [BuildOutputImpl]. /// /// This class is used in the protocol between the Dart and Flutter SDKs /// and packages through `build.dart` invocations. @@ -117,13 +103,13 @@ class BuildOutput implements api.BuildOutput { static const fileName = 'build_output.yaml'; /// Writes the YAML file from [outDir]/[fileName]. - static Future readFromFile({required Uri outDir}) async { + static Future readFromFile({required Uri outDir}) async { final buildOutputUri = outDir.resolve(fileName); final buildOutputFile = File.fromUri(buildOutputUri); if (!await buildOutputFile.exists()) { return null; } - return BuildOutput.fromYamlString(await buildOutputFile.readAsString()); + return BuildOutputImpl.fromYamlString(await buildOutputFile.readAsString()); } /// Writes the [toYamlString] to [outDir]/[fileName]. @@ -139,11 +125,11 @@ class BuildOutput implements api.BuildOutput { @override bool operator ==(Object other) { - if (other is! BuildOutput) { + if (other is! BuildOutputImpl) { return false; } return other.timestamp == timestamp && - const ListEquality().equals(other._assets, _assets) && + const ListEquality().equals(other._assets, _assets) && other._dependencies == _dependencies && other._metadata == _metadata; } @@ -151,7 +137,7 @@ class BuildOutput implements api.BuildOutput { @override int get hashCode => Object.hash( timestamp.hashCode, - const ListEquality().hash(_assets), + const ListEquality().hash(_assets), _dependencies, _metadata, ); @@ -169,12 +155,12 @@ class BuildOutput implements api.BuildOutput { Metadata get metadataModel => _metadata; @override - void addAsset(api.Asset asset) { - _assets.add(asset as Asset); + void addAsset(Asset asset) { + _assets.add(asset as AssetImpl); } @override - void addAssets(Iterable assets) { + void addAssets(Iterable assets) { _assets.addAll(assets.cast()); } } diff --git a/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart b/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart index 8955b44bf..7b94f6f13 100644 --- a/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart +++ b/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart @@ -2,25 +2,25 @@ // 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 '../api/ios_sdk.dart' as api; +part of '../api/ios_sdk.dart'; /// For an iOS target, a build is either done for the device or the simulator. /// /// Only fat binaries or xcframeworks can contain both targets. -class IOSSdk implements api.IOSSdk { +class IOSSdkImpl implements IOSSdk { final String xcodebuildSdk; - const IOSSdk._(this.xcodebuildSdk); + const IOSSdkImpl._(this.xcodebuildSdk); - static const iPhoneOs = IOSSdk._('iphoneos'); - static const iPhoneSimulator = IOSSdk._('iphonesimulator'); + static const iPhoneOs = IOSSdkImpl._('iphoneos'); + static const iPhoneSimulator = IOSSdkImpl._('iphonesimulator'); static const values = [ iPhoneOs, iPhoneSimulator, ]; - factory IOSSdk.fromString(String target) => + factory IOSSdkImpl.fromString(String target) => values.firstWhere((e) => e.xcodebuildSdk == target); /// The `package:config` key preferably used. diff --git a/pkgs/native_assets_cli/lib/src/model/link_mode.dart b/pkgs/native_assets_cli/lib/src/model/link_mode.dart index 8f87983a2..f073db1e5 100644 --- a/pkgs/native_assets_cli/lib/src/model/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/model/link_mode.dart @@ -2,23 +2,23 @@ // 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 '../api/link_mode.dart' as api; +part of '../api/link_mode.dart'; -class LinkMode implements api.LinkMode { +class LinkModeImpl implements LinkMode { final String name; - const LinkMode._(this.name); + const LinkModeImpl._(this.name); - static const LinkMode dynamic = LinkMode._('dynamic'); - static const LinkMode static = LinkMode._('static'); + static const LinkModeImpl dynamic = LinkModeImpl._('dynamic'); + static const LinkModeImpl static = LinkModeImpl._('static'); - /// Known values for [LinkMode]. - static const List values = [ + /// Known values for [LinkModeImpl]. + static const List values = [ dynamic, static, ]; - factory LinkMode.fromName(String name) => + factory LinkModeImpl.fromName(String name) => values.where((element) => element.name == name).first; @override diff --git a/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart index 83d72e92e..4a5291540 100644 --- a/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart @@ -2,50 +2,49 @@ // 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 '../api/link_mode_preference.dart' as api; -import 'link_mode.dart'; +part of '../api/link_mode_preference.dart'; -class LinkModePreference implements api.LinkModePreference { +class LinkModePreferenceImpl implements LinkModePreference { @override final String name; @override - final LinkMode preferredLinkMode; + final LinkModeImpl preferredLinkMode; @override - final List potentialLinkMode; + final List potentialLinkMode; - const LinkModePreference( + const LinkModePreferenceImpl( this.name, { required this.preferredLinkMode, required this.potentialLinkMode, }); - factory LinkModePreference.fromString(String name) => + factory LinkModePreferenceImpl.fromString(String name) => values.where((element) => element.name == name).first; - static const dynamic = LinkModePreference( + static const dynamic = LinkModePreferenceImpl( 'dynamic', - preferredLinkMode: LinkMode.dynamic, - potentialLinkMode: [LinkMode.dynamic], + preferredLinkMode: LinkModeImpl.dynamic, + potentialLinkMode: [LinkModeImpl.dynamic], ); - static const static = LinkModePreference( + static const static = LinkModePreferenceImpl( 'static', - preferredLinkMode: LinkMode.static, - potentialLinkMode: [LinkMode.static], + preferredLinkMode: LinkModeImpl.static, + potentialLinkMode: [LinkModeImpl.static], ); - static const preferDynamic = LinkModePreference( + static const preferDynamic = LinkModePreferenceImpl( 'prefer-dynamic', - preferredLinkMode: LinkMode.dynamic, - potentialLinkMode: LinkMode.values, + preferredLinkMode: LinkModeImpl.dynamic, + potentialLinkMode: LinkModeImpl.values, ); - static const preferStatic = LinkModePreference( + static const preferStatic = LinkModePreferenceImpl( 'prefer-static', - preferredLinkMode: LinkMode.static, - potentialLinkMode: LinkMode.values, + preferredLinkMode: LinkModeImpl.static, + potentialLinkMode: LinkModeImpl.values, ); static const values = [ diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index de9cfbb63..76ac2a3c7 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -2,31 +2,26 @@ // 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' show Abi; -import 'dart:io'; - -import '../api/link_mode.dart' as api; -import '../api/target.dart' as api; -import 'link_mode.dart'; +part of '../api/target.dart'; /// The hardware architectures the Dart VM runs on. -class Architecture implements api.Architecture { +class ArchitectureImpl implements Architecture { /// This architecture as used in [Platform.version]. final String dartPlatform; - const Architecture._(this.dartPlatform); + const ArchitectureImpl._(this.dartPlatform); - factory Architecture.fromAbi(Abi abi) => _abiToArch[abi]!; + factory ArchitectureImpl.fromAbi(Abi abi) => _abiToArch[abi]!; - static const Architecture arm = Architecture._('arm'); - static const Architecture arm64 = Architecture._('arm64'); - static const Architecture ia32 = Architecture._('ia32'); - static const Architecture riscv32 = Architecture._('riscv32'); - static const Architecture riscv64 = Architecture._('riscv64'); - static const Architecture x64 = Architecture._('x64'); + static const ArchitectureImpl arm = ArchitectureImpl._('arm'); + static const ArchitectureImpl arm64 = ArchitectureImpl._('arm64'); + static const ArchitectureImpl ia32 = ArchitectureImpl._('ia32'); + static const ArchitectureImpl riscv32 = ArchitectureImpl._('riscv32'); + static const ArchitectureImpl riscv64 = ArchitectureImpl._('riscv64'); + static const ArchitectureImpl x64 = ArchitectureImpl._('x64'); - /// Known values for [Architecture]. - static const List values = [ + /// Known values for [ArchitectureImpl]. + static const List values = [ arm, arm64, ia32, @@ -36,27 +31,27 @@ class Architecture implements api.Architecture { ]; static const _abiToArch = { - Abi.androidArm: Architecture.arm, - Abi.androidArm64: Architecture.arm64, - Abi.androidIA32: Architecture.ia32, - Abi.androidX64: Architecture.x64, - Abi.androidRiscv64: Architecture.riscv64, - Abi.fuchsiaArm64: Architecture.arm64, - Abi.fuchsiaX64: Architecture.x64, - Abi.iosArm: Architecture.arm, - Abi.iosArm64: Architecture.arm64, - Abi.iosX64: Architecture.x64, - Abi.linuxArm: Architecture.arm, - Abi.linuxArm64: Architecture.arm64, - Abi.linuxIA32: Architecture.ia32, - Abi.linuxRiscv32: Architecture.riscv32, - Abi.linuxRiscv64: Architecture.riscv64, - Abi.linuxX64: Architecture.x64, - Abi.macosArm64: Architecture.arm64, - Abi.macosX64: Architecture.x64, - Abi.windowsArm64: Architecture.arm64, - Abi.windowsIA32: Architecture.ia32, - Abi.windowsX64: Architecture.x64, + Abi.androidArm: ArchitectureImpl.arm, + Abi.androidArm64: ArchitectureImpl.arm64, + Abi.androidIA32: ArchitectureImpl.ia32, + Abi.androidX64: ArchitectureImpl.x64, + Abi.androidRiscv64: ArchitectureImpl.riscv64, + Abi.fuchsiaArm64: ArchitectureImpl.arm64, + Abi.fuchsiaX64: ArchitectureImpl.x64, + Abi.iosArm: ArchitectureImpl.arm, + Abi.iosArm64: ArchitectureImpl.arm64, + Abi.iosX64: ArchitectureImpl.x64, + Abi.linuxArm: ArchitectureImpl.arm, + Abi.linuxArm64: ArchitectureImpl.arm64, + Abi.linuxIA32: ArchitectureImpl.ia32, + Abi.linuxRiscv32: ArchitectureImpl.riscv32, + Abi.linuxRiscv64: ArchitectureImpl.riscv64, + Abi.linuxX64: ArchitectureImpl.x64, + Abi.macosArm64: ArchitectureImpl.arm64, + Abi.macosX64: ArchitectureImpl.x64, + Abi.windowsArm64: ArchitectureImpl.arm64, + Abi.windowsIA32: ArchitectureImpl.ia32, + Abi.windowsX64: ArchitectureImpl.x64, }; /// The `package:config` key preferably used. @@ -65,39 +60,39 @@ class Architecture implements api.Architecture { @override String toString() => dartPlatform; - /// Mapping from strings as used in [Architecture.toString] to - /// [Architecture]s. - static final Map _stringToArchitecture = - Map.fromEntries(Architecture.values.map( + /// Mapping from strings as used in [ArchitectureImpl.toString] to + /// [ArchitectureImpl]s. + static final Map _stringToArchitecture = + Map.fromEntries(ArchitectureImpl.values.map( (architecture) => MapEntry(architecture.toString(), architecture))); - factory Architecture.fromString(String target) => + factory ArchitectureImpl.fromString(String target) => _stringToArchitecture[target]!; - /// The current [Architecture]. + /// The current [ArchitectureImpl]. /// /// Read from the [Platform.version] string. - static final Architecture current = Target.current.architecture; + static final ArchitectureImpl current = TargetImpl.current.architecture; } /// The operating systems the Dart VM runs on. -class OS implements api.OS { +class OSImpl implements OS { /// This OS as used in [Platform.version] final String dartPlatform; - const OS._(this.dartPlatform); + const OSImpl._(this.dartPlatform); - factory OS.fromAbi(Abi abi) => _abiToOS[abi]!; + factory OSImpl.fromAbi(Abi abi) => _abiToOS[abi]!; - static const OS android = OS._('android'); - static const OS fuchsia = OS._('fuchsia'); - static const OS iOS = OS._('ios'); - static const OS linux = OS._('linux'); - static const OS macOS = OS._('macos'); - static const OS windows = OS._('windows'); + static const OSImpl android = OSImpl._('android'); + static const OSImpl fuchsia = OSImpl._('fuchsia'); + static const OSImpl iOS = OSImpl._('ios'); + static const OSImpl linux = OSImpl._('linux'); + static const OSImpl macOS = OSImpl._('macos'); + static const OSImpl windows = OSImpl._('windows'); - /// Known values for [OS]. - static const List values = [ + /// Known values for [OSImpl]. + static const List values = [ android, fuchsia, iOS, @@ -107,37 +102,37 @@ class OS implements api.OS { ]; static const _abiToOS = { - Abi.androidArm: OS.android, - Abi.androidArm64: OS.android, - Abi.androidIA32: OS.android, - Abi.androidX64: OS.android, - Abi.androidRiscv64: OS.android, - Abi.fuchsiaArm64: OS.fuchsia, - Abi.fuchsiaX64: OS.fuchsia, - Abi.iosArm: OS.iOS, - Abi.iosArm64: OS.iOS, - Abi.iosX64: OS.iOS, - Abi.linuxArm: OS.linux, - Abi.linuxArm64: OS.linux, - Abi.linuxIA32: OS.linux, - Abi.linuxRiscv32: OS.linux, - Abi.linuxRiscv64: OS.linux, - Abi.linuxX64: OS.linux, - Abi.macosArm64: OS.macOS, - Abi.macosX64: OS.macOS, - Abi.windowsArm64: OS.windows, - Abi.windowsIA32: OS.windows, - Abi.windowsX64: OS.windows, + Abi.androidArm: OSImpl.android, + Abi.androidArm64: OSImpl.android, + Abi.androidIA32: OSImpl.android, + Abi.androidX64: OSImpl.android, + Abi.androidRiscv64: OSImpl.android, + Abi.fuchsiaArm64: OSImpl.fuchsia, + Abi.fuchsiaX64: OSImpl.fuchsia, + Abi.iosArm: OSImpl.iOS, + Abi.iosArm64: OSImpl.iOS, + Abi.iosX64: OSImpl.iOS, + Abi.linuxArm: OSImpl.linux, + Abi.linuxArm64: OSImpl.linux, + Abi.linuxIA32: OSImpl.linux, + Abi.linuxRiscv32: OSImpl.linux, + Abi.linuxRiscv64: OSImpl.linux, + Abi.linuxX64: OSImpl.linux, + Abi.macosArm64: OSImpl.macOS, + Abi.macosX64: OSImpl.macOS, + Abi.windowsArm64: OSImpl.windows, + Abi.windowsIA32: OSImpl.windows, + Abi.windowsX64: OSImpl.windows, }; /// Typical cross compilation between OSes. static const _osCrossCompilationDefault = { - OS.macOS: [OS.macOS, OS.iOS, OS.android], - OS.linux: [OS.linux, OS.android], - OS.windows: [OS.windows, OS.android], + OSImpl.macOS: [OSImpl.macOS, OSImpl.iOS, OSImpl.android], + OSImpl.linux: [OSImpl.linux, OSImpl.android], + OSImpl.windows: [OSImpl.windows, OSImpl.android], }; - /// The default dynamic library file name on this [OS]. + /// The default dynamic library file name on this [OSImpl]. @override String dylibFileName(String name) { final prefix = _dylibPrefix[this]!; @@ -145,7 +140,7 @@ class OS implements api.OS { return '$prefix$name.$extension'; } - /// The default static library file name on this [OS]. + /// The default static library file name on this [OSImpl]. @override String staticlibFileName(String name) { final prefix = _staticlibPrefix[this]!; @@ -154,15 +149,15 @@ class OS implements api.OS { } @override - String libraryFileName(String name, api.LinkMode linkMode) { - if (linkMode == LinkMode.dynamic) { + String libraryFileName(String name, LinkMode linkMode) { + if (linkMode == LinkModeImpl.dynamic) { return dylibFileName(name); } - assert(linkMode == LinkMode.static); + assert(linkMode == LinkModeImpl.static); return staticlibFileName(name); } - /// The default executable file name on this [OS]. + /// The default executable file name on this [OSImpl]. @override String executableFileName(String name) { final extension = _executableExtension[this]!; @@ -170,47 +165,47 @@ class OS implements api.OS { return '$name$dot$extension'; } - /// The default name prefix for dynamic libraries per [OS]. + /// The default name prefix for dynamic libraries per [OSImpl]. static const _dylibPrefix = { - OS.android: 'lib', - OS.fuchsia: 'lib', - OS.iOS: 'lib', - OS.linux: 'lib', - OS.macOS: 'lib', - OS.windows: '', + OSImpl.android: 'lib', + OSImpl.fuchsia: 'lib', + OSImpl.iOS: 'lib', + OSImpl.linux: 'lib', + OSImpl.macOS: 'lib', + OSImpl.windows: '', }; - /// The default extension for dynamic libraries per [OS]. + /// The default extension for dynamic libraries per [OSImpl]. static const _dylibExtension = { - OS.android: 'so', - OS.fuchsia: 'so', - OS.iOS: 'dylib', - OS.linux: 'so', - OS.macOS: 'dylib', - OS.windows: 'dll', + OSImpl.android: 'so', + OSImpl.fuchsia: 'so', + OSImpl.iOS: 'dylib', + OSImpl.linux: 'so', + OSImpl.macOS: 'dylib', + OSImpl.windows: 'dll', }; - /// The default name prefix for static libraries per [OS]. + /// The default name prefix for static libraries per [OSImpl]. static const _staticlibPrefix = _dylibPrefix; - /// The default extension for static libraries per [OS]. + /// The default extension for static libraries per [OSImpl]. static const _staticlibExtension = { - OS.android: 'a', - OS.fuchsia: 'a', - OS.iOS: 'a', - OS.linux: 'a', - OS.macOS: 'a', - OS.windows: 'lib', + OSImpl.android: 'a', + OSImpl.fuchsia: 'a', + OSImpl.iOS: 'a', + OSImpl.linux: 'a', + OSImpl.macOS: 'a', + OSImpl.windows: 'lib', }; - /// The default extension for executables per [OS]. + /// The default extension for executables per [OSImpl]. static const _executableExtension = { - OS.android: '', - OS.fuchsia: '', - OS.iOS: '', - OS.linux: '', - OS.macOS: '', - OS.windows: 'exe', + OSImpl.android: '', + OSImpl.fuchsia: '', + OSImpl.iOS: '', + OSImpl.linux: '', + OSImpl.macOS: '', + OSImpl.windows: 'exe', }; /// The `package:config` key preferably used. @@ -219,35 +214,35 @@ class OS implements api.OS { @override String toString() => dartPlatform; - /// Mapping from strings as used in [OS.toString] to - /// [OS]s. - static final Map _stringToOS = - Map.fromEntries(OS.values.map((os) => MapEntry(os.toString(), os))); + /// Mapping from strings as used in [OSImpl.toString] to + /// [OSImpl]s. + static final Map _stringToOS = + Map.fromEntries(OSImpl.values.map((os) => MapEntry(os.toString(), os))); - factory OS.fromString(String target) => _stringToOS[target]!; + factory OSImpl.fromString(String target) => _stringToOS[target]!; - /// The current [OS]. + /// The current [OSImpl]. /// /// Read from the [Platform.version] string. - static final OS current = Target.current.os; + static final OSImpl current = TargetImpl.current.os; } /// Application binary interface. /// -/// The Dart VM can run on a variety of [Target]s, see [Target.values]. -class Target implements api.Target { +/// The Dart VM can run on a variety of [TargetImpl]s, see [TargetImpl.values]. +class TargetImpl implements Target { final Abi abi; - const Target._(this.abi); + const TargetImpl._(this.abi); - factory Target.fromString(String target) => _stringToTarget[target]!; + factory TargetImpl.fromString(String target) => _stringToTarget[target]!; - /// The [Target] corresponding the substring of [Platform.version] - /// describing the [Target]. + /// The [TargetImpl] corresponding the substring of [Platform.version] + /// describing the [TargetImpl]. /// /// The [Platform.version] strings are formatted as follows: /// ` () on ""`. - factory Target.fromDartPlatform(String versionStringFull) { + factory TargetImpl.fromDartPlatform(String versionStringFull) { final split = versionStringFull.split('"'); if (split.length < 2) { throw FormatException( @@ -262,7 +257,8 @@ class Target implements api.Target { return target; } - factory Target.fromArchitectureAndOs(Architecture architecture, OS os) { + factory TargetImpl.fromArchitectureAndOs( + ArchitectureImpl architecture, OSImpl os) { for (final value in values) { if (value.os == os && value.architecture == architecture) { return value; @@ -272,33 +268,33 @@ class Target implements api.Target { "'${os}_$architecture'"); } - static const androidArm = Target._(Abi.androidArm); - static const androidArm64 = Target._(Abi.androidArm64); - static const androidIA32 = Target._(Abi.androidIA32); - static const androidX64 = Target._(Abi.androidX64); - static const androidRiscv64 = Target._(Abi.androidRiscv64); - static const fuchsiaArm64 = Target._(Abi.fuchsiaArm64); - static const fuchsiaX64 = Target._(Abi.fuchsiaX64); - static const iOSArm = Target._(Abi.iosArm); - static const iOSArm64 = Target._(Abi.iosArm64); - static const iOSX64 = Target._(Abi.iosX64); - static const linuxArm = Target._(Abi.linuxArm); - static const linuxArm64 = Target._(Abi.linuxArm64); - static const linuxIA32 = Target._(Abi.linuxIA32); - static const linuxRiscv32 = Target._(Abi.linuxRiscv32); - static const linuxRiscv64 = Target._(Abi.linuxRiscv64); - static const linuxX64 = Target._(Abi.linuxX64); - static const macOSArm64 = Target._(Abi.macosArm64); - static const macOSX64 = Target._(Abi.macosX64); - static const windowsArm64 = Target._(Abi.windowsArm64); - static const windowsIA32 = Target._(Abi.windowsIA32); - static const windowsX64 = Target._(Abi.windowsX64); + static const androidArm = TargetImpl._(Abi.androidArm); + static const androidArm64 = TargetImpl._(Abi.androidArm64); + static const androidIA32 = TargetImpl._(Abi.androidIA32); + static const androidX64 = TargetImpl._(Abi.androidX64); + static const androidRiscv64 = TargetImpl._(Abi.androidRiscv64); + static const fuchsiaArm64 = TargetImpl._(Abi.fuchsiaArm64); + static const fuchsiaX64 = TargetImpl._(Abi.fuchsiaX64); + static const iOSArm = TargetImpl._(Abi.iosArm); + static const iOSArm64 = TargetImpl._(Abi.iosArm64); + static const iOSX64 = TargetImpl._(Abi.iosX64); + static const linuxArm = TargetImpl._(Abi.linuxArm); + static const linuxArm64 = TargetImpl._(Abi.linuxArm64); + static const linuxIA32 = TargetImpl._(Abi.linuxIA32); + static const linuxRiscv32 = TargetImpl._(Abi.linuxRiscv32); + static const linuxRiscv64 = TargetImpl._(Abi.linuxRiscv64); + static const linuxX64 = TargetImpl._(Abi.linuxX64); + static const macOSArm64 = TargetImpl._(Abi.macosArm64); + static const macOSX64 = TargetImpl._(Abi.macosX64); + static const windowsArm64 = TargetImpl._(Abi.windowsArm64); + static const windowsIA32 = TargetImpl._(Abi.windowsIA32); + static const windowsX64 = TargetImpl._(Abi.windowsX64); /// All Targets that we can build for. /// /// Note that for some of these a Dart SDK is not available and they are only /// used as target architectures for Flutter apps. - static const Set values = { + static const Set values = { androidArm, androidArm64, androidIA32, @@ -323,25 +319,27 @@ class Target implements api.Target { // TODO(dacoharkes): Add support for `wasm`. }; - /// Mapping from strings as used in [Target.toString] to [Target]s. - static final Map _stringToTarget = Map.fromEntries( - Target.values.map((target) => MapEntry(target.toString(), target))); + /// Mapping from strings as used in [TargetImpl.toString] to [TargetImpl]s. + static final Map _stringToTarget = Map.fromEntries( + TargetImpl.values.map((target) => MapEntry(target.toString(), target))); /// Mapping from lowercased strings as used in [Platform.version] to - /// [Target]s. - static final Map _dartVMstringToTarget = Map.fromEntries( - Target.values.map((target) => MapEntry(target.dartVMToString(), target))); + /// [TargetImpl]s. + static final Map _dartVMstringToTarget = Map.fromEntries( + TargetImpl.values + .map((target) => MapEntry(target.dartVMToString(), target))); - /// The current [Target]. + /// The current [TargetImpl]. /// /// Read from the [Platform.version] string. - static final Target current = Target.fromDartPlatform(Platform.version); + static final TargetImpl current = + TargetImpl.fromDartPlatform(Platform.version); @override - Architecture get architecture => Architecture.fromAbi(abi); + ArchitectureImpl get architecture => ArchitectureImpl.fromAbi(abi); @override - OS get os => OS.fromAbi(abi); + OSImpl get os => OSImpl.fromAbi(abi); String get _architectureString => architecture.dartPlatform; @@ -356,25 +354,25 @@ class Target implements api.Target { /// Compares `this` to [other]. /// - /// If [other] is also an [Target], consistent with sorting on [toString]. + /// If [other] is also an [TargetImpl], consistent with sorting on [toString]. @override - int compareTo(api.Target other) => toString().compareTo(other.toString()); + int compareTo(Target other) => toString().compareTo(other.toString()); - /// A list of supported target [Target]s from this host [os]. - List supportedTargetTargets( - {Map> osCrossCompilation = - OS._osCrossCompilationDefault}) => - Target.values + /// A list of supported target [TargetImpl]s from this host [os]. + List supportedTargetTargets( + {Map> osCrossCompilation = + OSImpl._osCrossCompilationDefault}) => + TargetImpl.values .where((target) => // Only valid cross compilation. osCrossCompilation[os]!.contains(target.os) && // And no deprecated architectures. - target != Target.iOSArm) + target != TargetImpl.iOSArm) .sorted; } -/// Common methods for manipulating iterables of [Target]s. -extension TargetList on Iterable { - /// The [Target]s in `this` sorted by name alphabetically. - List get sorted => [for (final target in this) target]..sort(); +/// Common methods for manipulating iterables of [TargetImpl]s. +extension TargetList on Iterable { + /// The [TargetImpl]s in `this` sorted by name alphabetically. + List get sorted => [for (final target in this) target]..sort(); } diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 82a0c5712..1dfbd29cb 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -6,7 +6,7 @@ import 'dart:io'; import 'package:cli_config/cli_config.dart'; import 'package:native_assets_cli/native_assets_cli.dart'; -import 'package:native_assets_cli/native_assets_cli_internal.dart' as internal; +import 'package:native_assets_cli/src/api/build_config.dart'; import 'package:test/test.dart'; void main() async { @@ -199,8 +199,7 @@ void main() async { buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, ); - final configFileContents = - (buildConfig as internal.BuildConfig).toYamlString(); + final configFileContents = (buildConfig as BuildConfigImpl).toYamlString(); final configUri = tempUri.resolve('config.yaml'); final configFile = File.fromUri(configUri); await configFile.writeAsString(configFileContents); diff --git a/pkgs/native_assets_cli/test/example/local_asset_test.dart b/pkgs/native_assets_cli/test/example/local_asset_test.dart index 2179c0866..9411a2d80 100644 --- a/pkgs/native_assets_cli/test/example/local_asset_test.dart +++ b/pkgs/native_assets_cli/test/example/local_asset_test.dart @@ -42,19 +42,19 @@ void main() async { '-Dout_dir=${tempUri.toFilePath()}', '-Dpackage_name=$name', '-Dpackage_root=${testPackageUri.toFilePath()}', - '-Dtarget_os=${OS.current}', - '-Dversion=${BuildConfig.version}', + '-Dtarget_os=${OSImpl.current}', + '-Dversion=${BuildConfigImpl.version}', '-Dlink_mode_preference=dynamic', '-Ddry_run=$dryRun', if (!dryRun) ...[ - '-Dtarget_architecture=${Architecture.current}', + '-Dtarget_architecture=${ArchitectureImpl.current}', '-Dbuild_mode=debug', if (cc != null) '-Dcc=${cc!.toFilePath()}', if (envScript != null) - '-D${CCompilerConfig.envScriptConfigKeyFull}=' + '-D${CCompilerConfigImpl.envScriptConfigKeyFull}=' '${envScript!.toFilePath()}', if (envScriptArgs != null) - '-D${CCompilerConfig.envScriptArgsConfigKeyFull}=' + '-D${CCompilerConfigImpl.envScriptArgsConfigKeyFull}=' '${envScriptArgs!.join(' ')}', ], ], @@ -68,14 +68,14 @@ void main() async { expect(processResult.exitCode, 0); final buildOutputUri = tempUri.resolve('build_output.yaml'); - final buildOutput = BuildOutput.fromYamlString( + final buildOutput = BuildOutputImpl.fromYamlString( await File.fromUri(buildOutputUri).readAsString()); final assets = buildOutput.assets; final dependencies = buildOutput.dependencies; if (dryRun) { expect(assets.length, greaterThanOrEqualTo(1)); expect( - await File.fromUri((assets.first.path as AssetAbsolutePath).uri) + await File.fromUri((assets.first.path as AssetAbsolutePathImpl).uri) .exists(), false); expect(dependencies, []); diff --git a/pkgs/native_assets_cli/test/example/native_add_library_test.dart b/pkgs/native_assets_cli/test/example/native_add_library_test.dart index 2c4f22f91..5a2584c6b 100644 --- a/pkgs/native_assets_cli/test/example/native_add_library_test.dart +++ b/pkgs/native_assets_cli/test/example/native_add_library_test.dart @@ -42,19 +42,19 @@ void main() async { '-Dout_dir=${tempUri.toFilePath()}', '-Dpackage_name=$name', '-Dpackage_root=${testPackageUri.toFilePath()}', - '-Dtarget_os=${OS.current}', - '-Dversion=${BuildConfig.version}', + '-Dtarget_os=${OSImpl.current}', + '-Dversion=${BuildConfigImpl.version}', '-Dlink_mode_preference=dynamic', '-Ddry_run=$dryRun', if (!dryRun) ...[ - '-Dtarget_architecture=${Architecture.current}', + '-Dtarget_architecture=${ArchitectureImpl.current}', '-Dbuild_mode=debug', if (cc != null) '-Dcc=${cc!.toFilePath()}', if (envScript != null) - '-D${CCompilerConfig.envScriptConfigKeyFull}=' + '-D${CCompilerConfigImpl.envScriptConfigKeyFull}=' '${envScript!.toFilePath()}', if (envScriptArgs != null) - '-D${CCompilerConfig.envScriptArgsConfigKeyFull}=' + '-D${CCompilerConfigImpl.envScriptArgsConfigKeyFull}=' '${envScriptArgs!.join(' ')}', ], ], @@ -68,14 +68,14 @@ void main() async { expect(processResult.exitCode, 0); final buildOutputUri = tempUri.resolve('build_output.yaml'); - final buildOutput = BuildOutput.fromYamlString( + final buildOutput = BuildOutputImpl.fromYamlString( await File.fromUri(buildOutputUri).readAsString()); final assets = buildOutput.assets; final dependencies = buildOutput.dependencies; if (dryRun) { expect(assets.length, greaterThanOrEqualTo(1)); expect( - await File.fromUri((assets.first.path as AssetAbsolutePath).uri) + await File.fromUri((assets.first.path as AssetAbsolutePathImpl).uri) .exists(), false); expect(dependencies, []); diff --git a/pkgs/native_assets_cli/test/helpers.dart b/pkgs/native_assets_cli/test/helpers.dart index e4c633c86..1e921950b 100644 --- a/pkgs/native_assets_cli/test/helpers.dart +++ b/pkgs/native_assets_cli/test/helpers.dart @@ -78,35 +78,35 @@ String unparseKey(String key) => key.replaceAll('.', '__').toUpperCase(); /// /// Provided on Dart CI. final Uri? ar = Platform - .environment[unparseKey(internal.CCompilerConfig.arConfigKeyFull)] + .environment[unparseKey(internal.CCompilerConfigImpl.arConfigKeyFull)] ?.asFileUri(); /// Compiler provided by the environment. /// /// Provided on Dart CI. final Uri? cc = Platform - .environment[unparseKey(internal.CCompilerConfig.ccConfigKeyFull)] + .environment[unparseKey(internal.CCompilerConfigImpl.ccConfigKeyFull)] ?.asFileUri(); /// Linker provided by the environment. /// /// Provided on Dart CI. final Uri? ld = Platform - .environment[unparseKey(internal.CCompilerConfig.ldConfigKeyFull)] + .environment[unparseKey(internal.CCompilerConfigImpl.ldConfigKeyFull)] ?.asFileUri(); /// Path to script that sets environment variables for [cc], [ld], and [ar]. /// /// Provided on Dart CI. -final Uri? envScript = Platform - .environment[unparseKey(internal.CCompilerConfig.envScriptConfigKeyFull)] +final Uri? envScript = Platform.environment[ + unparseKey(internal.CCompilerConfigImpl.envScriptConfigKeyFull)] ?.asFileUri(); /// Arguments for [envScript] provided by environment. /// /// Provided on Dart CI. final List? envScriptArgs = Platform.environment[ - unparseKey(internal.CCompilerConfig.envScriptArgsConfigKeyFull)] + unparseKey(internal.CCompilerConfigImpl.envScriptArgsConfigKeyFull)] ?.split(' '); extension on String { diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index a7dd3bd0c..8a4e3faba 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -4,6 +4,7 @@ import 'package:collection/collection.dart'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; +import 'package:native_assets_cli/src/api/asset.dart'; import 'package:test/test.dart'; void main() { @@ -12,41 +13,41 @@ void main() { final barUri = Uri(path: 'path/to/libbar.a'); final blaUri = Uri(path: 'path/with spaces/bla.dll'); final assets = [ - Asset( + AssetImpl( id: 'foo', - path: AssetAbsolutePath(fooUri), - target: Target.androidX64, - linkMode: LinkMode.dynamic, + path: AssetAbsolutePathImpl(fooUri), + target: TargetImpl.androidX64, + linkMode: LinkModeImpl.dynamic, ), - Asset( + AssetImpl( id: 'foo3', - path: AssetSystemPath(foo3Uri), - target: Target.androidX64, - linkMode: LinkMode.dynamic, + path: AssetSystemPathImpl(foo3Uri), + target: TargetImpl.androidX64, + linkMode: LinkModeImpl.dynamic, ), - Asset( + AssetImpl( id: 'foo4', - path: AssetInExecutable(), - target: Target.androidX64, - linkMode: LinkMode.dynamic, + path: AssetInExecutableImpl(), + target: TargetImpl.androidX64, + linkMode: LinkModeImpl.dynamic, ), - Asset( + AssetImpl( id: 'foo5', - path: AssetInProcess(), - target: Target.androidX64, - linkMode: LinkMode.dynamic, + path: AssetInProcessImpl(), + target: TargetImpl.androidX64, + linkMode: LinkModeImpl.dynamic, ), - Asset( + AssetImpl( id: 'bar', - path: AssetAbsolutePath(barUri), - target: Target.linuxArm64, - linkMode: LinkMode.static, + path: AssetAbsolutePathImpl(barUri), + target: TargetImpl.linuxArm64, + linkMode: LinkModeImpl.static, ), - Asset( + AssetImpl( id: 'bla', - path: AssetAbsolutePath(blaUri), - target: Target.windowsX64, - linkMode: LinkMode.dynamic, + path: AssetAbsolutePathImpl(blaUri), + target: TargetImpl.windowsX64, + linkMode: LinkModeImpl.dynamic, ), ]; @@ -88,13 +89,13 @@ void main() { test('asset yaml', () { final yaml = assets.toYamlString(); expect(yaml, assetsYamlEncoding); - final assets2 = Asset.listFromYamlString(yaml); + final assets2 = AssetImpl.listFromYamlString(yaml); expect(assets, assets2); }); test('AssetPath factory', () async { expect( - () => AssetPath('wrong', null), + () => AssetPathImpl('wrong', null), throwsA(predicate( (e) => e is FormatException && e.message.contains('Unknown pathType'), )), @@ -112,7 +113,7 @@ void main() { test('List hashCode', () async { final assets2 = assets.take(3).toList(); - const equality = ListEquality(); + const equality = ListEquality(); expect(equality.hash(assets) != equality.hash(assets2), true); }); @@ -121,7 +122,7 @@ void main() { }); test('Asset listFromYamlString', () async { - final assets = Asset.listFromYamlString(''); - expect(assets, []); + final assets = AssetImpl.listFromYamlString(''); + expect(assets, []); }); } diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index d8fb8fec3..aacb13626 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -48,31 +48,31 @@ void main() async { }); test('BuildConfig ==', () { - final config1 = BuildConfig( + final config1 = BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: tempUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.iOS, - targetIOSSdk: IOSSdk.iPhoneOs, - cCompiler: CCompilerConfig( + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOs, + cCompiler: CCompilerConfigImpl( cc: fakeClang, ld: fakeLd, ar: fakeAr, ), - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, ); - final config2 = BuildConfig( + final config2 = BuildConfigImpl( outDir: outDir2Uri, packageName: packageName, packageRoot: tempUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.android, targetAndroidNdkApi: 30, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, ); expect(config1, equals(config1)); @@ -93,15 +93,15 @@ void main() async { }); test('BuildConfig fromConfig', () { - final buildConfig2 = BuildConfig( + final buildConfig2 = BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: packageRootUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.android, targetAndroidNdkApi: 30, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, ); final config = Config(fileParsed: { @@ -114,20 +114,20 @@ void main() async { 'target_android_ndk_api': 30, 'target_architecture': 'arm64', 'target_os': 'android', - 'version': BuildOutput.version.toString(), + 'version': BuildOutputImpl.version.toString(), }); - final fromConfig = BuildConfig.fromConfig(config); + final fromConfig = BuildConfigImpl.fromConfig(config); expect(fromConfig, equals(buildConfig2)); }); test('BuildConfig.dryRun', () { - final buildConfig2 = BuildConfig.dryRun( + final buildConfig2 = BuildConfigImpl.dryRun( outDir: outDirUri, packageName: packageName, packageRoot: packageRootUri, - targetOs: OS.android, - linkModePreference: LinkModePreference.preferStatic, + targetOs: OSImpl.android, + linkModePreference: LinkModePreferenceImpl.preferStatic, ); final config = Config(fileParsed: { @@ -137,45 +137,45 @@ void main() async { 'package_name': packageName, 'package_root': packageRootUri.toFilePath(), 'target_os': 'android', - 'version': BuildOutput.version.toString(), + 'version': BuildOutputImpl.version.toString(), }); - final fromConfig = BuildConfig.fromConfig(config); + final fromConfig = BuildConfigImpl.fromConfig(config); expect(fromConfig, equals(buildConfig2)); }); test('BuildConfig toYaml fromConfig', () { - final buildConfig1 = BuildConfig( + final buildConfig1 = BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: packageRootUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.iOS, - targetIOSSdk: IOSSdk.iPhoneOs, - cCompiler: CCompilerConfig( + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOs, + cCompiler: CCompilerConfigImpl( cc: fakeClang, ld: fakeLd, ), - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, ); final configFile = buildConfig1.toYaml(); final config = Config(fileParsed: configFile); - final fromConfig = BuildConfig.fromConfig(config); + final fromConfig = BuildConfigImpl.fromConfig(config); expect(fromConfig, equals(buildConfig1)); }); test('BuildConfig == dependency metadata', () { - final buildConfig1 = BuildConfig( + final buildConfig1 = BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: tempUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.android, targetAndroidNdkApi: 30, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, dependencyMetadata: { 'bar': const Metadata({ 'key': 'value', @@ -187,15 +187,15 @@ void main() async { }, ); - final buildConfig2 = BuildConfig( + final buildConfig2 = BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: tempUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.android, targetAndroidNdkApi: 30, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, dependencyMetadata: { 'bar': const Metadata({ 'key': 'value', @@ -213,19 +213,19 @@ void main() async { test('BuildConfig toYaml fromYaml', () { final outDir = outDirUri; - final buildConfig1 = BuildConfig( + final buildConfig1 = BuildConfigImpl( outDir: outDir, packageName: packageName, packageRoot: tempUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.iOS, - targetIOSSdk: IOSSdk.iPhoneOs, - cCompiler: CCompilerConfig( + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOs, + cCompiler: CCompilerConfigImpl( cc: fakeClang, ld: fakeLd, ), - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, // This map should be sorted on key for two layers. dependencyMetadata: { 'foo': const Metadata({ @@ -257,10 +257,10 @@ package_root: ${tempUri.toFilePath()} target_architecture: arm64 target_ios_sdk: iphoneos target_os: ios -version: ${BuildConfig.version}'''; +version: ${BuildConfigImpl.version}'''; expect(yamlString, equals(expectedYamlString)); - final buildConfig2 = BuildConfig.fromConfig( + final buildConfig2 = BuildConfigImpl.fromConfig( Config.fromConfigFileContents( fileContents: yamlString, ), @@ -270,7 +270,7 @@ version: ${BuildConfig.version}'''; test('BuildConfig FormatExceptions', () { expect( - () => BuildConfig.fromConfig(Config(fileParsed: {})), + () => BuildConfigImpl.fromConfig(Config(fileParsed: {})), throwsA(predicate( (e) => e is FormatException && @@ -280,8 +280,8 @@ version: ${BuildConfig.version}'''; )), ); expect( - () => BuildConfig.fromConfig(Config(fileParsed: { - 'version': BuildConfig.version.toString(), + () => BuildConfigImpl.fromConfig(Config(fileParsed: { + 'version': BuildConfigImpl.version.toString(), 'package_name': packageName, 'package_root': packageRootUri.toFilePath(), 'target_architecture': 'arm64', @@ -298,8 +298,8 @@ version: ${BuildConfig.version}'''; )), ); expect( - () => BuildConfig.fromConfig(Config(fileParsed: { - 'version': BuildConfig.version.toString(), + () => BuildConfigImpl.fromConfig(Config(fileParsed: { + 'version': BuildConfigImpl.version.toString(), 'out_dir': outDirUri.toFilePath(), 'package_name': packageName, 'package_root': packageRootUri.toFilePath(), @@ -322,9 +322,9 @@ version: ${BuildConfig.version}'''; )), ); expect( - () => BuildConfig.fromConfig(Config(fileParsed: { + () => BuildConfigImpl.fromConfig(Config(fileParsed: { 'out_dir': outDirUri.toFilePath(), - 'version': BuildConfig.version.toString(), + 'version': BuildConfigImpl.version.toString(), 'package_name': packageName, 'package_root': packageRootUri.toFilePath(), 'target_architecture': 'arm64', @@ -343,7 +343,7 @@ version: ${BuildConfig.version}'''; test('FormatExceptions contain full stack trace of wrapped exception', () { try { - BuildConfig.fromConfig(Config(fileParsed: { + BuildConfigImpl.fromConfig(Config(fileParsed: { 'out_dir': outDirUri.toFilePath(), 'package_root': packageRootUri.toFilePath(), 'target': [1, 2, 3, 4, 5], @@ -355,39 +355,39 @@ version: ${BuildConfig.version}'''; }); test('BuildConfig toString', () { - final config = BuildConfig( + final config = BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: tempUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.iOS, - targetIOSSdk: IOSSdk.iPhoneOs, - cCompiler: CCompilerConfig( + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOs, + cCompiler: CCompilerConfigImpl( cc: fakeClang, ld: fakeLd, ), - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, ); config.toString(); }); test('BuildConfig fromArgs', () async { - final buildConfig = BuildConfig( + final buildConfig = BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: tempUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.android, targetAndroidNdkApi: 30, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, ); final configFileContents = buildConfig.toYamlString(); final configUri = tempUri.resolve('config.yaml'); final configFile = File.fromUri(configUri); await configFile.writeAsString(configFileContents); - final buildConfig2 = await BuildConfig.fromArgs( + final buildConfig2 = await BuildConfigImpl.fromArgs( ['--config', configUri.toFilePath()], environment: {}, // Don't inherit the test environment. ); @@ -395,15 +395,15 @@ version: ${BuildConfig.version}'''; }); test('dependency metadata via config accessor', () { - final buildConfig1 = BuildConfig( + final buildConfig1 = BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: tempUri, - targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.android, targetAndroidNdkApi: 30, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.preferStatic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, dependencyMetadata: { 'bar': const Metadata({ 'key': {'key2': 'value'}, @@ -413,7 +413,7 @@ version: ${BuildConfig.version}'''; // Useful for doing `path(..., exists: true)`. expect( buildConfig1.config.string([ - BuildConfig.dependencyMetadataConfigKey, + BuildConfigImpl.dependencyMetadataConfigKey, 'bar', 'key', 'key2' @@ -423,24 +423,24 @@ version: ${BuildConfig.version}'''; }); test('envScript', () { - final buildConfig1 = BuildConfig( + final buildConfig1 = BuildConfigImpl( outDir: outDirUri, packageName: packageName, packageRoot: packageRootUri, - targetArchitecture: Architecture.x64, - targetOs: OS.windows, - cCompiler: CCompilerConfig( + targetArchitecture: ArchitectureImpl.x64, + targetOs: OSImpl.windows, + cCompiler: CCompilerConfigImpl( cc: fakeCl, envScript: fakeVcVars, envScriptArgs: ['x64'], ), - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.dynamic, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.dynamic, ); final configFile = buildConfig1.toYaml(); final config = Config(fileParsed: configFile); - final fromConfig = BuildConfig.fromConfig(config); + final fromConfig = BuildConfigImpl.fromConfig(config); expect(fromConfig, equals(buildConfig1)); }); @@ -456,12 +456,12 @@ version: ${BuildConfig.version}'''; 'version': version, }); expect( - () => BuildConfig.fromConfig(config), + () => BuildConfigImpl.fromConfig(config), throwsA(predicate( (e) => e is FormatException && e.message.contains(version) && - e.message.contains(BuildConfig.version.toString()), + e.message.contains(BuildConfigImpl.version.toString()), )), ); }); @@ -473,26 +473,26 @@ version: ${BuildConfig.version}'''; final fakeClangUri = tempUri.resolve('fake_clang'); await File.fromUri(fakeClangUri).create(); - final name1 = BuildConfig.checksum( + final name1 = BuildConfigImpl.checksum( packageName: packageName, packageRoot: nativeAddUri, - targetArchitecture: Architecture.x64, - targetOs: OS.linux, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.dynamic, + targetArchitecture: ArchitectureImpl.x64, + targetOs: OSImpl.linux, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.dynamic, ); // Using the checksum for a build folder should be stable. expect(name1, '037109b9824b2559502fa7bd42e1b6f8'); // Build folder different due to metadata. - final name2 = BuildConfig.checksum( + final name2 = BuildConfigImpl.checksum( packageName: packageName, packageRoot: nativeAddUri, - targetArchitecture: Architecture.x64, - targetOs: OS.linux, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.dynamic, + targetArchitecture: ArchitectureImpl.x64, + targetOs: OSImpl.linux, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.dynamic, dependencyMetadata: { 'foo': const Metadata({'key': 'value'}) }, @@ -501,14 +501,14 @@ version: ${BuildConfig.version}'''; expect(name1 != name2, true); // Build folder different due to cc. - final name3 = BuildConfig.checksum( + final name3 = BuildConfigImpl.checksum( packageName: packageName, packageRoot: nativeAddUri, - targetArchitecture: Architecture.x64, - targetOs: OS.linux, - buildMode: BuildMode.release, - linkModePreference: LinkModePreference.dynamic, - cCompiler: CCompilerConfig( + targetArchitecture: ArchitectureImpl.x64, + targetOs: OSImpl.linux, + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.dynamic, + cCompiler: CCompilerConfigImpl( cc: fakeClangUri, )); printOnFailure([name1, name3].toString()); @@ -526,10 +526,10 @@ version: ${BuildConfig.version}'''; 'target_os': 'windows', 'target_architecture': 'arm', 'build_mode': 'debug', - 'version': BuildConfig.version.toString(), + 'version': BuildConfigImpl.version.toString(), }); expect( - () => BuildConfig.fromConfig(config), + () => BuildConfigImpl.fromConfig(config), throwsA(predicate( (e) => e is FormatException && e.message.contains('arm'), )), @@ -547,10 +547,10 @@ version: ${BuildConfig.version}'''; 'target_architecture': 'arm64', 'build_mode': 'debug', 'dry_run': true, - 'version': BuildConfig.version.toString(), + 'version': BuildConfigImpl.version.toString(), }); expect( - () => BuildConfig.fromConfig(config), + () => BuildConfigImpl.fromConfig(config), throwsA(predicate( (e) => e is FormatException && e.message.contains('In Flutter projects'), @@ -567,9 +567,9 @@ version: ${BuildConfig.version}'''; 'package_root': tempUri.toFilePath(), 'target_os': 'windows', 'dry_run': true, - 'version': BuildConfig.version.toString(), + 'version': BuildConfigImpl.version.toString(), }); - final buildConfig = BuildConfig.fromConfig(config); + final buildConfig = BuildConfigImpl.fromConfig(config); expect( () => buildConfig.targetArchitecture, throwsA(predicate( @@ -579,12 +579,12 @@ version: ${BuildConfig.version}'''; }); test('BuildConfig dry_run access invalid args', () { - final buildConfig = BuildConfig.dryRun( + final buildConfig = BuildConfigImpl.dryRun( packageName: packageName, outDir: outDirUri, packageRoot: tempUri, - targetOs: OS.windows, - linkModePreference: LinkModePreference.dynamic, + targetOs: OSImpl.windows, + linkModePreference: LinkModePreferenceImpl.dynamic, ); buildConfig.toYamlString(); // No crash. diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 8aa2ab65f..49c06d4c0 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -18,20 +18,20 @@ void main() { await Directory.fromUri(tempUri).delete(recursive: true); }); - final buildOutput = BuildOutput( + final buildOutput = BuildOutputImpl( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ - Asset( + AssetImpl( id: 'foo', - path: AssetAbsolutePath(Uri(path: 'path/to/libfoo.so')), - target: Target.androidX64, - linkMode: LinkMode.dynamic, + path: AssetAbsolutePathImpl(Uri(path: 'path/to/libfoo.so')), + target: TargetImpl.androidX64, + linkMode: LinkModeImpl.dynamic, ), - Asset( + AssetImpl( id: 'foo2', - path: AssetSystemPath(Uri(path: 'path/to/libfoo2.so')), - target: Target.androidX64, - linkMode: LinkMode.dynamic, + path: AssetSystemPathImpl(Uri(path: 'path/to/libfoo2.so')), + target: TargetImpl.androidX64, + linkMode: LinkModeImpl.dynamic, ), ], dependencies: Dependencies([ @@ -60,12 +60,12 @@ dependencies: - path/to/file.ext metadata: key: value -version: ${BuildOutput.version}'''; +version: ${BuildOutputImpl.version}'''; test('built info yaml', () { final yaml = buildOutput.toYamlString().replaceAll('\\', '/'); expect(yaml, yamlEncoding); - final buildOutput2 = BuildOutput.fromYamlString(yaml); + final buildOutput2 = BuildOutputImpl.fromYamlString(yaml); expect(buildOutput.hashCode, buildOutput2.hashCode); expect(buildOutput, buildOutput2); }); @@ -73,10 +73,10 @@ version: ${BuildOutput.version}'''; test('BuildOutput.toString', buildOutput.toString); test('BuildOutput.hashCode', () { - final buildOutput2 = BuildOutput.fromYamlString(yamlEncoding); + final buildOutput2 = BuildOutputImpl.fromYamlString(yamlEncoding); expect(buildOutput.hashCode, buildOutput2.hashCode); - final buildOutput3 = BuildOutput( + final buildOutput3 = BuildOutputImpl( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), ); expect(buildOutput.hashCode != buildOutput3.hashCode, true); @@ -85,12 +85,12 @@ version: ${BuildOutput.version}'''; test('BuildOutput.readFromFile BuildOutput.writeToFile', () async { final outDir = tempUri.resolve('out_dir/'); await buildOutput.writeToFile(outDir: outDir); - final buildOutput2 = await BuildOutput.readFromFile(outDir: outDir); + final buildOutput2 = await BuildOutputImpl.readFromFile(outDir: outDir); expect(buildOutput2, buildOutput); }); test('Round timestamp', () { - final buildOutput3 = BuildOutput( + final buildOutput3 = BuildOutputImpl( timestamp: DateTime.parse('2022-11-10 13:25:01.372257'), ); expect(buildOutput3.timestamp, DateTime.parse('2022-11-10 13:25:01.000')); @@ -99,12 +99,12 @@ version: ${BuildOutput.version}'''; for (final version in ['9001.0.0', '0.0.1']) { test('BuildOutput version $version', () { expect( - () => BuildOutput.fromYamlString('version: $version'), + () => BuildOutputImpl.fromYamlString('version: $version'), throwsA(predicate( (e) => e is FormatException && e.message.contains(version) && - e.message.contains(BuildConfig.version.toString()), + e.message.contains(BuildConfigImpl.version.toString()), )), ); }); @@ -112,7 +112,7 @@ version: ${BuildOutput.version}'''; test('format exception', () { expect( - () => BuildOutput.fromYamlString('''timestamp: 2022-11-10 13:25:01.000 + () => BuildOutputImpl.fromYamlString('''timestamp: 2022-11-10 13:25:01.000 assets: - name: foo link_mode: dynamic @@ -124,11 +124,11 @@ assets: dependencies: [] metadata: key: value -version: ${BuildOutput.version}'''), +version: ${BuildOutputImpl.version}'''), throwsFormatException, ); expect( - () => BuildOutput.fromYamlString('''timestamp: 2022-11-10 13:25:01.000 + () => BuildOutputImpl.fromYamlString('''timestamp: 2022-11-10 13:25:01.000 assets: - name: foo link_mode: dynamic @@ -140,11 +140,11 @@ dependencies: 1: foo metadata: key: value -version: ${BuildOutput.version}'''), +version: ${BuildOutputImpl.version}'''), throwsFormatException, ); expect( - () => BuildOutput.fromYamlString('''timestamp: 2022-11-10 13:25:01.000 + () => BuildOutputImpl.fromYamlString('''timestamp: 2022-11-10 13:25:01.000 assets: - name: foo link_mode: dynamic @@ -155,7 +155,7 @@ assets: dependencies: [] metadata: 123: value -version: ${BuildOutput.version}'''), +version: ${BuildOutputImpl.version}'''), throwsFormatException, ); }); @@ -163,7 +163,7 @@ version: ${BuildOutput.version}'''), test('BuildOutput dependencies can be modified', () { // TODO(https://github.com/dart-lang/native/issues/25): // Remove once dependencies are made immutable. - final buildOutput = BuildOutput(); + final buildOutput = BuildOutputImpl(); expect( () => buildOutput.addDependencies([Uri.file('path/to/file.ext')]), returnsNormally, diff --git a/pkgs/native_assets_cli/test/model/target_test.dart b/pkgs/native_assets_cli/test/model/target_test.dart index 412082b2a..4672c4454 100644 --- a/pkgs/native_assets_cli/test/model/target_test.dart +++ b/pkgs/native_assets_cli/test/model/target_test.dart @@ -10,25 +10,27 @@ import 'package:test/test.dart'; void main() { test('OS naming conventions', () async { - expect(OS.android.dylibFileName('foo'), 'libfoo.so'); - expect(OS.android.staticlibFileName('foo'), 'libfoo.a'); - expect(OS.windows.dylibFileName('foo'), 'foo.dll'); - expect(OS.windows.libraryFileName('foo', LinkMode.dynamic), 'foo.dll'); - expect(OS.windows.staticlibFileName('foo'), 'foo.lib'); - expect(OS.windows.libraryFileName('foo', LinkMode.static), 'foo.lib'); - expect(OS.windows.executableFileName('foo'), 'foo.exe'); + expect(OSImpl.android.dylibFileName('foo'), 'libfoo.so'); + expect(OSImpl.android.staticlibFileName('foo'), 'libfoo.a'); + expect(OSImpl.windows.dylibFileName('foo'), 'foo.dll'); + expect( + OSImpl.windows.libraryFileName('foo', LinkModeImpl.dynamic), 'foo.dll'); + expect(OSImpl.windows.staticlibFileName('foo'), 'foo.lib'); + expect( + OSImpl.windows.libraryFileName('foo', LinkModeImpl.static), 'foo.lib'); + expect(OSImpl.windows.executableFileName('foo'), 'foo.exe'); }); test('Target current', () async { - final current = Target.current; + final current = TargetImpl.current; expect(current.toString(), Abi.current().toString()); }); test('Target fromDartPlatform', () async { - final current = Target.fromDartPlatform(Platform.version); + final current = TargetImpl.fromDartPlatform(Platform.version); expect(current.toString(), Abi.current().toString()); expect( - () => Target.fromDartPlatform('bogus'), + () => TargetImpl.fromDartPlatform('bogus'), throwsA(predicate( (e) => e is FormatException && @@ -37,7 +39,7 @@ void main() { )), ); expect( - () => Target.fromDartPlatform( + () => TargetImpl.fromDartPlatform( '3.0.0 (be) (Wed Apr 5 14:19:42 2023 +0000) on "myfancyos_ia32"', ), throwsA(predicate( @@ -51,19 +53,20 @@ void main() { test('Target cross compilation', () async { // All hosts can cross compile to Android. - expect( - Target.current.supportedTargetTargets(), contains(Target.androidArm64)); - expect( - Target.macOSArm64.supportedTargetTargets(), contains(Target.iOSArm64)); + expect(TargetImpl.current.supportedTargetTargets(), + contains(TargetImpl.androidArm64)); + expect(TargetImpl.macOSArm64.supportedTargetTargets(), + contains(TargetImpl.iOSArm64)); }); test('Target fromArchitectureAndOs', () async { - final current = - Target.fromArchitectureAndOs(Architecture.current, OS.current); + final current = TargetImpl.fromArchitectureAndOs( + ArchitectureImpl.current, OSImpl.current); expect(current.toString(), Abi.current().toString()); expect( - () => Target.fromArchitectureAndOs(Architecture.arm, OS.windows), + () => TargetImpl.fromArchitectureAndOs( + ArchitectureImpl.arm, OSImpl.windows), throwsA(predicate( (e) => e is ArgumentError && From 7af60c02471b847320773ebe70f499e81d6f62c8 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 16 Feb 2024 19:13:48 +0100 Subject: [PATCH 15/75] Rename `Asset` to `CCodeAsset` --- .../lib/src/build_runner/build_runner.dart | 19 +++--- .../test/build_runner/helpers.dart | 4 +- .../wrong_namespace_asset/build.dart | 2 +- .../example/local_asset/build.dart | 2 +- .../lib/native_assets_cli.dart | 2 +- .../lib/native_assets_cli_internal.dart | 2 +- pkgs/native_assets_cli/lib/src/api/asset.dart | 10 +++- .../lib/src/api/build_output.dart | 16 ++--- .../lib/src/helpers/asset_downloader.dart | 2 +- .../lib/src/model/asset.dart | 22 +++---- .../lib/src/model/build_output.dart | 19 +++--- .../test/api/asset_test.dart | 12 ++-- .../test/api/build_output_test.dart | 4 +- pkgs/native_assets_cli/test/helpers.dart | 4 +- .../test/model/asset_test.dart | 60 +++++++++++++++---- .../test/model/build_output_test.dart | 4 +- .../lib/src/cbuilder/cbuilder.dart | 2 +- 17 files changed, 116 insertions(+), 70 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 11ff277e7..234420ff0 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -77,7 +77,7 @@ class NativeAssetsBuildRunner { buildPlan = plan; packageGraph = planner.packageGraph; } - final assets = []; + final assets = []; final dependencies = []; final metadata = {}; var success = true; @@ -163,7 +163,7 @@ class NativeAssetsBuildRunner { } buildPlan = plan; } - final assets = []; + final assets = []; var success = true; for (final package in buildPlan) { final config = await _cliConfigDryRun( @@ -296,7 +296,7 @@ build_output.yaml contained a format error. ${e.message} '''); success = false; - return ([], [], const Metadata({}), false); + return ([], [], const Metadata({}), false); // TODO(https://github.com/dart-lang/native/issues/109): Stop throwing // type errors in native_assets_cli, release a new version of that package // and then remove this. @@ -307,7 +307,7 @@ Building native assets for package:${config.packageName} failed. build_output.yaml contained a format error. '''); success = false; - return ([], [], const Metadata({}), false); + return ([], [], const Metadata({}), false); } finally { if (!success) { final buildOutputFile = @@ -401,7 +401,8 @@ build_output.yaml contained a format error. }; } - bool validateAssetsPackage(Iterable assets, String packageName) { + bool validateAssetsPackage( + Iterable assets, String packageName) { final invalidAssetIds = assets .map((a) => a.id) .where((n) => !n.startsWith('package:$packageName/')) @@ -420,7 +421,7 @@ build_output.yaml contained a format error. } typedef _PackageBuildRecord = ( - Iterable, + Iterable, Iterable dependencies, Metadata?, bool success, @@ -429,7 +430,7 @@ typedef _PackageBuildRecord = ( /// The result from a [NativeAssetsBuildRunner.dryRun]. abstract interface class DryRunResult { /// The native assets for all [TargetImpl]s for the build or dry run. - List get assets; + List get assets; /// Whether all builds completed without errors. /// @@ -439,7 +440,7 @@ abstract interface class DryRunResult { final class _DryRunResultImpl implements DryRunResult { @override - final List assets; + final List assets; @override final bool success; @@ -463,7 +464,7 @@ abstract class BuildResult implements DryRunResult { final class _BuildResultImpl implements BuildResult { @override - final List assets; + final List assets; @override final List dependencies; diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index 498b92824..e583dd52e 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -105,7 +105,7 @@ Future dryRun( return result; } -Future expectAssetsExist(List assets) async { +Future expectAssetsExist(List assets) async { for (final asset in assets) { final uri = (asset.path as AssetAbsolutePathImpl).uri; expect( @@ -118,7 +118,7 @@ Future expectAssetsExist(List assets) async { } Future expectSymbols({ - required AssetImpl asset, + required CCodeAssetImpl asset, required List symbols, }) async { if (Platform.isLinux) { diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 457aa902b..9817d3a0b 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -8,7 +8,7 @@ void main(List args) async { final buildConfig = await BuildConfig.fromArgs(args); final buildOutput = BuildOutput( assets: [ - Asset( + CCodeAsset( id: 'package:other_package/foo', linkMode: LinkMode.dynamic, target: Target.current, diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index f6e8eaa52..3cbdc2513 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -41,7 +41,7 @@ void main(List args) async { output.addAssets([ for (final target in targets) - Asset( + CCodeAsset( id: 'library:$packageName/asset.txt', linkMode: LinkMode.dynamic, target: target, diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index e5d7fe8c9..56ba2d7b9 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -8,7 +8,7 @@ library native_assets_cli; export 'src/api/asset.dart' show - Asset, + CCodeAsset, AssetAbsolutePath, AssetInExecutable, AssetInProcess, diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index 74f790ec5..0ac3ec9c2 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -11,7 +11,7 @@ library native_assets_cli_internal; export 'src/api/asset.dart' show AssetAbsolutePathImpl, - AssetImpl, + CCodeAssetImpl, AssetInExecutableImpl, AssetInProcessImpl, AssetSystemPathImpl; diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index 83a9984c6..18d887568 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -40,19 +40,23 @@ abstract final class AssetInExecutable implements AssetPath { factory AssetInExecutable() = AssetInExecutableImpl; } -abstract final class Asset { +/// A code asset which respects the C application binary interface (ABI). +/// +/// Typical other languages which produce code assets that respect the C ABI +/// include C++ and Rust. +abstract final class CCodeAsset { LinkMode get linkMode; String get id; Target get target; AssetPath get path; - factory Asset({ + factory CCodeAsset({ required String id, required LinkMode linkMode, required Target target, required AssetPath path, }) => - AssetImpl( + CCodeAssetImpl( id: id, linkMode: linkMode as LinkModeImpl, target: target as TargetImpl, diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index c0adb8c9c..4ab1737f8 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -37,7 +37,7 @@ abstract class BuildOutput { /// /// In dry runs, the assets for all [Architecture]s for the [OS] specified in /// the dry run must be provided. - Iterable get assets; + Iterable get assets; /// The files used by this build. /// @@ -54,7 +54,7 @@ abstract class BuildOutput { /// seconds, because [File.lastModified] is rounded to whole seconds and /// caching logic compares these timestamps. /// - /// The [Asset]s produced by this build or dry-run can be provided to the + /// The [CCodeAsset]s produced by this build or dry-run can be provided to the /// constructor as [assets], or can be added later using [addAssets]. In dry /// runs, the assets for all [Architecture]s for the [OS] specified in the dry /// run must be provided. @@ -68,28 +68,28 @@ abstract class BuildOutput { /// via [metadata] or [addMetadata]. factory BuildOutput({ DateTime? timestamp, - Iterable? assets, + Iterable? assets, Iterable? dependencies, Map? metadata, }) => BuildOutputImpl( timestamp: timestamp, - assets: assets?.cast().toList(), + assets: assets?.cast().toList(), dependencies: Dependencies([...?dependencies]), metadata: Metadata({...?metadata}), ); - /// Adds [Asset]s produced by this build or dry run. + /// Adds [CCodeAsset]s produced by this build or dry run. /// /// In dry runs, the assets for all [Architecture]s for the [OS] specified in /// the dry run must be provided. - void addAsset(Asset asset); + void addAsset(CCodeAsset asset); - /// Adds [Asset]s produced by this build or dry run. + /// Adds [CCodeAsset]s produced by this build or dry run. /// /// In dry runs, the assets for all [Architecture]s for the [OS] specified in /// the dry run must be provided. - void addAssets(Iterable assets); + void addAssets(Iterable assets); /// Adds file used by this build. /// diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart index a67771304..6dcbd4201 100644 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -60,7 +60,7 @@ class AssetDownloader implements Builder { await response.pipe(File.fromUri(targetUri).openWrite()); } buildOutput.addAssets([ - Asset( + CCodeAsset( id: assetId, linkMode: LinkMode.dynamic, path: AssetAbsolutePath(targetUri), diff --git a/pkgs/native_assets_cli/lib/src/model/asset.dart b/pkgs/native_assets_cli/lib/src/model/asset.dart index 53bfd508f..878d67a99 100644 --- a/pkgs/native_assets_cli/lib/src/model/asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/asset.dart @@ -122,7 +122,7 @@ final class AssetInExecutableImpl implements AssetPathImpl, AssetInExecutable { }; } -final class AssetImpl implements Asset { +final class CCodeAssetImpl implements CCodeAsset { @override final LinkModeImpl linkMode; @override @@ -132,43 +132,43 @@ final class AssetImpl implements Asset { @override final AssetPathImpl path; - AssetImpl({ + CCodeAssetImpl({ required this.id, required this.linkMode, required this.target, required this.path, }); - factory AssetImpl.fromYaml(YamlMap yamlMap) => AssetImpl( + factory CCodeAssetImpl.fromYaml(YamlMap yamlMap) => CCodeAssetImpl( id: as(yamlMap[_idKey]), path: AssetPathImpl.fromYaml(as(yamlMap[_pathKey])), target: TargetImpl.fromString(as(yamlMap[_targetKey])), linkMode: LinkModeImpl.fromName(as(yamlMap[_linkModeKey])), ); - static List listFromYamlString(String yaml) { + static List listFromYamlString(String yaml) { final yamlObject = loadYaml(yaml); if (yamlObject == null) { return []; } return [ for (final yamlElement in as(yamlObject)) - AssetImpl.fromYaml(as(yamlElement)), + CCodeAssetImpl.fromYaml(as(yamlElement)), ]; } - static List listFromYamlList(YamlList yamlList) => [ + static List listFromYamlList(YamlList yamlList) => [ for (final yamlElement in yamlList) - AssetImpl.fromYaml(as(yamlElement)), + CCodeAssetImpl.fromYaml(as(yamlElement)), ]; - AssetImpl copyWith({ + CCodeAssetImpl copyWith({ LinkModeImpl? linkMode, String? id, Target? target, AssetPathImpl? path, }) => - AssetImpl( + CCodeAssetImpl( id: id ?? this.id, linkMode: linkMode ?? this.linkMode, target: target ?? this.target, @@ -177,7 +177,7 @@ final class AssetImpl implements Asset { @override bool operator ==(Object other) { - if (other is! AssetImpl) { + if (other is! CCodeAssetImpl) { return false; } return other.id == id && @@ -207,7 +207,7 @@ final class AssetImpl implements Asset { String toString() => 'Asset(${toYaml()})'; } -extension AssetIterable on Iterable { +extension AssetIterable on Iterable { List toYaml() => [for (final item in this) item.toYaml()]; String toYamlString() => yamlEncode(toYaml()); diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index 711658325..7bcb2f53c 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -8,10 +8,10 @@ class BuildOutputImpl implements BuildOutput { @override final DateTime timestamp; - final List _assets; + final List _assets; @override - Iterable get assets => _assets; + Iterable get assets => _assets; final Dependencies _dependencies; @@ -24,7 +24,7 @@ class BuildOutputImpl implements BuildOutput { BuildOutputImpl({ DateTime? timestamp, - List? assets, + List? assets, Dependencies? dependencies, Metadata? metadata, }) : timestamp = (timestamp ?? DateTime.now()).roundDownToSeconds(), @@ -72,7 +72,8 @@ class BuildOutputImpl implements BuildOutput { return BuildOutputImpl( timestamp: DateTime.parse(as(yamlMap[_timestampKey])), - assets: AssetImpl.listFromYamlList(as(yamlMap[_assetsKey])), + assets: + CCodeAssetImpl.listFromYamlList(as(yamlMap[_assetsKey])), dependencies: Dependencies.fromYaml(as(yamlMap[_dependenciesKey])), metadata: Metadata.fromYaml(as(yamlMap[_metadataKey])), @@ -129,7 +130,7 @@ class BuildOutputImpl implements BuildOutput { return false; } return other.timestamp == timestamp && - const ListEquality().equals(other._assets, _assets) && + const ListEquality().equals(other._assets, _assets) && other._dependencies == _dependencies && other._metadata == _metadata; } @@ -137,7 +138,7 @@ class BuildOutputImpl implements BuildOutput { @override int get hashCode => Object.hash( timestamp.hashCode, - const ListEquality().hash(_assets), + const ListEquality().hash(_assets), _dependencies, _metadata, ); @@ -155,12 +156,12 @@ class BuildOutputImpl implements BuildOutput { Metadata get metadataModel => _metadata; @override - void addAsset(Asset asset) { - _assets.add(asset as AssetImpl); + void addAsset(CCodeAsset asset) { + _assets.add(asset as CCodeAssetImpl); } @override - void addAssets(Iterable assets) { + void addAssets(Iterable assets) { _assets.addAll(assets.cast()); } } diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index 54f2c61bc..7353b0ebe 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -8,37 +8,37 @@ import 'package:test/test.dart'; void main() { test('Asset constructors', () async { final assets = [ - Asset( + CCodeAsset( id: 'foo', path: AssetAbsolutePath(Uri.file('path/to/libfoo.so')), target: Target.androidX64, linkMode: LinkMode.dynamic, ), - Asset( + CCodeAsset( id: 'foo3', path: AssetSystemPath(Uri(path: 'libfoo3.so')), target: Target.androidX64, linkMode: LinkMode.dynamic, ), - Asset( + CCodeAsset( id: 'foo4', path: AssetInExecutable(), target: Target.androidX64, linkMode: LinkMode.dynamic, ), - Asset( + CCodeAsset( id: 'foo5', path: AssetInProcess(), target: Target.androidX64, linkMode: LinkMode.dynamic, ), - Asset( + CCodeAsset( id: 'bar', path: AssetAbsolutePath(Uri(path: 'path/to/libbar.a')), target: Target.linuxArm64, linkMode: LinkMode.static, ), - Asset( + CCodeAsset( id: 'bla', path: AssetAbsolutePath(Uri(path: 'path/with spaces/bla.dll')), target: Target.windowsX64, diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index 0307911cf..ed74fe26e 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -22,13 +22,13 @@ void main() { BuildOutput( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ - Asset( + CCodeAsset( id: 'foo', path: AssetAbsolutePath(Uri(path: 'path/to/libfoo.so')), target: Target.androidX64, linkMode: LinkMode.dynamic, ), - Asset( + CCodeAsset( id: 'foo2', path: AssetSystemPath(Uri(path: 'path/to/libfoo2.so')), target: Target.androidX64, diff --git a/pkgs/native_assets_cli/test/helpers.dart b/pkgs/native_assets_cli/test/helpers.dart index 1e921950b..c9cbd6b08 100644 --- a/pkgs/native_assets_cli/test/helpers.dart +++ b/pkgs/native_assets_cli/test/helpers.dart @@ -113,7 +113,7 @@ extension on String { Uri asFileUri() => Uri.file(this); } -extension AssetIterable on Iterable { +extension AssetIterable on Iterable { Future allExist() async { final allResults = await Future.wait(map((e) => e.exists())); final missing = allResults.contains(false); @@ -121,7 +121,7 @@ extension AssetIterable on Iterable { } } -extension on Asset { +extension on CCodeAsset { Future exists() async { final path_ = path; return switch (path_) { diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 8a4e3faba..fd78f7f46 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -13,37 +13,37 @@ void main() { final barUri = Uri(path: 'path/to/libbar.a'); final blaUri = Uri(path: 'path/with spaces/bla.dll'); final assets = [ - AssetImpl( + CCodeAssetImpl( id: 'foo', path: AssetAbsolutePathImpl(fooUri), target: TargetImpl.androidX64, linkMode: LinkModeImpl.dynamic, ), - AssetImpl( + CCodeAssetImpl( id: 'foo3', path: AssetSystemPathImpl(foo3Uri), target: TargetImpl.androidX64, linkMode: LinkModeImpl.dynamic, ), - AssetImpl( + CCodeAssetImpl( id: 'foo4', path: AssetInExecutableImpl(), target: TargetImpl.androidX64, linkMode: LinkModeImpl.dynamic, ), - AssetImpl( + CCodeAssetImpl( id: 'foo5', path: AssetInProcessImpl(), target: TargetImpl.androidX64, linkMode: LinkModeImpl.dynamic, ), - AssetImpl( + CCodeAssetImpl( id: 'bar', path: AssetAbsolutePathImpl(barUri), target: TargetImpl.linuxArm64, linkMode: LinkModeImpl.static, ), - AssetImpl( + CCodeAssetImpl( id: 'bla', path: AssetAbsolutePathImpl(blaUri), target: TargetImpl.windowsX64, @@ -51,6 +51,41 @@ void main() { ), ]; + final assetsYamlEncodingV1_0_0 = '''- id: foo + link_mode: dynamic + path: + path_type: absolute + uri: ${fooUri.toFilePath()} + target: android_x64 +- id: foo3 + link_mode: dynamic + path: + path_type: system + uri: ${foo3Uri.toFilePath()} + target: android_x64 +- id: foo4 + link_mode: dynamic + path: + path_type: executable + target: android_x64 +- id: foo5 + link_mode: dynamic + path: + path_type: process + target: android_x64 +- id: bar + link_mode: static + path: + path_type: absolute + uri: ${barUri.toFilePath()} + target: linux_arm64 +- id: bla + link_mode: dynamic + path: + path_type: absolute + uri: ${blaUri.toFilePath()} + target: windows_x64'''; + final assetsYamlEncoding = '''- id: foo link_mode: dynamic path: @@ -89,7 +124,12 @@ void main() { test('asset yaml', () { final yaml = assets.toYamlString(); expect(yaml, assetsYamlEncoding); - final assets2 = AssetImpl.listFromYamlString(yaml); + final assets2 = CCodeAssetImpl.listFromYamlString(yaml); + expect(assets, assets2); + }); + + test('build_output protocol v1.0.0 keeps working', () { + final assets2 = CCodeAssetImpl.listFromYamlString(assetsYamlEncodingV1_0_0); expect(assets, assets2); }); @@ -113,7 +153,7 @@ void main() { test('List hashCode', () async { final assets2 = assets.take(3).toList(); - const equality = ListEquality(); + const equality = ListEquality(); expect(equality.hash(assets) != equality.hash(assets2), true); }); @@ -122,7 +162,7 @@ void main() { }); test('Asset listFromYamlString', () async { - final assets = AssetImpl.listFromYamlString(''); - expect(assets, []); + final assets = CCodeAssetImpl.listFromYamlString(''); + expect(assets, []); }); } diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 49c06d4c0..36f2aaea5 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -21,13 +21,13 @@ void main() { final buildOutput = BuildOutputImpl( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ - AssetImpl( + CCodeAssetImpl( id: 'foo', path: AssetAbsolutePathImpl(Uri(path: 'path/to/libfoo.so')), target: TargetImpl.androidX64, linkMode: LinkModeImpl.dynamic, ), - AssetImpl( + CCodeAssetImpl( id: 'foo2', path: AssetSystemPathImpl(Uri(path: 'path/to/libfoo2.so')), target: TargetImpl.androidX64, diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 1e9142427..c8558cf23 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -256,7 +256,7 @@ class CBuilder implements Builder { ]; for (final target in targets) { buildOutput.addAssets([ - Asset( + CCodeAsset( id: assetId!, linkMode: linkMode, target: target, From e6794fbcea7d2f74eec6328d8d5a095420e29913 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 16 Feb 2024 19:15:47 +0100 Subject: [PATCH 16/75] Move c code assets --- pkgs/native_assets_cli/lib/native_assets_cli.dart | 14 +++++++------- .../lib/native_assets_cli_internal.dart | 12 ++++++------ .../lib/src/api/build_output.dart | 2 +- .../lib/src/api/{asset.dart => c_code_asset.dart} | 2 +- .../src/model/{asset.dart => c_code_asset.dart} | 2 +- pkgs/native_assets_cli/test/model/asset_test.dart | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) rename pkgs/native_assets_cli/lib/src/api/{asset.dart => c_code_asset.dart} (98%) rename pkgs/native_assets_cli/lib/src/model/{asset.dart => c_code_asset.dart} (99%) diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 56ba2d7b9..8d9a9cf94 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -6,17 +6,17 @@ /// native assets CLI, e.g. a `build.dart` script. library native_assets_cli; -export 'src/api/asset.dart' - show - CCodeAsset, - AssetAbsolutePath, - AssetInExecutable, - AssetInProcess, - AssetSystemPath; export 'src/api/build.dart'; export 'src/api/build_config.dart' show BuildConfig, CCompilerConfig; export 'src/api/build_mode.dart' show BuildMode; export 'src/api/build_output.dart' show BuildOutput; +export 'src/api/c_code_asset.dart' + show + AssetAbsolutePath, + AssetInExecutable, + AssetInProcess, + AssetSystemPath, + CCodeAsset; export 'src/api/ios_sdk.dart' show IOSSdk; export 'src/api/link_mode.dart' show LinkMode; export 'src/api/link_mode_preference.dart' show LinkModePreference; diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index 0ac3ec9c2..d9ab9bde0 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -8,16 +8,16 @@ /// @nodoc library native_assets_cli_internal; -export 'src/api/asset.dart' +export 'src/api/build_config.dart' show BuildConfigImpl, CCompilerConfigImpl; +export 'src/api/build_mode.dart' show BuildModeImpl; +export 'src/api/build_output.dart' show BuildOutputImpl; +export 'src/api/c_code_asset.dart' show AssetAbsolutePathImpl, - CCodeAssetImpl, AssetInExecutableImpl, AssetInProcessImpl, - AssetSystemPathImpl; -export 'src/api/build_config.dart' show BuildConfigImpl, CCompilerConfigImpl; -export 'src/api/build_mode.dart' show BuildModeImpl; -export 'src/api/build_output.dart' show BuildOutputImpl; + AssetSystemPathImpl, + CCodeAssetImpl; export 'src/api/ios_sdk.dart' show IOSSdkImpl; export 'src/api/link_mode.dart' show LinkModeImpl; export 'src/api/link_mode_preference.dart' show LinkModePreferenceImpl; diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 4ab1737f8..d7a14d141 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -14,7 +14,7 @@ import '../utils/datetime.dart'; import '../utils/file.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; -import 'asset.dart'; +import 'c_code_asset.dart'; import 'target.dart'; part '../model/build_output.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart similarity index 98% rename from pkgs/native_assets_cli/lib/src/api/asset.dart rename to pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index 18d887568..98e3693b0 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -8,7 +8,7 @@ import '../utils/yaml.dart'; import 'link_mode.dart'; import 'target.dart'; -part '../model/asset.dart'; +part '../model/c_code_asset.dart'; abstract final class AssetPath {} diff --git a/pkgs/native_assets_cli/lib/src/model/asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart similarity index 99% rename from pkgs/native_assets_cli/lib/src/model/asset.dart rename to pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index 878d67a99..64768d9f7 100644 --- a/pkgs/native_assets_cli/lib/src/model/asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -2,7 +2,7 @@ // 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. -part of '../api/asset.dart'; +part of '../api/c_code_asset.dart'; abstract final class AssetPathImpl implements AssetPath { factory AssetPathImpl(String pathType, Uri? uri) { diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index fd78f7f46..b29d899be 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -4,7 +4,7 @@ import 'package:collection/collection.dart'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; -import 'package:native_assets_cli/src/api/asset.dart'; +import 'package:native_assets_cli/src/api/c_code_asset.dart'; import 'package:test/test.dart'; void main() { From e1e4f2241dd066595cd7d36ca33dbe345d1575ef Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 16 Feb 2024 20:03:21 +0100 Subject: [PATCH 17/75] Add asset type to protocol --- .../lib/src/model/build_output.dart | 2 +- .../lib/src/model/build_output_CHANGELOG.md | 8 +++++ .../lib/src/model/c_code_asset.dart | 7 +++-- .../test/model/asset_test.dart | 8 ++++- .../test/model/build_output_test.dart | 30 ++++++++++++++++++- 5 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index 7bcb2f53c..37dc93bd2 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -99,7 +99,7 @@ class BuildOutputImpl implements BuildOutput { /// If we ever were to make breaking changes, it would be useful to give /// proper error messages rather than just fail to parse the YAML /// representation in the protocol. - static Version version = Version(1, 0, 0); + static Version version = Version(1, 1, 0); static const fileName = 'build_output.yaml'; diff --git a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md new file mode 100644 index 000000000..e0caefba8 --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md @@ -0,0 +1,8 @@ +## 1.1.0 + +- Assets now have a `type`. + Backwards compatibility: assets without a type are interpreted as `c_code` assets. + +## 1.0.0 + +- Initial version. diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index 64768d9f7..8e5e3d516 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -194,17 +194,18 @@ final class CCodeAssetImpl implements CCodeAsset { _linkModeKey: linkMode.name, _pathKey: path.toYaml(), _targetKey: target.toString(), + _typeKey: _type, }; + static const _typeKey = 'type'; + static const _type = 'c_code'; static const _idKey = 'id'; static const _linkModeKey = 'link_mode'; static const _pathKey = 'path'; static const _targetKey = 'target'; - // Future exists() => path.exists(); - @override - String toString() => 'Asset(${toYaml()})'; + String toString() => 'CCodeAsset(${toYaml()})'; } extension AssetIterable on Iterable { diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index b29d899be..66a76a403 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -92,34 +92,40 @@ void main() { path_type: absolute uri: ${fooUri.toFilePath()} target: android_x64 + type: c_code - id: foo3 link_mode: dynamic path: path_type: system uri: ${foo3Uri.toFilePath()} target: android_x64 + type: c_code - id: foo4 link_mode: dynamic path: path_type: executable target: android_x64 + type: c_code - id: foo5 link_mode: dynamic path: path_type: process target: android_x64 + type: c_code - id: bar link_mode: static path: path_type: absolute uri: ${barUri.toFilePath()} target: linux_arm64 + type: c_code - id: bla link_mode: dynamic path: path_type: absolute uri: ${blaUri.toFilePath()} - target: windows_x64'''; + target: windows_x64 + type: c_code'''; test('asset yaml', () { final yaml = assets.toYamlString(); diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 36f2aaea5..96086a430 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -42,6 +42,26 @@ void main() { }), ); + const yamlEncodingV1_0_0 = '''timestamp: 2022-11-10 13:25:01.000 +assets: + - id: foo + link_mode: dynamic + path: + path_type: absolute + uri: path/to/libfoo.so + target: android_x64 + - id: foo2 + link_mode: dynamic + path: + path_type: system + uri: path/to/libfoo2.so + target: android_x64 +dependencies: + - path/to/file.ext +metadata: + key: value +version: 1.0.0'''; + final yamlEncoding = '''timestamp: 2022-11-10 13:25:01.000 assets: - id: foo @@ -50,12 +70,14 @@ assets: path_type: absolute uri: path/to/libfoo.so target: android_x64 + type: c_code - id: foo2 link_mode: dynamic path: path_type: system uri: path/to/libfoo2.so target: android_x64 + type: c_code dependencies: - path/to/file.ext metadata: @@ -70,6 +92,12 @@ version: ${BuildOutputImpl.version}'''; expect(buildOutput, buildOutput2); }); + test('built info yaml v1.0.0 keeps working', () { + final buildOutput2 = BuildOutputImpl.fromYamlString(yamlEncodingV1_0_0); + expect(buildOutput.hashCode, buildOutput2.hashCode); + expect(buildOutput, buildOutput2); + }); + test('BuildOutput.toString', buildOutput.toString); test('BuildOutput.hashCode', () { @@ -104,7 +132,7 @@ version: ${BuildOutputImpl.version}'''; (e) => e is FormatException && e.message.contains(version) && - e.message.contains(BuildConfigImpl.version.toString()), + e.message.contains(BuildOutputImpl.version.toString()), )), ); }); From 8915b773660299ba8146f10f144b9134054290dc Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 16 Feb 2024 20:12:54 +0100 Subject: [PATCH 18/75] Parse assets based on asset type --- .../lib/src/model/build_output.dart | 18 ++++++++++++++++-- .../lib/src/model/c_code_asset.dart | 6 +++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index 37dc93bd2..ce5d1357f 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -70,10 +70,24 @@ class BuildOutputImpl implements BuildOutput { ); } + final assets = []; + for (final yamlElement in as(yamlMap[_assetsKey])) { + final yamlMap = as(yamlElement); + final type = yamlMap[CCodeAssetImpl.typeKey]; + switch (type) { + case CCodeAssetImpl.type: + case null: // Backwards compatibility with v1.0.0. + assets.add(CCodeAssetImpl.fromYaml(yamlMap)); + default: + throw FormatException( + "Unexpected asset type '$type' in YAML.", + ); + } + } + return BuildOutputImpl( timestamp: DateTime.parse(as(yamlMap[_timestampKey])), - assets: - CCodeAssetImpl.listFromYamlList(as(yamlMap[_assetsKey])), + assets: assets, dependencies: Dependencies.fromYaml(as(yamlMap[_dependenciesKey])), metadata: Metadata.fromYaml(as(yamlMap[_metadataKey])), diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index 8e5e3d516..ed5eb3c22 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -194,11 +194,11 @@ final class CCodeAssetImpl implements CCodeAsset { _linkModeKey: linkMode.name, _pathKey: path.toYaml(), _targetKey: target.toString(), - _typeKey: _type, + typeKey: type, }; - static const _typeKey = 'type'; - static const _type = 'c_code'; + static const typeKey = 'type'; + static const type = 'c_code'; static const _idKey = 'id'; static const _linkModeKey = 'link_mode'; static const _pathKey = 'path'; From bee8f2d7c84220f32efaf17ef47b973cdcdca34d Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 19 Feb 2024 12:19:39 +0100 Subject: [PATCH 19/75] Introduce `Asset` with `id` and `file`. Remove `AssetAbsolutePath.uri` and instead use `Asset.file`. --- .../test/build_runner/helpers.dart | 4 +- .../wrong_namespace_asset/build.dart | 9 ++- .../example/local_asset/build.dart | 3 +- .../lib/native_assets_cli.dart | 11 ++-- .../lib/native_assets_cli_internal.dart | 8 +-- pkgs/native_assets_cli/lib/src/api/asset.dart | 35 +++++++++++ .../lib/src/api/build_output.dart | 2 +- .../lib/src/api/c_code_asset.dart | 41 ++++++++----- .../lib/src/helpers/asset_downloader.dart | 3 +- .../lib/src/model/build_output_CHANGELOG.md | 2 + .../lib/src/model/c_code_asset.dart | 61 +++++++++++++------ .../test/api/asset_test.dart | 9 ++- .../test/api/build_output_test.dart | 3 +- .../test/example/local_asset_test.dart | 5 +- .../test/example/native_add_library_test.dart | 5 +- pkgs/native_assets_cli/test/helpers.dart | 8 +-- .../test/model/asset_test.dart | 23 ++++--- .../test/model/build_output_test.dart | 7 ++- .../lib/src/cbuilder/cbuilder.dart | 3 +- 19 files changed, 161 insertions(+), 81 deletions(-) create mode 100644 pkgs/native_assets_cli/lib/src/api/asset.dart diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index e583dd52e..e9fefb917 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -107,7 +107,7 @@ Future dryRun( Future expectAssetsExist(List assets) async { for (final asset in assets) { - final uri = (asset.path as AssetAbsolutePathImpl).uri; + final uri = asset.file!; expect( uri.toFilePath(), contains('${Platform.pathSeparator}.dart_tool${Platform.pathSeparator}' @@ -122,7 +122,7 @@ Future expectSymbols({ required List symbols, }) async { if (Platform.isLinux) { - final assetUri = (asset.path as AssetAbsolutePathImpl).uri; + final assetUri = asset.file!; final nmResult = await runProcess( executable: Uri(path: 'nm'), arguments: [ diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 9817d3a0b..3147bba94 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -10,13 +10,12 @@ void main(List args) async { assets: [ CCodeAsset( id: 'package:other_package/foo', + file: buildConfig.outDir.resolve( + Target.current.os.dylibFileName('foo'), + ), linkMode: LinkMode.dynamic, target: Target.current, - path: AssetAbsolutePath( - buildConfig.outDir.resolve( - Target.current.os.dylibFileName('foo'), - ), - ), + path: AssetAbsolutePath(), ), ], ); diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 3cbdc2513..5ead3bad4 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -43,9 +43,10 @@ void main(List args) async { for (final target in targets) CCodeAsset( id: 'library:$packageName/asset.txt', + file: assetPath, linkMode: LinkMode.dynamic, target: target, - path: AssetAbsolutePath(assetPath), + path: AssetAbsolutePath(), ) ]); }); diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 8d9a9cf94..adc855098 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -6,17 +6,18 @@ /// native assets CLI, e.g. a `build.dart` script. library native_assets_cli; -export 'src/api/build.dart'; -export 'src/api/build_config.dart' show BuildConfig, CCompilerConfig; -export 'src/api/build_mode.dart' show BuildMode; -export 'src/api/build_output.dart' show BuildOutput; -export 'src/api/c_code_asset.dart' +export 'src/api/asset.dart' show + Asset, AssetAbsolutePath, AssetInExecutable, AssetInProcess, AssetSystemPath, CCodeAsset; +export 'src/api/build.dart'; +export 'src/api/build_config.dart' show BuildConfig, CCompilerConfig; +export 'src/api/build_mode.dart' show BuildMode; +export 'src/api/build_output.dart' show BuildOutput; export 'src/api/ios_sdk.dart' show IOSSdk; export 'src/api/link_mode.dart' show LinkMode; export 'src/api/link_mode_preference.dart' show LinkModePreference; diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index d9ab9bde0..d792b001c 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -8,16 +8,16 @@ /// @nodoc library native_assets_cli_internal; -export 'src/api/build_config.dart' show BuildConfigImpl, CCompilerConfigImpl; -export 'src/api/build_mode.dart' show BuildModeImpl; -export 'src/api/build_output.dart' show BuildOutputImpl; -export 'src/api/c_code_asset.dart' +export 'src/api/asset.dart' show AssetAbsolutePathImpl, AssetInExecutableImpl, AssetInProcessImpl, AssetSystemPathImpl, CCodeAssetImpl; +export 'src/api/build_config.dart' show BuildConfigImpl, CCompilerConfigImpl; +export 'src/api/build_mode.dart' show BuildModeImpl; +export 'src/api/build_output.dart' show BuildOutputImpl; export 'src/api/ios_sdk.dart' show IOSSdkImpl; export 'src/api/link_mode.dart' show LinkModeImpl; export 'src/api/link_mode_preference.dart' show LinkModePreferenceImpl; diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart new file mode 100644 index 000000000..f9cc925bd --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -0,0 +1,35 @@ +// 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:yaml/yaml.dart'; + +import '../utils/yaml.dart'; +import 'link_mode.dart'; +import 'target.dart'; + +part 'c_code_asset.dart'; +part '../model/c_code_asset.dart'; + +/// Data bundled with a Dart or Flutter application. +/// +/// An asset is data or code which is accessible from a Dart or Flutter +/// application. To retrieve an asset at runtime, the [id] is used. This enables +/// access to the asset irrespective of how and where the application is run. +abstract final class Asset { + /// The identifier for this asset. + /// + /// An [Asset] must have a string identifier called `assetId`. Dart code that + /// uses an asset, references the asset using this `assetId`. + /// + /// A package must prefix all `assetId`s it defines with: + /// `package:/`, `` being the current package's name. This + /// ensures assets don't conflict between packages. + /// + /// Additionally, the convention is that an asset referenced from + /// `lib/src/foo.dart` in `package:foo` has the `assetId` + /// `'package:foo/src/foo.dart'`. + String get id; + + Uri? get file; +} diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index d7a14d141..4ab1737f8 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -14,7 +14,7 @@ import '../utils/datetime.dart'; import '../utils/file.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; -import 'c_code_asset.dart'; +import 'asset.dart'; import 'target.dart'; part '../model/build_output.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index 98e3693b0..3f1d3107c 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -2,21 +2,13 @@ // 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:yaml/yaml.dart'; - -import '../utils/yaml.dart'; -import 'link_mode.dart'; -import 'target.dart'; - -part '../model/c_code_asset.dart'; +part of 'asset.dart'; abstract final class AssetPath {} -/// Asset at absolute path [uri]. +/// Asset at absolute path. abstract final class AssetAbsolutePath implements AssetPath { - Uri get uri; - - factory AssetAbsolutePath(Uri uri) = AssetAbsolutePathImpl; + factory AssetAbsolutePath() = AssetAbsolutePathImpl; } /// Asset is avaliable on the system `PATH`. @@ -40,13 +32,32 @@ abstract final class AssetInExecutable implements AssetPath { factory AssetInExecutable() = AssetInExecutableImpl; } -/// A code asset which respects the C application binary interface (ABI). +/// A code [Asset] which respects the C application binary interface (ABI). /// /// Typical other languages which produce code assets that respect the C ABI /// include C++ and Rust. -abstract final class CCodeAsset { +/// +/// There are several example types of assets: +/// * Assets which designate symbols present in the target system, process, or +/// executable. They are identified by their name. +/// * Dynamic libraries bundled into the application. +/// +/// An application is compiled to run on a certain target OS and architecture. +/// If different targets require different assets, the package developer must +/// specify which asset to bundle for which target. +/// +/// An asset has different ways of being accessible in the final application. It +/// is either brought in "manually" by having the package developer specify a +/// path of the asset on the current system, it can be part of the Dart or +/// Flutter SDK, or it can be already present in the target system. If the asset +/// is bundled "manually", the Dart or Flutter SDK will take care of copying the +/// asset from its specified location on the current system into the application +/// bundle. +/// +/// Assets are also called "native assets" to differentiate them from the Dart +/// code also bundled with an application. +abstract final class CCodeAsset implements Asset { LinkMode get linkMode; - String get id; Target get target; AssetPath get path; @@ -55,11 +66,13 @@ abstract final class CCodeAsset { required LinkMode linkMode, required Target target, required AssetPath path, + Uri? file, }) => CCodeAssetImpl( id: id, linkMode: linkMode as LinkModeImpl, target: target as TargetImpl, path: path as AssetPathImpl, + file: file, ); } diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart index 6dcbd4201..b4b7bb21b 100644 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -62,8 +62,9 @@ class AssetDownloader implements Builder { buildOutput.addAssets([ CCodeAsset( id: assetId, + file: targetUri, linkMode: LinkMode.dynamic, - path: AssetAbsolutePath(targetUri), + path: AssetAbsolutePath(), target: target, ) ]); diff --git a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md index e0caefba8..17092035e 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md +++ b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md @@ -2,6 +2,8 @@ - Assets now have a `type`. Backwards compatibility: assets without a type are interpreted as `c_code` assets. +- Assets now have an optional `file`. + Backwards compatibility: assets that have an `AssetAbsolutePath` will have that path used as `file`. ## 1.0.0 diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index ed5eb3c22..c64a326f9 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -2,13 +2,13 @@ // 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. -part of '../api/c_code_asset.dart'; +part of '../api/asset.dart'; abstract final class AssetPathImpl implements AssetPath { factory AssetPathImpl(String pathType, Uri? uri) { switch (pathType) { case AssetAbsolutePathImpl._pathTypeValue: - return AssetAbsolutePathImpl(uri!); + return AssetAbsolutePathImpl(); case AssetSystemPathImpl._pathTypeValue: return AssetSystemPathImpl(uri!); case AssetInExecutableImpl._pathTypeValue: @@ -32,30 +32,25 @@ abstract final class AssetPathImpl implements AssetPath { static const _uriKey = 'uri'; } -/// Asset at absolute path [uri]. final class AssetAbsolutePathImpl implements AssetPathImpl, AssetAbsolutePath { - @override - final Uri uri; - - AssetAbsolutePathImpl(this.uri); + AssetAbsolutePathImpl(); static const _pathTypeValue = 'absolute'; @override Map toYaml() => { AssetPathImpl._pathTypeKey: _pathTypeValue, - AssetPathImpl._uriKey: uri.toFilePath(), }; @override - int get hashCode => Object.hash(uri, 133711); + int get hashCode => 133711; @override bool operator ==(Object other) { if (other is! AssetAbsolutePathImpl) { return false; } - return uri == other.uri; + return true; } } @@ -123,6 +118,8 @@ final class AssetInExecutableImpl implements AssetPathImpl, AssetInExecutable { } final class CCodeAssetImpl implements CCodeAsset { + @override + final Uri? file; @override final LinkModeImpl linkMode; @override @@ -133,18 +130,35 @@ final class CCodeAssetImpl implements CCodeAsset { final AssetPathImpl path; CCodeAssetImpl({ + this.file, required this.id, required this.linkMode, required this.target, required this.path, }); - factory CCodeAssetImpl.fromYaml(YamlMap yamlMap) => CCodeAssetImpl( - id: as(yamlMap[_idKey]), - path: AssetPathImpl.fromYaml(as(yamlMap[_pathKey])), - target: TargetImpl.fromString(as(yamlMap[_targetKey])), - linkMode: LinkModeImpl.fromName(as(yamlMap[_linkModeKey])), - ); + factory CCodeAssetImpl.fromYaml(YamlMap yamlMap) { + final path = AssetPathImpl.fromYaml(as(yamlMap[_pathKey])); + final fileString = as(yamlMap[_fileKey]); + final Uri? file; + if (fileString != null) { + file = Uri(path: fileString); + } else if (path is AssetAbsolutePathImpl) { + // Compatibility with v1.0.0. + final oldPath = + as((yamlMap[_pathKey] as YamlMap)[AssetPathImpl._uriKey]); + file = oldPath != null ? Uri(path: oldPath) : null; + } else { + file = null; + } + return CCodeAssetImpl( + id: as(yamlMap[_idKey]), + path: path, + target: TargetImpl.fromString(as(yamlMap[_targetKey])), + linkMode: LinkModeImpl.fromName(as(yamlMap[_linkModeKey])), + file: file, + ); + } static List listFromYamlString(String yaml) { final yamlObject = loadYaml(yaml); @@ -167,12 +181,14 @@ final class CCodeAssetImpl implements CCodeAsset { String? id, Target? target, AssetPathImpl? path, + Uri? file, }) => CCodeAssetImpl( id: id ?? this.id, linkMode: linkMode ?? this.linkMode, target: target ?? this.target, path: path ?? this.path, + file: file ?? this.file, ); @override @@ -183,13 +199,21 @@ final class CCodeAssetImpl implements CCodeAsset { return other.id == id && other.linkMode == linkMode && other.target == target && - other.path == path; + other.path == path && + other.file == file; } @override - int get hashCode => Object.hash(id, linkMode, target, path); + int get hashCode => Object.hash( + id, + linkMode, + target, + path, + file, + ); Map toYaml() => { + if (file != null) _fileKey: file!.toFilePath(), _idKey: id, _linkModeKey: linkMode.name, _pathKey: path.toYaml(), @@ -203,6 +227,7 @@ final class CCodeAssetImpl implements CCodeAsset { static const _linkModeKey = 'link_mode'; static const _pathKey = 'path'; static const _targetKey = 'target'; + static const _fileKey = 'file'; @override String toString() => 'CCodeAsset(${toYaml()})'; diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index 7353b0ebe..639cd9681 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -10,7 +10,8 @@ void main() { final assets = [ CCodeAsset( id: 'foo', - path: AssetAbsolutePath(Uri.file('path/to/libfoo.so')), + file: Uri.file('path/to/libfoo.so'), + path: AssetAbsolutePath(), target: Target.androidX64, linkMode: LinkMode.dynamic, ), @@ -34,13 +35,15 @@ void main() { ), CCodeAsset( id: 'bar', - path: AssetAbsolutePath(Uri(path: 'path/to/libbar.a')), + file: Uri(path: 'path/to/libbar.a'), + path: AssetAbsolutePath(), target: Target.linuxArm64, linkMode: LinkMode.static, ), CCodeAsset( id: 'bla', - path: AssetAbsolutePath(Uri(path: 'path/with spaces/bla.dll')), + file: Uri(path: 'path/with spaces/bla.dll'), + path: AssetAbsolutePath(), target: Target.windowsX64, linkMode: LinkMode.dynamic, ), diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index ed74fe26e..71206cebe 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -24,7 +24,8 @@ void main() { assets: [ CCodeAsset( id: 'foo', - path: AssetAbsolutePath(Uri(path: 'path/to/libfoo.so')), + file: Uri(path: 'path/to/libfoo.so'), + path: AssetAbsolutePath(), target: Target.androidX64, linkMode: LinkMode.dynamic, ), diff --git a/pkgs/native_assets_cli/test/example/local_asset_test.dart b/pkgs/native_assets_cli/test/example/local_asset_test.dart index 9411a2d80..f2c55be50 100644 --- a/pkgs/native_assets_cli/test/example/local_asset_test.dart +++ b/pkgs/native_assets_cli/test/example/local_asset_test.dart @@ -74,10 +74,7 @@ void main() async { final dependencies = buildOutput.dependencies; if (dryRun) { expect(assets.length, greaterThanOrEqualTo(1)); - expect( - await File.fromUri((assets.first.path as AssetAbsolutePathImpl).uri) - .exists(), - false); + expect(await File.fromUri(assets.first.file!).exists(), false); expect(dependencies, []); } else { expect(assets.length, 1); diff --git a/pkgs/native_assets_cli/test/example/native_add_library_test.dart b/pkgs/native_assets_cli/test/example/native_add_library_test.dart index 5a2584c6b..d7b713313 100644 --- a/pkgs/native_assets_cli/test/example/native_add_library_test.dart +++ b/pkgs/native_assets_cli/test/example/native_add_library_test.dart @@ -74,10 +74,7 @@ void main() async { final dependencies = buildOutput.dependencies; if (dryRun) { expect(assets.length, greaterThanOrEqualTo(1)); - expect( - await File.fromUri((assets.first.path as AssetAbsolutePathImpl).uri) - .exists(), - false); + expect(await File.fromUri(assets.first.file!).exists(), false); expect(dependencies, []); } else { expect(assets.length, 1); diff --git a/pkgs/native_assets_cli/test/helpers.dart b/pkgs/native_assets_cli/test/helpers.dart index c9cbd6b08..4e0677b4e 100644 --- a/pkgs/native_assets_cli/test/helpers.dart +++ b/pkgs/native_assets_cli/test/helpers.dart @@ -121,12 +121,12 @@ extension AssetIterable on Iterable { } } -extension on CCodeAsset { +extension on Asset { Future exists() async { - final path_ = path; + final path_ = file; return switch (path_) { - AssetAbsolutePath _ => await path_.uri.fileSystemEntity.exists(), - _ => true, + null => true, + _ => await path_.fileSystemEntity.exists(), }; } } diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 66a76a403..17a78a909 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -4,7 +4,7 @@ import 'package:collection/collection.dart'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; -import 'package:native_assets_cli/src/api/c_code_asset.dart'; +import 'package:native_assets_cli/src/api/asset.dart'; import 'package:test/test.dart'; void main() { @@ -15,7 +15,8 @@ void main() { final assets = [ CCodeAssetImpl( id: 'foo', - path: AssetAbsolutePathImpl(fooUri), + file: fooUri, + path: AssetAbsolutePathImpl(), target: TargetImpl.androidX64, linkMode: LinkModeImpl.dynamic, ), @@ -39,13 +40,15 @@ void main() { ), CCodeAssetImpl( id: 'bar', - path: AssetAbsolutePathImpl(barUri), + file: barUri, + path: AssetAbsolutePathImpl(), target: TargetImpl.linuxArm64, linkMode: LinkModeImpl.static, ), CCodeAssetImpl( id: 'bla', - path: AssetAbsolutePathImpl(blaUri), + file: blaUri, + path: AssetAbsolutePathImpl(), target: TargetImpl.windowsX64, linkMode: LinkModeImpl.dynamic, ), @@ -86,11 +89,11 @@ void main() { uri: ${blaUri.toFilePath()} target: windows_x64'''; - final assetsYamlEncoding = '''- id: foo + final assetsYamlEncoding = '''- file: ${fooUri.toFilePath()} + id: foo link_mode: dynamic path: path_type: absolute - uri: ${fooUri.toFilePath()} target: android_x64 type: c_code - id: foo3 @@ -112,18 +115,18 @@ void main() { path_type: process target: android_x64 type: c_code -- id: bar +- file: ${barUri.toFilePath()} + id: bar link_mode: static path: path_type: absolute - uri: ${barUri.toFilePath()} target: linux_arm64 type: c_code -- id: bla +- file: ${blaUri.toFilePath()} + id: bla link_mode: dynamic path: path_type: absolute - uri: ${blaUri.toFilePath()} target: windows_x64 type: c_code'''; diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 96086a430..b24743f2c 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -23,7 +23,8 @@ void main() { assets: [ CCodeAssetImpl( id: 'foo', - path: AssetAbsolutePathImpl(Uri(path: 'path/to/libfoo.so')), + file: Uri(path: 'path/to/libfoo.so'), + path: AssetAbsolutePathImpl(), target: TargetImpl.androidX64, linkMode: LinkModeImpl.dynamic, ), @@ -64,11 +65,11 @@ version: 1.0.0'''; final yamlEncoding = '''timestamp: 2022-11-10 13:25:01.000 assets: - - id: foo + - file: path/to/libfoo.so + id: foo link_mode: dynamic path: path_type: absolute - uri: path/to/libfoo.so target: android_x64 type: c_code - id: foo2 diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index c8558cf23..022c2a8a9 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -258,9 +258,10 @@ class CBuilder implements Builder { buildOutput.addAssets([ CCodeAsset( id: assetId!, + file: libUri, linkMode: linkMode, target: target, - path: AssetAbsolutePath(libUri), + path: AssetAbsolutePath(), ) ]); } From 9d9f894e09fb91fc35f1fe267cccf69b4f8de585 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 19 Feb 2024 14:38:26 +0100 Subject: [PATCH 20/75] Split `CCodeAsset.target` in `os` and `architecture`. Dry run does not have to provide architecture. --- .../build_runner_dry_run_test.dart | 11 ++-- .../wrong_namespace_asset/build.dart | 3 +- .../example/local_asset/build.dart | 26 +++----- pkgs/native_assets_cli/lib/src/api/asset.dart | 2 + .../lib/src/api/build_config.dart | 8 +-- .../lib/src/api/c_code_asset.dart | 15 ++++- .../lib/src/helpers/asset_downloader.dart | 60 +++++++++---------- .../lib/src/model/build_config.dart | 7 --- .../lib/src/model/c_code_asset.dart | 58 ++++++++++++++---- .../lib/src/model/target.dart | 4 +- .../test/api/asset_test.dart | 18 ++++-- .../test/api/build_output_test.dart | 6 +- .../test/example/native_add_library_test.dart | 2 +- .../test/model/asset_test.dart | 48 +++++++++------ .../test/model/build_output_test.dart | 16 +++-- .../lib/src/cbuilder/cbuilder.dart | 31 ++++------ .../lib/src/cbuilder/run_cbuilder.dart | 50 ++++++++-------- 17 files changed, 209 insertions(+), 156 deletions(-) diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart index b98659753..f7a5fba2c 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart @@ -24,10 +24,8 @@ void main() async { logger: logger, ); - final dryRunAssets = (await dryRun(packageUri, logger, dartExecutable)) - .assets - .where((element) => element.target == TargetImpl.current) - .toList(); + final dryRunAssets = + (await dryRun(packageUri, logger, dartExecutable)).assets.toList(); final result = await build(packageUri, logger, dartExecutable); expect(dryRunAssets.length, result.assets.length); @@ -36,7 +34,10 @@ void main() async { final buildAsset = result.assets[0]; expect(dryRunAsset.linkMode, buildAsset.linkMode); expect(dryRunAsset.id, buildAsset.id); - expect(dryRunAsset.target, buildAsset.target); + expect(buildAsset.architecture, isNotNull); + expect(buildAsset.file, isNotNull); + expect(dryRunAsset.file, isNull); + expect(dryRunAsset.os, buildAsset.os); // The target folders are different, so the paths are different. } diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 3147bba94..8983bbaa8 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -14,7 +14,8 @@ void main(List args) async { Target.current.os.dylibFileName('foo'), ), linkMode: LinkMode.dynamic, - target: Target.current, + os: Target.current.os, + architecture: Target.current.architecture, path: AssetAbsolutePath(), ), ], diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 5ead3bad4..019c7176e 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -18,18 +18,10 @@ void main(List args) async { ); } - final Iterable targets; final packageName = config.packageName; final assetPath = config.outDir.resolve(assetName); final assetSourcePath = config.packageRoot.resolveUri(packageAssetPath); - if (config.dryRun) { - // Dry run invocations report assets for all architectures for that OS. - targets = Target.values.where( - (element) => element.os == config.targetOs, - ); - } else { - targets = [config.target]; - + if (!config.dryRun) { // Insert code that downloads or builds the asset to `assetPath`. await File.fromUri(assetSourcePath).copy(assetPath.toFilePath()); @@ -40,14 +32,14 @@ void main(List args) async { } output.addAssets([ - for (final target in targets) - CCodeAsset( - id: 'library:$packageName/asset.txt', - file: assetPath, - linkMode: LinkMode.dynamic, - target: target, - path: AssetAbsolutePath(), - ) + CCodeAsset( + id: 'library:$packageName/asset.txt', + file: assetPath, + linkMode: LinkMode.dynamic, + os: config.targetOs, + architecture: config.dryRun ? null : config.targetArchitecture, + path: AssetAbsolutePath(), + ) ]); }); } diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index f9cc925bd..accf329e2 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -4,6 +4,8 @@ import 'package:yaml/yaml.dart'; +import '../../native_assets_cli.dart'; +import '../../native_assets_cli_internal.dart'; import '../utils/yaml.dart'; import 'link_mode.dart'; import 'target.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 1165abf65..1a012d782 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -40,10 +40,10 @@ abstract final class BuildConfig { /// dependency of another. For this it is convenient to know the packageRoot. Uri get packageRoot; - /// The target being compiled for. - /// - /// Not available during a [dryRun]. - Target get target; + // /// The target being compiled for. + // /// + // /// Not available during a [dryRun]. + // Target get target; /// The architecture being compiled for. /// diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index 3f1d3107c..304b58cf9 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -58,20 +58,29 @@ abstract final class AssetInExecutable implements AssetPath { /// code also bundled with an application. abstract final class CCodeAsset implements Asset { LinkMode get linkMode; - Target get target; + + /// The operating system this asset can run on. + OS get os; + + /// The architecture this asset can run on. + /// + /// Not available during a [BuildConfig.dryRun]. + Architecture get architecture; AssetPath get path; factory CCodeAsset({ required String id, required LinkMode linkMode, - required Target target, + required OS os, required AssetPath path, Uri? file, + Architecture? architecture, }) => CCodeAssetImpl( id: id, linkMode: linkMode as LinkModeImpl, - target: target as TargetImpl, + os: os as OSImpl, + architecture: architecture as ArchitectureImpl?, path: path as AssetPathImpl, file: file, ); diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart index b4b7bb21b..0ebd49656 100644 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -10,8 +10,8 @@ import '../../native_assets_cli.dart'; abstract class Builder { Future run({ - required BuildConfig buildConfig, - required BuildOutput buildOutput, + required BuildConfig config, + required BuildOutput output, required Logger? logger, }); } @@ -19,7 +19,7 @@ abstract class Builder { // TODO(dacoharkes): Should we really add this? It seems too specific. // E.g. what about varying file names, zipped downloads etc. etc. ? class AssetDownloader implements Builder { - final Uri Function(Target) downloadUri; + final Uri Function(OS, Architecture) downloadUri; /// Asset identifier. /// @@ -33,41 +33,37 @@ class AssetDownloader implements Builder { @override Future run({ - required BuildConfig buildConfig, - required BuildOutput buildOutput, + required BuildConfig config, + required BuildOutput output, required Logger? logger, }) async { final assetId = this.assetId.startsWith('package:') ? this.assetId - : 'package:${buildConfig.packageName}/${this.assetId}'; - final List targets; - if (buildConfig.dryRun) { - targets = [ - for (final target in Target.values) - if (target.os == buildConfig.targetOs) target - ]; - } else { - targets = [buildConfig.target]; - } - for (final target in targets) { - final downloadUri2 = downloadUri(buildConfig.target); + : 'package:${config.packageName}/${this.assetId}'; + + Uri? targetUri; + if (!config.dryRun) { + final downloadUri2 = downloadUri( + config.targetOs, + config.targetArchitecture, + ); final fileName = downloadUri2.pathSegments.lastWhere((element) => element.isNotEmpty); - final targetUri = buildConfig.outDir.resolve(fileName); - if (!buildConfig.dryRun) { - final request = await HttpClient().getUrl(downloadUri2); - final response = await request.close(); - await response.pipe(File.fromUri(targetUri).openWrite()); - } - buildOutput.addAssets([ - CCodeAsset( - id: assetId, - file: targetUri, - linkMode: LinkMode.dynamic, - path: AssetAbsolutePath(), - target: target, - ) - ]); + targetUri = config.outDir.resolve(fileName); + final request = await HttpClient().getUrl(downloadUri2); + final response = await request.close(); + await response.pipe(File.fromUri(targetUri).openWrite()); } + + output.addAssets([ + CCodeAsset( + id: assetId, + file: targetUri, + linkMode: LinkMode.dynamic, + path: AssetAbsolutePath(), + os: config.targetOs, + architecture: config.dryRun ? null : config.targetArchitecture, + ) + ]); } } diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index a962c4d55..411d4838a 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -24,13 +24,6 @@ final class BuildConfigImpl implements BuildConfig { Uri get packageRoot => _packageRoot; late final Uri _packageRoot; - /// The target being compiled for. - /// - /// Not available during a [dryRun]. - @override - late final TargetImpl target = - TargetImpl.fromArchitectureAndOs(targetArchitecture, targetOs); - /// The architecture being compiled for. /// /// Not available during a [dryRun]. diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index c64a326f9..f5937fc68 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -120,22 +120,35 @@ final class AssetInExecutableImpl implements AssetPathImpl, AssetInExecutable { final class CCodeAssetImpl implements CCodeAsset { @override final Uri? file; + @override final LinkModeImpl linkMode; + @override final String id; - @override - final Target target; + @override final AssetPathImpl path; + @override + final OSImpl os; + + final ArchitectureImpl? _architecture; + + @override + ArchitectureImpl get architecture => _architecture!; + + @override + // Target get target => TargetImpl.fromArchitectureAndOs(_architecture!, os); + CCodeAssetImpl({ this.file, required this.id, required this.linkMode, - required this.target, + required this.os, required this.path, - }); + ArchitectureImpl? architecture, + }) : _architecture = architecture; factory CCodeAssetImpl.fromYaml(YamlMap yamlMap) { final path = AssetPathImpl.fromYaml(as(yamlMap[_pathKey])); @@ -151,10 +164,28 @@ final class CCodeAssetImpl implements CCodeAsset { } else { file = null; } + final targetString = as(yamlMap[_targetKey]); + final ArchitectureImpl? architecture; + final OSImpl os; + if (targetString != null) { + // Compatibility with v1.0.0. + final target = TargetImpl.fromString(targetString); + os = target.os; + architecture = target.architecture; + } else { + os = OSImpl.fromString(as(yamlMap[_osKey])); + final architectureString = as(yamlMap[_architectureKey]); + if (architectureString != null) { + architecture = ArchitectureImpl.fromString(architectureString); + } else { + architecture = null; + } + } return CCodeAssetImpl( id: as(yamlMap[_idKey]), path: path, - target: TargetImpl.fromString(as(yamlMap[_targetKey])), + os: os, + architecture: architecture, linkMode: LinkModeImpl.fromName(as(yamlMap[_linkModeKey])), file: file, ); @@ -179,14 +210,16 @@ final class CCodeAssetImpl implements CCodeAsset { CCodeAssetImpl copyWith({ LinkModeImpl? linkMode, String? id, - Target? target, + OSImpl? os, + ArchitectureImpl? architecture, AssetPathImpl? path, Uri? file, }) => CCodeAssetImpl( id: id ?? this.id, linkMode: linkMode ?? this.linkMode, - target: target ?? this.target, + os: os ?? this.os, + architecture: architecture ?? _architecture, path: path ?? this.path, file: file ?? this.file, ); @@ -198,7 +231,8 @@ final class CCodeAssetImpl implements CCodeAsset { } return other.id == id && other.linkMode == linkMode && - other.target == target && + other.architecture == architecture && + other.os == os && other.path == path && other.file == file; } @@ -207,17 +241,19 @@ final class CCodeAssetImpl implements CCodeAsset { int get hashCode => Object.hash( id, linkMode, - target, + architecture, + os, path, file, ); Map toYaml() => { + if (_architecture != null) _architectureKey: _architecture.toString(), if (file != null) _fileKey: file!.toFilePath(), _idKey: id, _linkModeKey: linkMode.name, + _osKey: os.toString(), _pathKey: path.toYaml(), - _targetKey: target.toString(), typeKey: type, }; @@ -228,6 +264,8 @@ final class CCodeAssetImpl implements CCodeAsset { static const _pathKey = 'path'; static const _targetKey = 'target'; static const _fileKey = 'file'; + static const _osKey = 'os'; + static const _architectureKey = 'architecture'; @override String toString() => 'CCodeAsset(${toYaml()})'; diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index 76ac2a3c7..d1610f53c 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -258,7 +258,9 @@ class TargetImpl implements Target { } factory TargetImpl.fromArchitectureAndOs( - ArchitectureImpl architecture, OSImpl os) { + ArchitectureImpl architecture, + OSImpl os, + ) { for (final value in values) { if (value.os == os && value.architecture == architecture) { return value; diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index 639cd9681..796f41ec0 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -12,39 +12,45 @@ void main() { id: 'foo', file: Uri.file('path/to/libfoo.so'), path: AssetAbsolutePath(), - target: Target.androidX64, + os: OS.android, + architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( id: 'foo3', path: AssetSystemPath(Uri(path: 'libfoo3.so')), - target: Target.androidX64, + os: OS.android, + architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( id: 'foo4', path: AssetInExecutable(), - target: Target.androidX64, + os: OS.android, + architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( id: 'foo5', path: AssetInProcess(), - target: Target.androidX64, + os: OS.android, + architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( id: 'bar', file: Uri(path: 'path/to/libbar.a'), path: AssetAbsolutePath(), - target: Target.linuxArm64, + os: OS.linux, + architecture: Architecture.arm64, linkMode: LinkMode.static, ), CCodeAsset( id: 'bla', file: Uri(path: 'path/with spaces/bla.dll'), path: AssetAbsolutePath(), - target: Target.windowsX64, + os: OS.windows, + architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), ]; diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index 71206cebe..a9cdd8cce 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -26,13 +26,15 @@ void main() { id: 'foo', file: Uri(path: 'path/to/libfoo.so'), path: AssetAbsolutePath(), - target: Target.androidX64, + os: OS.android, + architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( id: 'foo2', path: AssetSystemPath(Uri(path: 'path/to/libfoo2.so')), - target: Target.androidX64, + os: OS.android, + architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), ], diff --git a/pkgs/native_assets_cli/test/example/native_add_library_test.dart b/pkgs/native_assets_cli/test/example/native_add_library_test.dart index d7b713313..644b169c5 100644 --- a/pkgs/native_assets_cli/test/example/native_add_library_test.dart +++ b/pkgs/native_assets_cli/test/example/native_add_library_test.dart @@ -74,7 +74,7 @@ void main() async { final dependencies = buildOutput.dependencies; if (dryRun) { expect(assets.length, greaterThanOrEqualTo(1)); - expect(await File.fromUri(assets.first.file!).exists(), false); + expect(assets.first.file, isNull); expect(dependencies, []); } else { expect(assets.length, 1); diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 17a78a909..1bd1a98e6 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -17,39 +17,45 @@ void main() { id: 'foo', file: fooUri, path: AssetAbsolutePathImpl(), - target: TargetImpl.androidX64, + os: OSImpl.android, + architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( id: 'foo3', path: AssetSystemPathImpl(foo3Uri), - target: TargetImpl.androidX64, + os: OSImpl.android, + architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( id: 'foo4', path: AssetInExecutableImpl(), - target: TargetImpl.androidX64, + os: OSImpl.android, + architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( id: 'foo5', path: AssetInProcessImpl(), - target: TargetImpl.androidX64, + os: OSImpl.android, + architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( id: 'bar', file: barUri, path: AssetAbsolutePathImpl(), - target: TargetImpl.linuxArm64, + os: OSImpl.linux, + architecture: ArchitectureImpl.arm64, linkMode: LinkModeImpl.static, ), CCodeAssetImpl( id: 'bla', file: blaUri, path: AssetAbsolutePathImpl(), - target: TargetImpl.windowsX64, + os: OSImpl.windows, + architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), ]; @@ -89,45 +95,51 @@ void main() { uri: ${blaUri.toFilePath()} target: windows_x64'''; - final assetsYamlEncoding = '''- file: ${fooUri.toFilePath()} + final assetsYamlEncoding = '''- architecture: x64 + file: ${fooUri.toFilePath()} id: foo link_mode: dynamic + os: android path: path_type: absolute - target: android_x64 type: c_code -- id: foo3 +- architecture: x64 + id: foo3 link_mode: dynamic + os: android path: path_type: system uri: ${foo3Uri.toFilePath()} - target: android_x64 type: c_code -- id: foo4 +- architecture: x64 + id: foo4 link_mode: dynamic + os: android path: path_type: executable - target: android_x64 type: c_code -- id: foo5 +- architecture: x64 + id: foo5 link_mode: dynamic + os: android path: path_type: process - target: android_x64 type: c_code -- file: ${barUri.toFilePath()} +- architecture: arm64 + file: ${barUri.toFilePath()} id: bar link_mode: static + os: linux path: path_type: absolute - target: linux_arm64 type: c_code -- file: ${blaUri.toFilePath()} +- architecture: x64 + file: ${blaUri.toFilePath()} id: bla link_mode: dynamic + os: windows path: path_type: absolute - target: windows_x64 type: c_code'''; test('asset yaml', () { diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index b24743f2c..f6b12040c 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -25,13 +25,15 @@ void main() { id: 'foo', file: Uri(path: 'path/to/libfoo.so'), path: AssetAbsolutePathImpl(), - target: TargetImpl.androidX64, + os: OSImpl.android, + architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( id: 'foo2', path: AssetSystemPathImpl(Uri(path: 'path/to/libfoo2.so')), - target: TargetImpl.androidX64, + os: OSImpl.android, + architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), ], @@ -65,19 +67,21 @@ version: 1.0.0'''; final yamlEncoding = '''timestamp: 2022-11-10 13:25:01.000 assets: - - file: path/to/libfoo.so + - architecture: x64 + file: path/to/libfoo.so id: foo link_mode: dynamic + os: android path: path_type: absolute - target: android_x64 type: c_code - - id: foo2 + - architecture: x64 + id: foo2 link_mode: dynamic + os: android path: path_type: system uri: path/to/libfoo2.so - target: android_x64 type: c_code dependencies: - path/to/file.ext diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 022c2a8a9..5e7bb1438 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -44,7 +44,7 @@ class CBuilder implements Builder { /// Name of the library or executable to build. /// - /// The filename will be decided by [BuildConfig.target] and + /// The filename will be decided by [BuildConfig.targetOs] and /// [OS.libraryFileName] or [OS.executableFileName]. /// /// File will be placed in [BuildConfig.outDir]. @@ -247,24 +247,17 @@ class CBuilder implements Builder { } if (assetId != null) { - final targets = [ - if (!buildConfig.dryRun) - buildConfig.target - else - for (final target in Target.values) - if (target.os == buildConfig.targetOs) target - ]; - for (final target in targets) { - buildOutput.addAssets([ - CCodeAsset( - id: assetId!, - file: libUri, - linkMode: linkMode, - target: target, - path: AssetAbsolutePath(), - ) - ]); - } + buildOutput.addAssets([ + CCodeAsset( + id: assetId!, + file: buildConfig.dryRun ? null : libUri, + linkMode: linkMode, + os: buildConfig.targetOs, + architecture: + buildConfig.dryRun ? null : buildConfig.targetArchitecture, + path: AssetAbsolutePath(), + ) + ]); } if (!buildConfig.dryRun) { final includeFiles = await Stream.fromIterable(includes) diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart index dc2b8fb21..dd0b312c9 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart @@ -27,7 +27,7 @@ class RunCBuilder { final Uri? dynamicLibrary; final Uri? staticLibrary; final Uri outDir; - final Target target; + // final Target target; /// The install name of the [dynamicLibrary]. /// @@ -59,12 +59,11 @@ class RunCBuilder { this.language = Language.c, this.cppLinkStdLib, }) : outDir = buildConfig.outDir, - target = buildConfig.target, assert([executable, dynamicLibrary, staticLibrary] .whereType() .length == 1) { - if (target.os == OS.windows && cppLinkStdLib != null) { + if (buildConfig.targetOs == OS.windows && cppLinkStdLib != null) { throw ArgumentError.value( cppLinkStdLib, 'cppLinkStdLib', @@ -124,7 +123,7 @@ class RunCBuilder { } late final IOSSdk targetIosSdk; - if (target.os == OS.iOS) { + if (buildConfig.targetOs == OS.iOS) { targetIosSdk = buildConfig.targetIOSSdk!; } @@ -132,29 +131,32 @@ class RunCBuilder { // invoking clang. Mimic that behavior here. // See https://github.com/dart-lang/native/issues/171. late final int targetAndroidNdkApi; - if (target.os == OS.android) { - final minimumApi = target == Target.androidRiscv64 ? 35 : 21; + if (buildConfig.targetOs == OS.android) { + final minimumApi = + buildConfig.targetArchitecture == Architecture.riscv64 ? 35 : 21; targetAndroidNdkApi = max(buildConfig.targetAndroidNdkApi!, minimumApi); } + final architecture = buildConfig.targetArchitecture; + await runProcess( executable: compiler.uri, arguments: [ - if (target.os == OS.android) ...[ + if (buildConfig.targetOs == OS.android) ...[ '--target=' - '${androidNdkClangTargetFlags[target]!}' + '${androidNdkClangTargetFlags[architecture]!}' '$targetAndroidNdkApi', '--sysroot=${androidSysroot(compiler).toFilePath()}', ], - if (target.os == OS.macOS) - '--target=${appleClangMacosTargetFlags[target]!}', - if (target.os == OS.iOS) - '--target=${appleClangIosTargetFlags[target]![targetIosSdk]!}', - if (target.os == OS.iOS) ...[ + if (buildConfig.targetOs == OS.macOS) + '--target=${appleClangMacosTargetFlags[architecture]!}', + if (buildConfig.targetOs == OS.iOS) + '--target=${appleClangIosTargetFlags[architecture]![targetIosSdk]!}', + if (buildConfig.targetOs == OS.iOS) ...[ '-isysroot', (await iosSdk(targetIosSdk, logger: logger)).toFilePath(), ], - if (target.os == OS.macOS) ...[ + if (buildConfig.targetOs == OS.macOS) ...[ '-isysroot', (await macosSdk(logger: logger)).toFilePath(), ], @@ -189,7 +191,7 @@ class RunCBuilder { '-x', 'c++', '-l', - cppLinkStdLib ?? defaultCppLinkStdLib[target.os]! + cppLinkStdLib ?? defaultCppLinkStdLib[buildConfig.targetOs]! ], ...flags, for (final MapEntry(key: name, :value) in defines.entries) @@ -292,24 +294,24 @@ class RunCBuilder { } static const androidNdkClangTargetFlags = { - Target.androidArm: 'armv7a-linux-androideabi', - Target.androidArm64: 'aarch64-linux-android', - Target.androidIA32: 'i686-linux-android', - Target.androidX64: 'x86_64-linux-android', - Target.androidRiscv64: 'riscv64-linux-android', + Architecture.arm: 'armv7a-linux-androideabi', + Architecture.arm64: 'aarch64-linux-android', + Architecture.ia32: 'i686-linux-android', + Architecture.x64: 'x86_64-linux-android', + Architecture.riscv64: 'riscv64-linux-android', }; static const appleClangMacosTargetFlags = { - Target.macOSArm64: 'arm64-apple-darwin', - Target.macOSX64: 'x86_64-apple-darwin', + Architecture.arm64: 'arm64-apple-darwin', + Architecture.x64: 'x86_64-apple-darwin', }; static const appleClangIosTargetFlags = { - Target.iOSArm64: { + Architecture.arm64: { IOSSdk.iPhoneOs: 'arm64-apple-ios', IOSSdk.iPhoneSimulator: 'arm64-apple-ios-simulator', }, - Target.iOSX64: { + Architecture.x64: { IOSSdk.iPhoneSimulator: 'x86_64-apple-ios-simulator', }, }; From 624383fab4295708bf14165760b8f8ad37e6ce80 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 19 Feb 2024 18:32:53 +0100 Subject: [PATCH 21/75] Make build runner expand the architectures for CCodeAssets in dry run --- .../lib/src/build_runner/build_runner.dart | 16 ++++- .../build_runner_dry_run_test.dart | 22 +++++-- .../test_data/native_add/pubspec.yaml | 1 - .../native_add_add_source/pubspec.yaml | 1 - .../test_data/native_subtract/pubspec.yaml | 1 - .../lib/src/api/c_code_asset.dart | 59 ++++++++++--------- .../lib/src/model/c_code_asset.dart | 15 ++--- .../lib/src/model/target.dart | 39 +++++++++++- 8 files changed, 104 insertions(+), 50 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 234420ff0..69d00aeb4 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -180,7 +180,21 @@ class NativeAssetsBuildRunner { includeParentEnvironment, dryRun: true, ); - assets.addAll(packageAssets); + for (final asset in packageAssets) { + if (asset.architecture != null) { + // Backwards compatibility, if an architecture is provided use that. + assets.add(asset); + } else { + // Dry run does not report architecture. Dart VM branches on OS and + // Target when looking up assets, so populate assets for all + // architectures. + for (final architecture in asset.os.architectures) { + assets.add(asset.copyWith( + architecture: architecture, + )); + } + } + } success &= packageSuccess; } return _DryRunResultImpl( diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart index f7a5fba2c..6c4b0270d 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart @@ -24,21 +24,31 @@ void main() async { logger: logger, ); - final dryRunAssets = - (await dryRun(packageUri, logger, dartExecutable)).assets.toList(); - final result = await build(packageUri, logger, dartExecutable); + final dryRunResult = await dryRun( + packageUri, + logger, + dartExecutable, + ); + final dryRunAssets = dryRunResult.assets.toList(); + final result = await build( + packageUri, + logger, + dartExecutable, + ); - expect(dryRunAssets.length, result.assets.length); + // Every OS has more than one architecture. + expect(dryRunAssets.length, greaterThan(result.assets.length)); for (var i = 0; i < dryRunAssets.length; i++) { - final dryRunAsset = dryRunAssets[0]; + final dryRunAsset = dryRunAssets[i]; final buildAsset = result.assets[0]; expect(dryRunAsset.linkMode, buildAsset.linkMode); expect(dryRunAsset.id, buildAsset.id); + // The build runner expands CCodeAssets to all architectures. + expect(dryRunAsset.architecture, isNotNull); expect(buildAsset.architecture, isNotNull); expect(buildAsset.file, isNotNull); expect(dryRunAsset.file, isNull); expect(dryRunAsset.os, buildAsset.os); - // The target folders are different, so the paths are different. } final dryRunDir = packageUri.resolve( diff --git a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml index d99730406..46941ace5 100644 --- a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml @@ -12,7 +12,6 @@ dependencies: logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ - # native_toolchain_c: ^0.3.4+1 native_toolchain_c: path: ../../../native_toolchain_c/ diff --git a/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml index eadbcb078..831bb0017 100644 --- a/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml @@ -12,7 +12,6 @@ dependencies: logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ - # native_toolchain_c: ^0.3.4+1 native_toolchain_c: path: ../../../native_toolchain_c/ diff --git a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml index a4f5ee5e3..8ad7b6d8a 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml @@ -12,7 +12,6 @@ dependencies: logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ - # native_toolchain_c: ^0.3.4+1 native_toolchain_c: path: ../../../native_toolchain_c/ diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index 304b58cf9..19761c440 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -4,34 +4,6 @@ part of 'asset.dart'; -abstract final class AssetPath {} - -/// Asset at absolute path. -abstract final class AssetAbsolutePath implements AssetPath { - factory AssetAbsolutePath() = AssetAbsolutePathImpl; -} - -/// Asset is avaliable on the system `PATH`. -/// -/// [uri] only contains a file name. -abstract final class AssetSystemPath implements AssetPath { - Uri get uri; - - factory AssetSystemPath(Uri uri) = AssetSystemPathImpl; -} - -/// Asset is loaded in the process and symbols are available through -/// `DynamicLibrary.process()`. -abstract final class AssetInProcess implements AssetPath { - factory AssetInProcess() = AssetInProcessImpl; -} - -/// Asset is embedded in executable and symbols are available through -/// `DynamicLibrary.executable()`. -abstract final class AssetInExecutable implements AssetPath { - factory AssetInExecutable() = AssetInExecutableImpl; -} - /// A code [Asset] which respects the C application binary interface (ABI). /// /// Typical other languages which produce code assets that respect the C ABI @@ -65,7 +37,8 @@ abstract final class CCodeAsset implements Asset { /// The architecture this asset can run on. /// /// Not available during a [BuildConfig.dryRun]. - Architecture get architecture; + Architecture? get architecture; + AssetPath get path; factory CCodeAsset({ @@ -85,3 +58,31 @@ abstract final class CCodeAsset implements Asset { file: file, ); } + +abstract final class AssetPath {} + +/// Asset at absolute path. +abstract final class AssetAbsolutePath implements AssetPath { + factory AssetAbsolutePath() = AssetAbsolutePathImpl; +} + +/// Asset is avaliable on the system `PATH`. +/// +/// [uri] only contains a file name. +abstract final class AssetSystemPath implements AssetPath { + Uri get uri; + + factory AssetSystemPath(Uri uri) = AssetSystemPathImpl; +} + +/// Asset is loaded in the process and symbols are available through +/// `DynamicLibrary.process()`. +abstract final class AssetInProcess implements AssetPath { + factory AssetInProcess() = AssetInProcessImpl; +} + +/// Asset is embedded in executable and symbols are available through +/// `DynamicLibrary.executable()`. +abstract final class AssetInExecutable implements AssetPath { + factory AssetInExecutable() = AssetInExecutableImpl; +} diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index f5937fc68..731f0e3a0 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -133,13 +133,8 @@ final class CCodeAssetImpl implements CCodeAsset { @override final OSImpl os; - final ArchitectureImpl? _architecture; - - @override - ArchitectureImpl get architecture => _architecture!; - @override - // Target get target => TargetImpl.fromArchitectureAndOs(_architecture!, os); + final ArchitectureImpl? architecture; CCodeAssetImpl({ this.file, @@ -147,8 +142,8 @@ final class CCodeAssetImpl implements CCodeAsset { required this.linkMode, required this.os, required this.path, - ArchitectureImpl? architecture, - }) : _architecture = architecture; + this.architecture, + }); factory CCodeAssetImpl.fromYaml(YamlMap yamlMap) { final path = AssetPathImpl.fromYaml(as(yamlMap[_pathKey])); @@ -219,7 +214,7 @@ final class CCodeAssetImpl implements CCodeAsset { id: id ?? this.id, linkMode: linkMode ?? this.linkMode, os: os ?? this.os, - architecture: architecture ?? _architecture, + architecture: this.architecture ?? architecture, path: path ?? this.path, file: file ?? this.file, ); @@ -248,7 +243,7 @@ final class CCodeAssetImpl implements CCodeAsset { ); Map toYaml() => { - if (_architecture != null) _architectureKey: _architecture.toString(), + if (architecture != null) _architectureKey: architecture.toString(), if (file != null) _fileKey: file!.toFilePath(), _idKey: id, _linkModeKey: linkMode.name, diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index d1610f53c..1ce6e2adb 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -125,6 +125,44 @@ class OSImpl implements OS { Abi.windowsX64: OSImpl.windows, }; + static const _osTargets = { + OSImpl.android: { + ArchitectureImpl.arm, + ArchitectureImpl.arm64, + ArchitectureImpl.ia32, + ArchitectureImpl.x64, + ArchitectureImpl.riscv64, + }, + OSImpl.fuchsia: { + ArchitectureImpl.arm64, + ArchitectureImpl.x64, + }, + OSImpl.iOS: { + ArchitectureImpl.arm, + ArchitectureImpl.arm64, + ArchitectureImpl.x64, + }, + OSImpl.linux: { + ArchitectureImpl.arm, + ArchitectureImpl.arm64, + ArchitectureImpl.ia32, + ArchitectureImpl.riscv32, + ArchitectureImpl.riscv64, + ArchitectureImpl.x64, + }, + OSImpl.macOS: { + ArchitectureImpl.arm64, + ArchitectureImpl.x64, + }, + OSImpl.windows: { + ArchitectureImpl.arm64, + ArchitectureImpl.ia32, + ArchitectureImpl.x64, + }, + }; + + Iterable get architectures => _osTargets[this]!; + /// Typical cross compilation between OSes. static const _osCrossCompilationDefault = { OSImpl.macOS: [OSImpl.macOS, OSImpl.iOS, OSImpl.android], @@ -318,7 +356,6 @@ class TargetImpl implements Target { windowsArm64, windowsIA32, windowsX64, - // TODO(dacoharkes): Add support for `wasm`. }; /// Mapping from strings as used in [TargetImpl.toString] to [TargetImpl]s. From c772761c2147382b5699e8dfc60a37697cb56e15 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 19 Feb 2024 19:19:22 +0100 Subject: [PATCH 22/75] Rename `AssetPathType` to `DynamicLinking` --- .../wrong_namespace_asset/build.dart | 2 +- .../example/local_asset/build.dart | 2 +- .../lib/native_assets_cli.dart | 10 +- .../lib/native_assets_cli_internal.dart | 10 +- .../lib/src/api/c_code_asset.dart | 40 +++--- .../lib/src/api/link_mode.dart | 6 + .../lib/src/helpers/asset_downloader.dart | 2 +- .../lib/src/model/build_output_CHANGELOG.md | 4 + .../lib/src/model/c_code_asset.dart | 129 +++++++++--------- .../test/api/asset_test.dart | 12 +- .../test/api/build_output_test.dart | 4 +- .../test/model/asset_test.dart | 42 +++--- .../test/model/build_output_test.dart | 14 +- .../lib/src/cbuilder/cbuilder.dart | 2 +- 14 files changed, 146 insertions(+), 133 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 8983bbaa8..4c2e68234 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -16,7 +16,7 @@ void main(List args) async { linkMode: LinkMode.dynamic, os: Target.current.os, architecture: Target.current.architecture, - path: AssetAbsolutePath(), + dynamicLoading: BundledDylib(), ), ], ); diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 019c7176e..d26757354 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -38,7 +38,7 @@ void main(List args) async { linkMode: LinkMode.dynamic, os: config.targetOs, architecture: config.dryRun ? null : config.targetArchitecture, - path: AssetAbsolutePath(), + dynamicLoading: BundledDylib(), ) ]); }); diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index adc855098..9add5eaf1 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -9,11 +9,11 @@ library native_assets_cli; export 'src/api/asset.dart' show Asset, - AssetAbsolutePath, - AssetInExecutable, - AssetInProcess, - AssetSystemPath, - CCodeAsset; + BundledDylib, + CCodeAsset, + LookupInExecutable, + LookupInProcess, + SystemDylib; export 'src/api/build.dart'; export 'src/api/build_config.dart' show BuildConfig, CCompilerConfig; export 'src/api/build_mode.dart' show BuildMode; diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index d792b001c..6c00be375 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -10,11 +10,11 @@ library native_assets_cli_internal; export 'src/api/asset.dart' show - AssetAbsolutePathImpl, - AssetInExecutableImpl, - AssetInProcessImpl, - AssetSystemPathImpl, - CCodeAssetImpl; + BundledDylibImpl, + CCodeAssetImpl, + LookupInExecutableImpl, + LookupInProcessImpl, + SystemDylibImpl; export 'src/api/build_config.dart' show BuildConfigImpl, CCompilerConfigImpl; export 'src/api/build_mode.dart' show BuildModeImpl; export 'src/api/build_output.dart' show BuildOutputImpl; diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index 19761c440..1b8112050 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -29,8 +29,6 @@ part of 'asset.dart'; /// Assets are also called "native assets" to differentiate them from the Dart /// code also bundled with an application. abstract final class CCodeAsset implements Asset { - LinkMode get linkMode; - /// The operating system this asset can run on. OS get os; @@ -39,13 +37,19 @@ abstract final class CCodeAsset implements Asset { /// Not available during a [BuildConfig.dryRun]. Architecture? get architecture; - AssetPath get path; + /// The link mode for this native code. + /// + /// Either dynamic loading or static linking. + LinkMode get linkMode; + + /// The dynamic loading method when the [linkMode] is [LinkMode.dynamic]. + DynamicLoading get dynamicLoading; factory CCodeAsset({ required String id, required LinkMode linkMode, required OS os, - required AssetPath path, + required DynamicLoading dynamicLoading, Uri? file, Architecture? architecture, }) => @@ -54,35 +58,39 @@ abstract final class CCodeAsset implements Asset { linkMode: linkMode as LinkModeImpl, os: os as OSImpl, architecture: architecture as ArchitectureImpl?, - path: path as AssetPathImpl, + dynamicLoading: dynamicLoading as DynamicLoadingImpl, file: file, ); } -abstract final class AssetPath {} +/// The dynamic loading method when the [CCodeAsset.linkMode] is +/// [LinkMode.dynamic]. +abstract final class DynamicLoading {} -/// Asset at absolute path. -abstract final class AssetAbsolutePath implements AssetPath { - factory AssetAbsolutePath() = AssetAbsolutePathImpl; +/// The asset file should be bundled by Dart/Flutter. +/// +/// An asset with this dynamic loading method must provide a [Asset.file]. +abstract final class BundledDylib implements DynamicLoading { + factory BundledDylib() = BundledDylibImpl; } -/// Asset is avaliable on the system `PATH`. +/// Asset is avaliable on the target system `PATH`. /// /// [uri] only contains a file name. -abstract final class AssetSystemPath implements AssetPath { +abstract final class SystemDylib implements DynamicLoading { Uri get uri; - factory AssetSystemPath(Uri uri) = AssetSystemPathImpl; + factory SystemDylib(Uri uri) = SystemDylibImpl; } /// Asset is loaded in the process and symbols are available through /// `DynamicLibrary.process()`. -abstract final class AssetInProcess implements AssetPath { - factory AssetInProcess() = AssetInProcessImpl; +abstract final class LookupInProcess implements DynamicLoading { + factory LookupInProcess() = LookupInProcessImpl; } /// Asset is embedded in executable and symbols are available through /// `DynamicLibrary.executable()`. -abstract final class AssetInExecutable implements AssetPath { - factory AssetInExecutable() = AssetInExecutableImpl; +abstract final class LookupInExecutable implements DynamicLoading { + factory LookupInExecutable() = LookupInExecutableImpl; } diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode.dart b/pkgs/native_assets_cli/lib/src/api/link_mode.dart index 701471880..7206957a0 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode.dart @@ -2,10 +2,16 @@ // 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 'asset.dart'; + part '../model/link_mode.dart'; +/// The link mode for [CCodeAsset]s. abstract class LinkMode { + /// Dynamic loading. static const LinkMode dynamic = LinkModeImpl.dynamic; + + /// Static linking. static const LinkMode static = LinkModeImpl.static; /// Known values for [LinkMode]. diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart index 0ebd49656..23e120158 100644 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -60,7 +60,7 @@ class AssetDownloader implements Builder { id: assetId, file: targetUri, linkMode: LinkMode.dynamic, - path: AssetAbsolutePath(), + dynamicLoading: BundledDylib(), os: config.targetOs, architecture: config.dryRun ? null : config.targetArchitecture, ) diff --git a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md index 17092035e..1f5a4e9fb 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md +++ b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md @@ -4,6 +4,10 @@ Backwards compatibility: assets without a type are interpreted as `c_code` assets. - Assets now have an optional `file`. Backwards compatibility: assets that have an `AssetAbsolutePath` will have that path used as `file`. +- Assets now have a `dynamic_loading` field instead of `path_type`. + The `absolute` path_type is renamed to `bundled` as dynamic loading type. + Backwards compatibility: `path` is parsed as `dynamic_linking`. + Backwards compatibility: the nested `path_type` is parsed as `type`. ## 1.0.0 diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index 731f0e3a0..d4b9a4687 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -4,71 +4,65 @@ part of '../api/asset.dart'; -abstract final class AssetPathImpl implements AssetPath { - factory AssetPathImpl(String pathType, Uri? uri) { - switch (pathType) { - case AssetAbsolutePathImpl._pathTypeValue: - return AssetAbsolutePathImpl(); - case AssetSystemPathImpl._pathTypeValue: - return AssetSystemPathImpl(uri!); - case AssetInExecutableImpl._pathTypeValue: - return AssetInExecutableImpl(); - case AssetInProcessImpl._pathTypeValue: - return AssetInProcessImpl(); +abstract final class DynamicLoadingImpl implements DynamicLoading { + factory DynamicLoadingImpl(String type, Uri? uri) { + switch (type) { + case BundledDylibImpl._typeValueV1_0_0: + // For backwards compatibility. + case BundledDylibImpl._typeValue: + return BundledDylibImpl(); + case SystemDylibImpl._typeValue: + return SystemDylibImpl(uri!); + case LookupInExecutableImpl._typeValue: + return LookupInExecutableImpl(); + case LookupInProcessImpl._typeValue: + return LookupInProcessImpl(); } - throw FormatException('Unknown pathType: $pathType.'); + throw FormatException('Unknown type: $type.'); } - factory AssetPathImpl.fromYaml(YamlMap yamlMap) { - final pathType = as(yamlMap[_pathTypeKey]); + factory DynamicLoadingImpl.fromYaml(YamlMap yamlMap) { + final type = as(yamlMap[_typeKey] ?? yamlMap[_pathTypeKey]); final uriString = as(yamlMap[_uriKey]); final uri = uriString != null ? Uri(path: uriString) : null; - return AssetPathImpl(pathType, uri); + return DynamicLoadingImpl(type, uri); } Map toYaml(); static const _pathTypeKey = 'path_type'; + static const _typeKey = 'type'; static const _uriKey = 'uri'; } -final class AssetAbsolutePathImpl implements AssetPathImpl, AssetAbsolutePath { - AssetAbsolutePathImpl(); +final class BundledDylibImpl implements DynamicLoadingImpl, BundledDylib { + BundledDylibImpl._(); - static const _pathTypeValue = 'absolute'; + static final BundledDylibImpl _singleton = BundledDylibImpl._(); - @override - Map toYaml() => { - AssetPathImpl._pathTypeKey: _pathTypeValue, - }; + factory BundledDylibImpl() => _singleton; - @override - int get hashCode => 133711; + static const _typeValueV1_0_0 = 'absolute'; + static const _typeValue = 'bundle'; @override - bool operator ==(Object other) { - if (other is! AssetAbsolutePathImpl) { - return false; - } - return true; - } + Map toYaml() => { + DynamicLoadingImpl._typeKey: _typeValue, + }; } -/// Asset is avaliable on the system `PATH`. -/// -/// [uri] only contains a file name. -final class AssetSystemPathImpl implements AssetPathImpl, AssetSystemPath { +final class SystemDylibImpl implements DynamicLoadingImpl, SystemDylib { @override final Uri uri; - AssetSystemPathImpl(this.uri); + SystemDylibImpl(this.uri); - static const _pathTypeValue = 'system'; + static const _typeValue = 'system'; @override Map toYaml() => { - AssetPathImpl._pathTypeKey: _pathTypeValue, - AssetPathImpl._uriKey: uri.toFilePath(), + DynamicLoadingImpl._typeKey: _typeValue, + DynamicLoadingImpl._uriKey: uri.toFilePath(), }; @override @@ -76,44 +70,41 @@ final class AssetSystemPathImpl implements AssetPathImpl, AssetSystemPath { @override bool operator ==(Object other) { - if (other is! AssetSystemPathImpl) { + if (other is! SystemDylibImpl) { return false; } return uri == other.uri; } } -/// Asset is loaded in the process and symbols are available through -/// `DynamicLibrary.process()`. -final class AssetInProcessImpl implements AssetPathImpl, AssetInProcess { - AssetInProcessImpl._(); +final class LookupInProcessImpl implements DynamicLoadingImpl, LookupInProcess { + LookupInProcessImpl._(); - static final AssetInProcessImpl _singleton = AssetInProcessImpl._(); + static final LookupInProcessImpl _singleton = LookupInProcessImpl._(); - factory AssetInProcessImpl() => _singleton; + factory LookupInProcessImpl() => _singleton; - static const _pathTypeValue = 'process'; + static const _typeValue = 'process'; @override Map toYaml() => { - AssetPathImpl._pathTypeKey: _pathTypeValue, + DynamicLoadingImpl._typeKey: _typeValue, }; } -/// Asset is embedded in executable and symbols are available through -/// `DynamicLibrary.executable()`. -final class AssetInExecutableImpl implements AssetPathImpl, AssetInExecutable { - AssetInExecutableImpl._(); +final class LookupInExecutableImpl + implements DynamicLoadingImpl, LookupInExecutable { + LookupInExecutableImpl._(); - static final AssetInExecutableImpl _singleton = AssetInExecutableImpl._(); + static final LookupInExecutableImpl _singleton = LookupInExecutableImpl._(); - factory AssetInExecutableImpl() => _singleton; + factory LookupInExecutableImpl() => _singleton; - static const _pathTypeValue = 'executable'; + static const _typeValue = 'executable'; @override Map toYaml() => { - AssetPathImpl._pathTypeKey: _pathTypeValue, + DynamicLoadingImpl._typeKey: _typeValue, }; } @@ -128,7 +119,7 @@ final class CCodeAssetImpl implements CCodeAsset { final String id; @override - final AssetPathImpl path; + final DynamicLoadingImpl dynamicLoading; @override final OSImpl os; @@ -141,20 +132,23 @@ final class CCodeAssetImpl implements CCodeAsset { required this.id, required this.linkMode, required this.os, - required this.path, + required this.dynamicLoading, this.architecture, }); factory CCodeAssetImpl.fromYaml(YamlMap yamlMap) { - final path = AssetPathImpl.fromYaml(as(yamlMap[_pathKey])); + final dynamicLoading = DynamicLoadingImpl.fromYaml( + as(yamlMap[_dynamicLoadingKey] ?? yamlMap[_pathKey]), + ); final fileString = as(yamlMap[_fileKey]); final Uri? file; if (fileString != null) { file = Uri(path: fileString); - } else if (path is AssetAbsolutePathImpl) { + } else if (dynamicLoading is BundledDylibImpl && + yamlMap[_pathKey] != null) { // Compatibility with v1.0.0. - final oldPath = - as((yamlMap[_pathKey] as YamlMap)[AssetPathImpl._uriKey]); + final oldPath = as( + (yamlMap[_pathKey] as YamlMap)[DynamicLoadingImpl._uriKey]); file = oldPath != null ? Uri(path: oldPath) : null; } else { file = null; @@ -178,7 +172,7 @@ final class CCodeAssetImpl implements CCodeAsset { } return CCodeAssetImpl( id: as(yamlMap[_idKey]), - path: path, + dynamicLoading: dynamicLoading, os: os, architecture: architecture, linkMode: LinkModeImpl.fromName(as(yamlMap[_linkModeKey])), @@ -207,7 +201,7 @@ final class CCodeAssetImpl implements CCodeAsset { String? id, OSImpl? os, ArchitectureImpl? architecture, - AssetPathImpl? path, + DynamicLoadingImpl? dynamicLoading, Uri? file, }) => CCodeAssetImpl( @@ -215,7 +209,7 @@ final class CCodeAssetImpl implements CCodeAsset { linkMode: linkMode ?? this.linkMode, os: os ?? this.os, architecture: this.architecture ?? architecture, - path: path ?? this.path, + dynamicLoading: dynamicLoading ?? this.dynamicLoading, file: file ?? this.file, ); @@ -228,7 +222,7 @@ final class CCodeAssetImpl implements CCodeAsset { other.linkMode == linkMode && other.architecture == architecture && other.os == os && - other.path == path && + other.dynamicLoading == dynamicLoading && other.file == file; } @@ -238,17 +232,17 @@ final class CCodeAssetImpl implements CCodeAsset { linkMode, architecture, os, - path, + dynamicLoading, file, ); Map toYaml() => { if (architecture != null) _architectureKey: architecture.toString(), + _dynamicLoadingKey: dynamicLoading.toYaml(), if (file != null) _fileKey: file!.toFilePath(), _idKey: id, _linkModeKey: linkMode.name, _osKey: os.toString(), - _pathKey: path.toYaml(), typeKey: type, }; @@ -257,6 +251,7 @@ final class CCodeAssetImpl implements CCodeAsset { static const _idKey = 'id'; static const _linkModeKey = 'link_mode'; static const _pathKey = 'path'; + static const _dynamicLoadingKey = 'dynamic_loading'; static const _targetKey = 'target'; static const _fileKey = 'file'; static const _osKey = 'os'; diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index 796f41ec0..f56577b86 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -11,28 +11,28 @@ void main() { CCodeAsset( id: 'foo', file: Uri.file('path/to/libfoo.so'), - path: AssetAbsolutePath(), + dynamicLoading: BundledDylib(), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( id: 'foo3', - path: AssetSystemPath(Uri(path: 'libfoo3.so')), + dynamicLoading: SystemDylib(Uri(path: 'libfoo3.so')), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( id: 'foo4', - path: AssetInExecutable(), + dynamicLoading: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( id: 'foo5', - path: AssetInProcess(), + dynamicLoading: LookupInProcess(), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, @@ -40,7 +40,7 @@ void main() { CCodeAsset( id: 'bar', file: Uri(path: 'path/to/libbar.a'), - path: AssetAbsolutePath(), + dynamicLoading: BundledDylib(), os: OS.linux, architecture: Architecture.arm64, linkMode: LinkMode.static, @@ -48,7 +48,7 @@ void main() { CCodeAsset( id: 'bla', file: Uri(path: 'path/with spaces/bla.dll'), - path: AssetAbsolutePath(), + dynamicLoading: BundledDylib(), os: OS.windows, architecture: Architecture.x64, linkMode: LinkMode.dynamic, diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index a9cdd8cce..dd97ff33e 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -25,14 +25,14 @@ void main() { CCodeAsset( id: 'foo', file: Uri(path: 'path/to/libfoo.so'), - path: AssetAbsolutePath(), + dynamicLoading: BundledDylib(), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( id: 'foo2', - path: AssetSystemPath(Uri(path: 'path/to/libfoo2.so')), + dynamicLoading: SystemDylib(Uri(path: 'path/to/libfoo2.so')), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 1bd1a98e6..9ca944018 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -16,28 +16,28 @@ void main() { CCodeAssetImpl( id: 'foo', file: fooUri, - path: AssetAbsolutePathImpl(), + dynamicLoading: BundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( id: 'foo3', - path: AssetSystemPathImpl(foo3Uri), + dynamicLoading: SystemDylibImpl(foo3Uri), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( id: 'foo4', - path: AssetInExecutableImpl(), + dynamicLoading: LookupInExecutableImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( id: 'foo5', - path: AssetInProcessImpl(), + dynamicLoading: LookupInProcessImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, @@ -45,7 +45,7 @@ void main() { CCodeAssetImpl( id: 'bar', file: barUri, - path: AssetAbsolutePathImpl(), + dynamicLoading: BundledDylibImpl(), os: OSImpl.linux, architecture: ArchitectureImpl.arm64, linkMode: LinkModeImpl.static, @@ -53,7 +53,7 @@ void main() { CCodeAssetImpl( id: 'bla', file: blaUri, - path: AssetAbsolutePathImpl(), + dynamicLoading: BundledDylibImpl(), os: OSImpl.windows, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, @@ -96,50 +96,50 @@ void main() { target: windows_x64'''; final assetsYamlEncoding = '''- architecture: x64 + dynamic_loading: + type: bundle file: ${fooUri.toFilePath()} id: foo link_mode: dynamic os: android - path: - path_type: absolute type: c_code - architecture: x64 + dynamic_loading: + type: system + uri: ${foo3Uri.toFilePath()} id: foo3 link_mode: dynamic os: android - path: - path_type: system - uri: ${foo3Uri.toFilePath()} type: c_code - architecture: x64 + dynamic_loading: + type: executable id: foo4 link_mode: dynamic os: android - path: - path_type: executable type: c_code - architecture: x64 + dynamic_loading: + type: process id: foo5 link_mode: dynamic os: android - path: - path_type: process type: c_code - architecture: arm64 + dynamic_loading: + type: bundle file: ${barUri.toFilePath()} id: bar link_mode: static os: linux - path: - path_type: absolute type: c_code - architecture: x64 + dynamic_loading: + type: bundle file: ${blaUri.toFilePath()} id: bla link_mode: dynamic os: windows - path: - path_type: absolute type: c_code'''; test('asset yaml', () { @@ -156,9 +156,9 @@ void main() { test('AssetPath factory', () async { expect( - () => AssetPathImpl('wrong', null), + () => DynamicLoadingImpl('wrong', null), throwsA(predicate( - (e) => e is FormatException && e.message.contains('Unknown pathType'), + (e) => e is FormatException && e.message.contains('Unknown type'), )), ); }); diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index f6b12040c..5255d0695 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -24,14 +24,14 @@ void main() { CCodeAssetImpl( id: 'foo', file: Uri(path: 'path/to/libfoo.so'), - path: AssetAbsolutePathImpl(), + dynamicLoading: BundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( id: 'foo2', - path: AssetSystemPathImpl(Uri(path: 'path/to/libfoo2.so')), + dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, @@ -68,20 +68,20 @@ version: 1.0.0'''; final yamlEncoding = '''timestamp: 2022-11-10 13:25:01.000 assets: - architecture: x64 + dynamic_loading: + type: bundle file: path/to/libfoo.so id: foo link_mode: dynamic os: android - path: - path_type: absolute type: c_code - architecture: x64 + dynamic_loading: + type: system + uri: path/to/libfoo2.so id: foo2 link_mode: dynamic os: android - path: - path_type: system - uri: path/to/libfoo2.so type: c_code dependencies: - path/to/file.ext diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 5e7bb1438..6250ab690 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -255,7 +255,7 @@ class CBuilder implements Builder { os: buildConfig.targetOs, architecture: buildConfig.dryRun ? null : buildConfig.targetArchitecture, - path: AssetAbsolutePath(), + dynamicLoading: BundledDylib(), ) ]); } From 6c5003bf4bb5a0256b5dba05185f6090cc2cf2ae Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 19 Feb 2024 20:00:23 +0100 Subject: [PATCH 23/75] Add a supported_asset_types to BuildConfig --- .../lib/src/api/build_config.dart | 12 ++-- .../lib/src/api/c_code_asset.dart | 2 + .../lib/src/model/build_config.dart | 35 +++++++++-- .../lib/src/model/build_config_CHANGELOG.md | 8 +++ .../lib/src/model/build_output.dart | 2 +- .../lib/src/model/c_code_asset.dart | 3 +- .../test/model/build_config_test.dart | 58 +++++++++++++++++++ 7 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 1a012d782..964c25fe7 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -13,6 +13,7 @@ import 'package:pub_semver/pub_semver.dart'; import '../model/metadata.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; +import 'asset.dart'; import 'build_mode.dart'; import 'ios_sdk.dart'; import 'link_mode_preference.dart'; @@ -40,11 +41,6 @@ abstract final class BuildConfig { /// dependency of another. For this it is convenient to know the packageRoot. Uri get packageRoot; - // /// The target being compiled for. - // /// - // /// Not available during a [dryRun]. - // Target get target; - /// The architecture being compiled for. /// /// Not available during a [dryRun]. @@ -103,6 +99,12 @@ abstract final class BuildConfig { /// The underlying config. Config get config; + /// The asset types the invoker of this build supports. + /// + /// Currently known values: + /// * [CCodeAsset.type] + Iterable get supportedAssetTypes; + /// The version of [BuildConfig]. /// /// This class is used in the protocol between the Dart and Flutter SDKs diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index 1b8112050..fc86010bc 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -61,6 +61,8 @@ abstract final class CCodeAsset implements Asset { dynamicLoading: dynamicLoading as DynamicLoadingImpl, file: file, ); + + static const String type = 'c_code'; } /// The dynamic loading method when the [CCodeAsset.linkMode] is diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 411d4838a..e491c9ab1 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -111,6 +111,11 @@ final class BuildConfigImpl implements BuildConfig { late final BuildModeImpl _buildMode; + @override + Iterable get supportedAssetTypes => _supportedAssetTypes; + + late final List _supportedAssetTypes; + @override Config get config => _config; late final Config _config; @@ -127,6 +132,7 @@ final class BuildConfigImpl implements BuildConfig { CCompilerConfigImpl? cCompiler, required LinkModePreferenceImpl linkModePreference, Map? dependencyMetadata, + Iterable? supportedAssetTypes, }) { final nonValidated = BuildConfigImpl._() .._outDir = outDir @@ -140,7 +146,12 @@ final class BuildConfigImpl implements BuildConfig { .._cCompiler = cCompiler ?? CCompilerConfigImpl() .._linkModePreference = linkModePreference .._dependencyMetadata = dependencyMetadata - .._dryRun = false; + .._dryRun = false + .._supportedAssetTypes = [ + ...?supportedAssetTypes, + // Backwards compatibility. + if (supportedAssetTypes == null) CCodeAsset.type, + ]; final parsedConfigFile = nonValidated.toYaml(); final config = Config(fileParsed: parsedConfigFile); return BuildConfigImpl.fromConfig(config); @@ -152,6 +163,7 @@ final class BuildConfigImpl implements BuildConfig { required Uri packageRoot, required OSImpl targetOs, required LinkModePreferenceImpl linkModePreference, + Iterable? supportedAssetTypes, }) { final nonValidated = BuildConfigImpl._() .._outDir = outDir @@ -160,7 +172,12 @@ final class BuildConfigImpl implements BuildConfig { .._targetOs = targetOs .._linkModePreference = linkModePreference .._cCompiler = CCompilerConfigImpl() - .._dryRun = true; + .._dryRun = true + .._supportedAssetTypes = [ + ...?supportedAssetTypes, + // Backwards compatibility. + if (supportedAssetTypes == null) CCodeAsset.type, + ]; final parsedConfigFile = nonValidated.toYaml(); final config = Config(fileParsed: parsedConfigFile); return BuildConfigImpl.fromConfig(config); @@ -184,6 +201,7 @@ final class BuildConfigImpl implements BuildConfig { CCompilerConfigImpl? cCompiler, required LinkModePreferenceImpl linkModePreference, Map? dependencyMetadata, + Iterable? supportedAssetTypes, }) { final input = [ packageName, @@ -202,7 +220,8 @@ final class BuildConfigImpl implements BuildConfig { for (final entry in dependencyMetadata.entries) ...[ entry.key, json.encode(entry.value.toYaml()), - ] + ], + ...?supportedAssetTypes, ].join('###'); final sha256String = sha256.convert(utf8.encode(input)).toString(); // 256 bit hashes lead to 64 hex character strings. @@ -222,7 +241,7 @@ final class BuildConfigImpl implements BuildConfig { /// If we ever were to make breaking changes, it would be useful to give /// proper error messages rather than just fail to parse the YAML /// representation in the protocol. - static Version version = Version(1, 0, 0); + static Version version = Version(1, 1, 0); factory BuildConfigImpl.fromConfig(Config config) { final result = BuildConfigImpl._().._cCompiler = CCompilerConfigImpl._(); @@ -276,6 +295,7 @@ final class BuildConfigImpl implements BuildConfig { static const _versionKey = 'version'; static const targetAndroidNdkApiConfigKey = 'target_android_ndk_api'; static const dryRunConfigKey = 'dry_run'; + static const supportedAssetTypesKey = 'supported_asset_types'; List _readFieldsFromConfig() { var osSet = false; @@ -431,6 +451,9 @@ final class BuildConfigImpl implements BuildConfig { (config) { _dependencyMetadata = _readDependencyMetadataFromConfig(config); }, + (config) => _supportedAssetTypes = + config.optionalStringList(supportedAssetTypesKey) ?? + [CCodeAsset.type], ]; } @@ -472,6 +495,7 @@ final class BuildConfigImpl implements BuildConfig { packageRootConfigKey: _packageRoot.toFilePath(), OSImpl.configKey: _targetOs.toString(), LinkModePreferenceImpl.configKey: _linkModePreference.toString(), + supportedAssetTypesKey: _supportedAssetTypes, _versionKey: version.toString(), if (dryRun) dryRunConfigKey: dryRun, if (!dryRun) ...{ @@ -505,6 +529,8 @@ final class BuildConfigImpl implements BuildConfig { if (other.dryRun != dryRun) return false; if (other.targetOs != targetOs) return false; if (other.linkModePreference != linkModePreference) return false; + if (!const DeepCollectionEquality() + .equals(other._supportedAssetTypes, _supportedAssetTypes)) return false; if (!dryRun) { if (other.buildMode != buildMode) return false; if (other.targetArchitecture != targetArchitecture) return false; @@ -525,6 +551,7 @@ final class BuildConfigImpl implements BuildConfig { targetOs, linkModePreference, dryRun, + const DeepCollectionEquality().hash(_supportedAssetTypes), if (!dryRun) ...[ buildMode, const DeepCollectionEquality().hash(_dependencyMetadata), diff --git a/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md new file mode 100644 index 000000000..191e8d88e --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md @@ -0,0 +1,8 @@ +## 1.1.0 + +- Added supported asset types. + Backwards compatibility: Defaults to a list with a single element: `c_code`. + +## 1.0.0 + +- Initial version. diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index ce5d1357f..d12bf802f 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -75,7 +75,7 @@ class BuildOutputImpl implements BuildOutput { final yamlMap = as(yamlElement); final type = yamlMap[CCodeAssetImpl.typeKey]; switch (type) { - case CCodeAssetImpl.type: + case CCodeAsset.type: case null: // Backwards compatibility with v1.0.0. assets.add(CCodeAssetImpl.fromYaml(yamlMap)); default: diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index d4b9a4687..5967a78d6 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -243,11 +243,10 @@ final class CCodeAssetImpl implements CCodeAsset { _idKey: id, _linkModeKey: linkMode.name, _osKey: os.toString(), - typeKey: type, + typeKey: CCodeAsset.type, }; static const typeKey = 'type'; - static const type = 'c_code'; static const _idKey = 'id'; static const _linkModeKey = 'link_mode'; static const _pathKey = 'path'; diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index aacb13626..5fdc7af32 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -6,6 +6,7 @@ import 'dart:io'; import 'package:cli_config/cli_config.dart'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; +import 'package:native_assets_cli/src/api/asset.dart'; import 'package:test/test.dart'; import '../helpers.dart'; @@ -254,6 +255,8 @@ link_mode_preference: prefer-static out_dir: ${outDir.toFilePath()} package_name: $packageName package_root: ${tempUri.toFilePath()} +supported_asset_types: + - ${CCodeAsset.type} target_architecture: arm64 target_ios_sdk: iphoneos target_os: ios @@ -268,6 +271,61 @@ version: ${BuildConfigImpl.version}'''; expect(buildConfig2, buildConfig1); }); + test('BuildConfig fromYaml v1.0.0 keeps working', () { + final outDir = outDirUri; + final yamlString = '''build_mode: release +c_compiler: + cc: ${fakeClang.toFilePath()} + ld: ${fakeLd.toFilePath()} +dependency_metadata: + bar: + key: value + foo: + a: 321 + z: + - z + - a +link_mode_preference: prefer-static +out_dir: ${outDir.toFilePath()} +package_name: $packageName +package_root: ${tempUri.toFilePath()} +target_architecture: arm64 +target_ios_sdk: iphoneos +target_os: ios +version: 1.0.0'''; + final buildConfig1 = BuildConfigImpl( + outDir: outDir, + packageName: packageName, + packageRoot: tempUri, + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOs, + cCompiler: CCompilerConfigImpl( + cc: fakeClang, + ld: fakeLd, + ), + buildMode: BuildModeImpl.release, + linkModePreference: LinkModePreferenceImpl.preferStatic, + // This map should be sorted on key for two layers. + dependencyMetadata: { + 'foo': const Metadata({ + 'z': ['z', 'a'], + 'a': 321, + }), + 'bar': const Metadata({ + 'key': 'value', + }), + }, + ); + + final buildConfig2 = BuildConfigImpl.fromConfig( + Config.fromConfigFileContents( + fileContents: yamlString, + ), + ); + expect(buildConfig2, buildConfig1); + }); + test('BuildConfig FormatExceptions', () { expect( () => BuildConfigImpl.fromConfig(Config(fileParsed: {})), From 564eeee15a558659b83d0632e1216669cee01370 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 20 Feb 2024 09:25:55 +0100 Subject: [PATCH 24/75] Output older BuildOutput format if old BuildConfig was passed --- .../test_data/cyclic_package_1/build.dart | 6 +- .../test_data/cyclic_package_2/build.dart | 6 +- .../test_data/native_add/build.dart | 2 +- .../native_add_add_source/build.dart | 2 +- .../test_data/native_subtract/build.dart | 2 +- .../package_with_metadata/build.dart | 2 +- .../wrong_namespace_asset/build.dart | 2 +- .../example/use_dart_api/build.dart | 2 +- pkgs/native_assets_cli/lib/src/api/asset.dart | 2 + pkgs/native_assets_cli/lib/src/api/build.dart | 2 +- .../lib/src/api/build_config.dart | 2 +- .../lib/src/api/build_output.dart | 7 +- .../lib/src/model/build_config.dart | 23 ++++--- .../lib/src/model/build_output.dart | 43 ++++++++----- .../lib/src/model/build_output_CHANGELOG.md | 3 +- .../lib/src/model/c_code_asset.dart | 64 ++++++++++++------- .../test/api/build_config_test.dart | 6 +- .../test/example/local_asset_test.dart | 2 +- .../test/example/native_add_library_test.dart | 2 +- .../test/model/asset_test.dart | 5 +- .../test/model/build_config_test.dart | 20 +++--- .../test/model/build_output_test.dart | 37 ++++++++--- 22 files changed, 151 insertions(+), 91 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_1/build.dart b/pkgs/native_assets_builder/test_data/cyclic_package_1/build.dart index 2479e4ea2..1756bf9e4 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_1/build.dart +++ b/pkgs/native_assets_builder/test_data/cyclic_package_1/build.dart @@ -5,7 +5,7 @@ import 'package:native_assets_cli/native_assets_cli.dart'; void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - final buildOutput = BuildOutput(); - await buildOutput.writeToFile(outDir: buildConfig.outDir); + final config = await BuildConfig.fromArgs(args); + final output = BuildOutput(); + await output.writeToFile(config: config); } diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_2/build.dart b/pkgs/native_assets_builder/test_data/cyclic_package_2/build.dart index 2479e4ea2..1756bf9e4 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_2/build.dart +++ b/pkgs/native_assets_builder/test_data/cyclic_package_2/build.dart @@ -5,7 +5,7 @@ import 'package:native_assets_cli/native_assets_cli.dart'; void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - final buildOutput = BuildOutput(); - await buildOutput.writeToFile(outDir: buildConfig.outDir); + final config = await BuildConfig.fromArgs(args); + final output = BuildOutput(); + await output.writeToFile(config: config); } diff --git a/pkgs/native_assets_builder/test_data/native_add/build.dart b/pkgs/native_assets_builder/test_data/native_add/build.dart index 69bbb9ede..75286846f 100644 --- a/pkgs/native_assets_builder/test_data/native_add/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add/build.dart @@ -27,5 +27,5 @@ void main(List args) async { print('${record.level.name}: ${record.time}: ${record.message}'); }), ); - await buildOutput.writeToFile(outDir: buildConfig.outDir); + await buildOutput.writeToFile(config: buildConfig); } diff --git a/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart b/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart index 3fcb30e4b..3970445f2 100644 --- a/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart @@ -28,5 +28,5 @@ void main(List args) async { print('${record.level.name}: ${record.time}: ${record.message}'); }), ); - await buildOutput.writeToFile(outDir: buildConfig.outDir); + await buildOutput.writeToFile(config: buildConfig); } diff --git a/pkgs/native_assets_builder/test_data/native_subtract/build.dart b/pkgs/native_assets_builder/test_data/native_subtract/build.dart index 66b62ea90..227987771 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/build.dart +++ b/pkgs/native_assets_builder/test_data/native_subtract/build.dart @@ -27,5 +27,5 @@ void main(List args) async { print('${record.level.name}: ${record.time}: ${record.message}'); }), ); - await buildOutput.writeToFile(outDir: buildConfig.outDir); + await buildOutput.writeToFile(config: buildConfig); } diff --git a/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart b/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart index d8cdc389c..8f54c9143 100644 --- a/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart +++ b/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart @@ -12,5 +12,5 @@ void main(List args) async { 'some_int': 3, }, ); - await buildOutput.writeToFile(outDir: buildConfig.outDir); + await buildOutput.writeToFile(config: buildConfig); } diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 4c2e68234..10a16a7b4 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -20,5 +20,5 @@ void main(List args) async { ), ], ); - await buildOutput.writeToFile(outDir: buildConfig.outDir); + await buildOutput.writeToFile(config: buildConfig); } diff --git a/pkgs/native_assets_cli/example/use_dart_api/build.dart b/pkgs/native_assets_cli/example/use_dart_api/build.dart index 10380ec58..d4ced1fe3 100644 --- a/pkgs/native_assets_cli/example/use_dart_api/build.dart +++ b/pkgs/native_assets_cli/example/use_dart_api/build.dart @@ -28,5 +28,5 @@ void main(List args) async { print('${record.level.name}: ${record.time}: ${record.message}'); }), ); - await buildOutput.writeToFile(outDir: buildConfig.outDir); + await buildOutput.writeToFile(config: buildConfig); } diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index accf329e2..5f7c4e091 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -2,10 +2,12 @@ // 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:pub_semver/pub_semver.dart'; import 'package:yaml/yaml.dart'; import '../../native_assets_cli.dart'; import '../../native_assets_cli_internal.dart'; +import '../utils/map.dart'; import '../utils/yaml.dart'; import 'link_mode.dart'; import 'target.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 60833cc55..7bd1925ee 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -86,5 +86,5 @@ Future build( final config = await BuildConfig.fromArgs(commandlineArguments); final output = BuildOutput(); await builder(config, output); - await output.writeToFile(outDir: config.outDir); + await output.writeToFile(config: config); } diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 964c25fe7..f2d8d11c4 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -113,7 +113,7 @@ abstract final class BuildConfig { /// If we ever were to make breaking changes, it would be useful to give /// proper error messages rather than just fail to parse the YAML /// representation in the protocol. - static Version get version => BuildConfigImpl.version; + static Version get latestVersion => BuildConfigImpl.latestVersion; factory BuildConfig({ required Uri outDir, diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 4ab1737f8..c74423c9a 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -15,6 +15,7 @@ import '../utils/file.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; import 'asset.dart'; +import 'build_config.dart'; import 'target.dart'; part '../model/build_output.dart'; @@ -119,8 +120,8 @@ abstract class BuildOutput { /// If we ever were to make breaking changes, it would be useful to give /// proper error messages rather than just fail to parse the YAML /// representation in the protocol. - static Version get version => BuildOutputImpl.version; + static Version get latestVersion => BuildOutputImpl.latestVersion; - /// Write out this build output to a file inside [outDir]. - Future writeToFile({required Uri outDir}); + /// Write out this build output to a file inside [BuildConfig.outDir]. + Future writeToFile({required BuildConfig config}); } diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index e491c9ab1..b75c0433f 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -116,6 +116,10 @@ final class BuildConfigImpl implements BuildConfig { late final List _supportedAssetTypes; + Version get version => _version; + + late final Version _version; + @override Config get config => _config; late final Config _config; @@ -241,7 +245,7 @@ final class BuildConfigImpl implements BuildConfig { /// If we ever were to make breaking changes, it would be useful to give /// proper error messages rather than just fail to parse the YAML /// representation in the protocol. - static Version version = Version(1, 1, 0); + static Version latestVersion = Version(1, 1, 0); factory BuildConfigImpl.fromConfig(Config config) { final result = BuildConfigImpl._().._cCompiler = CCompilerConfigImpl._(); @@ -302,21 +306,22 @@ final class BuildConfigImpl implements BuildConfig { var ccSet = false; return [ (config) { - final configVersion = Version.parse(config.string('version')); - if (configVersion.major > version.major) { + final version = Version.parse(config.string('version')); + if (version.major > latestVersion.major) { throw FormatException( - 'The config version $configVersion is newer than this ' - 'package:native_assets_cli config version $version, ' + 'The config version $version is newer than this ' + 'package:native_assets_cli config version $latestVersion, ' 'please update native_assets_cli.', ); } - if (configVersion.major < version.major) { + if (version.major < latestVersion.major) { throw FormatException( - 'The config version $configVersion is newer than this ' - 'package:native_assets_cli config version $version, ' + 'The config version $version is newer than this ' + 'package:native_assets_cli config version $latestVersion, ' 'please update the Dart or Flutter SDK.', ); } + _version = version; }, (config) => _config = config, (config) => _dryRun = config.optionalBool(dryRunConfigKey), @@ -496,7 +501,7 @@ final class BuildConfigImpl implements BuildConfig { OSImpl.configKey: _targetOs.toString(), LinkModePreferenceImpl.configKey: _linkModePreference.toString(), supportedAssetTypesKey: _supportedAssetTypes, - _versionKey: version.toString(), + _versionKey: latestVersion.toString(), if (dryRun) dryRunConfigKey: dryRun, if (!dryRun) ...{ BuildModeImpl.configKey: _buildMode.toString(), diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index d12bf802f..7a2781d88 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -55,18 +55,18 @@ class BuildOutputImpl implements BuildOutput { factory BuildOutputImpl.fromYaml(YamlMap yamlMap) { final outputVersion = Version.parse(as(yamlMap['version'])); - if (outputVersion.major > version.major) { + if (outputVersion.major > latestVersion.major) { throw FormatException( 'The output version $outputVersion is newer than the ' - 'package:native_assets_cli config version $version in Dart or Flutter, ' - 'please update the Dart or Flutter SDK.', + 'package:native_assets_cli config version $latestVersion in Dart or ' + 'Flutter, please update the Dart or Flutter SDK.', ); } - if (outputVersion.major < version.major) { + if (outputVersion.major < latestVersion.major) { throw FormatException( 'The output version $outputVersion is newer than this ' - 'package:native_assets_cli config version $version in Dart or Flutter, ' - 'please update native_assets_cli.', + 'package:native_assets_cli config version $latestVersion in Dart or ' + 'Flutter, please update native_assets_cli.', ); } @@ -94,26 +94,34 @@ class BuildOutputImpl implements BuildOutput { ); } - Map toYaml() => { + Map toYaml(Version version) => { _timestampKey: timestamp.toString(), - _assetsKey: _assets.toYaml(), + _assetsKey: [ + for (final asset in _assets) asset.toYaml(version), + ], if (_dependencies.dependencies.isNotEmpty) _dependenciesKey: _dependencies.toYaml(), _metadataKey: _metadata.toYaml(), _versionKey: version.toString(), }..sortOnKey(); - String toYamlString() => yamlEncode(toYaml()); + String toYamlString(Version version) => yamlEncode(toYaml(version)); /// The version of [BuildOutputImpl]. /// - /// This class is used in the protocol between the Dart and Flutter SDKs - /// and packages through `build.dart` invocations. + /// This class is used in the protocol between the Dart and Flutter SDKs and + /// packages through `build.dart` invocations. /// /// If we ever were to make breaking changes, it would be useful to give /// proper error messages rather than just fail to parse the YAML /// representation in the protocol. - static Version version = Version(1, 1, 0); + /// + /// [BuildOutput.latestVersion] is tied to [BuildConfig.latestVersion]. This + /// enables making the yaml serialization in `build.dart` dependent on the + /// version of the Dart or Flutter SDK. When there is a need to split the + /// versions of BuildConfig and BuildOutput, the BuildConfig should start + /// passing the highest supported version of BuildOutput. + static Version latestVersion = BuildConfigImpl.latestVersion; static const fileName = 'build_output.yaml'; @@ -127,16 +135,17 @@ class BuildOutputImpl implements BuildOutput { return BuildOutputImpl.fromYamlString(await buildOutputFile.readAsString()); } - /// Writes the [toYamlString] to [outDir]/[fileName]. + /// Writes the [toYamlString] to [BuildConfig.outDir]/[fileName]. @override - Future writeToFile({required Uri outDir}) async { + Future writeToFile({required BuildConfig config}) async { + final outDir = config.outDir; final buildOutputUri = outDir.resolve(fileName); - await File.fromUri(buildOutputUri) - .writeAsStringCreateDirectory(toYamlString()); + final yamlString = toYamlString((config as BuildConfigImpl).version); + await File.fromUri(buildOutputUri).writeAsStringCreateDirectory(yamlString); } @override - String toString() => toYamlString(); + String toString() => toYamlString(BuildConfigImpl.latestVersion); @override bool operator ==(Object other) { diff --git a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md index 1f5a4e9fb..1bb81c02f 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md +++ b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md @@ -4,10 +4,11 @@ Backwards compatibility: assets without a type are interpreted as `c_code` assets. - Assets now have an optional `file`. Backwards compatibility: assets that have an `AssetAbsolutePath` will have that path used as `file`. -- Assets now have a `dynamic_loading` field instead of `path_type`. +- **Breaking change** Assets now have a `dynamic_loading` field instead of `path_type`. The `absolute` path_type is renamed to `bundled` as dynamic loading type. Backwards compatibility: `path` is parsed as `dynamic_linking`. Backwards compatibility: the nested `path_type` is parsed as `type`. + Backwards compatibility older SDKs: emit the old format if an older BuildConfig was passed in. ## 1.0.0 diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index 5967a78d6..f1d67fef5 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -28,7 +28,7 @@ abstract final class DynamicLoadingImpl implements DynamicLoading { return DynamicLoadingImpl(type, uri); } - Map toYaml(); + Map toYaml(Version version, Uri? file); static const _pathTypeKey = 'path_type'; static const _typeKey = 'type'; @@ -46,8 +46,12 @@ final class BundledDylibImpl implements DynamicLoadingImpl, BundledDylib { static const _typeValue = 'bundle'; @override - Map toYaml() => { - DynamicLoadingImpl._typeKey: _typeValue, + Map toYaml(Version version, Uri? file) => { + if (version == Version(1, 0, 0)) ...{ + DynamicLoadingImpl._pathTypeKey: _typeValueV1_0_0, + DynamicLoadingImpl._uriKey: file!.toFilePath(), + } else + DynamicLoadingImpl._typeKey: _typeValue, }; } @@ -60,8 +64,11 @@ final class SystemDylibImpl implements DynamicLoadingImpl, SystemDylib { static const _typeValue = 'system'; @override - Map toYaml() => { - DynamicLoadingImpl._typeKey: _typeValue, + Map toYaml(Version version, Uri? file) => { + if (version == Version(1, 0, 0)) + DynamicLoadingImpl._pathTypeKey: _typeValue + else + DynamicLoadingImpl._typeKey: _typeValue, DynamicLoadingImpl._uriKey: uri.toFilePath(), }; @@ -87,8 +94,11 @@ final class LookupInProcessImpl implements DynamicLoadingImpl, LookupInProcess { static const _typeValue = 'process'; @override - Map toYaml() => { - DynamicLoadingImpl._typeKey: _typeValue, + Map toYaml(Version version, Uri? file) => { + if (version == Version(1, 0, 0)) + DynamicLoadingImpl._pathTypeKey: _typeValue + else + DynamicLoadingImpl._typeKey: _typeValue, }; } @@ -103,8 +113,11 @@ final class LookupInExecutableImpl static const _typeValue = 'executable'; @override - Map toYaml() => { - DynamicLoadingImpl._typeKey: _typeValue, + Map toYaml(Version version, Uri? file) => { + if (version == Version(1, 0, 0)) + DynamicLoadingImpl._pathTypeKey: _typeValue + else + DynamicLoadingImpl._typeKey: _typeValue, }; } @@ -236,15 +249,26 @@ final class CCodeAssetImpl implements CCodeAsset { file, ); - Map toYaml() => { - if (architecture != null) _architectureKey: architecture.toString(), - _dynamicLoadingKey: dynamicLoading.toYaml(), - if (file != null) _fileKey: file!.toFilePath(), + Map toYaml(Version version) { + if (version == Version(1, 0, 0)) { + return { _idKey: id, _linkModeKey: linkMode.name, - _osKey: os.toString(), - typeKey: CCodeAsset.type, - }; + _pathKey: dynamicLoading.toYaml(version, file), + _targetKey: + TargetImpl.fromArchitectureAndOs(architecture!, os).toString(), + }..sortOnKey(); + } + return { + if (architecture != null) _architectureKey: architecture.toString(), + _dynamicLoadingKey: dynamicLoading.toYaml(version, file), + if (file != null) _fileKey: file!.toFilePath(), + _idKey: id, + _linkModeKey: linkMode.name, + _osKey: os.toString(), + typeKey: CCodeAsset.type, + }..sortOnKey(); + } static const typeKey = 'type'; static const _idKey = 'id'; @@ -257,11 +281,5 @@ final class CCodeAssetImpl implements CCodeAsset { static const _architectureKey = 'architecture'; @override - String toString() => 'CCodeAsset(${toYaml()})'; -} - -extension AssetIterable on Iterable { - List toYaml() => [for (final item in this) item.toYaml()]; - - String toYamlString() => yamlEncode(toYaml()); + String toString() => 'CCodeAsset(${toYaml(BuildOutputImpl.latestVersion)})'; } diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 1dfbd29cb..c9b8d66a6 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -113,7 +113,7 @@ void main() async { 'target_android_ndk_api': 30, 'target_architecture': 'arm64', 'target_os': 'android', - 'version': BuildOutput.version.toString(), + 'version': BuildOutput.latestVersion.toString(), }); final fromConfig = BuildConfig.fromConfig(config); @@ -136,7 +136,7 @@ void main() async { 'package_name': packageName, 'package_root': packageRootUri.toFilePath(), 'target_os': 'android', - 'version': BuildOutput.version.toString(), + 'version': BuildOutput.latestVersion.toString(), }); final fromConfig = BuildConfig.fromConfig(config); @@ -211,6 +211,6 @@ void main() async { }); test('BuildConfig.version', () { - BuildConfig.version.toString(); + BuildConfig.latestVersion.toString(); }); } diff --git a/pkgs/native_assets_cli/test/example/local_asset_test.dart b/pkgs/native_assets_cli/test/example/local_asset_test.dart index f2c55be50..f007d22b6 100644 --- a/pkgs/native_assets_cli/test/example/local_asset_test.dart +++ b/pkgs/native_assets_cli/test/example/local_asset_test.dart @@ -43,7 +43,7 @@ void main() async { '-Dpackage_name=$name', '-Dpackage_root=${testPackageUri.toFilePath()}', '-Dtarget_os=${OSImpl.current}', - '-Dversion=${BuildConfigImpl.version}', + '-Dversion=${BuildConfigImpl.latestVersion}', '-Dlink_mode_preference=dynamic', '-Ddry_run=$dryRun', if (!dryRun) ...[ diff --git a/pkgs/native_assets_cli/test/example/native_add_library_test.dart b/pkgs/native_assets_cli/test/example/native_add_library_test.dart index 644b169c5..0de664ef9 100644 --- a/pkgs/native_assets_cli/test/example/native_add_library_test.dart +++ b/pkgs/native_assets_cli/test/example/native_add_library_test.dart @@ -43,7 +43,7 @@ void main() async { '-Dpackage_name=$name', '-Dpackage_root=${testPackageUri.toFilePath()}', '-Dtarget_os=${OSImpl.current}', - '-Dversion=${BuildConfigImpl.version}', + '-Dversion=${BuildConfigImpl.latestVersion}', '-Dlink_mode_preference=dynamic', '-Ddry_run=$dryRun', if (!dryRun) ...[ diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 9ca944018..d42a545a2 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -5,6 +5,7 @@ import 'package:collection/collection.dart'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; import 'package:native_assets_cli/src/api/asset.dart'; +import 'package:native_assets_cli/src/utils/yaml.dart'; import 'package:test/test.dart'; void main() { @@ -143,7 +144,9 @@ void main() { type: c_code'''; test('asset yaml', () { - final yaml = assets.toYamlString(); + final yaml = yamlEncode([ + for (final item in assets) item.toYaml(BuildOutputImpl.latestVersion) + ]); expect(yaml, assetsYamlEncoding); final assets2 = CCodeAssetImpl.listFromYamlString(yaml); expect(assets, assets2); diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index 5fdc7af32..e19c15c28 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -115,7 +115,7 @@ void main() async { 'target_android_ndk_api': 30, 'target_architecture': 'arm64', 'target_os': 'android', - 'version': BuildOutputImpl.version.toString(), + 'version': BuildOutputImpl.latestVersion.toString(), }); final fromConfig = BuildConfigImpl.fromConfig(config); @@ -138,7 +138,7 @@ void main() async { 'package_name': packageName, 'package_root': packageRootUri.toFilePath(), 'target_os': 'android', - 'version': BuildOutputImpl.version.toString(), + 'version': BuildOutputImpl.latestVersion.toString(), }); final fromConfig = BuildConfigImpl.fromConfig(config); @@ -260,7 +260,7 @@ supported_asset_types: target_architecture: arm64 target_ios_sdk: iphoneos target_os: ios -version: ${BuildConfigImpl.version}'''; +version: ${BuildConfigImpl.latestVersion}'''; expect(yamlString, equals(expectedYamlString)); final buildConfig2 = BuildConfigImpl.fromConfig( @@ -339,7 +339,7 @@ version: 1.0.0'''; ); expect( () => BuildConfigImpl.fromConfig(Config(fileParsed: { - 'version': BuildConfigImpl.version.toString(), + 'version': BuildConfigImpl.latestVersion.toString(), 'package_name': packageName, 'package_root': packageRootUri.toFilePath(), 'target_architecture': 'arm64', @@ -357,7 +357,7 @@ version: 1.0.0'''; ); expect( () => BuildConfigImpl.fromConfig(Config(fileParsed: { - 'version': BuildConfigImpl.version.toString(), + 'version': BuildConfigImpl.latestVersion.toString(), 'out_dir': outDirUri.toFilePath(), 'package_name': packageName, 'package_root': packageRootUri.toFilePath(), @@ -382,7 +382,7 @@ version: 1.0.0'''; expect( () => BuildConfigImpl.fromConfig(Config(fileParsed: { 'out_dir': outDirUri.toFilePath(), - 'version': BuildConfigImpl.version.toString(), + 'version': BuildConfigImpl.latestVersion.toString(), 'package_name': packageName, 'package_root': packageRootUri.toFilePath(), 'target_architecture': 'arm64', @@ -519,7 +519,7 @@ version: 1.0.0'''; (e) => e is FormatException && e.message.contains(version) && - e.message.contains(BuildConfigImpl.version.toString()), + e.message.contains(BuildConfigImpl.latestVersion.toString()), )), ); }); @@ -584,7 +584,7 @@ version: 1.0.0'''; 'target_os': 'windows', 'target_architecture': 'arm', 'build_mode': 'debug', - 'version': BuildConfigImpl.version.toString(), + 'version': BuildConfigImpl.latestVersion.toString(), }); expect( () => BuildConfigImpl.fromConfig(config), @@ -605,7 +605,7 @@ version: 1.0.0'''; 'target_architecture': 'arm64', 'build_mode': 'debug', 'dry_run': true, - 'version': BuildConfigImpl.version.toString(), + 'version': BuildConfigImpl.latestVersion.toString(), }); expect( () => BuildConfigImpl.fromConfig(config), @@ -625,7 +625,7 @@ version: 1.0.0'''; 'package_root': tempUri.toFilePath(), 'target_os': 'windows', 'dry_run': true, - 'version': BuildConfigImpl.version.toString(), + 'version': BuildConfigImpl.latestVersion.toString(), }); final buildConfig = BuildConfigImpl.fromConfig(config); expect( diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 5255d0695..a5406058c 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -5,6 +5,8 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; +import 'package:native_assets_cli/src/utils/yaml.dart'; +import 'package:pub_semver/pub_semver.dart'; import 'package:test/test.dart'; void main() { @@ -87,22 +89,29 @@ dependencies: - path/to/file.ext metadata: key: value -version: ${BuildOutputImpl.version}'''; +version: ${BuildOutputImpl.latestVersion}'''; test('built info yaml', () { - final yaml = buildOutput.toYamlString().replaceAll('\\', '/'); + final yaml = buildOutput + .toYamlString(BuildOutputImpl.latestVersion) + .replaceAll('\\', '/'); expect(yaml, yamlEncoding); final buildOutput2 = BuildOutputImpl.fromYamlString(yaml); expect(buildOutput.hashCode, buildOutput2.hashCode); expect(buildOutput, buildOutput2); }); - test('built info yaml v1.0.0 keeps working', () { + test('built info yaml v1.0.0 parsing keeps working', () { final buildOutput2 = BuildOutputImpl.fromYamlString(yamlEncodingV1_0_0); expect(buildOutput.hashCode, buildOutput2.hashCode); expect(buildOutput, buildOutput2); }); + test('built info yaml v1.0.0 serialization keeps working', () { + final yamlEncoding = yamlEncode(buildOutput.toYaml(Version(1, 0, 0))); + expect(yamlEncoding, yamlEncodingV1_0_0); + }); + test('BuildOutput.toString', buildOutput.toString); test('BuildOutput.hashCode', () { @@ -117,7 +126,19 @@ version: ${BuildOutputImpl.version}'''; test('BuildOutput.readFromFile BuildOutput.writeToFile', () async { final outDir = tempUri.resolve('out_dir/'); - await buildOutput.writeToFile(outDir: outDir); + final packageRoot = tempUri.resolve('package_root/'); + await Directory.fromUri(outDir).create(); + await Directory.fromUri(packageRoot).create(); + final config = BuildConfigImpl( + outDir: outDir, + packageName: 'dontcare', + packageRoot: packageRoot, + buildMode: BuildModeImpl.debug, + targetArchitecture: ArchitectureImpl.arm64, + targetOs: OSImpl.macOS, + linkModePreference: LinkModePreferenceImpl.dynamic, + ); + await buildOutput.writeToFile(config: config); final buildOutput2 = await BuildOutputImpl.readFromFile(outDir: outDir); expect(buildOutput2, buildOutput); }); @@ -137,7 +158,7 @@ version: ${BuildOutputImpl.version}'''; (e) => e is FormatException && e.message.contains(version) && - e.message.contains(BuildOutputImpl.version.toString()), + e.message.contains(BuildOutputImpl.latestVersion.toString()), )), ); }); @@ -157,7 +178,7 @@ assets: dependencies: [] metadata: key: value -version: ${BuildOutputImpl.version}'''), +version: ${BuildOutputImpl.latestVersion}'''), throwsFormatException, ); expect( @@ -173,7 +194,7 @@ dependencies: 1: foo metadata: key: value -version: ${BuildOutputImpl.version}'''), +version: ${BuildOutputImpl.latestVersion}'''), throwsFormatException, ); expect( @@ -188,7 +209,7 @@ assets: dependencies: [] metadata: 123: value -version: ${BuildOutputImpl.version}'''), +version: ${BuildOutputImpl.latestVersion}'''), throwsFormatException, ); }); From 1f1509a84d75850e76468d4b2fd08828f1712cdc Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 20 Feb 2024 09:27:44 +0100 Subject: [PATCH 25/75] bump changelog --- pkgs/native_assets_builder/CHANGELOG.md | 4 ++-- pkgs/native_assets_builder/pubspec.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/native_assets_builder/CHANGELOG.md b/pkgs/native_assets_builder/CHANGELOG.md index c68610946..343e554f3 100644 --- a/pkgs/native_assets_builder/CHANGELOG.md +++ b/pkgs/native_assets_builder/CHANGELOG.md @@ -1,6 +1,6 @@ -## 0.5.1-wip +## 0.6.0-wip -- Bump `package:native_assets_cli` to path dependency. +- **Breaking change**: New API. ## 0.5.0 diff --git a/pkgs/native_assets_builder/pubspec.yaml b/pkgs/native_assets_builder/pubspec.yaml index c65f56cee..aa134edd8 100644 --- a/pkgs/native_assets_builder/pubspec.yaml +++ b/pkgs/native_assets_builder/pubspec.yaml @@ -1,7 +1,7 @@ name: native_assets_builder description: >- This package is the backend that invokes top-level `build.dart` scripts. -version: 0.5.1-wip +version: 0.6.0-wip repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_builder environment: From dcd2165b33749b85135107b5dad8349ed4b71438 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 20 Feb 2024 09:29:08 +0100 Subject: [PATCH 26/75] Bump SDK constraint to use field promotion --- pkgs/native_assets_builder/pubspec.yaml | 2 +- .../test_data/cyclic_package_1/pubspec.yaml | 2 +- .../test_data/cyclic_package_2/pubspec.yaml | 2 +- .../test_data/dart_app/pubspec.yaml | 2 +- .../test_data/native_add/pubspec.yaml | 2 +- .../test_data/native_add_add_source/pubspec.yaml | 2 +- .../test_data/native_subtract/pubspec.yaml | 2 +- .../package_reading_metadata/pubspec.yaml | 2 +- .../test_data/package_with_metadata/pubspec.yaml | 2 +- .../test_data/some_dev_dep/pubspec.yaml | 2 +- .../test_data/wrong_build_output/pubspec.yaml | 2 +- .../test_data/wrong_build_output_2/pubspec.yaml | 2 +- .../test_data/wrong_build_output_3/pubspec.yaml | 2 +- .../test_data/wrong_namespace_asset/pubspec.yaml | 2 +- .../example/local_asset/pubspec.yaml | 2 +- .../example/native_add_app/pubspec.yaml | 2 +- .../example/native_add_library/pubspec.yaml | 2 +- .../example/use_dart_api/pubspec.yaml | 2 +- .../lib/src/model/build_config.dart | 16 ++++++++-------- pkgs/native_assets_cli/pubspec.yaml | 2 +- 20 files changed, 27 insertions(+), 27 deletions(-) diff --git a/pkgs/native_assets_builder/pubspec.yaml b/pkgs/native_assets_builder/pubspec.yaml index aa134edd8..bed088c25 100644 --- a/pkgs/native_assets_builder/pubspec.yaml +++ b/pkgs/native_assets_builder/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.6.0-wip repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_builder environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' publish_to: none diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml b/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml index 87f9dfdf3..a57866f39 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml b/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml index 9b49072e4..e4c64eed4 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/dart_app/pubspec.yaml b/pkgs/native_assets_builder/test_data/dart_app/pubspec.yaml index 1ead5cb85..b1a44c2a3 100644 --- a/pkgs/native_assets_builder/test_data/dart_app/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/dart_app/pubspec.yaml @@ -3,7 +3,7 @@ name: dart_app publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: native_add: diff --git a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml index 46941ace5..1c83e3978 100644 --- a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml index 831bb0017..fb456ebf0 100644 --- a/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml index 8ad7b6d8a..1f2d7f1dd 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml b/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml index 81c554892..0f826078c 100644 --- a/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml b/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml index 84a7437dc..44baf66ef 100644 --- a/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/some_dev_dep/pubspec.yaml b/pkgs/native_assets_builder/test_data/some_dev_dep/pubspec.yaml index 4d22d95cd..f3fe7dc84 100644 --- a/pkgs/native_assets_builder/test_data/some_dev_dep/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/some_dev_dep/pubspec.yaml @@ -5,4 +5,4 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml index 1aa7e38ee..34ee56f1a 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml index 82cc12d17..04cb18c78 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml index 630439123..762410904 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml index b422479d1..44e052ab0 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_cli/example/local_asset/pubspec.yaml b/pkgs/native_assets_cli/example/local_asset/pubspec.yaml index 06d2d89b9..626ce2376 100644 --- a/pkgs/native_assets_cli/example/local_asset/pubspec.yaml +++ b/pkgs/native_assets_cli/example/local_asset/pubspec.yaml @@ -6,7 +6,7 @@ version: 0.1.0 repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli/example/native_add_library environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_cli/example/native_add_app/pubspec.yaml b/pkgs/native_assets_cli/example/native_add_app/pubspec.yaml index 68f3d8e64..f6b2adc52 100644 --- a/pkgs/native_assets_cli/example/native_add_app/pubspec.yaml +++ b/pkgs/native_assets_cli/example/native_add_app/pubspec.yaml @@ -6,7 +6,7 @@ version: 0.1.0 repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli/example/native_add_app environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: native_add_library: diff --git a/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml b/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml index cbf53ffc2..b87654a41 100644 --- a/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml +++ b/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml @@ -6,7 +6,7 @@ version: 0.1.0 repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli/example/native_add_library environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml b/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml index 31c94cb6c..33bee4050 100644 --- a/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml +++ b/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index b75c0433f..e7e6be469 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -509,12 +509,12 @@ final class BuildConfigImpl implements BuildConfig { if (_targetIOSSdk != null) IOSSdkImpl.configKey: _targetIOSSdk.toString(), if (_targetAndroidNdkApi != null) - targetAndroidNdkApiConfigKey: _targetAndroidNdkApi!, + targetAndroidNdkApiConfigKey: _targetAndroidNdkApi, if (cCompilerYaml.isNotEmpty) CCompilerConfigImpl.configKey: cCompilerYaml, - if (_dependencyMetadata != null && _dependencyMetadata!.isNotEmpty) + if (_dependencyMetadata != null && _dependencyMetadata.isNotEmpty) dependencyMetadataConfigKey: { - for (final entry in _dependencyMetadata!.entries) + for (final entry in _dependencyMetadata.entries) entry.key: entry.value.toYaml(), }, }, @@ -646,11 +646,11 @@ class CCompilerConfigImpl implements CCompilerConfig { '$configKey.$envScriptArgsConfigKey'; Map toYaml() => { - if (_ar != null) arConfigKey: _ar!.toFilePath(), - if (_cc != null) ccConfigKey: _cc!.toFilePath(), - if (_ld != null) ldConfigKey: _ld!.toFilePath(), - if (_envScript != null) envScriptConfigKey: _envScript!.toFilePath(), - if (_envScriptArgs != null) envScriptArgsConfigKey: _envScriptArgs!, + if (_ar != null) arConfigKey: _ar.toFilePath(), + if (_cc != null) ccConfigKey: _cc.toFilePath(), + if (_ld != null) ldConfigKey: _ld.toFilePath(), + if (_envScript != null) envScriptConfigKey: _envScript.toFilePath(), + if (_envScriptArgs != null) envScriptArgsConfigKey: _envScriptArgs, }.sortOnKey(); @override diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index 10226fa16..84c0c00d5 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -13,7 +13,7 @@ topics: - native-assets environment: - sdk: '>=3.0.0 <4.0.0' + sdk: '>=3.3.0 <4.0.0' dependencies: cli_config: ^0.1.1 From 752dee9469f7c12ba9a17edbcd649e66f2aa62bf Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 20 Feb 2024 09:38:58 +0100 Subject: [PATCH 27/75] Fix test on Windows --- pkgs/native_assets_cli/test/model/build_output_test.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index a5406058c..530d89da1 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -108,7 +108,8 @@ version: ${BuildOutputImpl.latestVersion}'''; }); test('built info yaml v1.0.0 serialization keeps working', () { - final yamlEncoding = yamlEncode(buildOutput.toYaml(Version(1, 0, 0))); + final yamlEncoding = + yamlEncode(buildOutput.toYaml(Version(1, 0, 0))).replaceAll('\\', '/'); expect(yamlEncoding, yamlEncodingV1_0_0); }); From ba4a81fd26eacd47a5428ea81322dbf9bf4811c1 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 20 Feb 2024 10:08:02 +0100 Subject: [PATCH 28/75] Make `BuildConfig.targetArchitecture` nullable instead of throw --- .../example/local_asset/build.dart | 2 +- pkgs/native_assets_cli/lib/src/api/build.dart | 33 ++++++++++--------- .../lib/src/api/build_config.dart | 4 +-- .../lib/src/helpers/asset_downloader.dart | 4 +-- .../lib/src/model/build_config.dart | 13 +++++--- .../test/model/build_config_test.dart | 13 +++----- 6 files changed, 34 insertions(+), 35 deletions(-) diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index d26757354..146516f90 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -37,7 +37,7 @@ void main(List args) async { file: assetPath, linkMode: LinkMode.dynamic, os: config.targetOs, - architecture: config.dryRun ? null : config.targetArchitecture, + architecture: config.targetArchitecture, dynamicLoading: BundledDylib(), ) ]); diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 7bd1925ee..c1f5910b6 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -10,6 +10,10 @@ import 'build_output.dart'; /// Example using `package:native_toolchain_c`: /// /// ```dart +/// 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 args) async { /// await build(args, (config, output) async { /// final packageName = config.packageName; @@ -34,6 +38,10 @@ import 'build_output.dart'; /// Example outputting assets manually: /// /// ```dart +/// import 'dart:io'; +/// +/// import 'package:native_assets_cli/native_assets_cli.dart'; +/// /// const assetName = 'asset.txt'; /// final packageAssetPath = Uri.file('data/$assetName'); /// @@ -46,18 +54,10 @@ import 'build_output.dart'; /// ); /// } /// -/// final Iterable targets; /// final packageName = config.packageName; /// final assetPath = config.outDir.resolve(assetName); /// final assetSourcePath = config.packageRoot.resolveUri(packageAssetPath); -/// if (config.dryRun) { -/// // Dry run invocations report assets for all architectures for that OS. -/// targets = Target.values.where( -/// (element) => element.os == config.targetOs, -/// ); -/// } else { -/// targets = [config.target]; -/// +/// if (!config.dryRun) { /// // Insert code that downloads or builds the asset to `assetPath`. /// await File.fromUri(assetSourcePath).copy(assetPath.toFilePath()); /// @@ -68,13 +68,14 @@ import 'build_output.dart'; /// } /// /// output.addAssets([ -/// for (final target in targets) -/// Asset( -/// id: 'library:$packageName/asset.txt', -/// linkMode: LinkMode.dynamic, -/// target: target, -/// path: AssetAbsolutePath(assetPath), -/// ) +/// CCodeAsset( +/// id: 'library:$packageName/asset.txt', +/// file: assetPath, +/// linkMode: LinkMode.dynamic, +/// os: config.targetOs, +/// architecture: config.targetArchitecture, +/// dynamicLoading: BundledDylib(), +/// ) /// ]); /// }); /// } diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index f2d8d11c4..a87e1091f 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -43,8 +43,8 @@ abstract final class BuildConfig { /// The architecture being compiled for. /// - /// Not available during a [dryRun]. - Architecture get targetArchitecture; + /// Not specified (`null`) during a [dryRun]. + Architecture? get targetArchitecture; /// The operating system being compiled for. OS get targetOs; diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart index 23e120158..c641be1d2 100644 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -45,7 +45,7 @@ class AssetDownloader implements Builder { if (!config.dryRun) { final downloadUri2 = downloadUri( config.targetOs, - config.targetArchitecture, + config.targetArchitecture!, ); final fileName = downloadUri2.pathSegments.lastWhere((element) => element.isNotEmpty); @@ -62,7 +62,7 @@ class AssetDownloader implements Builder { linkMode: LinkMode.dynamic, dynamicLoading: BundledDylib(), os: config.targetOs, - architecture: config.dryRun ? null : config.targetArchitecture, + architecture: config.targetArchitecture, ) ]); } diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index e7e6be469..7e7b16dcd 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -28,12 +28,9 @@ final class BuildConfigImpl implements BuildConfig { /// /// Not available during a [dryRun]. @override - ArchitectureImpl get targetArchitecture { - _ensureNotDryRun(); - return _targetArchitecture; - } + ArchitectureImpl? get targetArchitecture => _targetArchitecture; - late final ArchitectureImpl _targetArchitecture; + late final ArchitectureImpl? _targetArchitecture; /// The operating system being compiled for. @override @@ -139,6 +136,7 @@ final class BuildConfigImpl implements BuildConfig { Iterable? supportedAssetTypes, }) { final nonValidated = BuildConfigImpl._() + .._version = latestVersion .._outDir = outDir .._packageName = packageName .._packageRoot = packageRoot @@ -170,10 +168,12 @@ final class BuildConfigImpl implements BuildConfig { Iterable? supportedAssetTypes, }) { final nonValidated = BuildConfigImpl._() + .._version = latestVersion .._outDir = outDir .._packageName = packageName .._packageRoot = packageRoot .._targetOs = targetOs + .._targetArchitecture = null .._linkModePreference = linkModePreference .._cCompiler = CCompilerConfigImpl() .._dryRun = true @@ -206,8 +206,10 @@ final class BuildConfigImpl implements BuildConfig { required LinkModePreferenceImpl linkModePreference, Map? dependencyMetadata, Iterable? supportedAssetTypes, + Version? version, }) { final input = [ + version ?? latestVersion, packageName, targetArchitecture.toString(), targetOs.toString(), @@ -353,6 +355,7 @@ final class BuildConfigImpl implements BuildConfig { (config) { if (dryRun) { _throwIfNotNullInDryRun(ArchitectureImpl.configKey); + _targetArchitecture = null; } else { final validArchitectures = [ if (!osSet) diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index e19c15c28..8af79b784 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -541,7 +541,7 @@ version: 1.0.0'''; ); // Using the checksum for a build folder should be stable. - expect(name1, '037109b9824b2559502fa7bd42e1b6f8'); + expect(name1, 'e8da644fa538c7b63e61feb102974b93'); // Build folder different due to metadata. final name2 = BuildConfigImpl.checksum( @@ -616,7 +616,7 @@ version: 1.0.0'''; ); }); - test('BuildConfig dry_run access invalid args', () { + test('BuildConfig dry_run target arch', () { final outDir = outDirUri; final config = Config(fileParsed: { 'link_mode_preference': 'prefer-static', @@ -628,15 +628,10 @@ version: 1.0.0'''; 'version': BuildConfigImpl.latestVersion.toString(), }); final buildConfig = BuildConfigImpl.fromConfig(config); - expect( - () => buildConfig.targetArchitecture, - throwsA(predicate( - (e) => e is StateError && e.message.contains('In Flutter projects'), - )), - ); + expect(buildConfig.targetArchitecture, isNull); }); - test('BuildConfig dry_run access invalid args', () { + test('BuildConfig dry_run toString', () { final buildConfig = BuildConfigImpl.dryRun( packageName: packageName, outDir: outDirUri, From 7a0c6683cb732bb9c07e93cabc751e84c0a8b291 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 20 Feb 2024 11:02:15 +0100 Subject: [PATCH 29/75] Increase test coverage --- .../lib/src/api/build_config.dart | 4 + .../lib/src/model/build_config.dart | 24 ++-- .../lib/src/model/build_output.dart | 4 +- .../lib/src/model/c_code_asset.dart | 5 - pkgs/native_assets_cli/pubspec.yaml | 1 + .../test/api/build_config_test.dart | 6 + .../test/api/build_test.dart | 71 ++++++++++++ .../test/model/build_config_test.dart | 23 +++- .../test/model/build_output_test.dart | 107 ++++++++++++++++++ .../test/model/target_test.dart | 10 ++ 10 files changed, 235 insertions(+), 20 deletions(-) create mode 100644 pkgs/native_assets_cli/test/api/build_test.dart diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index a87e1091f..dc2900ef6 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -127,6 +127,7 @@ abstract final class BuildConfig { CCompilerConfig? cCompiler, required LinkModePreference linkModePreference, Map>? dependencyMetadata, + Iterable? supportedAssetTypes, }) => BuildConfigImpl( outDir: outDir, @@ -145,6 +146,7 @@ abstract final class BuildConfig { entry.key: Metadata(entry.value.cast()) } : {}, + supportedAssetTypes: supportedAssetTypes, ); factory BuildConfig.dryRun({ @@ -153,6 +155,7 @@ abstract final class BuildConfig { required Uri packageRoot, required OS targetOs, required LinkModePreference linkModePreference, + Iterable? supportedAssetTypes, }) => BuildConfigImpl.dryRun( outDir: outDir, @@ -160,6 +163,7 @@ abstract final class BuildConfig { packageRoot: packageRoot, targetOs: targetOs as OSImpl, linkModePreference: linkModePreference as LinkModePreferenceImpl, + supportedAssetTypes: supportedAssetTypes, ); factory BuildConfig.fromConfig(Config config) => diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 7e7b16dcd..ec05c3896 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -149,11 +149,8 @@ final class BuildConfigImpl implements BuildConfig { .._linkModePreference = linkModePreference .._dependencyMetadata = dependencyMetadata .._dryRun = false - .._supportedAssetTypes = [ - ...?supportedAssetTypes, - // Backwards compatibility. - if (supportedAssetTypes == null) CCodeAsset.type, - ]; + .._supportedAssetTypes = + _supportedAssetTypesBackwardsCompatibility(supportedAssetTypes); final parsedConfigFile = nonValidated.toYaml(); final config = Config(fileParsed: parsedConfigFile); return BuildConfigImpl.fromConfig(config); @@ -177,11 +174,8 @@ final class BuildConfigImpl implements BuildConfig { .._linkModePreference = linkModePreference .._cCompiler = CCompilerConfigImpl() .._dryRun = true - .._supportedAssetTypes = [ - ...?supportedAssetTypes, - // Backwards compatibility. - if (supportedAssetTypes == null) CCodeAsset.type, - ]; + .._supportedAssetTypes = + _supportedAssetTypesBackwardsCompatibility(supportedAssetTypes); final parsedConfigFile = nonValidated.toYaml(); final config = Config(fileParsed: parsedConfigFile); return BuildConfigImpl.fromConfig(config); @@ -227,7 +221,7 @@ final class BuildConfigImpl implements BuildConfig { entry.key, json.encode(entry.value.toYaml()), ], - ...?supportedAssetTypes, + ..._supportedAssetTypesBackwardsCompatibility(supportedAssetTypes), ].join('###'); final sha256String = sha256.convert(utf8.encode(input)).toString(); // 256 bit hashes lead to 64 hex character strings. @@ -237,6 +231,14 @@ final class BuildConfigImpl implements BuildConfig { return sha256String.substring(0, nameLength); } + static List _supportedAssetTypesBackwardsCompatibility( + Iterable? supportedAssetTypes, + ) => + [ + ...?supportedAssetTypes, + if (supportedAssetTypes == null) CCodeAsset.type, + ]; + BuildConfigImpl._(); /// The version of [BuildConfigImpl]. diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index 7a2781d88..bbed6d84f 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -79,9 +79,7 @@ class BuildOutputImpl implements BuildOutput { case null: // Backwards compatibility with v1.0.0. assets.add(CCodeAssetImpl.fromYaml(yamlMap)); default: - throw FormatException( - "Unexpected asset type '$type' in YAML.", - ); + // Do nothing, some other launcher might define it's own asset types. } } diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index f1d67fef5..832df5f5f 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -204,11 +204,6 @@ final class CCodeAssetImpl implements CCodeAsset { ]; } - static List listFromYamlList(YamlList yamlList) => [ - for (final yamlElement in yamlList) - CCodeAssetImpl.fromYaml(as(yamlElement)), - ]; - CCodeAssetImpl copyWith({ LinkModeImpl? linkMode, String? id, diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index 84c0c00d5..399757824 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -26,4 +26,5 @@ dependencies: dev_dependencies: dart_flutter_team_lints: ^2.1.1 + file_testing: ^3.0.0 test: ^1.21.0 diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index c9b8d66a6..71dc4cc71 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -61,6 +61,7 @@ void main() async { ), buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, + supportedAssetTypes: [CCodeAsset.type], ); final config2 = BuildConfig( @@ -72,6 +73,7 @@ void main() async { targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, + supportedAssetTypes: [CCodeAsset.type], ); expect(config1, equals(config1)); @@ -89,6 +91,7 @@ void main() async { true); expect(config1.cCompiler != config2.cCompiler, true); expect(config1.linkModePreference, config2.linkModePreference); + expect(config1.supportedAssetTypes, config2.supportedAssetTypes); }); test('BuildConfig fromConfig', () { @@ -127,6 +130,7 @@ void main() async { packageRoot: packageRootUri, targetOs: OS.android, linkModePreference: LinkModePreference.preferStatic, + supportedAssetTypes: [CCodeAsset.type], ); final config = Config(fileParsed: { @@ -186,6 +190,8 @@ void main() async { expect(buildConfig1, equals(buildConfig1)); expect(buildConfig1 == buildConfig2, false); expect(buildConfig1.hashCode == buildConfig2.hashCode, false); + + expect(buildConfig1.metadatum('bar', 'key'), 'value'); }); test('BuildConfig fromArgs', () async { diff --git a/pkgs/native_assets_cli/test/api/build_test.dart b/pkgs/native_assets_cli/test/api/build_test.dart new file mode 100644 index 000000000..448e86169 --- /dev/null +++ b/pkgs/native_assets_cli/test/api/build_test.dart @@ -0,0 +1,71 @@ +// 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 'dart:io'; + +import 'package:file_testing/file_testing.dart'; +import 'package:native_assets_cli/native_assets_cli.dart'; +import 'package:native_assets_cli/src/api/build_config.dart'; +import 'package:native_assets_cli/src/api/build_output.dart'; +import 'package:test/test.dart'; + +void main() async { + late Uri tempUri; + late Uri outDirUri; + late String packageName; + late Uri packageRootUri; + late Uri fakeClang; + late Uri fakeLd; + late Uri fakeAr; + late Uri fakeCl; + late Uri fakeVcVars; + late Uri buildConfigUri; + + setUp(() async { + tempUri = (await Directory.systemTemp.createTemp()).uri; + outDirUri = tempUri.resolve('out1/'); + await Directory.fromUri(outDirUri).create(); + packageName = 'my_package'; + packageRootUri = tempUri.resolve('$packageName/'); + await Directory.fromUri(packageRootUri).create(); + fakeClang = tempUri.resolve('fake_clang'); + await File.fromUri(fakeClang).create(); + fakeLd = tempUri.resolve('fake_ld'); + await File.fromUri(fakeLd).create(); + fakeAr = tempUri.resolve('fake_ar'); + await File.fromUri(fakeAr).create(); + fakeCl = tempUri.resolve('cl.exe'); + await File.fromUri(fakeCl).create(); + fakeVcVars = tempUri.resolve('vcvarsall.bat'); + await File.fromUri(fakeVcVars).create(); + + final config1 = BuildConfig( + outDir: outDirUri, + packageName: packageName, + packageRoot: tempUri, + targetArchitecture: Architecture.arm64, + targetOs: OS.iOS, + targetIOSSdk: IOSSdk.iPhoneOs, + cCompiler: CCompilerConfig( + cc: fakeClang, + ld: fakeLd, + ar: fakeAr, + ), + buildMode: BuildMode.release, + linkModePreference: LinkModePreference.preferDynamic, + ); + final configYaml = (config1 as BuildConfigImpl).toYamlString(); + buildConfigUri = tempUri.resolve('build_config.yaml'); + await File.fromUri(buildConfigUri).writeAsString(configYaml); + }); + + test('build method', () async { + await build(['--config', buildConfigUri.toFilePath()], + (config, output) async { + output.addDependency(packageRootUri.resolve('foo')); + }); + final buildOutputUri = outDirUri.resolve(BuildOutputImpl.fileName); + expect(File.fromUri(buildOutputUri), exists); + }); +} diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index 8af79b784..fd47f567d 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -538,10 +538,11 @@ version: 1.0.0'''; targetOs: OSImpl.linux, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.dynamic, + supportedAssetTypes: [CCodeAsset.type], ); // Using the checksum for a build folder should be stable. - expect(name1, 'e8da644fa538c7b63e61feb102974b93'); + expect(name1, '4dec7292cfab417c27529307d949773f'); // Build folder different due to metadata. final name2 = BuildConfigImpl.checksum( @@ -616,6 +617,26 @@ version: 1.0.0'''; ); }); + test('BuildConfig dry_run access invalid args', () { + final outDir = outDirUri; + final config = Config(fileParsed: { + 'link_mode_preference': 'prefer-static', + 'out_dir': outDir.toFilePath(), + 'package_name': packageName, + 'package_root': tempUri.toFilePath(), + 'target_os': 'android', + 'dry_run': true, + 'version': BuildConfigImpl.latestVersion.toString(), + }); + final buildConfig = BuildConfigImpl.fromConfig(config); + expect( + () => buildConfig.targetAndroidNdkApi, + throwsA(predicate( + (e) => e is StateError && e.message.contains('In Flutter projects'), + )), + ); + }); + test('BuildConfig dry_run target arch', () { final outDir = outDirUri; final config = Config(fileParsed: { diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 530d89da1..dc60ddcc7 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -38,6 +38,20 @@ void main() { architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), + CCodeAssetImpl( + id: 'foo3', + dynamicLoading: LookupInProcessImpl(), + os: OSImpl.android, + architecture: ArchitectureImpl.x64, + linkMode: LinkModeImpl.dynamic, + ), + CCodeAssetImpl( + id: 'foo4', + dynamicLoading: LookupInExecutableImpl(), + os: OSImpl.android, + architecture: ArchitectureImpl.x64, + linkMode: LinkModeImpl.dynamic, + ), ], dependencies: Dependencies([ Uri.file('path/to/file.ext'), @@ -61,6 +75,16 @@ assets: path_type: system uri: path/to/libfoo2.so target: android_x64 + - id: foo3 + link_mode: dynamic + path: + path_type: process + target: android_x64 + - id: foo4 + link_mode: dynamic + path: + path_type: executable + target: android_x64 dependencies: - path/to/file.ext metadata: @@ -85,6 +109,20 @@ assets: link_mode: dynamic os: android type: c_code + - architecture: x64 + dynamic_loading: + type: process + id: foo3 + link_mode: dynamic + os: android + type: c_code + - architecture: x64 + dynamic_loading: + type: executable + id: foo4 + link_mode: dynamic + os: android + type: c_code dependencies: - path/to/file.ext metadata: @@ -224,4 +262,73 @@ version: ${BuildOutputImpl.latestVersion}'''), returnsNormally, ); }); + + test('BuildOutput setters', () { + final buildOutput = BuildOutputImpl( + timestamp: DateTime.parse('2022-11-10 13:25:01.000'), + assets: [ + CCodeAssetImpl( + id: 'foo', + file: Uri(path: 'path/to/libfoo.so'), + dynamicLoading: BundledDylibImpl(), + os: OSImpl.android, + architecture: ArchitectureImpl.x64, + linkMode: LinkModeImpl.dynamic, + ), + CCodeAssetImpl( + id: 'foo2', + dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), + os: OSImpl.android, + architecture: ArchitectureImpl.x64, + linkMode: LinkModeImpl.dynamic, + ), + ], + dependencies: Dependencies([ + Uri.file('path/to/file.ext'), + Uri.file('path/to/file2.ext'), + ]), + metadata: const Metadata({ + 'key': 'value', + 'key2': 'value2', + }), + ); + + final buildOutput2 = BuildOutputImpl( + timestamp: DateTime.parse('2022-11-10 13:25:01.000'), + ); + buildOutput2.addAsset( + CCodeAssetImpl( + id: 'foo', + file: Uri(path: 'path/to/libfoo.so'), + dynamicLoading: BundledDylibImpl(), + os: OSImpl.android, + architecture: ArchitectureImpl.x64, + linkMode: LinkModeImpl.dynamic, + ), + ); + buildOutput2.addAssets([ + CCodeAssetImpl( + id: 'foo2', + dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), + os: OSImpl.android, + architecture: ArchitectureImpl.x64, + linkMode: LinkModeImpl.dynamic, + ), + ]); + buildOutput2.addDependency( + Uri.file('path/to/file.ext'), + ); + buildOutput2.addDependencies([ + Uri.file('path/to/file2.ext'), + ]); + buildOutput2.addMetadata({ + 'key': 'value', + }); + buildOutput2.addMetadatum('key2', 'value2'); + + expect(buildOutput2, equals(buildOutput)); + expect( + buildOutput2.dependenciesModel, equals(buildOutput.dependenciesModel)); + expect(buildOutput2.metadataModel, equals(buildOutput.metadataModel)); + }); } diff --git a/pkgs/native_assets_cli/test/model/target_test.dart b/pkgs/native_assets_cli/test/model/target_test.dart index 4672c4454..fc928ee45 100644 --- a/pkgs/native_assets_cli/test/model/target_test.dart +++ b/pkgs/native_assets_cli/test/model/target_test.dart @@ -75,4 +75,14 @@ void main() { )), ); }); + + test('OS.architectures', () { + expect(OSImpl.android.architectures, [ + ArchitectureImpl.arm, + ArchitectureImpl.arm64, + ArchitectureImpl.ia32, + ArchitectureImpl.x64, + ArchitectureImpl.riscv64, + ]); + }); } From e9a49a06ecdc131daba7e7b2ec8ab0a74c76f02f Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 20 Feb 2024 11:56:50 +0100 Subject: [PATCH 30/75] Add `DataAsset`s --- .../lib/src/build_runner/build_runner.dart | 44 ++++++++-------- .../build_runner_caching_test.dart | 12 +++-- .../build_runner_dry_run_test.dart | 10 ++-- .../build_runner_failure_test.dart | 7 ++- .../test/build_runner/helpers.dart | 2 +- .../packaging_preference_test.dart | 20 ++++++-- .../lib/native_assets_cli_internal.dart | 2 + pkgs/native_assets_cli/lib/src/api/asset.dart | 5 +- .../lib/src/api/build_output.dart | 10 ++-- .../lib/src/api/data_asset.dart | 23 +++++++++ .../lib/src/model/asset.dart | 27 ++++++++++ .../lib/src/model/build_output.dart | 30 ++++------- .../lib/src/model/build_output_CHANGELOG.md | 2 + .../lib/src/model/c_code_asset.dart | 14 +---- .../lib/src/model/data_asset.dart | 51 +++++++++++++++++++ pkgs/native_assets_cli/test/helpers.dart | 2 +- .../test/model/asset_test.dart | 47 ++++++++++++----- 17 files changed, 221 insertions(+), 87 deletions(-) create mode 100644 pkgs/native_assets_cli/lib/src/api/data_asset.dart create mode 100644 pkgs/native_assets_cli/lib/src/model/asset.dart create mode 100644 pkgs/native_assets_cli/lib/src/model/data_asset.dart diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 69d00aeb4..352e2427f 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -77,7 +77,7 @@ class NativeAssetsBuildRunner { buildPlan = plan; packageGraph = planner.packageGraph; } - final assets = []; + final assets = []; final dependencies = []; final metadata = {}; var success = true; @@ -163,7 +163,7 @@ class NativeAssetsBuildRunner { } buildPlan = plan; } - final assets = []; + final assets = []; var success = true; for (final package in buildPlan) { final config = await _cliConfigDryRun( @@ -181,18 +181,23 @@ class NativeAssetsBuildRunner { dryRun: true, ); for (final asset in packageAssets) { - if (asset.architecture != null) { - // Backwards compatibility, if an architecture is provided use that. - assets.add(asset); - } else { - // Dry run does not report architecture. Dart VM branches on OS and - // Target when looking up assets, so populate assets for all - // architectures. - for (final architecture in asset.os.architectures) { - assets.add(asset.copyWith( - architecture: architecture, - )); - } + switch (asset) { + case CCodeAssetImpl _: + if (asset.architecture != null) { + // Backwards compatibility, if an architecture is provided use it. + assets.add(asset); + } else { + // Dry run does not report architecture. Dart VM branches on OS + // and Target when looking up assets, so populate assets for all + // architectures. + for (final architecture in asset.os.architectures) { + assets.add(asset.copyWith( + architecture: architecture, + )); + } + } + case DataAssetImpl _: + assets.add(asset); } } success &= packageSuccess; @@ -415,8 +420,7 @@ build_output.yaml contained a format error. }; } - bool validateAssetsPackage( - Iterable assets, String packageName) { + bool validateAssetsPackage(Iterable assets, String packageName) { final invalidAssetIds = assets .map((a) => a.id) .where((n) => !n.startsWith('package:$packageName/')) @@ -435,7 +439,7 @@ build_output.yaml contained a format error. } typedef _PackageBuildRecord = ( - Iterable, + Iterable, Iterable dependencies, Metadata?, bool success, @@ -444,7 +448,7 @@ typedef _PackageBuildRecord = ( /// The result from a [NativeAssetsBuildRunner.dryRun]. abstract interface class DryRunResult { /// The native assets for all [TargetImpl]s for the build or dry run. - List get assets; + List get assets; /// Whether all builds completed without errors. /// @@ -454,7 +458,7 @@ abstract interface class DryRunResult { final class _DryRunResultImpl implements DryRunResult { @override - final List assets; + final List assets; @override final bool success; @@ -478,7 +482,7 @@ abstract class BuildResult implements DryRunResult { final class _BuildResultImpl implements BuildResult { @override - final List assets; + final List assets; @override final List dependencies; diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart index 2749e53a5..4e9d2b211 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart @@ -4,6 +4,7 @@ import 'dart:io'; +import 'package:native_assets_cli/native_assets_cli_internal.dart'; import 'package:test/test.dart'; import '../helpers.dart'; @@ -74,7 +75,8 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); - await expectSymbols(asset: result.assets.single, symbols: ['add']); + await expectSymbols( + asset: result.assets.single as CCodeAssetImpl, symbols: ['add']); } await copyTestProjects( @@ -85,7 +87,7 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); await expectSymbols( - asset: result.assets.single, + asset: result.assets.single as CCodeAssetImpl, symbols: ['add', 'subtract'], ); } @@ -101,7 +103,8 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); - await expectSymbols(asset: result.assets.single, symbols: ['add']); + await expectSymbols( + asset: result.assets.single as CCodeAssetImpl, symbols: ['add']); } await copyTestProjects( @@ -111,7 +114,8 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); await expectSymbols( - asset: result.assets.single, symbols: ['add', 'multiply']); + asset: result.assets.single as CCodeAssetImpl, + symbols: ['add', 'multiply']); } }); }); diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart index 6c4b0270d..caf719e75 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart @@ -41,14 +41,16 @@ void main() async { for (var i = 0; i < dryRunAssets.length; i++) { final dryRunAsset = dryRunAssets[i]; final buildAsset = result.assets[0]; - expect(dryRunAsset.linkMode, buildAsset.linkMode); expect(dryRunAsset.id, buildAsset.id); // The build runner expands CCodeAssets to all architectures. - expect(dryRunAsset.architecture, isNotNull); - expect(buildAsset.architecture, isNotNull); expect(buildAsset.file, isNotNull); expect(dryRunAsset.file, isNull); - expect(dryRunAsset.os, buildAsset.os); + if (dryRunAsset is CCodeAssetImpl && buildAsset is CCodeAssetImpl) { + expect(dryRunAsset.architecture, isNotNull); + expect(buildAsset.architecture, isNotNull); + expect(dryRunAsset.os, buildAsset.os); + expect(dryRunAsset.linkMode, buildAsset.linkMode); + } } final dryRunDir = packageUri.resolve( diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart index c844be2bf..53130e2a5 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart @@ -5,6 +5,7 @@ import 'dart:io'; import 'package:logging/logging.dart'; +import 'package:native_assets_cli/native_assets_cli_internal.dart'; import 'package:test/test.dart'; import '../helpers.dart'; @@ -26,7 +27,8 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); expect(result.assets.length, 1); - await expectSymbols(asset: result.assets.single, symbols: ['add']); + await expectSymbols( + asset: result.assets.single as CCodeAssetImpl, symbols: ['add']); expect( result.dependencies, [ @@ -69,7 +71,8 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); expect(result.assets.length, 1); - await expectSymbols(asset: result.assets.single, symbols: ['add']); + await expectSymbols( + asset: result.assets.single as CCodeAssetImpl, symbols: ['add']); expect( result.dependencies, [ diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index e9fefb917..8aeefdef5 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -61,7 +61,7 @@ Future build( runPackageName: runPackageName, ); if (result.success) { - await expectAssetsExist(result.assets); + await expectAssetsExist(result.assets.cast()); } if (subscription != null) { diff --git a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart index 461bf5fc8..375c8d126 100644 --- a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart @@ -51,10 +51,22 @@ void main() async { ); // This package honors preferences. - expect(resultDynamic.assets.single.linkMode, LinkModeImpl.dynamic); - expect(resultPreferDynamic.assets.single.linkMode, LinkModeImpl.dynamic); - expect(resultStatic.assets.single.linkMode, LinkModeImpl.static); - expect(resultPreferStatic.assets.single.linkMode, LinkModeImpl.static); + expect( + (resultDynamic.assets.single as CCodeAssetImpl).linkMode, + LinkModeImpl.dynamic, + ); + expect( + (resultPreferDynamic.assets.single as CCodeAssetImpl).linkMode, + LinkModeImpl.dynamic, + ); + expect( + (resultStatic.assets.single as CCodeAssetImpl).linkMode, + LinkModeImpl.static, + ); + expect( + (resultPreferStatic.assets.single as CCodeAssetImpl).linkMode, + LinkModeImpl.static, + ); }); }); } diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index 6c00be375..677bbe12e 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -10,8 +10,10 @@ library native_assets_cli_internal; export 'src/api/asset.dart' show + AssetImpl, BundledDylibImpl, CCodeAssetImpl, + DataAssetImpl, LookupInExecutableImpl, LookupInProcessImpl, SystemDylibImpl; diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index 5f7c4e091..320d9da41 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -13,9 +13,12 @@ import 'link_mode.dart'; import 'target.dart'; part 'c_code_asset.dart'; +part 'data_asset.dart'; +part '../model/asset.dart'; part '../model/c_code_asset.dart'; +part '../model/data_asset.dart'; -/// Data bundled with a Dart or Flutter application. +/// Data or code bundled with a Dart or Flutter application. /// /// An asset is data or code which is accessible from a Dart or Flutter /// application. To retrieve an asset at runtime, the [id] is used. This enables diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index c74423c9a..5ac372294 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -38,7 +38,7 @@ abstract class BuildOutput { /// /// In dry runs, the assets for all [Architecture]s for the [OS] specified in /// the dry run must be provided. - Iterable get assets; + Iterable get assets; /// The files used by this build. /// @@ -69,13 +69,13 @@ abstract class BuildOutput { /// via [metadata] or [addMetadata]. factory BuildOutput({ DateTime? timestamp, - Iterable? assets, + Iterable? assets, Iterable? dependencies, Map? metadata, }) => BuildOutputImpl( timestamp: timestamp, - assets: assets?.cast().toList(), + assets: assets?.cast().toList(), dependencies: Dependencies([...?dependencies]), metadata: Metadata({...?metadata}), ); @@ -84,13 +84,13 @@ abstract class BuildOutput { /// /// In dry runs, the assets for all [Architecture]s for the [OS] specified in /// the dry run must be provided. - void addAsset(CCodeAsset asset); + void addAsset(Asset asset); /// Adds [CCodeAsset]s produced by this build or dry run. /// /// In dry runs, the assets for all [Architecture]s for the [OS] specified in /// the dry run must be provided. - void addAssets(Iterable assets); + void addAssets(Iterable assets); /// Adds file used by this build. /// diff --git a/pkgs/native_assets_cli/lib/src/api/data_asset.dart b/pkgs/native_assets_cli/lib/src/api/data_asset.dart new file mode 100644 index 000000000..f5415111c --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/api/data_asset.dart @@ -0,0 +1,23 @@ +// 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. + +part of 'asset.dart'; + +/// Data bundled with a Dart or Flutter application. +/// +/// An data asset is data which is accessible from a Dart or Flutter +/// application. To retrieve an asset at runtime, the [id] is used. This enables +/// access to the asset irrespective of how and where the application is run. +abstract final class DataAsset implements Asset { + factory DataAsset({ + required String id, + required Uri file, + }) => + DataAssetImpl( + id: id, + file: file, + ); + + static const String type = 'data'; +} diff --git a/pkgs/native_assets_cli/lib/src/model/asset.dart b/pkgs/native_assets_cli/lib/src/model/asset.dart new file mode 100644 index 000000000..f38dba45c --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/model/asset.dart @@ -0,0 +1,27 @@ +// 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. + +part of '../api/asset.dart'; + +abstract final class AssetImpl implements Asset { + Map toYaml(Version version); + + static List listFromYamlList(YamlList list) { + final assets = []; + for (final yamlElement in list) { + final yamlMap = as(yamlElement); + final type = yamlMap[CCodeAssetImpl.typeKey]; + switch (type) { + case CCodeAsset.type: + case null: // Backwards compatibility with v1.0.0. + assets.add(CCodeAssetImpl.fromYaml(yamlMap)); + case DataAsset.type: + assets.add(DataAssetImpl.fromYaml(yamlMap)); + default: + // Do nothing, some other launcher might define it's own asset types. + } + } + return assets; + } +} diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index bbed6d84f..dcfaabda3 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -8,10 +8,10 @@ class BuildOutputImpl implements BuildOutput { @override final DateTime timestamp; - final List _assets; + final List _assets; @override - Iterable get assets => _assets; + Iterable get assets => _assets; final Dependencies _dependencies; @@ -24,7 +24,7 @@ class BuildOutputImpl implements BuildOutput { BuildOutputImpl({ DateTime? timestamp, - List? assets, + List? assets, Dependencies? dependencies, Metadata? metadata, }) : timestamp = (timestamp ?? DateTime.now()).roundDownToSeconds(), @@ -70,18 +70,8 @@ class BuildOutputImpl implements BuildOutput { ); } - final assets = []; - for (final yamlElement in as(yamlMap[_assetsKey])) { - final yamlMap = as(yamlElement); - final type = yamlMap[CCodeAssetImpl.typeKey]; - switch (type) { - case CCodeAsset.type: - case null: // Backwards compatibility with v1.0.0. - assets.add(CCodeAssetImpl.fromYaml(yamlMap)); - default: - // Do nothing, some other launcher might define it's own asset types. - } - } + final assets = + AssetImpl.listFromYamlList(as(yamlMap[_assetsKey])); return BuildOutputImpl( timestamp: DateTime.parse(as(yamlMap[_timestampKey])), @@ -151,7 +141,7 @@ class BuildOutputImpl implements BuildOutput { return false; } return other.timestamp == timestamp && - const ListEquality().equals(other._assets, _assets) && + const ListEquality().equals(other._assets, _assets) && other._dependencies == _dependencies && other._metadata == _metadata; } @@ -159,7 +149,7 @@ class BuildOutputImpl implements BuildOutput { @override int get hashCode => Object.hash( timestamp.hashCode, - const ListEquality().hash(_assets), + const ListEquality().hash(_assets), _dependencies, _metadata, ); @@ -177,12 +167,12 @@ class BuildOutputImpl implements BuildOutput { Metadata get metadataModel => _metadata; @override - void addAsset(CCodeAsset asset) { - _assets.add(asset as CCodeAssetImpl); + void addAsset(Asset asset) { + _assets.add(asset as AssetImpl); } @override - void addAssets(Iterable assets) { + void addAssets(Iterable assets) { _assets.addAll(assets.cast()); } } diff --git a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md index 1bb81c02f..01e558e36 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md +++ b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md @@ -9,6 +9,8 @@ Backwards compatibility: `path` is parsed as `dynamic_linking`. Backwards compatibility: the nested `path_type` is parsed as `type`. Backwards compatibility older SDKs: emit the old format if an older BuildConfig was passed in. +- Added `DataAsset`s. + Backwards compatibility: These are ignored on older SDKs. ## 1.0.0 diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index 832df5f5f..75a992433 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -121,7 +121,7 @@ final class LookupInExecutableImpl }; } -final class CCodeAssetImpl implements CCodeAsset { +final class CCodeAssetImpl implements CCodeAsset, AssetImpl { @override final Uri? file; @@ -193,17 +193,6 @@ final class CCodeAssetImpl implements CCodeAsset { ); } - static List listFromYamlString(String yaml) { - final yamlObject = loadYaml(yaml); - if (yamlObject == null) { - return []; - } - return [ - for (final yamlElement in as(yamlObject)) - CCodeAssetImpl.fromYaml(as(yamlElement)), - ]; - } - CCodeAssetImpl copyWith({ LinkModeImpl? linkMode, String? id, @@ -244,6 +233,7 @@ final class CCodeAssetImpl implements CCodeAsset { file, ); + @override Map toYaml(Version version) { if (version == Version(1, 0, 0)) { return { diff --git a/pkgs/native_assets_cli/lib/src/model/data_asset.dart b/pkgs/native_assets_cli/lib/src/model/data_asset.dart new file mode 100644 index 000000000..16df695e7 --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/model/data_asset.dart @@ -0,0 +1,51 @@ +// 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. + +part of '../api/asset.dart'; + +final class DataAssetImpl implements DataAsset, AssetImpl { + @override + final Uri file; + + @override + final String id; + + DataAssetImpl({ + required this.file, + required this.id, + }); + + factory DataAssetImpl.fromYaml(YamlMap yamlMap) => DataAssetImpl( + id: as(yamlMap[_idKey]), + file: Uri(path: as(yamlMap[_fileKey])), + ); + + @override + bool operator ==(Object other) { + if (other is! DataAssetImpl) { + return false; + } + return other.id == id && other.file == file; + } + + @override + int get hashCode => Object.hash( + id, + file, + ); + + @override + Map toYaml(Version version) => { + _idKey: id, + _fileKey: file.toFilePath(), + typeKey: DataAsset.type, + }..sortOnKey(); + + static const typeKey = 'type'; + static const _idKey = 'id'; + static const _fileKey = 'file'; + + @override + String toString() => 'DataAsset(${toYaml(BuildOutput.latestVersion)})'; +} diff --git a/pkgs/native_assets_cli/test/helpers.dart b/pkgs/native_assets_cli/test/helpers.dart index 4e0677b4e..1b70aa739 100644 --- a/pkgs/native_assets_cli/test/helpers.dart +++ b/pkgs/native_assets_cli/test/helpers.dart @@ -113,7 +113,7 @@ extension on String { Uri asFileUri() => Uri.file(this); } -extension AssetIterable on Iterable { +extension AssetIterable on Iterable { Future allExist() async { final allResults = await Future.wait(map((e) => e.exists())); final missing = allResults.contains(false); diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index d42a545a2..d2faa185c 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -7,13 +7,16 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart'; import 'package:native_assets_cli/src/api/asset.dart'; import 'package:native_assets_cli/src/utils/yaml.dart'; import 'package:test/test.dart'; +import 'package:yaml/yaml.dart'; void main() { final fooUri = Uri.file('path/to/libfoo.so'); final foo3Uri = Uri(path: 'libfoo3.so'); final barUri = Uri(path: 'path/to/libbar.a'); final blaUri = Uri(path: 'path/with spaces/bla.dll'); - final assets = [ + final dataUri = Uri.file('path/to/data.txt'); + final data2Uri = Uri.file('path/to/data.json'); + final cCodeAssets = [ CCodeAssetImpl( id: 'foo', file: fooUri, @@ -60,6 +63,20 @@ void main() { linkMode: LinkModeImpl.dynamic, ), ]; + final dataAssets = [ + DataAssetImpl( + id: 'my_data_asset', + file: dataUri, + ), + DataAssetImpl( + id: 'my_data_asset2', + file: data2Uri, + ), + ]; + final assets = [ + ...cCodeAssets, + ...dataAssets, + ]; final assetsYamlEncodingV1_0_0 = '''- id: foo link_mode: dynamic @@ -141,20 +158,27 @@ void main() { id: bla link_mode: dynamic os: windows - type: c_code'''; + type: c_code +- id: my_data_asset + file: path/to/data.txt + type: data +- id: my_data_asset2 + file: path/to/data.json + type: data'''; test('asset yaml', () { final yaml = yamlEncode([ for (final item in assets) item.toYaml(BuildOutputImpl.latestVersion) ]); expect(yaml, assetsYamlEncoding); - final assets2 = CCodeAssetImpl.listFromYamlString(yaml); + final assets2 = AssetImpl.listFromYamlList(loadYaml(yaml) as YamlList); expect(assets, assets2); }); test('build_output protocol v1.0.0 keeps working', () { - final assets2 = CCodeAssetImpl.listFromYamlString(assetsYamlEncodingV1_0_0); - expect(assets, assets2); + final assets2 = AssetImpl.listFromYamlList( + loadYaml(assetsYamlEncodingV1_0_0) as YamlList); + expect(cCodeAssets, assets2); }); test('AssetPath factory', () async { @@ -167,26 +191,23 @@ void main() { }); test('Asset hashCode copyWith', () async { - final asset = assets.first; + final asset = cCodeAssets.first; final asset2 = asset.copyWith(id: 'foo321'); expect(asset.hashCode != asset2.hashCode, true); final asset3 = asset.copyWith(); expect(asset.hashCode, asset3.hashCode); + + expect(dataAssets[0].hashCode, isNot(dataAssets[1].hashCode)); }); test('List hashCode', () async { - final assets2 = assets.take(3).toList(); + final assets2 = cCodeAssets.take(3).toList(); const equality = ListEquality(); - expect(equality.hash(assets) != equality.hash(assets2), true); + expect(equality.hash(cCodeAssets) != equality.hash(assets2), true); }); test('Asset toString', () async { assets.toString(); }); - - test('Asset listFromYamlString', () async { - final assets = CCodeAssetImpl.listFromYamlString(''); - expect(assets, []); - }); } From 8170ef883998c9ba4c2c71531012889641fe8399 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 20 Feb 2024 12:31:30 +0100 Subject: [PATCH 31/75] Fix windows test --- pkgs/native_assets_cli/test/model/asset_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index d2faa185c..0d9027472 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -169,7 +169,7 @@ void main() { test('asset yaml', () { final yaml = yamlEncode([ for (final item in assets) item.toYaml(BuildOutputImpl.latestVersion) - ]); + ]).replaceAll('\\', '/'); expect(yaml, assetsYamlEncoding); final assets2 = AssetImpl.listFromYamlList(loadYaml(yaml) as YamlList); expect(assets, assets2); From b8822efd8c92b3aa26103c4a9622e1f65586cdaf Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 20 Feb 2024 12:37:22 +0100 Subject: [PATCH 32/75] Fix test try 2 --- pkgs/native_assets_cli/test/model/asset_test.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 0d9027472..a15dbeb4e 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -160,16 +160,16 @@ void main() { os: windows type: c_code - id: my_data_asset - file: path/to/data.txt + file: ${dataUri.toFilePath()} type: data - id: my_data_asset2 - file: path/to/data.json + file: ${data2Uri.toFilePath()} type: data'''; test('asset yaml', () { final yaml = yamlEncode([ for (final item in assets) item.toYaml(BuildOutputImpl.latestVersion) - ]).replaceAll('\\', '/'); + ]); expect(yaml, assetsYamlEncoding); final assets2 = AssetImpl.listFromYamlList(loadYaml(yaml) as YamlList); expect(assets, assets2); From 81b7260fd24543900e1691ff1eafe0700fe41f14 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 13:57:54 +0100 Subject: [PATCH 33/75] Iterate on documentation in public API --- pkgs/native_assets_cli/lib/src/api/asset.dart | 14 +++++- .../lib/src/api/build_config.dart | 2 +- .../lib/src/api/build_output.dart | 33 ++++++------- .../lib/src/api/c_code_asset.dart | 32 ++++++------ .../lib/src/api/data_asset.dart | 9 ++-- .../lib/src/api/link_mode.dart | 9 ++++ .../lib/src/api/link_mode_preference.dart | 12 ++--- .../native_assets_cli/lib/src/api/target.dart | 49 ++++++++++++++++--- .../lib/src/model/build_config.dart | 47 ------------------ .../lib/src/model/build_mode.dart | 1 - .../lib/src/model/build_output.dart | 2 +- .../lib/src/model/ios_sdk.dart | 4 -- .../lib/src/model/link_mode_preference.dart | 1 - .../lib/src/model/target.dart | 24 +-------- 14 files changed, 111 insertions(+), 128 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index 320d9da41..ce47d3bc5 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -21,8 +21,9 @@ part '../model/data_asset.dart'; /// Data or code bundled with a Dart or Flutter application. /// /// An asset is data or code which is accessible from a Dart or Flutter -/// application. To retrieve an asset at runtime, the [id] is used. This enables -/// access to the asset irrespective of how and where the application is run. +/// application. To access an asset at runtime, the asset [id] is used. This +/// enables access to the asset irrespective of how and where the application is +/// run. abstract final class Asset { /// The identifier for this asset. /// @@ -38,5 +39,14 @@ abstract final class Asset { /// `'package:foo/src/foo.dart'`. String get id; + /// The file to be bundled with the Dart or Flutter application. + /// + /// How this file is bundled depends on the subtype [Asset] and the SDK (Dart + /// or Flutter). + /// + /// The file can be omitted in the [BuildOutput] for [BuildConfig.dryRun]. + /// + /// The file can also be omitted for asset types which refer to an asset + /// already present on the target system or an asset in Dart or Flutter. Uri? get file; } diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index dc2900ef6..3163e3fd9 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -68,7 +68,7 @@ abstract final class BuildConfig { /// in the Android documentation. int? get targetAndroidNdkApi; - /// Preferred linkMode method for library. + /// The preferred linkMode method for [CCodeAsset]s. LinkModePreference get linkModePreference; /// Get the metadata from a direct dependency. diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 5ac372294..61ab4b4c0 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -55,18 +55,20 @@ abstract class BuildOutput { /// seconds, because [File.lastModified] is rounded to whole seconds and /// caching logic compares these timestamps. /// - /// The [CCodeAsset]s produced by this build or dry-run can be provided to the - /// constructor as [assets], or can be added later using [addAssets]. In dry - /// runs, the assets for all [Architecture]s for the [OS] specified in the dry - /// run must be provided. + /// The [Asset]s produced by this build or dry-run can be provided to the + /// constructor as [assets], or can be added later using [addAsset] and + /// [addAssets]. In dry runs, the [Architecture] for [CCodeAsset]s can be + /// omitted. /// - /// The files used by this build must be passed in [dependencies] or + /// The files used by this build must be provided to the constructor as + /// [dependencies], or can be added later with [addDependency] and /// [addDependencies]. If any of these files are modified after [timestamp], /// the build will be re-run. Typically these dependencies contain the /// `build.dart` script itself, and the source files used in the build. /// - /// Metadata can be passed to `build.dart` invocations of dependent packages - /// via [metadata] or [addMetadata]. + /// Metadata can be passed to `build.dart` invocations of dependent packages. + /// It must be provided to the constructor as [metadata], or added later with + /// [addMetadatum] and [addMetadata]. factory BuildOutput({ DateTime? timestamp, Iterable? assets, @@ -80,16 +82,14 @@ abstract class BuildOutput { metadata: Metadata({...?metadata}), ); - /// Adds [CCodeAsset]s produced by this build or dry run. + /// Adds [Asset]s produced by this build or dry run. /// - /// In dry runs, the assets for all [Architecture]s for the [OS] specified in - /// the dry run must be provided. + /// In dry runs, the [Architecture] for [CCodeAsset]s can be omitted. void addAsset(Asset asset); - /// Adds [CCodeAsset]s produced by this build or dry run. + /// Adds [Asset]s produced by this build or dry run. /// - /// In dry runs, the assets for all [Architecture]s for the [OS] specified in - /// the dry run must be provided. + /// In dry runs, the [Architecture] for [CCodeAsset]s can be omitted. void addAssets(Iterable assets); /// Adds file used by this build. @@ -116,12 +116,11 @@ abstract class BuildOutput { /// /// This class is used in the protocol between the Dart and Flutter SDKs /// and packages through `build.dart` invocations. - /// - /// If we ever were to make breaking changes, it would be useful to give - /// proper error messages rather than just fail to parse the YAML - /// representation in the protocol. static Version get latestVersion => BuildOutputImpl.latestVersion; /// Write out this build output to a file inside [BuildConfig.outDir]. + /// + /// This takes into account the [BuildConfig.latestVersion] to write the + /// build output in a format the SDK that invoked the script supports. Future writeToFile({required BuildConfig config}); } diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index fc86010bc..bfcb3c060 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -9,22 +9,25 @@ part of 'asset.dart'; /// Typical other languages which produce code assets that respect the C ABI /// include C++ and Rust. /// -/// There are several example types of assets: -/// * Assets which designate symbols present in the target system, process, or -/// executable. They are identified by their name. -/// * Dynamic libraries bundled into the application. +/// There are several types of C code assets: +/// * Assets which designate symbols present in the target system +/// ([SystemDylib]), process ([LookupInProcess]), or executable +/// ([LookupInExecutable]). These assets are identified by their [id], and do +/// not have a [file]. +/// * Dynamic libraries bundled into the application ([BundledDylib]). These +/// assets must provide a [file] to be bundled. /// -/// An application is compiled to run on a certain target OS and architecture. -/// If different targets require different assets, the package developer must -/// specify which asset to bundle for which target. +/// An application is compiled to run on a specific target [os] and +/// [architecture]. Different targets require different assets, so the package +/// developer must specify which asset to bundle for which target. /// /// An asset has different ways of being accessible in the final application. It /// is either brought in "manually" by having the package developer specify a -/// path of the asset on the current system, it can be part of the Dart or -/// Flutter SDK, or it can be already present in the target system. If the asset -/// is bundled "manually", the Dart or Flutter SDK will take care of copying the -/// asset from its specified location on the current system into the application -/// bundle. +/// [file] path of the asset on the current system, it can be part of the Dart +/// or Flutter SDK ([LookupInProcess]), or it can be already present in the +/// target system ([SystemDylib]). If the asset is bundled "manually", the Dart +/// or Flutter SDK will take care of copying the asset [file] from its specified +/// location on the current system into the application bundle. /// /// Assets are also called "native assets" to differentiate them from the Dart /// code also bundled with an application. @@ -71,14 +74,13 @@ abstract final class DynamicLoading {} /// The asset file should be bundled by Dart/Flutter. /// -/// An asset with this dynamic loading method must provide a [Asset.file]. +/// An asset with this dynamic loading method must provide a [Asset.file]. The +/// Dart and Flutter SDK will bundle this code in the final application. abstract final class BundledDylib implements DynamicLoading { factory BundledDylib() = BundledDylibImpl; } /// Asset is avaliable on the target system `PATH`. -/// -/// [uri] only contains a file name. abstract final class SystemDylib implements DynamicLoading { Uri get uri; diff --git a/pkgs/native_assets_cli/lib/src/api/data_asset.dart b/pkgs/native_assets_cli/lib/src/api/data_asset.dart index f5415111c..45f76828c 100644 --- a/pkgs/native_assets_cli/lib/src/api/data_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/data_asset.dart @@ -6,9 +6,12 @@ part of 'asset.dart'; /// Data bundled with a Dart or Flutter application. /// -/// An data asset is data which is accessible from a Dart or Flutter -/// application. To retrieve an asset at runtime, the [id] is used. This enables -/// access to the asset irrespective of how and where the application is run. +/// A data asset is accessible in a Dart or Flutter application. To retrieve an +/// asset at runtime, the [id] is used. This enables access to the asset +/// irrespective of how and where the application is run. +/// +/// An data asset must provide a [Asset.file]. The Dart and Flutter SDK will +/// bundle this code in the final application. abstract final class DataAsset implements Asset { factory DataAsset({ required String id, diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode.dart b/pkgs/native_assets_cli/lib/src/api/link_mode.dart index 7206957a0..8db166080 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode.dart @@ -9,9 +9,18 @@ part '../model/link_mode.dart'; /// The link mode for [CCodeAsset]s. abstract class LinkMode { /// Dynamic loading. + /// + /// Supported in the Dart and Flutter SDK. + /// + /// Note: Dynamic loading is not equal to dynamic linking. Dynamic linking + /// would have to run the linker at compile-time, which is currently not + /// supported in the Dart and Flutter SDK. static const LinkMode dynamic = LinkModeImpl.dynamic; /// Static linking. + /// + /// Not yet supported in the Dart and Flutter SDK. + // TODO(https://github.com/dart-lang/sdk/issues/49418): Support static linking. static const LinkMode static = LinkModeImpl.static; /// Known values for [LinkMode]. diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart index a7ce492e5..036d8a455 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart @@ -2,15 +2,20 @@ // 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 'asset.dart'; import 'link_mode.dart'; part '../model/link_mode_preference.dart'; +/// The preferred linkMode method for [CCodeAsset]s. abstract class LinkModePreference { + /// The name for this link mode. String get name; + /// The preferred [LinkMode] for this link mode preference. LinkMode get preferredLinkMode; + /// The potential [LinkMode]s for this link mode preference. List get potentialLinkMode; /// Provide native assets as dynamic libraries. @@ -39,11 +44,4 @@ abstract class LinkModePreference { /// dynamic libraries. static const LinkModePreference preferStatic = LinkModePreferenceImpl.preferStatic; - - static const values = [ - dynamic, - static, - preferDynamic, - preferStatic, - ]; } diff --git a/pkgs/native_assets_cli/lib/src/api/target.dart b/pkgs/native_assets_cli/lib/src/api/target.dart index 72ad1ef7e..09327f354 100644 --- a/pkgs/native_assets_cli/lib/src/api/target.dart +++ b/pkgs/native_assets_cli/lib/src/api/target.dart @@ -5,17 +5,31 @@ import 'dart:ffi' show Abi; import 'dart:io'; +import 'build_config.dart'; +import 'ios_sdk.dart'; import 'link_mode.dart'; part '../model/target.dart'; /// The hardware architectures the Dart VM runs on. abstract class Architecture { + /// The [arm](https://en.wikipedia.org/wiki/ARM_architecture_family) + /// architecture. static const Architecture arm = ArchitectureImpl.arm; + + /// The [AArch64](https://en.wikipedia.org/wiki/AArch64) architecture. static const Architecture arm64 = ArchitectureImpl.arm64; + + /// The [IA-32](https://en.wikipedia.org/wiki/IA-32) architecture. static const Architecture ia32 = ArchitectureImpl.ia32; + + /// The [RISC-V](https://en.wikipedia.org/wiki/RISC-V) 32 bit architecture. static const Architecture riscv32 = ArchitectureImpl.riscv32; + + /// The [RISC-V](https://en.wikipedia.org/wiki/RISC-V) 64 bit architecture. static const Architecture riscv64 = ArchitectureImpl.riscv64; + + /// The [x86-64](https://en.wikipedia.org/wiki/X86-64) architecture. static const Architecture x64 = ArchitectureImpl.x64; /// Known values for [Architecture]. @@ -36,11 +50,27 @@ abstract class Architecture { /// The operating systems the Dart VM runs on. abstract class OS { + /// The + /// [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29) + /// operating system. static const OS android = OSImpl.android; + + /// The [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia) operating + /// system. static const OS fuchsia = OSImpl.fuchsia; + + /// The [iOS](https://en.wikipedia.org/wiki/IOS) operating system. static const OS iOS = OSImpl.iOS; + + /// The [Linux](https://en.wikipedia.org/wiki/Linux) operating system. static const OS linux = OSImpl.linux; + + /// The [macOS](https://en.wikipedia.org/wiki/MacOS) operating system. static const OS macOS = OSImpl.macOS; + + /// The + /// [Microsoft Windows](https://en.wikipedia.org/wiki/Microsoft_Windows) + /// operating system. static const OS windows = OSImpl.windows; /// Known values for [OS]. @@ -53,26 +83,31 @@ abstract class OS { windows, ]; - /// The default dynamic library file name on this [OS]. + /// The default dynamic library file name on this os. String dylibFileName(String name); - /// The default static library file name on this [OS]. + /// The default static library file name on this os. String staticlibFileName(String name); + /// The default library file name on this os. String libraryFileName(String name, LinkMode linkMode); - /// The default executable file name on this [OS]. + /// The default executable file name on this os. String executableFileName(String name); /// The current [OS]. /// - /// Read from the [Platform.version] string. + /// Consisten with the [Platform.version] string. static OS get current => OSImpl.current; } /// Application binary interface. /// /// The Dart VM can run on a variety of [Target]s, see [Target.values]. +/// +/// Please note that the [Target] does _not_ uniquely define a compilation +/// target. For example, the [IOSSdk], [BuildConfig.targetIOSSdk], and +/// [BuildConfig.targetAndroidNdkApi] also influence the compilation. abstract class Target implements Comparable { static const Target androidArm = TargetImpl.androidArm; static const Target androidArm64 = TargetImpl.androidArm64; @@ -96,7 +131,7 @@ abstract class Target implements Comparable { static const Target windowsIA32 = TargetImpl.windowsIA32; static const Target windowsX64 = TargetImpl.windowsX64; - /// All Targets that native assets can be built for. + /// All the application binary interfaces (ABIs) the Dart VM runs on. /// /// Note that for some of these a Dart SDK is not available and they are only /// used as target architectures for Flutter apps. @@ -104,10 +139,12 @@ abstract class Target implements Comparable { /// The current [Target]. /// - /// Read from the [Platform.version] string. + /// Consistent with the [Platform.version] string. static Target get current => TargetImpl.current; + /// The architecture for this target. Architecture get architecture; + /// The operating system for this target. OS get os; } diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index ec05c3896..4be4a5e51 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -5,43 +5,26 @@ part of '../api/build_config.dart'; final class BuildConfigImpl implements BuildConfig { - /// The folder in which all output and intermediate artifacts should be - /// placed. @override Uri get outDir => _outDir; late final Uri _outDir; - /// The name of the package the native assets are built for. @override String get packageName => _packageName; late final String _packageName; - /// The root of the package the native assets are built for. - /// - /// Often a package's native assets are built because a package is a - /// dependency of another. For this it is convenient to know the packageRoot. @override Uri get packageRoot => _packageRoot; late final Uri _packageRoot; - /// The architecture being compiled for. - /// - /// Not available during a [dryRun]. @override ArchitectureImpl? get targetArchitecture => _targetArchitecture; - late final ArchitectureImpl? _targetArchitecture; - /// The operating system being compiled for. @override OSImpl get targetOs => _targetOs; late final OSImpl _targetOs; - /// When compiling for iOS, whether to target device or simulator. - /// - /// Required when [targetOs] equals [OSImpl.iOS]. - /// - /// Not available during a [dryRun]. @override IOSSdkImpl? get targetIOSSdk { _ensureNotDryRun(); @@ -50,16 +33,6 @@ final class BuildConfigImpl implements BuildConfig { late final IOSSdkImpl? _targetIOSSdk; - /// When compiling for Android, the minimum Android SDK API version to that - /// the compiled code will be compatible with. - /// - /// Required when [targetOs] equals [OSImpl.android]. - /// - /// Not available during a [dryRun]. - /// - /// For more information about the Android API version, refer to - /// [`minSdkVersion`](https://developer.android.com/ndk/guides/sdk-versions#minsdkversion) - /// in the Android documentation. @override int? get targetAndroidNdkApi { _ensureNotDryRun(); @@ -68,7 +41,6 @@ final class BuildConfigImpl implements BuildConfig { late final int? _targetAndroidNdkApi; - /// Preferred linkMode method for library. @override LinkModePreferenceImpl get linkModePreference => _linkModePreference; late final LinkModePreferenceImpl _linkModePreference; @@ -81,9 +53,6 @@ final class BuildConfigImpl implements BuildConfig { late final Map? _dependencyMetadata; - /// The configuration for invoking the C compiler. - /// - /// Not available during a [dryRun]. @override CCompilerConfigImpl get cCompiler { _ensureNotDryRun(); @@ -92,14 +61,10 @@ final class BuildConfigImpl implements BuildConfig { late final CCompilerConfigImpl _cCompiler; - /// Don't run the build, only report the native assets produced. @override bool get dryRun => _dryRun ?? false; late final bool? _dryRun; - /// The build mode that the code should be compiled in. - /// - /// Not available during a [dryRun]. @override BuildModeImpl get buildMode { _ensureNotDryRun(); @@ -271,18 +236,6 @@ final class BuildConfigImpl implements BuildConfig { return result; } - /// Constructs a config by parsing CLI arguments and loading the config file. - /// - /// The [args] must be commandline arguments. - /// - /// If provided, [environment] must be a map containing environment variables. - /// If not provided, [environment] defaults to [Platform.environment]. - /// - /// If provided, [workingDirectory] is used to resolves paths inside - /// [environment]. - /// If not provided, [workingDirectory] defaults to [Directory.current]. - /// - /// This async constructor is intended to be used directly in CLI files. static Future fromArgs( List args, { Map? environment, diff --git a/pkgs/native_assets_cli/lib/src/model/build_mode.dart b/pkgs/native_assets_cli/lib/src/model/build_mode.dart index d44cf14e5..d5e53b788 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_mode.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_mode.dart @@ -21,7 +21,6 @@ class BuildModeImpl implements BuildMode { factory BuildModeImpl.fromString(String target) => values.firstWhere((e) => e.name == target); - /// The `package:config` key preferably used. static const String configKey = 'build_mode'; @override diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index dcfaabda3..ddd58261e 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -113,7 +113,7 @@ class BuildOutputImpl implements BuildOutput { static const fileName = 'build_output.yaml'; - /// Writes the YAML file from [outDir]/[fileName]. + /// Reads the YAML file from [outDir]/[fileName]. static Future readFromFile({required Uri outDir}) async { final buildOutputUri = outDir.resolve(fileName); final buildOutputFile = File.fromUri(buildOutputUri); diff --git a/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart b/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart index 7b94f6f13..1c17bedb4 100644 --- a/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart +++ b/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart @@ -4,9 +4,6 @@ part of '../api/ios_sdk.dart'; -/// For an iOS target, a build is either done for the device or the simulator. -/// -/// Only fat binaries or xcframeworks can contain both targets. class IOSSdkImpl implements IOSSdk { final String xcodebuildSdk; @@ -23,7 +20,6 @@ class IOSSdkImpl implements IOSSdk { factory IOSSdkImpl.fromString(String target) => values.firstWhere((e) => e.xcodebuildSdk == target); - /// The `package:config` key preferably used. static const String configKey = 'target_ios_sdk'; @override diff --git a/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart index 4a5291540..44ec80251 100644 --- a/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart @@ -54,7 +54,6 @@ class LinkModePreferenceImpl implements LinkModePreference { preferStatic, ]; - /// The `package:config` key preferably used. static const String configKey = 'link_mode_preference'; @override diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index 1ce6e2adb..cad0e36a3 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -4,7 +4,6 @@ part of '../api/target.dart'; -/// The hardware architectures the Dart VM runs on. class ArchitectureImpl implements Architecture { /// This architecture as used in [Platform.version]. final String dartPlatform; @@ -20,7 +19,6 @@ class ArchitectureImpl implements Architecture { static const ArchitectureImpl riscv64 = ArchitectureImpl._('riscv64'); static const ArchitectureImpl x64 = ArchitectureImpl._('x64'); - /// Known values for [ArchitectureImpl]. static const List values = [ arm, arm64, @@ -54,14 +52,11 @@ class ArchitectureImpl implements Architecture { Abi.windowsX64: ArchitectureImpl.x64, }; - /// The `package:config` key preferably used. static const String configKey = 'target_architecture'; @override String toString() => dartPlatform; - /// Mapping from strings as used in [ArchitectureImpl.toString] to - /// [ArchitectureImpl]s. static final Map _stringToArchitecture = Map.fromEntries(ArchitectureImpl.values.map( (architecture) => MapEntry(architecture.toString(), architecture))); @@ -69,13 +64,9 @@ class ArchitectureImpl implements Architecture { factory ArchitectureImpl.fromString(String target) => _stringToArchitecture[target]!; - /// The current [ArchitectureImpl]. - /// - /// Read from the [Platform.version] string. static final ArchitectureImpl current = TargetImpl.current.architecture; } -/// The operating systems the Dart VM runs on. class OSImpl implements OS { /// This OS as used in [Platform.version] final String dartPlatform; @@ -91,7 +82,6 @@ class OSImpl implements OS { static const OSImpl macOS = OSImpl._('macos'); static const OSImpl windows = OSImpl._('windows'); - /// Known values for [OSImpl]. static const List values = [ android, fuchsia, @@ -170,7 +160,6 @@ class OSImpl implements OS { OSImpl.windows: [OSImpl.windows, OSImpl.android], }; - /// The default dynamic library file name on this [OSImpl]. @override String dylibFileName(String name) { final prefix = _dylibPrefix[this]!; @@ -178,7 +167,6 @@ class OSImpl implements OS { return '$prefix$name.$extension'; } - /// The default static library file name on this [OSImpl]. @override String staticlibFileName(String name) { final prefix = _staticlibPrefix[this]!; @@ -195,7 +183,6 @@ class OSImpl implements OS { return staticlibFileName(name); } - /// The default executable file name on this [OSImpl]. @override String executableFileName(String name) { final extension = _executableExtension[this]!; @@ -246,7 +233,6 @@ class OSImpl implements OS { OSImpl.windows: 'exe', }; - /// The `package:config` key preferably used. static const String configKey = 'target_os'; @override @@ -265,9 +251,6 @@ class OSImpl implements OS { static final OSImpl current = TargetImpl.current.os; } -/// Application binary interface. -/// -/// The Dart VM can run on a variety of [TargetImpl]s, see [TargetImpl.values]. class TargetImpl implements Target { final Abi abi; @@ -330,10 +313,6 @@ class TargetImpl implements Target { static const windowsIA32 = TargetImpl._(Abi.windowsIA32); static const windowsX64 = TargetImpl._(Abi.windowsX64); - /// All Targets that we can build for. - /// - /// Note that for some of these a Dart SDK is not available and they are only - /// used as target architectures for Flutter apps. static const Set values = { androidArm, androidArm64, @@ -384,7 +363,6 @@ class TargetImpl implements Target { String get _osString => os.dartPlatform; - /// A string representation of this object. @override String toString() => dartVMToString(); @@ -411,7 +389,7 @@ class TargetImpl implements Target { } /// Common methods for manipulating iterables of [TargetImpl]s. -extension TargetList on Iterable { +extension on Iterable { /// The [TargetImpl]s in `this` sorted by name alphabetically. List get sorted => [for (final target in this) target]..sort(); } From 8d5da09d95fb641bc47cebe74256d542b2af1846 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 14:11:55 +0100 Subject: [PATCH 34/75] Move Architecture and OS to their own files --- .../lib/native_assets_cli.dart | 4 +- .../lib/native_assets_cli_internal.dart | 4 +- .../lib/src/api/architecture.dart | 47 ++++ pkgs/native_assets_cli/lib/src/api/asset.dart | 6 +- .../lib/src/api/build_config.dart | 2 + .../lib/src/api/build_output.dart | 3 +- pkgs/native_assets_cli/lib/src/api/os.dart | 65 +++++ .../native_assets_cli/lib/src/api/target.dart | 93 +------ .../lib/src/model/architecture.dart | 68 +++++ pkgs/native_assets_cli/lib/src/model/os.dart | 189 +++++++++++++ .../lib/src/model/target.dart | 249 +----------------- 11 files changed, 386 insertions(+), 344 deletions(-) create mode 100644 pkgs/native_assets_cli/lib/src/api/architecture.dart create mode 100644 pkgs/native_assets_cli/lib/src/api/os.dart create mode 100644 pkgs/native_assets_cli/lib/src/model/architecture.dart create mode 100644 pkgs/native_assets_cli/lib/src/model/os.dart diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 9add5eaf1..34f0fc844 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -6,6 +6,7 @@ /// native assets CLI, e.g. a `build.dart` script. library native_assets_cli; +export 'src/api/architecture.dart' show Architecture; export 'src/api/asset.dart' show Asset, @@ -21,4 +22,5 @@ export 'src/api/build_output.dart' show BuildOutput; export 'src/api/ios_sdk.dart' show IOSSdk; export 'src/api/link_mode.dart' show LinkMode; export 'src/api/link_mode_preference.dart' show LinkModePreference; -export 'src/api/target.dart' show Architecture, OS, Target; +export 'src/api/os.dart' show OS; +export 'src/api/target.dart' show Target; diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index 677bbe12e..cc21f5365 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -8,6 +8,7 @@ /// @nodoc library native_assets_cli_internal; +export 'src/api/architecture.dart' show ArchitectureImpl; export 'src/api/asset.dart' show AssetImpl, @@ -23,6 +24,7 @@ export 'src/api/build_output.dart' show BuildOutputImpl; export 'src/api/ios_sdk.dart' show IOSSdkImpl; export 'src/api/link_mode.dart' show LinkModeImpl; export 'src/api/link_mode_preference.dart' show LinkModePreferenceImpl; -export 'src/api/target.dart' show ArchitectureImpl, OSImpl, TargetImpl; +export 'src/api/os.dart' show OSImpl; +export 'src/api/target.dart' show TargetImpl; export 'src/model/dependencies.dart'; export 'src/model/metadata.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/architecture.dart b/pkgs/native_assets_cli/lib/src/api/architecture.dart new file mode 100644 index 000000000..18f155786 --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/api/architecture.dart @@ -0,0 +1,47 @@ +// 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' show Abi; +import 'dart:io'; + +import 'target.dart'; + +part '../model/architecture.dart'; + +/// The hardware architectures the Dart VM runs on. +abstract class Architecture { + /// The [arm](https://en.wikipedia.org/wiki/ARM_architecture_family) + /// architecture. + static const Architecture arm = ArchitectureImpl.arm; + + /// The [AArch64](https://en.wikipedia.org/wiki/AArch64) architecture. + static const Architecture arm64 = ArchitectureImpl.arm64; + + /// The [IA-32](https://en.wikipedia.org/wiki/IA-32) architecture. + static const Architecture ia32 = ArchitectureImpl.ia32; + + /// The [RISC-V](https://en.wikipedia.org/wiki/RISC-V) 32 bit architecture. + static const Architecture riscv32 = ArchitectureImpl.riscv32; + + /// The [RISC-V](https://en.wikipedia.org/wiki/RISC-V) 64 bit architecture. + static const Architecture riscv64 = ArchitectureImpl.riscv64; + + /// The [x86-64](https://en.wikipedia.org/wiki/X86-64) architecture. + static const Architecture x64 = ArchitectureImpl.x64; + + /// Known values for [Architecture]. + static const List values = [ + arm, + arm64, + ia32, + riscv32, + riscv64, + x64, + ]; + + /// The current [Architecture]. + /// + /// Read from the [Platform.version] string. + static Architecture get current => ArchitectureImpl.current; +} diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index ce47d3bc5..93e3f17a9 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -5,11 +5,13 @@ import 'package:pub_semver/pub_semver.dart'; import 'package:yaml/yaml.dart'; -import '../../native_assets_cli.dart'; -import '../../native_assets_cli_internal.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; +import 'architecture.dart'; +import 'build_config.dart'; +import 'build_output.dart'; import 'link_mode.dart'; +import 'os.dart'; import 'target.dart'; part 'c_code_asset.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 3163e3fd9..41099aa29 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -13,10 +13,12 @@ import 'package:pub_semver/pub_semver.dart'; import '../model/metadata.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; +import 'architecture.dart'; import 'asset.dart'; import 'build_mode.dart'; import 'ios_sdk.dart'; import 'link_mode_preference.dart'; +import 'os.dart'; import 'target.dart'; part '../model/build_config.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 61ab4b4c0..8d50f26c8 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -14,9 +14,10 @@ import '../utils/datetime.dart'; import '../utils/file.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; +import 'architecture.dart'; import 'asset.dart'; import 'build_config.dart'; -import 'target.dart'; +import 'os.dart'; part '../model/build_output.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/os.dart b/pkgs/native_assets_cli/lib/src/api/os.dart new file mode 100644 index 000000000..e3aad87e9 --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/api/os.dart @@ -0,0 +1,65 @@ +// 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' show Abi; +import 'dart:io'; + +import 'architecture.dart'; +import 'link_mode.dart'; +import 'target.dart'; + +part '../model/os.dart'; + +/// The operating systems the Dart VM runs on. +abstract class OS { + /// The + /// [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29) + /// operating system. + static const OS android = OSImpl.android; + + /// The [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia) operating + /// system. + static const OS fuchsia = OSImpl.fuchsia; + + /// The [iOS](https://en.wikipedia.org/wiki/IOS) operating system. + static const OS iOS = OSImpl.iOS; + + /// The [Linux](https://en.wikipedia.org/wiki/Linux) operating system. + static const OS linux = OSImpl.linux; + + /// The [macOS](https://en.wikipedia.org/wiki/MacOS) operating system. + static const OS macOS = OSImpl.macOS; + + /// The + /// [Microsoft Windows](https://en.wikipedia.org/wiki/Microsoft_Windows) + /// operating system. + static const OS windows = OSImpl.windows; + + /// Known values for [OS]. + static const List values = [ + android, + fuchsia, + iOS, + linux, + macOS, + windows, + ]; + + /// The default dynamic library file name on this os. + String dylibFileName(String name); + + /// The default static library file name on this os. + String staticlibFileName(String name); + + /// The default library file name on this os. + String libraryFileName(String name, LinkMode linkMode); + + /// The default executable file name on this os. + String executableFileName(String name); + + /// The current [OS]. + /// + /// Consisten with the [Platform.version] string. + static OS get current => OSImpl.current; +} diff --git a/pkgs/native_assets_cli/lib/src/api/target.dart b/pkgs/native_assets_cli/lib/src/api/target.dart index 09327f354..8fc85a4fd 100644 --- a/pkgs/native_assets_cli/lib/src/api/target.dart +++ b/pkgs/native_assets_cli/lib/src/api/target.dart @@ -5,102 +5,13 @@ import 'dart:ffi' show Abi; import 'dart:io'; +import 'architecture.dart'; import 'build_config.dart'; import 'ios_sdk.dart'; -import 'link_mode.dart'; +import 'os.dart'; part '../model/target.dart'; -/// The hardware architectures the Dart VM runs on. -abstract class Architecture { - /// The [arm](https://en.wikipedia.org/wiki/ARM_architecture_family) - /// architecture. - static const Architecture arm = ArchitectureImpl.arm; - - /// The [AArch64](https://en.wikipedia.org/wiki/AArch64) architecture. - static const Architecture arm64 = ArchitectureImpl.arm64; - - /// The [IA-32](https://en.wikipedia.org/wiki/IA-32) architecture. - static const Architecture ia32 = ArchitectureImpl.ia32; - - /// The [RISC-V](https://en.wikipedia.org/wiki/RISC-V) 32 bit architecture. - static const Architecture riscv32 = ArchitectureImpl.riscv32; - - /// The [RISC-V](https://en.wikipedia.org/wiki/RISC-V) 64 bit architecture. - static const Architecture riscv64 = ArchitectureImpl.riscv64; - - /// The [x86-64](https://en.wikipedia.org/wiki/X86-64) architecture. - static const Architecture x64 = ArchitectureImpl.x64; - - /// Known values for [Architecture]. - static const List values = [ - arm, - arm64, - ia32, - riscv32, - riscv64, - x64, - ]; - - /// The current [Architecture]. - /// - /// Read from the [Platform.version] string. - static Architecture get current => ArchitectureImpl.current; -} - -/// The operating systems the Dart VM runs on. -abstract class OS { - /// The - /// [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29) - /// operating system. - static const OS android = OSImpl.android; - - /// The [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia) operating - /// system. - static const OS fuchsia = OSImpl.fuchsia; - - /// The [iOS](https://en.wikipedia.org/wiki/IOS) operating system. - static const OS iOS = OSImpl.iOS; - - /// The [Linux](https://en.wikipedia.org/wiki/Linux) operating system. - static const OS linux = OSImpl.linux; - - /// The [macOS](https://en.wikipedia.org/wiki/MacOS) operating system. - static const OS macOS = OSImpl.macOS; - - /// The - /// [Microsoft Windows](https://en.wikipedia.org/wiki/Microsoft_Windows) - /// operating system. - static const OS windows = OSImpl.windows; - - /// Known values for [OS]. - static const List values = [ - android, - fuchsia, - iOS, - linux, - macOS, - windows, - ]; - - /// The default dynamic library file name on this os. - String dylibFileName(String name); - - /// The default static library file name on this os. - String staticlibFileName(String name); - - /// The default library file name on this os. - String libraryFileName(String name, LinkMode linkMode); - - /// The default executable file name on this os. - String executableFileName(String name); - - /// The current [OS]. - /// - /// Consisten with the [Platform.version] string. - static OS get current => OSImpl.current; -} - /// Application binary interface. /// /// The Dart VM can run on a variety of [Target]s, see [Target.values]. diff --git a/pkgs/native_assets_cli/lib/src/model/architecture.dart b/pkgs/native_assets_cli/lib/src/model/architecture.dart new file mode 100644 index 000000000..c5c284f33 --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/model/architecture.dart @@ -0,0 +1,68 @@ +// 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. + +part of '../api/architecture.dart'; + +class ArchitectureImpl implements Architecture { + /// This architecture as used in [Platform.version]. + final String dartPlatform; + + const ArchitectureImpl._(this.dartPlatform); + + factory ArchitectureImpl.fromAbi(Abi abi) => _abiToArch[abi]!; + + static const ArchitectureImpl arm = ArchitectureImpl._('arm'); + static const ArchitectureImpl arm64 = ArchitectureImpl._('arm64'); + static const ArchitectureImpl ia32 = ArchitectureImpl._('ia32'); + static const ArchitectureImpl riscv32 = ArchitectureImpl._('riscv32'); + static const ArchitectureImpl riscv64 = ArchitectureImpl._('riscv64'); + static const ArchitectureImpl x64 = ArchitectureImpl._('x64'); + + static const List values = [ + arm, + arm64, + ia32, + riscv32, + riscv64, + x64, + ]; + + static const _abiToArch = { + Abi.androidArm: ArchitectureImpl.arm, + Abi.androidArm64: ArchitectureImpl.arm64, + Abi.androidIA32: ArchitectureImpl.ia32, + Abi.androidX64: ArchitectureImpl.x64, + Abi.androidRiscv64: ArchitectureImpl.riscv64, + Abi.fuchsiaArm64: ArchitectureImpl.arm64, + Abi.fuchsiaX64: ArchitectureImpl.x64, + Abi.iosArm: ArchitectureImpl.arm, + Abi.iosArm64: ArchitectureImpl.arm64, + Abi.iosX64: ArchitectureImpl.x64, + Abi.linuxArm: ArchitectureImpl.arm, + Abi.linuxArm64: ArchitectureImpl.arm64, + Abi.linuxIA32: ArchitectureImpl.ia32, + Abi.linuxRiscv32: ArchitectureImpl.riscv32, + Abi.linuxRiscv64: ArchitectureImpl.riscv64, + Abi.linuxX64: ArchitectureImpl.x64, + Abi.macosArm64: ArchitectureImpl.arm64, + Abi.macosX64: ArchitectureImpl.x64, + Abi.windowsArm64: ArchitectureImpl.arm64, + Abi.windowsIA32: ArchitectureImpl.ia32, + Abi.windowsX64: ArchitectureImpl.x64, + }; + + static const String configKey = 'target_architecture'; + + @override + String toString() => dartPlatform; + + static final Map _stringToArchitecture = + Map.fromEntries(ArchitectureImpl.values.map( + (architecture) => MapEntry(architecture.toString(), architecture))); + + factory ArchitectureImpl.fromString(String target) => + _stringToArchitecture[target]!; + + static final ArchitectureImpl current = TargetImpl.current.architecture; +} diff --git a/pkgs/native_assets_cli/lib/src/model/os.dart b/pkgs/native_assets_cli/lib/src/model/os.dart new file mode 100644 index 000000000..e9e5695fd --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/model/os.dart @@ -0,0 +1,189 @@ +// 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. + +part of '../api/os.dart'; + +class OSImpl implements OS { + /// This OS as used in [Platform.version] + final String dartPlatform; + + const OSImpl._(this.dartPlatform); + + factory OSImpl.fromAbi(Abi abi) => _abiToOS[abi]!; + + static const OSImpl android = OSImpl._('android'); + static const OSImpl fuchsia = OSImpl._('fuchsia'); + static const OSImpl iOS = OSImpl._('ios'); + static const OSImpl linux = OSImpl._('linux'); + static const OSImpl macOS = OSImpl._('macos'); + static const OSImpl windows = OSImpl._('windows'); + + static const List values = [ + android, + fuchsia, + iOS, + linux, + macOS, + windows, + ]; + + static const _abiToOS = { + Abi.androidArm: OSImpl.android, + Abi.androidArm64: OSImpl.android, + Abi.androidIA32: OSImpl.android, + Abi.androidX64: OSImpl.android, + Abi.androidRiscv64: OSImpl.android, + Abi.fuchsiaArm64: OSImpl.fuchsia, + Abi.fuchsiaX64: OSImpl.fuchsia, + Abi.iosArm: OSImpl.iOS, + Abi.iosArm64: OSImpl.iOS, + Abi.iosX64: OSImpl.iOS, + Abi.linuxArm: OSImpl.linux, + Abi.linuxArm64: OSImpl.linux, + Abi.linuxIA32: OSImpl.linux, + Abi.linuxRiscv32: OSImpl.linux, + Abi.linuxRiscv64: OSImpl.linux, + Abi.linuxX64: OSImpl.linux, + Abi.macosArm64: OSImpl.macOS, + Abi.macosX64: OSImpl.macOS, + Abi.windowsArm64: OSImpl.windows, + Abi.windowsIA32: OSImpl.windows, + Abi.windowsX64: OSImpl.windows, + }; + + static const _osTargets = { + OSImpl.android: { + ArchitectureImpl.arm, + ArchitectureImpl.arm64, + ArchitectureImpl.ia32, + ArchitectureImpl.x64, + ArchitectureImpl.riscv64, + }, + OSImpl.fuchsia: { + ArchitectureImpl.arm64, + ArchitectureImpl.x64, + }, + OSImpl.iOS: { + ArchitectureImpl.arm, + ArchitectureImpl.arm64, + ArchitectureImpl.x64, + }, + OSImpl.linux: { + ArchitectureImpl.arm, + ArchitectureImpl.arm64, + ArchitectureImpl.ia32, + ArchitectureImpl.riscv32, + ArchitectureImpl.riscv64, + ArchitectureImpl.x64, + }, + OSImpl.macOS: { + ArchitectureImpl.arm64, + ArchitectureImpl.x64, + }, + OSImpl.windows: { + ArchitectureImpl.arm64, + ArchitectureImpl.ia32, + ArchitectureImpl.x64, + }, + }; + + Iterable get architectures => _osTargets[this]!; + + /// Typical cross compilation between OSes. + static const osCrossCompilationDefault = { + OSImpl.macOS: [OSImpl.macOS, OSImpl.iOS, OSImpl.android], + OSImpl.linux: [OSImpl.linux, OSImpl.android], + OSImpl.windows: [OSImpl.windows, OSImpl.android], + }; + + @override + String dylibFileName(String name) { + final prefix = _dylibPrefix[this]!; + final extension = _dylibExtension[this]!; + return '$prefix$name.$extension'; + } + + @override + String staticlibFileName(String name) { + final prefix = _staticlibPrefix[this]!; + final extension = _staticlibExtension[this]!; + return '$prefix$name.$extension'; + } + + @override + String libraryFileName(String name, LinkMode linkMode) { + if (linkMode == LinkModeImpl.dynamic) { + return dylibFileName(name); + } + assert(linkMode == LinkModeImpl.static); + return staticlibFileName(name); + } + + @override + String executableFileName(String name) { + final extension = _executableExtension[this]!; + final dot = extension.isNotEmpty ? '.' : ''; + return '$name$dot$extension'; + } + + /// The default name prefix for dynamic libraries per [OSImpl]. + static const _dylibPrefix = { + OSImpl.android: 'lib', + OSImpl.fuchsia: 'lib', + OSImpl.iOS: 'lib', + OSImpl.linux: 'lib', + OSImpl.macOS: 'lib', + OSImpl.windows: '', + }; + + /// The default extension for dynamic libraries per [OSImpl]. + static const _dylibExtension = { + OSImpl.android: 'so', + OSImpl.fuchsia: 'so', + OSImpl.iOS: 'dylib', + OSImpl.linux: 'so', + OSImpl.macOS: 'dylib', + OSImpl.windows: 'dll', + }; + + /// The default name prefix for static libraries per [OSImpl]. + static const _staticlibPrefix = _dylibPrefix; + + /// The default extension for static libraries per [OSImpl]. + static const _staticlibExtension = { + OSImpl.android: 'a', + OSImpl.fuchsia: 'a', + OSImpl.iOS: 'a', + OSImpl.linux: 'a', + OSImpl.macOS: 'a', + OSImpl.windows: 'lib', + }; + + /// The default extension for executables per [OSImpl]. + static const _executableExtension = { + OSImpl.android: '', + OSImpl.fuchsia: '', + OSImpl.iOS: '', + OSImpl.linux: '', + OSImpl.macOS: '', + OSImpl.windows: 'exe', + }; + + static const String configKey = 'target_os'; + + @override + String toString() => dartPlatform; + + /// Mapping from strings as used in [OSImpl.toString] to + /// [OSImpl]s. + static final Map _stringToOS = + Map.fromEntries(OSImpl.values.map((os) => MapEntry(os.toString(), os))); + + factory OSImpl.fromString(String target) => _stringToOS[target]!; + + /// The current [OSImpl]. + /// + /// Read from the [Platform.version] string. + static final OSImpl current = TargetImpl.current.os; +} diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index cad0e36a3..6f5fa7f9b 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -4,253 +4,6 @@ part of '../api/target.dart'; -class ArchitectureImpl implements Architecture { - /// This architecture as used in [Platform.version]. - final String dartPlatform; - - const ArchitectureImpl._(this.dartPlatform); - - factory ArchitectureImpl.fromAbi(Abi abi) => _abiToArch[abi]!; - - static const ArchitectureImpl arm = ArchitectureImpl._('arm'); - static const ArchitectureImpl arm64 = ArchitectureImpl._('arm64'); - static const ArchitectureImpl ia32 = ArchitectureImpl._('ia32'); - static const ArchitectureImpl riscv32 = ArchitectureImpl._('riscv32'); - static const ArchitectureImpl riscv64 = ArchitectureImpl._('riscv64'); - static const ArchitectureImpl x64 = ArchitectureImpl._('x64'); - - static const List values = [ - arm, - arm64, - ia32, - riscv32, - riscv64, - x64, - ]; - - static const _abiToArch = { - Abi.androidArm: ArchitectureImpl.arm, - Abi.androidArm64: ArchitectureImpl.arm64, - Abi.androidIA32: ArchitectureImpl.ia32, - Abi.androidX64: ArchitectureImpl.x64, - Abi.androidRiscv64: ArchitectureImpl.riscv64, - Abi.fuchsiaArm64: ArchitectureImpl.arm64, - Abi.fuchsiaX64: ArchitectureImpl.x64, - Abi.iosArm: ArchitectureImpl.arm, - Abi.iosArm64: ArchitectureImpl.arm64, - Abi.iosX64: ArchitectureImpl.x64, - Abi.linuxArm: ArchitectureImpl.arm, - Abi.linuxArm64: ArchitectureImpl.arm64, - Abi.linuxIA32: ArchitectureImpl.ia32, - Abi.linuxRiscv32: ArchitectureImpl.riscv32, - Abi.linuxRiscv64: ArchitectureImpl.riscv64, - Abi.linuxX64: ArchitectureImpl.x64, - Abi.macosArm64: ArchitectureImpl.arm64, - Abi.macosX64: ArchitectureImpl.x64, - Abi.windowsArm64: ArchitectureImpl.arm64, - Abi.windowsIA32: ArchitectureImpl.ia32, - Abi.windowsX64: ArchitectureImpl.x64, - }; - - static const String configKey = 'target_architecture'; - - @override - String toString() => dartPlatform; - - static final Map _stringToArchitecture = - Map.fromEntries(ArchitectureImpl.values.map( - (architecture) => MapEntry(architecture.toString(), architecture))); - - factory ArchitectureImpl.fromString(String target) => - _stringToArchitecture[target]!; - - static final ArchitectureImpl current = TargetImpl.current.architecture; -} - -class OSImpl implements OS { - /// This OS as used in [Platform.version] - final String dartPlatform; - - const OSImpl._(this.dartPlatform); - - factory OSImpl.fromAbi(Abi abi) => _abiToOS[abi]!; - - static const OSImpl android = OSImpl._('android'); - static const OSImpl fuchsia = OSImpl._('fuchsia'); - static const OSImpl iOS = OSImpl._('ios'); - static const OSImpl linux = OSImpl._('linux'); - static const OSImpl macOS = OSImpl._('macos'); - static const OSImpl windows = OSImpl._('windows'); - - static const List values = [ - android, - fuchsia, - iOS, - linux, - macOS, - windows, - ]; - - static const _abiToOS = { - Abi.androidArm: OSImpl.android, - Abi.androidArm64: OSImpl.android, - Abi.androidIA32: OSImpl.android, - Abi.androidX64: OSImpl.android, - Abi.androidRiscv64: OSImpl.android, - Abi.fuchsiaArm64: OSImpl.fuchsia, - Abi.fuchsiaX64: OSImpl.fuchsia, - Abi.iosArm: OSImpl.iOS, - Abi.iosArm64: OSImpl.iOS, - Abi.iosX64: OSImpl.iOS, - Abi.linuxArm: OSImpl.linux, - Abi.linuxArm64: OSImpl.linux, - Abi.linuxIA32: OSImpl.linux, - Abi.linuxRiscv32: OSImpl.linux, - Abi.linuxRiscv64: OSImpl.linux, - Abi.linuxX64: OSImpl.linux, - Abi.macosArm64: OSImpl.macOS, - Abi.macosX64: OSImpl.macOS, - Abi.windowsArm64: OSImpl.windows, - Abi.windowsIA32: OSImpl.windows, - Abi.windowsX64: OSImpl.windows, - }; - - static const _osTargets = { - OSImpl.android: { - ArchitectureImpl.arm, - ArchitectureImpl.arm64, - ArchitectureImpl.ia32, - ArchitectureImpl.x64, - ArchitectureImpl.riscv64, - }, - OSImpl.fuchsia: { - ArchitectureImpl.arm64, - ArchitectureImpl.x64, - }, - OSImpl.iOS: { - ArchitectureImpl.arm, - ArchitectureImpl.arm64, - ArchitectureImpl.x64, - }, - OSImpl.linux: { - ArchitectureImpl.arm, - ArchitectureImpl.arm64, - ArchitectureImpl.ia32, - ArchitectureImpl.riscv32, - ArchitectureImpl.riscv64, - ArchitectureImpl.x64, - }, - OSImpl.macOS: { - ArchitectureImpl.arm64, - ArchitectureImpl.x64, - }, - OSImpl.windows: { - ArchitectureImpl.arm64, - ArchitectureImpl.ia32, - ArchitectureImpl.x64, - }, - }; - - Iterable get architectures => _osTargets[this]!; - - /// Typical cross compilation between OSes. - static const _osCrossCompilationDefault = { - OSImpl.macOS: [OSImpl.macOS, OSImpl.iOS, OSImpl.android], - OSImpl.linux: [OSImpl.linux, OSImpl.android], - OSImpl.windows: [OSImpl.windows, OSImpl.android], - }; - - @override - String dylibFileName(String name) { - final prefix = _dylibPrefix[this]!; - final extension = _dylibExtension[this]!; - return '$prefix$name.$extension'; - } - - @override - String staticlibFileName(String name) { - final prefix = _staticlibPrefix[this]!; - final extension = _staticlibExtension[this]!; - return '$prefix$name.$extension'; - } - - @override - String libraryFileName(String name, LinkMode linkMode) { - if (linkMode == LinkModeImpl.dynamic) { - return dylibFileName(name); - } - assert(linkMode == LinkModeImpl.static); - return staticlibFileName(name); - } - - @override - String executableFileName(String name) { - final extension = _executableExtension[this]!; - final dot = extension.isNotEmpty ? '.' : ''; - return '$name$dot$extension'; - } - - /// The default name prefix for dynamic libraries per [OSImpl]. - static const _dylibPrefix = { - OSImpl.android: 'lib', - OSImpl.fuchsia: 'lib', - OSImpl.iOS: 'lib', - OSImpl.linux: 'lib', - OSImpl.macOS: 'lib', - OSImpl.windows: '', - }; - - /// The default extension for dynamic libraries per [OSImpl]. - static const _dylibExtension = { - OSImpl.android: 'so', - OSImpl.fuchsia: 'so', - OSImpl.iOS: 'dylib', - OSImpl.linux: 'so', - OSImpl.macOS: 'dylib', - OSImpl.windows: 'dll', - }; - - /// The default name prefix for static libraries per [OSImpl]. - static const _staticlibPrefix = _dylibPrefix; - - /// The default extension for static libraries per [OSImpl]. - static const _staticlibExtension = { - OSImpl.android: 'a', - OSImpl.fuchsia: 'a', - OSImpl.iOS: 'a', - OSImpl.linux: 'a', - OSImpl.macOS: 'a', - OSImpl.windows: 'lib', - }; - - /// The default extension for executables per [OSImpl]. - static const _executableExtension = { - OSImpl.android: '', - OSImpl.fuchsia: '', - OSImpl.iOS: '', - OSImpl.linux: '', - OSImpl.macOS: '', - OSImpl.windows: 'exe', - }; - - static const String configKey = 'target_os'; - - @override - String toString() => dartPlatform; - - /// Mapping from strings as used in [OSImpl.toString] to - /// [OSImpl]s. - static final Map _stringToOS = - Map.fromEntries(OSImpl.values.map((os) => MapEntry(os.toString(), os))); - - factory OSImpl.fromString(String target) => _stringToOS[target]!; - - /// The current [OSImpl]. - /// - /// Read from the [Platform.version] string. - static final OSImpl current = TargetImpl.current.os; -} - class TargetImpl implements Target { final Abi abi; @@ -378,7 +131,7 @@ class TargetImpl implements Target { /// A list of supported target [TargetImpl]s from this host [os]. List supportedTargetTargets( {Map> osCrossCompilation = - OSImpl._osCrossCompilationDefault}) => + OSImpl.osCrossCompilationDefault}) => TargetImpl.values .where((target) => // Only valid cross compilation. From f539b852e1d0d3cc3a3b6216488268888418b3df Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 14:19:42 +0100 Subject: [PATCH 35/75] Move c compiler config into its own file --- .../lib/src/api/build_config.dart | 29 +----- .../lib/src/api/build_output.dart | 2 +- .../lib/src/api/c_compiler_config.dart | 30 ++++++ .../lib/src/model/build_config.dart | 89 ------------------ .../lib/src/model/c_compiler_config.dart | 94 +++++++++++++++++++ 5 files changed, 128 insertions(+), 116 deletions(-) create mode 100644 pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart create mode 100644 pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 41099aa29..61edc2c0a 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -21,7 +21,9 @@ import 'link_mode_preference.dart'; import 'os.dart'; import 'target.dart'; +part 'c_compiler_config.dart'; part '../model/build_config.dart'; +part '../model/c_compiler_config.dart'; /// The configuration for a `build.dart` invocation. /// @@ -109,7 +111,7 @@ abstract final class BuildConfig { /// The version of [BuildConfig]. /// - /// This class is used in the protocol between the Dart and Flutter SDKs + /// The build config is used in the protocol between the Dart and Flutter SDKs /// and packages through `build.dart` invocations. /// /// If we ever were to make breaking changes, it would be useful to give @@ -194,28 +196,3 @@ abstract final class BuildConfig { workingDirectory: workingDirectory, ); } - -abstract class CCompilerConfig { - /// Path to a C compiler. - Uri? get cc; - - /// Path to a native linker. - Uri? get ld; - - /// Path to a native archiver. - Uri? get ar; - - /// Path to script that sets environment variables for [cc], [ld], and [ar]. - Uri? get envScript; - - /// Arguments for [envScript]. - List? get envScriptArgs; - - factory CCompilerConfig({ - Uri? ar, - Uri? cc, - Uri? ld, - Uri? envScript, - List? envScriptArgs, - }) = CCompilerConfigImpl; -} diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 8d50f26c8..374fcc789 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -115,7 +115,7 @@ abstract class BuildOutput { /// The version of [BuildOutput]. /// - /// This class is used in the protocol between the Dart and Flutter SDKs + /// The build output is used in the protocol between the Dart and Flutter SDKs /// and packages through `build.dart` invocations. static Version get latestVersion => BuildOutputImpl.latestVersion; diff --git a/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart b/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart new file mode 100644 index 000000000..273cbe9eb --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart @@ -0,0 +1,30 @@ +// 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. + +part of 'build_config.dart'; + +abstract class CCompilerConfig { + /// Path to a C compiler. + Uri? get cc; + + /// Path to a native linker. + Uri? get ld; + + /// Path to a native archiver. + Uri? get ar; + + /// Path to script that sets environment variables for [cc], [ld], and [ar]. + Uri? get envScript; + + /// Arguments for [envScript]. + List? get envScriptArgs; + + factory CCompilerConfig({ + Uri? ar, + Uri? cc, + Uri? ld, + Uri? envScript, + List? envScriptArgs, + }) = CCompilerConfigImpl; +} diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 4be4a5e51..c26e41074 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -547,92 +547,3 @@ can _only_ depend on OS.'''); } } } - -class CCompilerConfigImpl implements CCompilerConfig { - /// Path to a C compiler. - @override - Uri? get cc => _cc; - late final Uri? _cc; - - /// Path to a native linker. - @override - Uri? get ld => _ld; - late final Uri? _ld; - - /// Path to a native archiver. - @override - Uri? get ar => _ar; - late final Uri? _ar; - - /// Path to script that sets environment variables for [cc], [ld], and [ar]. - @override - Uri? get envScript => _envScript; - late final Uri? _envScript; - - /// Arguments for [envScript]. - @override - List? get envScriptArgs => _envScriptArgs; - late final List? _envScriptArgs; - - factory CCompilerConfigImpl({ - Uri? ar, - Uri? cc, - Uri? ld, - Uri? envScript, - List? envScriptArgs, - }) => - CCompilerConfigImpl._() - .._ar = ar - .._cc = cc - .._ld = ld - .._envScript = envScript - .._envScriptArgs = envScriptArgs; - - CCompilerConfigImpl._(); - - static const configKey = 'c_compiler'; - static const arConfigKey = 'ar'; - static const arConfigKeyFull = '$configKey.$arConfigKey'; - static const ccConfigKey = 'cc'; - static const ccConfigKeyFull = '$configKey.$ccConfigKey'; - static const ldConfigKey = 'ld'; - static const ldConfigKeyFull = '$configKey.$ldConfigKey'; - static const envScriptConfigKey = 'env_script'; - static const envScriptConfigKeyFull = '$configKey.$envScriptConfigKey'; - static const envScriptArgsConfigKey = 'env_script_arguments'; - static const envScriptArgsConfigKeyFull = - '$configKey.$envScriptArgsConfigKey'; - - Map toYaml() => { - if (_ar != null) arConfigKey: _ar.toFilePath(), - if (_cc != null) ccConfigKey: _cc.toFilePath(), - if (_ld != null) ldConfigKey: _ld.toFilePath(), - if (_envScript != null) envScriptConfigKey: _envScript.toFilePath(), - if (_envScriptArgs != null) envScriptArgsConfigKey: _envScriptArgs, - }.sortOnKey(); - - @override - bool operator ==(Object other) { - if (other is! CCompilerConfigImpl) { - return false; - } - if (other.ar != ar) return false; - if (other.cc != cc) return false; - if (other.ld != ld) return false; - if (other.envScript != envScript) return false; - if (!const ListEquality() - .equals(other.envScriptArgs, envScriptArgs)) { - return false; - } - return true; - } - - @override - int get hashCode => Object.hash( - _ar, - _cc, - _ld, - _envScript, - const ListEquality().hash(envScriptArgs), - ); -} diff --git a/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart b/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart new file mode 100644 index 000000000..f7c0fcde3 --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart @@ -0,0 +1,94 @@ +// 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. + +part of '../api/build_config.dart'; + +class CCompilerConfigImpl implements CCompilerConfig { + /// Path to a C compiler. + @override + Uri? get cc => _cc; + late final Uri? _cc; + + /// Path to a native linker. + @override + Uri? get ld => _ld; + late final Uri? _ld; + + /// Path to a native archiver. + @override + Uri? get ar => _ar; + late final Uri? _ar; + + /// Path to script that sets environment variables for [cc], [ld], and [ar]. + @override + Uri? get envScript => _envScript; + late final Uri? _envScript; + + /// Arguments for [envScript]. + @override + List? get envScriptArgs => _envScriptArgs; + late final List? _envScriptArgs; + + factory CCompilerConfigImpl({ + Uri? ar, + Uri? cc, + Uri? ld, + Uri? envScript, + List? envScriptArgs, + }) => + CCompilerConfigImpl._() + .._ar = ar + .._cc = cc + .._ld = ld + .._envScript = envScript + .._envScriptArgs = envScriptArgs; + + CCompilerConfigImpl._(); + + static const configKey = 'c_compiler'; + static const arConfigKey = 'ar'; + static const arConfigKeyFull = '$configKey.$arConfigKey'; + static const ccConfigKey = 'cc'; + static const ccConfigKeyFull = '$configKey.$ccConfigKey'; + static const ldConfigKey = 'ld'; + static const ldConfigKeyFull = '$configKey.$ldConfigKey'; + static const envScriptConfigKey = 'env_script'; + static const envScriptConfigKeyFull = '$configKey.$envScriptConfigKey'; + static const envScriptArgsConfigKey = 'env_script_arguments'; + static const envScriptArgsConfigKeyFull = + '$configKey.$envScriptArgsConfigKey'; + + Map toYaml() => { + if (_ar != null) arConfigKey: _ar.toFilePath(), + if (_cc != null) ccConfigKey: _cc.toFilePath(), + if (_ld != null) ldConfigKey: _ld.toFilePath(), + if (_envScript != null) envScriptConfigKey: _envScript.toFilePath(), + if (_envScriptArgs != null) envScriptArgsConfigKey: _envScriptArgs, + }.sortOnKey(); + + @override + bool operator ==(Object other) { + if (other is! CCompilerConfigImpl) { + return false; + } + if (other.ar != ar) return false; + if (other.cc != cc) return false; + if (other.ld != ld) return false; + if (other.envScript != envScript) return false; + if (!const ListEquality() + .equals(other.envScriptArgs, envScriptArgs)) { + return false; + } + return true; + } + + @override + int get hashCode => Object.hash( + _ar, + _cc, + _ld, + _envScript, + const ListEquality().hash(envScriptArgs), + ); +} From f1544e338f6f234d0e8ffc016d202a3fb490822a Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 14:21:21 +0100 Subject: [PATCH 36/75] Mark all classes in the API final --- pkgs/native_assets_cli/lib/src/api/architecture.dart | 2 +- pkgs/native_assets_cli/lib/src/api/build_mode.dart | 2 +- pkgs/native_assets_cli/lib/src/api/build_output.dart | 2 +- pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart | 2 +- pkgs/native_assets_cli/lib/src/api/ios_sdk.dart | 2 +- pkgs/native_assets_cli/lib/src/api/link_mode.dart | 2 +- pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart | 2 +- pkgs/native_assets_cli/lib/src/api/os.dart | 2 +- pkgs/native_assets_cli/lib/src/api/target.dart | 2 +- pkgs/native_assets_cli/lib/src/model/architecture.dart | 2 +- pkgs/native_assets_cli/lib/src/model/build_mode.dart | 2 +- pkgs/native_assets_cli/lib/src/model/build_output.dart | 2 +- pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart | 2 +- pkgs/native_assets_cli/lib/src/model/ios_sdk.dart | 2 +- pkgs/native_assets_cli/lib/src/model/link_mode.dart | 2 +- pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart | 2 +- pkgs/native_assets_cli/lib/src/model/os.dart | 2 +- pkgs/native_assets_cli/lib/src/model/target.dart | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/architecture.dart b/pkgs/native_assets_cli/lib/src/api/architecture.dart index 18f155786..d9ad58ccc 100644 --- a/pkgs/native_assets_cli/lib/src/api/architecture.dart +++ b/pkgs/native_assets_cli/lib/src/api/architecture.dart @@ -10,7 +10,7 @@ import 'target.dart'; part '../model/architecture.dart'; /// The hardware architectures the Dart VM runs on. -abstract class Architecture { +abstract final class Architecture { /// The [arm](https://en.wikipedia.org/wiki/ARM_architecture_family) /// architecture. static const Architecture arm = ArchitectureImpl.arm; diff --git a/pkgs/native_assets_cli/lib/src/api/build_mode.dart b/pkgs/native_assets_cli/lib/src/api/build_mode.dart index 7a7f5e68a..aaa89f142 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_mode.dart @@ -4,7 +4,7 @@ part '../model/build_mode.dart'; -abstract class BuildMode { +abstract final class BuildMode { String get name; static const BuildMode debug = BuildModeImpl.debug; diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 374fcc789..1793d23be 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -27,7 +27,7 @@ part '../model/build_output.dart'; /// script exists, it will be automatically run, by the Flutter and Dart SDK /// tools. The script is expect to produce a specific output which [BuildOutput] /// can produce. -abstract class BuildOutput { +abstract final class BuildOutput { // Start time for the build of this output. /// /// The [timestamp] is rounded down to whole seconds, because diff --git a/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart b/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart index 273cbe9eb..e7056917a 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart @@ -4,7 +4,7 @@ part of 'build_config.dart'; -abstract class CCompilerConfig { +abstract final class CCompilerConfig { /// Path to a C compiler. Uri? get cc; diff --git a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart index 8b1679000..9b2538b46 100644 --- a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart +++ b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart @@ -7,7 +7,7 @@ part '../model/ios_sdk.dart'; /// For an iOS target, a build is either done for the device or the simulator. /// /// Only fat binaries or xcframeworks can contain both targets. -abstract class IOSSdk { +abstract final class IOSSdk { static const IOSSdk iPhoneOs = IOSSdkImpl.iPhoneOs; static const IOSSdk iPhoneSimulator = IOSSdkImpl.iPhoneSimulator; diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode.dart b/pkgs/native_assets_cli/lib/src/api/link_mode.dart index 8db166080..084192a75 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode.dart @@ -7,7 +7,7 @@ import 'asset.dart'; part '../model/link_mode.dart'; /// The link mode for [CCodeAsset]s. -abstract class LinkMode { +abstract final class LinkMode { /// Dynamic loading. /// /// Supported in the Dart and Flutter SDK. diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart index 036d8a455..0f472ecfd 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart @@ -8,7 +8,7 @@ import 'link_mode.dart'; part '../model/link_mode_preference.dart'; /// The preferred linkMode method for [CCodeAsset]s. -abstract class LinkModePreference { +abstract final class LinkModePreference { /// The name for this link mode. String get name; diff --git a/pkgs/native_assets_cli/lib/src/api/os.dart b/pkgs/native_assets_cli/lib/src/api/os.dart index e3aad87e9..9bba1c4fa 100644 --- a/pkgs/native_assets_cli/lib/src/api/os.dart +++ b/pkgs/native_assets_cli/lib/src/api/os.dart @@ -12,7 +12,7 @@ import 'target.dart'; part '../model/os.dart'; /// The operating systems the Dart VM runs on. -abstract class OS { +abstract final class OS { /// The /// [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29) /// operating system. diff --git a/pkgs/native_assets_cli/lib/src/api/target.dart b/pkgs/native_assets_cli/lib/src/api/target.dart index 8fc85a4fd..6486b6045 100644 --- a/pkgs/native_assets_cli/lib/src/api/target.dart +++ b/pkgs/native_assets_cli/lib/src/api/target.dart @@ -19,7 +19,7 @@ part '../model/target.dart'; /// Please note that the [Target] does _not_ uniquely define a compilation /// target. For example, the [IOSSdk], [BuildConfig.targetIOSSdk], and /// [BuildConfig.targetAndroidNdkApi] also influence the compilation. -abstract class Target implements Comparable { +abstract final class Target implements Comparable { static const Target androidArm = TargetImpl.androidArm; static const Target androidArm64 = TargetImpl.androidArm64; static const Target androidIA32 = TargetImpl.androidIA32; diff --git a/pkgs/native_assets_cli/lib/src/model/architecture.dart b/pkgs/native_assets_cli/lib/src/model/architecture.dart index c5c284f33..7cbb8e0e8 100644 --- a/pkgs/native_assets_cli/lib/src/model/architecture.dart +++ b/pkgs/native_assets_cli/lib/src/model/architecture.dart @@ -4,7 +4,7 @@ part of '../api/architecture.dart'; -class ArchitectureImpl implements Architecture { +final class ArchitectureImpl implements Architecture { /// This architecture as used in [Platform.version]. final String dartPlatform; diff --git a/pkgs/native_assets_cli/lib/src/model/build_mode.dart b/pkgs/native_assets_cli/lib/src/model/build_mode.dart index d5e53b788..9fc2d8d63 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_mode.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_mode.dart @@ -4,7 +4,7 @@ part of '../api/build_mode.dart'; -class BuildModeImpl implements BuildMode { +final class BuildModeImpl implements BuildMode { @override final String name; diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index ddd58261e..7f82e820c 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -4,7 +4,7 @@ part of '../api/build_output.dart'; -class BuildOutputImpl implements BuildOutput { +final class BuildOutputImpl implements BuildOutput { @override final DateTime timestamp; diff --git a/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart b/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart index f7c0fcde3..2c0bc5b03 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart @@ -4,7 +4,7 @@ part of '../api/build_config.dart'; -class CCompilerConfigImpl implements CCompilerConfig { +final class CCompilerConfigImpl implements CCompilerConfig { /// Path to a C compiler. @override Uri? get cc => _cc; diff --git a/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart b/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart index 1c17bedb4..c50388562 100644 --- a/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart +++ b/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart @@ -4,7 +4,7 @@ part of '../api/ios_sdk.dart'; -class IOSSdkImpl implements IOSSdk { +final class IOSSdkImpl implements IOSSdk { final String xcodebuildSdk; const IOSSdkImpl._(this.xcodebuildSdk); diff --git a/pkgs/native_assets_cli/lib/src/model/link_mode.dart b/pkgs/native_assets_cli/lib/src/model/link_mode.dart index f073db1e5..b8e860773 100644 --- a/pkgs/native_assets_cli/lib/src/model/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/model/link_mode.dart @@ -4,7 +4,7 @@ part of '../api/link_mode.dart'; -class LinkModeImpl implements LinkMode { +final class LinkModeImpl implements LinkMode { final String name; const LinkModeImpl._(this.name); diff --git a/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart index 44ec80251..35436757c 100644 --- a/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart @@ -4,7 +4,7 @@ part of '../api/link_mode_preference.dart'; -class LinkModePreferenceImpl implements LinkModePreference { +final class LinkModePreferenceImpl implements LinkModePreference { @override final String name; diff --git a/pkgs/native_assets_cli/lib/src/model/os.dart b/pkgs/native_assets_cli/lib/src/model/os.dart index e9e5695fd..8ba249bca 100644 --- a/pkgs/native_assets_cli/lib/src/model/os.dart +++ b/pkgs/native_assets_cli/lib/src/model/os.dart @@ -4,7 +4,7 @@ part of '../api/os.dart'; -class OSImpl implements OS { +final class OSImpl implements OS { /// This OS as used in [Platform.version] final String dartPlatform; diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index 6f5fa7f9b..a45d797f8 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -4,7 +4,7 @@ part of '../api/target.dart'; -class TargetImpl implements Target { +final class TargetImpl implements Target { final Abi abi; const TargetImpl._(this.abi); From 2958a2a0da62ea5f2df32c7cbed8cc55ae69850c Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 18:35:34 +0100 Subject: [PATCH 37/75] Another documentation round --- .../example/local_asset/build.dart | 6 +- .../lib/src/api/architecture.dart | 2 +- pkgs/native_assets_cli/lib/src/api/asset.dart | 11 ++-- pkgs/native_assets_cli/lib/src/api/build.dart | 6 +- .../lib/src/api/build_config.dart | 57 ++++++++++--------- .../lib/src/api/build_mode.dart | 22 +++++++ .../lib/src/api/build_output.dart | 9 ++- .../lib/src/api/c_compiler_config.dart | 1 + .../lib/src/api/ios_sdk.dart | 10 ++++ pkgs/native_assets_cli/lib/src/api/os.dart | 2 +- .../lib/src/helpers/asset_downloader.dart | 6 +- 11 files changed, 83 insertions(+), 49 deletions(-) diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 146516f90..014f8c9b0 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -31,7 +31,7 @@ void main(List args) async { ]); } - output.addAssets([ + output.addAsset( CCodeAsset( id: 'library:$packageName/asset.txt', file: assetPath, @@ -39,7 +39,7 @@ void main(List args) async { os: config.targetOs, architecture: config.targetArchitecture, dynamicLoading: BundledDylib(), - ) - ]); + ), + ); }); } diff --git a/pkgs/native_assets_cli/lib/src/api/architecture.dart b/pkgs/native_assets_cli/lib/src/api/architecture.dart index d9ad58ccc..21dacf5c8 100644 --- a/pkgs/native_assets_cli/lib/src/api/architecture.dart +++ b/pkgs/native_assets_cli/lib/src/api/architecture.dart @@ -9,7 +9,7 @@ import 'target.dart'; part '../model/architecture.dart'; -/// The hardware architectures the Dart VM runs on. +/// A hardware architecture the Dart VM runs on. abstract final class Architecture { /// The [arm](https://en.wikipedia.org/wiki/ARM_architecture_family) /// architecture. diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index 93e3f17a9..e3b4af188 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -29,15 +29,15 @@ part '../model/data_asset.dart'; abstract final class Asset { /// The identifier for this asset. /// - /// An [Asset] must have a string identifier called `assetId`. Dart code that - /// uses an asset, references the asset using this `assetId`. + /// An [Asset] must have a string identifier called "asset id". Dart code that + /// uses an asset, references the asset using this asset id. /// - /// A package must prefix all `assetId`s it defines with: + /// A package must prefix all asset ids it defines with: /// `package:/`, `` being the current package's name. This /// ensures assets don't conflict between packages. /// /// Additionally, the convention is that an asset referenced from - /// `lib/src/foo.dart` in `package:foo` has the `assetId` + /// `lib/src/foo.dart` in `package:foo` has the asset id /// `'package:foo/src/foo.dart'`. String get id; @@ -49,6 +49,7 @@ abstract final class Asset { /// The file can be omitted in the [BuildOutput] for [BuildConfig.dryRun]. /// /// The file can also be omitted for asset types which refer to an asset - /// already present on the target system or an asset in Dart or Flutter. + /// already present on the target system or an asset already present in Dart + /// or Flutter. Uri? get file; } diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index c1f5910b6..0379fa92c 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -67,7 +67,7 @@ import 'build_output.dart'; /// ]); /// } /// -/// output.addAssets([ +/// output.addAsset( /// CCodeAsset( /// id: 'library:$packageName/asset.txt', /// file: assetPath, @@ -75,8 +75,8 @@ import 'build_output.dart'; /// os: config.targetOs, /// architecture: config.targetArchitecture, /// dynamicLoading: BundledDylib(), -/// ) -/// ]); +/// ), +/// ); /// }); /// } /// ``` diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 61edc2c0a..52cc7adca 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -32,7 +32,7 @@ part '../model/c_compiler_config.dart'; /// tools. The script will then be run with specific commandline arguments, /// which [BuildConfig] can parse and provide more convenient access to. abstract final class BuildConfig { - /// The folder in which all output and intermediate artifacts should be + /// The directory in which all output and intermediate artifacts should be /// placed. Uri get outDir; @@ -75,7 +75,7 @@ abstract final class BuildConfig { /// The preferred linkMode method for [CCodeAsset]s. LinkModePreference get linkModePreference; - /// Get the metadata from a direct dependency. + /// Metadata from a direct dependency. /// /// The [packageName] of is the package name of the direct dependency. /// @@ -107,6 +107,7 @@ abstract final class BuildConfig { /// /// Currently known values: /// * [CCodeAsset.type] + /// * [DataAsset.type] Iterable get supportedAssetTypes; /// The version of [BuildConfig]. @@ -114,11 +115,34 @@ abstract final class BuildConfig { /// The build config is used in the protocol between the Dart and Flutter SDKs /// and packages through `build.dart` invocations. /// - /// If we ever were to make breaking changes, it would be useful to give - /// proper error messages rather than just fail to parse the YAML - /// representation in the protocol. + /// We're trying to avoid breaking changes. However, in the case that we have + /// to, the major version mismatch between the Dart or Flutter SDK and a + /// `build.dart` script will lead to a nice error message. static Version get latestVersion => BuildConfigImpl.latestVersion; + /// Constructs a config by parsing CLI arguments and loading the config file. + /// + /// The [args] must be commandline arguments. + /// + /// If provided, [environment] must be a map containing environment variables. + /// If not provided, [environment] defaults to [Platform.environment]. + /// + /// If provided, [workingDirectory] is used to resolves paths inside + /// [environment]. + /// If not provided, [workingDirectory] defaults to [Directory.current]. + /// + /// This async constructor is intended to be used directly in CLI files. + static Future fromArgs( + List args, { + Map? environment, + Uri? workingDirectory, + }) => + BuildConfigImpl.fromArgs( + args, + environment: environment, + workingDirectory: workingDirectory, + ); + factory BuildConfig({ required Uri outDir, required String packageName, @@ -172,27 +196,4 @@ abstract final class BuildConfig { factory BuildConfig.fromConfig(Config config) => BuildConfigImpl.fromConfig(config); - - /// Constructs a config by parsing CLI arguments and loading the config file. - /// - /// The [args] must be commandline arguments. - /// - /// If provided, [environment] must be a map containing environment variables. - /// If not provided, [environment] defaults to [Platform.environment]. - /// - /// If provided, [workingDirectory] is used to resolves paths inside - /// [environment]. - /// If not provided, [workingDirectory] defaults to [Directory.current]. - /// - /// This async constructor is intended to be used directly in CLI files. - static Future fromArgs( - List args, { - Map? environment, - Uri? workingDirectory, - }) => - BuildConfigImpl.fromArgs( - args, - environment: environment, - workingDirectory: workingDirectory, - ); } diff --git a/pkgs/native_assets_cli/lib/src/api/build_mode.dart b/pkgs/native_assets_cli/lib/src/api/build_mode.dart index aaa89f142..1e8e636f6 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_mode.dart @@ -4,12 +4,34 @@ part '../model/build_mode.dart'; +/// The build mode for compiling native code assets. +/// +/// The Dart SDK does not have build modes. All `build.dart` invocations +/// are invoked with [release]. +/// +/// The Flutter SDK build modes map as the following to `build.dart` build +/// modes: +/// * Flutter debug -> [debug]. +/// * Flutter release -> [release]. +/// * Flutter profile -> [release]. +/// * Flutter jit release -> [release]. abstract final class BuildMode { + /// The name for this build mode. String get name; + /// The debug build mode. + /// + /// Used by the Flutter SDK in its debug mode. static const BuildMode debug = BuildModeImpl.debug; + + /// The release build mode. + /// + /// Used by the Flutter SDK in its release, profile, and jit release modes. + /// + /// Used by the Dart SDK for every build. static const BuildMode release = BuildModeImpl.release; + /// All known build modes. static const values = [ debug, release, diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 1793d23be..90b743227 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -23,12 +23,11 @@ part '../model/build_output.dart'; /// The output of a `build.dart` invocation. /// -/// A package can choose to have a toplevel `build.dart` script. If such a -/// script exists, it will be automatically run, by the Flutter and Dart SDK -/// tools. The script is expect to produce a specific output which [BuildOutput] -/// can produce. +/// A package can have a toplevel `build.dart` script. If such a script exists, +/// it will be automatically run, by the Flutter and Dart SDK tools. The script +/// is expect to produce a specific output which [BuildOutput] can produce. abstract final class BuildOutput { - // Start time for the build of this output. + /// Start time for the build of this output. /// /// The [timestamp] is rounded down to whole seconds, because /// [File.lastModified] is rounded to whole seconds and caching logic compares diff --git a/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart b/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart index e7056917a..44bf50b47 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart @@ -4,6 +4,7 @@ part of 'build_config.dart'; +/// The configuration for a C toolchain. abstract final class CCompilerConfig { /// Path to a C compiler. Uri? get cc; diff --git a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart index 9b2538b46..152c82db6 100644 --- a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart +++ b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart @@ -8,9 +8,19 @@ part '../model/ios_sdk.dart'; /// /// Only fat binaries or xcframeworks can contain both targets. abstract final class IOSSdk { + /// The iphoneos SDK in Xcode. + /// + /// The SDK location can be found on the host machine with + /// `xcrun --sdk iphoneos --show-sdk-path`. static const IOSSdk iPhoneOs = IOSSdkImpl.iPhoneOs; + + /// The iphonesimulator SDK in Xcode. + /// + /// The SDK location can be found on the host machine with + /// `xcrun --sdk iphonesimulator --show-sdk-path`. static const IOSSdk iPhoneSimulator = IOSSdkImpl.iPhoneSimulator; + /// All known values for [IOSSdk]. static const values = [ iPhoneOs, iPhoneSimulator, diff --git a/pkgs/native_assets_cli/lib/src/api/os.dart b/pkgs/native_assets_cli/lib/src/api/os.dart index 9bba1c4fa..9ce74dbc6 100644 --- a/pkgs/native_assets_cli/lib/src/api/os.dart +++ b/pkgs/native_assets_cli/lib/src/api/os.dart @@ -11,7 +11,7 @@ import 'target.dart'; part '../model/os.dart'; -/// The operating systems the Dart VM runs on. +/// An operating system the Dart VM runs on. abstract final class OS { /// The /// [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29) diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart index c641be1d2..6c03f0092 100644 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -55,7 +55,7 @@ class AssetDownloader implements Builder { await response.pipe(File.fromUri(targetUri).openWrite()); } - output.addAssets([ + output.addAsset( CCodeAsset( id: assetId, file: targetUri, @@ -63,7 +63,7 @@ class AssetDownloader implements Builder { dynamicLoading: BundledDylib(), os: config.targetOs, architecture: config.targetArchitecture, - ) - ]); + ), + ); } } From 622b7986c70681cd192da3fb745097d2a8b17c31 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 18:39:18 +0100 Subject: [PATCH 38/75] Cleanup Target.current use --- .../wrong_namespace_asset/build.dart | 6 ++--- .../lib/src/native_toolchain/android_ndk.dart | 6 ++--- .../lib/src/native_toolchain/msvc.dart | 22 +++++++++---------- .../lib/src/native_toolchain/recognizer.dart | 6 ++--- .../lib/src/tool/tool_resolver.dart | 2 +- .../test/cbuilder/cbuilder_test.dart | 19 +++++++--------- .../test/tool/tool_resolver_test.dart | 10 ++++----- 7 files changed, 33 insertions(+), 38 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 10a16a7b4..033dd2f58 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -11,11 +11,11 @@ void main(List args) async { CCodeAsset( id: 'package:other_package/foo', file: buildConfig.outDir.resolve( - Target.current.os.dylibFileName('foo'), + OS.current.dylibFileName('foo'), ), linkMode: LinkMode.dynamic, - os: Target.current.os, - architecture: Target.current.architecture, + os: OS.current, + architecture: Architecture.current, dynamicLoading: BundledDylib(), ), ], diff --git a/pkgs/native_toolchain_c/lib/src/native_toolchain/android_ndk.dart b/pkgs/native_toolchain_c/lib/src/native_toolchain/android_ndk.dart index 31edfc54f..964a97a35 100644 --- a/pkgs/native_toolchain_c/lib/src/native_toolchain/android_ndk.dart +++ b/pkgs/native_toolchain_c/lib/src/native_toolchain/android_ndk.dart @@ -92,7 +92,7 @@ class _AndroidNdkResolver implements ToolResolver { for (final hostArchDir in hostArchDirs) { final clangUri = hostArchDir.uri .resolve('bin/') - .resolve(Target.current.os.executableFileName('clang')); + .resolve(OS.current.executableFileName('clang')); if (await File.fromUri(clangUri).exists()) { result.add(await CliVersionResolver.lookupVersion( ToolInstance( @@ -104,7 +104,7 @@ class _AndroidNdkResolver implements ToolResolver { } final arUri = hostArchDir.uri .resolve('bin/') - .resolve(Target.current.os.executableFileName('llvm-ar')); + .resolve(OS.current.executableFileName('llvm-ar')); if (await File.fromUri(arUri).exists()) { result.add(await CliVersionResolver.lookupVersion( ToolInstance( @@ -116,7 +116,7 @@ class _AndroidNdkResolver implements ToolResolver { } final ldUri = hostArchDir.uri .resolve('bin/') - .resolve(Target.current.os.executableFileName('ld.lld')); + .resolve(OS.current.executableFileName('ld.lld')); if (await File.fromUri(arUri).exists()) { result.add(await CliVersionResolver.lookupVersion( ToolInstance( diff --git a/pkgs/native_toolchain_c/lib/src/native_toolchain/msvc.dart b/pkgs/native_toolchain_c/lib/src/native_toolchain/msvc.dart index a95356e99..da527f0b0 100644 --- a/pkgs/native_toolchain_c/lib/src/native_toolchain/msvc.dart +++ b/pkgs/native_toolchain_c/lib/src/native_toolchain/msvc.dart @@ -145,7 +145,7 @@ final Tool cl = _msvcTool( name: 'cl', versionArguments: [], targetArchitecture: Architecture.x64, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, ); /// The C/C++ Optimizing Compiler main executable. @@ -155,7 +155,7 @@ final Tool clIA32 = _msvcTool( name: 'cl', versionArguments: [], targetArchitecture: Architecture.ia32, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, ); /// The C/C++ Optimizing Compiler main executable. @@ -165,13 +165,13 @@ final Tool clArm64 = _msvcTool( name: 'cl', versionArguments: [], targetArchitecture: Architecture.arm64, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, ); final Tool lib = _msvcTool( name: 'lib', targetArchitecture: Architecture.x64, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, // https://github.com/dart-lang/native/issues/18 resolveVersion: false, ); @@ -179,7 +179,7 @@ final Tool lib = _msvcTool( final Tool libIA32 = _msvcTool( name: 'lib', targetArchitecture: Architecture.ia32, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, // https://github.com/dart-lang/native/issues/18 resolveVersion: false, ); @@ -187,7 +187,7 @@ final Tool libIA32 = _msvcTool( final Tool libArm64 = _msvcTool( name: 'lib', targetArchitecture: Architecture.arm64, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, // https://github.com/dart-lang/native/issues/18 resolveVersion: false, ); @@ -197,7 +197,7 @@ final Tool link = _msvcTool( versionArguments: ['/help'], versionExitCode: 1100, targetArchitecture: Architecture.x64, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, ); final Tool linkIA32 = _msvcTool( @@ -205,7 +205,7 @@ final Tool linkIA32 = _msvcTool( versionArguments: ['/help'], versionExitCode: 1100, targetArchitecture: Architecture.ia32, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, ); final Tool linkArm64 = _msvcTool( @@ -213,13 +213,13 @@ final Tool linkArm64 = _msvcTool( versionArguments: ['/help'], versionExitCode: 1100, targetArchitecture: Architecture.arm64, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, ); final Tool dumpbin = _msvcTool( name: 'dumpbin', targetArchitecture: Architecture.x64, - hostArchitecture: Target.current.architecture, + hostArchitecture: Architecture.current, ); const _msvcArchNames = { @@ -237,7 +237,7 @@ Tool _msvcTool({ bool resolveVersion = true, }) { final executableName = OS.windows.executableFileName(name); - if (Target.current.os != OS.windows) { + if (OS.current != OS.windows) { return Tool( name: executableName, defaultResolver: ToolResolvers([]), diff --git a/pkgs/native_toolchain_c/lib/src/native_toolchain/recognizer.dart b/pkgs/native_toolchain_c/lib/src/native_toolchain/recognizer.dart index 0de8418ad..cc5bfaf58 100644 --- a/pkgs/native_toolchain_c/lib/src/native_toolchain/recognizer.dart +++ b/pkgs/native_toolchain_c/lib/src/native_toolchain/recognizer.dart @@ -20,7 +20,7 @@ class CompilerRecognizer implements ToolResolver { @override Future> resolve({required Logger? logger}) async { - final os = Target.current.os; + final os = OS.current; logger?.finer('Trying to recognize $uri.'); final filePath = uri.toFilePath(); Tool? tool; @@ -64,7 +64,7 @@ class LinkerRecognizer implements ToolResolver { @override Future> resolve({required Logger? logger}) async { - final os = Target.current.os; + final os = OS.current; logger?.finer('Trying to recognize $uri.'); final filePath = uri.toFilePath(); Tool? tool; @@ -115,7 +115,7 @@ class ArchiverRecognizer implements ToolResolver { @override Future> resolve({required Logger? logger}) async { logger?.finer('Trying to recognize $uri.'); - final os = Target.current.os; + final os = OS.current; final filePath = uri.toFilePath(); Tool? tool; if (filePath.contains('-gcc-ar')) { diff --git a/pkgs/native_toolchain_c/lib/src/tool/tool_resolver.dart b/pkgs/native_toolchain_c/lib/src/tool/tool_resolver.dart index 257ef8ee1..337254828 100644 --- a/pkgs/native_toolchain_c/lib/src/tool/tool_resolver.dart +++ b/pkgs/native_toolchain_c/lib/src/tool/tool_resolver.dart @@ -35,7 +35,7 @@ class PathToolResolver extends ToolResolver { required this.toolName, String? executableName, }) : executableName = executableName ?? - Target.current.os.executableFileName(toolName.toLowerCase()); + OS.current.executableFileName(toolName.toLowerCase()); @override Future> resolve({required Logger? logger}) async { diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart index aa98ed978..d9b65ad05 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart @@ -71,7 +71,7 @@ void main() { ); final executableUri = - tempUri.resolve(Target.current.os.executableFileName(name)); + tempUri.resolve(OS.current.executableFileName(name)); expect(await File.fromUri(executableUri).exists(), true); final result = await runProcess( executable: executableUri, @@ -150,7 +150,7 @@ void main() { logger: logger, ); - final dylibUri = tempUri.resolve(Target.current.os.dylibFileName(name)); + final dylibUri = tempUri.resolve(OS.current.dylibFileName(name)); expect(await File.fromUri(dylibUri).exists(), !dryRun); if (!dryRun) { final dylib = openDynamicLibraryForTest(dylibUri.toFilePath()); @@ -247,8 +247,7 @@ void main() { logger: logger, ); - final executableUri = - tempUri.resolve(Target.current.os.executableFileName(name)); + final executableUri = tempUri.resolve(OS.current.executableFileName(name)); expect(await File.fromUri(executableUri).exists(), true); final result = await runProcess( executable: executableUri, @@ -303,7 +302,7 @@ void main() { expect(buildOutput.dependencies, contains(includesHUri)); - final dylibUri = tempUri.resolve(Target.current.os.dylibFileName(name)); + final dylibUri = tempUri.resolve(OS.current.dylibFileName(name)); final dylib = openDynamicLibraryForTest(dylibUri.toFilePath()); final x = dylib.lookup('x'); expect(x.value, 42); @@ -351,7 +350,7 @@ void main() { logger: logger, ); - final dylibUri = tempUri.resolve(Target.current.os.dylibFileName(name)); + final dylibUri = tempUri.resolve(OS.current.dylibFileName(name)); final dylib = openDynamicLibraryForTest(dylibUri.toFilePath()); final add = dylib.lookupFunction testDefines({ logger: logger, ); - final executableUri = - tempUri.resolve(Target.current.os.executableFileName(name)); + final executableUri = tempUri.resolve(OS.current.executableFileName(name)); expect(await File.fromUri(executableUri).exists(), true); final result = await runProcess( executable: executableUri, diff --git a/pkgs/native_toolchain_c/test/tool/tool_resolver_test.dart b/pkgs/native_toolchain_c/test/tool/tool_resolver_test.dart index 9f30ce08b..18b1a83d9 100644 --- a/pkgs/native_toolchain_c/test/tool/tool_resolver_test.dart +++ b/pkgs/native_toolchain_c/test/tool/tool_resolver_test.dart @@ -59,9 +59,8 @@ void main() { test('RelativeToolResolver', () async { final tempUri = await tempDirForTest(); - final barExeUri = - tempUri.resolve(Target.current.os.executableFileName('bar')); - final bazExeName = Target.current.os.executableFileName('baz'); + final barExeUri = tempUri.resolve(OS.current.executableFileName('bar')); + final bazExeName = OS.current.executableFileName('baz'); final bazExeUri = tempUri.resolve(bazExeName); await File.fromUri(barExeUri).writeAsString('dummy'); await File.fromUri(bazExeUri).writeAsString('dummy'); @@ -90,9 +89,8 @@ void main() { test('logger', () async { final tempUri = await tempDirForTest(); - final barExeUri = - tempUri.resolve(Target.current.os.executableFileName('bar')); - final bazExeName = Target.current.os.executableFileName('baz'); + final barExeUri = tempUri.resolve(OS.current.executableFileName('bar')); + final bazExeName = OS.current.executableFileName('baz'); final bazExeUri = tempUri.resolve(bazExeName); await File.fromUri(barExeUri).writeAsString('dummy'); final barResolver = InstallLocationResolver( From e48860736109b7ab6110470ef6210095b2330f4e Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 18:50:58 +0100 Subject: [PATCH 39/75] Migrate away from Target --- .../lib/native_assets_cli.dart | 2 +- .../lib/native_assets_cli_internal.dart | 2 +- .../lib/src/api/architecture.dart | 2 +- pkgs/native_assets_cli/lib/src/api/asset.dart | 2 +- .../lib/src/api/build_config.dart | 2 +- pkgs/native_assets_cli/lib/src/api/os.dart | 2 +- .../native_assets_cli/lib/src/api/target.dart | 61 ------------------- .../lib/src/model/target.dart | 12 ++-- .../test/api/target_test.dart | 5 -- .../lib/src/cbuilder/compiler_resolver.dart | 35 ++++++----- .../lib/src/cbuilder/run_cbuilder.dart | 1 - .../cbuilder/cbuilder_cross_android_test.dart | 40 ++++++------ .../cbuilder/cbuilder_cross_ios_test.dart | 18 +++--- .../cbuilder_cross_linux_host_test.dart | 26 ++++---- .../cbuilder_cross_macos_host_test.dart | 14 ++--- .../cbuilder_cross_windows_host_test.dart | 14 ++--- .../test/cbuilder/compiler_resolver_test.dart | 3 +- 17 files changed, 91 insertions(+), 150 deletions(-) delete mode 100644 pkgs/native_assets_cli/lib/src/api/target.dart diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 34f0fc844..5301ad877 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -23,4 +23,4 @@ export 'src/api/ios_sdk.dart' show IOSSdk; export 'src/api/link_mode.dart' show LinkMode; export 'src/api/link_mode_preference.dart' show LinkModePreference; export 'src/api/os.dart' show OS; -export 'src/api/target.dart' show Target; +// export 'src/api/target.dart' show Target; diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index cc21f5365..7ec4cd347 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -25,6 +25,6 @@ export 'src/api/ios_sdk.dart' show IOSSdkImpl; export 'src/api/link_mode.dart' show LinkModeImpl; export 'src/api/link_mode_preference.dart' show LinkModePreferenceImpl; export 'src/api/os.dart' show OSImpl; -export 'src/api/target.dart' show TargetImpl; export 'src/model/dependencies.dart'; export 'src/model/metadata.dart'; +export 'src/model/target.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/architecture.dart b/pkgs/native_assets_cli/lib/src/api/architecture.dart index 21dacf5c8..f431487bb 100644 --- a/pkgs/native_assets_cli/lib/src/api/architecture.dart +++ b/pkgs/native_assets_cli/lib/src/api/architecture.dart @@ -5,7 +5,7 @@ import 'dart:ffi' show Abi; import 'dart:io'; -import 'target.dart'; +import '../model/target.dart'; part '../model/architecture.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index e3b4af188..419e2a25d 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -5,6 +5,7 @@ import 'package:pub_semver/pub_semver.dart'; import 'package:yaml/yaml.dart'; +import '../model/target.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; import 'architecture.dart'; @@ -12,7 +13,6 @@ import 'build_config.dart'; import 'build_output.dart'; import 'link_mode.dart'; import 'os.dart'; -import 'target.dart'; part 'c_code_asset.dart'; part 'data_asset.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 52cc7adca..770dabb90 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -11,6 +11,7 @@ import 'package:crypto/crypto.dart'; import 'package:pub_semver/pub_semver.dart'; import '../model/metadata.dart'; +import '../model/target.dart'; import '../utils/map.dart'; import '../utils/yaml.dart'; import 'architecture.dart'; @@ -19,7 +20,6 @@ import 'build_mode.dart'; import 'ios_sdk.dart'; import 'link_mode_preference.dart'; import 'os.dart'; -import 'target.dart'; part 'c_compiler_config.dart'; part '../model/build_config.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/os.dart b/pkgs/native_assets_cli/lib/src/api/os.dart index 9ce74dbc6..f64124990 100644 --- a/pkgs/native_assets_cli/lib/src/api/os.dart +++ b/pkgs/native_assets_cli/lib/src/api/os.dart @@ -5,9 +5,9 @@ import 'dart:ffi' show Abi; import 'dart:io'; +import '../model/target.dart'; import 'architecture.dart'; import 'link_mode.dart'; -import 'target.dart'; part '../model/os.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/target.dart b/pkgs/native_assets_cli/lib/src/api/target.dart deleted file mode 100644 index 6486b6045..000000000 --- a/pkgs/native_assets_cli/lib/src/api/target.dart +++ /dev/null @@ -1,61 +0,0 @@ -// 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' show Abi; -import 'dart:io'; - -import 'architecture.dart'; -import 'build_config.dart'; -import 'ios_sdk.dart'; -import 'os.dart'; - -part '../model/target.dart'; - -/// Application binary interface. -/// -/// The Dart VM can run on a variety of [Target]s, see [Target.values]. -/// -/// Please note that the [Target] does _not_ uniquely define a compilation -/// target. For example, the [IOSSdk], [BuildConfig.targetIOSSdk], and -/// [BuildConfig.targetAndroidNdkApi] also influence the compilation. -abstract final class Target implements Comparable { - static const Target androidArm = TargetImpl.androidArm; - static const Target androidArm64 = TargetImpl.androidArm64; - static const Target androidIA32 = TargetImpl.androidIA32; - static const Target androidX64 = TargetImpl.androidX64; - static const Target androidRiscv64 = TargetImpl.androidRiscv64; - static const Target fuchsiaArm64 = TargetImpl.fuchsiaArm64; - static const Target fuchsiaX64 = TargetImpl.fuchsiaX64; - static const Target iOSArm = TargetImpl.iOSArm; - static const Target iOSArm64 = TargetImpl.iOSArm64; - static const Target iOSX64 = TargetImpl.iOSX64; - static const Target linuxArm = TargetImpl.linuxArm; - static const Target linuxArm64 = TargetImpl.linuxArm64; - static const Target linuxIA32 = TargetImpl.linuxIA32; - static const Target linuxRiscv32 = TargetImpl.linuxRiscv32; - static const Target linuxRiscv64 = TargetImpl.linuxRiscv64; - static const Target linuxX64 = TargetImpl.linuxX64; - static const Target macOSArm64 = TargetImpl.macOSArm64; - static const Target macOSX64 = TargetImpl.macOSX64; - static const Target windowsArm64 = TargetImpl.windowsArm64; - static const Target windowsIA32 = TargetImpl.windowsIA32; - static const Target windowsX64 = TargetImpl.windowsX64; - - /// All the application binary interfaces (ABIs) the Dart VM runs on. - /// - /// Note that for some of these a Dart SDK is not available and they are only - /// used as target architectures for Flutter apps. - static const Set values = TargetImpl.values; - - /// The current [Target]. - /// - /// Consistent with the [Platform.version] string. - static Target get current => TargetImpl.current; - - /// The architecture for this target. - Architecture get architecture; - - /// The operating system for this target. - OS get os; -} diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index a45d797f8..274e9fc3f 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -2,9 +2,13 @@ // 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. -part of '../api/target.dart'; +import 'dart:ffi' show Abi; +import 'dart:io'; -final class TargetImpl implements Target { +import '../api/architecture.dart'; +import '../api/os.dart'; + +final class TargetImpl implements Comparable { final Abi abi; const TargetImpl._(this.abi); @@ -106,10 +110,8 @@ final class TargetImpl implements Target { static final TargetImpl current = TargetImpl.fromDartPlatform(Platform.version); - @override ArchitectureImpl get architecture => ArchitectureImpl.fromAbi(abi); - @override OSImpl get os => OSImpl.fromAbi(abi); String get _architectureString => architecture.dartPlatform; @@ -126,7 +128,7 @@ final class TargetImpl implements Target { /// /// If [other] is also an [TargetImpl], consistent with sorting on [toString]. @override - int compareTo(Target other) => toString().compareTo(other.toString()); + int compareTo(TargetImpl other) => toString().compareTo(other.toString()); /// A list of supported target [TargetImpl]s from this host [os]. List supportedTargetTargets( diff --git a/pkgs/native_assets_cli/test/api/target_test.dart b/pkgs/native_assets_cli/test/api/target_test.dart index 3f9084f18..4d44547d1 100644 --- a/pkgs/native_assets_cli/test/api/target_test.dart +++ b/pkgs/native_assets_cli/test/api/target_test.dart @@ -8,11 +8,6 @@ import 'package:native_assets_cli/native_assets_cli.dart'; import 'package:test/test.dart'; void main() { - test('Target current', () async { - final current = Target.current; - expect(current.toString(), Abi.current().toString()); - }); - test('OS current', () async { final current = OS.current; expect(current.toString(), Abi.current().toString().split('_').first); diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart index 243fdd910..dc5301d62 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart @@ -22,13 +22,16 @@ import '../tool/tool_instance.dart'; class CompilerResolver { final BuildConfig buildConfig; final Logger? logger; - final Target host; + final OS hostOS; + final Architecture hostArchitecture; CompilerResolver({ required this.buildConfig, required this.logger, - Target? host, // Only visible for testing. - }) : host = host ?? Target.current; + OS? hostOS, // Only visible for testing. + Architecture? hostArchitecture, // Only visible for testing. + }) : hostOS = hostOS ?? OS.current, + hostArchitecture = hostArchitecture ?? Architecture.current; Future resolveCompiler() async { // First, check if the launcher provided a direct path to the compiler. @@ -46,7 +49,8 @@ class CompilerResolver { final targetOs = buildConfig.targetOs; final targetArchitecture = buildConfig.targetArchitecture; - final errorMessage = "No tools configured on host '$host' with target " + final errorMessage = + "No tools configured on host '${hostOS}_$hostArchitecture' with target " "'${targetOs}_$targetArchitecture'."; logger?.severe(errorMessage); throw ToolError(errorMessage); @@ -58,12 +62,12 @@ class CompilerResolver { final targetArch = buildConfig.targetArchitecture; // TODO(dacoharkes): Support falling back on other tools. - if (targetArch == host.architecture && - targetOs == host.os && - host.os == OS.linux) return clang; + if (targetArch == hostArchitecture && + targetOs == hostOS && + hostOS == OS.linux) return clang; if (targetOs == OS.macOS || targetOs == OS.iOS) return appleClang; if (targetOs == OS.android) return androidNdkClang; - if (host.os == OS.linux) { + if (hostOS == OS.linux) { switch (targetArch) { case Architecture.arm: return armLinuxGnueabihfGcc; @@ -78,7 +82,7 @@ class CompilerResolver { } } - if (host.os == OS.windows) { + if (hostOS == OS.windows) { switch (targetArch) { case Architecture.ia32: return clIA32; @@ -129,7 +133,8 @@ class CompilerResolver { final targetOs = buildConfig.targetOs; final targetArchitecture = buildConfig.targetArchitecture; - final errorMessage = "No tools configured on host '$host' with target " + final errorMessage = + "No tools configured on host '${hostOS}_$hostArchitecture' with target " "'${targetOs}_$targetArchitecture'."; logger?.severe(errorMessage); throw ToolError(errorMessage); @@ -141,14 +146,14 @@ class CompilerResolver { final targetArchitecture = buildConfig.targetArchitecture; // TODO(dacoharkes): Support falling back on other tools. - if (targetArchitecture == host.architecture && - targetOs == host.os && - host.os == OS.linux) { + if (targetArchitecture == hostArchitecture && + targetOs == hostOS && + hostOS == OS.linux) { return llvmAr; } if (targetOs == OS.macOS || targetOs == OS.iOS) return appleAr; if (targetOs == OS.android) return androidNdkLlvmAr; - if (host.os == OS.linux) { + if (hostOS == OS.linux) { switch (targetArchitecture) { case Architecture.arm: return armLinuxGnueabihfGccAr; @@ -162,7 +167,7 @@ class CompilerResolver { return riscv64LinuxGnuGccAr; } } - if (host.os == OS.windows) { + if (hostOS == OS.windows) { switch (targetArchitecture) { case Architecture.ia32: return libIA32; diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart index dd0b312c9..665079a13 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart @@ -27,7 +27,6 @@ class RunCBuilder { final Uri? dynamicLibrary; final Uri? staticLibrary; final Uri outDir; - // final Target target; /// The install name of the [dynamicLibrary]. /// diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart index 6e1800e0b..340d0640e 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart @@ -13,28 +13,28 @@ import '../helpers.dart'; void main() { const targets = [ - Target.androidArm, - Target.androidArm64, - Target.androidIA32, - Target.androidX64, + Architecture.arm, + Architecture.arm64, + Architecture.ia32, + Architecture.x64, // TODO(rmacnak): Enable when stable NDK 27 is available. - // Target.androidRiscv64, + // Architecture.riscv64, ]; const readElfMachine = { - Target.androidArm: 'ARM', - Target.androidArm64: 'AArch64', - Target.androidIA32: 'Intel 80386', - Target.androidX64: 'Advanced Micro Devices X86-64', - Target.androidRiscv64: 'RISC-V', + Architecture.arm: 'ARM', + Architecture.arm64: 'AArch64', + Architecture.ia32: 'Intel 80386', + Architecture.x64: 'Advanced Micro Devices X86-64', + Architecture.riscv64: 'RISC-V', }; const objdumpFileFormat = { - Target.androidArm: 'elf32-littlearm', - Target.androidArm64: 'elf64-littleaarch64', - Target.androidIA32: 'elf32-i386', - Target.androidX64: 'elf64-x86-64', - Target.androidRiscv64: 'elf64-littleriscv', + Architecture.arm: 'elf32-littlearm', + Architecture.arm64: 'elf64-littleaarch64', + Architecture.ia32: 'elf32-i386', + Architecture.x64: 'elf64-x86-64', + Architecture.riscv64: 'elf64-littleriscv', }; /// From https://docs.flutter.dev/reference/supported-platforms. @@ -91,7 +91,7 @@ void main() { } test('CBuilder API levels binary difference', () async { - const target = Target.androidArm64; + const target = Architecture.arm64; const linkMode = LinkMode.dynamic; const apiLevel1 = flutterAndroidNdkVersionLowestSupported; const apiLevel2 = flutterAndroidNdkVersionHighestSupported; @@ -117,7 +117,7 @@ void main() { Future buildLib( Uri tempUri, - Target target, + Architecture targetArchitecture, int androidNdkApi, LinkMode linkMode, ) async { @@ -128,8 +128,8 @@ Future buildLib( outDir: tempUri, packageName: name, packageRoot: tempUri, - targetArchitecture: target.architecture, - targetOs: target.os, + targetArchitecture: targetArchitecture, + targetOs: OS.android, targetAndroidNdkApi: androidNdkApi, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic @@ -149,6 +149,6 @@ Future buildLib( logger: logger, ); - final libUri = tempUri.resolve(target.os.libraryFileName(name, linkMode)); + final libUri = tempUri.resolve(OS.android.libraryFileName(name, linkMode)); return libUri; } diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart index 586f7ff5b..6b66ff64f 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart @@ -24,14 +24,14 @@ void main() { } const targets = [ - Target.iOSArm64, - Target.iOSX64, + Architecture.arm64, + Architecture.x64, ]; // Dont include 'mach-o' or 'Mach-O', different spelling is used. const objdumpFileFormat = { - Target.iOSArm64: 'arm64', - Target.iOSX64: '64-bit x86-64', + Architecture.arm64: 'arm64', + Architecture.x64: '64-bit x86-64', }; const name = 'add'; @@ -39,11 +39,11 @@ void main() { for (final linkMode in LinkMode.values) { for (final targetIOSSdk in IOSSdk.values) { for (final target in targets) { - if (target == Target.iOSX64 && targetIOSSdk == IOSSdk.iPhoneOs) { + if (target == Architecture.x64 && targetIOSSdk == IOSSdk.iPhoneOs) { continue; } - final libName = target.os.libraryFileName(name, linkMode); + final libName = OS.iOS.libraryFileName(name, linkMode); for (final installName in [ null, if (linkMode == LinkMode.dynamic) @@ -60,8 +60,8 @@ void main() { outDir: tempUri, packageName: name, packageRoot: tempUri, - targetArchitecture: target.architecture, - targetOs: target.os, + targetArchitecture: target, + targetOs: OS.iOS, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic ? LinkModePreference.dynamic @@ -100,7 +100,7 @@ void main() { logger: logger, ); expect(otoolResult.exitCode, 0); - if (targetIOSSdk == IOSSdk.iPhoneOs || target == Target.iOSX64) { + if (targetIOSSdk == IOSSdk.iPhoneOs || target == Architecture.x64) { // The x64 simulator behaves as device, presumably because the // devices are never x64. expect(otoolResult.stdout, contains('LC_VERSION_MIN_IPHONEOS')); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart index 8abe7a492..ea86fff59 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart @@ -21,19 +21,19 @@ void main() { } const targets = [ - Target.linuxArm, - Target.linuxArm64, - Target.linuxIA32, - Target.linuxX64, - Target.linuxRiscv64, + Architecture.arm, + Architecture.arm64, + Architecture.ia32, + Architecture.x64, + Architecture.riscv64, ]; const readElfMachine = { - Target.linuxArm: 'ARM', - Target.linuxArm64: 'AArch64', - Target.linuxIA32: 'Intel 80386', - Target.linuxX64: 'Advanced Micro Devices X86-64', - Target.linuxRiscv64: 'RISC-V', + Architecture.arm: 'ARM', + Architecture.arm64: 'AArch64', + Architecture.ia32: 'Intel 80386', + Architecture.x64: 'Advanced Micro Devices X86-64', + Architecture.riscv64: 'RISC-V', }; for (final linkMode in LinkMode.values) { @@ -48,8 +48,8 @@ void main() { outDir: tempUri, packageName: name, packageRoot: tempUri, - targetArchitecture: target.architecture, - targetOs: target.os, + targetArchitecture: target, + targetOs: OS.linux, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic ? LinkModePreference.dynamic @@ -69,7 +69,7 @@ void main() { ); final libUri = - tempUri.resolve(target.os.libraryFileName(name, linkMode)); + tempUri.resolve(OS.linux.libraryFileName(name, linkMode)); final result = await runProcess( executable: Uri.file('readelf'), arguments: ['-h', libUri.path], diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart index 655ea253f..e2a5ec114 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart @@ -24,14 +24,14 @@ void main() { } const targets = [ - Target.macOSArm64, - Target.macOSX64, + Architecture.arm64, + Architecture.x64, ]; // Dont include 'mach-o' or 'Mach-O', different spelling is used. const objdumpFileFormat = { - Target.macOSArm64: 'arm64', - Target.macOSX64: '64-bit x86-64', + Architecture.arm64: 'arm64', + Architecture.x64: '64-bit x86-64', }; for (final linkMode in LinkMode.values) { @@ -46,8 +46,8 @@ void main() { outDir: tempUri, packageName: name, packageRoot: tempUri, - targetArchitecture: target.architecture, - targetOs: target.os, + targetArchitecture: target, + targetOs: OS.macOS, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic ? LinkModePreference.dynamic @@ -67,7 +67,7 @@ void main() { ); final libUri = - tempUri.resolve(target.os.libraryFileName(name, linkMode)); + tempUri.resolve(OS.macOS.libraryFileName(name, linkMode)); final result = await runProcess( executable: Uri.file('objdump'), arguments: ['-t', libUri.path], diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart index bf0e83693..9bc3ad166 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart @@ -25,8 +25,8 @@ void main() { } const targets = [ - Target.windowsIA32, - Target.windowsX64, + Architecture.ia32, + Architecture.x64, ]; late Uri dumpbinUri; @@ -37,8 +37,8 @@ void main() { }); const dumpbinMachine = { - Target.windowsIA32: 'x86', - Target.windowsX64: 'x64', + Architecture.ia32: 'x86', + Architecture.x64: 'x64', }; const dumpbinFileType = { @@ -58,8 +58,8 @@ void main() { outDir: tempUri, packageName: name, packageRoot: tempUri, - targetOs: target.os, - targetArchitecture: target.architecture, + targetOs: OS.windows, + targetArchitecture: target, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic ? LinkModePreference.dynamic @@ -79,7 +79,7 @@ void main() { ); final libUri = - tempUri.resolve(target.os.libraryFileName(name, linkMode)); + tempUri.resolve(OS.windows.libraryFileName(name, linkMode)); expect(await File.fromUri(libUri).exists(), true); final result = await runProcess( executable: dumpbinUri, diff --git a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart index b77c231d3..7421248f8 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart @@ -76,7 +76,8 @@ void main() { final resolver = CompilerResolver( buildConfig: buildConfig, logger: logger, - host: Target.androidArm64, // This is never a host. + hostOS: OS.android, // This is never a host. + hostArchitecture: Architecture.arm64, // This is never a host. ); expect(resolver.resolveCompiler, throwsA(isA())); expect(resolver.resolveArchiver, throwsA(isA())); From 1154c957d27b651600f4d4a98d699c6a08dae859 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 19:03:43 +0100 Subject: [PATCH 40/75] Rename `TargetImpl` to `Target` --- .../lib/src/build_runner/build_runner.dart | 6 +- .../lib/src/model/kernel_assets.dart | 4 +- .../build_runner_dry_run_test.dart | 2 +- .../build_runner_reusability_test.dart | 8 +- .../test/build_runner/helpers.dart | 4 +- .../test/model/kernel_assets_test.dart | 14 +-- .../lib/src/model/architecture.dart | 2 +- .../lib/src/model/build_config.dart | 2 +- .../lib/src/model/c_code_asset.dart | 5 +- pkgs/native_assets_cli/lib/src/model/os.dart | 2 +- .../lib/src/model/target.dart | 98 +++++++++---------- .../test/model/target_test.dart | 23 +++-- 12 files changed, 83 insertions(+), 87 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 352e2427f..c513c14e8 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -37,7 +37,7 @@ class NativeAssetsBuildRunner { /// [runPackageName] are built. Future build({ required LinkModePreferenceImpl linkModePreference, - required TargetImpl target, + required Target target, required Uri workingDirectory, required BuildModeImpl buildMode, CCompilerConfigImpl? cCompilerConfig, @@ -341,7 +341,7 @@ build_output.yaml contained a format error. static Future _cliConfig({ required String packageName, required Uri packageRoot, - required TargetImpl target, + required Target target, IOSSdkImpl? targetIOSSdk, int? targetAndroidNdkApi, required BuildModeImpl buildMode, @@ -447,7 +447,7 @@ typedef _PackageBuildRecord = ( /// The result from a [NativeAssetsBuildRunner.dryRun]. abstract interface class DryRunResult { - /// The native assets for all [TargetImpl]s for the build or dry run. + /// The native assets for all [Target]s for the build or dry run. List get assets; /// Whether all builds completed without errors. diff --git a/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart b/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart index fad307de1..14418e8a5 100644 --- a/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart +++ b/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart @@ -24,7 +24,7 @@ class KernelAssets { KernelAssets([Iterable? assets]) : _assets = [...?assets]; String toNativeAssetsFile() { - final assetsPerTarget = >{}; + final assetsPerTarget = >{}; for (final asset in _assets) { final assets = assetsPerTarget[asset.target] ?? []; assets.add(asset); @@ -47,7 +47,7 @@ class KernelAssets { class KernelAsset { final String id; - final TargetImpl target; + final Target target; final KernelAssetPath path; KernelAsset({ diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart index caf719e75..f70304306 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart @@ -54,7 +54,7 @@ void main() async { } final dryRunDir = packageUri.resolve( - '.dart_tool/native_assets_builder/dry_run_${TargetImpl.current.os}_dynamic/'); + '.dart_tool/native_assets_builder/dry_run_${Target.current.os}_dynamic/'); expect(File.fromUri(dryRunDir.resolve('config.yaml')), exists); expect(File.fromUri(dryRunDir.resolve('out/build_output.yaml')), exists); // diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart index 5659ac16c..57a955227 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart @@ -29,13 +29,13 @@ void main() async { ); await buildRunner.dryRun( - targetOs: TargetImpl.current.os, + targetOs: Target.current.os, linkModePreference: LinkModePreferenceImpl.dynamic, workingDirectory: packageUri, includeParentEnvironment: true, ); await buildRunner.dryRun( - targetOs: TargetImpl.current.os, + targetOs: Target.current.os, linkModePreference: LinkModePreferenceImpl.dynamic, workingDirectory: packageUri, includeParentEnvironment: true, @@ -43,14 +43,14 @@ void main() async { await buildRunner.build( buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.dynamic, - target: TargetImpl.current, + target: Target.current, workingDirectory: packageUri, includeParentEnvironment: true, ); await buildRunner.build( buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.dynamic, - target: TargetImpl.current, + target: Target.current, workingDirectory: packageUri, includeParentEnvironment: true, ); diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index 8aeefdef5..5b4a9254d 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -53,7 +53,7 @@ Future build( ).build( buildMode: BuildModeImpl.release, linkModePreference: linkModePreference, - target: TargetImpl.current, + target: Target.current, workingDirectory: packageUri, cCompilerConfig: cCompilerConfig, includeParentEnvironment: includeParentEnvironment, @@ -92,7 +92,7 @@ Future dryRun( dartExecutable: dartExecutable, ).dryRun( linkModePreference: linkModePreference, - targetOs: TargetImpl.current.os, + targetOs: Target.current.os, workingDirectory: packageUri, includeParentEnvironment: includeParentEnvironment, packageLayout: packageLayout, diff --git a/pkgs/native_assets_builder/test/model/kernel_assets_test.dart b/pkgs/native_assets_builder/test/model/kernel_assets_test.dart index a2a951b0f..8b8e92b6b 100644 --- a/pkgs/native_assets_builder/test/model/kernel_assets_test.dart +++ b/pkgs/native_assets_builder/test/model/kernel_assets_test.dart @@ -18,37 +18,37 @@ void main() { KernelAsset( id: 'foo', path: KernelAssetAbsolutePath(fooUri), - target: TargetImpl.androidX64, + target: Target.androidX64, ), KernelAsset( id: 'foo2', path: KernelAssetRelativePath(foo2Uri), - target: TargetImpl.androidX64, + target: Target.androidX64, ), KernelAsset( id: 'foo3', path: KernelAssetSystemPath(foo3Uri), - target: TargetImpl.androidX64, + target: Target.androidX64, ), KernelAsset( id: 'foo4', path: KernelAssetInExecutable(), - target: TargetImpl.androidX64, + target: Target.androidX64, ), KernelAsset( id: 'foo5', path: KernelAssetInProcess(), - target: TargetImpl.androidX64, + target: Target.androidX64, ), KernelAsset( id: 'bar', path: KernelAssetAbsolutePath(barUri), - target: TargetImpl.linuxArm64, + target: Target.linuxArm64, ), KernelAsset( id: 'bla', path: KernelAssetAbsolutePath(blaUri), - target: TargetImpl.windowsX64, + target: Target.windowsX64, ), ]); diff --git a/pkgs/native_assets_cli/lib/src/model/architecture.dart b/pkgs/native_assets_cli/lib/src/model/architecture.dart index 7cbb8e0e8..f8efffe18 100644 --- a/pkgs/native_assets_cli/lib/src/model/architecture.dart +++ b/pkgs/native_assets_cli/lib/src/model/architecture.dart @@ -64,5 +64,5 @@ final class ArchitectureImpl implements Architecture { factory ArchitectureImpl.fromString(String target) => _stringToArchitecture[target]!; - static final ArchitectureImpl current = TargetImpl.current.architecture; + static final ArchitectureImpl current = Target.current.architecture; } diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index c26e41074..36195a953 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -316,7 +316,7 @@ final class BuildConfigImpl implements BuildConfig { if (!osSet) ...ArchitectureImpl.values else - for (final target in TargetImpl.values) + for (final target in Target.values) if (target.os == _targetOs) target.architecture ]; _targetArchitecture = ArchitectureImpl.fromString( diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index 75a992433..f89cc2f31 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -171,7 +171,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { final OSImpl os; if (targetString != null) { // Compatibility with v1.0.0. - final target = TargetImpl.fromString(targetString); + final target = Target.fromString(targetString); os = target.os; architecture = target.architecture; } else { @@ -240,8 +240,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { _idKey: id, _linkModeKey: linkMode.name, _pathKey: dynamicLoading.toYaml(version, file), - _targetKey: - TargetImpl.fromArchitectureAndOs(architecture!, os).toString(), + _targetKey: Target.fromArchitectureAndOs(architecture!, os).toString(), }..sortOnKey(); } return { diff --git a/pkgs/native_assets_cli/lib/src/model/os.dart b/pkgs/native_assets_cli/lib/src/model/os.dart index 8ba249bca..1d63cf02a 100644 --- a/pkgs/native_assets_cli/lib/src/model/os.dart +++ b/pkgs/native_assets_cli/lib/src/model/os.dart @@ -185,5 +185,5 @@ final class OSImpl implements OS { /// The current [OSImpl]. /// /// Read from the [Platform.version] string. - static final OSImpl current = TargetImpl.current.os; + static final OSImpl current = Target.current.os; } diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index 274e9fc3f..44c749c0f 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -8,19 +8,19 @@ import 'dart:io'; import '../api/architecture.dart'; import '../api/os.dart'; -final class TargetImpl implements Comparable { +final class Target implements Comparable { final Abi abi; - const TargetImpl._(this.abi); + const Target._(this.abi); - factory TargetImpl.fromString(String target) => _stringToTarget[target]!; + factory Target.fromString(String target) => _stringToTarget[target]!; - /// The [TargetImpl] corresponding the substring of [Platform.version] - /// describing the [TargetImpl]. + /// The [Target] corresponding the substring of [Platform.version] + /// describing the [Target]. /// /// The [Platform.version] strings are formatted as follows: /// ` () on ""`. - factory TargetImpl.fromDartPlatform(String versionStringFull) { + factory Target.fromDartPlatform(String versionStringFull) { final split = versionStringFull.split('"'); if (split.length < 2) { throw FormatException( @@ -35,7 +35,7 @@ final class TargetImpl implements Comparable { return target; } - factory TargetImpl.fromArchitectureAndOs( + factory Target.fromArchitectureAndOs( ArchitectureImpl architecture, OSImpl os, ) { @@ -48,29 +48,29 @@ final class TargetImpl implements Comparable { "'${os}_$architecture'"); } - static const androidArm = TargetImpl._(Abi.androidArm); - static const androidArm64 = TargetImpl._(Abi.androidArm64); - static const androidIA32 = TargetImpl._(Abi.androidIA32); - static const androidX64 = TargetImpl._(Abi.androidX64); - static const androidRiscv64 = TargetImpl._(Abi.androidRiscv64); - static const fuchsiaArm64 = TargetImpl._(Abi.fuchsiaArm64); - static const fuchsiaX64 = TargetImpl._(Abi.fuchsiaX64); - static const iOSArm = TargetImpl._(Abi.iosArm); - static const iOSArm64 = TargetImpl._(Abi.iosArm64); - static const iOSX64 = TargetImpl._(Abi.iosX64); - static const linuxArm = TargetImpl._(Abi.linuxArm); - static const linuxArm64 = TargetImpl._(Abi.linuxArm64); - static const linuxIA32 = TargetImpl._(Abi.linuxIA32); - static const linuxRiscv32 = TargetImpl._(Abi.linuxRiscv32); - static const linuxRiscv64 = TargetImpl._(Abi.linuxRiscv64); - static const linuxX64 = TargetImpl._(Abi.linuxX64); - static const macOSArm64 = TargetImpl._(Abi.macosArm64); - static const macOSX64 = TargetImpl._(Abi.macosX64); - static const windowsArm64 = TargetImpl._(Abi.windowsArm64); - static const windowsIA32 = TargetImpl._(Abi.windowsIA32); - static const windowsX64 = TargetImpl._(Abi.windowsX64); - - static const Set values = { + static const androidArm = Target._(Abi.androidArm); + static const androidArm64 = Target._(Abi.androidArm64); + static const androidIA32 = Target._(Abi.androidIA32); + static const androidX64 = Target._(Abi.androidX64); + static const androidRiscv64 = Target._(Abi.androidRiscv64); + static const fuchsiaArm64 = Target._(Abi.fuchsiaArm64); + static const fuchsiaX64 = Target._(Abi.fuchsiaX64); + static const iOSArm = Target._(Abi.iosArm); + static const iOSArm64 = Target._(Abi.iosArm64); + static const iOSX64 = Target._(Abi.iosX64); + static const linuxArm = Target._(Abi.linuxArm); + static const linuxArm64 = Target._(Abi.linuxArm64); + static const linuxIA32 = Target._(Abi.linuxIA32); + static const linuxRiscv32 = Target._(Abi.linuxRiscv32); + static const linuxRiscv64 = Target._(Abi.linuxRiscv64); + static const linuxX64 = Target._(Abi.linuxX64); + static const macOSArm64 = Target._(Abi.macosArm64); + static const macOSX64 = Target._(Abi.macosX64); + static const windowsArm64 = Target._(Abi.windowsArm64); + static const windowsIA32 = Target._(Abi.windowsIA32); + static const windowsX64 = Target._(Abi.windowsX64); + + static const Set values = { androidArm, androidArm64, androidIA32, @@ -94,21 +94,19 @@ final class TargetImpl implements Comparable { windowsX64, }; - /// Mapping from strings as used in [TargetImpl.toString] to [TargetImpl]s. - static final Map _stringToTarget = Map.fromEntries( - TargetImpl.values.map((target) => MapEntry(target.toString(), target))); + /// Mapping from strings as used in [Target.toString] to [Target]s. + static final Map _stringToTarget = Map.fromEntries( + Target.values.map((target) => MapEntry(target.toString(), target))); /// Mapping from lowercased strings as used in [Platform.version] to - /// [TargetImpl]s. - static final Map _dartVMstringToTarget = Map.fromEntries( - TargetImpl.values - .map((target) => MapEntry(target.dartVMToString(), target))); + /// [Target]s. + static final Map _dartVMstringToTarget = Map.fromEntries( + Target.values.map((target) => MapEntry(target.dartVMToString(), target))); - /// The current [TargetImpl]. + /// The current [Target]. /// /// Read from the [Platform.version] string. - static final TargetImpl current = - TargetImpl.fromDartPlatform(Platform.version); + static final Target current = Target.fromDartPlatform(Platform.version); ArchitectureImpl get architecture => ArchitectureImpl.fromAbi(abi); @@ -126,25 +124,25 @@ final class TargetImpl implements Comparable { /// Compares `this` to [other]. /// - /// If [other] is also an [TargetImpl], consistent with sorting on [toString]. + /// If [other] is also an [Target], consistent with sorting on [toString]. @override - int compareTo(TargetImpl other) => toString().compareTo(other.toString()); + int compareTo(Target other) => toString().compareTo(other.toString()); - /// A list of supported target [TargetImpl]s from this host [os]. - List supportedTargetTargets( + /// A list of supported target [Target]s from this host [os]. + List supportedTargetTargets( {Map> osCrossCompilation = OSImpl.osCrossCompilationDefault}) => - TargetImpl.values + Target.values .where((target) => // Only valid cross compilation. osCrossCompilation[os]!.contains(target.os) && // And no deprecated architectures. - target != TargetImpl.iOSArm) + target != Target.iOSArm) .sorted; } -/// Common methods for manipulating iterables of [TargetImpl]s. -extension on Iterable { - /// The [TargetImpl]s in `this` sorted by name alphabetically. - List get sorted => [for (final target in this) target]..sort(); +/// Common methods for manipulating iterables of [Target]s. +extension on Iterable { + /// The [Target]s in `this` sorted by name alphabetically. + List get sorted => [for (final target in this) target]..sort(); } diff --git a/pkgs/native_assets_cli/test/model/target_test.dart b/pkgs/native_assets_cli/test/model/target_test.dart index fc928ee45..e5ddc01ba 100644 --- a/pkgs/native_assets_cli/test/model/target_test.dart +++ b/pkgs/native_assets_cli/test/model/target_test.dart @@ -22,15 +22,15 @@ void main() { }); test('Target current', () async { - final current = TargetImpl.current; + final current = Target.current; expect(current.toString(), Abi.current().toString()); }); test('Target fromDartPlatform', () async { - final current = TargetImpl.fromDartPlatform(Platform.version); + final current = Target.fromDartPlatform(Platform.version); expect(current.toString(), Abi.current().toString()); expect( - () => TargetImpl.fromDartPlatform('bogus'), + () => Target.fromDartPlatform('bogus'), throwsA(predicate( (e) => e is FormatException && @@ -39,7 +39,7 @@ void main() { )), ); expect( - () => TargetImpl.fromDartPlatform( + () => Target.fromDartPlatform( '3.0.0 (be) (Wed Apr 5 14:19:42 2023 +0000) on "myfancyos_ia32"', ), throwsA(predicate( @@ -53,20 +53,19 @@ void main() { test('Target cross compilation', () async { // All hosts can cross compile to Android. - expect(TargetImpl.current.supportedTargetTargets(), - contains(TargetImpl.androidArm64)); - expect(TargetImpl.macOSArm64.supportedTargetTargets(), - contains(TargetImpl.iOSArm64)); + expect( + Target.current.supportedTargetTargets(), contains(Target.androidArm64)); + expect( + Target.macOSArm64.supportedTargetTargets(), contains(Target.iOSArm64)); }); test('Target fromArchitectureAndOs', () async { - final current = TargetImpl.fromArchitectureAndOs( - ArchitectureImpl.current, OSImpl.current); + final current = + Target.fromArchitectureAndOs(ArchitectureImpl.current, OSImpl.current); expect(current.toString(), Abi.current().toString()); expect( - () => TargetImpl.fromArchitectureAndOs( - ArchitectureImpl.arm, OSImpl.windows), + () => Target.fromArchitectureAndOs(ArchitectureImpl.arm, OSImpl.windows), throwsA(predicate( (e) => e is ArgumentError && From 51e71e66fa76508416272aaf68504a96219c1212 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 19:08:57 +0100 Subject: [PATCH 41/75] Add changelog entries and bump version --- pkgs/native_assets_builder/CHANGELOG.md | 2 +- pkgs/native_assets_cli/CHANGELOG.md | 5 +++-- pkgs/native_assets_cli/pubspec.yaml | 2 +- pkgs/native_toolchain_c/CHANGELOG.md | 4 ++-- pkgs/native_toolchain_c/pubspec.yaml | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pkgs/native_assets_builder/CHANGELOG.md b/pkgs/native_assets_builder/CHANGELOG.md index 343e554f3..ca1125660 100644 --- a/pkgs/native_assets_builder/CHANGELOG.md +++ b/pkgs/native_assets_builder/CHANGELOG.md @@ -1,6 +1,6 @@ ## 0.6.0-wip -- **Breaking change**: New API. +- **Breaking change** Completely rewritten API in `native_assets_cli`. ## 0.5.0 diff --git a/pkgs/native_assets_cli/CHANGELOG.md b/pkgs/native_assets_cli/CHANGELOG.md index 30906ef31..858b731cf 100644 --- a/pkgs/native_assets_cli/CHANGELOG.md +++ b/pkgs/native_assets_cli/CHANGELOG.md @@ -1,6 +1,7 @@ -## 0.4.3-wip +## 0.5.0-wip -- **Breaking change** New API. +- **Breaking change** Completely rewritten API. + https://github.com/dart-lang/native/pull/946 - Bump examples dependencies to path dependencies. ## 0.4.2 diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index 399757824..b11ee7cb1 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -4,7 +4,7 @@ description: >- native assets CLI. # Note: Bump BuildConfig.version and BuildOutput.version on breaking changes! -version: 0.4.3-wip +version: 0.5.0-wip repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli topics: diff --git a/pkgs/native_toolchain_c/CHANGELOG.md b/pkgs/native_toolchain_c/CHANGELOG.md index dd97f5c7b..9e7e56f96 100644 --- a/pkgs/native_toolchain_c/CHANGELOG.md +++ b/pkgs/native_toolchain_c/CHANGELOG.md @@ -1,6 +1,6 @@ -## 0.3.5-wip +## 0.4.0-wip -- Bump `package:native_assets_cli` to path dependency. +- **Breaking change** Completely rewritten API in `native_assets_cli`. ## 0.3.4+1 diff --git a/pkgs/native_toolchain_c/pubspec.yaml b/pkgs/native_toolchain_c/pubspec.yaml index 99b4101f2..42c8fdfc2 100644 --- a/pkgs/native_toolchain_c/pubspec.yaml +++ b/pkgs/native_toolchain_c/pubspec.yaml @@ -1,7 +1,7 @@ name: native_toolchain_c description: >- A library to invoke the native C compiler installed on the host machine. -version: 0.3.5-wip +version: 0.4.0-wip repository: https://github.com/dart-lang/native/tree/main/pkgs/native_toolchain_c topics: From 7ac882f9354d249c77a1b6eb6d7063becdd4f4ad Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 19:11:24 +0100 Subject: [PATCH 42/75] Use Dart casing `targetOs` -> `targetOS` --- .../lib/src/build_runner/build_runner.dart | 14 +++--- .../build_runner_reusability_test.dart | 4 +- .../test/build_runner/helpers.dart | 2 +- .../example/local_asset/build.dart | 2 +- pkgs/native_assets_cli/lib/src/api/build.dart | 2 +- .../lib/src/api/build_config.dart | 14 +++--- .../lib/src/api/ios_sdk.dart | 4 +- .../lib/src/helpers/asset_downloader.dart | 4 +- .../lib/src/model/build_config.dart | 30 ++++++------ .../lib/src/model/c_code_asset.dart | 2 +- .../lib/src/model/ios_sdk.dart | 4 +- .../lib/src/model/target.dart | 2 +- .../test/api/build_config_test.dart | 18 ++++---- .../test/api/build_test.dart | 4 +- .../test/model/build_config_test.dart | 46 +++++++++---------- .../test/model/build_output_test.dart | 2 +- .../test/model/target_test.dart | 6 +-- .../lib/src/cbuilder/cbuilder.dart | 8 ++-- .../lib/src/cbuilder/compiler_resolver.dart | 24 +++++----- .../lib/src/cbuilder/run_cbuilder.dart | 22 ++++----- .../cbuilder/cbuilder_build_failure_test.dart | 2 +- .../cbuilder/cbuilder_cross_android_test.dart | 2 +- .../cbuilder/cbuilder_cross_ios_test.dart | 6 +-- .../cbuilder_cross_linux_host_test.dart | 2 +- .../cbuilder_cross_macos_host_test.dart | 2 +- .../cbuilder_cross_windows_host_test.dart | 2 +- .../test/cbuilder/cbuilder_test.dart | 30 ++++++------ .../test/cbuilder/compiler_resolver_test.dart | 4 +- 28 files changed, 132 insertions(+), 132 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index c513c14e8..38125d5d2 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -133,7 +133,7 @@ class NativeAssetsBuildRunner { /// [runPackageName] are built. Future dryRun({ required LinkModePreferenceImpl linkModePreference, - required OSImpl targetOs, + required OSImpl targetOS, required Uri workingDirectory, required bool includeParentEnvironment, PackageLayout? packageLayout, @@ -169,7 +169,7 @@ class NativeAssetsBuildRunner { final config = await _cliConfigDryRun( packageName: package.name, packageRoot: packageLayout.packageRoot(package.name), - targetOs: targetOs, + targetOS: targetOS, linkMode: linkModePreference, buildParentDir: packageLayout.dartToolNativeAssetsBuilder, ); @@ -353,7 +353,7 @@ build_output.yaml contained a format error. final buildDirName = BuildConfigImpl.checksum( packageName: packageName, packageRoot: packageRoot, - targetOs: target.os, + targetOS: target.os, targetArchitecture: target.architecture, buildMode: buildMode, linkModePreference: linkMode, @@ -372,7 +372,7 @@ build_output.yaml contained a format error. outDir: outDirUri, packageName: packageName, packageRoot: packageRoot, - targetOs: target.os, + targetOS: target.os, targetArchitecture: target.architecture, buildMode: buildMode, linkModePreference: linkMode, @@ -386,11 +386,11 @@ build_output.yaml contained a format error. static Future _cliConfigDryRun({ required String packageName, required Uri packageRoot, - required OSImpl targetOs, + required OSImpl targetOS, required LinkModePreferenceImpl linkMode, required Uri buildParentDir, }) async { - final buildDirName = 'dry_run_${targetOs}_$linkMode'; + final buildDirName = 'dry_run_${targetOS}_$linkMode'; final outDirUri = buildParentDir.resolve('$buildDirName/out/'); final outDir = Directory.fromUri(outDirUri); if (!await outDir.exists()) { @@ -400,7 +400,7 @@ build_output.yaml contained a format error. outDir: outDirUri, packageName: packageName, packageRoot: packageRoot, - targetOs: targetOs, + targetOS: targetOS, linkModePreference: linkMode, ); } diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart index 57a955227..c7e84e926 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart @@ -29,13 +29,13 @@ void main() async { ); await buildRunner.dryRun( - targetOs: Target.current.os, + targetOS: Target.current.os, linkModePreference: LinkModePreferenceImpl.dynamic, workingDirectory: packageUri, includeParentEnvironment: true, ); await buildRunner.dryRun( - targetOs: Target.current.os, + targetOS: Target.current.os, linkModePreference: LinkModePreferenceImpl.dynamic, workingDirectory: packageUri, includeParentEnvironment: true, diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index 5b4a9254d..e830af761 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -92,7 +92,7 @@ Future dryRun( dartExecutable: dartExecutable, ).dryRun( linkModePreference: linkModePreference, - targetOs: Target.current.os, + targetOS: Target.current.os, workingDirectory: packageUri, includeParentEnvironment: includeParentEnvironment, packageLayout: packageLayout, diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 014f8c9b0..1af40c190 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -36,7 +36,7 @@ void main(List args) async { id: 'library:$packageName/asset.txt', file: assetPath, linkMode: LinkMode.dynamic, - os: config.targetOs, + os: config.targetOS, architecture: config.targetArchitecture, dynamicLoading: BundledDylib(), ), diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 0379fa92c..febd1ea50 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -72,7 +72,7 @@ import 'build_output.dart'; /// id: 'library:$packageName/asset.txt', /// file: assetPath, /// linkMode: LinkMode.dynamic, -/// os: config.targetOs, +/// os: config.targetOS, /// architecture: config.targetArchitecture, /// dynamicLoading: BundledDylib(), /// ), diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 770dabb90..b612f6733 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -51,11 +51,11 @@ abstract final class BuildConfig { Architecture? get targetArchitecture; /// The operating system being compiled for. - OS get targetOs; + OS get targetOS; /// When compiling for iOS, whether to target device or simulator. /// - /// Required when [targetOs] equals [OS.iOS]. + /// Required when [targetOS] equals [OS.iOS]. /// /// Not available during a [dryRun]. IOSSdk? get targetIOSSdk; @@ -63,7 +63,7 @@ abstract final class BuildConfig { /// When compiling for Android, the minimum Android SDK API version to that /// the compiled code will be compatible with. /// - /// Required when [targetOs] equals [OS.android]. + /// Required when [targetOS] equals [OS.android]. /// /// Not available during a [dryRun]. /// @@ -149,7 +149,7 @@ abstract final class BuildConfig { required Uri packageRoot, required BuildMode buildMode, required Architecture targetArchitecture, - required OS targetOs, + required OS targetOS, IOSSdk? targetIOSSdk, int? targetAndroidNdkApi, CCompilerConfig? cCompiler, @@ -163,7 +163,7 @@ abstract final class BuildConfig { packageRoot: packageRoot, buildMode: buildMode as BuildModeImpl, targetArchitecture: targetArchitecture as ArchitectureImpl, - targetOs: targetOs as OSImpl, + targetOS: targetOS as OSImpl, targetIOSSdk: targetIOSSdk as IOSSdkImpl?, targetAndroidNdkApi: targetAndroidNdkApi, cCompiler: cCompiler as CCompilerConfigImpl?, @@ -181,7 +181,7 @@ abstract final class BuildConfig { required Uri outDir, required String packageName, required Uri packageRoot, - required OS targetOs, + required OS targetOS, required LinkModePreference linkModePreference, Iterable? supportedAssetTypes, }) => @@ -189,7 +189,7 @@ abstract final class BuildConfig { outDir: outDir, packageName: packageName, packageRoot: packageRoot, - targetOs: targetOs as OSImpl, + targetOS: targetOS as OSImpl, linkModePreference: linkModePreference as LinkModePreferenceImpl, supportedAssetTypes: supportedAssetTypes, ); diff --git a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart index 152c82db6..26fc67051 100644 --- a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart +++ b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart @@ -12,7 +12,7 @@ abstract final class IOSSdk { /// /// The SDK location can be found on the host machine with /// `xcrun --sdk iphoneos --show-sdk-path`. - static const IOSSdk iPhoneOs = IOSSdkImpl.iPhoneOs; + static const IOSSdk iPhoneOS = IOSSdkImpl.iPhoneOS; /// The iphonesimulator SDK in Xcode. /// @@ -22,7 +22,7 @@ abstract final class IOSSdk { /// All known values for [IOSSdk]. static const values = [ - iPhoneOs, + iPhoneOS, iPhoneSimulator, ]; } diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart index 6c03f0092..e8d137f85 100644 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -44,7 +44,7 @@ class AssetDownloader implements Builder { Uri? targetUri; if (!config.dryRun) { final downloadUri2 = downloadUri( - config.targetOs, + config.targetOS, config.targetArchitecture!, ); final fileName = @@ -61,7 +61,7 @@ class AssetDownloader implements Builder { file: targetUri, linkMode: LinkMode.dynamic, dynamicLoading: BundledDylib(), - os: config.targetOs, + os: config.targetOS, architecture: config.targetArchitecture, ), ); diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 36195a953..2227b9874 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -22,8 +22,8 @@ final class BuildConfigImpl implements BuildConfig { late final ArchitectureImpl? _targetArchitecture; @override - OSImpl get targetOs => _targetOs; - late final OSImpl _targetOs; + OSImpl get targetOS => _targetOS; + late final OSImpl _targetOS; @override IOSSdkImpl? get targetIOSSdk { @@ -92,7 +92,7 @@ final class BuildConfigImpl implements BuildConfig { required Uri packageRoot, required BuildModeImpl buildMode, required ArchitectureImpl targetArchitecture, - required OSImpl targetOs, + required OSImpl targetOS, IOSSdkImpl? targetIOSSdk, int? targetAndroidNdkApi, CCompilerConfigImpl? cCompiler, @@ -107,7 +107,7 @@ final class BuildConfigImpl implements BuildConfig { .._packageRoot = packageRoot .._buildMode = buildMode .._targetArchitecture = targetArchitecture - .._targetOs = targetOs + .._targetOS = targetOS .._targetIOSSdk = targetIOSSdk .._targetAndroidNdkApi = targetAndroidNdkApi .._cCompiler = cCompiler ?? CCompilerConfigImpl() @@ -125,7 +125,7 @@ final class BuildConfigImpl implements BuildConfig { required Uri outDir, required String packageName, required Uri packageRoot, - required OSImpl targetOs, + required OSImpl targetOS, required LinkModePreferenceImpl linkModePreference, Iterable? supportedAssetTypes, }) { @@ -134,7 +134,7 @@ final class BuildConfigImpl implements BuildConfig { .._outDir = outDir .._packageName = packageName .._packageRoot = packageRoot - .._targetOs = targetOs + .._targetOS = targetOS .._targetArchitecture = null .._linkModePreference = linkModePreference .._cCompiler = CCompilerConfigImpl() @@ -157,7 +157,7 @@ final class BuildConfigImpl implements BuildConfig { required String packageName, required Uri packageRoot, required ArchitectureImpl targetArchitecture, - required OSImpl targetOs, + required OSImpl targetOS, required BuildModeImpl buildMode, IOSSdkImpl? targetIOSSdk, int? targetAndroidNdkApi, @@ -171,7 +171,7 @@ final class BuildConfigImpl implements BuildConfig { version ?? latestVersion, packageName, targetArchitecture.toString(), - targetOs.toString(), + targetOS.toString(), targetIOSSdk.toString(), targetAndroidNdkApi.toString(), buildMode.toString(), @@ -299,7 +299,7 @@ final class BuildConfigImpl implements BuildConfig { } }, (config) { - _targetOs = OSImpl.fromString( + _targetOS = OSImpl.fromString( config.string( OSImpl.configKey, validValues: OSImpl.values.map((e) => '$e'), @@ -317,7 +317,7 @@ final class BuildConfigImpl implements BuildConfig { ...ArchitectureImpl.values else for (final target in Target.values) - if (target.os == _targetOs) target.architecture + if (target.os == _targetOS) target.architecture ]; _targetArchitecture = ArchitectureImpl.fromString( config.string( @@ -331,7 +331,7 @@ final class BuildConfigImpl implements BuildConfig { if (dryRun) { _throwIfNotNullInDryRun(IOSSdkImpl.configKey); } else { - _targetIOSSdk = (osSet && _targetOs == OSImpl.iOS) + _targetIOSSdk = (osSet && _targetOS == OSImpl.iOS) ? IOSSdkImpl.fromString( config.string( IOSSdkImpl.configKey, @@ -345,7 +345,7 @@ final class BuildConfigImpl implements BuildConfig { if (dryRun) { _throwIfNotNullInDryRun(targetAndroidNdkApiConfigKey); } else { - _targetAndroidNdkApi = (osSet && _targetOs == OSImpl.android) + _targetAndroidNdkApi = (osSet && _targetOS == OSImpl.android) ? config.int(targetAndroidNdkApiConfigKey) : null; } @@ -456,7 +456,7 @@ final class BuildConfigImpl implements BuildConfig { outDirConfigKey: _outDir.toFilePath(), packageNameConfigKey: _packageName, packageRootConfigKey: _packageRoot.toFilePath(), - OSImpl.configKey: _targetOs.toString(), + OSImpl.configKey: _targetOS.toString(), LinkModePreferenceImpl.configKey: _linkModePreference.toString(), supportedAssetTypesKey: _supportedAssetTypes, _versionKey: latestVersion.toString(), @@ -490,7 +490,7 @@ final class BuildConfigImpl implements BuildConfig { if (other.packageName != packageName) return false; if (other.packageRoot != packageRoot) return false; if (other.dryRun != dryRun) return false; - if (other.targetOs != targetOs) return false; + if (other.targetOS != targetOS) return false; if (other.linkModePreference != linkModePreference) return false; if (!const DeepCollectionEquality() .equals(other._supportedAssetTypes, _supportedAssetTypes)) return false; @@ -511,7 +511,7 @@ final class BuildConfigImpl implements BuildConfig { outDir, packageName, packageRoot, - targetOs, + targetOS, linkModePreference, dryRun, const DeepCollectionEquality().hash(_supportedAssetTypes), diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index f89cc2f31..9b0976719 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -240,7 +240,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { _idKey: id, _linkModeKey: linkMode.name, _pathKey: dynamicLoading.toYaml(version, file), - _targetKey: Target.fromArchitectureAndOs(architecture!, os).toString(), + _targetKey: Target.fromArchitectureAndOS(architecture!, os).toString(), }..sortOnKey(); } return { diff --git a/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart b/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart index c50388562..9c20c9e12 100644 --- a/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart +++ b/pkgs/native_assets_cli/lib/src/model/ios_sdk.dart @@ -9,11 +9,11 @@ final class IOSSdkImpl implements IOSSdk { const IOSSdkImpl._(this.xcodebuildSdk); - static const iPhoneOs = IOSSdkImpl._('iphoneos'); + static const iPhoneOS = IOSSdkImpl._('iphoneos'); static const iPhoneSimulator = IOSSdkImpl._('iphonesimulator'); static const values = [ - iPhoneOs, + iPhoneOS, iPhoneSimulator, ]; diff --git a/pkgs/native_assets_cli/lib/src/model/target.dart b/pkgs/native_assets_cli/lib/src/model/target.dart index 44c749c0f..18cdc9775 100644 --- a/pkgs/native_assets_cli/lib/src/model/target.dart +++ b/pkgs/native_assets_cli/lib/src/model/target.dart @@ -35,7 +35,7 @@ final class Target implements Comparable { return target; } - factory Target.fromArchitectureAndOs( + factory Target.fromArchitectureAndOS( ArchitectureImpl architecture, OSImpl os, ) { diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 71dc4cc71..0fcddf4fc 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -52,8 +52,8 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, - targetOs: OS.iOS, - targetIOSSdk: IOSSdk.iPhoneOs, + targetOS: OS.iOS, + targetIOSSdk: IOSSdk.iPhoneOS, cCompiler: CCompilerConfig( cc: fakeClang, ld: fakeLd, @@ -69,7 +69,7 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetOS: OS.android, targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, @@ -81,7 +81,7 @@ void main() async { expect(config1.outDir != config2.outDir, true); expect(config1.packageRoot, config2.packageRoot); expect(config1.targetArchitecture == config2.targetArchitecture, true); - expect(config1.targetOs != config2.targetOs, true); + expect(config1.targetOS != config2.targetOS, true); expect(config1.targetIOSSdk != config2.targetIOSSdk, true); expect(config1.cCompiler.cc != config2.cCompiler.cc, true); expect(config1.cCompiler.ld != config2.cCompiler.ld, true); @@ -100,7 +100,7 @@ void main() async { packageName: packageName, packageRoot: packageRootUri, targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetOS: OS.android, targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, @@ -128,7 +128,7 @@ void main() async { outDir: outDirUri, packageName: packageName, packageRoot: packageRootUri, - targetOs: OS.android, + targetOS: OS.android, linkModePreference: LinkModePreference.preferStatic, supportedAssetTypes: [CCodeAsset.type], ); @@ -153,7 +153,7 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetOS: OS.android, targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, @@ -173,7 +173,7 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetOS: OS.android, targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, @@ -200,7 +200,7 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, - targetOs: OS.android, + targetOS: OS.android, targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, diff --git a/pkgs/native_assets_cli/test/api/build_test.dart b/pkgs/native_assets_cli/test/api/build_test.dart index 448e86169..731ad27ec 100644 --- a/pkgs/native_assets_cli/test/api/build_test.dart +++ b/pkgs/native_assets_cli/test/api/build_test.dart @@ -45,8 +45,8 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, - targetOs: OS.iOS, - targetIOSSdk: IOSSdk.iPhoneOs, + targetOS: OS.iOS, + targetIOSSdk: IOSSdk.iPhoneOS, cCompiler: CCompilerConfig( cc: fakeClang, ld: fakeLd, diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index fd47f567d..2ec858527 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -54,8 +54,8 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.iOS, - targetIOSSdk: IOSSdkImpl.iPhoneOs, + targetOS: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( cc: fakeClang, ld: fakeLd, @@ -70,7 +70,7 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.android, + targetOS: OSImpl.android, targetAndroidNdkApi: 30, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -81,7 +81,7 @@ void main() async { expect(config1.outDir != config2.outDir, true); expect(config1.packageRoot, config2.packageRoot); expect(config1.targetArchitecture == config2.targetArchitecture, true); - expect(config1.targetOs != config2.targetOs, true); + expect(config1.targetOS != config2.targetOS, true); expect(config1.targetIOSSdk != config2.targetIOSSdk, true); expect(config1.cCompiler.cc != config2.cCompiler.cc, true); expect(config1.cCompiler.ld != config2.cCompiler.ld, true); @@ -99,7 +99,7 @@ void main() async { packageName: packageName, packageRoot: packageRootUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.android, + targetOS: OSImpl.android, targetAndroidNdkApi: 30, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -127,7 +127,7 @@ void main() async { outDir: outDirUri, packageName: packageName, packageRoot: packageRootUri, - targetOs: OSImpl.android, + targetOS: OSImpl.android, linkModePreference: LinkModePreferenceImpl.preferStatic, ); @@ -151,8 +151,8 @@ void main() async { packageName: packageName, packageRoot: packageRootUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.iOS, - targetIOSSdk: IOSSdkImpl.iPhoneOs, + targetOS: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( cc: fakeClang, ld: fakeLd, @@ -173,7 +173,7 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.android, + targetOS: OSImpl.android, targetAndroidNdkApi: 30, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -193,7 +193,7 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.android, + targetOS: OSImpl.android, targetAndroidNdkApi: 30, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -219,8 +219,8 @@ void main() async { packageName: packageName, packageRoot: tempUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.iOS, - targetIOSSdk: IOSSdkImpl.iPhoneOs, + targetOS: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( cc: fakeClang, ld: fakeLd, @@ -298,8 +298,8 @@ version: 1.0.0'''; packageName: packageName, packageRoot: tempUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.iOS, - targetIOSSdk: IOSSdkImpl.iPhoneOs, + targetOS: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( cc: fakeClang, ld: fakeLd, @@ -418,8 +418,8 @@ version: 1.0.0'''; packageName: packageName, packageRoot: tempUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.iOS, - targetIOSSdk: IOSSdkImpl.iPhoneOs, + targetOS: OSImpl.iOS, + targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( cc: fakeClang, ld: fakeLd, @@ -436,7 +436,7 @@ version: 1.0.0'''; packageName: packageName, packageRoot: tempUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.android, + targetOS: OSImpl.android, targetAndroidNdkApi: 30, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -458,7 +458,7 @@ version: 1.0.0'''; packageName: packageName, packageRoot: tempUri, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.android, + targetOS: OSImpl.android, targetAndroidNdkApi: 30, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -486,7 +486,7 @@ version: 1.0.0'''; packageName: packageName, packageRoot: packageRootUri, targetArchitecture: ArchitectureImpl.x64, - targetOs: OSImpl.windows, + targetOS: OSImpl.windows, cCompiler: CCompilerConfigImpl( cc: fakeCl, envScript: fakeVcVars, @@ -535,7 +535,7 @@ version: 1.0.0'''; packageName: packageName, packageRoot: nativeAddUri, targetArchitecture: ArchitectureImpl.x64, - targetOs: OSImpl.linux, + targetOS: OSImpl.linux, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.dynamic, supportedAssetTypes: [CCodeAsset.type], @@ -549,7 +549,7 @@ version: 1.0.0'''; packageName: packageName, packageRoot: nativeAddUri, targetArchitecture: ArchitectureImpl.x64, - targetOs: OSImpl.linux, + targetOS: OSImpl.linux, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.dynamic, dependencyMetadata: { @@ -564,7 +564,7 @@ version: 1.0.0'''; packageName: packageName, packageRoot: nativeAddUri, targetArchitecture: ArchitectureImpl.x64, - targetOs: OSImpl.linux, + targetOS: OSImpl.linux, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.dynamic, cCompiler: CCompilerConfigImpl( @@ -657,7 +657,7 @@ version: 1.0.0'''; packageName: packageName, outDir: outDirUri, packageRoot: tempUri, - targetOs: OSImpl.windows, + targetOS: OSImpl.windows, linkModePreference: LinkModePreferenceImpl.dynamic, ); buildConfig.toYamlString(); diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index dc60ddcc7..4cc2216a6 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -174,7 +174,7 @@ version: ${BuildOutputImpl.latestVersion}'''; packageRoot: packageRoot, buildMode: BuildModeImpl.debug, targetArchitecture: ArchitectureImpl.arm64, - targetOs: OSImpl.macOS, + targetOS: OSImpl.macOS, linkModePreference: LinkModePreferenceImpl.dynamic, ); await buildOutput.writeToFile(config: config); diff --git a/pkgs/native_assets_cli/test/model/target_test.dart b/pkgs/native_assets_cli/test/model/target_test.dart index e5ddc01ba..4c56d7402 100644 --- a/pkgs/native_assets_cli/test/model/target_test.dart +++ b/pkgs/native_assets_cli/test/model/target_test.dart @@ -59,13 +59,13 @@ void main() { Target.macOSArm64.supportedTargetTargets(), contains(Target.iOSArm64)); }); - test('Target fromArchitectureAndOs', () async { + test('Target fromArchitectureAndOS', () async { final current = - Target.fromArchitectureAndOs(ArchitectureImpl.current, OSImpl.current); + Target.fromArchitectureAndOS(ArchitectureImpl.current, OSImpl.current); expect(current.toString(), Abi.current().toString()); expect( - () => Target.fromArchitectureAndOs(ArchitectureImpl.arm, OSImpl.windows), + () => Target.fromArchitectureAndOS(ArchitectureImpl.arm, OSImpl.windows), throwsA(predicate( (e) => e is ArgumentError && diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 6250ab690..2fcc95b68 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -44,7 +44,7 @@ class CBuilder implements Builder { /// Name of the library or executable to build. /// - /// The filename will be decided by [BuildConfig.targetOs] and + /// The filename will be decided by [BuildConfig.targetOS] and /// [OS.libraryFileName] or [OS.executableFileName]. /// /// File will be placed in [BuildConfig.outDir]. @@ -201,9 +201,9 @@ class CBuilder implements Builder { await Directory.fromUri(outDir).create(recursive: true); final linkMode = buildConfig.linkModePreference.preferredLinkMode; final libUri = - outDir.resolve(buildConfig.targetOs.libraryFileName(name, linkMode)); + outDir.resolve(buildConfig.targetOS.libraryFileName(name, linkMode)); final exeUri = - outDir.resolve(buildConfig.targetOs.executableFileName(name)); + outDir.resolve(buildConfig.targetOS.executableFileName(name)); final sources = [ for (final source in this.sources) packageRoot.resolveUri(Uri.file(source)), @@ -252,7 +252,7 @@ class CBuilder implements Builder { id: assetId!, file: buildConfig.dryRun ? null : libUri, linkMode: linkMode, - os: buildConfig.targetOs, + os: buildConfig.targetOS, architecture: buildConfig.dryRun ? null : buildConfig.targetArchitecture, dynamicLoading: BundledDylib(), diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart index dc5301d62..b721da274 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart @@ -47,26 +47,26 @@ class CompilerResolver { return result; } - final targetOs = buildConfig.targetOs; + final targetOS = buildConfig.targetOS; final targetArchitecture = buildConfig.targetArchitecture; final errorMessage = "No tools configured on host '${hostOS}_$hostArchitecture' with target " - "'${targetOs}_$targetArchitecture'."; + "'${targetOS}_$targetArchitecture'."; logger?.severe(errorMessage); throw ToolError(errorMessage); } /// Select the right compiler for cross compiling to the specified target. Tool? _selectCompiler() { - final targetOs = buildConfig.targetOs; + final targetOS = buildConfig.targetOS; final targetArch = buildConfig.targetArchitecture; // TODO(dacoharkes): Support falling back on other tools. if (targetArch == hostArchitecture && - targetOs == hostOS && + targetOS == hostOS && hostOS == OS.linux) return clang; - if (targetOs == OS.macOS || targetOs == OS.iOS) return appleClang; - if (targetOs == OS.android) return androidNdkClang; + if (targetOS == OS.macOS || targetOS == OS.iOS) return appleClang; + if (targetOS == OS.android) return androidNdkClang; if (hostOS == OS.linux) { switch (targetArch) { case Architecture.arm: @@ -131,28 +131,28 @@ class CompilerResolver { return result; } - final targetOs = buildConfig.targetOs; + final targetOS = buildConfig.targetOS; final targetArchitecture = buildConfig.targetArchitecture; final errorMessage = "No tools configured on host '${hostOS}_$hostArchitecture' with target " - "'${targetOs}_$targetArchitecture'."; + "'${targetOS}_$targetArchitecture'."; logger?.severe(errorMessage); throw ToolError(errorMessage); } /// Select the right archiver for cross compiling to the specified target. Tool? _selectArchiver() { - final targetOs = buildConfig.targetOs; + final targetOS = buildConfig.targetOS; final targetArchitecture = buildConfig.targetArchitecture; // TODO(dacoharkes): Support falling back on other tools. if (targetArchitecture == hostArchitecture && - targetOs == hostOS && + targetOS == hostOS && hostOS == OS.linux) { return llvmAr; } - if (targetOs == OS.macOS || targetOs == OS.iOS) return appleAr; - if (targetOs == OS.android) return androidNdkLlvmAr; + if (targetOS == OS.macOS || targetOS == OS.iOS) return appleAr; + if (targetOS == OS.android) return androidNdkLlvmAr; if (hostOS == OS.linux) { switch (targetArchitecture) { case Architecture.arm: diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart index 665079a13..bb231b639 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart @@ -62,7 +62,7 @@ class RunCBuilder { .whereType() .length == 1) { - if (buildConfig.targetOs == OS.windows && cppLinkStdLib != null) { + if (buildConfig.targetOS == OS.windows && cppLinkStdLib != null) { throw ArgumentError.value( cppLinkStdLib, 'cppLinkStdLib', @@ -79,7 +79,7 @@ class RunCBuilder { Future archiver() async => (await _resolver.resolveArchiver()).uri; Future iosSdk(IOSSdk iosSdk, {required Logger? logger}) async { - if (iosSdk == IOSSdk.iPhoneOs) { + if (iosSdk == IOSSdk.iPhoneOS) { return (await iPhoneOSSdk.defaultResolver!.resolve(logger: logger)) .where((i) => i.tool == iPhoneOSSdk) .first @@ -122,7 +122,7 @@ class RunCBuilder { } late final IOSSdk targetIosSdk; - if (buildConfig.targetOs == OS.iOS) { + if (buildConfig.targetOS == OS.iOS) { targetIosSdk = buildConfig.targetIOSSdk!; } @@ -130,7 +130,7 @@ class RunCBuilder { // invoking clang. Mimic that behavior here. // See https://github.com/dart-lang/native/issues/171. late final int targetAndroidNdkApi; - if (buildConfig.targetOs == OS.android) { + if (buildConfig.targetOS == OS.android) { final minimumApi = buildConfig.targetArchitecture == Architecture.riscv64 ? 35 : 21; targetAndroidNdkApi = max(buildConfig.targetAndroidNdkApi!, minimumApi); @@ -141,21 +141,21 @@ class RunCBuilder { await runProcess( executable: compiler.uri, arguments: [ - if (buildConfig.targetOs == OS.android) ...[ + if (buildConfig.targetOS == OS.android) ...[ '--target=' '${androidNdkClangTargetFlags[architecture]!}' '$targetAndroidNdkApi', '--sysroot=${androidSysroot(compiler).toFilePath()}', ], - if (buildConfig.targetOs == OS.macOS) + if (buildConfig.targetOS == OS.macOS) '--target=${appleClangMacosTargetFlags[architecture]!}', - if (buildConfig.targetOs == OS.iOS) + if (buildConfig.targetOS == OS.iOS) '--target=${appleClangIosTargetFlags[architecture]![targetIosSdk]!}', - if (buildConfig.targetOs == OS.iOS) ...[ + if (buildConfig.targetOS == OS.iOS) ...[ '-isysroot', (await iosSdk(targetIosSdk, logger: logger)).toFilePath(), ], - if (buildConfig.targetOs == OS.macOS) ...[ + if (buildConfig.targetOS == OS.macOS) ...[ '-isysroot', (await macosSdk(logger: logger)).toFilePath(), ], @@ -190,7 +190,7 @@ class RunCBuilder { '-x', 'c++', '-l', - cppLinkStdLib ?? defaultCppLinkStdLib[buildConfig.targetOs]! + cppLinkStdLib ?? defaultCppLinkStdLib[buildConfig.targetOS]! ], ...flags, for (final MapEntry(key: name, :value) in defines.entries) @@ -307,7 +307,7 @@ class RunCBuilder { static const appleClangIosTargetFlags = { Architecture.arm64: { - IOSSdk.iPhoneOs: 'arm64-apple-ios', + IOSSdk.iPhoneOS: 'arm64-apple-ios', IOSSdk.iPhoneSimulator: 'arm64-apple-ios-simulator', }, Architecture.x64: { diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart index f2a73c4e1..5cf28c543 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart @@ -34,7 +34,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, linkModePreference: LinkModePreference.dynamic, buildMode: BuildMode.release, cCompiler: CCompilerConfig( diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart index 340d0640e..a20a5090b 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart @@ -129,7 +129,7 @@ Future buildLib( packageName: name, packageRoot: tempUri, targetArchitecture: targetArchitecture, - targetOs: OS.android, + targetOS: OS.android, targetAndroidNdkApi: androidNdkApi, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart index 6b66ff64f..1cac73a5c 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart @@ -39,7 +39,7 @@ void main() { for (final linkMode in LinkMode.values) { for (final targetIOSSdk in IOSSdk.values) { for (final target in targets) { - if (target == Architecture.x64 && targetIOSSdk == IOSSdk.iPhoneOs) { + if (target == Architecture.x64 && targetIOSSdk == IOSSdk.iPhoneOS) { continue; } @@ -61,7 +61,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: target, - targetOs: OS.iOS, + targetOS: OS.iOS, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic ? LinkModePreference.dynamic @@ -100,7 +100,7 @@ void main() { logger: logger, ); expect(otoolResult.exitCode, 0); - if (targetIOSSdk == IOSSdk.iPhoneOs || target == Architecture.x64) { + if (targetIOSSdk == IOSSdk.iPhoneOS || target == Architecture.x64) { // The x64 simulator behaves as device, presumably because the // devices are never x64. expect(otoolResult.stdout, contains('LC_VERSION_MIN_IPHONEOS')); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart index ea86fff59..593539158 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart @@ -49,7 +49,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: target, - targetOs: OS.linux, + targetOS: OS.linux, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic ? LinkModePreference.dynamic diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart index e2a5ec114..cd363b988 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart @@ -47,7 +47,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: target, - targetOs: OS.macOS, + targetOS: OS.macOS, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic ? LinkModePreference.dynamic diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart index 9bc3ad166..3d1d5a998 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart @@ -58,7 +58,7 @@ void main() { outDir: tempUri, packageName: name, packageRoot: tempUri, - targetOs: OS.windows, + targetOS: OS.windows, targetArchitecture: target, buildMode: BuildMode.release, linkModePreference: linkMode == LinkMode.dynamic diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart index d9b65ad05..9a96856b8 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart @@ -48,7 +48,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, buildMode: buildMode, // Ignored by executables. linkModePreference: LinkModePreference.dynamic, @@ -87,7 +87,7 @@ void main() { (message) => message.contains(helloWorldCUri.toFilePath()), ); - switch ((buildConfig.targetOs, pic)) { + switch ((buildConfig.targetOS, pic)) { case (OS.windows, _) || (_, null): expect(compilerInvocation, isNot(contains('-fPIC'))); expect(compilerInvocation, isNot(contains('-fPIE'))); @@ -119,7 +119,7 @@ void main() { outDir: tempUri, packageName: name, packageRoot: tempUri, - targetOs: OS.current, + targetOS: OS.current, linkModePreference: LinkModePreference.dynamic, ) : BuildConfig( @@ -127,7 +127,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( @@ -161,7 +161,7 @@ void main() { final compilerInvocation = logMessages.singleWhere( (message) => message.contains(addCUri.toFilePath()), ); - switch ((buildConfig.targetOs, pic)) { + switch ((buildConfig.targetOS, pic)) { case (OS.windows, _) || (_, null): expect(compilerInvocation, isNot(contains('-fPIC'))); expect(compilerInvocation, isNot(contains('-fPIE'))); @@ -219,7 +219,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, buildMode: BuildMode.release, // Ignored by executables. linkModePreference: LinkModePreference.dynamic, @@ -231,7 +231,7 @@ void main() { ); final buildOutput = BuildOutput(); - final flag = switch (buildConfig.targetOs) { + final flag = switch (buildConfig.targetOS) { OS.windows => '/DFOO=USER_FLAG', _ => '-DFOO=USER_FLAG', }; @@ -277,7 +277,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( @@ -322,7 +322,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( @@ -333,7 +333,7 @@ void main() { ); final buildOutput = BuildOutput(); - final stdFlag = switch (buildConfig.targetOs) { + final stdFlag = switch (buildConfig.targetOS) { OS.windows => '/std:$std', _ => '-std=$std', }; @@ -381,7 +381,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, // Ignored by executables. linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( @@ -392,7 +392,7 @@ void main() { ); final buildOutput = BuildOutput(); - final defaultStdLibLinkFlag = switch (buildConfig.targetOs) { + final defaultStdLibLinkFlag = switch (buildConfig.targetOS) { OS.windows => null, OS.linux => '-l stdc++', OS.macOS => '-l c++', @@ -445,7 +445,7 @@ void main() { packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, // Ignored by executables. linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( @@ -462,7 +462,7 @@ void main() { cppLinkStdLib: 'stdc++', ); - if (buildConfig.targetOs == OS.windows) { + if (buildConfig.targetOS == OS.windows) { await expectLater( () => cbuilder.run( buildConfig: buildConfig, @@ -515,7 +515,7 @@ Future testDefines({ packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, buildMode: buildMode, // Ignored by executables. linkModePreference: LinkModePreference.dynamic, diff --git a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart index 7421248f8..0a256e986 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart @@ -45,7 +45,7 @@ void main() { packageName: 'dummy', packageRoot: tempUri, targetArchitecture: Architecture.current, - targetOs: OS.current, + targetOS: OS.current, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( @@ -69,7 +69,7 @@ void main() { packageName: 'dummy', packageRoot: tempUri, targetArchitecture: Architecture.arm64, - targetOs: OS.windows, + targetOS: OS.windows, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, ); From 300c206131f8b551a035772222d67c6f781bab25 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 21 Feb 2024 19:27:17 +0100 Subject: [PATCH 43/75] data asset test coverage --- pkgs/native_assets_cli/test/api/asset_test.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index f56577b86..6ae2c6b0e 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_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.dart'; +import 'package:native_assets_cli/src/api/asset.dart'; import 'package:test/test.dart'; void main() { @@ -53,6 +54,10 @@ void main() { architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), + DataAsset( + id: 'package:my_package/data/some_text.txt', + file: Uri(path: 'data/some_text.txt'), + ), ]; assets.toString(); }); From 43ce1754bacdfdb21bb0cdc17d7f12f9b02a6ea3 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 23 Feb 2024 17:54:05 +0100 Subject: [PATCH 44/75] Address comment --- .../lib/native_assets_cli_internal.dart | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index 7ec4cd347..1b6038eff 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -2,9 +2,17 @@ // 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. -/// This library should not be used by `build.dart` scripts. +/// Internal API for invoking `build.dart` hooks. +/// +/// This library is intended for use by: +/// * `package:native_assets_builder`, +/// * `dartdev` (in the Dart SDK), and, +/// * `flutter_tools` (in the Flutter SDK). +/// +/// > [!CAUTION] +/// > Unless you are building a custom SDK that embeds Dart you should +/// > not be importing this library! /// -/// Only `package:native_assets_builder` should use this. /// @nodoc library native_assets_cli_internal; From f697827c9237167bd760fe9728dd1b9b1fe8fd02 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 23 Feb 2024 17:55:33 +0100 Subject: [PATCH 45/75] Address comment --- pkgs/native_assets_cli/lib/src/api/asset.dart | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index 419e2a25d..6a804ba69 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -32,13 +32,12 @@ abstract final class Asset { /// An [Asset] must have a string identifier called "asset id". Dart code that /// uses an asset, references the asset using this asset id. /// - /// A package must prefix all asset ids it defines with: - /// `package:/`, `` being the current package's name. This - /// ensures assets don't conflict between packages. + /// A package must prefix all asset ids it defines with: `package:/`, + /// `` being the current package's name. This ensures assets don't + /// conflict between packages. /// - /// Additionally, the convention is that an asset referenced from - /// `lib/src/foo.dart` in `package:foo` has the asset id - /// `'package:foo/src/foo.dart'`. + /// Additionally, the default asset id for an asset reference from + /// `lib/src/foo.dart` is `'package:foo/src/foo.dart'`. String get id; /// The file to be bundled with the Dart or Flutter application. From 5ba7e7782f9a6abdbc5ab9a2b334f2b42f255355 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 26 Feb 2024 18:27:11 +0100 Subject: [PATCH 46/75] Address comments --- .../lib/src/build_runner/build_runner.dart | 4 +- .../test_data/cyclic_package_1/build.dart | 6 +-- .../test_data/cyclic_package_2/build.dart | 6 +-- .../test_data/native_add/build.dart | 40 +++++++++--------- .../native_add_add_source/build.dart | 42 +++++++++---------- .../test_data/native_subtract/build.dart | 40 +++++++++--------- .../package_reading_metadata/build.dart | 4 +- .../package_with_metadata/build.dart | 12 +++--- .../test_data/wrong_build_output/build.dart | 3 +- .../test_data/wrong_build_output_2/build.dart | 3 +- .../test_data/wrong_build_output_3/build.dart | 3 +- .../wrong_namespace_asset/build.dart | 14 +++---- .../example/local_asset/build.dart | 2 +- .../example/use_dart_api/build.dart | 42 +++++++++---------- pkgs/native_assets_cli/lib/src/api/build.dart | 8 ++-- .../lib/src/api/build_config.dart | 35 +++++++++++----- .../lib/src/api/build_output.dart | 21 ++++------ .../lib/src/helpers/asset_downloader.dart | 2 +- .../lib/src/model/build_config.dart | 18 ++++---- .../lib/src/model/build_output.dart | 5 +-- .../test/api/build_config_test.dart | 20 ++++----- .../test/api/build_test.dart | 2 +- .../test/model/build_config_test.dart | 2 +- .../lib/src/cbuilder/cbuilder.dart | 4 +- .../lib/src/cbuilder/run_cbuilder.dart | 2 +- .../cbuilder/cbuilder_build_failure_test.dart | 2 +- .../cbuilder/cbuilder_cross_android_test.dart | 2 +- .../cbuilder/cbuilder_cross_ios_test.dart | 2 +- .../cbuilder_cross_linux_host_test.dart | 2 +- .../cbuilder_cross_macos_host_test.dart | 2 +- .../cbuilder_cross_windows_host_test.dart | 2 +- .../test/cbuilder/cbuilder_test.dart | 16 +++---- .../test/cbuilder/compiler_resolver_test.dart | 4 +- 33 files changed, 187 insertions(+), 185 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 38125d5d2..15f589ea9 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -215,7 +215,7 @@ class NativeAssetsBuildRunner { bool includeParentEnvironment, ) async { final packageName = config.packageName; - final outDir = config.outDir; + final outDir = config.outputDirectory; if (!await Directory.fromUri(outDir).exists()) { await Directory.fromUri(outDir).create(recursive: true); } @@ -253,7 +253,7 @@ class NativeAssetsBuildRunner { bool includeParentEnvironment, { required bool dryRun, }) async { - final outDir = config.outDir; + final outDir = config.outputDirectory; final configFile = outDir.resolve('../config.yaml'); final buildDotDart = config.packageRoot.resolve('build.dart'); final configFileContents = config.toYamlString(); diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_1/build.dart b/pkgs/native_assets_builder/test_data/cyclic_package_1/build.dart index 1756bf9e4..291cc50bd 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_1/build.dart +++ b/pkgs/native_assets_builder/test_data/cyclic_package_1/build.dart @@ -4,8 +4,6 @@ import 'package:native_assets_cli/native_assets_cli.dart'; -void main(List args) async { - final config = await BuildConfig.fromArgs(args); - final output = BuildOutput(); - await output.writeToFile(config: config); +void main(List arguments) async { + await build(arguments, (config, output) async {}); } diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_2/build.dart b/pkgs/native_assets_builder/test_data/cyclic_package_2/build.dart index 1756bf9e4..291cc50bd 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_2/build.dart +++ b/pkgs/native_assets_builder/test_data/cyclic_package_2/build.dart @@ -4,8 +4,6 @@ import 'package:native_assets_cli/native_assets_cli.dart'; -void main(List args) async { - final config = await BuildConfig.fromArgs(args); - final output = BuildOutput(); - await output.writeToFile(config: config); +void main(List arguments) async { + await build(arguments, (config, output) async {}); } diff --git a/pkgs/native_assets_builder/test_data/native_add/build.dart b/pkgs/native_assets_builder/test_data/native_add/build.dart index 75286846f..a3d47b362 100644 --- a/pkgs/native_assets_builder/test_data/native_add/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add/build.dart @@ -8,24 +8,24 @@ import 'package:native_toolchain_c/native_toolchain_c.dart'; const packageName = 'native_add'; -void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - final buildOutput = BuildOutput(); - final cbuilder = CBuilder.library( - name: packageName, - assetId: 'package:$packageName/src/${packageName}_bindings_generated.dart', - sources: [ - 'src/$packageName.c', - ], - ); - await cbuilder.run( - buildConfig: buildConfig, - buildOutput: buildOutput, - logger: Logger('') - ..level = Level.ALL - ..onRecord.listen((record) { - print('${record.level.name}: ${record.time}: ${record.message}'); - }), - ); - await buildOutput.writeToFile(config: buildConfig); +void main(List arguments) async { + await build(arguments, (config, output) async { + final cbuilder = CBuilder.library( + name: packageName, + assetId: + 'package:$packageName/src/${packageName}_bindings_generated.dart', + sources: [ + 'src/$packageName.c', + ], + ); + await cbuilder.run( + buildConfig: config, + buildOutput: 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_add_source/build.dart b/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart index 3970445f2..33cef46bd 100644 --- a/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart @@ -8,25 +8,25 @@ import 'package:native_toolchain_c/native_toolchain_c.dart'; const packageName = 'native_add'; -void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - final buildOutput = BuildOutput(); - final cbuilder = CBuilder.library( - name: packageName, - assetId: 'package:$packageName/src/${packageName}_bindings_generated.dart', - sources: [ - 'src/$packageName.c', - 'src/native_multiply.c', - ], - ); - await cbuilder.run( - buildConfig: buildConfig, - buildOutput: buildOutput, - logger: Logger('') - ..level = Level.ALL - ..onRecord.listen((record) { - print('${record.level.name}: ${record.time}: ${record.message}'); - }), - ); - await buildOutput.writeToFile(config: buildConfig); +void main(List arguments) async { + await build(arguments, (config, output) async { + final cbuilder = CBuilder.library( + name: packageName, + assetId: + 'package:$packageName/src/${packageName}_bindings_generated.dart', + sources: [ + 'src/$packageName.c', + 'src/native_multiply.c', + ], + ); + await cbuilder.run( + buildConfig: config, + buildOutput: 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_subtract/build.dart b/pkgs/native_assets_builder/test_data/native_subtract/build.dart index 227987771..6dae032c0 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/build.dart +++ b/pkgs/native_assets_builder/test_data/native_subtract/build.dart @@ -8,24 +8,24 @@ import 'package:native_toolchain_c/native_toolchain_c.dart'; const packageName = 'native_subtract'; -void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - final buildOutput = BuildOutput(); - final cbuilder = CBuilder.library( - name: packageName, - assetId: 'package:$packageName/src/${packageName}_bindings_generated.dart', - sources: [ - 'src/$packageName.c', - ], - ); - await cbuilder.run( - buildConfig: buildConfig, - buildOutput: buildOutput, - logger: Logger('') - ..level = Level.ALL - ..onRecord.listen((record) { - print('${record.level.name}: ${record.time}: ${record.message}'); - }), - ); - await buildOutput.writeToFile(config: buildConfig); +void main(List arguments) async { + await build(arguments, (config, output) async { + final cbuilder = CBuilder.library( + name: packageName, + assetId: + 'package:$packageName/src/${packageName}_bindings_generated.dart', + sources: [ + 'src/$packageName.c', + ], + ); + await cbuilder.run( + buildConfig: config, + buildOutput: 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/package_reading_metadata/build.dart b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart index e03f24cde..06a6d27ad 100644 --- a/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart +++ b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart @@ -2,12 +2,10 @@ // 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:cli_config/cli_config.dart'; import 'package:native_assets_cli/native_assets_cli.dart'; void main(List args) async { - final config = await Config.fromArgs(args: args); - final buildConfig = BuildConfig.fromConfig(config); + final buildConfig = await BuildConfig.fromArguments(args); if (!buildConfig.dryRun) { final someValue = buildConfig.metadatum('package_with_metadata', 'some_key'); diff --git a/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart b/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart index 8f54c9143..87be9a0da 100644 --- a/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart +++ b/pkgs/native_assets_builder/test_data/package_with_metadata/build.dart @@ -4,13 +4,11 @@ import 'package:native_assets_cli/native_assets_cli.dart'; -void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - final buildOutput = BuildOutput( - metadata: { +void main(List arguments) async { + await build(arguments, (config, output) async { + output.addMetadata({ 'some_key': 'some_value', 'some_int': 3, - }, - ); - await buildOutput.writeToFile(config: buildConfig); + }); + }); } diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart index dd16fa64d..e5d70370d 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart @@ -8,7 +8,8 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { final buildConfig = await BuildConfigImpl.fromArgs(args); - await File.fromUri(buildConfig.outDir.resolve(BuildOutputImpl.fileName)) + await File.fromUri( + buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) .writeAsString(_wrongContents); } diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart index f09a82313..d2f8f45bd 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart @@ -8,7 +8,8 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { final buildConfig = await BuildConfigImpl.fromArgs(args); - await File.fromUri(buildConfig.outDir.resolve(BuildOutputImpl.fileName)) + await File.fromUri( + buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) .writeAsString(_wrongContents); } diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart index 4483cf8ce..6963e133d 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart @@ -8,7 +8,8 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { final buildConfig = await BuildConfigImpl.fromArgs(args); - await File.fromUri(buildConfig.outDir.resolve(BuildOutputImpl.fileName)) + await File.fromUri( + buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) .writeAsString(_rightContents); exit(1); } diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 033dd2f58..76e8cc2e9 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -4,13 +4,12 @@ import 'package:native_assets_cli/native_assets_cli.dart'; -void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - final buildOutput = BuildOutput( - assets: [ +void main(List arguments) async { + await build(arguments, (config, output) async { + output.addAsset( CCodeAsset( id: 'package:other_package/foo', - file: buildConfig.outDir.resolve( + file: config.outputDirectory.resolve( OS.current.dylibFileName('foo'), ), linkMode: LinkMode.dynamic, @@ -18,7 +17,6 @@ void main(List args) async { architecture: Architecture.current, dynamicLoading: BundledDylib(), ), - ], - ); - await buildOutput.writeToFile(config: buildConfig); + ); + }); } diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 1af40c190..36306a060 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -19,7 +19,7 @@ void main(List args) async { } final packageName = config.packageName; - final assetPath = config.outDir.resolve(assetName); + final assetPath = config.outputDirectory.resolve(assetName); final assetSourcePath = config.packageRoot.resolveUri(packageAssetPath); if (!config.dryRun) { // Insert code that downloads or builds the asset to `assetPath`. diff --git a/pkgs/native_assets_cli/example/use_dart_api/build.dart b/pkgs/native_assets_cli/example/use_dart_api/build.dart index d4ced1fe3..61977ddaa 100644 --- a/pkgs/native_assets_cli/example/use_dart_api/build.dart +++ b/pkgs/native_assets_cli/example/use_dart_api/build.dart @@ -8,25 +8,25 @@ import 'package:native_toolchain_c/native_toolchain_c.dart'; const packageName = 'use_dart_api'; -void main(List args) async { - final buildConfig = await BuildConfig.fromArgs(args); - final buildOutput = BuildOutput(); - final cbuilder = CBuilder.library( - name: packageName, - assetId: 'package:$packageName/src/${packageName}_bindings_generated.dart', - sources: [ - 'src/$packageName.c', - 'src/dart_api_dl.c', - ], - ); - await cbuilder.run( - buildConfig: buildConfig, - buildOutput: buildOutput, - logger: Logger('') - ..level = Level.ALL - ..onRecord.listen((record) { - print('${record.level.name}: ${record.time}: ${record.message}'); - }), - ); - await buildOutput.writeToFile(config: buildConfig); +void main(List arguments) async { + await build(arguments, (config, output) async { + final cbuilder = CBuilder.library( + name: packageName, + assetId: + 'package:$packageName/src/${packageName}_bindings_generated.dart', + sources: [ + 'src/$packageName.c', + 'src/dart_api_dl.c', + ], + ); + await cbuilder.run( + buildConfig: config, + buildOutput: output, + logger: Logger('') + ..level = Level.ALL + ..onRecord.listen((record) { + print('${record.level.name}: ${record.time}: ${record.message}'); + }), + ); + }); } diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index febd1ea50..5b8759c02 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -81,11 +81,11 @@ import 'build_output.dart'; /// } /// ``` Future build( - List commandlineArguments, - Future Function(BuildConfig, BuildOutput) builder, + List arguments, + Future Function(BuildConfig config, BuildOutput output) builder, ) async { - final config = await BuildConfig.fromArgs(commandlineArguments); - final output = BuildOutput(); + final config = await BuildConfig.fromArguments(arguments); + final output = BuildOutputImpl(); await builder(config, output); await output.writeToFile(config: config); } diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index b612f6733..309bbcf8e 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -18,6 +18,7 @@ import 'architecture.dart'; import 'asset.dart'; import 'build_mode.dart'; import 'ios_sdk.dart'; +import 'link_mode.dart'; import 'link_mode_preference.dart'; import 'os.dart'; @@ -34,7 +35,7 @@ part '../model/c_compiler_config.dart'; abstract final class BuildConfig { /// The directory in which all output and intermediate artifacts should be /// placed. - Uri get outDir; + Uri get outputDirectory; /// The name of the package the native assets are built for. String get packageName; @@ -72,7 +73,7 @@ abstract final class BuildConfig { /// in the Android documentation. int? get targetAndroidNdkApi; - /// The preferred linkMode method for [CCodeAsset]s. + /// The preferred [LinkMode] method for [CCodeAsset]s. LinkModePreference get linkModePreference; /// Metadata from a direct dependency. @@ -100,9 +101,6 @@ abstract final class BuildConfig { /// Not available during a [dryRun]. BuildMode get buildMode; - /// The underlying config. - Config get config; - /// The asset types the invoker of this build supports. /// /// Currently known values: @@ -132,7 +130,7 @@ abstract final class BuildConfig { /// If not provided, [workingDirectory] defaults to [Directory.current]. /// /// This async constructor is intended to be used directly in CLI files. - static Future fromArgs( + static Future fromArguments( List args, { Map? environment, Uri? workingDirectory, @@ -143,8 +141,19 @@ abstract final class BuildConfig { workingDirectory: workingDirectory, ); + /// Construct a config for a non-dry run by providing values for each field. + /// + /// `build.dart` hooks will most likely use [BuildConfig.fromArguments]. + /// However, for unit testing code which consumes a [BuildConfig], this + /// constructor facilitates easy construction. + /// + /// For the documentation of the parameters, see the equally named fields. + /// + /// Parameter [dependencyMetadata] must be a nested map + /// `{'packageName' : {'key' : 'value'}}` + /// where `packageName` and `key` correspond to the parameters in [metadatum]. factory BuildConfig({ - required Uri outDir, + required Uri outputDirectory, required String packageName, required Uri packageRoot, required BuildMode buildMode, @@ -158,7 +167,7 @@ abstract final class BuildConfig { Iterable? supportedAssetTypes, }) => BuildConfigImpl( - outDir: outDir, + outDir: outputDirectory, packageName: packageName, packageRoot: packageRoot, buildMode: buildMode as BuildModeImpl, @@ -177,6 +186,13 @@ abstract final class BuildConfig { supportedAssetTypes: supportedAssetTypes, ); + /// Construct a config for a dry run by providing values for each field. + /// + /// `build.dart` hooks will most likely use [BuildConfig.fromArguments]. + /// However, for unit testing code which consumes a [BuildConfig], this + /// constructor facilitates easy construction. + /// + /// For the documentation of the parameters, see the equally named fields. factory BuildConfig.dryRun({ required Uri outDir, required String packageName, @@ -193,7 +209,4 @@ abstract final class BuildConfig { linkModePreference: linkModePreference as LinkModePreferenceImpl, supportedAssetTypes: supportedAssetTypes, ); - - factory BuildConfig.fromConfig(Config config) => - BuildConfigImpl.fromConfig(config); } diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 90b743227..57660436d 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -48,10 +48,10 @@ abstract final class BuildOutput { /// Create a build output. /// - /// The [timestamp] must be before any any [dependencies] are read by the - /// build this output belongs to. If the [BuildOutput] object is created at - /// the beginning of the `build.dart` script, [timestamp] can be omitted and - /// will default to [DateTime.now]. The [timestamp] is rounded down to whole + /// The [timestamp] must be before any [dependencies] are read by the build + /// this output belongs to. If the [BuildOutput] object is created at the + /// beginning of the `build.dart` script, [timestamp] can be omitted and will + /// default to [DateTime.now]. The [timestamp] is rounded down to whole /// seconds, because [File.lastModified] is rounded to whole seconds and /// caching logic compares these timestamps. /// @@ -83,13 +83,9 @@ abstract final class BuildOutput { ); /// Adds [Asset]s produced by this build or dry run. - /// - /// In dry runs, the [Architecture] for [CCodeAsset]s can be omitted. void addAsset(Asset asset); /// Adds [Asset]s produced by this build or dry run. - /// - /// In dry runs, the [Architecture] for [CCodeAsset]s can be omitted. void addAssets(Iterable assets); /// Adds file used by this build. @@ -118,9 +114,10 @@ abstract final class BuildOutput { /// and packages through `build.dart` invocations. static Version get latestVersion => BuildOutputImpl.latestVersion; - /// Write out this build output to a file inside [BuildConfig.outDir]. + /// Write out this build output to a file inside + /// [BuildConfig.outputDirectory]. /// - /// This takes into account the [BuildConfig.latestVersion] to write the - /// build output in a format the SDK that invoked the script supports. - Future writeToFile({required BuildConfig config}); + /// This takes into account the [BuildConfig.latestVersion] to write the build + /// output in a format the SDK that invoked the script supports. + // Future writeToFile({required BuildConfig config}); } diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart index e8d137f85..ce0f384a8 100644 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -49,7 +49,7 @@ class AssetDownloader implements Builder { ); final fileName = downloadUri2.pathSegments.lastWhere((element) => element.isNotEmpty); - targetUri = config.outDir.resolve(fileName); + targetUri = config.outputDirectory.resolve(fileName); final request = await HttpClient().getUrl(downloadUri2); final response = await request.close(); await response.pipe(File.fromUri(targetUri).openWrite()); diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 2227b9874..083eb3b69 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -6,7 +6,7 @@ part of '../api/build_config.dart'; final class BuildConfigImpl implements BuildConfig { @override - Uri get outDir => _outDir; + Uri get outputDirectory => _outDir; late final Uri _outDir; @override @@ -82,7 +82,6 @@ final class BuildConfigImpl implements BuildConfig { late final Version _version; - @override Config get config => _config; late final Config _config; @@ -146,13 +145,14 @@ final class BuildConfigImpl implements BuildConfig { return BuildConfigImpl.fromConfig(config); } - /// Constructs a checksum for a [BuildConfigImpl] based on the fields - /// of a buildconfig that influence the build. + /// Constructs a checksum for a [BuildConfigImpl] based on the fields of a + /// buildconfig that influence the build. /// - /// This can be used for an [outDir], but should not be used for dry-runs. + /// This can be used for an [outputDirectory], but should not be used for + /// dry-runs. /// - /// In particular, it only takes the package name from [packageRoot], - /// so that the hash is equal across checkouts and ignores [outDir] itself. + /// In particular, it only takes the package name from [packageRoot], so that + /// the hash is equal across checkouts and ignores [outputDirectory] itself. static String checksum({ required String packageName, required Uri packageRoot, @@ -486,7 +486,7 @@ final class BuildConfigImpl implements BuildConfig { if (other is! BuildConfigImpl) { return false; } - if (other.outDir != outDir) return false; + if (other.outputDirectory != outputDirectory) return false; if (other.packageName != packageName) return false; if (other.packageRoot != packageRoot) return false; if (other.dryRun != dryRun) return false; @@ -508,7 +508,7 @@ final class BuildConfigImpl implements BuildConfig { @override int get hashCode => Object.hashAll([ - outDir, + outputDirectory, packageName, packageRoot, targetOS, diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index 7f82e820c..fd549287a 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -123,10 +123,9 @@ final class BuildOutputImpl implements BuildOutput { return BuildOutputImpl.fromYamlString(await buildOutputFile.readAsString()); } - /// Writes the [toYamlString] to [BuildConfig.outDir]/[fileName]. - @override + /// Writes the [toYamlString] to [BuildConfig.outputDirectory]/[fileName]. Future writeToFile({required BuildConfig config}) async { - final outDir = config.outDir; + final outDir = config.outputDirectory; final buildOutputUri = outDir.resolve(fileName); final yamlString = toYamlString((config as BuildConfigImpl).version); await File.fromUri(buildOutputUri).writeAsStringCreateDirectory(yamlString); diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 0fcddf4fc..f1565735c 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -48,7 +48,7 @@ void main() async { test('BuildConfig ==', () { final config1 = BuildConfig( - outDir: outDirUri, + outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, @@ -65,7 +65,7 @@ void main() async { ); final config2 = BuildConfig( - outDir: outDir2Uri, + outputDirectory: outDir2Uri, packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, @@ -78,7 +78,7 @@ void main() async { expect(config1, equals(config1)); expect(config1 == config2, false); - expect(config1.outDir != config2.outDir, true); + expect(config1.outputDirectory != config2.outputDirectory, true); expect(config1.packageRoot, config2.packageRoot); expect(config1.targetArchitecture == config2.targetArchitecture, true); expect(config1.targetOS != config2.targetOS, true); @@ -96,7 +96,7 @@ void main() async { test('BuildConfig fromConfig', () { final buildConfig2 = BuildConfig( - outDir: outDirUri, + outputDirectory: outDirUri, packageName: packageName, packageRoot: packageRootUri, targetArchitecture: Architecture.arm64, @@ -119,7 +119,7 @@ void main() async { 'version': BuildOutput.latestVersion.toString(), }); - final fromConfig = BuildConfig.fromConfig(config); + final fromConfig = BuildConfigImpl.fromConfig(config); expect(fromConfig, equals(buildConfig2)); }); @@ -143,13 +143,13 @@ void main() async { 'version': BuildOutput.latestVersion.toString(), }); - final fromConfig = BuildConfig.fromConfig(config); + final fromConfig = BuildConfigImpl.fromConfig(config); expect(fromConfig, equals(buildConfig2)); }); test('BuildConfig == dependency metadata', () { final buildConfig1 = BuildConfig( - outDir: outDirUri, + outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, @@ -169,7 +169,7 @@ void main() async { ); final buildConfig2 = BuildConfig( - outDir: outDirUri, + outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, @@ -196,7 +196,7 @@ void main() async { test('BuildConfig fromArgs', () async { final buildConfig = BuildConfig( - outDir: outDirUri, + outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, @@ -209,7 +209,7 @@ void main() async { final configUri = tempUri.resolve('config.yaml'); final configFile = File.fromUri(configUri); await configFile.writeAsString(configFileContents); - final buildConfigFromArgs = await BuildConfig.fromArgs( + final buildConfigFromArgs = await BuildConfig.fromArguments( ['--config', configUri.toFilePath()], environment: {}, // Don't inherit the test environment. ); diff --git a/pkgs/native_assets_cli/test/api/build_test.dart b/pkgs/native_assets_cli/test/api/build_test.dart index 731ad27ec..f8aa2d4b0 100644 --- a/pkgs/native_assets_cli/test/api/build_test.dart +++ b/pkgs/native_assets_cli/test/api/build_test.dart @@ -41,7 +41,7 @@ void main() async { await File.fromUri(fakeVcVars).create(); final config1 = BuildConfig( - outDir: outDirUri, + outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, targetArchitecture: Architecture.arm64, diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index 2ec858527..56b58008d 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -78,7 +78,7 @@ void main() async { expect(config1, equals(config1)); expect(config1 == config2, false); - expect(config1.outDir != config2.outDir, true); + expect(config1.outputDirectory != config2.outputDirectory, true); expect(config1.packageRoot, config2.packageRoot); expect(config1.targetArchitecture == config2.targetArchitecture, true); expect(config1.targetOS != config2.targetOS, true); diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 2fcc95b68..ea73dd538 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -47,7 +47,7 @@ class CBuilder implements Builder { /// The filename will be decided by [BuildConfig.targetOS] and /// [OS.libraryFileName] or [OS.executableFileName]. /// - /// File will be placed in [BuildConfig.outDir]. + /// File will be placed in [BuildConfig.outputDirectory]. final String name; /// Asset identifier. @@ -196,7 +196,7 @@ class CBuilder implements Builder { required BuildOutput buildOutput, required Logger? logger, }) async { - final outDir = buildConfig.outDir; + final outDir = buildConfig.outputDirectory; final packageRoot = buildConfig.packageRoot; await Directory.fromUri(outDir).create(recursive: true); final linkMode = buildConfig.linkModePreference.preferredLinkMode; diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart index bb231b639..eb556c604 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart @@ -57,7 +57,7 @@ class RunCBuilder { this.std, this.language = Language.c, this.cppLinkStdLib, - }) : outDir = buildConfig.outDir, + }) : outDir = buildConfig.outputDirectory, assert([executable, dynamicLibrary, staticLibrary] .whereType() .length == diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart index 5cf28c543..8d641ff4d 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart @@ -30,7 +30,7 @@ void main() { const name = 'add'; final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart index a20a5090b..80693148b 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart @@ -125,7 +125,7 @@ Future buildLib( const name = 'add'; final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: targetArchitecture, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart index 1cac73a5c..dc8999087 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart @@ -57,7 +57,7 @@ void main() { final addCUri = packageUri.resolve('test/cbuilder/testfiles/add/src/add.c'); final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: target, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart index 593539158..1e092b729 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart @@ -45,7 +45,7 @@ void main() { const name = 'add'; final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: target, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart index cd363b988..530e0db83 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart @@ -43,7 +43,7 @@ void main() { const name = 'add'; final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: target, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart index 3d1d5a998..afcb42fbf 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart @@ -55,7 +55,7 @@ void main() { const name = 'add'; final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetOS: OS.windows, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart index 9a96856b8..26db9fb69 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart @@ -44,7 +44,7 @@ void main() { final logger = createCapturingLogger(logMessages); final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, @@ -123,7 +123,7 @@ void main() { linkModePreference: LinkModePreference.dynamic, ) : BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, @@ -215,7 +215,7 @@ void main() { final logger = createCapturingLogger(logMessages); final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, @@ -273,7 +273,7 @@ void main() { const name = 'includes'; final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, @@ -318,7 +318,7 @@ void main() { final logger = createCapturingLogger(logMessages); final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, @@ -377,7 +377,7 @@ void main() { final buildConfig = BuildConfig( buildMode: BuildMode.release, - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, @@ -441,7 +441,7 @@ void main() { final buildConfig = BuildConfig( buildMode: BuildMode.release, - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, @@ -511,7 +511,7 @@ Future testDefines({ const name = 'defines'; final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: name, packageRoot: tempUri, targetArchitecture: Architecture.current, diff --git a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart index 0a256e986..644ca5641 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart @@ -41,7 +41,7 @@ void main() { ...await vcvars64.defaultResolver!.resolve(logger: logger) ].firstOrNull?.uri; final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: 'dummy', packageRoot: tempUri, targetArchitecture: Architecture.current, @@ -65,7 +65,7 @@ void main() { test('No compiler found', () async { final tempUri = await tempDirForTest(); final buildConfig = BuildConfig( - outDir: tempUri, + outputDirectory: tempUri, packageName: 'dummy', packageRoot: tempUri, targetArchitecture: Architecture.arm64, From 9992ce2c8d730683545522f1ae897b7ae4df7b93 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 27 Feb 2024 09:37:50 +0100 Subject: [PATCH 47/75] address comment --- .../build_runner_run_in_isolation_test.dart | 6 +-- .../lib/src/api/c_compiler_config.dart | 15 ++++--- .../lib/src/model/build_config.dart | 16 +++---- .../lib/src/model/c_compiler_config.dart | 45 ++++++++++--------- .../test/api/build_config_test.dart | 12 ++--- .../test/api/build_test.dart | 6 +-- .../test/model/build_config_test.dart | 32 ++++++------- .../lib/src/cbuilder/compiler_resolver.dart | 4 +- .../cbuilder/cbuilder_build_failure_test.dart | 2 +- .../test/cbuilder/cbuilder_test.dart | 16 +++---- .../test/cbuilder/compiler_resolver_test.dart | 10 ++--- 11 files changed, 83 insertions(+), 81 deletions(-) diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_run_in_isolation_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_run_in_isolation_test.dart index 84e7b5264..a7f02557f 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_run_in_isolation_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_run_in_isolation_test.dart @@ -53,11 +53,11 @@ void main() async { dartExecutable, // Manually pass in a compiler. cCompilerConfig: CCompilerConfigImpl( - ar: Platform.environment[arKey]?.fileUri, - cc: cc, + archiver: Platform.environment[arKey]?.fileUri, + compiler: cc, envScript: Platform.environment[envScriptKey]?.fileUri, envScriptArgs: Platform.environment[envScriptArgsKey]?.split(' '), - ld: Platform.environment[ldKey]?.fileUri, + linker: Platform.environment[ldKey]?.fileUri, ), // Prevent any other environment variables. includeParentEnvironment: false, diff --git a/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart b/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart index 44bf50b47..30dc67ac4 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_compiler_config.dart @@ -7,24 +7,25 @@ part of 'build_config.dart'; /// The configuration for a C toolchain. abstract final class CCompilerConfig { /// Path to a C compiler. - Uri? get cc; + Uri? get compiler; /// Path to a native linker. - Uri? get ld; + Uri? get linker; /// Path to a native archiver. - Uri? get ar; + Uri? get archiver; - /// Path to script that sets environment variables for [cc], [ld], and [ar]. + /// Path to script that sets environment variables for [compiler], [linker], + /// and [archiver]. Uri? get envScript; /// Arguments for [envScript]. List? get envScriptArgs; factory CCompilerConfig({ - Uri? ar, - Uri? cc, - Uri? ld, + Uri? archiver, + Uri? compiler, + Uri? linker, Uri? envScript, List? envScriptArgs, }) = CCompilerConfigImpl; diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 083eb3b69..9b5951c77 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -176,11 +176,11 @@ final class BuildConfigImpl implements BuildConfig { targetAndroidNdkApi.toString(), buildMode.toString(), linkModePreference.toString(), - cCompiler?.ar.toString(), - cCompiler?.cc.toString(), + cCompiler?.archiver.toString(), + cCompiler?.compiler.toString(), cCompiler?.envScript.toString(), cCompiler?.envScriptArgs.toString(), - cCompiler?.ld.toString(), + cCompiler?.linker.toString(), if (dependencyMetadata != null) for (final entry in dependencyMetadata.entries) ...[ entry.key, @@ -354,7 +354,7 @@ final class BuildConfigImpl implements BuildConfig { if (dryRun) { _throwIfNotNullInDryRun(CCompilerConfigImpl.arConfigKeyFull); } else { - cCompiler._ar = config.optionalPath( + cCompiler._archiver = config.optionalPath( CCompilerConfigImpl.arConfigKeyFull, mustExist: true, ); @@ -364,7 +364,7 @@ final class BuildConfigImpl implements BuildConfig { if (dryRun) { _throwIfNotNullInDryRun(CCompilerConfigImpl.ccConfigKeyFull); } else { - cCompiler._cc = config.optionalPath( + cCompiler._compiler = config.optionalPath( CCompilerConfigImpl.ccConfigKeyFull, mustExist: true, ); @@ -375,7 +375,7 @@ final class BuildConfigImpl implements BuildConfig { if (dryRun) { _throwIfNotNullInDryRun(CCompilerConfigImpl.ccConfigKeyFull); } else { - cCompiler._ld = config.optionalPath( + cCompiler._linker = config.optionalPath( CCompilerConfigImpl.ldConfigKeyFull, mustExist: true, ); @@ -386,8 +386,8 @@ final class BuildConfigImpl implements BuildConfig { _throwIfNotNullInDryRun(CCompilerConfigImpl.ccConfigKeyFull); } else { cCompiler._envScript = (ccSet && - cCompiler.cc != null && - cCompiler.cc!.toFilePath().endsWith('cl.exe')) + cCompiler.compiler != null && + cCompiler.compiler!.toFilePath().endsWith('cl.exe')) ? config.path(CCompilerConfigImpl.envScriptConfigKeyFull, mustExist: true) : null; diff --git a/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart b/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart index 2c0bc5b03..10416e4e0 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart @@ -7,20 +7,21 @@ part of '../api/build_config.dart'; final class CCompilerConfigImpl implements CCompilerConfig { /// Path to a C compiler. @override - Uri? get cc => _cc; - late final Uri? _cc; + Uri? get compiler => _compiler; + late final Uri? _compiler; /// Path to a native linker. @override - Uri? get ld => _ld; - late final Uri? _ld; + Uri? get linker => _linker; + late final Uri? _linker; /// Path to a native archiver. @override - Uri? get ar => _ar; - late final Uri? _ar; + Uri? get archiver => _archiver; + late final Uri? _archiver; - /// Path to script that sets environment variables for [cc], [ld], and [ar]. + /// Path to script that sets environment variables for [compiler], [linker], + /// and [archiver]. @override Uri? get envScript => _envScript; late final Uri? _envScript; @@ -31,16 +32,16 @@ final class CCompilerConfigImpl implements CCompilerConfig { late final List? _envScriptArgs; factory CCompilerConfigImpl({ - Uri? ar, - Uri? cc, - Uri? ld, + Uri? archiver, + Uri? compiler, + Uri? linker, Uri? envScript, List? envScriptArgs, }) => CCompilerConfigImpl._() - .._ar = ar - .._cc = cc - .._ld = ld + .._archiver = archiver + .._compiler = compiler + .._linker = linker .._envScript = envScript .._envScriptArgs = envScriptArgs; @@ -60,9 +61,9 @@ final class CCompilerConfigImpl implements CCompilerConfig { '$configKey.$envScriptArgsConfigKey'; Map toYaml() => { - if (_ar != null) arConfigKey: _ar.toFilePath(), - if (_cc != null) ccConfigKey: _cc.toFilePath(), - if (_ld != null) ldConfigKey: _ld.toFilePath(), + if (_archiver != null) arConfigKey: _archiver.toFilePath(), + if (_compiler != null) ccConfigKey: _compiler.toFilePath(), + if (_linker != null) ldConfigKey: _linker.toFilePath(), if (_envScript != null) envScriptConfigKey: _envScript.toFilePath(), if (_envScriptArgs != null) envScriptArgsConfigKey: _envScriptArgs, }.sortOnKey(); @@ -72,9 +73,9 @@ final class CCompilerConfigImpl implements CCompilerConfig { if (other is! CCompilerConfigImpl) { return false; } - if (other.ar != ar) return false; - if (other.cc != cc) return false; - if (other.ld != ld) return false; + if (other.archiver != archiver) return false; + if (other.compiler != compiler) return false; + if (other.linker != linker) return false; if (other.envScript != envScript) return false; if (!const ListEquality() .equals(other.envScriptArgs, envScriptArgs)) { @@ -85,9 +86,9 @@ final class CCompilerConfigImpl implements CCompilerConfig { @override int get hashCode => Object.hash( - _ar, - _cc, - _ld, + _archiver, + _compiler, + _linker, _envScript, const ListEquality().hash(envScriptArgs), ); diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index f1565735c..7172e5300 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -55,9 +55,9 @@ void main() async { targetOS: OS.iOS, targetIOSSdk: IOSSdk.iPhoneOS, cCompiler: CCompilerConfig( - cc: fakeClang, - ld: fakeLd, - ar: fakeAr, + compiler: fakeClang, + linker: fakeLd, + archiver: fakeAr, ), buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, @@ -83,9 +83,9 @@ void main() async { expect(config1.targetArchitecture == config2.targetArchitecture, true); expect(config1.targetOS != config2.targetOS, true); expect(config1.targetIOSSdk != config2.targetIOSSdk, true); - expect(config1.cCompiler.cc != config2.cCompiler.cc, true); - expect(config1.cCompiler.ld != config2.cCompiler.ld, true); - expect(config1.cCompiler.ar != config2.cCompiler.ar, true); + expect(config1.cCompiler.compiler != config2.cCompiler.compiler, true); + expect(config1.cCompiler.linker != config2.cCompiler.linker, true); + expect(config1.cCompiler.archiver != config2.cCompiler.archiver, true); expect(config1.cCompiler.envScript == config2.cCompiler.envScript, true); expect(config1.cCompiler.envScriptArgs == config2.cCompiler.envScriptArgs, true); diff --git a/pkgs/native_assets_cli/test/api/build_test.dart b/pkgs/native_assets_cli/test/api/build_test.dart index f8aa2d4b0..3207e14e8 100644 --- a/pkgs/native_assets_cli/test/api/build_test.dart +++ b/pkgs/native_assets_cli/test/api/build_test.dart @@ -48,9 +48,9 @@ void main() async { targetOS: OS.iOS, targetIOSSdk: IOSSdk.iPhoneOS, cCompiler: CCompilerConfig( - cc: fakeClang, - ld: fakeLd, - ar: fakeAr, + compiler: fakeClang, + linker: fakeLd, + archiver: fakeAr, ), buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferDynamic, diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index 56b58008d..c8603aa31 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -57,9 +57,9 @@ void main() async { targetOS: OSImpl.iOS, targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( - cc: fakeClang, - ld: fakeLd, - ar: fakeAr, + compiler: fakeClang, + linker: fakeLd, + archiver: fakeAr, ), buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -83,9 +83,9 @@ void main() async { expect(config1.targetArchitecture == config2.targetArchitecture, true); expect(config1.targetOS != config2.targetOS, true); expect(config1.targetIOSSdk != config2.targetIOSSdk, true); - expect(config1.cCompiler.cc != config2.cCompiler.cc, true); - expect(config1.cCompiler.ld != config2.cCompiler.ld, true); - expect(config1.cCompiler.ar != config2.cCompiler.ar, true); + expect(config1.cCompiler.compiler != config2.cCompiler.compiler, true); + expect(config1.cCompiler.linker != config2.cCompiler.linker, true); + expect(config1.cCompiler.archiver != config2.cCompiler.archiver, true); expect(config1.cCompiler.envScript == config2.cCompiler.envScript, true); expect(config1.cCompiler.envScriptArgs == config2.cCompiler.envScriptArgs, true); @@ -154,8 +154,8 @@ void main() async { targetOS: OSImpl.iOS, targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( - cc: fakeClang, - ld: fakeLd, + compiler: fakeClang, + linker: fakeLd, ), buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -222,8 +222,8 @@ void main() async { targetOS: OSImpl.iOS, targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( - cc: fakeClang, - ld: fakeLd, + compiler: fakeClang, + linker: fakeLd, ), buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -301,8 +301,8 @@ version: 1.0.0'''; targetOS: OSImpl.iOS, targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( - cc: fakeClang, - ld: fakeLd, + compiler: fakeClang, + linker: fakeLd, ), buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -421,8 +421,8 @@ version: 1.0.0'''; targetOS: OSImpl.iOS, targetIOSSdk: IOSSdkImpl.iPhoneOS, cCompiler: CCompilerConfigImpl( - cc: fakeClang, - ld: fakeLd, + compiler: fakeClang, + linker: fakeLd, ), buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, @@ -488,7 +488,7 @@ version: 1.0.0'''; targetArchitecture: ArchitectureImpl.x64, targetOS: OSImpl.windows, cCompiler: CCompilerConfigImpl( - cc: fakeCl, + compiler: fakeCl, envScript: fakeVcVars, envScriptArgs: ['x64'], ), @@ -568,7 +568,7 @@ version: 1.0.0'''; buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.dynamic, cCompiler: CCompilerConfigImpl( - cc: fakeClangUri, + compiler: fakeClangUri, )); printOnFailure([name1, name3].toString()); expect(name1 != name3, true); diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart index b721da274..cdc8ee0e6 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart @@ -97,7 +97,7 @@ class CompilerResolver { } Future _tryLoadCompilerFromConfig() async { - final configCcUri = buildConfig.cCompiler.cc; + final configCcUri = buildConfig.cCompiler.compiler; if (configCcUri != null) { assert(await File.fromUri(configCcUri).exists()); logger?.finer('Using compiler ${configCcUri.toFilePath()} ' @@ -182,7 +182,7 @@ class CompilerResolver { } Future _tryLoadArchiverFromConfig() async { - final configArUri = buildConfig.cCompiler.ar; + final configArUri = buildConfig.cCompiler.archiver; if (configArUri != null) { assert(await File.fromUri(configArUri).exists()); logger?.finer('Using archiver ${configArUri.toFilePath()} ' diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart index 8d641ff4d..b7de76050 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart @@ -38,7 +38,7 @@ void main() { linkModePreference: LinkModePreference.dynamic, buildMode: BuildMode.release, cCompiler: CCompilerConfig( - cc: cc, + compiler: cc, envScript: envScript, envScriptArgs: envScriptArgs, ), diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart index 26db9fb69..ae3167045 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart @@ -53,7 +53,7 @@ void main() { // Ignored by executables. linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( - cc: cc, + compiler: cc, envScript: envScript, envScriptArgs: envScriptArgs, ), @@ -131,7 +131,7 @@ void main() { buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( - cc: cc, + compiler: cc, envScript: envScript, envScriptArgs: envScriptArgs, ), @@ -224,7 +224,7 @@ void main() { // Ignored by executables. linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( - cc: cc, + compiler: cc, envScript: envScript, envScriptArgs: envScriptArgs, ), @@ -281,7 +281,7 @@ void main() { buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( - cc: cc, + compiler: cc, envScript: envScript, envScriptArgs: envScriptArgs, ), @@ -326,7 +326,7 @@ void main() { buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( - cc: cc, + compiler: cc, envScript: envScript, envScriptArgs: envScriptArgs, ), @@ -385,7 +385,7 @@ void main() { // Ignored by executables. linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( - cc: cc, + compiler: cc, envScript: envScript, envScriptArgs: envScriptArgs, ), @@ -449,7 +449,7 @@ void main() { // Ignored by executables. linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( - cc: cc, + compiler: cc, envScript: envScript, envScriptArgs: envScriptArgs, ), @@ -520,7 +520,7 @@ Future testDefines({ // Ignored by executables. linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( - cc: cc, + compiler: cc, envScript: envScript, envScriptArgs: envScriptArgs, ), diff --git a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart index 644ca5641..854154c1b 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart @@ -49,17 +49,17 @@ void main() { buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, cCompiler: CCompilerConfig( - ar: ar, - cc: cc, - ld: ld, + archiver: ar, + compiler: cc, + linker: ld, envScript: envScript, ), ); final resolver = CompilerResolver(buildConfig: buildConfig, logger: logger); final compiler = await resolver.resolveCompiler(); final archiver = await resolver.resolveArchiver(); - expect(compiler.uri, buildConfig.cCompiler.cc); - expect(archiver.uri, buildConfig.cCompiler.ar); + expect(compiler.uri, buildConfig.cCompiler.compiler); + expect(archiver.uri, buildConfig.cCompiler.archiver); }); test('No compiler found', () async { From 21d5f833eefac56148cfe35478190c0883c273ff Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 27 Feb 2024 09:41:13 +0100 Subject: [PATCH 48/75] address comment --- pkgs/native_assets_cli/lib/src/api/ios_sdk.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart index 26fc67051..a949f226f 100644 --- a/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart +++ b/pkgs/native_assets_cli/lib/src/api/ios_sdk.dart @@ -5,8 +5,6 @@ part '../model/ios_sdk.dart'; /// For an iOS target, a build is either done for the device or the simulator. -/// -/// Only fat binaries or xcframeworks can contain both targets. abstract final class IOSSdk { /// The iphoneos SDK in Xcode. /// From aa4d83e68fc91688e9f8018a13465651346ee6b9 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 27 Feb 2024 09:52:11 +0100 Subject: [PATCH 49/75] Improve docs --- .../lib/src/api/c_code_asset.dart | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index bfcb3c060..aa88c11e0 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -9,11 +9,25 @@ part of 'asset.dart'; /// Typical other languages which produce code assets that respect the C ABI /// include C++ and Rust. /// +/// C code assets can be accessed at runtime through native external functions +/// via their asset [id]: +/// +/// ```dart +/// import 'dart:ffi'; +/// +/// void main() { +/// final result = add(14, 28); +/// print(result); +/// } +/// +/// @Native(assetId: 'package:my_package/add.dart') +/// external int add(int a, int b); +/// ``` +/// /// There are several types of C code assets: /// * Assets which designate symbols present in the target system /// ([SystemDylib]), process ([LookupInProcess]), or executable -/// ([LookupInExecutable]). These assets are identified by their [id], and do -/// not have a [file]. +/// ([LookupInExecutable]). These assets do not have a [file]. /// * Dynamic libraries bundled into the application ([BundledDylib]). These /// assets must provide a [file] to be bundled. /// @@ -28,9 +42,6 @@ part of 'asset.dart'; /// target system ([SystemDylib]). If the asset is bundled "manually", the Dart /// or Flutter SDK will take care of copying the asset [file] from its specified /// location on the current system into the application bundle. -/// -/// Assets are also called "native assets" to differentiate them from the Dart -/// code also bundled with an application. abstract final class CCodeAsset implements Asset { /// The operating system this asset can run on. OS get os; From 0ffe6e469adbf06badd8eb512139dc30a2522c8b Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 27 Feb 2024 11:43:05 +0100 Subject: [PATCH 50/75] Make all asset ids use a `package:...` --- .../test/api/asset_test.dart | 12 ++--- .../test/api/build_output_test.dart | 4 +- .../test/model/asset_test.dart | 44 +++++++++---------- .../test/model/build_output_test.dart | 32 +++++++------- 4 files changed, 46 insertions(+), 46 deletions(-) diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index 6ae2c6b0e..5a8cb10b9 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -10,7 +10,7 @@ void main() { test('Asset constructors', () async { final assets = [ CCodeAsset( - id: 'foo', + id: 'package:my_package/foo', file: Uri.file('path/to/libfoo.so'), dynamicLoading: BundledDylib(), os: OS.android, @@ -18,28 +18,28 @@ void main() { linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'foo3', + id: 'package:my_package/foo3', dynamicLoading: SystemDylib(Uri(path: 'libfoo3.so')), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'foo4', + id: 'package:my_package/foo4', dynamicLoading: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'foo5', + id: 'package:my_package/foo5', dynamicLoading: LookupInProcess(), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'bar', + id: 'package:my_package/bar', file: Uri(path: 'path/to/libbar.a'), dynamicLoading: BundledDylib(), os: OS.linux, @@ -47,7 +47,7 @@ void main() { linkMode: LinkMode.static, ), CCodeAsset( - id: 'bla', + id: 'package:my_package/bla', file: Uri(path: 'path/with spaces/bla.dll'), dynamicLoading: BundledDylib(), os: OS.windows, diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index dd97ff33e..3f1b321f5 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -23,7 +23,7 @@ void main() { timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ CCodeAsset( - id: 'foo', + id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), dynamicLoading: BundledDylib(), os: OS.android, @@ -31,7 +31,7 @@ void main() { linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'foo2', + id: 'package:my_package/foo2', dynamicLoading: SystemDylib(Uri(path: 'path/to/libfoo2.so')), os: OS.android, architecture: Architecture.x64, diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index a15dbeb4e..e8043f7c3 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -18,7 +18,7 @@ void main() { final data2Uri = Uri.file('path/to/data.json'); final cCodeAssets = [ CCodeAssetImpl( - id: 'foo', + id: 'package:my_package/foo', file: fooUri, dynamicLoading: BundledDylibImpl(), os: OSImpl.android, @@ -26,28 +26,28 @@ void main() { linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( - id: 'foo3', + id: 'package:my_package/foo3', dynamicLoading: SystemDylibImpl(foo3Uri), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( - id: 'foo4', + id: 'package:my_package/foo4', dynamicLoading: LookupInExecutableImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( - id: 'foo5', + id: 'package:my_package/foo5', dynamicLoading: LookupInProcessImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( - id: 'bar', + id: 'package:my_package/bar', file: barUri, dynamicLoading: BundledDylibImpl(), os: OSImpl.linux, @@ -55,7 +55,7 @@ void main() { linkMode: LinkModeImpl.static, ), CCodeAssetImpl( - id: 'bla', + id: 'package:my_package/bla', file: blaUri, dynamicLoading: BundledDylibImpl(), os: OSImpl.windows, @@ -65,11 +65,11 @@ void main() { ]; final dataAssets = [ DataAssetImpl( - id: 'my_data_asset', + id: 'package:my_package/my_data_asset', file: dataUri, ), DataAssetImpl( - id: 'my_data_asset2', + id: 'package:my_package/my_data_asset2', file: data2Uri, ), ]; @@ -78,35 +78,35 @@ void main() { ...dataAssets, ]; - final assetsYamlEncodingV1_0_0 = '''- id: foo + final assetsYamlEncodingV1_0_0 = '''- id: package:my_package/foo link_mode: dynamic path: path_type: absolute uri: ${fooUri.toFilePath()} target: android_x64 -- id: foo3 +- id: package:my_package/foo3 link_mode: dynamic path: path_type: system uri: ${foo3Uri.toFilePath()} target: android_x64 -- id: foo4 +- id: package:my_package/foo4 link_mode: dynamic path: path_type: executable target: android_x64 -- id: foo5 +- id: package:my_package/foo5 link_mode: dynamic path: path_type: process target: android_x64 -- id: bar +- id: package:my_package/bar link_mode: static path: path_type: absolute uri: ${barUri.toFilePath()} target: linux_arm64 -- id: bla +- id: package:my_package/bla link_mode: dynamic path: path_type: absolute @@ -117,7 +117,7 @@ void main() { dynamic_loading: type: bundle file: ${fooUri.toFilePath()} - id: foo + id: package:my_package/foo link_mode: dynamic os: android type: c_code @@ -125,21 +125,21 @@ void main() { dynamic_loading: type: system uri: ${foo3Uri.toFilePath()} - id: foo3 + id: package:my_package/foo3 link_mode: dynamic os: android type: c_code - architecture: x64 dynamic_loading: type: executable - id: foo4 + id: package:my_package/foo4 link_mode: dynamic os: android type: c_code - architecture: x64 dynamic_loading: type: process - id: foo5 + id: package:my_package/foo5 link_mode: dynamic os: android type: c_code @@ -147,7 +147,7 @@ void main() { dynamic_loading: type: bundle file: ${barUri.toFilePath()} - id: bar + id: package:my_package/bar link_mode: static os: linux type: c_code @@ -155,14 +155,14 @@ void main() { dynamic_loading: type: bundle file: ${blaUri.toFilePath()} - id: bla + id: package:my_package/bla link_mode: dynamic os: windows type: c_code -- id: my_data_asset +- id: package:my_package/my_data_asset file: ${dataUri.toFilePath()} type: data -- id: my_data_asset2 +- id: package:my_package/my_data_asset2 file: ${data2Uri.toFilePath()} type: data'''; diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 4cc2216a6..09587067b 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -24,7 +24,7 @@ void main() { timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ CCodeAssetImpl( - id: 'foo', + id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), dynamicLoading: BundledDylibImpl(), os: OSImpl.android, @@ -32,21 +32,21 @@ void main() { linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( - id: 'foo2', + id: 'package:my_package/foo2', dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( - id: 'foo3', + id: 'package:my_package/foo3', dynamicLoading: LookupInProcessImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( - id: 'foo4', + id: 'package:my_package/foo4', dynamicLoading: LookupInExecutableImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, @@ -63,24 +63,24 @@ void main() { const yamlEncodingV1_0_0 = '''timestamp: 2022-11-10 13:25:01.000 assets: - - id: foo + - id: package:my_package/foo link_mode: dynamic path: path_type: absolute uri: path/to/libfoo.so target: android_x64 - - id: foo2 + - id: package:my_package/foo2 link_mode: dynamic path: path_type: system uri: path/to/libfoo2.so target: android_x64 - - id: foo3 + - id: package:my_package/foo3 link_mode: dynamic path: path_type: process target: android_x64 - - id: foo4 + - id: package:my_package/foo4 link_mode: dynamic path: path_type: executable @@ -97,7 +97,7 @@ assets: dynamic_loading: type: bundle file: path/to/libfoo.so - id: foo + id: package:my_package/foo link_mode: dynamic os: android type: c_code @@ -105,21 +105,21 @@ assets: dynamic_loading: type: system uri: path/to/libfoo2.so - id: foo2 + id: package:my_package/foo2 link_mode: dynamic os: android type: c_code - architecture: x64 dynamic_loading: type: process - id: foo3 + id: package:my_package/foo3 link_mode: dynamic os: android type: c_code - architecture: x64 dynamic_loading: type: executable - id: foo4 + id: package:my_package/foo4 link_mode: dynamic os: android type: c_code @@ -268,7 +268,7 @@ version: ${BuildOutputImpl.latestVersion}'''), timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ CCodeAssetImpl( - id: 'foo', + id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), dynamicLoading: BundledDylibImpl(), os: OSImpl.android, @@ -276,7 +276,7 @@ version: ${BuildOutputImpl.latestVersion}'''), linkMode: LinkModeImpl.dynamic, ), CCodeAssetImpl( - id: 'foo2', + id: 'package:my_package/foo2', dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, @@ -298,7 +298,7 @@ version: ${BuildOutputImpl.latestVersion}'''), ); buildOutput2.addAsset( CCodeAssetImpl( - id: 'foo', + id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), dynamicLoading: BundledDylibImpl(), os: OSImpl.android, @@ -308,7 +308,7 @@ version: ${BuildOutputImpl.latestVersion}'''), ); buildOutput2.addAssets([ CCodeAssetImpl( - id: 'foo2', + id: 'package:my_package/foo2', dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, From 653dd163701578db49025252c618ee5a31f48c63 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 27 Feb 2024 11:55:04 +0100 Subject: [PATCH 51/75] cleanup --- pkgs/native_assets_cli/lib/src/api/build_output.dart | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 57660436d..9c1878cf6 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -113,11 +113,4 @@ abstract final class BuildOutput { /// The build output is used in the protocol between the Dart and Flutter SDKs /// and packages through `build.dart` invocations. static Version get latestVersion => BuildOutputImpl.latestVersion; - - /// Write out this build output to a file inside - /// [BuildConfig.outputDirectory]. - /// - /// This takes into account the [BuildConfig.latestVersion] to write the build - /// output in a format the SDK that invoked the script supports. - // Future writeToFile({required BuildConfig config}); } From a1a3554447e6ec592a5ecf041767db26b960255a Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 28 Feb 2024 09:16:08 +0100 Subject: [PATCH 52/75] address comments --- pkgs/native_assets_cli/example/local_asset/build.dart | 1 + pkgs/native_assets_cli/lib/native_assets_cli.dart | 1 - pkgs/native_assets_cli/lib/src/api/build.dart | 1 + pkgs/native_assets_cli/lib/src/api/build_config.dart | 3 ++- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 36306a060..71365fe63 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -32,6 +32,7 @@ void main(List args) async { } output.addAsset( + // TODO: Change to DataAsset once the Dart/Flutter SDK can consume it. CCodeAsset( id: 'library:$packageName/asset.txt', file: assetPath, diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 5301ad877..9369cb1b0 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -23,4 +23,3 @@ export 'src/api/ios_sdk.dart' show IOSSdk; export 'src/api/link_mode.dart' show LinkMode; export 'src/api/link_mode_preference.dart' show LinkModePreference; export 'src/api/os.dart' show OS; -// export 'src/api/target.dart' show Target; diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 5b8759c02..50fd43dfb 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -68,6 +68,7 @@ import 'build_output.dart'; /// } /// /// output.addAsset( +/// // TODO: Change to DataAsset once the Dart/Flutter SDK can consume it. /// CCodeAsset( /// id: 'library:$packageName/asset.txt', /// file: assetPath, diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 309bbcf8e..f480703c0 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -90,7 +90,8 @@ abstract final class BuildConfig { /// Not available during a [dryRun]. CCompilerConfig get cCompiler; - /// Whether the current run is a "dry run". + /// Indicates whether a run should build (wet run) or report potential assets + /// (dry run). /// /// If so, the build won't actually be run, but will report the native assets /// which would have been produced. From d82edcdc273b41284ae9f75bbd1e4994edfe6d2c Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 28 Feb 2024 09:19:54 +0100 Subject: [PATCH 53/75] address comment --- .../lib/src/api/build_config.dart | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index f480703c0..8629a595e 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -58,7 +58,8 @@ abstract final class BuildConfig { /// /// Required when [targetOS] equals [OS.iOS]. /// - /// Not available during a [dryRun]. + /// Not available during a [dryRun]. Will throw a [StateError] if accessed + /// during a [dryRun]. IOSSdk? get targetIOSSdk; /// When compiling for Android, the minimum Android SDK API version to that @@ -66,7 +67,8 @@ abstract final class BuildConfig { /// /// Required when [targetOS] equals [OS.android]. /// - /// Not available during a [dryRun]. + /// Not available during a [dryRun]. Will throw a [StateError] if accessed + /// during a [dryRun]. /// /// For more information about the Android API version, refer to /// [`minSdkVersion`](https://developer.android.com/ndk/guides/sdk-versions#minsdkversion) @@ -82,12 +84,14 @@ abstract final class BuildConfig { /// /// Returns `null` if metadata was not provided. /// - /// Not available during a [dryRun]. + /// Not available during a [dryRun]. Will throw a [StateError] if accessed + /// during a [dryRun]. Object? metadatum(String packageName, String key); /// The configuration for invoking the C compiler. /// - /// Not available during a [dryRun]. + /// Not available during a [dryRun]. Will throw a [StateError] if accessed + /// during a [dryRun]. CCompilerConfig get cCompiler; /// Indicates whether a run should build (wet run) or report potential assets @@ -99,7 +103,8 @@ abstract final class BuildConfig { /// The build mode that the code should be compiled in. /// - /// Not available during a [dryRun]. + /// Not available during a [dryRun]. Will throw a [StateError] if accessed + /// during a [dryRun]. BuildMode get buildMode; /// The asset types the invoker of this build supports. From 84d77b457f953fe993fb965fb66c5a23bb785f4b Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 28 Feb 2024 09:44:16 +0100 Subject: [PATCH 54/75] Add Argument/State errors to DynamicLoading with LinkMode.static --- .../lib/src/api/c_code_asset.dart | 4 +- .../lib/src/model/c_code_asset.dart | 49 ++++++++++++++----- .../test/api/asset_test.dart | 37 +++++++++++++- .../test/model/asset_test.dart | 3 -- .../lib/src/cbuilder/cbuilder.dart | 2 +- 5 files changed, 77 insertions(+), 18 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index aa88c11e0..dbe04ac73 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -63,7 +63,7 @@ abstract final class CCodeAsset implements Asset { required String id, required LinkMode linkMode, required OS os, - required DynamicLoading dynamicLoading, + DynamicLoading? dynamicLoading, Uri? file, Architecture? architecture, }) => @@ -72,7 +72,7 @@ abstract final class CCodeAsset implements Asset { linkMode: linkMode as LinkModeImpl, os: os as OSImpl, architecture: architecture as ArchitectureImpl?, - dynamicLoading: dynamicLoading as DynamicLoadingImpl, + dynamicLoading: dynamicLoading as DynamicLoadingImpl?, file: file, ); diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index 9b0976719..fb52a47f2 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -132,7 +132,14 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { final String id; @override - final DynamicLoadingImpl dynamicLoading; + DynamicLoadingImpl get dynamicLoading { + if (linkMode == LinkMode.static) { + throw StateError('LinkMode is LinkMode.static.'); + } + return _dynamicLoading!; + } + + final DynamicLoadingImpl? _dynamicLoading; @override final OSImpl os; @@ -145,14 +152,32 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { required this.id, required this.linkMode, required this.os, - required this.dynamicLoading, + DynamicLoadingImpl? dynamicLoading, this.architecture, - }); + }) : _dynamicLoading = dynamicLoading { + if (linkMode == LinkMode.dynamic && dynamicLoading == null) { + throw ArgumentError.value( + dynamicLoading, + 'dynamicLoading', + 'Must not be null if linkMode == LinkMode.dynamic.', + ); + } + if (linkMode == LinkMode.static && dynamicLoading != null) { + throw ArgumentError.value( + dynamicLoading, + 'dynamicLoading', + 'Must be null if linkMode == LinkMode.static.', + ); + } + } factory CCodeAssetImpl.fromYaml(YamlMap yamlMap) { - final dynamicLoading = DynamicLoadingImpl.fromYaml( - as(yamlMap[_dynamicLoadingKey] ?? yamlMap[_pathKey]), - ); + final linkMode = LinkModeImpl.fromName(as(yamlMap[_linkModeKey])); + final dynamicLoadingYaml = + as(yamlMap[_dynamicLoadingKey] ?? yamlMap[_pathKey]); + final dynamicLoading = dynamicLoadingYaml == null + ? null + : DynamicLoadingImpl.fromYaml(dynamicLoadingYaml); final fileString = as(yamlMap[_fileKey]); final Uri? file; if (fileString != null) { @@ -183,12 +208,13 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { architecture = null; } } + return CCodeAssetImpl( id: as(yamlMap[_idKey]), - dynamicLoading: dynamicLoading, + dynamicLoading: linkMode == LinkMode.dynamic ? dynamicLoading : null, os: os, architecture: architecture, - linkMode: LinkModeImpl.fromName(as(yamlMap[_linkModeKey])), + linkMode: linkMode, file: file, ); } @@ -219,7 +245,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { other.linkMode == linkMode && other.architecture == architecture && other.os == os && - other.dynamicLoading == dynamicLoading && + other._dynamicLoading == _dynamicLoading && other.file == file; } @@ -229,7 +255,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { linkMode, architecture, os, - dynamicLoading, + _dynamicLoading, file, ); @@ -245,7 +271,8 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { } return { if (architecture != null) _architectureKey: architecture.toString(), - _dynamicLoadingKey: dynamicLoading.toYaml(version, file), + if (linkMode == LinkMode.dynamic) + _dynamicLoadingKey: dynamicLoading.toYaml(version, file), if (file != null) _fileKey: file!.toFilePath(), _idKey: id, _linkModeKey: linkMode.name, diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index 5a8cb10b9..fa199a028 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -41,7 +41,6 @@ void main() { CCodeAsset( id: 'package:my_package/bar', file: Uri(path: 'path/to/libbar.a'), - dynamicLoading: BundledDylib(), os: OS.linux, architecture: Architecture.arm64, linkMode: LinkMode.static, @@ -61,4 +60,40 @@ void main() { ]; assets.toString(); }); + + test('LinkMode state errors', () { + expect( + () => CCodeAsset( + id: 'package:my_package/foo', + file: Uri.file('path/to/libfoo.so'), + dynamicLoading: BundledDylib(), + os: OS.android, + architecture: Architecture.x64, + linkMode: LinkMode.static, + ), + throwsArgumentError, + ); + expect( + () => CCodeAsset( + id: 'package:my_package/foo', + file: Uri.file('path/to/libfoo.so'), + os: OS.android, + architecture: Architecture.x64, + linkMode: LinkMode.dynamic, + ), + throwsArgumentError, + ); + + final staticLinkingAsset = CCodeAsset( + id: 'package:my_package/foo', + file: Uri.file('path/to/libfoo.so'), + os: OS.android, + architecture: Architecture.x64, + linkMode: LinkMode.static, + ); + expect( + () => staticLinkingAsset.dynamicLoading, + throwsStateError, + ); + }); } diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index e8043f7c3..e7dcf223a 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -49,7 +49,6 @@ void main() { CCodeAssetImpl( id: 'package:my_package/bar', file: barUri, - dynamicLoading: BundledDylibImpl(), os: OSImpl.linux, architecture: ArchitectureImpl.arm64, linkMode: LinkModeImpl.static, @@ -144,8 +143,6 @@ void main() { os: android type: c_code - architecture: arm64 - dynamic_loading: - type: bundle file: ${barUri.toFilePath()} id: package:my_package/bar link_mode: static diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index ea73dd538..0c08f27e8 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -255,7 +255,7 @@ class CBuilder implements Builder { os: buildConfig.targetOS, architecture: buildConfig.dryRun ? null : buildConfig.targetArchitecture, - dynamicLoading: BundledDylib(), + dynamicLoading: linkMode == LinkMode.dynamic ? BundledDylib() : null, ) ]); } From 818ac1560c8a0fee0ab85b962261bb00dae8cd1d Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 28 Feb 2024 09:57:18 +0100 Subject: [PATCH 55/75] More documentation for CCodeAssets and more errors --- .../native_assets_cli/lib/src/api/c_code_asset.dart | 11 +++++++++++ .../lib/src/model/c_code_asset.dart | 9 +++++++++ pkgs/native_assets_cli/test/api/asset_test.dart | 13 ++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index dbe04ac73..ad5c5b649 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -57,8 +57,19 @@ abstract final class CCodeAsset implements Asset { LinkMode get linkMode; /// The dynamic loading method when the [linkMode] is [LinkMode.dynamic]. + /// + /// Throws a [StateError] when accessed with [linkMode] is [LinkMode.static]. DynamicLoading get dynamicLoading; + /// Consructs a c code asset. + /// + /// If [linkMode] is [LinkMode.dynamic], a non-null [dynamicLoading] must be + /// provided. If [linkMode] is [LinkMode.static], [dynamicLoading] must not be + /// provided. + /// + /// If [linkMode] is [LinkMode.dynamic] and[dynamicLoading] is not + /// [BundledDylib], a [file] must not be provided. If [dynamicLoading] is + /// [BundledDylib], a [file] must be provided in non-[BuildConfig.dryRun]s. factory CCodeAsset({ required String id, required LinkMode linkMode, diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart index fb52a47f2..6f803264c 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart @@ -169,6 +169,15 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { 'Must be null if linkMode == LinkMode.static.', ); } + if (linkMode == LinkMode.dynamic && + dynamicLoading is! BundledDylib && + file != null) { + throw ArgumentError.value( + file, + 'file', + 'Must be null if dynamicLoading is not BundledDylib.', + ); + } } factory CCodeAssetImpl.fromYaml(YamlMap yamlMap) { diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index fa199a028..57a778015 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -61,7 +61,7 @@ void main() { assets.toString(); }); - test('LinkMode state errors', () { + test('Errors', () { expect( () => CCodeAsset( id: 'package:my_package/foo', @@ -83,6 +83,17 @@ void main() { ), throwsArgumentError, ); + expect( + () => CCodeAsset( + id: 'package:my_package/foo', + file: Uri.file('path/to/libfoo.so'), + dynamicLoading: LookupInExecutable(), + os: OS.android, + architecture: Architecture.x64, + linkMode: LinkMode.dynamic, + ), + throwsArgumentError, + ); final staticLinkingAsset = CCodeAsset( id: 'package:my_package/foo', From 03c44d211c04582729f5b637734a09e5389d9534 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 28 Feb 2024 10:19:30 +0100 Subject: [PATCH 56/75] Fix comment --- pkgs/native_assets_cli/lib/src/api/c_code_asset.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index ad5c5b649..08c30a970 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -67,7 +67,7 @@ abstract final class CCodeAsset implements Asset { /// provided. If [linkMode] is [LinkMode.static], [dynamicLoading] must not be /// provided. /// - /// If [linkMode] is [LinkMode.dynamic] and[dynamicLoading] is not + /// If [linkMode] is [LinkMode.dynamic] and [dynamicLoading] is not /// [BundledDylib], a [file] must not be provided. If [dynamicLoading] is /// [BundledDylib], a [file] must be provided in non-[BuildConfig.dryRun]s. factory CCodeAsset({ From bc77f0db6f95244cec9f45c3ce0aa0bf2edd4ecd Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 4 Mar 2024 16:08:21 +0100 Subject: [PATCH 57/75] Split up asset id into package and name in constructors --- .../test_data/native_add/build.dart | 2 +- .../native_add_add_source/build.dart | 2 +- .../test_data/native_subtract/build.dart | 2 +- .../wrong_namespace_asset/build.dart | 3 +- .../example/local_asset/build.dart | 3 +- .../example/native_add_library/build.dart | 2 +- .../example/use_dart_api/build.dart | 2 +- pkgs/native_assets_cli/lib/src/api/asset.dart | 11 ++++--- .../lib/src/api/c_code_asset.dart | 8 +++-- .../lib/src/api/data_asset.dart | 9 +++-- .../lib/src/helpers/asset_downloader.dart | 11 +++---- .../test/api/asset_test.dart | 33 ++++++++++++------- .../test/api/build_output_test.dart | 6 ++-- .../lib/src/cbuilder/cbuilder.dart | 11 ++++--- .../cbuilder/cbuilder_build_failure_test.dart | 2 +- .../cbuilder/cbuilder_cross_android_test.dart | 2 +- .../cbuilder/cbuilder_cross_ios_test.dart | 2 +- .../cbuilder_cross_linux_host_test.dart | 2 +- .../cbuilder_cross_macos_host_test.dart | 2 +- .../cbuilder_cross_windows_host_test.dart | 2 +- .../test/cbuilder/cbuilder_test.dart | 6 ++-- 21 files changed, 73 insertions(+), 50 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/native_add/build.dart b/pkgs/native_assets_builder/test_data/native_add/build.dart index a3d47b362..7693c9f5a 100644 --- a/pkgs/native_assets_builder/test_data/native_add/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add/build.dart @@ -12,7 +12,7 @@ void main(List arguments) async { await build(arguments, (config, output) async { final cbuilder = CBuilder.library( name: packageName, - assetId: + assetName: 'package:$packageName/src/${packageName}_bindings_generated.dart', sources: [ 'src/$packageName.c', diff --git a/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart b/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart index 33cef46bd..763f2950c 100644 --- a/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart @@ -12,7 +12,7 @@ void main(List arguments) async { await build(arguments, (config, output) async { final cbuilder = CBuilder.library( name: packageName, - assetId: + assetName: 'package:$packageName/src/${packageName}_bindings_generated.dart', sources: [ 'src/$packageName.c', diff --git a/pkgs/native_assets_builder/test_data/native_subtract/build.dart b/pkgs/native_assets_builder/test_data/native_subtract/build.dart index 6dae032c0..49b8fa2fe 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/build.dart +++ b/pkgs/native_assets_builder/test_data/native_subtract/build.dart @@ -12,7 +12,7 @@ void main(List arguments) async { await build(arguments, (config, output) async { final cbuilder = CBuilder.library( name: packageName, - assetId: + assetName: 'package:$packageName/src/${packageName}_bindings_generated.dart', sources: [ 'src/$packageName.c', diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 76e8cc2e9..651ff3e02 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -8,7 +8,8 @@ void main(List arguments) async { await build(arguments, (config, output) async { output.addAsset( CCodeAsset( - id: 'package:other_package/foo', + package: 'other_package', + name: 'foo', file: config.outputDirectory.resolve( OS.current.dylibFileName('foo'), ), diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 71365fe63..cc10b1727 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -34,7 +34,8 @@ void main(List args) async { output.addAsset( // TODO: Change to DataAsset once the Dart/Flutter SDK can consume it. CCodeAsset( - id: 'library:$packageName/asset.txt', + package: packageName, + name: 'asset.txt', file: assetPath, linkMode: LinkMode.dynamic, os: config.targetOS, diff --git a/pkgs/native_assets_cli/example/native_add_library/build.dart b/pkgs/native_assets_cli/example/native_add_library/build.dart index f05fe779b..ed2732319 100644 --- a/pkgs/native_assets_cli/example/native_add_library/build.dart +++ b/pkgs/native_assets_cli/example/native_add_library/build.dart @@ -11,7 +11,7 @@ void main(List args) async { final packageName = config.packageName; final cbuilder = CBuilder.library( name: packageName, - assetId: 'package:$packageName/$packageName.dart', + assetName: 'package:$packageName/$packageName.dart', sources: [ 'src/$packageName.c', ], diff --git a/pkgs/native_assets_cli/example/use_dart_api/build.dart b/pkgs/native_assets_cli/example/use_dart_api/build.dart index 61977ddaa..80995b898 100644 --- a/pkgs/native_assets_cli/example/use_dart_api/build.dart +++ b/pkgs/native_assets_cli/example/use_dart_api/build.dart @@ -12,7 +12,7 @@ void main(List arguments) async { await build(arguments, (config, output) async { final cbuilder = CBuilder.library( name: packageName, - assetId: + assetName: 'package:$packageName/src/${packageName}_bindings_generated.dart', sources: [ 'src/$packageName.c', diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index 6a804ba69..e9c9597de 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -32,12 +32,13 @@ abstract final class Asset { /// An [Asset] must have a string identifier called "asset id". Dart code that /// uses an asset, references the asset using this asset id. /// - /// A package must prefix all asset ids it defines with: `package:/`, - /// `` being the current package's name. This ensures assets don't - /// conflict between packages. + /// An asset identifier consists of two elements, the `package` and `name`, + /// which together make a library uri `package:/`. The package + /// being part of the identifer prevents name collisions between assets of + /// different packages. /// - /// Additionally, the default asset id for an asset reference from - /// `lib/src/foo.dart` is `'package:foo/src/foo.dart'`. + /// The default asset id for an asset reference from `lib/src/foo.dart` is + /// `'package:foo/src/foo.dart'`. String get id; /// The file to be bundled with the Dart or Flutter application. diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index 08c30a970..671035698 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -63,6 +63,9 @@ abstract final class CCodeAsset implements Asset { /// Consructs a c code asset. /// + /// The [id] of this asset is a uri `package:/` from [package] + /// and [name]. + /// /// If [linkMode] is [LinkMode.dynamic], a non-null [dynamicLoading] must be /// provided. If [linkMode] is [LinkMode.static], [dynamicLoading] must not be /// provided. @@ -71,7 +74,8 @@ abstract final class CCodeAsset implements Asset { /// [BundledDylib], a [file] must not be provided. If [dynamicLoading] is /// [BundledDylib], a [file] must be provided in non-[BuildConfig.dryRun]s. factory CCodeAsset({ - required String id, + required String package, + required String name, required LinkMode linkMode, required OS os, DynamicLoading? dynamicLoading, @@ -79,7 +83,7 @@ abstract final class CCodeAsset implements Asset { Architecture? architecture, }) => CCodeAssetImpl( - id: id, + id: 'package:$package/$name', linkMode: linkMode as LinkModeImpl, os: os as OSImpl, architecture: architecture as ArchitectureImpl?, diff --git a/pkgs/native_assets_cli/lib/src/api/data_asset.dart b/pkgs/native_assets_cli/lib/src/api/data_asset.dart index 45f76828c..60fe54f36 100644 --- a/pkgs/native_assets_cli/lib/src/api/data_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/data_asset.dart @@ -13,12 +13,17 @@ part of 'asset.dart'; /// An data asset must provide a [Asset.file]. The Dart and Flutter SDK will /// bundle this code in the final application. abstract final class DataAsset implements Asset { + /// Construct a data asset. + /// + /// The [id] of this asset is a uri `package:/` from [package] + /// and [name]. factory DataAsset({ - required String id, + required String package, + required String name, required Uri file, }) => DataAssetImpl( - id: id, + id: 'package:$package/$name', file: file, ); diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart index ce0f384a8..81c55b756 100644 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart @@ -24,11 +24,11 @@ class AssetDownloader implements Builder { /// Asset identifier. /// /// If omitted, no asset will be added to the build output. - final String assetId; + final String assetName; AssetDownloader({ required this.downloadUri, - required this.assetId, + required this.assetName, }); @override @@ -37,10 +37,6 @@ class AssetDownloader implements Builder { required BuildOutput output, required Logger? logger, }) async { - final assetId = this.assetId.startsWith('package:') - ? this.assetId - : 'package:${config.packageName}/${this.assetId}'; - Uri? targetUri; if (!config.dryRun) { final downloadUri2 = downloadUri( @@ -57,7 +53,8 @@ class AssetDownloader implements Builder { output.addAsset( CCodeAsset( - id: assetId, + package: config.packageName, + name: assetName, file: targetUri, linkMode: LinkMode.dynamic, dynamicLoading: BundledDylib(), diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index 57a778015..3b8e95600 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -10,7 +10,8 @@ void main() { test('Asset constructors', () async { final assets = [ CCodeAsset( - id: 'package:my_package/foo', + package: 'my_package', + name: 'foo', file: Uri.file('path/to/libfoo.so'), dynamicLoading: BundledDylib(), os: OS.android, @@ -18,35 +19,40 @@ void main() { linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'package:my_package/foo3', + package: 'my_package', + name: 'foo3', dynamicLoading: SystemDylib(Uri(path: 'libfoo3.so')), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'package:my_package/foo4', + package: 'my_package', + name: 'foo4', dynamicLoading: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'package:my_package/foo5', + package: 'my_package', + name: 'foo5', dynamicLoading: LookupInProcess(), os: OS.android, architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'package:my_package/bar', + package: 'my_package', + name: 'bar', file: Uri(path: 'path/to/libbar.a'), os: OS.linux, architecture: Architecture.arm64, linkMode: LinkMode.static, ), CCodeAsset( - id: 'package:my_package/bla', + package: 'my_package', + name: 'bla', file: Uri(path: 'path/with spaces/bla.dll'), dynamicLoading: BundledDylib(), os: OS.windows, @@ -54,7 +60,8 @@ void main() { linkMode: LinkMode.dynamic, ), DataAsset( - id: 'package:my_package/data/some_text.txt', + package: 'my_package', + name: 'data/some_text.txt', file: Uri(path: 'data/some_text.txt'), ), ]; @@ -64,7 +71,8 @@ void main() { test('Errors', () { expect( () => CCodeAsset( - id: 'package:my_package/foo', + package: 'my_package', + name: 'foo', file: Uri.file('path/to/libfoo.so'), dynamicLoading: BundledDylib(), os: OS.android, @@ -75,7 +83,8 @@ void main() { ); expect( () => CCodeAsset( - id: 'package:my_package/foo', + package: 'my_package', + name: 'foo', file: Uri.file('path/to/libfoo.so'), os: OS.android, architecture: Architecture.x64, @@ -85,7 +94,8 @@ void main() { ); expect( () => CCodeAsset( - id: 'package:my_package/foo', + package: 'my_package', + name: 'foo', file: Uri.file('path/to/libfoo.so'), dynamicLoading: LookupInExecutable(), os: OS.android, @@ -96,7 +106,8 @@ void main() { ); final staticLinkingAsset = CCodeAsset( - id: 'package:my_package/foo', + package: 'my_package', + name: 'foo', file: Uri.file('path/to/libfoo.so'), os: OS.android, architecture: Architecture.x64, diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index 3f1b321f5..b5069b49b 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -23,7 +23,8 @@ void main() { timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ CCodeAsset( - id: 'package:my_package/foo', + package: 'my_package', + name: 'foo', file: Uri(path: 'path/to/libfoo.so'), dynamicLoading: BundledDylib(), os: OS.android, @@ -31,7 +32,8 @@ void main() { linkMode: LinkMode.dynamic, ), CCodeAsset( - id: 'package:my_package/foo2', + package: 'my_package', + name: 'foo2', dynamicLoading: SystemDylib(Uri(path: 'path/to/libfoo2.so')), os: OS.android, architecture: Architecture.x64, diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 0c08f27e8..990bd9cb5 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -55,7 +55,7 @@ class CBuilder implements Builder { /// Used to output the [BuildOutput.assets]. /// /// If omitted, no asset will be added to the build output. - final String? assetId; + final String? assetName; /// Sources to build the library or executable. /// @@ -154,7 +154,7 @@ class CBuilder implements Builder { CBuilder.library({ required this.name, - required this.assetId, + required this.assetName, this.sources = const [], this.includes = const [], this.dartBuildFiles = const ['build.dart'], @@ -183,7 +183,7 @@ class CBuilder implements Builder { this.language = Language.c, this.cppLinkStdLib, }) : _type = _CBuilderType.executable, - assetId = null, + assetName = null, installName = null, pic = pie; @@ -246,10 +246,11 @@ class CBuilder implements Builder { await task.run(); } - if (assetId != null) { + if (assetName != null) { buildOutput.addAssets([ CCodeAsset( - id: assetId!, + package: buildConfig.packageName, + name: assetName!, file: buildConfig.dryRun ? null : libUri, linkMode: linkMode, os: buildConfig.targetOS, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart index b7de76050..828bddb6c 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart @@ -48,7 +48,7 @@ void main() { final cbuilder = CBuilder.library( sources: [addCUri.toFilePath()], name: name, - assetId: name, + assetName: name, ); expect( () => cbuilder.run( diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart index 80693148b..bf37556a5 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart @@ -140,7 +140,7 @@ Future buildLib( final cbuilder = CBuilder.library( name: name, - assetId: name, + assetName: name, sources: [addCUri.toFilePath()], ); await cbuilder.run( diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart index dc8999087..82d836543 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart @@ -72,7 +72,7 @@ void main() { final cbuilder = CBuilder.library( name: name, - assetId: name, + assetName: name, sources: [addCUri.toFilePath()], installName: installName, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart index 1e092b729..5cfe59ccb 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart @@ -59,7 +59,7 @@ void main() { final cbuilder = CBuilder.library( name: name, - assetId: name, + assetName: name, sources: [addCUri.toFilePath()], ); await cbuilder.run( diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart index 530e0db83..5cc9a501f 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart @@ -57,7 +57,7 @@ void main() { final cbuilder = CBuilder.library( name: name, - assetId: name, + assetName: name, sources: [addCUri.toFilePath()], ); await cbuilder.run( diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart index afcb42fbf..65ba2d65e 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart @@ -69,7 +69,7 @@ void main() { final cbuilder = CBuilder.library( name: name, - assetId: name, + assetName: name, sources: [addCUri.toFilePath()], ); await cbuilder.run( diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart index ae3167045..795715314 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart @@ -141,7 +141,7 @@ void main() { final cbuilder = CBuilder.library( sources: [addCUri.toFilePath()], name: name, - assetId: name, + assetName: name, pic: pic, ); await cbuilder.run( @@ -290,7 +290,7 @@ void main() { final cbuilder = CBuilder.library( name: name, - assetId: name, + assetName: name, includes: [includeDirectoryUri.toFilePath()], sources: [includesCUri.toFilePath()], ); @@ -341,7 +341,7 @@ void main() { final cbuilder = CBuilder.library( sources: [addCUri.toFilePath()], name: name, - assetId: name, + assetName: name, std: std, ); await cbuilder.run( From 41163764874788ce3ac5d6eb940ff5cc6b6f606a Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 4 Mar 2024 17:07:21 +0100 Subject: [PATCH 58/75] Address comments --- .../lib/src/build_runner/build_planner.dart | 4 +-- .../lib/src/api/architecture.dart | 11 ++----- pkgs/native_assets_cli/lib/src/api/asset.dart | 29 ++++++++++++++----- pkgs/native_assets_cli/lib/src/api/build.dart | 5 +++- .../lib/src/api/build_config.dart | 19 ++++++------ .../lib/src/api/c_code_asset.dart | 4 +-- .../lib/src/api/data_asset.dart | 2 +- pkgs/native_assets_cli/lib/src/api/os.dart | 9 +----- .../lib/src/model/architecture.dart | 8 ++--- 9 files changed, 47 insertions(+), 44 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart index 492f8a533..0df0cbff3 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart @@ -88,11 +88,11 @@ class PackageGraph { PackageGraph(this.map); - /// Construct a graph from the JSON produced by `dart pub deps --json`. + /// Constructs a graph from the JSON produced by `dart pub deps --json`. factory PackageGraph.fromPubDepsJsonString(String json) => PackageGraph.fromPubDepsJson(jsonDecode(json) as Map); - /// Construct a graph from the JSON produced by `dart pub deps --json`. + /// Constructs a graph from the JSON produced by `dart pub deps --json`. factory PackageGraph.fromPubDepsJson(Map map) { final result = >{}; final packages = map['packages'] as List; diff --git a/pkgs/native_assets_cli/lib/src/api/architecture.dart b/pkgs/native_assets_cli/lib/src/api/architecture.dart index f431487bb..8c78531fe 100644 --- a/pkgs/native_assets_cli/lib/src/api/architecture.dart +++ b/pkgs/native_assets_cli/lib/src/api/architecture.dart @@ -9,7 +9,7 @@ import '../model/target.dart'; part '../model/architecture.dart'; -/// A hardware architecture the Dart VM runs on. +/// A hardware architecture which the Dart VM can run on. abstract final class Architecture { /// The [arm](https://en.wikipedia.org/wiki/ARM_architecture_family) /// architecture. @@ -31,14 +31,7 @@ abstract final class Architecture { static const Architecture x64 = ArchitectureImpl.x64; /// Known values for [Architecture]. - static const List values = [ - arm, - arm64, - ia32, - riscv32, - riscv64, - x64, - ]; + static const List values = ArchitectureImpl.values; /// The current [Architecture]. /// diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index e9c9597de..908caf6f3 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -23,14 +23,12 @@ part '../model/data_asset.dart'; /// Data or code bundled with a Dart or Flutter application. /// /// An asset is data or code which is accessible from a Dart or Flutter -/// application. To access an asset at runtime, the asset [id] is used. This -/// enables access to the asset irrespective of how and where the application is -/// run. +/// application. To access an asset at runtime, the asset [id] is used. abstract final class Asset { /// The identifier for this asset. /// - /// An [Asset] must have a string identifier called "asset id". Dart code that - /// uses an asset, references the asset using this asset id. + /// An [Asset] has a string identifier called "asset id". Dart code that uses + /// an asset references the asset using this asset id. /// /// An asset identifier consists of two elements, the `package` and `name`, /// which together make a library uri `package:/`. The package @@ -38,13 +36,28 @@ abstract final class Asset { /// different packages. /// /// The default asset id for an asset reference from `lib/src/foo.dart` is - /// `'package:foo/src/foo.dart'`. + /// `'package:foo/src/foo.dart'`. For example a [CCodeAsset] can be accessed + /// via `@Native` with the `assetId` argument omitted: + /// + /// ```dart + /// // file package:foo/src/foo.dart + /// @Native() + /// external int add(int a, int b); + /// ``` + /// + /// This will be then automatically expanded to + /// + /// ```dart + /// // file package:foo/src/foo.dart + /// @Native(assetId: 'package:foo/src/foo.dart') + /// external int add(int a, int b); + /// ``` String get id; /// The file to be bundled with the Dart or Flutter application. /// - /// How this file is bundled depends on the subtype [Asset] and the SDK (Dart - /// or Flutter). + /// How this file is bundled depends on the kind of asset, represented by a + /// concrete subtype of [Asset], and the SDK (Dart or Flutter). /// /// The file can be omitted in the [BuildOutput] for [BuildConfig.dryRun]. /// diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 50fd43dfb..77f96a139 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -5,7 +5,10 @@ import 'build_config.dart'; import 'build_output.dart'; -/// Run a native assets build. +/// Runs a native assets build. +/// +/// Can build native assets which are not already available, or expose existing +/// files. Each individual asset is assigned a unique asset ID. /// /// Example using `package:native_toolchain_c`: /// diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 8629a595e..d56597282 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -56,7 +56,7 @@ abstract final class BuildConfig { /// When compiling for iOS, whether to target device or simulator. /// - /// Required when [targetOS] equals [OS.iOS]. + /// Always non-null when [targetOS] is [OS.iOS]. /// /// Not available during a [dryRun]. Will throw a [StateError] if accessed /// during a [dryRun]. @@ -94,20 +94,21 @@ abstract final class BuildConfig { /// during a [dryRun]. CCompilerConfig get cCompiler; - /// Indicates whether a run should build (wet run) or report potential assets - /// (dry run). + /// Whether this run is a dry-run, which doesn't build anything. /// - /// If so, the build won't actually be run, but will report the native assets - /// which would have been produced. + /// A dry-run only reports information about which assets a build would + /// create, but doesn't actually create files. bool get dryRun; - /// The build mode that the code should be compiled in. + /// The [BuildMode] that the code should be compiled in. + /// + /// Currently [BuildMode.debug] and [BuildMode.release] are the only modes. /// /// Not available during a [dryRun]. Will throw a [StateError] if accessed /// during a [dryRun]. BuildMode get buildMode; - /// The asset types the invoker of this build supports. + /// The asset types that the invoker of this build supports. /// /// Currently known values: /// * [CCodeAsset.type] @@ -147,7 +148,7 @@ abstract final class BuildConfig { workingDirectory: workingDirectory, ); - /// Construct a config for a non-dry run by providing values for each field. + /// Constructs a config for a non-dry run by providing values for each field. /// /// `build.dart` hooks will most likely use [BuildConfig.fromArguments]. /// However, for unit testing code which consumes a [BuildConfig], this @@ -192,7 +193,7 @@ abstract final class BuildConfig { supportedAssetTypes: supportedAssetTypes, ); - /// Construct a config for a dry run by providing values for each field. + /// Constructs a config for a dry run by providing values for each field. /// /// `build.dart` hooks will most likely use [BuildConfig.fromArguments]. /// However, for unit testing code which consumes a [BuildConfig], this diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart index 671035698..88b9aa820 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart @@ -4,7 +4,7 @@ part of 'asset.dart'; -/// A code [Asset] which respects the C application binary interface (ABI). +/// A code [Asset] which respects the native application binary interface (ABI). /// /// Typical other languages which produce code assets that respect the C ABI /// include C++ and Rust. @@ -61,7 +61,7 @@ abstract final class CCodeAsset implements Asset { /// Throws a [StateError] when accessed with [linkMode] is [LinkMode.static]. DynamicLoading get dynamicLoading; - /// Consructs a c code asset. + /// Constructs a C code asset. /// /// The [id] of this asset is a uri `package:/` from [package] /// and [name]. diff --git a/pkgs/native_assets_cli/lib/src/api/data_asset.dart b/pkgs/native_assets_cli/lib/src/api/data_asset.dart index 60fe54f36..6726cf583 100644 --- a/pkgs/native_assets_cli/lib/src/api/data_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/data_asset.dart @@ -13,7 +13,7 @@ part of 'asset.dart'; /// An data asset must provide a [Asset.file]. The Dart and Flutter SDK will /// bundle this code in the final application. abstract final class DataAsset implements Asset { - /// Construct a data asset. + /// Constructs a data asset. /// /// The [id] of this asset is a uri `package:/` from [package] /// and [name]. diff --git a/pkgs/native_assets_cli/lib/src/api/os.dart b/pkgs/native_assets_cli/lib/src/api/os.dart index f64124990..66d47a690 100644 --- a/pkgs/native_assets_cli/lib/src/api/os.dart +++ b/pkgs/native_assets_cli/lib/src/api/os.dart @@ -37,14 +37,7 @@ abstract final class OS { static const OS windows = OSImpl.windows; /// Known values for [OS]. - static const List values = [ - android, - fuchsia, - iOS, - linux, - macOS, - windows, - ]; + static const List values = OSImpl.values; /// The default dynamic library file name on this os. String dylibFileName(String name); diff --git a/pkgs/native_assets_cli/lib/src/model/architecture.dart b/pkgs/native_assets_cli/lib/src/model/architecture.dart index f8efffe18..384eb5487 100644 --- a/pkgs/native_assets_cli/lib/src/model/architecture.dart +++ b/pkgs/native_assets_cli/lib/src/model/architecture.dart @@ -57,12 +57,12 @@ final class ArchitectureImpl implements Architecture { @override String toString() => dartPlatform; - static final Map _stringToArchitecture = - Map.fromEntries(ArchitectureImpl.values.map( - (architecture) => MapEntry(architecture.toString(), architecture))); + static final Map _architectureByName = { + for (var architecture in values) architecture.dartPlatform: architecture + }; factory ArchitectureImpl.fromString(String target) => - _stringToArchitecture[target]!; + _architectureByName[target]!; static final ArchitectureImpl current = Target.current.architecture; } From ea8e581368e93280b7037f458d23f85b09e44dc3 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 4 Mar 2024 17:11:32 +0100 Subject: [PATCH 59/75] Fix test --- pkgs/native_assets_cli/example/native_add_library/build.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/native_assets_cli/example/native_add_library/build.dart b/pkgs/native_assets_cli/example/native_add_library/build.dart index ed2732319..86ea4c888 100644 --- a/pkgs/native_assets_cli/example/native_add_library/build.dart +++ b/pkgs/native_assets_cli/example/native_add_library/build.dart @@ -11,7 +11,7 @@ void main(List args) async { final packageName = config.packageName; final cbuilder = CBuilder.library( name: packageName, - assetName: 'package:$packageName/$packageName.dart', + assetName: '$packageName.dart', sources: [ 'src/$packageName.c', ], From 9ed573ffd0ac09d63f2586642a624cfe9bdedc66 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 4 Mar 2024 17:41:40 +0100 Subject: [PATCH 60/75] Fix more tests --- pkgs/native_assets_builder/test_data/native_add/build.dart | 3 +-- .../test_data/native_add_add_source/build.dart | 3 +-- .../native_assets_builder/test_data/native_subtract/build.dart | 3 +-- pkgs/native_assets_cli/example/use_dart_api/build.dart | 3 +-- pkgs/native_assets_cli/lib/src/api/build.dart | 2 +- 5 files changed, 5 insertions(+), 9 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/native_add/build.dart b/pkgs/native_assets_builder/test_data/native_add/build.dart index 7693c9f5a..07118b3c8 100644 --- a/pkgs/native_assets_builder/test_data/native_add/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add/build.dart @@ -12,8 +12,7 @@ void main(List arguments) async { await build(arguments, (config, output) async { final cbuilder = CBuilder.library( name: packageName, - assetName: - 'package:$packageName/src/${packageName}_bindings_generated.dart', + assetName: 'src/${packageName}_bindings_generated.dart', sources: [ 'src/$packageName.c', ], diff --git a/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart b/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart index 763f2950c..aba869dee 100644 --- a/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add_add_source/build.dart @@ -12,8 +12,7 @@ void main(List arguments) async { await build(arguments, (config, output) async { final cbuilder = CBuilder.library( name: packageName, - assetName: - 'package:$packageName/src/${packageName}_bindings_generated.dart', + assetName: '${packageName}_bindings_generated.dart', sources: [ 'src/$packageName.c', 'src/native_multiply.c', diff --git a/pkgs/native_assets_builder/test_data/native_subtract/build.dart b/pkgs/native_assets_builder/test_data/native_subtract/build.dart index 49b8fa2fe..d1b1f6f07 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/build.dart +++ b/pkgs/native_assets_builder/test_data/native_subtract/build.dart @@ -12,8 +12,7 @@ void main(List arguments) async { await build(arguments, (config, output) async { final cbuilder = CBuilder.library( name: packageName, - assetName: - 'package:$packageName/src/${packageName}_bindings_generated.dart', + assetName: 'src/${packageName}_bindings_generated.dart', sources: [ 'src/$packageName.c', ], diff --git a/pkgs/native_assets_cli/example/use_dart_api/build.dart b/pkgs/native_assets_cli/example/use_dart_api/build.dart index 80995b898..a776b5e6d 100644 --- a/pkgs/native_assets_cli/example/use_dart_api/build.dart +++ b/pkgs/native_assets_cli/example/use_dart_api/build.dart @@ -12,8 +12,7 @@ void main(List arguments) async { await build(arguments, (config, output) async { final cbuilder = CBuilder.library( name: packageName, - assetName: - 'package:$packageName/src/${packageName}_bindings_generated.dart', + assetName: 'src/${packageName}_bindings_generated.dart', sources: [ 'src/$packageName.c', 'src/dart_api_dl.c', diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 77f96a139..a13ff7a9c 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -22,7 +22,7 @@ import 'build_output.dart'; /// final packageName = config.packageName; /// final cbuilder = CBuilder.library( /// name: packageName, -/// assetId: 'package:$packageName/$packageName.dart', +/// assetName: '$packageName.dart', /// sources: [ /// 'src/$packageName.c', /// ], From 230be8996f1279d5d37d2562c7134e1ca4e91401 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 4 Mar 2024 18:59:31 +0100 Subject: [PATCH 61/75] address comment --- .../lib/src/api/build_config.dart | 5 +++-- .../lib/src/model/build_config.dart | 15 +++++++++++---- .../test/api/build_config_test.dart | 3 ++- .../test/model/build_config_test.dart | 3 ++- .../lib/src/cbuilder/run_cbuilder.dart | 2 +- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index d56597282..b5c33556e 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -56,11 +56,12 @@ abstract final class BuildConfig { /// When compiling for iOS, whether to target device or simulator. /// - /// Always non-null when [targetOS] is [OS.iOS]. + /// Not available if [targetOS] is [OS.iOS]. Will throw a [StateError] if + /// accessed during a [dryRun]. /// /// Not available during a [dryRun]. Will throw a [StateError] if accessed /// during a [dryRun]. - IOSSdk? get targetIOSSdk; + IOSSdk get targetIOSSdk; /// When compiling for Android, the minimum Android SDK API version to that /// the compiled code will be compatible with. diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 9b5951c77..65217ce9d 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -26,9 +26,14 @@ final class BuildConfigImpl implements BuildConfig { late final OSImpl _targetOS; @override - IOSSdkImpl? get targetIOSSdk { + IOSSdkImpl get targetIOSSdk { _ensureNotDryRun(); - return _targetIOSSdk; + if (_targetOS != OS.iOS) { + throw StateError( + 'This field is not available in if targetOS is not OS.iOS.', + ); + } + return _targetIOSSdk!; } late final IOSSdkImpl? _targetIOSSdk; @@ -497,7 +502,9 @@ final class BuildConfigImpl implements BuildConfig { if (!dryRun) { if (other.buildMode != buildMode) return false; if (other.targetArchitecture != targetArchitecture) return false; - if (other.targetIOSSdk != targetIOSSdk) return false; + if (targetOS == OS.iOS && other.targetIOSSdk != targetIOSSdk) { + return false; + } if (other.targetAndroidNdkApi != targetAndroidNdkApi) return false; if (other.cCompiler != cCompiler) return false; if (!const DeepCollectionEquality() @@ -519,7 +526,7 @@ final class BuildConfigImpl implements BuildConfig { buildMode, const DeepCollectionEquality().hash(_dependencyMetadata), targetArchitecture, - targetIOSSdk, + if (targetOS == OS.iOS) targetIOSSdk, targetAndroidNdkApi, cCompiler, ], diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 7172e5300..3be1baf72 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -82,7 +82,8 @@ void main() async { expect(config1.packageRoot, config2.packageRoot); expect(config1.targetArchitecture == config2.targetArchitecture, true); expect(config1.targetOS != config2.targetOS, true); - expect(config1.targetIOSSdk != config2.targetIOSSdk, true); + expect(config1.targetIOSSdk, IOSSdk.iPhoneOS); + expect(() => config2.targetIOSSdk, throwsStateError); expect(config1.cCompiler.compiler != config2.cCompiler.compiler, true); expect(config1.cCompiler.linker != config2.cCompiler.linker, true); expect(config1.cCompiler.archiver != config2.cCompiler.archiver, true); diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index c8603aa31..64f65b2cf 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -82,7 +82,8 @@ void main() async { expect(config1.packageRoot, config2.packageRoot); expect(config1.targetArchitecture == config2.targetArchitecture, true); expect(config1.targetOS != config2.targetOS, true); - expect(config1.targetIOSSdk != config2.targetIOSSdk, true); + expect(config1.targetIOSSdk, IOSSdkImpl.iPhoneOS); + expect(() => config2.targetIOSSdk, throwsStateError); expect(config1.cCompiler.compiler != config2.cCompiler.compiler, true); expect(config1.cCompiler.linker != config2.cCompiler.linker, true); expect(config1.cCompiler.archiver != config2.cCompiler.archiver, true); diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart index eb556c604..6f5fdbbad 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart @@ -123,7 +123,7 @@ class RunCBuilder { late final IOSSdk targetIosSdk; if (buildConfig.targetOS == OS.iOS) { - targetIosSdk = buildConfig.targetIOSSdk!; + targetIosSdk = buildConfig.targetIOSSdk; } // The Android Gradle plugin does not honor API level 19 and 20 when From ab85d477518acedcfeabe51347c8f6994cf94d3f Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 4 Mar 2024 19:05:30 +0100 Subject: [PATCH 62/75] add test --- .../test/model/build_config_test.dart | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index 64f65b2cf..e0c8381c5 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -664,4 +664,23 @@ version: 1.0.0'''; buildConfig.toYamlString(); // No crash. }); + + test('invalid architecture', () { + final config = Config(fileParsed: { + 'build_mode': 'release', + 'dry_run': false, + 'link_mode_preference': 'prefer-static', + 'out_dir': outDirUri.toFilePath(), + 'package_name': packageName, + 'package_root': packageRootUri.toFilePath(), + 'target_android_ndk_api': 30, + 'target_architecture': 'invalid_architecture', + 'target_os': 'android', + 'version': BuildOutputImpl.latestVersion.toString(), + }); + expect( + () => BuildConfigImpl.fromConfig(config), + throwsFormatException, + ); + }); } From 0ef0dccb683934fab1e6d6752cd8f532d86c6810 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 12 Mar 2024 12:02:13 +0100 Subject: [PATCH 63/75] Remove `AssetDownloader` for now --- .../lib/src/helpers/asset_downloader.dart | 66 ------------------- 1 file changed, 66 deletions(-) delete mode 100644 pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart diff --git a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart b/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart deleted file mode 100644 index 81c55b756..000000000 --- a/pkgs/native_assets_cli/lib/src/helpers/asset_downloader.dart +++ /dev/null @@ -1,66 +0,0 @@ -// 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:io'; - -import 'package:logging/logging.dart'; - -import '../../native_assets_cli.dart'; - -abstract class Builder { - Future run({ - required BuildConfig config, - required BuildOutput output, - required Logger? logger, - }); -} - -// TODO(dacoharkes): Should we really add this? It seems too specific. -// E.g. what about varying file names, zipped downloads etc. etc. ? -class AssetDownloader implements Builder { - final Uri Function(OS, Architecture) downloadUri; - - /// Asset identifier. - /// - /// If omitted, no asset will be added to the build output. - final String assetName; - - AssetDownloader({ - required this.downloadUri, - required this.assetName, - }); - - @override - Future run({ - required BuildConfig config, - required BuildOutput output, - required Logger? logger, - }) async { - Uri? targetUri; - if (!config.dryRun) { - final downloadUri2 = downloadUri( - config.targetOS, - config.targetArchitecture!, - ); - final fileName = - downloadUri2.pathSegments.lastWhere((element) => element.isNotEmpty); - targetUri = config.outputDirectory.resolve(fileName); - final request = await HttpClient().getUrl(downloadUri2); - final response = await request.close(); - await response.pipe(File.fromUri(targetUri).openWrite()); - } - - output.addAsset( - CCodeAsset( - package: config.packageName, - name: assetName, - file: targetUri, - linkMode: LinkMode.dynamic, - dynamicLoading: BundledDylib(), - os: config.targetOS, - architecture: config.targetArchitecture, - ), - ); - } -} From c66a963dbbcd4a98dc9a416f6372ce7e81c5aec7 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 12 Mar 2024 12:17:46 +0100 Subject: [PATCH 64/75] Make BuildConfig loading sync --- .../test_data/cyclic_package_1/pubspec.yaml | 2 +- .../test_data/cyclic_package_2/pubspec.yaml | 2 +- .../test_data/native_add/pubspec.yaml | 2 +- .../test_data/native_add_add_source/pubspec.yaml | 2 +- .../test_data/native_subtract/pubspec.yaml | 2 +- .../test_data/package_reading_metadata/pubspec.yaml | 2 +- .../test_data/package_with_metadata/pubspec.yaml | 2 +- .../test_data/wrong_build_output/build.dart | 2 +- .../test_data/wrong_build_output/pubspec.yaml | 2 +- .../test_data/wrong_build_output_2/build.dart | 2 +- .../test_data/wrong_build_output_2/pubspec.yaml | 2 +- .../test_data/wrong_build_output_3/build.dart | 2 +- .../test_data/wrong_build_output_3/pubspec.yaml | 2 +- .../test_data/wrong_namespace_asset/pubspec.yaml | 2 +- .../native_assets_cli/example/local_asset/pubspec.yaml | 2 +- .../example/native_add_library/pubspec.yaml | 2 +- .../example/use_dart_api/pubspec.yaml | 2 +- pkgs/native_assets_cli/lib/src/api/build.dart | 2 +- pkgs/native_assets_cli/lib/src/api/build_config.dart | 10 +++++----- pkgs/native_assets_cli/lib/src/model/build_config.dart | 8 ++++---- pkgs/native_assets_cli/pubspec.yaml | 2 +- pkgs/native_assets_cli/test/api/build_config_test.dart | 2 +- .../test/model/build_config_test.dart | 2 +- pkgs/native_toolchain_c/pubspec.yaml | 2 +- 24 files changed, 31 insertions(+), 31 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml b/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml index a57866f39..d3e6537d9 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 cyclic_package_2: path: ../cyclic_package_2 native_assets_cli: diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml b/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml index e4c64eed4..12784e248 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 cyclic_package_1: path: ../cyclic_package_1 native_assets_cli: diff --git a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml index 1c83e3978..e9b1cba22 100644 --- a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml index fb456ebf0..ee7c8b48e 100644 --- a/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml index 1f2d7f1dd..16b271e34 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml b/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml index 0f826078c..c62d0db25 100644 --- a/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ package_with_metadata: diff --git a/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml b/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml index 44baf66ef..0f13531ad 100644 --- a/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart index e5d70370d..77ba0abf2 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart @@ -7,7 +7,7 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { - final buildConfig = await BuildConfigImpl.fromArgs(args); + final buildConfig = await BuildConfigImpl.fromArguments(args); await File.fromUri( buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) .writeAsString(_wrongContents); diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml index 34ee56f1a..6d49bf1fe 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart index d2f8f45bd..b0ab298f6 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart @@ -7,7 +7,7 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { - final buildConfig = await BuildConfigImpl.fromArgs(args); + final buildConfig = await BuildConfigImpl.fromArguments(args); await File.fromUri( buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) .writeAsString(_wrongContents); diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml index 04cb18c78..00e064e5b 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart index 6963e133d..9089375c0 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart @@ -7,7 +7,7 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { - final buildConfig = await BuildConfigImpl.fromArgs(args); + final buildConfig = await BuildConfigImpl.fromArguments(args); await File.fromUri( buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) .writeAsString(_rightContents); diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml index 762410904..2068586f0 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml index 44e052ab0..52d12276c 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_cli/example/local_asset/pubspec.yaml b/pkgs/native_assets_cli/example/local_asset/pubspec.yaml index 626ce2376..63988d623 100644 --- a/pkgs/native_assets_cli/example/local_asset/pubspec.yaml +++ b/pkgs/native_assets_cli/example/local_asset/pubspec.yaml @@ -9,7 +9,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml b/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml index b87654a41..f806f139f 100644 --- a/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml +++ b/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml @@ -9,7 +9,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml b/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml index 33bee4050..476cedecb 100644 --- a/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml +++ b/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index a13ff7a9c..d15cb3b40 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -88,7 +88,7 @@ Future build( List arguments, Future Function(BuildConfig config, BuildOutput output) builder, ) async { - final config = await BuildConfig.fromArguments(arguments); + final config = BuildConfig.fromArguments(arguments); final output = BuildOutputImpl(); await builder(config, output); await output.writeToFile(config: config); diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index b5c33556e..b530cfa90 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -128,7 +128,7 @@ abstract final class BuildConfig { /// Constructs a config by parsing CLI arguments and loading the config file. /// - /// The [args] must be commandline arguments. + /// The [arguments] must be commandline arguments. /// /// If provided, [environment] must be a map containing environment variables. /// If not provided, [environment] defaults to [Platform.environment]. @@ -138,13 +138,13 @@ abstract final class BuildConfig { /// If not provided, [workingDirectory] defaults to [Directory.current]. /// /// This async constructor is intended to be used directly in CLI files. - static Future fromArguments( - List args, { + factory BuildConfig.fromArguments( + List arguments, { Map? environment, Uri? workingDirectory, }) => - BuildConfigImpl.fromArgs( - args, + BuildConfigImpl.fromArguments( + arguments, environment: environment, workingDirectory: workingDirectory, ); diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 65217ce9d..278d2cc06 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -241,13 +241,13 @@ final class BuildConfigImpl implements BuildConfig { return result; } - static Future fromArgs( + static BuildConfigImpl fromArguments( List args, { Map? environment, Uri? workingDirectory, - }) async { - final config = await Config.fromArgs( - args: args, + }) { + final config = Config.fromArgumentsSync( + arguments: args, environment: environment, workingDirectory: workingDirectory, ); diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index b11ee7cb1..fe02649a9 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -16,7 +16,7 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 collection: ^1.17.1 crypto: ^3.0.3 logging: ^1.2.0 diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 3be1baf72..2c7c11fbe 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -210,7 +210,7 @@ void main() async { final configUri = tempUri.resolve('config.yaml'); final configFile = File.fromUri(configUri); await configFile.writeAsString(configFileContents); - final buildConfigFromArgs = await BuildConfig.fromArguments( + final buildConfigFromArgs = BuildConfig.fromArguments( ['--config', configUri.toFilePath()], environment: {}, // Don't inherit the test environment. ); diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index e0c8381c5..e44851de2 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -446,7 +446,7 @@ version: 1.0.0'''; final configUri = tempUri.resolve('config.yaml'); final configFile = File.fromUri(configUri); await configFile.writeAsString(configFileContents); - final buildConfig2 = await BuildConfigImpl.fromArgs( + final buildConfig2 = BuildConfigImpl.fromArguments( ['--config', configUri.toFilePath()], environment: {}, // Don't inherit the test environment. ); diff --git a/pkgs/native_toolchain_c/pubspec.yaml b/pkgs/native_toolchain_c/pubspec.yaml index 42c8fdfc2..1f22c5349 100644 --- a/pkgs/native_toolchain_c/pubspec.yaml +++ b/pkgs/native_toolchain_c/pubspec.yaml @@ -17,7 +17,7 @@ environment: publish_to: none dependencies: - cli_config: ^0.1.1 + cli_config: ^0.2.0 glob: ^2.1.1 logging: ^1.1.1 meta: ^1.9.1 From 7dab2e081ea2a4a17ff7e732e81e027c211abd93 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 12 Mar 2024 12:30:25 +0100 Subject: [PATCH 65/75] Remove unused imports --- .../test_data/cyclic_package_1/pubspec.yaml | 1 - .../test_data/cyclic_package_2/pubspec.yaml | 1 - pkgs/native_assets_builder/test_data/native_add/pubspec.yaml | 1 - .../test_data/native_add_add_source/pubspec.yaml | 1 - .../native_assets_builder/test_data/native_subtract/pubspec.yaml | 1 - .../test_data/package_reading_metadata/pubspec.yaml | 1 - .../test_data/package_with_metadata/pubspec.yaml | 1 - .../test_data/wrong_build_output/pubspec.yaml | 1 - .../test_data/wrong_build_output_2/pubspec.yaml | 1 - .../test_data/wrong_build_output_3/pubspec.yaml | 1 - .../test_data/wrong_namespace_asset/pubspec.yaml | 1 - pkgs/native_assets_cli/example/local_asset/pubspec.yaml | 1 - pkgs/native_assets_cli/example/native_add_library/pubspec.yaml | 1 - pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml | 1 - pkgs/native_toolchain_c/pubspec.yaml | 1 - 15 files changed, 15 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml b/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml index d3e6537d9..266a06602 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 cyclic_package_2: path: ../cyclic_package_2 native_assets_cli: diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml b/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml index 12784e248..fa798a453 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 cyclic_package_1: path: ../cyclic_package_1 native_assets_cli: diff --git a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml index e9b1cba22..461a6e9ec 100644 --- a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml index ee7c8b48e..94a7c0e61 100644 --- a/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add_add_source/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml index 16b271e34..78a51acc9 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml b/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml index c62d0db25..679d9c119 100644 --- a/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/package_reading_metadata/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ package_with_metadata: diff --git a/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml b/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml index 0f13531ad..577487967 100644 --- a/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/package_with_metadata/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml index 6d49bf1fe..e752f2ab2 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_build_output/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml index 00e064e5b..bb3708d01 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_2/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml index 2068586f0..e56d04670 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_3/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml index 52d12276c..f48f8a63c 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 native_assets_cli: path: ../../../native_assets_cli/ yaml: ^3.1.1 diff --git a/pkgs/native_assets_cli/example/local_asset/pubspec.yaml b/pkgs/native_assets_cli/example/local_asset/pubspec.yaml index 63988d623..392fe0021 100644 --- a/pkgs/native_assets_cli/example/local_asset/pubspec.yaml +++ b/pkgs/native_assets_cli/example/local_asset/pubspec.yaml @@ -9,7 +9,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml b/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml index f806f139f..f80bf153b 100644 --- a/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml +++ b/pkgs/native_assets_cli/example/native_add_library/pubspec.yaml @@ -9,7 +9,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml b/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml index 476cedecb..6701efa43 100644 --- a/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml +++ b/pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml @@ -8,7 +8,6 @@ environment: sdk: '>=3.3.0 <4.0.0' dependencies: - cli_config: ^0.2.0 logging: ^1.1.1 native_assets_cli: path: ../../../native_assets_cli/ diff --git a/pkgs/native_toolchain_c/pubspec.yaml b/pkgs/native_toolchain_c/pubspec.yaml index 1f22c5349..4015fc05b 100644 --- a/pkgs/native_toolchain_c/pubspec.yaml +++ b/pkgs/native_toolchain_c/pubspec.yaml @@ -17,7 +17,6 @@ environment: publish_to: none dependencies: - cli_config: ^0.2.0 glob: ^2.1.1 logging: ^1.1.1 meta: ^1.9.1 From 8fa2b2e82e8465c47e202728feec0c06ac05c7e9 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 12 Mar 2024 14:22:15 +0100 Subject: [PATCH 66/75] Make `BuildConfig.fromArguments` the default constructor --- .../package_reading_metadata/build.dart | 2 +- pkgs/native_assets_cli/lib/src/api/build.dart | 2 +- .../lib/src/api/build_config.dart | 22 +++++++++---------- .../lib/src/model/build_config.dart | 2 +- .../test/api/build_config_test.dart | 14 ++++++------ .../test/api/build_test.dart | 2 +- .../cbuilder/cbuilder_build_failure_test.dart | 2 +- .../cbuilder/cbuilder_cross_android_test.dart | 2 +- .../cbuilder/cbuilder_cross_ios_test.dart | 2 +- .../cbuilder_cross_linux_host_test.dart | 2 +- .../cbuilder_cross_macos_host_test.dart | 2 +- .../cbuilder_cross_windows_host_test.dart | 2 +- .../test/cbuilder/cbuilder_test.dart | 16 +++++++------- .../test/cbuilder/compiler_resolver_test.dart | 4 ++-- 14 files changed, 38 insertions(+), 38 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart index 06a6d27ad..e5278254b 100644 --- a/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart +++ b/pkgs/native_assets_builder/test_data/package_reading_metadata/build.dart @@ -5,7 +5,7 @@ import 'package:native_assets_cli/native_assets_cli.dart'; void main(List args) async { - final buildConfig = await BuildConfig.fromArguments(args); + final buildConfig = BuildConfig(args); if (!buildConfig.dryRun) { final someValue = buildConfig.metadatum('package_with_metadata', 'some_key'); diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index d15cb3b40..6ce069762 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -88,7 +88,7 @@ Future build( List arguments, Future Function(BuildConfig config, BuildOutput output) builder, ) async { - final config = BuildConfig.fromArguments(arguments); + final config = BuildConfig(arguments); final output = BuildOutputImpl(); await builder(config, output); await output.writeToFile(config: config); diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index b530cfa90..76e4996d8 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -138,7 +138,7 @@ abstract final class BuildConfig { /// If not provided, [workingDirectory] defaults to [Directory.current]. /// /// This async constructor is intended to be used directly in CLI files. - factory BuildConfig.fromArguments( + factory BuildConfig( List arguments, { Map? environment, Uri? workingDirectory, @@ -151,16 +151,16 @@ abstract final class BuildConfig { /// Constructs a config for a non-dry run by providing values for each field. /// - /// `build.dart` hooks will most likely use [BuildConfig.fromArguments]. - /// However, for unit testing code which consumes a [BuildConfig], this - /// constructor facilitates easy construction. + /// `build.dart` hooks will most likely use the unnamed constructor. However, + /// for unit testing code which consumes a [BuildConfig], this constructor + /// facilitates easy construction. /// /// For the documentation of the parameters, see the equally named fields. /// - /// Parameter [dependencyMetadata] must be a nested map - /// `{'packageName' : {'key' : 'value'}}` - /// where `packageName` and `key` correspond to the parameters in [metadatum]. - factory BuildConfig({ + /// Parameter [dependencyMetadata] must be a nested map `{'packageName' : + /// {'key' : 'value'}}` where `packageName` and `key` correspond to the + /// parameters in [metadatum]. + factory BuildConfig.build({ required Uri outputDirectory, required String packageName, required Uri packageRoot, @@ -196,9 +196,9 @@ abstract final class BuildConfig { /// Constructs a config for a dry run by providing values for each field. /// - /// `build.dart` hooks will most likely use [BuildConfig.fromArguments]. - /// However, for unit testing code which consumes a [BuildConfig], this - /// constructor facilitates easy construction. + /// `build.dart` hooks will most likely use the unnamed constructor. However, + /// for unit testing code which consumes a [BuildConfig], this constructor + /// facilitates easy construction. /// /// For the documentation of the parameters, see the equally named fields. factory BuildConfig.dryRun({ diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 278d2cc06..93f2c2c50 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -533,7 +533,7 @@ final class BuildConfigImpl implements BuildConfig { ]); @override - String toString() => 'BuildConfig(${toYaml()})'; + String toString() => 'BuildConfig.build(${toYaml()})'; void _ensureNotDryRun() { if (dryRun) { diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 2c7c11fbe..87780a6d9 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -47,7 +47,7 @@ void main() async { }); test('BuildConfig ==', () { - final config1 = BuildConfig( + final config1 = BuildConfig.build( outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, @@ -64,7 +64,7 @@ void main() async { supportedAssetTypes: [CCodeAsset.type], ); - final config2 = BuildConfig( + final config2 = BuildConfig.build( outputDirectory: outDir2Uri, packageName: packageName, packageRoot: tempUri, @@ -96,7 +96,7 @@ void main() async { }); test('BuildConfig fromConfig', () { - final buildConfig2 = BuildConfig( + final buildConfig2 = BuildConfig.build( outputDirectory: outDirUri, packageName: packageName, packageRoot: packageRootUri, @@ -149,7 +149,7 @@ void main() async { }); test('BuildConfig == dependency metadata', () { - final buildConfig1 = BuildConfig( + final buildConfig1 = BuildConfig.build( outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, @@ -169,7 +169,7 @@ void main() async { }, ); - final buildConfig2 = BuildConfig( + final buildConfig2 = BuildConfig.build( outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, @@ -196,7 +196,7 @@ void main() async { }); test('BuildConfig fromArgs', () async { - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, @@ -210,7 +210,7 @@ void main() async { final configUri = tempUri.resolve('config.yaml'); final configFile = File.fromUri(configUri); await configFile.writeAsString(configFileContents); - final buildConfigFromArgs = BuildConfig.fromArguments( + final buildConfigFromArgs = BuildConfig( ['--config', configUri.toFilePath()], environment: {}, // Don't inherit the test environment. ); diff --git a/pkgs/native_assets_cli/test/api/build_test.dart b/pkgs/native_assets_cli/test/api/build_test.dart index 3207e14e8..85b74d8ee 100644 --- a/pkgs/native_assets_cli/test/api/build_test.dart +++ b/pkgs/native_assets_cli/test/api/build_test.dart @@ -40,7 +40,7 @@ void main() async { fakeVcVars = tempUri.resolve('vcvarsall.bat'); await File.fromUri(fakeVcVars).create(); - final config1 = BuildConfig( + final config1 = BuildConfig.build( outputDirectory: outDirUri, packageName: packageName, packageRoot: tempUri, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart index 828bddb6c..d7aa14d6b 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_build_failure_test.dart @@ -29,7 +29,7 @@ void main() { await File.fromUri(addCUri).writeAsString(addCBrokenContents); const name = 'add'; - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart index bf37556a5..81e9ced4a 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart @@ -124,7 +124,7 @@ Future buildLib( final addCUri = packageUri.resolve('test/cbuilder/testfiles/add/src/add.c'); const name = 'add'; - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart index 82d836543..dcf8d3fa6 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart @@ -56,7 +56,7 @@ void main() { final tempUri = await tempDirForTest(); final addCUri = packageUri.resolve('test/cbuilder/testfiles/add/src/add.c'); - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart index 5cfe59ccb..bb5aa5d5f 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart @@ -44,7 +44,7 @@ void main() { packageUri.resolve('test/cbuilder/testfiles/add/src/add.c'); const name = 'add'; - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart index 5cc9a501f..4405054f0 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart @@ -42,7 +42,7 @@ void main() { packageUri.resolve('test/cbuilder/testfiles/add/src/add.c'); const name = 'add'; - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart index 65ba2d65e..4aeddc0d1 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart @@ -54,7 +54,7 @@ void main() { packageUri.resolve('test/cbuilder/testfiles/add/src/add.c'); const name = 'add'; - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart index 795715314..b801a1e5a 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_test.dart @@ -43,7 +43,7 @@ void main() { final logMessages = []; final logger = createCapturingLogger(logMessages); - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, @@ -122,7 +122,7 @@ void main() { targetOS: OS.current, linkModePreference: LinkModePreference.dynamic, ) - : BuildConfig( + : BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, @@ -214,7 +214,7 @@ void main() { final logMessages = []; final logger = createCapturingLogger(logMessages); - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, @@ -272,7 +272,7 @@ void main() { packageUri.resolve('test/cbuilder/testfiles/includes/src/includes.c'); const name = 'includes'; - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, @@ -317,7 +317,7 @@ void main() { final logMessages = []; final logger = createCapturingLogger(logMessages); - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, @@ -375,7 +375,7 @@ void main() { final logMessages = []; final logger = createCapturingLogger(logMessages); - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( buildMode: BuildMode.release, outputDirectory: tempUri, packageName: name, @@ -439,7 +439,7 @@ void main() { final logMessages = []; final logger = createCapturingLogger(logMessages); - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( buildMode: BuildMode.release, outputDirectory: tempUri, packageName: name, @@ -510,7 +510,7 @@ Future testDefines({ } const name = 'defines'; - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: name, packageRoot: tempUri, diff --git a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart index 854154c1b..328412f72 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/compiler_resolver_test.dart @@ -40,7 +40,7 @@ void main() { final envScript = [ ...await vcvars64.defaultResolver!.resolve(logger: logger) ].firstOrNull?.uri; - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: 'dummy', packageRoot: tempUri, @@ -64,7 +64,7 @@ void main() { test('No compiler found', () async { final tempUri = await tempDirForTest(); - final buildConfig = BuildConfig( + final buildConfig = BuildConfig.build( outputDirectory: tempUri, packageName: 'dummy', packageRoot: tempUri, From 491cc2572617e9ba62d9eacc4210b509950fc3e3 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 12 Mar 2024 14:37:03 +0100 Subject: [PATCH 67/75] Rename `CCodeAsset` to `NativeCodeAsset` --- .../lib/src/build_runner/build_runner.dart | 6 +-- .../build_runner_caching_test.dart | 10 +++-- .../build_runner_dry_run_test.dart | 5 ++- .../build_runner_failure_test.dart | 6 ++- .../test/build_runner/helpers.dart | 6 +-- .../packaging_preference_test.dart | 8 ++-- .../wrong_namespace_asset/build.dart | 2 +- .../example/local_asset/build.dart | 2 +- .../lib/native_assets_cli.dart | 2 +- .../lib/native_assets_cli_internal.dart | 2 +- pkgs/native_assets_cli/lib/src/api/asset.dart | 6 +-- pkgs/native_assets_cli/lib/src/api/build.dart | 2 +- .../lib/src/api/build_config.dart | 4 +- .../lib/src/api/build_output.dart | 2 +- .../lib/src/api/link_mode.dart | 2 +- .../lib/src/api/link_mode_preference.dart | 2 +- ...code_asset.dart => native_code_asset.dart} | 23 +++++------ .../lib/src/model/asset.dart | 6 +-- .../lib/src/model/build_config.dart | 4 +- .../lib/src/model/build_config_CHANGELOG.md | 2 +- .../lib/src/model/build_output_CHANGELOG.md | 2 +- ...code_asset.dart => native_code_asset.dart} | 19 +++++----- .../test/api/asset_test.dart | 20 +++++----- .../test/api/build_config_test.dart | 6 +-- .../test/api/build_output_test.dart | 4 +- .../test/model/asset_test.dart | 38 +++++++++---------- .../test/model/build_config_test.dart | 6 +-- .../test/model/build_output_test.dart | 24 ++++++------ .../lib/src/cbuilder/cbuilder.dart | 2 +- 29 files changed, 115 insertions(+), 108 deletions(-) rename pkgs/native_assets_cli/lib/src/api/{c_code_asset.dart => native_code_asset.dart} (87%) rename pkgs/native_assets_cli/lib/src/model/{c_code_asset.dart => native_code_asset.dart} (95%) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 15f589ea9..bf6fa3ca2 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -182,7 +182,7 @@ class NativeAssetsBuildRunner { ); for (final asset in packageAssets) { switch (asset) { - case CCodeAssetImpl _: + case NativeCodeAssetImpl _: if (asset.architecture != null) { // Backwards compatibility, if an architecture is provided use it. assets.add(asset); @@ -315,7 +315,7 @@ build_output.yaml contained a format error. ${e.message} '''); success = false; - return ([], [], const Metadata({}), false); + return ([], [], const Metadata({}), false); // TODO(https://github.com/dart-lang/native/issues/109): Stop throwing // type errors in native_assets_cli, release a new version of that package // and then remove this. @@ -326,7 +326,7 @@ Building native assets for package:${config.packageName} failed. build_output.yaml contained a format error. '''); success = false; - return ([], [], const Metadata({}), false); + return ([], [], const Metadata({}), false); } finally { if (!success) { final buildOutputFile = diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart index 4e9d2b211..d5774cd10 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart @@ -76,7 +76,8 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); await expectSymbols( - asset: result.assets.single as CCodeAssetImpl, symbols: ['add']); + asset: result.assets.single as NativeCodeAssetImpl, + symbols: ['add']); } await copyTestProjects( @@ -87,7 +88,7 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); await expectSymbols( - asset: result.assets.single as CCodeAssetImpl, + asset: result.assets.single as NativeCodeAssetImpl, symbols: ['add', 'subtract'], ); } @@ -104,7 +105,8 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); await expectSymbols( - asset: result.assets.single as CCodeAssetImpl, symbols: ['add']); + asset: result.assets.single as NativeCodeAssetImpl, + symbols: ['add']); } await copyTestProjects( @@ -114,7 +116,7 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); await expectSymbols( - asset: result.assets.single as CCodeAssetImpl, + asset: result.assets.single as NativeCodeAssetImpl, symbols: ['add', 'multiply']); } }); diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart index f70304306..a7b4614b9 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart @@ -42,10 +42,11 @@ void main() async { final dryRunAsset = dryRunAssets[i]; final buildAsset = result.assets[0]; expect(dryRunAsset.id, buildAsset.id); - // The build runner expands CCodeAssets to all architectures. + // The build runner expands NativeCodeAssets to all architectures. expect(buildAsset.file, isNotNull); expect(dryRunAsset.file, isNull); - if (dryRunAsset is CCodeAssetImpl && buildAsset is CCodeAssetImpl) { + if (dryRunAsset is NativeCodeAssetImpl && + buildAsset is NativeCodeAssetImpl) { expect(dryRunAsset.architecture, isNotNull); expect(buildAsset.architecture, isNotNull); expect(dryRunAsset.os, buildAsset.os); diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart index 53130e2a5..2355353ed 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart @@ -28,7 +28,8 @@ void main() async { final result = await build(packageUri, logger, dartExecutable); expect(result.assets.length, 1); await expectSymbols( - asset: result.assets.single as CCodeAssetImpl, symbols: ['add']); + asset: result.assets.single as NativeCodeAssetImpl, + symbols: ['add']); expect( result.dependencies, [ @@ -72,7 +73,8 @@ void main() async { final result = await build(packageUri, logger, dartExecutable); expect(result.assets.length, 1); await expectSymbols( - asset: result.assets.single as CCodeAssetImpl, symbols: ['add']); + asset: result.assets.single as NativeCodeAssetImpl, + symbols: ['add']); expect( result.dependencies, [ diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index e830af761..6dab3951e 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -61,7 +61,7 @@ Future build( runPackageName: runPackageName, ); if (result.success) { - await expectAssetsExist(result.assets.cast()); + await expectAssetsExist(result.assets.cast()); } if (subscription != null) { @@ -105,7 +105,7 @@ Future dryRun( return result; } -Future expectAssetsExist(List assets) async { +Future expectAssetsExist(List assets) async { for (final asset in assets) { final uri = asset.file!; expect( @@ -118,7 +118,7 @@ Future expectAssetsExist(List assets) async { } Future expectSymbols({ - required CCodeAssetImpl asset, + required NativeCodeAssetImpl asset, required List symbols, }) async { if (Platform.isLinux) { diff --git a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart index 375c8d126..777fd459b 100644 --- a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart @@ -52,19 +52,19 @@ void main() async { // This package honors preferences. expect( - (resultDynamic.assets.single as CCodeAssetImpl).linkMode, + (resultDynamic.assets.single as NativeCodeAssetImpl).linkMode, LinkModeImpl.dynamic, ); expect( - (resultPreferDynamic.assets.single as CCodeAssetImpl).linkMode, + (resultPreferDynamic.assets.single as NativeCodeAssetImpl).linkMode, LinkModeImpl.dynamic, ); expect( - (resultStatic.assets.single as CCodeAssetImpl).linkMode, + (resultStatic.assets.single as NativeCodeAssetImpl).linkMode, LinkModeImpl.static, ); expect( - (resultPreferStatic.assets.single as CCodeAssetImpl).linkMode, + (resultPreferStatic.assets.single as NativeCodeAssetImpl).linkMode, LinkModeImpl.static, ); }); diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 651ff3e02..3a66de276 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -7,7 +7,7 @@ import 'package:native_assets_cli/native_assets_cli.dart'; void main(List arguments) async { await build(arguments, (config, output) async { output.addAsset( - CCodeAsset( + NativeCodeAsset( package: 'other_package', name: 'foo', file: config.outputDirectory.resolve( diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index cc10b1727..3afc1aac4 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -33,7 +33,7 @@ void main(List args) async { output.addAsset( // TODO: Change to DataAsset once the Dart/Flutter SDK can consume it. - CCodeAsset( + NativeCodeAsset( package: packageName, name: 'asset.txt', file: assetPath, diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 9369cb1b0..af16ae9f7 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -11,9 +11,9 @@ export 'src/api/asset.dart' show Asset, BundledDylib, - CCodeAsset, LookupInExecutable, LookupInProcess, + NativeCodeAsset, SystemDylib; export 'src/api/build.dart'; export 'src/api/build_config.dart' show BuildConfig, CCompilerConfig; diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index 1b6038eff..c79cb9f6a 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -21,10 +21,10 @@ export 'src/api/asset.dart' show AssetImpl, BundledDylibImpl, - CCodeAssetImpl, DataAssetImpl, LookupInExecutableImpl, LookupInProcessImpl, + NativeCodeAssetImpl, SystemDylibImpl; export 'src/api/build_config.dart' show BuildConfigImpl, CCompilerConfigImpl; export 'src/api/build_mode.dart' show BuildModeImpl; diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index 908caf6f3..dc9b5c3e3 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -14,10 +14,10 @@ import 'build_output.dart'; import 'link_mode.dart'; import 'os.dart'; -part 'c_code_asset.dart'; +part 'native_code_asset.dart'; part 'data_asset.dart'; part '../model/asset.dart'; -part '../model/c_code_asset.dart'; +part '../model/native_code_asset.dart'; part '../model/data_asset.dart'; /// Data or code bundled with a Dart or Flutter application. @@ -36,7 +36,7 @@ abstract final class Asset { /// different packages. /// /// The default asset id for an asset reference from `lib/src/foo.dart` is - /// `'package:foo/src/foo.dart'`. For example a [CCodeAsset] can be accessed + /// `'package:foo/src/foo.dart'`. For example a [NativeCodeAsset] can be accessed /// via `@Native` with the `assetId` argument omitted: /// /// ```dart diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 6ce069762..bf3689eb2 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -72,7 +72,7 @@ import 'build_output.dart'; /// /// output.addAsset( /// // TODO: Change to DataAsset once the Dart/Flutter SDK can consume it. -/// CCodeAsset( +/// NativeCodeAsset( /// id: 'library:$packageName/asset.txt', /// file: assetPath, /// linkMode: LinkMode.dynamic, diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 76e4996d8..53f779941 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -76,7 +76,7 @@ abstract final class BuildConfig { /// in the Android documentation. int? get targetAndroidNdkApi; - /// The preferred [LinkMode] method for [CCodeAsset]s. + /// The preferred [LinkMode] method for [NativeCodeAsset]s. LinkModePreference get linkModePreference; /// Metadata from a direct dependency. @@ -112,7 +112,7 @@ abstract final class BuildConfig { /// The asset types that the invoker of this build supports. /// /// Currently known values: - /// * [CCodeAsset.type] + /// * [NativeCodeAsset.type] /// * [DataAsset.type] Iterable get supportedAssetTypes; diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 9c1878cf6..e7004a413 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -57,7 +57,7 @@ abstract final class BuildOutput { /// /// The [Asset]s produced by this build or dry-run can be provided to the /// constructor as [assets], or can be added later using [addAsset] and - /// [addAssets]. In dry runs, the [Architecture] for [CCodeAsset]s can be + /// [addAssets]. In dry runs, the [Architecture] for [NativeCodeAsset]s can be /// omitted. /// /// The files used by this build must be provided to the constructor as diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode.dart b/pkgs/native_assets_cli/lib/src/api/link_mode.dart index 084192a75..ce4cca124 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode.dart @@ -6,7 +6,7 @@ import 'asset.dart'; part '../model/link_mode.dart'; -/// The link mode for [CCodeAsset]s. +/// The link mode for [NativeCodeAsset]s. abstract final class LinkMode { /// Dynamic loading. /// diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart index 0f472ecfd..3702bc39a 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart @@ -7,7 +7,7 @@ import 'link_mode.dart'; part '../model/link_mode_preference.dart'; -/// The preferred linkMode method for [CCodeAsset]s. +/// The preferred linkMode method for [NativeCodeAsset]s. abstract final class LinkModePreference { /// The name for this link mode. String get name; diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart similarity index 87% rename from pkgs/native_assets_cli/lib/src/api/c_code_asset.dart rename to pkgs/native_assets_cli/lib/src/api/native_code_asset.dart index 88b9aa820..11ca1f547 100644 --- a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart @@ -6,11 +6,12 @@ part of 'asset.dart'; /// A code [Asset] which respects the native application binary interface (ABI). /// -/// Typical other languages which produce code assets that respect the C ABI -/// include C++ and Rust. +/// Typical languages which produce code assets that respect the native ABI +/// include C, C++ (with `extern "C"`), Rust (with `extern "C"`), and a subset +/// of language features of Objective-C. /// -/// C code assets can be accessed at runtime through native external functions -/// via their asset [id]: +/// Native code assets can be accessed at runtime through native external +/// functions via their asset [id]: /// /// ```dart /// import 'dart:ffi'; @@ -24,7 +25,7 @@ part of 'asset.dart'; /// external int add(int a, int b); /// ``` /// -/// There are several types of C code assets: +/// There are several types of native code assets: /// * Assets which designate symbols present in the target system /// ([SystemDylib]), process ([LookupInProcess]), or executable /// ([LookupInExecutable]). These assets do not have a [file]. @@ -42,7 +43,7 @@ part of 'asset.dart'; /// target system ([SystemDylib]). If the asset is bundled "manually", the Dart /// or Flutter SDK will take care of copying the asset [file] from its specified /// location on the current system into the application bundle. -abstract final class CCodeAsset implements Asset { +abstract final class NativeCodeAsset implements Asset { /// The operating system this asset can run on. OS get os; @@ -61,7 +62,7 @@ abstract final class CCodeAsset implements Asset { /// Throws a [StateError] when accessed with [linkMode] is [LinkMode.static]. DynamicLoading get dynamicLoading; - /// Constructs a C code asset. + /// Constructs a native code asset. /// /// The [id] of this asset is a uri `package:/` from [package] /// and [name]. @@ -73,7 +74,7 @@ abstract final class CCodeAsset implements Asset { /// If [linkMode] is [LinkMode.dynamic] and [dynamicLoading] is not /// [BundledDylib], a [file] must not be provided. If [dynamicLoading] is /// [BundledDylib], a [file] must be provided in non-[BuildConfig.dryRun]s. - factory CCodeAsset({ + factory NativeCodeAsset({ required String package, required String name, required LinkMode linkMode, @@ -82,7 +83,7 @@ abstract final class CCodeAsset implements Asset { Uri? file, Architecture? architecture, }) => - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:$package/$name', linkMode: linkMode as LinkModeImpl, os: os as OSImpl, @@ -91,10 +92,10 @@ abstract final class CCodeAsset implements Asset { file: file, ); - static const String type = 'c_code'; + static const String type = 'native_code'; } -/// The dynamic loading method when the [CCodeAsset.linkMode] is +/// The dynamic loading method when the [NativeCodeAsset.linkMode] is /// [LinkMode.dynamic]. abstract final class DynamicLoading {} diff --git a/pkgs/native_assets_cli/lib/src/model/asset.dart b/pkgs/native_assets_cli/lib/src/model/asset.dart index f38dba45c..d8f8ea19f 100644 --- a/pkgs/native_assets_cli/lib/src/model/asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/asset.dart @@ -11,11 +11,11 @@ abstract final class AssetImpl implements Asset { final assets = []; for (final yamlElement in list) { final yamlMap = as(yamlElement); - final type = yamlMap[CCodeAssetImpl.typeKey]; + final type = yamlMap[NativeCodeAssetImpl.typeKey]; switch (type) { - case CCodeAsset.type: + case NativeCodeAsset.type: case null: // Backwards compatibility with v1.0.0. - assets.add(CCodeAssetImpl.fromYaml(yamlMap)); + assets.add(NativeCodeAssetImpl.fromYaml(yamlMap)); case DataAsset.type: assets.add(DataAssetImpl.fromYaml(yamlMap)); default: diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index 93f2c2c50..dc807060a 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -206,7 +206,7 @@ final class BuildConfigImpl implements BuildConfig { ) => [ ...?supportedAssetTypes, - if (supportedAssetTypes == null) CCodeAsset.type, + if (supportedAssetTypes == null) NativeCodeAsset.type, ]; BuildConfigImpl._(); @@ -421,7 +421,7 @@ final class BuildConfigImpl implements BuildConfig { }, (config) => _supportedAssetTypes = config.optionalStringList(supportedAssetTypesKey) ?? - [CCodeAsset.type], + [NativeCodeAsset.type], ]; } diff --git a/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md index 191e8d88e..5975085a5 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md +++ b/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md @@ -1,7 +1,7 @@ ## 1.1.0 - Added supported asset types. - Backwards compatibility: Defaults to a list with a single element: `c_code`. + Backwards compatibility: Defaults to a list with a single element: `native_code`. ## 1.0.0 diff --git a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md index 01e558e36..a807846fe 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md +++ b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md @@ -1,7 +1,7 @@ ## 1.1.0 - Assets now have a `type`. - Backwards compatibility: assets without a type are interpreted as `c_code` assets. + Backwards compatibility: assets without a type are interpreted as `native_code` assets. - Assets now have an optional `file`. Backwards compatibility: assets that have an `AssetAbsolutePath` will have that path used as `file`. - **Breaking change** Assets now have a `dynamic_loading` field instead of `path_type`. diff --git a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart similarity index 95% rename from pkgs/native_assets_cli/lib/src/model/c_code_asset.dart rename to pkgs/native_assets_cli/lib/src/model/native_code_asset.dart index 6f803264c..2b390f540 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart @@ -121,7 +121,7 @@ final class LookupInExecutableImpl }; } -final class CCodeAssetImpl implements CCodeAsset, AssetImpl { +final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { @override final Uri? file; @@ -147,7 +147,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { @override final ArchitectureImpl? architecture; - CCodeAssetImpl({ + NativeCodeAssetImpl({ this.file, required this.id, required this.linkMode, @@ -180,7 +180,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { } } - factory CCodeAssetImpl.fromYaml(YamlMap yamlMap) { + factory NativeCodeAssetImpl.fromYaml(YamlMap yamlMap) { final linkMode = LinkModeImpl.fromName(as(yamlMap[_linkModeKey])); final dynamicLoadingYaml = as(yamlMap[_dynamicLoadingKey] ?? yamlMap[_pathKey]); @@ -218,7 +218,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { } } - return CCodeAssetImpl( + return NativeCodeAssetImpl( id: as(yamlMap[_idKey]), dynamicLoading: linkMode == LinkMode.dynamic ? dynamicLoading : null, os: os, @@ -228,7 +228,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { ); } - CCodeAssetImpl copyWith({ + NativeCodeAssetImpl copyWith({ LinkModeImpl? linkMode, String? id, OSImpl? os, @@ -236,7 +236,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { DynamicLoadingImpl? dynamicLoading, Uri? file, }) => - CCodeAssetImpl( + NativeCodeAssetImpl( id: id ?? this.id, linkMode: linkMode ?? this.linkMode, os: os ?? this.os, @@ -247,7 +247,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { @override bool operator ==(Object other) { - if (other is! CCodeAssetImpl) { + if (other is! NativeCodeAssetImpl) { return false; } return other.id == id && @@ -286,7 +286,7 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { _idKey: id, _linkModeKey: linkMode.name, _osKey: os.toString(), - typeKey: CCodeAsset.type, + typeKey: NativeCodeAsset.type, }..sortOnKey(); } @@ -301,5 +301,6 @@ final class CCodeAssetImpl implements CCodeAsset, AssetImpl { static const _architectureKey = 'architecture'; @override - String toString() => 'CCodeAsset(${toYaml(BuildOutputImpl.latestVersion)})'; + String toString() => + 'NativeCodeAsset(${toYaml(BuildOutputImpl.latestVersion)})'; } diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index 3b8e95600..c143fed48 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -9,7 +9,7 @@ import 'package:test/test.dart'; void main() { test('Asset constructors', () async { final assets = [ - CCodeAsset( + NativeCodeAsset( package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), @@ -18,7 +18,7 @@ void main() { architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), - CCodeAsset( + NativeCodeAsset( package: 'my_package', name: 'foo3', dynamicLoading: SystemDylib(Uri(path: 'libfoo3.so')), @@ -26,7 +26,7 @@ void main() { architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), - CCodeAsset( + NativeCodeAsset( package: 'my_package', name: 'foo4', dynamicLoading: LookupInExecutable(), @@ -34,7 +34,7 @@ void main() { architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), - CCodeAsset( + NativeCodeAsset( package: 'my_package', name: 'foo5', dynamicLoading: LookupInProcess(), @@ -42,7 +42,7 @@ void main() { architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), - CCodeAsset( + NativeCodeAsset( package: 'my_package', name: 'bar', file: Uri(path: 'path/to/libbar.a'), @@ -50,7 +50,7 @@ void main() { architecture: Architecture.arm64, linkMode: LinkMode.static, ), - CCodeAsset( + NativeCodeAsset( package: 'my_package', name: 'bla', file: Uri(path: 'path/with spaces/bla.dll'), @@ -70,7 +70,7 @@ void main() { test('Errors', () { expect( - () => CCodeAsset( + () => NativeCodeAsset( package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), @@ -82,7 +82,7 @@ void main() { throwsArgumentError, ); expect( - () => CCodeAsset( + () => NativeCodeAsset( package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), @@ -93,7 +93,7 @@ void main() { throwsArgumentError, ); expect( - () => CCodeAsset( + () => NativeCodeAsset( package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), @@ -105,7 +105,7 @@ void main() { throwsArgumentError, ); - final staticLinkingAsset = CCodeAsset( + final staticLinkingAsset = NativeCodeAsset( package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 87780a6d9..cc0204b35 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -61,7 +61,7 @@ void main() async { ), buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, - supportedAssetTypes: [CCodeAsset.type], + supportedAssetTypes: [NativeCodeAsset.type], ); final config2 = BuildConfig.build( @@ -73,7 +73,7 @@ void main() async { targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, - supportedAssetTypes: [CCodeAsset.type], + supportedAssetTypes: [NativeCodeAsset.type], ); expect(config1, equals(config1)); @@ -131,7 +131,7 @@ void main() async { packageRoot: packageRootUri, targetOS: OS.android, linkModePreference: LinkModePreference.preferStatic, - supportedAssetTypes: [CCodeAsset.type], + supportedAssetTypes: [NativeCodeAsset.type], ); final config = Config(fileParsed: { diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index b5069b49b..72abd68fe 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -22,7 +22,7 @@ void main() { BuildOutput( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ - CCodeAsset( + NativeCodeAsset( package: 'my_package', name: 'foo', file: Uri(path: 'path/to/libfoo.so'), @@ -31,7 +31,7 @@ void main() { architecture: Architecture.x64, linkMode: LinkMode.dynamic, ), - CCodeAsset( + NativeCodeAsset( package: 'my_package', name: 'foo2', dynamicLoading: SystemDylib(Uri(path: 'path/to/libfoo2.so')), diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index e7dcf223a..21559df59 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -16,8 +16,8 @@ void main() { final blaUri = Uri(path: 'path/with spaces/bla.dll'); final dataUri = Uri.file('path/to/data.txt'); final data2Uri = Uri.file('path/to/data.json'); - final cCodeAssets = [ - CCodeAssetImpl( + final nativeCodeAssets = [ + NativeCodeAssetImpl( id: 'package:my_package/foo', file: fooUri, dynamicLoading: BundledDylibImpl(), @@ -25,35 +25,35 @@ void main() { architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo3', dynamicLoading: SystemDylibImpl(foo3Uri), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo4', dynamicLoading: LookupInExecutableImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo5', dynamicLoading: LookupInProcessImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/bar', file: barUri, os: OSImpl.linux, architecture: ArchitectureImpl.arm64, linkMode: LinkModeImpl.static, ), - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/bla', file: blaUri, dynamicLoading: BundledDylibImpl(), @@ -73,7 +73,7 @@ void main() { ), ]; final assets = [ - ...cCodeAssets, + ...nativeCodeAssets, ...dataAssets, ]; @@ -119,7 +119,7 @@ void main() { id: package:my_package/foo link_mode: dynamic os: android - type: c_code + type: native_code - architecture: x64 dynamic_loading: type: system @@ -127,27 +127,27 @@ void main() { id: package:my_package/foo3 link_mode: dynamic os: android - type: c_code + type: native_code - architecture: x64 dynamic_loading: type: executable id: package:my_package/foo4 link_mode: dynamic os: android - type: c_code + type: native_code - architecture: x64 dynamic_loading: type: process id: package:my_package/foo5 link_mode: dynamic os: android - type: c_code + type: native_code - architecture: arm64 file: ${barUri.toFilePath()} id: package:my_package/bar link_mode: static os: linux - type: c_code + type: native_code - architecture: x64 dynamic_loading: type: bundle @@ -155,7 +155,7 @@ void main() { id: package:my_package/bla link_mode: dynamic os: windows - type: c_code + type: native_code - id: package:my_package/my_data_asset file: ${dataUri.toFilePath()} type: data @@ -175,7 +175,7 @@ void main() { test('build_output protocol v1.0.0 keeps working', () { final assets2 = AssetImpl.listFromYamlList( loadYaml(assetsYamlEncodingV1_0_0) as YamlList); - expect(cCodeAssets, assets2); + expect(nativeCodeAssets, assets2); }); test('AssetPath factory', () async { @@ -188,7 +188,7 @@ void main() { }); test('Asset hashCode copyWith', () async { - final asset = cCodeAssets.first; + final asset = nativeCodeAssets.first; final asset2 = asset.copyWith(id: 'foo321'); expect(asset.hashCode != asset2.hashCode, true); @@ -199,9 +199,9 @@ void main() { }); test('List hashCode', () async { - final assets2 = cCodeAssets.take(3).toList(); - const equality = ListEquality(); - expect(equality.hash(cCodeAssets) != equality.hash(assets2), true); + final assets2 = nativeCodeAssets.take(3).toList(); + const equality = ListEquality(); + expect(equality.hash(nativeCodeAssets) != equality.hash(assets2), true); }); test('Asset toString', () async { diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index e44851de2..6eeb63535 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -257,7 +257,7 @@ out_dir: ${outDir.toFilePath()} package_name: $packageName package_root: ${tempUri.toFilePath()} supported_asset_types: - - ${CCodeAsset.type} + - ${NativeCodeAsset.type} target_architecture: arm64 target_ios_sdk: iphoneos target_os: ios @@ -539,11 +539,11 @@ version: 1.0.0'''; targetOS: OSImpl.linux, buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.dynamic, - supportedAssetTypes: [CCodeAsset.type], + supportedAssetTypes: [NativeCodeAsset.type], ); // Using the checksum for a build folder should be stable. - expect(name1, '4dec7292cfab417c27529307d949773f'); + expect(name1, 'd95547e3b45e88a475739eb4fe03600a'); // Build folder different due to metadata. final name2 = BuildConfigImpl.checksum( diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 09587067b..993484414 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -23,7 +23,7 @@ void main() { final buildOutput = BuildOutputImpl( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), dynamicLoading: BundledDylibImpl(), @@ -31,21 +31,21 @@ void main() { architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo2', dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo3', dynamicLoading: LookupInProcessImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo4', dynamicLoading: LookupInExecutableImpl(), os: OSImpl.android, @@ -100,7 +100,7 @@ assets: id: package:my_package/foo link_mode: dynamic os: android - type: c_code + type: native_code - architecture: x64 dynamic_loading: type: system @@ -108,21 +108,21 @@ assets: id: package:my_package/foo2 link_mode: dynamic os: android - type: c_code + type: native_code - architecture: x64 dynamic_loading: type: process id: package:my_package/foo3 link_mode: dynamic os: android - type: c_code + type: native_code - architecture: x64 dynamic_loading: type: executable id: package:my_package/foo4 link_mode: dynamic os: android - type: c_code + type: native_code dependencies: - path/to/file.ext metadata: @@ -267,7 +267,7 @@ version: ${BuildOutputImpl.latestVersion}'''), final buildOutput = BuildOutputImpl( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), dynamicLoading: BundledDylibImpl(), @@ -275,7 +275,7 @@ version: ${BuildOutputImpl.latestVersion}'''), architecture: ArchitectureImpl.x64, linkMode: LinkModeImpl.dynamic, ), - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo2', dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, @@ -297,7 +297,7 @@ version: ${BuildOutputImpl.latestVersion}'''), timestamp: DateTime.parse('2022-11-10 13:25:01.000'), ); buildOutput2.addAsset( - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), dynamicLoading: BundledDylibImpl(), @@ -307,7 +307,7 @@ version: ${BuildOutputImpl.latestVersion}'''), ), ); buildOutput2.addAssets([ - CCodeAssetImpl( + NativeCodeAssetImpl( id: 'package:my_package/foo2', dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 990bd9cb5..09d35e610 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -248,7 +248,7 @@ class CBuilder implements Builder { if (assetName != null) { buildOutput.addAssets([ - CCodeAsset( + NativeCodeAsset( package: buildConfig.packageName, name: assetName!, file: buildConfig.dryRun ? null : libUri, From 0411dd94ef620e751eb45a5ca9d837770d85a735 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 12 Mar 2024 14:42:25 +0100 Subject: [PATCH 68/75] Fix analysis --- .../test_data/wrong_build_output/build.dart | 2 +- .../test_data/wrong_build_output_2/build.dart | 2 +- .../test_data/wrong_build_output_3/build.dart | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart index 77ba0abf2..f36522b9d 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output/build.dart @@ -7,7 +7,7 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { - final buildConfig = await BuildConfigImpl.fromArguments(args); + final buildConfig = BuildConfigImpl.fromArguments(args); await File.fromUri( buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) .writeAsString(_wrongContents); diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart index b0ab298f6..ae90eac44 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_2/build.dart @@ -7,7 +7,7 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { - final buildConfig = await BuildConfigImpl.fromArguments(args); + final buildConfig = BuildConfigImpl.fromArguments(args); await File.fromUri( buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) .writeAsString(_wrongContents); diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart index 9089375c0..1a660dc60 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_3/build.dart @@ -7,7 +7,7 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { - final buildConfig = await BuildConfigImpl.fromArguments(args); + final buildConfig = BuildConfigImpl.fromArguments(args); await File.fromUri( buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) .writeAsString(_rightContents); From 88863be2edcfdfc00210e203f77db305b80f5024 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 12 Mar 2024 15:15:39 +0100 Subject: [PATCH 69/75] Remove link between `LinkModePreference` and `LinkMode` --- .../lib/src/api/c_code_asset.dart | 0 .../lib/src/api/link_mode.dart | 6 ------ .../lib/src/api/link_mode_preference.dart | 7 ------- .../lib/src/model/link_mode.dart | 4 ++-- .../lib/src/model/link_mode_preference.dart | 20 +------------------ .../lib/src/cbuilder/cbuilder.dart | 12 ++++++++++- .../cbuilder/cbuilder_cross_android_test.dart | 2 +- .../cbuilder/cbuilder_cross_ios_test.dart | 2 +- .../cbuilder_cross_linux_host_test.dart | 2 +- .../cbuilder_cross_macos_host_test.dart | 2 +- .../cbuilder_cross_windows_host_test.dart | 2 +- 11 files changed, 19 insertions(+), 40 deletions(-) create mode 100644 pkgs/native_assets_cli/lib/src/api/c_code_asset.dart diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart new file mode 100644 index 000000000..e69de29bb diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode.dart b/pkgs/native_assets_cli/lib/src/api/link_mode.dart index ce4cca124..f2b8a7942 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode.dart @@ -22,10 +22,4 @@ abstract final class LinkMode { /// Not yet supported in the Dart and Flutter SDK. // TODO(https://github.com/dart-lang/sdk/issues/49418): Support static linking. static const LinkMode static = LinkModeImpl.static; - - /// Known values for [LinkMode]. - static const List values = [ - dynamic, - static, - ]; } diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart index 3702bc39a..f600704a1 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode_preference.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'asset.dart'; -import 'link_mode.dart'; part '../model/link_mode_preference.dart'; @@ -12,12 +11,6 @@ abstract final class LinkModePreference { /// The name for this link mode. String get name; - /// The preferred [LinkMode] for this link mode preference. - LinkMode get preferredLinkMode; - - /// The potential [LinkMode]s for this link mode preference. - List get potentialLinkMode; - /// Provide native assets as dynamic libraries. /// /// Fails if not all native assets can only be provided as static library. diff --git a/pkgs/native_assets_cli/lib/src/model/link_mode.dart b/pkgs/native_assets_cli/lib/src/model/link_mode.dart index b8e860773..7394dfeb3 100644 --- a/pkgs/native_assets_cli/lib/src/model/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/model/link_mode.dart @@ -13,13 +13,13 @@ final class LinkModeImpl implements LinkMode { static const LinkModeImpl static = LinkModeImpl._('static'); /// Known values for [LinkModeImpl]. - static const List values = [ + static const List _values = [ dynamic, static, ]; factory LinkModeImpl.fromName(String name) => - values.where((element) => element.name == name).first; + _values.where((element) => element.name == name).first; @override String toString() => name; diff --git a/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart index 35436757c..1ee1357a4 100644 --- a/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/model/link_mode_preference.dart @@ -8,43 +8,25 @@ final class LinkModePreferenceImpl implements LinkModePreference { @override final String name; - @override - final LinkModeImpl preferredLinkMode; - - @override - final List potentialLinkMode; - - const LinkModePreferenceImpl( - this.name, { - required this.preferredLinkMode, - required this.potentialLinkMode, - }); + const LinkModePreferenceImpl(this.name); factory LinkModePreferenceImpl.fromString(String name) => values.where((element) => element.name == name).first; static const dynamic = LinkModePreferenceImpl( 'dynamic', - preferredLinkMode: LinkModeImpl.dynamic, - potentialLinkMode: [LinkModeImpl.dynamic], ); static const static = LinkModePreferenceImpl( 'static', - preferredLinkMode: LinkModeImpl.static, - potentialLinkMode: [LinkModeImpl.static], ); static const preferDynamic = LinkModePreferenceImpl( 'prefer-dynamic', - preferredLinkMode: LinkModeImpl.dynamic, - potentialLinkMode: LinkModeImpl.values, ); static const preferStatic = LinkModePreferenceImpl( 'prefer-static', - preferredLinkMode: LinkModeImpl.static, - potentialLinkMode: LinkModeImpl.values, ); static const values = [ diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 09d35e610..2a1ada976 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -199,7 +199,7 @@ class CBuilder implements Builder { final outDir = buildConfig.outputDirectory; final packageRoot = buildConfig.packageRoot; await Directory.fromUri(outDir).create(recursive: true); - final linkMode = buildConfig.linkModePreference.preferredLinkMode; + final linkMode = _linkMode(buildConfig.linkModePreference); final libUri = outDir.resolve(buildConfig.targetOS.libraryFileName(name, linkMode)); final exeUri = @@ -284,3 +284,13 @@ enum _CBuilderType { executable, library, } + +LinkMode _linkMode(LinkModePreference preference) { + if (preference == LinkModePreference.dynamic || + preference == LinkModePreference.preferDynamic) { + return LinkMode.dynamic; + } + assert(preference == LinkModePreference.static || + preference == LinkModePreference.preferStatic); + return LinkMode.static; +} diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart index 81e9ced4a..7b2e03754 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart @@ -46,7 +46,7 @@ void main() { /// From https://docs.flutter.dev/reference/supported-platforms. const flutterAndroidNdkVersionHighestSupported = 34; - for (final linkMode in LinkMode.values) { + for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { for (final target in targets) { for (final apiLevel in [ flutterAndroidNdkVersionLowestBestEffort, diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart index dcf8d3fa6..23fd12945 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart @@ -36,7 +36,7 @@ void main() { const name = 'add'; - for (final linkMode in LinkMode.values) { + for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { for (final targetIOSSdk in IOSSdk.values) { for (final target in targets) { if (target == Architecture.x64 && targetIOSSdk == IOSSdk.iPhoneOS) { diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart index bb5aa5d5f..9584f5557 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart @@ -36,7 +36,7 @@ void main() { Architecture.riscv64: 'RISC-V', }; - for (final linkMode in LinkMode.values) { + for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart index 4405054f0..429b554c1 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart @@ -34,7 +34,7 @@ void main() { Architecture.x64: '64-bit x86-64', }; - for (final linkMode in LinkMode.values) { + for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart index 4aeddc0d1..a94742365 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart @@ -46,7 +46,7 @@ void main() { LinkMode.static: 'LIBRARY', }; - for (final linkMode in LinkMode.values) { + for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); From 05e473e4f20fa6b2f4c42e19c09ab48263cd255e Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 12 Mar 2024 15:33:18 +0100 Subject: [PATCH 70/75] Rename `LinkMode.dynamic` -> `LinkMode.dynamicLoading` --- .../build_runner/packaging_preference_test.dart | 4 ++-- .../test_data/wrong_namespace_asset/build.dart | 2 +- .../example/local_asset/build.dart | 2 +- .../native_assets_cli/lib/src/api/link_mode.dart | 2 +- .../lib/src/api/native_code_asset.dart | 13 +++++++------ .../lib/src/model/link_mode.dart | 4 ++-- .../lib/src/model/native_code_asset.dart | 9 +++++---- pkgs/native_assets_cli/lib/src/model/os.dart | 2 +- pkgs/native_assets_cli/test/api/asset_test.dart | 14 +++++++------- .../test/api/build_output_test.dart | 4 ++-- .../native_assets_cli/test/model/asset_test.dart | 10 +++++----- .../test/model/build_output_test.dart | 16 ++++++++-------- .../test/model/target_test.dart | 4 ++-- .../lib/src/cbuilder/cbuilder.dart | 13 +++++++------ .../cbuilder/cbuilder_cross_android_test.dart | 6 +++--- .../test/cbuilder/cbuilder_cross_ios_test.dart | 8 ++++---- .../cbuilder/cbuilder_cross_linux_host_test.dart | 4 ++-- .../cbuilder/cbuilder_cross_macos_host_test.dart | 4 ++-- .../cbuilder_cross_windows_host_test.dart | 6 +++--- 19 files changed, 65 insertions(+), 62 deletions(-) diff --git a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart index 777fd459b..c2cc4739e 100644 --- a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart @@ -53,11 +53,11 @@ void main() async { // This package honors preferences. expect( (resultDynamic.assets.single as NativeCodeAssetImpl).linkMode, - LinkModeImpl.dynamic, + LinkModeImpl.dynamicLoading, ); expect( (resultPreferDynamic.assets.single as NativeCodeAssetImpl).linkMode, - LinkModeImpl.dynamic, + LinkModeImpl.dynamicLoading, ); expect( (resultStatic.assets.single as NativeCodeAssetImpl).linkMode, diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 3a66de276..3cd631816 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -13,7 +13,7 @@ void main(List arguments) async { file: config.outputDirectory.resolve( OS.current.dylibFileName('foo'), ), - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, os: OS.current, architecture: Architecture.current, dynamicLoading: BundledDylib(), diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 3afc1aac4..4a172a315 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -37,7 +37,7 @@ void main(List args) async { package: packageName, name: 'asset.txt', file: assetPath, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, os: config.targetOS, architecture: config.targetArchitecture, dynamicLoading: BundledDylib(), diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode.dart b/pkgs/native_assets_cli/lib/src/api/link_mode.dart index f2b8a7942..6f296dc66 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_mode.dart @@ -15,7 +15,7 @@ abstract final class LinkMode { /// Note: Dynamic loading is not equal to dynamic linking. Dynamic linking /// would have to run the linker at compile-time, which is currently not /// supported in the Dart and Flutter SDK. - static const LinkMode dynamic = LinkModeImpl.dynamic; + static const LinkMode dynamicLoading = LinkModeImpl.dynamicLoading; /// Static linking. /// diff --git a/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart index 11ca1f547..795bec9c6 100644 --- a/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart @@ -57,7 +57,8 @@ abstract final class NativeCodeAsset implements Asset { /// Either dynamic loading or static linking. LinkMode get linkMode; - /// The dynamic loading method when the [linkMode] is [LinkMode.dynamic]. + /// The dynamic loading method when the [linkMode] is + /// [LinkMode.dynamicLoading]. /// /// Throws a [StateError] when accessed with [linkMode] is [LinkMode.static]. DynamicLoading get dynamicLoading; @@ -67,11 +68,11 @@ abstract final class NativeCodeAsset implements Asset { /// The [id] of this asset is a uri `package:/` from [package] /// and [name]. /// - /// If [linkMode] is [LinkMode.dynamic], a non-null [dynamicLoading] must be - /// provided. If [linkMode] is [LinkMode.static], [dynamicLoading] must not be - /// provided. + /// If [linkMode] is [LinkMode.dynamicLoading], a non-null [dynamicLoading] + /// must be provided. If [linkMode] is [LinkMode.static], [dynamicLoading] + /// must not be provided. /// - /// If [linkMode] is [LinkMode.dynamic] and [dynamicLoading] is not + /// If [linkMode] is [LinkMode.dynamicLoading] and [dynamicLoading] is not /// [BundledDylib], a [file] must not be provided. If [dynamicLoading] is /// [BundledDylib], a [file] must be provided in non-[BuildConfig.dryRun]s. factory NativeCodeAsset({ @@ -96,7 +97,7 @@ abstract final class NativeCodeAsset implements Asset { } /// The dynamic loading method when the [NativeCodeAsset.linkMode] is -/// [LinkMode.dynamic]. +/// [LinkMode.dynamicLoading]. abstract final class DynamicLoading {} /// The asset file should be bundled by Dart/Flutter. diff --git a/pkgs/native_assets_cli/lib/src/model/link_mode.dart b/pkgs/native_assets_cli/lib/src/model/link_mode.dart index 7394dfeb3..aff53d9e4 100644 --- a/pkgs/native_assets_cli/lib/src/model/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/model/link_mode.dart @@ -9,12 +9,12 @@ final class LinkModeImpl implements LinkMode { const LinkModeImpl._(this.name); - static const LinkModeImpl dynamic = LinkModeImpl._('dynamic'); + static const LinkModeImpl dynamicLoading = LinkModeImpl._('dynamic'); static const LinkModeImpl static = LinkModeImpl._('static'); /// Known values for [LinkModeImpl]. static const List _values = [ - dynamic, + dynamicLoading, static, ]; diff --git a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart index 2b390f540..0a7f708c8 100644 --- a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart @@ -155,7 +155,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { DynamicLoadingImpl? dynamicLoading, this.architecture, }) : _dynamicLoading = dynamicLoading { - if (linkMode == LinkMode.dynamic && dynamicLoading == null) { + if (linkMode == LinkMode.dynamicLoading && dynamicLoading == null) { throw ArgumentError.value( dynamicLoading, 'dynamicLoading', @@ -169,7 +169,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { 'Must be null if linkMode == LinkMode.static.', ); } - if (linkMode == LinkMode.dynamic && + if (linkMode == LinkMode.dynamicLoading && dynamicLoading is! BundledDylib && file != null) { throw ArgumentError.value( @@ -220,7 +220,8 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { return NativeCodeAssetImpl( id: as(yamlMap[_idKey]), - dynamicLoading: linkMode == LinkMode.dynamic ? dynamicLoading : null, + dynamicLoading: + linkMode == LinkMode.dynamicLoading ? dynamicLoading : null, os: os, architecture: architecture, linkMode: linkMode, @@ -280,7 +281,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { } return { if (architecture != null) _architectureKey: architecture.toString(), - if (linkMode == LinkMode.dynamic) + if (linkMode == LinkMode.dynamicLoading) _dynamicLoadingKey: dynamicLoading.toYaml(version, file), if (file != null) _fileKey: file!.toFilePath(), _idKey: id, diff --git a/pkgs/native_assets_cli/lib/src/model/os.dart b/pkgs/native_assets_cli/lib/src/model/os.dart index 1d63cf02a..3ec1e0f28 100644 --- a/pkgs/native_assets_cli/lib/src/model/os.dart +++ b/pkgs/native_assets_cli/lib/src/model/os.dart @@ -113,7 +113,7 @@ final class OSImpl implements OS { @override String libraryFileName(String name, LinkMode linkMode) { - if (linkMode == LinkModeImpl.dynamic) { + if (linkMode == LinkModeImpl.dynamicLoading) { return dylibFileName(name); } assert(linkMode == LinkModeImpl.static); diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index c143fed48..a1d08589e 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -16,7 +16,7 @@ void main() { dynamicLoading: BundledDylib(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', @@ -24,7 +24,7 @@ void main() { dynamicLoading: SystemDylib(Uri(path: 'libfoo3.so')), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', @@ -32,7 +32,7 @@ void main() { dynamicLoading: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', @@ -40,7 +40,7 @@ void main() { dynamicLoading: LookupInProcess(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', @@ -57,7 +57,7 @@ void main() { dynamicLoading: BundledDylib(), os: OS.windows, architecture: Architecture.x64, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, ), DataAsset( package: 'my_package', @@ -88,7 +88,7 @@ void main() { file: Uri.file('path/to/libfoo.so'), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, ), throwsArgumentError, ); @@ -100,7 +100,7 @@ void main() { dynamicLoading: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, ), throwsArgumentError, ); diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index 72abd68fe..552a2c069 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -29,7 +29,7 @@ void main() { dynamicLoading: BundledDylib(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', @@ -37,7 +37,7 @@ void main() { dynamicLoading: SystemDylib(Uri(path: 'path/to/libfoo2.so')), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamic, + linkMode: LinkMode.dynamicLoading, ), ], dependencies: [ diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 21559df59..2d4a10e7b 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -23,28 +23,28 @@ void main() { dynamicLoading: BundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo3', dynamicLoading: SystemDylibImpl(foo3Uri), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo4', dynamicLoading: LookupInExecutableImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo5', dynamicLoading: LookupInProcessImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/bar', @@ -59,7 +59,7 @@ void main() { dynamicLoading: BundledDylibImpl(), os: OSImpl.windows, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), ]; final dataAssets = [ diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 993484414..4070192e3 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -29,28 +29,28 @@ void main() { dynamicLoading: BundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo2', dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo3', dynamicLoading: LookupInProcessImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo4', dynamicLoading: LookupInExecutableImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), ], dependencies: Dependencies([ @@ -273,14 +273,14 @@ version: ${BuildOutputImpl.latestVersion}'''), dynamicLoading: BundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo2', dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), ], dependencies: Dependencies([ @@ -303,7 +303,7 @@ version: ${BuildOutputImpl.latestVersion}'''), dynamicLoading: BundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), ); buildOutput2.addAssets([ @@ -312,7 +312,7 @@ version: ${BuildOutputImpl.latestVersion}'''), dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamic, + linkMode: LinkModeImpl.dynamicLoading, ), ]); buildOutput2.addDependency( diff --git a/pkgs/native_assets_cli/test/model/target_test.dart b/pkgs/native_assets_cli/test/model/target_test.dart index 4c56d7402..76e025bc9 100644 --- a/pkgs/native_assets_cli/test/model/target_test.dart +++ b/pkgs/native_assets_cli/test/model/target_test.dart @@ -13,8 +13,8 @@ void main() { expect(OSImpl.android.dylibFileName('foo'), 'libfoo.so'); expect(OSImpl.android.staticlibFileName('foo'), 'libfoo.a'); expect(OSImpl.windows.dylibFileName('foo'), 'foo.dll'); - expect( - OSImpl.windows.libraryFileName('foo', LinkModeImpl.dynamic), 'foo.dll'); + expect(OSImpl.windows.libraryFileName('foo', LinkModeImpl.dynamicLoading), + 'foo.dll'); expect(OSImpl.windows.staticlibFileName('foo'), 'foo.lib'); expect( OSImpl.windows.libraryFileName('foo', LinkModeImpl.static), 'foo.lib'); diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 2a1ada976..43c322e66 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -221,10 +221,10 @@ class CBuilder implements Builder { logger: logger, sources: sources, includes: includes, - dynamicLibrary: - _type == _CBuilderType.library && linkMode == LinkMode.dynamic - ? libUri - : null, + dynamicLibrary: _type == _CBuilderType.library && + linkMode == LinkMode.dynamicLoading + ? libUri + : null, staticLibrary: _type == _CBuilderType.library && linkMode == LinkMode.static ? libUri @@ -256,7 +256,8 @@ class CBuilder implements Builder { os: buildConfig.targetOS, architecture: buildConfig.dryRun ? null : buildConfig.targetArchitecture, - dynamicLoading: linkMode == LinkMode.dynamic ? BundledDylib() : null, + dynamicLoading: + linkMode == LinkMode.dynamicLoading ? BundledDylib() : null, ) ]); } @@ -288,7 +289,7 @@ enum _CBuilderType { LinkMode _linkMode(LinkModePreference preference) { if (preference == LinkModePreference.dynamic || preference == LinkModePreference.preferDynamic) { - return LinkMode.dynamic; + return LinkMode.dynamicLoading; } assert(preference == LinkModePreference.static || preference == LinkModePreference.preferStatic); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart index 7b2e03754..044160fd0 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart @@ -46,7 +46,7 @@ void main() { /// From https://docs.flutter.dev/reference/supported-platforms. const flutterAndroidNdkVersionHighestSupported = 34; - for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { + for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { for (final target in targets) { for (final apiLevel in [ flutterAndroidNdkVersionLowestBestEffort, @@ -92,7 +92,7 @@ void main() { test('CBuilder API levels binary difference', () async { const target = Architecture.arm64; - const linkMode = LinkMode.dynamic; + const linkMode = LinkMode.dynamicLoading; const apiLevel1 = flutterAndroidNdkVersionLowestSupported; const apiLevel2 = flutterAndroidNdkVersionHighestSupported; final tempUri = await tempDirForTest(); @@ -132,7 +132,7 @@ Future buildLib( targetOS: OS.android, targetAndroidNdkApi: androidNdkApi, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamic + linkModePreference: linkMode == LinkMode.dynamicLoading ? LinkModePreference.dynamic : LinkModePreference.static, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart index 23fd12945..7550135c0 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart @@ -36,7 +36,7 @@ void main() { const name = 'add'; - for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { + for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { for (final targetIOSSdk in IOSSdk.values) { for (final target in targets) { if (target == Architecture.x64 && targetIOSSdk == IOSSdk.iPhoneOS) { @@ -46,7 +46,7 @@ void main() { final libName = OS.iOS.libraryFileName(name, linkMode); for (final installName in [ null, - if (linkMode == LinkMode.dynamic) + if (linkMode == LinkMode.dynamicLoading) Uri.file('@executable_path/Frameworks/$libName'), ]) { test( @@ -63,7 +63,7 @@ void main() { targetArchitecture: target, targetOS: OS.iOS, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamic + linkModePreference: linkMode == LinkMode.dynamicLoading ? LinkModePreference.dynamic : LinkModePreference.static, targetIOSSdk: targetIOSSdk, @@ -116,7 +116,7 @@ void main() { expect(platform, contains(platformIosSimulator.toString())); } - if (linkMode == LinkMode.dynamic) { + if (linkMode == LinkMode.dynamicLoading) { final libInstallName = await runOtoolInstallName(libUri, libName); if (installName == null) { // If no install path is passed, we have an absolute path. diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart index 9584f5557..9e97b5f6e 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart @@ -36,7 +36,7 @@ void main() { Architecture.riscv64: 'RISC-V', }; - for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { + for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); @@ -51,7 +51,7 @@ void main() { targetArchitecture: target, targetOS: OS.linux, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamic + linkModePreference: linkMode == LinkMode.dynamicLoading ? LinkModePreference.dynamic : LinkModePreference.static, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart index 429b554c1..bfe219732 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart @@ -34,7 +34,7 @@ void main() { Architecture.x64: '64-bit x86-64', }; - for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { + for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); @@ -49,7 +49,7 @@ void main() { targetArchitecture: target, targetOS: OS.macOS, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamic + linkModePreference: linkMode == LinkMode.dynamicLoading ? LinkModePreference.dynamic : LinkModePreference.static, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart index a94742365..5f903b14c 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart @@ -42,11 +42,11 @@ void main() { }; const dumpbinFileType = { - LinkMode.dynamic: 'DLL', + LinkMode.dynamicLoading: 'DLL', LinkMode.static: 'LIBRARY', }; - for (final linkMode in [LinkMode.dynamic, LinkMode.static]) { + for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); @@ -61,7 +61,7 @@ void main() { targetOS: OS.windows, targetArchitecture: target, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamic + linkModePreference: linkMode == LinkMode.dynamicLoading ? LinkModePreference.dynamic : LinkModePreference.static, ); From 8134f461a116216cd65d4a00100eb166a4b90b6f Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 13 Mar 2024 18:33:00 +0100 Subject: [PATCH 71/75] Merge `DynamicLoadingMode` into `LinkMode` --- .../packaging_preference_test.dart | 8 +- .../wrong_namespace_asset/build.dart | 3 +- .../example/local_asset/build.dart | 3 +- .../lib/native_assets_cli.dart | 7 +- .../lib/native_assets_cli_internal.dart | 7 +- pkgs/native_assets_cli/lib/src/api/asset.dart | 1 - pkgs/native_assets_cli/lib/src/api/build.dart | 3 +- .../lib/src/api/build_config.dart | 1 - .../lib/src/api/c_code_asset.dart | 0 .../lib/src/api/link_mode.dart | 25 -- .../lib/src/api/native_code_asset.dart | 92 ++++--- pkgs/native_assets_cli/lib/src/api/os.dart | 2 +- .../lib/src/model/link_mode.dart | 26 -- .../lib/src/model/native_code_asset.dart | 254 +++++++++++------- pkgs/native_assets_cli/lib/src/model/os.dart | 4 +- .../test/api/asset_test.dart | 56 +--- .../test/api/build_output_test.dart | 6 +- .../test/model/asset_test.dart | 47 ++-- .../test/model/build_output_test.dart | 57 ++-- .../test/model/link_mode_test.dart | 2 +- .../test/model/target_test.dart | 5 +- .../lib/src/cbuilder/cbuilder.dart | 10 +- .../cbuilder/cbuilder_cross_android_test.dart | 6 +- .../cbuilder/cbuilder_cross_ios_test.dart | 8 +- .../cbuilder_cross_linux_host_test.dart | 4 +- .../cbuilder_cross_macos_host_test.dart | 4 +- .../cbuilder_cross_windows_host_test.dart | 10 +- 27 files changed, 315 insertions(+), 336 deletions(-) delete mode 100644 pkgs/native_assets_cli/lib/src/api/c_code_asset.dart delete mode 100644 pkgs/native_assets_cli/lib/src/api/link_mode.dart delete mode 100644 pkgs/native_assets_cli/lib/src/model/link_mode.dart diff --git a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart index c2cc4739e..9524e5424 100644 --- a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart @@ -53,19 +53,19 @@ void main() async { // This package honors preferences. expect( (resultDynamic.assets.single as NativeCodeAssetImpl).linkMode, - LinkModeImpl.dynamicLoading, + DynamicLoadingBundledDylibImpl(), ); expect( (resultPreferDynamic.assets.single as NativeCodeAssetImpl).linkMode, - LinkModeImpl.dynamicLoading, + DynamicLoadingBundledDylibImpl(), ); expect( (resultStatic.assets.single as NativeCodeAssetImpl).linkMode, - LinkModeImpl.static, + StaticLinkingImpl(), ); expect( (resultPreferStatic.assets.single as NativeCodeAssetImpl).linkMode, - LinkModeImpl.static, + StaticLinkingImpl(), ); }); }); diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 3cd631816..89e3a2533 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -13,10 +13,9 @@ void main(List arguments) async { file: config.outputDirectory.resolve( OS.current.dylibFileName('foo'), ), - linkMode: LinkMode.dynamicLoading, + linkMode: DynamicLoadingBundledDylib(), os: OS.current, architecture: Architecture.current, - dynamicLoading: BundledDylib(), ), ); }); diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 4a172a315..284cf030f 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -37,10 +37,9 @@ void main(List args) async { package: packageName, name: 'asset.txt', file: assetPath, - linkMode: LinkMode.dynamicLoading, + linkMode: DynamicLoadingBundledDylib(), os: config.targetOS, architecture: config.targetArchitecture, - dynamicLoading: BundledDylib(), ), ); }); diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index af16ae9f7..09811157a 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -10,16 +10,17 @@ export 'src/api/architecture.dart' show Architecture; export 'src/api/asset.dart' show Asset, - BundledDylib, + DynamicLoadingBundledDylib, + DynamicLoadingSystemDylib, + LinkMode, LookupInExecutable, LookupInProcess, NativeCodeAsset, - SystemDylib; + StaticLinking; export 'src/api/build.dart'; export 'src/api/build_config.dart' show BuildConfig, CCompilerConfig; export 'src/api/build_mode.dart' show BuildMode; export 'src/api/build_output.dart' show BuildOutput; export 'src/api/ios_sdk.dart' show IOSSdk; -export 'src/api/link_mode.dart' show LinkMode; export 'src/api/link_mode_preference.dart' show LinkModePreference; export 'src/api/os.dart' show OS; diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index c79cb9f6a..b8fc9e8db 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -20,17 +20,18 @@ export 'src/api/architecture.dart' show ArchitectureImpl; export 'src/api/asset.dart' show AssetImpl, - BundledDylibImpl, DataAssetImpl, + DynamicLoadingBundledDylibImpl, + DynamicLoadingSystemDylibImpl, + LinkModeImpl, LookupInExecutableImpl, LookupInProcessImpl, NativeCodeAssetImpl, - SystemDylibImpl; + StaticLinkingImpl; export 'src/api/build_config.dart' show BuildConfigImpl, CCompilerConfigImpl; export 'src/api/build_mode.dart' show BuildModeImpl; export 'src/api/build_output.dart' show BuildOutputImpl; export 'src/api/ios_sdk.dart' show IOSSdkImpl; -export 'src/api/link_mode.dart' show LinkModeImpl; export 'src/api/link_mode_preference.dart' show LinkModePreferenceImpl; export 'src/api/os.dart' show OSImpl; export 'src/model/dependencies.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index dc9b5c3e3..00fcc9c4a 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -11,7 +11,6 @@ import '../utils/yaml.dart'; import 'architecture.dart'; import 'build_config.dart'; import 'build_output.dart'; -import 'link_mode.dart'; import 'os.dart'; part 'native_code_asset.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index bf3689eb2..e21df02e3 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -75,10 +75,9 @@ import 'build_output.dart'; /// NativeCodeAsset( /// id: 'library:$packageName/asset.txt', /// file: assetPath, -/// linkMode: LinkMode.dynamic, +/// linkMode: BundledDylib(), /// os: config.targetOS, /// architecture: config.targetArchitecture, -/// dynamicLoading: BundledDylib(), /// ), /// ); /// }); diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 53f779941..479f23b36 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -18,7 +18,6 @@ import 'architecture.dart'; import 'asset.dart'; import 'build_mode.dart'; import 'ios_sdk.dart'; -import 'link_mode.dart'; import 'link_mode_preference.dart'; import 'os.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/c_code_asset.dart deleted file mode 100644 index e69de29bb..000000000 diff --git a/pkgs/native_assets_cli/lib/src/api/link_mode.dart b/pkgs/native_assets_cli/lib/src/api/link_mode.dart deleted file mode 100644 index 6f296dc66..000000000 --- a/pkgs/native_assets_cli/lib/src/api/link_mode.dart +++ /dev/null @@ -1,25 +0,0 @@ -// 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 'asset.dart'; - -part '../model/link_mode.dart'; - -/// The link mode for [NativeCodeAsset]s. -abstract final class LinkMode { - /// Dynamic loading. - /// - /// Supported in the Dart and Flutter SDK. - /// - /// Note: Dynamic loading is not equal to dynamic linking. Dynamic linking - /// would have to run the linker at compile-time, which is currently not - /// supported in the Dart and Flutter SDK. - static const LinkMode dynamicLoading = LinkModeImpl.dynamicLoading; - - /// Static linking. - /// - /// Not yet supported in the Dart and Flutter SDK. - // TODO(https://github.com/dart-lang/sdk/issues/49418): Support static linking. - static const LinkMode static = LinkModeImpl.static; -} diff --git a/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart index 795bec9c6..d72334136 100644 --- a/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart @@ -27,10 +27,11 @@ part of 'asset.dart'; /// /// There are several types of native code assets: /// * Assets which designate symbols present in the target system -/// ([SystemDylib]), process ([LookupInProcess]), or executable +/// ([DynamicLoadingSystemDylib]), process ([LookupInProcess]), or executable /// ([LookupInExecutable]). These assets do not have a [file]. -/// * Dynamic libraries bundled into the application ([BundledDylib]). These -/// assets must provide a [file] to be bundled. +/// * Dynamic libraries bundled into the application +/// ([DynamicLoadingBundledDylib]). These assets must provide a [file] to be +/// bundled. /// /// An application is compiled to run on a specific target [os] and /// [architecture]. Different targets require different assets, so the package @@ -40,9 +41,10 @@ part of 'asset.dart'; /// is either brought in "manually" by having the package developer specify a /// [file] path of the asset on the current system, it can be part of the Dart /// or Flutter SDK ([LookupInProcess]), or it can be already present in the -/// target system ([SystemDylib]). If the asset is bundled "manually", the Dart -/// or Flutter SDK will take care of copying the asset [file] from its specified -/// location on the current system into the application bundle. +/// target system ([DynamicLoadingSystemDylib]). If the asset is bundled +/// "manually", the Dart or Flutter SDK will take care of copying the asset +/// [file] from its specified location on the current system into the +/// application bundle. abstract final class NativeCodeAsset implements Asset { /// The operating system this asset can run on. OS get os; @@ -57,30 +59,15 @@ abstract final class NativeCodeAsset implements Asset { /// Either dynamic loading or static linking. LinkMode get linkMode; - /// The dynamic loading method when the [linkMode] is - /// [LinkMode.dynamicLoading]. - /// - /// Throws a [StateError] when accessed with [linkMode] is [LinkMode.static]. - DynamicLoading get dynamicLoading; - /// Constructs a native code asset. /// /// The [id] of this asset is a uri `package:/` from [package] /// and [name]. - /// - /// If [linkMode] is [LinkMode.dynamicLoading], a non-null [dynamicLoading] - /// must be provided. If [linkMode] is [LinkMode.static], [dynamicLoading] - /// must not be provided. - /// - /// If [linkMode] is [LinkMode.dynamicLoading] and [dynamicLoading] is not - /// [BundledDylib], a [file] must not be provided. If [dynamicLoading] is - /// [BundledDylib], a [file] must be provided in non-[BuildConfig.dryRun]s. factory NativeCodeAsset({ required String package, required String name, required LinkMode linkMode, required OS os, - DynamicLoading? dynamicLoading, Uri? file, Architecture? architecture, }) => @@ -89,40 +76,79 @@ abstract final class NativeCodeAsset implements Asset { linkMode: linkMode as LinkModeImpl, os: os as OSImpl, architecture: architecture as ArchitectureImpl?, - dynamicLoading: dynamicLoading as DynamicLoadingImpl?, file: file, ); static const String type = 'native_code'; } -/// The dynamic loading method when the [NativeCodeAsset.linkMode] is -/// [LinkMode.dynamicLoading]. -abstract final class DynamicLoading {} +/// The link mode for a [NativeCodeAsset]. +/// +/// Known linking modes: +/// +/// * [DynamicLoading] +/// * [DynamicLoadingBundledDylib] +/// * [DynamicLoadingSystemDylib] +/// * [LookupInProcess] +/// * [LookupInExecutable] +/// * [StaticLinking] +/// +/// See the documentation on the above classes. +abstract final class LinkMode {} + +/// The [NativeCodeAsset] will be loaded at runtime. +/// +/// Nothing happens at native code linking time. +/// +/// Supported in the Dart and Flutter SDK. +/// +/// Note: Dynamic loading is not equal to dynamic linking. Dynamic linking +/// would have to run the linker at compile-time, which is currently not +/// supported in the Dart and Flutter SDK. +abstract final class DynamicLoading implements LinkMode {} -/// The asset file should be bundled by Dart/Flutter. +/// The dynamic library is bundled by Dart/Flutter at build time. +/// +/// At runtime, the dynamic library will be loaded and the symbols will be +/// looked up in this dynamic library. /// /// An asset with this dynamic loading method must provide a [Asset.file]. The /// Dart and Flutter SDK will bundle this code in the final application. -abstract final class BundledDylib implements DynamicLoading { - factory BundledDylib() = BundledDylibImpl; +abstract final class DynamicLoadingBundledDylib implements DynamicLoading { + factory DynamicLoadingBundledDylib() = DynamicLoadingBundledDylibImpl; } -/// Asset is avaliable on the target system `PATH`. -abstract final class SystemDylib implements DynamicLoading { +/// The dynamic library is avaliable on the target system `PATH`. +/// +/// At buildtime, nothing happens. +/// +/// At runtime, the dynamic library will be loaded and the symbols will be +/// looked up in this dynamic library. +abstract final class DynamicLoadingSystemDylib implements DynamicLoading { Uri get uri; - factory SystemDylib(Uri uri) = SystemDylibImpl; + factory DynamicLoadingSystemDylib(Uri uri) = DynamicLoadingSystemDylibImpl; } -/// Asset is loaded in the process and symbols are available through +/// The native code is loaded in the process and symbols are available through /// `DynamicLibrary.process()`. abstract final class LookupInProcess implements DynamicLoading { factory LookupInProcess() = LookupInProcessImpl; } -/// Asset is embedded in executable and symbols are available through +/// The native code is embedded in executable and symbols are available through /// `DynamicLibrary.executable()`. abstract final class LookupInExecutable implements DynamicLoading { factory LookupInExecutable() = LookupInExecutableImpl; } + +/// Static linking. +/// +/// At native linking time, native function names will be resolved to static +/// libraries. +/// +/// Not yet supported in the Dart and Flutter SDK. +// TODO(https://github.com/dart-lang/sdk/issues/49418): Support static linking. +abstract final class StaticLinking implements LinkMode { + factory StaticLinking() = StaticLinkingImpl; +} diff --git a/pkgs/native_assets_cli/lib/src/api/os.dart b/pkgs/native_assets_cli/lib/src/api/os.dart index 66d47a690..b4b301fa2 100644 --- a/pkgs/native_assets_cli/lib/src/api/os.dart +++ b/pkgs/native_assets_cli/lib/src/api/os.dart @@ -7,7 +7,7 @@ import 'dart:io'; import '../model/target.dart'; import 'architecture.dart'; -import 'link_mode.dart'; +import 'asset.dart'; part '../model/os.dart'; diff --git a/pkgs/native_assets_cli/lib/src/model/link_mode.dart b/pkgs/native_assets_cli/lib/src/model/link_mode.dart deleted file mode 100644 index aff53d9e4..000000000 --- a/pkgs/native_assets_cli/lib/src/model/link_mode.dart +++ /dev/null @@ -1,26 +0,0 @@ -// 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. - -part of '../api/link_mode.dart'; - -final class LinkModeImpl implements LinkMode { - final String name; - - const LinkModeImpl._(this.name); - - static const LinkModeImpl dynamicLoading = LinkModeImpl._('dynamic'); - static const LinkModeImpl static = LinkModeImpl._('static'); - - /// Known values for [LinkModeImpl]. - static const List _values = [ - dynamicLoading, - static, - ]; - - factory LinkModeImpl.fromName(String name) => - _values.where((element) => element.name == name).first; - - @override - String toString() => name; -} diff --git a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart index 0a7f708c8..6b07fad01 100644 --- a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart @@ -4,80 +4,140 @@ part of '../api/asset.dart'; -abstract final class DynamicLoadingImpl implements DynamicLoading { - factory DynamicLoadingImpl(String type, Uri? uri) { +abstract final class LinkModeImpl implements LinkMode { + /// v1.0.0 Includes the parent keys. + /// + /// ``` + /// link_mode: dynamic + /// path: + /// path_type: system + /// uri: ${foo3Uri.toFilePath()} + /// ``` + Map toYamlV1_0_0(Uri? file); + + /// v1.1.0 does not include the toplevel keys. + /// + /// ``` + /// type: dynamic_loading_system + /// uri: ${foo3Uri.toFilePath()} + /// ``` + Map toYaml(); + + factory LinkModeImpl(String type, Uri? uri) { switch (type) { - case BundledDylibImpl._typeValueV1_0_0: - // For backwards compatibility. - case BundledDylibImpl._typeValue: - return BundledDylibImpl(); - case SystemDylibImpl._typeValue: - return SystemDylibImpl(uri!); + case DynamicLoadingBundledDylibImpl._typeValueV1_0_0: + case DynamicLoadingBundledDylibImpl._typeValue: + return DynamicLoadingBundledDylibImpl(); + case DynamicLoadingSystemDylibImpl._typeValueV1_0_0: + case DynamicLoadingSystemDylibImpl._typeValue: + return DynamicLoadingSystemDylibImpl(uri!); + case LookupInExecutableImpl._typeValueV1_0_0: case LookupInExecutableImpl._typeValue: return LookupInExecutableImpl(); + case LookupInProcessImpl._typeValueV1_0_0: case LookupInProcessImpl._typeValue: return LookupInProcessImpl(); + case StaticLinkingImpl._typeValue: + return StaticLinkingImpl(); } throw FormatException('Unknown type: $type.'); } - factory DynamicLoadingImpl.fromYaml(YamlMap yamlMap) { - final type = as(yamlMap[_typeKey] ?? yamlMap[_pathTypeKey]); + /// v1.1.0 only. + factory LinkModeImpl.fromYaml(YamlMap yamlMap) { + final type = as(yamlMap[_typeKey]); final uriString = as(yamlMap[_uriKey]); final uri = uriString != null ? Uri(path: uriString) : null; - return DynamicLoadingImpl(type, uri); + return LinkModeImpl(type, uri); } - Map toYaml(Version version, Uri? file); + static const _typeKey = 'type'; + static const _uriKey = 'uri'; +} + +abstract final class DynamicLoadingImpl + implements LinkModeImpl, DynamicLoading { + factory DynamicLoadingImpl(String type, Uri? uri) { + switch (type) { + case DynamicLoadingBundledDylibImpl._typeValueV1_0_0: + // For backwards compatibility. + case DynamicLoadingBundledDylibImpl._typeValue: + return DynamicLoadingBundledDylibImpl(); + case DynamicLoadingSystemDylibImpl._typeValue: + return DynamicLoadingSystemDylibImpl(uri!); + case LookupInExecutableImpl._typeValue: + return LookupInExecutableImpl(); + case LookupInProcessImpl._typeValue: + return LookupInProcessImpl(); + } + throw FormatException('Unknown type: $type.'); + } - static const _pathTypeKey = 'path_type'; + static const _pathTypeKeyV1_0_0 = 'path_type'; static const _typeKey = 'type'; static const _uriKey = 'uri'; + + static const _typeValueV1_0_0 = 'dynamic'; } -final class BundledDylibImpl implements DynamicLoadingImpl, BundledDylib { - BundledDylibImpl._(); +final class DynamicLoadingBundledDylibImpl + implements DynamicLoadingImpl, DynamicLoadingBundledDylib { + DynamicLoadingBundledDylibImpl._(); - static final BundledDylibImpl _singleton = BundledDylibImpl._(); + static final DynamicLoadingBundledDylibImpl _singleton = + DynamicLoadingBundledDylibImpl._(); - factory BundledDylibImpl() => _singleton; + factory DynamicLoadingBundledDylibImpl() => _singleton; static const _typeValueV1_0_0 = 'absolute'; - static const _typeValue = 'bundle'; + static const _typeValue = 'dynamic_loading_bundle'; @override - Map toYaml(Version version, Uri? file) => { - if (version == Version(1, 0, 0)) ...{ - DynamicLoadingImpl._pathTypeKey: _typeValueV1_0_0, + Map toYaml() => { + DynamicLoadingImpl._typeKey: _typeValue, + }; + + @override + Map toYamlV1_0_0(Uri? file) => { + NativeCodeAssetImpl._linkModeKey: DynamicLoadingImpl._typeValueV1_0_0, + NativeCodeAssetImpl._pathKey: { + DynamicLoadingImpl._pathTypeKeyV1_0_0: _typeValueV1_0_0, DynamicLoadingImpl._uriKey: file!.toFilePath(), - } else - DynamicLoadingImpl._typeKey: _typeValue, + } }; } -final class SystemDylibImpl implements DynamicLoadingImpl, SystemDylib { +final class DynamicLoadingSystemDylibImpl + implements DynamicLoadingImpl, DynamicLoadingSystemDylib { @override final Uri uri; - SystemDylibImpl(this.uri); + DynamicLoadingSystemDylibImpl(this.uri); - static const _typeValue = 'system'; + static const _typeValue = 'dynamic_loading_system'; + static const _typeValueV1_0_0 = 'system'; @override - Map toYaml(Version version, Uri? file) => { - if (version == Version(1, 0, 0)) - DynamicLoadingImpl._pathTypeKey: _typeValue - else - DynamicLoadingImpl._typeKey: _typeValue, + Map toYaml() => { + DynamicLoadingImpl._typeKey: _typeValue, DynamicLoadingImpl._uriKey: uri.toFilePath(), }; + @override + Map toYamlV1_0_0(Uri? file) => { + NativeCodeAssetImpl._linkModeKey: DynamicLoadingImpl._typeValueV1_0_0, + NativeCodeAssetImpl._pathKey: { + DynamicLoadingImpl._pathTypeKeyV1_0_0: _typeValueV1_0_0, + DynamicLoadingImpl._uriKey: uri.toFilePath(), + } + }; + @override int get hashCode => Object.hash(uri, 133723); @override bool operator ==(Object other) { - if (other is! SystemDylibImpl) { + if (other is! DynamicLoadingSystemDylibImpl) { return false; } return uri == other.uri; @@ -91,14 +151,20 @@ final class LookupInProcessImpl implements DynamicLoadingImpl, LookupInProcess { factory LookupInProcessImpl() => _singleton; - static const _typeValue = 'process'; + static const _typeValue = 'dynamic_loading_process'; + static const _typeValueV1_0_0 = 'process'; @override - Map toYaml(Version version, Uri? file) => { - if (version == Version(1, 0, 0)) - DynamicLoadingImpl._pathTypeKey: _typeValue - else - DynamicLoadingImpl._typeKey: _typeValue, + Map toYaml() => { + DynamicLoadingImpl._typeKey: _typeValue, + }; + + @override + Map toYamlV1_0_0(Uri? file) => { + NativeCodeAssetImpl._linkModeKey: DynamicLoadingImpl._typeValueV1_0_0, + NativeCodeAssetImpl._pathKey: { + DynamicLoadingImpl._pathTypeKeyV1_0_0: _typeValueV1_0_0, + } }; } @@ -110,14 +176,40 @@ final class LookupInExecutableImpl factory LookupInExecutableImpl() => _singleton; - static const _typeValue = 'executable'; + static const _typeValue = 'dynamic_loading_executable'; + static const _typeValueV1_0_0 = 'executable'; + + @override + Map toYaml() => { + DynamicLoadingImpl._typeKey: _typeValue, + }; + + @override + Map toYamlV1_0_0(Uri? file) => { + NativeCodeAssetImpl._linkModeKey: DynamicLoadingImpl._typeValueV1_0_0, + NativeCodeAssetImpl._pathKey: { + DynamicLoadingImpl._pathTypeKeyV1_0_0: _typeValueV1_0_0, + } + }; +} + +final class StaticLinkingImpl implements LinkModeImpl, StaticLinking { + StaticLinkingImpl._(); + + static final StaticLinkingImpl _singleton = StaticLinkingImpl._(); + + factory StaticLinkingImpl() => _singleton; + + static const _typeValue = 'static'; @override - Map toYaml(Version version, Uri? file) => { - if (version == Version(1, 0, 0)) - DynamicLoadingImpl._pathTypeKey: _typeValue - else - DynamicLoadingImpl._typeKey: _typeValue, + Map toYaml() => { + DynamicLoadingImpl._typeKey: _typeValue, + }; + + @override + Map toYamlV1_0_0(Uri? file) => { + NativeCodeAssetImpl._linkModeKey: _typeValue, }; } @@ -131,16 +223,6 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { @override final String id; - @override - DynamicLoadingImpl get dynamicLoading { - if (linkMode == LinkMode.static) { - throw StateError('LinkMode is LinkMode.static.'); - } - return _dynamicLoading!; - } - - final DynamicLoadingImpl? _dynamicLoading; - @override final OSImpl os; @@ -152,25 +234,10 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { required this.id, required this.linkMode, required this.os, - DynamicLoadingImpl? dynamicLoading, this.architecture, - }) : _dynamicLoading = dynamicLoading { - if (linkMode == LinkMode.dynamicLoading && dynamicLoading == null) { - throw ArgumentError.value( - dynamicLoading, - 'dynamicLoading', - 'Must not be null if linkMode == LinkMode.dynamic.', - ); - } - if (linkMode == LinkMode.static && dynamicLoading != null) { - throw ArgumentError.value( - dynamicLoading, - 'dynamicLoading', - 'Must be null if linkMode == LinkMode.static.', - ); - } - if (linkMode == LinkMode.dynamicLoading && - dynamicLoading is! BundledDylib && + }) { + if (linkMode is DynamicLoading && + linkMode is! DynamicLoadingBundledDylib && file != null) { throw ArgumentError.value( file, @@ -181,17 +248,32 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { } factory NativeCodeAssetImpl.fromYaml(YamlMap yamlMap) { - final linkMode = LinkModeImpl.fromName(as(yamlMap[_linkModeKey])); - final dynamicLoadingYaml = - as(yamlMap[_dynamicLoadingKey] ?? yamlMap[_pathKey]); - final dynamicLoading = dynamicLoadingYaml == null - ? null - : DynamicLoadingImpl.fromYaml(dynamicLoadingYaml); + final LinkModeImpl linkMode; + final linkModeYaml = yamlMap[_linkModeKey]; + if (linkModeYaml is String) { + // v1.0.0 + if (linkModeYaml == StaticLinkingImpl._typeValue) { + linkMode = StaticLinkingImpl(); + } else { + assert(linkModeYaml == DynamicLoadingImpl._typeValueV1_0_0); + final pathYaml = as(yamlMap[_pathKey]); + final type = + as(pathYaml[DynamicLoadingImpl._pathTypeKeyV1_0_0]); + final uriString = as(pathYaml[DynamicLoadingImpl._uriKey]); + final uri = uriString != null ? Uri(path: uriString) : null; + linkMode = LinkModeImpl(type, uri); + } + } else { + // v1.1.0 + linkMode = LinkModeImpl.fromYaml(as(linkModeYaml)); + } + final fileString = as(yamlMap[_fileKey]); final Uri? file; if (fileString != null) { file = Uri(path: fileString); - } else if (dynamicLoading is BundledDylibImpl && + } else if ((linkMode is DynamicLoadingBundledDylibImpl || + linkMode is StaticLinkingImpl) && yamlMap[_pathKey] != null) { // Compatibility with v1.0.0. final oldPath = as( @@ -220,8 +302,6 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { return NativeCodeAssetImpl( id: as(yamlMap[_idKey]), - dynamicLoading: - linkMode == LinkMode.dynamicLoading ? dynamicLoading : null, os: os, architecture: architecture, linkMode: linkMode, @@ -234,7 +314,6 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { String? id, OSImpl? os, ArchitectureImpl? architecture, - DynamicLoadingImpl? dynamicLoading, Uri? file, }) => NativeCodeAssetImpl( @@ -242,7 +321,6 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { linkMode: linkMode ?? this.linkMode, os: os ?? this.os, architecture: this.architecture ?? architecture, - dynamicLoading: dynamicLoading ?? this.dynamicLoading, file: file ?? this.file, ); @@ -255,7 +333,6 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { other.linkMode == linkMode && other.architecture == architecture && other.os == os && - other._dynamicLoading == _dynamicLoading && other.file == file; } @@ -265,7 +342,6 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { linkMode, architecture, os, - _dynamicLoading, file, ); @@ -274,18 +350,15 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { if (version == Version(1, 0, 0)) { return { _idKey: id, - _linkModeKey: linkMode.name, - _pathKey: dynamicLoading.toYaml(version, file), + ...linkMode.toYamlV1_0_0(file), _targetKey: Target.fromArchitectureAndOS(architecture!, os).toString(), }..sortOnKey(); } return { if (architecture != null) _architectureKey: architecture.toString(), - if (linkMode == LinkMode.dynamicLoading) - _dynamicLoadingKey: dynamicLoading.toYaml(version, file), if (file != null) _fileKey: file!.toFilePath(), _idKey: id, - _linkModeKey: linkMode.name, + _linkModeKey: linkMode.toYaml(), _osKey: os.toString(), typeKey: NativeCodeAsset.type, }..sortOnKey(); @@ -295,7 +368,6 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { static const _idKey = 'id'; static const _linkModeKey = 'link_mode'; static const _pathKey = 'path'; - static const _dynamicLoadingKey = 'dynamic_loading'; static const _targetKey = 'target'; static const _fileKey = 'file'; static const _osKey = 'os'; diff --git a/pkgs/native_assets_cli/lib/src/model/os.dart b/pkgs/native_assets_cli/lib/src/model/os.dart index 3ec1e0f28..73fd3ea0c 100644 --- a/pkgs/native_assets_cli/lib/src/model/os.dart +++ b/pkgs/native_assets_cli/lib/src/model/os.dart @@ -113,10 +113,10 @@ final class OSImpl implements OS { @override String libraryFileName(String name, LinkMode linkMode) { - if (linkMode == LinkModeImpl.dynamicLoading) { + if (linkMode is DynamicLoading) { return dylibFileName(name); } - assert(linkMode == LinkModeImpl.static); + assert(linkMode is StaticLinking); return staticlibFileName(name); } diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index a1d08589e..e15901795 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -13,34 +13,30 @@ void main() { package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), - dynamicLoading: BundledDylib(), + linkMode: DynamicLoadingBundledDylib(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', name: 'foo3', - dynamicLoading: SystemDylib(Uri(path: 'libfoo3.so')), + linkMode: DynamicLoadingSystemDylib(Uri(path: 'libfoo3.so')), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', name: 'foo4', - dynamicLoading: LookupInExecutable(), + linkMode: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', name: 'foo5', - dynamicLoading: LookupInProcess(), + linkMode: LookupInProcess(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', @@ -48,16 +44,15 @@ void main() { file: Uri(path: 'path/to/libbar.a'), os: OS.linux, architecture: Architecture.arm64, - linkMode: LinkMode.static, + linkMode: StaticLinking(), ), NativeCodeAsset( package: 'my_package', name: 'bla', file: Uri(path: 'path/with spaces/bla.dll'), - dynamicLoading: BundledDylib(), + linkMode: DynamicLoadingBundledDylib(), os: OS.windows, architecture: Architecture.x64, - linkMode: LinkMode.dynamicLoading, ), DataAsset( package: 'my_package', @@ -74,48 +69,11 @@ void main() { package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), - dynamicLoading: BundledDylib(), + linkMode: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.static, ), throwsArgumentError, ); - expect( - () => NativeCodeAsset( - package: 'my_package', - name: 'foo', - file: Uri.file('path/to/libfoo.so'), - os: OS.android, - architecture: Architecture.x64, - linkMode: LinkMode.dynamicLoading, - ), - throwsArgumentError, - ); - expect( - () => NativeCodeAsset( - package: 'my_package', - name: 'foo', - file: Uri.file('path/to/libfoo.so'), - dynamicLoading: LookupInExecutable(), - os: OS.android, - architecture: Architecture.x64, - linkMode: LinkMode.dynamicLoading, - ), - throwsArgumentError, - ); - - final staticLinkingAsset = NativeCodeAsset( - package: 'my_package', - name: 'foo', - file: Uri.file('path/to/libfoo.so'), - os: OS.android, - architecture: Architecture.x64, - linkMode: LinkMode.static, - ); - expect( - () => staticLinkingAsset.dynamicLoading, - throwsStateError, - ); }); } diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index 552a2c069..a2b1e98d3 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -26,18 +26,16 @@ void main() { package: 'my_package', name: 'foo', file: Uri(path: 'path/to/libfoo.so'), - dynamicLoading: BundledDylib(), + linkMode: DynamicLoadingBundledDylib(), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamicLoading, ), NativeCodeAsset( package: 'my_package', name: 'foo2', - dynamicLoading: SystemDylib(Uri(path: 'path/to/libfoo2.so')), + linkMode: DynamicLoadingSystemDylib(Uri(path: 'path/to/libfoo2.so')), os: OS.android, architecture: Architecture.x64, - linkMode: LinkMode.dynamicLoading, ), ], dependencies: [ diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 2d4a10e7b..ce5eecf00 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -20,46 +20,41 @@ void main() { NativeCodeAssetImpl( id: 'package:my_package/foo', file: fooUri, - dynamicLoading: BundledDylibImpl(), + linkMode: DynamicLoadingBundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo3', - dynamicLoading: SystemDylibImpl(foo3Uri), + linkMode: DynamicLoadingSystemDylibImpl(foo3Uri), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo4', - dynamicLoading: LookupInExecutableImpl(), + linkMode: LookupInExecutableImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo5', - dynamicLoading: LookupInProcessImpl(), + linkMode: LookupInProcessImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/bar', file: barUri, os: OSImpl.linux, architecture: ArchitectureImpl.arm64, - linkMode: LinkModeImpl.static, + linkMode: StaticLinkingImpl(), ), NativeCodeAssetImpl( id: 'package:my_package/bla', file: blaUri, - dynamicLoading: BundledDylibImpl(), + linkMode: DynamicLoadingBundledDylibImpl(), os: OSImpl.windows, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), ]; final dataAssets = [ @@ -113,47 +108,43 @@ void main() { target: windows_x64'''; final assetsYamlEncoding = '''- architecture: x64 - dynamic_loading: - type: bundle file: ${fooUri.toFilePath()} id: package:my_package/foo - link_mode: dynamic + link_mode: + type: dynamic_loading_bundle os: android type: native_code - architecture: x64 - dynamic_loading: - type: system - uri: ${foo3Uri.toFilePath()} id: package:my_package/foo3 - link_mode: dynamic + link_mode: + type: dynamic_loading_system + uri: ${foo3Uri.toFilePath()} os: android type: native_code - architecture: x64 - dynamic_loading: - type: executable id: package:my_package/foo4 - link_mode: dynamic + link_mode: + type: dynamic_loading_executable os: android type: native_code - architecture: x64 - dynamic_loading: - type: process id: package:my_package/foo5 - link_mode: dynamic + link_mode: + type: dynamic_loading_process os: android type: native_code - architecture: arm64 file: ${barUri.toFilePath()} id: package:my_package/bar - link_mode: static + link_mode: + type: static os: linux type: native_code - architecture: x64 - dynamic_loading: - type: bundle file: ${blaUri.toFilePath()} id: package:my_package/bla - link_mode: dynamic + link_mode: + type: dynamic_loading_bundle os: windows type: native_code - id: package:my_package/my_data_asset diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index 4070192e3..b180db894 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -26,31 +26,28 @@ void main() { NativeCodeAssetImpl( id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), - dynamicLoading: BundledDylibImpl(), + linkMode: DynamicLoadingBundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo2', - dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), + linkMode: + DynamicLoadingSystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo3', - dynamicLoading: LookupInProcessImpl(), + linkMode: LookupInProcessImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo4', - dynamicLoading: LookupInExecutableImpl(), + linkMode: LookupInExecutableImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), ], dependencies: Dependencies([ @@ -94,33 +91,29 @@ version: 1.0.0'''; final yamlEncoding = '''timestamp: 2022-11-10 13:25:01.000 assets: - architecture: x64 - dynamic_loading: - type: bundle file: path/to/libfoo.so id: package:my_package/foo - link_mode: dynamic + link_mode: + type: dynamic_loading_bundle os: android type: native_code - architecture: x64 - dynamic_loading: - type: system - uri: path/to/libfoo2.so id: package:my_package/foo2 - link_mode: dynamic + link_mode: + type: dynamic_loading_system + uri: path/to/libfoo2.so os: android type: native_code - architecture: x64 - dynamic_loading: - type: process id: package:my_package/foo3 - link_mode: dynamic + link_mode: + type: dynamic_loading_process os: android type: native_code - architecture: x64 - dynamic_loading: - type: executable id: package:my_package/foo4 - link_mode: dynamic + link_mode: + type: dynamic_loading_executable os: android type: native_code dependencies: @@ -217,7 +210,7 @@ assets: dependencies: [] metadata: key: value -version: ${BuildOutputImpl.latestVersion}'''), +version: 1.0.0'''), throwsFormatException, ); expect( @@ -233,7 +226,7 @@ dependencies: 1: foo metadata: key: value -version: ${BuildOutputImpl.latestVersion}'''), +version: 1.0.0'''), throwsFormatException, ); expect( @@ -248,14 +241,12 @@ assets: dependencies: [] metadata: 123: value -version: ${BuildOutputImpl.latestVersion}'''), +version: 1.0.0'''), throwsFormatException, ); }); test('BuildOutput dependencies can be modified', () { - // TODO(https://github.com/dart-lang/native/issues/25): - // Remove once dependencies are made immutable. final buildOutput = BuildOutputImpl(); expect( () => buildOutput.addDependencies([Uri.file('path/to/file.ext')]), @@ -270,17 +261,16 @@ version: ${BuildOutputImpl.latestVersion}'''), NativeCodeAssetImpl( id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), - dynamicLoading: BundledDylibImpl(), + linkMode: DynamicLoadingBundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), NativeCodeAssetImpl( id: 'package:my_package/foo2', - dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), + linkMode: + DynamicLoadingSystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), ], dependencies: Dependencies([ @@ -300,19 +290,18 @@ version: ${BuildOutputImpl.latestVersion}'''), NativeCodeAssetImpl( id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), - dynamicLoading: BundledDylibImpl(), + linkMode: DynamicLoadingBundledDylibImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), ); buildOutput2.addAssets([ NativeCodeAssetImpl( id: 'package:my_package/foo2', - dynamicLoading: SystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), + linkMode: + DynamicLoadingSystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, - linkMode: LinkModeImpl.dynamicLoading, ), ]); buildOutput2.addDependency( diff --git a/pkgs/native_assets_cli/test/model/link_mode_test.dart b/pkgs/native_assets_cli/test/model/link_mode_test.dart index ba86d444c..d27fdc129 100644 --- a/pkgs/native_assets_cli/test/model/link_mode_test.dart +++ b/pkgs/native_assets_cli/test/model/link_mode_test.dart @@ -7,6 +7,6 @@ import 'package:test/test.dart'; void main() { test('LinkMode toString', () async { - LinkMode.static.toString(); + StaticLinking().toString(); }); } diff --git a/pkgs/native_assets_cli/test/model/target_test.dart b/pkgs/native_assets_cli/test/model/target_test.dart index 76e025bc9..0ee1bc45f 100644 --- a/pkgs/native_assets_cli/test/model/target_test.dart +++ b/pkgs/native_assets_cli/test/model/target_test.dart @@ -13,11 +13,12 @@ void main() { expect(OSImpl.android.dylibFileName('foo'), 'libfoo.so'); expect(OSImpl.android.staticlibFileName('foo'), 'libfoo.a'); expect(OSImpl.windows.dylibFileName('foo'), 'foo.dll'); - expect(OSImpl.windows.libraryFileName('foo', LinkModeImpl.dynamicLoading), + expect( + OSImpl.windows.libraryFileName('foo', DynamicLoadingBundledDylibImpl()), 'foo.dll'); expect(OSImpl.windows.staticlibFileName('foo'), 'foo.lib'); expect( - OSImpl.windows.libraryFileName('foo', LinkModeImpl.static), 'foo.lib'); + OSImpl.windows.libraryFileName('foo', StaticLinkingImpl()), 'foo.lib'); expect(OSImpl.windows.executableFileName('foo'), 'foo.exe'); }); diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 43c322e66..d184d75d2 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -222,11 +222,11 @@ class CBuilder implements Builder { sources: sources, includes: includes, dynamicLibrary: _type == _CBuilderType.library && - linkMode == LinkMode.dynamicLoading + linkMode == DynamicLoadingBundledDylib() ? libUri : null, staticLibrary: - _type == _CBuilderType.library && linkMode == LinkMode.static + _type == _CBuilderType.library && linkMode == StaticLinking() ? libUri : null, executable: _type == _CBuilderType.executable ? exeUri : null, @@ -256,8 +256,6 @@ class CBuilder implements Builder { os: buildConfig.targetOS, architecture: buildConfig.dryRun ? null : buildConfig.targetArchitecture, - dynamicLoading: - linkMode == LinkMode.dynamicLoading ? BundledDylib() : null, ) ]); } @@ -289,9 +287,9 @@ enum _CBuilderType { LinkMode _linkMode(LinkModePreference preference) { if (preference == LinkModePreference.dynamic || preference == LinkModePreference.preferDynamic) { - return LinkMode.dynamicLoading; + return DynamicLoadingBundledDylib(); } assert(preference == LinkModePreference.static || preference == LinkModePreference.preferStatic); - return LinkMode.static; + return StaticLinking(); } diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart index 044160fd0..3a05f20b4 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart @@ -46,7 +46,7 @@ void main() { /// From https://docs.flutter.dev/reference/supported-platforms. const flutterAndroidNdkVersionHighestSupported = 34; - for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { + for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { for (final target in targets) { for (final apiLevel in [ flutterAndroidNdkVersionLowestBestEffort, @@ -92,7 +92,7 @@ void main() { test('CBuilder API levels binary difference', () async { const target = Architecture.arm64; - const linkMode = LinkMode.dynamicLoading; + final linkMode = DynamicLoadingBundledDylib(); const apiLevel1 = flutterAndroidNdkVersionLowestSupported; const apiLevel2 = flutterAndroidNdkVersionHighestSupported; final tempUri = await tempDirForTest(); @@ -132,7 +132,7 @@ Future buildLib( targetOS: OS.android, targetAndroidNdkApi: androidNdkApi, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamicLoading + linkModePreference: linkMode == DynamicLoadingBundledDylib() ? LinkModePreference.dynamic : LinkModePreference.static, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart index 7550135c0..7fb8379a8 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart @@ -36,7 +36,7 @@ void main() { const name = 'add'; - for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { + for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { for (final targetIOSSdk in IOSSdk.values) { for (final target in targets) { if (target == Architecture.x64 && targetIOSSdk == IOSSdk.iPhoneOS) { @@ -46,7 +46,7 @@ void main() { final libName = OS.iOS.libraryFileName(name, linkMode); for (final installName in [ null, - if (linkMode == LinkMode.dynamicLoading) + if (linkMode == DynamicLoadingBundledDylib()) Uri.file('@executable_path/Frameworks/$libName'), ]) { test( @@ -63,7 +63,7 @@ void main() { targetArchitecture: target, targetOS: OS.iOS, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamicLoading + linkModePreference: linkMode == DynamicLoadingBundledDylib() ? LinkModePreference.dynamic : LinkModePreference.static, targetIOSSdk: targetIOSSdk, @@ -116,7 +116,7 @@ void main() { expect(platform, contains(platformIosSimulator.toString())); } - if (linkMode == LinkMode.dynamicLoading) { + if (linkMode == DynamicLoadingBundledDylib()) { final libInstallName = await runOtoolInstallName(libUri, libName); if (installName == null) { // If no install path is passed, we have an absolute path. diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart index 9e97b5f6e..25a10fd14 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart @@ -36,7 +36,7 @@ void main() { Architecture.riscv64: 'RISC-V', }; - for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { + for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); @@ -51,7 +51,7 @@ void main() { targetArchitecture: target, targetOS: OS.linux, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamicLoading + linkModePreference: linkMode == DynamicLoadingBundledDylib() ? LinkModePreference.dynamic : LinkModePreference.static, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart index bfe219732..287794a7f 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart @@ -34,7 +34,7 @@ void main() { Architecture.x64: '64-bit x86-64', }; - for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { + for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); @@ -49,7 +49,7 @@ void main() { targetArchitecture: target, targetOS: OS.macOS, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamicLoading + linkModePreference: linkMode == DynamicLoadingBundledDylib() ? LinkModePreference.dynamic : LinkModePreference.static, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart index 5f903b14c..008bf20c2 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart @@ -41,12 +41,12 @@ void main() { Architecture.x64: 'x64', }; - const dumpbinFileType = { - LinkMode.dynamicLoading: 'DLL', - LinkMode.static: 'LIBRARY', + final dumpbinFileType = { + DynamicLoadingBundledDylib(): 'DLL', + StaticLinking(): 'LIBRARY', }; - for (final linkMode in [LinkMode.dynamicLoading, LinkMode.static]) { + for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); @@ -61,7 +61,7 @@ void main() { targetOS: OS.windows, targetArchitecture: target, buildMode: BuildMode.release, - linkModePreference: linkMode == LinkMode.dynamicLoading + linkModePreference: linkMode == DynamicLoadingBundledDylib() ? LinkModePreference.dynamic : LinkModePreference.static, ); From acd8a2265c4e1b58b28975169dfb8eb5418ab5e9 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 14 Mar 2024 11:59:11 +0100 Subject: [PATCH 72/75] address comments --- .../packaging_preference_test.dart | 4 +- .../wrong_namespace_asset/build.dart | 2 +- .../example/local_asset/build.dart | 2 +- .../lib/native_assets_cli.dart | 4 +- .../lib/native_assets_cli_internal.dart | 4 +- .../lib/src/api/build_config.dart | 16 +++-- .../lib/src/api/native_code_asset.dart | 18 ++--- .../lib/src/model/native_code_asset.dart | 66 ++++++++++--------- .../test/api/asset_test.dart | 6 +- .../test/api/build_output_test.dart | 4 +- .../test/model/asset_test.dart | 6 +- .../test/model/build_output_test.dart | 15 ++--- .../test/model/target_test.dart | 3 +- .../lib/src/cbuilder/cbuilder.dart | 4 +- .../cbuilder/cbuilder_cross_android_test.dart | 6 +- .../cbuilder/cbuilder_cross_ios_test.dart | 8 +-- .../cbuilder_cross_linux_host_test.dart | 4 +- .../cbuilder_cross_macos_host_test.dart | 4 +- .../cbuilder_cross_windows_host_test.dart | 6 +- 19 files changed, 93 insertions(+), 89 deletions(-) diff --git a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart index 9524e5424..b9be4a481 100644 --- a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart @@ -53,11 +53,11 @@ void main() async { // This package honors preferences. expect( (resultDynamic.assets.single as NativeCodeAssetImpl).linkMode, - DynamicLoadingBundledDylibImpl(), + DynamicLoadingBundledImpl(), ); expect( (resultPreferDynamic.assets.single as NativeCodeAssetImpl).linkMode, - DynamicLoadingBundledDylibImpl(), + DynamicLoadingBundledImpl(), ); expect( (resultStatic.assets.single as NativeCodeAssetImpl).linkMode, diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart index 89e3a2533..17c7e0b43 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/build.dart @@ -13,7 +13,7 @@ void main(List arguments) async { file: config.outputDirectory.resolve( OS.current.dylibFileName('foo'), ), - linkMode: DynamicLoadingBundledDylib(), + linkMode: DynamicLoadingBundled(), os: OS.current, architecture: Architecture.current, ), diff --git a/pkgs/native_assets_cli/example/local_asset/build.dart b/pkgs/native_assets_cli/example/local_asset/build.dart index 284cf030f..4dab00e6c 100644 --- a/pkgs/native_assets_cli/example/local_asset/build.dart +++ b/pkgs/native_assets_cli/example/local_asset/build.dart @@ -37,7 +37,7 @@ void main(List args) async { package: packageName, name: 'asset.txt', file: assetPath, - linkMode: DynamicLoadingBundledDylib(), + linkMode: DynamicLoadingBundled(), os: config.targetOS, architecture: config.targetArchitecture, ), diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 09811157a..13e43029a 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -10,8 +10,8 @@ export 'src/api/architecture.dart' show Architecture; export 'src/api/asset.dart' show Asset, - DynamicLoadingBundledDylib, - DynamicLoadingSystemDylib, + DynamicLoadingBundled, + DynamicLoadingSystem, LinkMode, LookupInExecutable, LookupInProcess, diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index b8fc9e8db..a76b12210 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -21,8 +21,8 @@ export 'src/api/asset.dart' show AssetImpl, DataAssetImpl, - DynamicLoadingBundledDylibImpl, - DynamicLoadingSystemDylibImpl, + DynamicLoadingBundledImpl, + DynamicLoadingSystemImpl, LinkModeImpl, LookupInExecutableImpl, LookupInProcessImpl, diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 479f23b36..715439cd9 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -16,6 +16,7 @@ import '../utils/map.dart'; import '../utils/yaml.dart'; import 'architecture.dart'; import 'asset.dart'; +import 'build.dart'; import 'build_mode.dart'; import 'ios_sdk.dart'; import 'link_mode_preference.dart'; @@ -127,6 +128,9 @@ abstract final class BuildConfig { /// Constructs a config by parsing CLI arguments and loading the config file. /// + /// `build.dart` hooks will most likely use [build] instead of this + /// constructor. + /// /// The [arguments] must be commandline arguments. /// /// If provided, [environment] must be a map containing environment variables. @@ -150,9 +154,9 @@ abstract final class BuildConfig { /// Constructs a config for a non-dry run by providing values for each field. /// - /// `build.dart` hooks will most likely use the unnamed constructor. However, - /// for unit testing code which consumes a [BuildConfig], this constructor - /// facilitates easy construction. + /// `build.dart` hooks will most likely use [build] instead of this + /// constructor. However, for unit testing code which consumes a + /// [BuildConfig], this constructor facilitates easy construction. /// /// For the documentation of the parameters, see the equally named fields. /// @@ -195,9 +199,9 @@ abstract final class BuildConfig { /// Constructs a config for a dry run by providing values for each field. /// - /// `build.dart` hooks will most likely use the unnamed constructor. However, - /// for unit testing code which consumes a [BuildConfig], this constructor - /// facilitates easy construction. + /// `build.dart` hooks will most likely use [build] instead of this + /// constructor. However, for unit testing code which consumes a + /// [BuildConfig], this constructor facilitates easy construction. /// /// For the documentation of the parameters, see the equally named fields. factory BuildConfig.dryRun({ diff --git a/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart b/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart index d72334136..171af2fa0 100644 --- a/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart @@ -27,10 +27,10 @@ part of 'asset.dart'; /// /// There are several types of native code assets: /// * Assets which designate symbols present in the target system -/// ([DynamicLoadingSystemDylib]), process ([LookupInProcess]), or executable +/// ([DynamicLoadingSystem]), process ([LookupInProcess]), or executable /// ([LookupInExecutable]). These assets do not have a [file]. /// * Dynamic libraries bundled into the application -/// ([DynamicLoadingBundledDylib]). These assets must provide a [file] to be +/// ([DynamicLoadingBundled]). These assets must provide a [file] to be /// bundled. /// /// An application is compiled to run on a specific target [os] and @@ -41,7 +41,7 @@ part of 'asset.dart'; /// is either brought in "manually" by having the package developer specify a /// [file] path of the asset on the current system, it can be part of the Dart /// or Flutter SDK ([LookupInProcess]), or it can be already present in the -/// target system ([DynamicLoadingSystemDylib]). If the asset is bundled +/// target system ([DynamicLoadingSystem]). If the asset is bundled /// "manually", the Dart or Flutter SDK will take care of copying the asset /// [file] from its specified location on the current system into the /// application bundle. @@ -87,8 +87,8 @@ abstract final class NativeCodeAsset implements Asset { /// Known linking modes: /// /// * [DynamicLoading] -/// * [DynamicLoadingBundledDylib] -/// * [DynamicLoadingSystemDylib] +/// * [DynamicLoadingBundled] +/// * [DynamicLoadingSystem] /// * [LookupInProcess] /// * [LookupInExecutable] /// * [StaticLinking] @@ -114,8 +114,8 @@ abstract final class DynamicLoading implements LinkMode {} /// /// An asset with this dynamic loading method must provide a [Asset.file]. The /// Dart and Flutter SDK will bundle this code in the final application. -abstract final class DynamicLoadingBundledDylib implements DynamicLoading { - factory DynamicLoadingBundledDylib() = DynamicLoadingBundledDylibImpl; +abstract final class DynamicLoadingBundled implements DynamicLoading { + factory DynamicLoadingBundled() = DynamicLoadingBundledImpl; } /// The dynamic library is avaliable on the target system `PATH`. @@ -124,10 +124,10 @@ abstract final class DynamicLoadingBundledDylib implements DynamicLoading { /// /// At runtime, the dynamic library will be loaded and the symbols will be /// looked up in this dynamic library. -abstract final class DynamicLoadingSystemDylib implements DynamicLoading { +abstract final class DynamicLoadingSystem implements DynamicLoading { Uri get uri; - factory DynamicLoadingSystemDylib(Uri uri) = DynamicLoadingSystemDylibImpl; + factory DynamicLoadingSystem(Uri uri) = DynamicLoadingSystemImpl; } /// The native code is loaded in the process and symbols are available through diff --git a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart index 6b07fad01..1608c8ed6 100644 --- a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart @@ -5,32 +5,36 @@ part of '../api/asset.dart'; abstract final class LinkModeImpl implements LinkMode { - /// v1.0.0 Includes the parent keys. + /// Serialization of the current version. + /// + /// v1.1.0 does not include the toplevel keys. /// /// ``` - /// link_mode: dynamic - /// path: - /// path_type: system + /// type: dynamic_loading_system /// uri: ${foo3Uri.toFilePath()} /// ``` - Map toYamlV1_0_0(Uri? file); + Map toYaml(); - /// v1.1.0 does not include the toplevel keys. + /// Backwards compatibility with v1.0.0 of the protocol + /// + /// Includes the parent keys. /// /// ``` - /// type: dynamic_loading_system + /// link_mode: dynamic + /// path: + /// path_type: system /// uri: ${foo3Uri.toFilePath()} /// ``` - Map toYaml(); + Map toYamlV1_0_0(Uri? file); factory LinkModeImpl(String type, Uri? uri) { switch (type) { - case DynamicLoadingBundledDylibImpl._typeValueV1_0_0: - case DynamicLoadingBundledDylibImpl._typeValue: - return DynamicLoadingBundledDylibImpl(); - case DynamicLoadingSystemDylibImpl._typeValueV1_0_0: - case DynamicLoadingSystemDylibImpl._typeValue: - return DynamicLoadingSystemDylibImpl(uri!); + case DynamicLoadingBundledImpl._typeValueV1_0_0: + case DynamicLoadingBundledImpl._typeValue: + return DynamicLoadingBundledImpl(); + case DynamicLoadingSystemImpl._typeValueV1_0_0: + case DynamicLoadingSystemImpl._typeValue: + return DynamicLoadingSystemImpl(uri!); case LookupInExecutableImpl._typeValueV1_0_0: case LookupInExecutableImpl._typeValue: return LookupInExecutableImpl(); @@ -59,12 +63,12 @@ abstract final class DynamicLoadingImpl implements LinkModeImpl, DynamicLoading { factory DynamicLoadingImpl(String type, Uri? uri) { switch (type) { - case DynamicLoadingBundledDylibImpl._typeValueV1_0_0: + case DynamicLoadingBundledImpl._typeValueV1_0_0: // For backwards compatibility. - case DynamicLoadingBundledDylibImpl._typeValue: - return DynamicLoadingBundledDylibImpl(); - case DynamicLoadingSystemDylibImpl._typeValue: - return DynamicLoadingSystemDylibImpl(uri!); + case DynamicLoadingBundledImpl._typeValue: + return DynamicLoadingBundledImpl(); + case DynamicLoadingSystemImpl._typeValue: + return DynamicLoadingSystemImpl(uri!); case LookupInExecutableImpl._typeValue: return LookupInExecutableImpl(); case LookupInProcessImpl._typeValue: @@ -80,14 +84,14 @@ abstract final class DynamicLoadingImpl static const _typeValueV1_0_0 = 'dynamic'; } -final class DynamicLoadingBundledDylibImpl - implements DynamicLoadingImpl, DynamicLoadingBundledDylib { - DynamicLoadingBundledDylibImpl._(); +final class DynamicLoadingBundledImpl + implements DynamicLoadingImpl, DynamicLoadingBundled { + DynamicLoadingBundledImpl._(); - static final DynamicLoadingBundledDylibImpl _singleton = - DynamicLoadingBundledDylibImpl._(); + static final DynamicLoadingBundledImpl _singleton = + DynamicLoadingBundledImpl._(); - factory DynamicLoadingBundledDylibImpl() => _singleton; + factory DynamicLoadingBundledImpl() => _singleton; static const _typeValueV1_0_0 = 'absolute'; static const _typeValue = 'dynamic_loading_bundle'; @@ -107,12 +111,12 @@ final class DynamicLoadingBundledDylibImpl }; } -final class DynamicLoadingSystemDylibImpl - implements DynamicLoadingImpl, DynamicLoadingSystemDylib { +final class DynamicLoadingSystemImpl + implements DynamicLoadingImpl, DynamicLoadingSystem { @override final Uri uri; - DynamicLoadingSystemDylibImpl(this.uri); + DynamicLoadingSystemImpl(this.uri); static const _typeValue = 'dynamic_loading_system'; static const _typeValueV1_0_0 = 'system'; @@ -137,7 +141,7 @@ final class DynamicLoadingSystemDylibImpl @override bool operator ==(Object other) { - if (other is! DynamicLoadingSystemDylibImpl) { + if (other is! DynamicLoadingSystemImpl) { return false; } return uri == other.uri; @@ -237,7 +241,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { this.architecture, }) { if (linkMode is DynamicLoading && - linkMode is! DynamicLoadingBundledDylib && + linkMode is! DynamicLoadingBundled && file != null) { throw ArgumentError.value( file, @@ -272,7 +276,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { final Uri? file; if (fileString != null) { file = Uri(path: fileString); - } else if ((linkMode is DynamicLoadingBundledDylibImpl || + } else if ((linkMode is DynamicLoadingBundledImpl || linkMode is StaticLinkingImpl) && yamlMap[_pathKey] != null) { // Compatibility with v1.0.0. diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index e15901795..606d775a5 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -13,14 +13,14 @@ void main() { package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), - linkMode: DynamicLoadingBundledDylib(), + linkMode: DynamicLoadingBundled(), os: OS.android, architecture: Architecture.x64, ), NativeCodeAsset( package: 'my_package', name: 'foo3', - linkMode: DynamicLoadingSystemDylib(Uri(path: 'libfoo3.so')), + linkMode: DynamicLoadingSystem(Uri(path: 'libfoo3.so')), os: OS.android, architecture: Architecture.x64, ), @@ -50,7 +50,7 @@ void main() { package: 'my_package', name: 'bla', file: Uri(path: 'path/with spaces/bla.dll'), - linkMode: DynamicLoadingBundledDylib(), + linkMode: DynamicLoadingBundled(), os: OS.windows, architecture: Architecture.x64, ), diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index a2b1e98d3..88993a72c 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -26,14 +26,14 @@ void main() { package: 'my_package', name: 'foo', file: Uri(path: 'path/to/libfoo.so'), - linkMode: DynamicLoadingBundledDylib(), + linkMode: DynamicLoadingBundled(), os: OS.android, architecture: Architecture.x64, ), NativeCodeAsset( package: 'my_package', name: 'foo2', - linkMode: DynamicLoadingSystemDylib(Uri(path: 'path/to/libfoo2.so')), + linkMode: DynamicLoadingSystem(Uri(path: 'path/to/libfoo2.so')), os: OS.android, architecture: Architecture.x64, ), diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index ce5eecf00..901932cc2 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -20,13 +20,13 @@ void main() { NativeCodeAssetImpl( id: 'package:my_package/foo', file: fooUri, - linkMode: DynamicLoadingBundledDylibImpl(), + linkMode: DynamicLoadingBundledImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, ), NativeCodeAssetImpl( id: 'package:my_package/foo3', - linkMode: DynamicLoadingSystemDylibImpl(foo3Uri), + linkMode: DynamicLoadingSystemImpl(foo3Uri), os: OSImpl.android, architecture: ArchitectureImpl.x64, ), @@ -52,7 +52,7 @@ void main() { NativeCodeAssetImpl( id: 'package:my_package/bla', file: blaUri, - linkMode: DynamicLoadingBundledDylibImpl(), + linkMode: DynamicLoadingBundledImpl(), os: OSImpl.windows, architecture: ArchitectureImpl.x64, ), diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index b180db894..3b6ea00d3 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -26,14 +26,13 @@ void main() { NativeCodeAssetImpl( id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), - linkMode: DynamicLoadingBundledDylibImpl(), + linkMode: DynamicLoadingBundledImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, ), NativeCodeAssetImpl( id: 'package:my_package/foo2', - linkMode: - DynamicLoadingSystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), + linkMode: DynamicLoadingSystemImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, ), @@ -261,14 +260,13 @@ version: 1.0.0'''), NativeCodeAssetImpl( id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), - linkMode: DynamicLoadingBundledDylibImpl(), + linkMode: DynamicLoadingBundledImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, ), NativeCodeAssetImpl( id: 'package:my_package/foo2', - linkMode: - DynamicLoadingSystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), + linkMode: DynamicLoadingSystemImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, ), @@ -290,7 +288,7 @@ version: 1.0.0'''), NativeCodeAssetImpl( id: 'package:my_package/foo', file: Uri(path: 'path/to/libfoo.so'), - linkMode: DynamicLoadingBundledDylibImpl(), + linkMode: DynamicLoadingBundledImpl(), os: OSImpl.android, architecture: ArchitectureImpl.x64, ), @@ -298,8 +296,7 @@ version: 1.0.0'''), buildOutput2.addAssets([ NativeCodeAssetImpl( id: 'package:my_package/foo2', - linkMode: - DynamicLoadingSystemDylibImpl(Uri(path: 'path/to/libfoo2.so')), + linkMode: DynamicLoadingSystemImpl(Uri(path: 'path/to/libfoo2.so')), os: OSImpl.android, architecture: ArchitectureImpl.x64, ), diff --git a/pkgs/native_assets_cli/test/model/target_test.dart b/pkgs/native_assets_cli/test/model/target_test.dart index 0ee1bc45f..ac6a34b76 100644 --- a/pkgs/native_assets_cli/test/model/target_test.dart +++ b/pkgs/native_assets_cli/test/model/target_test.dart @@ -13,8 +13,7 @@ void main() { expect(OSImpl.android.dylibFileName('foo'), 'libfoo.so'); expect(OSImpl.android.staticlibFileName('foo'), 'libfoo.a'); expect(OSImpl.windows.dylibFileName('foo'), 'foo.dll'); - expect( - OSImpl.windows.libraryFileName('foo', DynamicLoadingBundledDylibImpl()), + expect(OSImpl.windows.libraryFileName('foo', DynamicLoadingBundledImpl()), 'foo.dll'); expect(OSImpl.windows.staticlibFileName('foo'), 'foo.lib'); expect( diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index d184d75d2..1476c3bb6 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -222,7 +222,7 @@ class CBuilder implements Builder { sources: sources, includes: includes, dynamicLibrary: _type == _CBuilderType.library && - linkMode == DynamicLoadingBundledDylib() + linkMode == DynamicLoadingBundled() ? libUri : null, staticLibrary: @@ -287,7 +287,7 @@ enum _CBuilderType { LinkMode _linkMode(LinkModePreference preference) { if (preference == LinkModePreference.dynamic || preference == LinkModePreference.preferDynamic) { - return DynamicLoadingBundledDylib(); + return DynamicLoadingBundled(); } assert(preference == LinkModePreference.static || preference == LinkModePreference.preferStatic); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart index 3a05f20b4..af22594f4 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_android_test.dart @@ -46,7 +46,7 @@ void main() { /// From https://docs.flutter.dev/reference/supported-platforms. const flutterAndroidNdkVersionHighestSupported = 34; - for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { + for (final linkMode in [DynamicLoadingBundled(), StaticLinking()]) { for (final target in targets) { for (final apiLevel in [ flutterAndroidNdkVersionLowestBestEffort, @@ -92,7 +92,7 @@ void main() { test('CBuilder API levels binary difference', () async { const target = Architecture.arm64; - final linkMode = DynamicLoadingBundledDylib(); + final linkMode = DynamicLoadingBundled(); const apiLevel1 = flutterAndroidNdkVersionLowestSupported; const apiLevel2 = flutterAndroidNdkVersionHighestSupported; final tempUri = await tempDirForTest(); @@ -132,7 +132,7 @@ Future buildLib( targetOS: OS.android, targetAndroidNdkApi: androidNdkApi, buildMode: BuildMode.release, - linkModePreference: linkMode == DynamicLoadingBundledDylib() + linkModePreference: linkMode == DynamicLoadingBundled() ? LinkModePreference.dynamic : LinkModePreference.static, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart index 7fb8379a8..7cc4365e4 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_ios_test.dart @@ -36,7 +36,7 @@ void main() { const name = 'add'; - for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { + for (final linkMode in [DynamicLoadingBundled(), StaticLinking()]) { for (final targetIOSSdk in IOSSdk.values) { for (final target in targets) { if (target == Architecture.x64 && targetIOSSdk == IOSSdk.iPhoneOS) { @@ -46,7 +46,7 @@ void main() { final libName = OS.iOS.libraryFileName(name, linkMode); for (final installName in [ null, - if (linkMode == DynamicLoadingBundledDylib()) + if (linkMode == DynamicLoadingBundled()) Uri.file('@executable_path/Frameworks/$libName'), ]) { test( @@ -63,7 +63,7 @@ void main() { targetArchitecture: target, targetOS: OS.iOS, buildMode: BuildMode.release, - linkModePreference: linkMode == DynamicLoadingBundledDylib() + linkModePreference: linkMode == DynamicLoadingBundled() ? LinkModePreference.dynamic : LinkModePreference.static, targetIOSSdk: targetIOSSdk, @@ -116,7 +116,7 @@ void main() { expect(platform, contains(platformIosSimulator.toString())); } - if (linkMode == DynamicLoadingBundledDylib()) { + if (linkMode == DynamicLoadingBundled()) { final libInstallName = await runOtoolInstallName(libUri, libName); if (installName == null) { // If no install path is passed, we have an absolute path. diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart index 25a10fd14..eab2a77e4 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_linux_host_test.dart @@ -36,7 +36,7 @@ void main() { Architecture.riscv64: 'RISC-V', }; - for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { + for (final linkMode in [DynamicLoadingBundled(), StaticLinking()]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); @@ -51,7 +51,7 @@ void main() { targetArchitecture: target, targetOS: OS.linux, buildMode: BuildMode.release, - linkModePreference: linkMode == DynamicLoadingBundledDylib() + linkModePreference: linkMode == DynamicLoadingBundled() ? LinkModePreference.dynamic : LinkModePreference.static, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart index 287794a7f..971855e2f 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_macos_host_test.dart @@ -34,7 +34,7 @@ void main() { Architecture.x64: '64-bit x86-64', }; - for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { + for (final linkMode in [DynamicLoadingBundled(), StaticLinking()]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); @@ -49,7 +49,7 @@ void main() { targetArchitecture: target, targetOS: OS.macOS, buildMode: BuildMode.release, - linkModePreference: linkMode == DynamicLoadingBundledDylib() + linkModePreference: linkMode == DynamicLoadingBundled() ? LinkModePreference.dynamic : LinkModePreference.static, ); diff --git a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart index 008bf20c2..8575c7795 100644 --- a/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart +++ b/pkgs/native_toolchain_c/test/cbuilder/cbuilder_cross_windows_host_test.dart @@ -42,11 +42,11 @@ void main() { }; final dumpbinFileType = { - DynamicLoadingBundledDylib(): 'DLL', + DynamicLoadingBundled(): 'DLL', StaticLinking(): 'LIBRARY', }; - for (final linkMode in [DynamicLoadingBundledDylib(), StaticLinking()]) { + for (final linkMode in [DynamicLoadingBundled(), StaticLinking()]) { for (final target in targets) { test('CBuilder $linkMode library $target', () async { final tempUri = await tempDirForTest(); @@ -61,7 +61,7 @@ void main() { targetOS: OS.windows, targetArchitecture: target, buildMode: BuildMode.release, - linkModePreference: linkMode == DynamicLoadingBundledDylib() + linkModePreference: linkMode == DynamicLoadingBundled() ? LinkModePreference.dynamic : LinkModePreference.static, ); From 81353f01831e381ab873239a8ced2f206a3c57c1 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 14 Mar 2024 13:09:34 +0100 Subject: [PATCH 73/75] Pipe through `supportedAssetTypes` --- .../lib/src/build_runner/build_runner.dart | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index bf6fa3ca2..53e083ca3 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -46,6 +46,7 @@ class NativeAssetsBuildRunner { required bool includeParentEnvironment, PackageLayout? packageLayout, String? runPackageName, + Iterable? supportedAssetTypes, }) async { packageLayout ??= await PackageLayout.fromRootPackageRoot(workingDirectory); final packagesWithNativeAssets = @@ -98,6 +99,7 @@ class NativeAssetsBuildRunner { cCompilerConfig: cCompilerConfig, targetIOSSdk: targetIOSSdk, targetAndroidNdkApi: targetAndroidNdkApi, + supportedAssetTypes: supportedAssetTypes, ); final ( packageAssets, @@ -138,6 +140,7 @@ class NativeAssetsBuildRunner { required bool includeParentEnvironment, PackageLayout? packageLayout, String? runPackageName, + Iterable? supportedAssetTypes, }) async { packageLayout ??= await PackageLayout.fromRootPackageRoot(workingDirectory); final packagesWithNativeAssets = @@ -172,6 +175,7 @@ class NativeAssetsBuildRunner { targetOS: targetOS, linkMode: linkModePreference, buildParentDir: packageLayout.dartToolNativeAssetsBuilder, + supportedAssetTypes: supportedAssetTypes, ); final (packageAssets, _, _, packageSuccess) = await _buildPackage( config, @@ -349,6 +353,7 @@ build_output.yaml contained a format error. required Uri buildParentDir, CCompilerConfigImpl? cCompilerConfig, DependencyMetadata? dependencyMetadata, + Iterable? supportedAssetTypes, }) async { final buildDirName = BuildConfigImpl.checksum( packageName: packageName, @@ -361,6 +366,7 @@ build_output.yaml contained a format error. cCompiler: cCompilerConfig, dependencyMetadata: dependencyMetadata, targetAndroidNdkApi: targetAndroidNdkApi, + supportedAssetTypes: supportedAssetTypes, ); final outDirUri = buildParentDir.resolve('$buildDirName/out/'); final outDir = Directory.fromUri(outDirUri); @@ -389,6 +395,7 @@ build_output.yaml contained a format error. required OSImpl targetOS, required LinkModePreferenceImpl linkMode, required Uri buildParentDir, + Iterable? supportedAssetTypes, }) async { final buildDirName = 'dry_run_${targetOS}_$linkMode'; final outDirUri = buildParentDir.resolve('$buildDirName/out/'); @@ -402,6 +409,7 @@ build_output.yaml contained a format error. packageRoot: packageRoot, targetOS: targetOS, linkModePreference: linkMode, + supportedAssetTypes: supportedAssetTypes, ); } From 7e996d33a9262100212f2428eba306bcea6d6156 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 14 Mar 2024 16:29:05 +0100 Subject: [PATCH 74/75] update example --- pkgs/native_assets_cli/lib/src/api/build.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index e21df02e3..271f3892a 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -1,6 +1,6 @@ // 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. +/// 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 'build_config.dart'; import 'build_output.dart'; @@ -58,7 +58,7 @@ import 'build_output.dart'; /// } /// /// final packageName = config.packageName; -/// final assetPath = config.outDir.resolve(assetName); +/// final assetPath = config.outputDirectory.resolve(assetName); /// final assetSourcePath = config.packageRoot.resolveUri(packageAssetPath); /// if (!config.dryRun) { /// // Insert code that downloads or builds the asset to `assetPath`. @@ -73,9 +73,10 @@ import 'build_output.dart'; /// output.addAsset( /// // TODO: Change to DataAsset once the Dart/Flutter SDK can consume it. /// NativeCodeAsset( -/// id: 'library:$packageName/asset.txt', +/// package: packageName, +/// name: 'asset.txt', /// file: assetPath, -/// linkMode: BundledDylib(), +/// linkMode: DynamicLoadingBundled(), /// os: config.targetOS, /// architecture: config.targetArchitecture, /// ), From 8dc1ef7c8f0d83cda6a4bcd431f7bc73d18b2f9b Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 14 Mar 2024 16:55:14 +0100 Subject: [PATCH 75/75] undo copyright comment change --- pkgs/native_assets_cli/lib/src/api/build.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 271f3892a..b6e805b5f 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -1,6 +1,6 @@ // 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. +// 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 'build_config.dart'; import 'build_output.dart';