Skip to content

[native_assets_cli] Refactor API #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions pkgs/c_compiler/lib/c_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,3 @@
library;

export 'src/cbuilder/cbuilder.dart';
export 'src/native_toolchain/android_ndk.dart';
export 'src/native_toolchain/apple_clang.dart';
export 'src/native_toolchain/clang.dart';
export 'src/native_toolchain/gcc.dart';
export 'src/tool/tool.dart';
export 'src/tool/tool_instance.dart';
export 'src/tool/tool_requirement.dart';
export 'src/tool/tool_resolver.dart';
10 changes: 5 additions & 5 deletions pkgs/c_compiler/lib/src/cbuilder/cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ class CBuilder implements Builder {
final outDir = buildConfig.outDir;
final packageRoot = buildConfig.packageRoot;
await Directory.fromUri(outDir).create(recursive: true);
final packaging = buildConfig.packaging.preferredPackaging.first;
final linkMode = buildConfig.linkModePreference.preferredLinkMode.first;
final libUri =
outDir.resolve(buildConfig.target.os.libraryFileName(name, packaging));
outDir.resolve(buildConfig.target.os.libraryFileName(name, linkMode));
final exeUri =
outDir.resolve(buildConfig.target.os.executableFileName(name));
final sources = [
Expand All @@ -91,11 +91,11 @@ class CBuilder implements Builder {
logger: logger,
sources: sources,
dynamicLibrary:
_type == _CBuilderType.library && packaging == Packaging.dynamic
_type == _CBuilderType.library && linkMode == LinkMode.dynamic
? libUri
: null,
staticLibrary:
_type == _CBuilderType.library && packaging == Packaging.static
_type == _CBuilderType.library && linkMode == LinkMode.static
? libUri
: null,
executable: _type == _CBuilderType.executable ? exeUri : null,
Expand All @@ -105,7 +105,7 @@ class CBuilder implements Builder {
if (assetName != null) {
buildOutput.assets.add(Asset(
name: assetName!,
packaging: packaging,
linkMode: linkMode,
target: buildConfig.target,
path: AssetAbsolutePath(libUri),
));
Expand Down
12 changes: 6 additions & 6 deletions pkgs/c_compiler/test/cbuilder/cbuilder_cross_android_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ void main() {
Target.androidX64: 'elf64-x86-64',
};

for (final packaging in Packaging.values) {
for (final linkMode in LinkMode.values) {
for (final target in targets) {
test('Cbuilder $packaging library $target', () async {
test('Cbuilder $linkMode library $target', () async {
await inTempDir((tempUri) async {
final addCUri =
packageUri.resolve('test/cbuilder/testfiles/add/src/add.c');
Expand All @@ -45,9 +45,9 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: target,
packaging: packaging == Packaging.dynamic
? PackagingPreference.dynamic
: PackagingPreference.static,
linkModePreference: linkMode == LinkMode.dynamic
? LinkModePreference.dynamic
: LinkModePreference.static,
);
final buildOutput = BuildOutput();

Expand All @@ -63,7 +63,7 @@ void main() {
);

final libUri =
tempUri.resolve(target.os.libraryFileName(name, packaging));
tempUri.resolve(target.os.libraryFileName(name, linkMode));
if (Platform.isLinux) {
final result = await runProcess(
executable: Uri.file('readelf'),
Expand Down
12 changes: 6 additions & 6 deletions pkgs/c_compiler/test/cbuilder/cbuilder_cross_ios_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ void main() {
Target.iOSArm64: 'arm64',
};

for (final packaging in Packaging.values) {
for (final linkMode in LinkMode.values) {
for (final targetIOSSdk in IOSSdk.values) {
for (final target in targets) {
test('Cbuilder $packaging library $targetIOSSdk $target', () async {
test('Cbuilder $linkMode library $targetIOSSdk $target', () async {
await inTempDir((tempUri) async {
final addCUri =
packageUri.resolve('test/cbuilder/testfiles/add/src/add.c');
Expand All @@ -42,9 +42,9 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: target,
packaging: packaging == Packaging.dynamic
? PackagingPreference.dynamic
: PackagingPreference.static,
linkModePreference: linkMode == LinkMode.dynamic
? LinkModePreference.dynamic
: LinkModePreference.static,
targetIOSSdk: targetIOSSdk,
);
final buildOutput = BuildOutput();
Expand All @@ -61,7 +61,7 @@ void main() {
);

final libUri =
tempUri.resolve(target.os.libraryFileName(name, packaging));
tempUri.resolve(target.os.libraryFileName(name, linkMode));
final result = await runProcess(
executable: Uri.file('objdump'),
arguments: ['-t', libUri.path],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ void main() {
Target.linuxX64: 'Advanced Micro Devices X86-64',
};

for (final packaging in Packaging.values) {
for (final linkMode in LinkMode.values) {
for (final target in targets) {
test('Cbuilder $packaging library $target', () async {
test('Cbuilder $linkMode library $target', () async {
await inTempDir((tempUri) async {
final addCUri =
packageUri.resolve('test/cbuilder/testfiles/add/src/add.c');
Expand All @@ -46,9 +46,9 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: target,
packaging: packaging == Packaging.dynamic
? PackagingPreference.dynamic
: PackagingPreference.static,
linkModePreference: linkMode == LinkMode.dynamic
? LinkModePreference.dynamic
: LinkModePreference.static,
);
final buildOutput = BuildOutput();

Expand All @@ -64,7 +64,7 @@ void main() {
);

final libUri =
tempUri.resolve(target.os.libraryFileName(name, packaging));
tempUri.resolve(target.os.libraryFileName(name, linkMode));
final result = await runProcess(
executable: Uri.file('readelf'),
arguments: ['-h', libUri.path],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ void main() {
Target.macOSX64: '64-bit x86-64',
};

for (final packaging in Packaging.values) {
for (final linkMode in LinkMode.values) {
for (final target in targets) {
test('Cbuilder $packaging library $target', () async {
test('Cbuilder $linkMode library $target', () async {
await inTempDir((tempUri) async {
final addCUri =
packageUri.resolve('test/cbuilder/testfiles/add/src/add.c');
Expand All @@ -43,9 +43,9 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: target,
packaging: packaging == Packaging.dynamic
? PackagingPreference.dynamic
: PackagingPreference.static,
linkModePreference: linkMode == LinkMode.dynamic
? LinkModePreference.dynamic
: LinkModePreference.static,
);
final buildOutput = BuildOutput();

Expand All @@ -61,7 +61,7 @@ void main() {
);

final libUri =
tempUri.resolve(target.os.libraryFileName(name, packaging));
tempUri.resolve(target.os.libraryFileName(name, linkMode));
final result = await runProcess(
executable: Uri.file('objdump'),
arguments: ['-t', libUri.path],
Expand Down
5 changes: 3 additions & 2 deletions pkgs/c_compiler/test/cbuilder/cbuilder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: Target.current,
packaging: PackagingPreference.dynamic, // Ignored by executables.
linkModePreference:
LinkModePreference.dynamic, // Ignored by executables.
cc: cc,
);
final buildOutput = BuildOutput();
Expand Down Expand Up @@ -62,7 +63,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: Target.current,
packaging: PackagingPreference.dynamic,
linkModePreference: LinkModePreference.dynamic,
cc: cc,
);
final buildOutput = BuildOutput();
Expand Down
7 changes: 4 additions & 3 deletions pkgs/c_compiler/test/cbuilder/compiler_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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 'package:c_compiler/c_compiler.dart';
import 'package:c_compiler/src/cbuilder/compiler_resolver.dart';
import 'package:c_compiler/src/native_toolchain/apple_clang.dart';
import 'package:c_compiler/src/native_toolchain/clang.dart';
import 'package:c_compiler/src/tool/tool_error.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:test/test.dart';
Expand All @@ -29,7 +30,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: Target.current,
packaging: PackagingPreference.dynamic,
linkModePreference: LinkModePreference.dynamic,
ar: ar,
cc: cc,
ld: ld,
Expand All @@ -49,7 +50,7 @@ void main() {
outDir: tempUri,
packageRoot: tempUri,
target: Target.windowsX64,
packaging: PackagingPreference.dynamic,
linkModePreference: LinkModePreference.dynamic,
);
final resolver = CompilerResolver(
buildConfig: buildConfig,
Expand Down
3 changes: 2 additions & 1 deletion pkgs/c_compiler/test/native_toolchain/apple_clang_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ library;

import 'dart:io';

import 'package:c_compiler/c_compiler.dart';
import 'package:c_compiler/src/native_toolchain/apple_clang.dart';
import 'package:c_compiler/src/tool/tool_requirement.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';

Expand Down
3 changes: 2 additions & 1 deletion pkgs/c_compiler/test/tool/tool_instance_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:c_compiler/c_compiler.dart';
import 'package:c_compiler/src/tool/tool.dart';
import 'package:c_compiler/src/tool/tool_instance.dart';
import 'package:collection/collection.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';
Expand Down
6 changes: 5 additions & 1 deletion pkgs/c_compiler/test/tool/tool_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

import 'dart:io';

import 'package:c_compiler/c_compiler.dart';
import 'package:c_compiler/src/native_toolchain/apple_clang.dart';
import 'package:c_compiler/src/native_toolchain/clang.dart';
import 'package:c_compiler/src/tool/tool.dart';
import 'package:c_compiler/src/tool/tool_error.dart';
import 'package:c_compiler/src/tool/tool_instance.dart';
import 'package:c_compiler/src/tool/tool_resolver.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:test/test.dart';

Expand Down
4 changes: 2 additions & 2 deletions pkgs/native_assets_cli/lib/native_assets_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export 'src/model/build_config.dart';
export 'src/model/build_output.dart';
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/packaging.dart';
export 'src/model/packaging_preference.dart';
export 'src/model/target.dart';
26 changes: 13 additions & 13 deletions pkgs/native_assets_cli/lib/src/model/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:yaml/yaml.dart';

import '../utils/uri.dart';
import '../utils/yaml.dart';
import 'packaging.dart';
import 'link_mode.dart';
import 'target.dart';

abstract class AssetPath {
Expand Down Expand Up @@ -76,7 +76,7 @@ class AssetAbsolutePath implements AssetPath {

/// Asset is avaliable on a relative path.
///
/// If [Packaging] of an [Asset] is [Packaging.dynamic],
/// If [LinkMode] of an [Asset] is [LinkMode.dynamic],
/// `Platform.script.resolve(uri)` will be used to load the asset at runtime.
class AssetRelativePath implements AssetPath {
final Uri uri;
Expand Down Expand Up @@ -190,14 +190,14 @@ class AssetInExecutable implements AssetPath {
}

class Asset {
final Packaging packaging;
final LinkMode linkMode;
final String name;
final Target target;
final AssetPath path;

Asset({
required this.name,
required this.packaging,
required this.linkMode,
required this.target,
required this.path,
});
Expand All @@ -206,7 +206,7 @@ class Asset {
name: yamlMap[_nameKey] as String,
path: AssetPath.fromYaml(yamlMap[_pathKey] as YamlMap),
target: Target.fromString(yamlMap[_targetKey] as String),
packaging: Packaging.fromName(yamlMap[_packagingKey] as String),
linkMode: LinkMode.fromName(yamlMap[_linkModeKey] as String),
);

static List<Asset> listFromYamlString(String yaml) {
Expand All @@ -226,14 +226,14 @@ class Asset {
];

Asset copyWith({
Packaging? packaging,
LinkMode? linkMode,
String? name,
Target? target,
AssetPath? path,
}) =>
Asset(
name: name ?? this.name,
packaging: packaging ?? this.packaging,
linkMode: linkMode ?? this.linkMode,
target: target ?? this.target,
path: path ?? this.path,
);
Expand All @@ -244,17 +244,17 @@ class Asset {
return false;
}
return other.name == name &&
other.packaging == packaging &&
other.linkMode == linkMode &&
other.target == target &&
other.path == path;
}

@override
int get hashCode => Object.hash(name, packaging, target, path);
int get hashCode => Object.hash(name, linkMode, target, path);

Map<String, Object> toYaml() => {
_nameKey: name,
_packagingKey: packaging.name,
_linkModeKey: linkMode.name,
_pathKey: path.toYaml(),
_targetKey: target.toString(),
};
Expand All @@ -266,7 +266,7 @@ class Asset {
String toYamlString() => yamlEncode(toYaml());

static const _nameKey = 'name';
static const _packagingKey = 'packaging';
static const _linkModeKey = 'link_mode';
static const _pathKey = 'path';
static const _targetKey = 'target';

Expand All @@ -281,8 +281,8 @@ extension AssetIterable on Iterable<Asset> {

String toYamlString() => yamlEncode(toYaml());

Iterable<Asset> wherePackaging(Packaging packaging) =>
where((e) => e.packaging == packaging);
Iterable<Asset> whereLinkMode(LinkMode linkMode) =>
where((e) => e.linkMode == linkMode);

Map<Target, List<Asset>> get assetsPerTarget {
final result = <Target, List<Asset>>{};
Expand Down
Loading