Skip to content

Commit

Permalink
Unify {OS,Architecture,...} and {OS,Architecture,...}Impl classes tha…
Browse files Browse the repository at this point in the history
…t are effectively simple enums or data classes (#1604)

The API of `package:native_assets_cli` uses currently a
one-size-fits-all approach:

* almost every API class has two versions a `<Foo>` and a `<Foo>Impl`
* `package:native_assets_cli/native_assets_cli.dart` exposes `<Foo>`
* `package:native_assets_cli/native_assets_cli_internal.dart` exposes `<Foo>Impl`

This has a number of issues

* The `_internal.dart` is actually a public API: It's used e.g. by
  flutter tools and breaking changes to it need major version bumps.

* A user of the `<Foo>` API doesn't know that such objects are for
  example
    * hashable (e.g. go-to-definition to `<Foo>` there's no
      indication of that), e.g. `DynamicLoadingSystem` makes no mention
      of it, no `get hashCode` override, ... only in
      `DynamicLoadingSystemImpl` there is.
    * have a `toString()` that code can rely on the returned string not
      changing
    * ...

* It causes code to have to choose between e.g. `OS` and `OSImpl` -
  which is very confusing as it's just one concept.

* It causes code to have to downcast e.g. `os as OSImpl`.
  => This is a very bad smell, as generally it's an unsafe operation to
  downcast.

* It's code duplication across the api/ and model/ folders, make it
  harder to navigate in codebase (as one may have to constantly switch
  between the files), ...

One original intention for this separation may have been to hide all traces
of the json protocol in the non-internal version, but I think this is not
needed at all, because the protocol itself will be *more* stable than the APIs
offered by the package itself (much easier to break the Dart API and publish and
new version of the package than to break the CLI protocol).

=> So this PR starts to merge some `<Foo>` and `<Foo>Impl` classes into
one `<Foo>`.
=> This simplifies the code, removes `as ...Impl` downcasts, etc

One extra thing this CL does is remove some members from e.g. `OS`. This
is part of disentangling the concepts. The build protocol may always
expose the target operating system, but it may only expose the target
ABI if the building code supports code assets.

=> So one should be able to use `OS` without `OS.dylibFileName` /
`OS.architectures` / ...

=> We may even decide to move code asset related code into it's own
package, making the core build protocol package not need the concept of
`Architecture` at all.
  • Loading branch information
mkustermann authored Sep 27, 2024
1 parent 5d79761 commit 1aae9da
Show file tree
Hide file tree
Showing 57 changed files with 1,130 additions and 1,375 deletions.
1 change: 1 addition & 0 deletions pkgs/native_assets_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
dart-lang/native repository to make it clear those are not intended to be used
by end-users.
- Remove link-dry-run concept as it's unused by Flutter Tools & Dart SDK
- Use unified classes instead of two `{OS,...}` and `{OS,,...}Impl`

## 0.8.3

Expand Down
97 changes: 66 additions & 31 deletions pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ class NativeAssetsBuildRunner {
/// [api.BuildConfig] and [api.LinkConfig]! For more info see:
/// https://github.com/dart-lang/native/issues/1319
Future<BuildResult> build({
required LinkModePreferenceImpl linkModePreference,
required LinkModePreference linkModePreference,
required Target target,
required Uri workingDirectory,
required BuildModeImpl buildMode,
CCompilerConfigImpl? cCompilerConfig,
IOSSdkImpl? targetIOSSdk,
required BuildMode buildMode,
CCompilerConfig? cCompilerConfig,
IOSSdk? targetIOSSdk,
int? targetIOSVersion,
int? targetMacOSVersion,
int? targetAndroidNdkApi,
Expand Down Expand Up @@ -99,12 +99,12 @@ class NativeAssetsBuildRunner {
/// [api.BuildConfig] and [api.LinkConfig]! For more info see:
/// https://github.com/dart-lang/native/issues/1319
Future<LinkResult> link({
required LinkModePreferenceImpl linkModePreference,
required LinkModePreference linkModePreference,
required Target target,
required Uri workingDirectory,
required BuildModeImpl buildMode,
CCompilerConfigImpl? cCompilerConfig,
IOSSdkImpl? targetIOSSdk,
required BuildMode buildMode,
CCompilerConfig? cCompilerConfig,
IOSSdk? targetIOSSdk,
int? targetIOSVersion,
int? targetMacOSVersion,
int? targetAndroidNdkApi,
Expand Down Expand Up @@ -137,12 +137,12 @@ class NativeAssetsBuildRunner {
/// The common method for running building or linking of assets.
Future<HookResult> _run({
required Hook hook,
required LinkModePreferenceImpl linkModePreference,
required LinkModePreference linkModePreference,
required Target target,
required Uri workingDirectory,
required BuildModeImpl buildMode,
CCompilerConfigImpl? cCompilerConfig,
IOSSdkImpl? targetIOSSdk,
required BuildMode buildMode,
CCompilerConfig? cCompilerConfig,
IOSSdk? targetIOSSdk,
int? targetIOSVersion,
int? targetMacOSVersion,
int? targetAndroidNdkApi,
Expand All @@ -160,18 +160,13 @@ class NativeAssetsBuildRunner {
// Specifically for running our tests on Dart CI with the test runner, we
// recognize specific variables to setup the C Compiler configuration.
if (cCompilerConfig == null) {
String? unparseKey(String key) =>
'DART_HOOK_TESTING_${key.replaceAll('.', '__').toUpperCase()}';

final env = Platform.environment;
String? lookup(String key) => env[unparseKey(key)];

final cc = lookup(CCompilerConfigImpl.ccConfigKeyFull);
final ar = lookup(CCompilerConfigImpl.arConfigKeyFull);
final ld = lookup(CCompilerConfigImpl.ldConfigKeyFull);
final envScript = lookup(CCompilerConfigImpl.envScriptConfigKeyFull);
final cc = env['DART_HOOK_TESTING_C_COMPILER__CC'];
final ar = env['DART_HOOK_TESTING_C_COMPILER__AR'];
final ld = env['DART_HOOK_TESTING_C_COMPILER__LD'];
final envScript = env['DART_HOOK_TESTING_C_COMPILER__ENV_SCRIPT'];
final envScriptArgs =
lookup(CCompilerConfigImpl.envScriptArgsConfigKeyFull)
env['DART_HOOK_TESTING_C_COMPILER__ENV_SCRIPT_ARGUMENTS']
?.split(' ')
.map((arg) => arg.trim())
.where((arg) => arg.isNotEmpty)
Expand All @@ -184,7 +179,7 @@ class NativeAssetsBuildRunner {
ld != null ||
envScript != null ||
hasEnvScriptArgs) {
cCompilerConfig = CCompilerConfigImpl(
cCompilerConfig = CCompilerConfig(
archiver: ar != null ? Uri.file(ar) : null,
compiler: cc != null ? Uri.file(cc) : null,
envScript: envScript != null ? Uri.file(envScript) : null,
Expand Down Expand Up @@ -268,12 +263,12 @@ class NativeAssetsBuildRunner {
Package package,
PackageLayout packageLayout,
Target target,
BuildModeImpl buildMode,
LinkModePreferenceImpl linkModePreference,
BuildMode buildMode,
LinkModePreference linkModePreference,
DependencyMetadata? dependencyMetadata,
bool? linkingEnabled,
CCompilerConfigImpl? cCompilerConfig,
IOSSdkImpl? targetIOSSdk,
CCompilerConfig? cCompilerConfig,
IOSSdk? targetIOSSdk,
int? targetAndroidNdkApi,
int? targetIOSVersion,
int? targetMacOSVersion,
Expand Down Expand Up @@ -370,8 +365,8 @@ class NativeAssetsBuildRunner {
/// If provided, only native assets of all transitive dependencies of
/// [runPackageName] are built.
Future<BuildDryRunResult> buildDryRun({
required LinkModePreferenceImpl linkModePreference,
required OSImpl targetOS,
required LinkModePreference linkModePreference,
required OS targetOS,
required Uri workingDirectory,
required bool includeParentEnvironment,
required bool linkingEnabled,
Expand Down Expand Up @@ -760,8 +755,8 @@ ${compileResult.stdout}
required Package package,
required String packageName,
required Uri packageRoot,
required OSImpl targetOS,
required LinkModePreferenceImpl linkMode,
required OS targetOS,
required LinkModePreference linkMode,
required Uri buildParentDir,
Iterable<String>? supportedAssetTypes,
required bool? linkingEnabled,
Expand Down Expand Up @@ -914,3 +909,43 @@ extension on DateTime {
extension on Uri {
Uri get parent => File(toFilePath()).parent.uri;
}

extension OSArchitectures on OS {
Set<Architecture> get architectures => _osTargets[this]!;
}

const _osTargets = {
OS.android: {
Architecture.arm,
Architecture.arm64,
Architecture.ia32,
Architecture.x64,
Architecture.riscv64,
},
OS.fuchsia: {
Architecture.arm64,
Architecture.x64,
},
OS.iOS: {
Architecture.arm,
Architecture.arm64,
Architecture.x64,
},
OS.linux: {
Architecture.arm,
Architecture.arm64,
Architecture.ia32,
Architecture.riscv32,
Architecture.riscv64,
Architecture.x64,
},
OS.macOS: {
Architecture.arm64,
Architecture.x64,
},
OS.windows: {
Architecture.arm64,
Architecture.ia32,
Architecture.x64,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,29 @@ void main() async {

await buildRunner.buildDryRun(
targetOS: Target.current.os,
linkModePreference: LinkModePreferenceImpl.dynamic,
linkModePreference: LinkModePreference.dynamic,
workingDirectory: packageUri,
includeParentEnvironment: true,
linkingEnabled: false,
);
await buildRunner.buildDryRun(
targetOS: Target.current.os,
linkModePreference: LinkModePreferenceImpl.dynamic,
linkModePreference: LinkModePreference.dynamic,
workingDirectory: packageUri,
includeParentEnvironment: true,
linkingEnabled: false,
);
await buildRunner.build(
buildMode: BuildModeImpl.release,
linkModePreference: LinkModePreferenceImpl.dynamic,
buildMode: BuildMode.release,
linkModePreference: LinkModePreference.dynamic,
target: Target.current,
workingDirectory: packageUri,
includeParentEnvironment: true,
linkingEnabled: false,
);
await buildRunner.build(
buildMode: BuildModeImpl.release,
linkModePreference: LinkModePreferenceImpl.dynamic,
buildMode: BuildMode.release,
linkModePreference: LinkModePreference.dynamic,
target: Target.current,
workingDirectory: packageUri,
includeParentEnvironment: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import 'helpers.dart';
const Timeout longTimeout = Timeout(Duration(minutes: 5));

void main() async {
String unparseKey(String key) =>
'DART_HOOK_TESTING_${key.replaceAll('.', '__').toUpperCase()}';

final arKey = unparseKey(CCompilerConfigImpl.arConfigKeyFull);
final ccKey = unparseKey(CCompilerConfigImpl.ccConfigKeyFull);
final ldKey = unparseKey(CCompilerConfigImpl.ldConfigKeyFull);
final envScriptKey = unparseKey(CCompilerConfigImpl.envScriptConfigKeyFull);
final envScriptArgsKey =
unparseKey(CCompilerConfigImpl.envScriptArgsConfigKeyFull);

final cc = Platform.environment[ccKey]?.fileUri;
final env = Platform.environment;
final cc = env['DART_HOOK_TESTING_C_COMPILER__CC'];
final ar = env['DART_HOOK_TESTING_C_COMPILER__AR'];
final ld = env['DART_HOOK_TESTING_C_COMPILER__LD'];
final envScript = env['DART_HOOK_TESTING_C_COMPILER__ENV_SCRIPT'];
final envScriptArgs =
env['DART_HOOK_TESTING_C_COMPILER__ENV_SCRIPT_ARGUMENTS']
?.split(' ')
.map((arg) => arg.trim())
.where((arg) => arg.isNotEmpty)
.toList();

if (cc == null) {
// We don't set any compiler paths on the GitHub CI.
Expand All @@ -45,21 +45,19 @@ void main() async {

await runPubGet(workingDirectory: packageUri, logger: logger);

printOnFailure(
'Platform.environment[ccKey]: ${Platform.environment[ccKey]}');
printOnFailure('cc: $cc');

final result = await build(
packageUri,
logger,
dartExecutable,
// Manually pass in a compiler.
cCompilerConfig: CCompilerConfigImpl(
archiver: Platform.environment[arKey]?.fileUri,
compiler: cc,
envScript: Platform.environment[envScriptKey]?.fileUri,
envScriptArgs: Platform.environment[envScriptArgsKey]?.split(' '),
linker: Platform.environment[ldKey]?.fileUri,
cCompilerConfig: CCompilerConfig(
archiver: ar?.fileUri,
compiler: cc.fileUri,
envScript: envScript?.fileUri,
envScriptArgs: envScriptArgs,
linker: ld?.fileUri,
),
// Prevent any other environment variables.
includeParentEnvironment: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'package:logging/logging.dart';
import 'package:native_assets_builder/native_assets_builder.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:native_assets_cli/native_assets_cli_internal.dart';

import '../helpers.dart';
Expand All @@ -22,8 +21,8 @@ void main(List<String> args) async {
logger: logger,
dartExecutable: dartExecutable,
).build(
buildMode: BuildModeImpl.release,
linkModePreference: LinkModePreferenceImpl.dynamic,
buildMode: BuildMode.release,
linkModePreference: LinkModePreference.dynamic,
target: target,
workingDirectory: packageUri,
includeParentEnvironment: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ void main(List<String> args) async {
dartExecutable: dartExecutable,
singleHookTimeout: timeout,
).build(
buildMode: BuildModeImpl.release,
linkModePreference: LinkModePreferenceImpl.dynamic,
buildMode: BuildMode.release,
linkModePreference: LinkModePreference.dynamic,
target: Target.current,
workingDirectory: packageUri,
includeParentEnvironment: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart' show OS;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
show IOSSdkImpl, Target;
show IOSSdk, Target;
import 'package:test/test.dart';

import '../helpers.dart';
Expand Down Expand Up @@ -69,8 +69,7 @@ void main() async {
final logMessages = <String>[];
final (buildResult, linkResult) = await buildAndLink(
target: target,
targetIOSSdk:
(target.os == OS.iOS) ? IOSSdkImpl.iPhoneOS : null,
targetIOSSdk: (target.os == OS.iOS) ? IOSSdk.iPhoneOS : null,
targetIOSVersion: (target.os == OS.iOS) ? version : null,
targetMacOSVersion: (target.os == OS.macOS) ? version : null,
targetAndroidNdkApi: (target.os == OS.android) ? version : null,
Expand Down
Loading

0 comments on commit 1aae9da

Please sign in to comment.