Skip to content
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

[native_assets_cli] BuildConfig.linkingEnabled #1273

Merged
merged 8 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions pkgs/native_assets_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.7.2-wip
## 0.8.0-wip

- Nothing yet.
- `BuildRunner.build` and `BuildRunner.buildDryRun` now have a required
`linkingEnabled` parameter.

## 0.7.1

Expand Down
15 changes: 15 additions & 0 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,6 +54,7 @@ class NativeAssetsBuildRunner {
PackageLayout? packageLayout,
String? runPackageName,
Iterable<String>? supportedAssetTypes,
required bool linkingEnabled,
}) async =>
_run(
hook: Hook.build,
Expand All @@ -70,6 +71,7 @@ class NativeAssetsBuildRunner {
packageLayout: packageLayout,
runPackageName: runPackageName,
supportedAssetTypes: supportedAssetTypes,
linkingEnabled: linkingEnabled,
);

/// [workingDirectory] is expected to contain `.dart_tool`.
Expand Down Expand Up @@ -133,8 +135,10 @@ class NativeAssetsBuildRunner {
String? runPackageName,
Iterable<String>? supportedAssetTypes,
BuildResult? buildResult,
bool? linkingEnabled,
}) async {
assert(hook == Hook.link || buildResult == null);
assert(hook == Hook.build || linkingEnabled == null);

packageLayout ??= await PackageLayout.fromRootPackageRoot(workingDirectory);
final (buildPlan, packageGraph, planSuccess) = await _makePlan(
Expand Down Expand Up @@ -168,6 +172,7 @@ class NativeAssetsBuildRunner {
buildMode,
linkModePreference,
dependencyMetadata,
linkingEnabled,
cCompilerConfig,
targetIOSSdk,
targetAndroidNdkApi,
Expand Down Expand Up @@ -201,6 +206,7 @@ class NativeAssetsBuildRunner {
BuildModeImpl buildMode,
LinkModePreferenceImpl linkModePreference,
DependencyMetadata? dependencyMetadata,
bool? linkingEnabled,
CCompilerConfigImpl? cCompilerConfig,
IOSSdkImpl? targetIOSSdk,
int? targetAndroidNdkApi,
Expand All @@ -224,6 +230,7 @@ class NativeAssetsBuildRunner {
targetAndroidNdkApi: targetAndroidNdkApi,
supportedAssetTypes: supportedAssetTypes,
hook: hook,
linkingEnabled: linkingEnabled,
);
final buildDirUri =
packageLayout.dartToolNativeAssetsBuilder.resolve('$buildDirName/');
Expand Down Expand Up @@ -273,6 +280,7 @@ class NativeAssetsBuildRunner {
targetMacOSVersion: targetMacOSVersion,
cCompiler: cCompilerConfig,
dependencyMetadata: dependencyMetadata,
linkingEnabled: linkingEnabled,
targetAndroidNdkApi: targetAndroidNdkApi,
);
}
Expand All @@ -290,6 +298,7 @@ class NativeAssetsBuildRunner {
required OSImpl targetOS,
required Uri workingDirectory,
required bool includeParentEnvironment,
required bool linkingEnabled,
PackageLayout? packageLayout,
String? runPackageName,
Iterable<String>? supportedAssetTypes,
Expand All @@ -303,6 +312,7 @@ class NativeAssetsBuildRunner {
packageLayout: packageLayout,
runPackageName: runPackageName,
supportedAssetTypes: supportedAssetTypes,
linkingEnabled: linkingEnabled,
);

/// [workingDirectory] is expected to contain `.dart_tool`.
Expand Down Expand Up @@ -332,6 +342,7 @@ class NativeAssetsBuildRunner {
runPackageName: runPackageName,
supportedAssetTypes: supportedAssetTypes,
buildDryRunResult: buildDryRunResult,
linkingEnabled: null,
);

Future<HookResult> _runDryRun({
Expand All @@ -344,6 +355,7 @@ class NativeAssetsBuildRunner {
Iterable<String>? supportedAssetTypes,
required Hook hook,
BuildDryRunResult? buildDryRunResult,
required bool? linkingEnabled,
}) async {
packageLayout ??= await PackageLayout.fromRootPackageRoot(workingDirectory);
final (buildPlan, _, planSuccess) = await _makePlan(
Expand All @@ -367,6 +379,7 @@ class NativeAssetsBuildRunner {
supportedAssetTypes: supportedAssetTypes,
hook: hook,
buildDryRunResult: buildDryRunResult,
linkingEnabled: linkingEnabled,
);
var (buildOutput, packageSuccess) = await _runHookForPackage(
hook,
Expand Down Expand Up @@ -538,6 +551,7 @@ ${e.message}
required Hook hook,
BuildDryRunResult? buildDryRunResult,
Iterable<String>? supportedAssetTypes,
required bool? linkingEnabled,
}) async {
final hookDirName = 'dry_run_${hook.name}_${targetOS}_$linkMode';
final outDirUri = buildParentDir.resolve('$hookDirName/out/');
Expand All @@ -554,6 +568,7 @@ ${e.message}
targetOS: targetOS,
linkModePreference: linkMode,
supportedAssetTypes: supportedAssetTypes,
linkingEnabled: linkingEnabled,
);
case Hook.link:
return LinkConfigImpl.dryRun(
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_builder/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: native_assets_builder
description: >-
This package is the backend that invokes build hooks.
version: 0.7.2-wip
version: 0.8.0-wip
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_builder

publish_to: none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void main() async {
packageUri,
logger,
dartExecutable,
linkingEnabled: false,
);
final dryRunAssets = dryRunResult.assets.toList();
final result = await build(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void main() async {
packageUri,
createCapturingLogger(logMessages, level: Level.SEVERE),
dartExecutable,
linkingEnabled: false,
);
final fullLog = logMessages.join('\n');
expect(result.success, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,30 @@ void main() async {
linkModePreference: LinkModePreferenceImpl.dynamic,
workingDirectory: packageUri,
includeParentEnvironment: true,
linkingEnabled: false,
);
await buildRunner.buildDryRun(
targetOS: Target.current.os,
linkModePreference: LinkModePreferenceImpl.dynamic,
workingDirectory: packageUri,
includeParentEnvironment: true,
linkingEnabled: false,
);
await buildRunner.build(
buildMode: BuildModeImpl.release,
linkModePreference: LinkModePreferenceImpl.dynamic,
target: Target.current,
workingDirectory: packageUri,
includeParentEnvironment: true,
linkingEnabled: false,
);
await buildRunner.build(
buildMode: BuildModeImpl.release,
linkModePreference: LinkModePreferenceImpl.dynamic,
target: Target.current,
workingDirectory: packageUri,
includeParentEnvironment: true,
linkingEnabled: false,
);
});
});
Expand Down
5 changes: 5 additions & 0 deletions pkgs/native_assets_builder/test/build_runner/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Future<BuildResult> build(
int? targetMacOSVersion,
int? targetAndroidNdkApi,
Target? target,
bool linkingEnabled = false,
}) async =>
await runWithLog(capturedLogs, () async {
final result = await NativeAssetsBuildRunner(
Expand All @@ -64,6 +65,7 @@ Future<BuildResult> build(
targetIOSVersion: targetIOSVersion,
targetMacOSVersion: targetMacOSVersion,
targetAndroidNdkApi: targetAndroidNdkApi,
linkingEnabled: linkingEnabled,
);

if (result.success) {
Expand Down Expand Up @@ -155,6 +157,7 @@ Future<(BuildResult, LinkResult)> buildAndLink(
targetIOSVersion: targetIOSVersion,
targetMacOSVersion: targetMacOSVersion,
targetAndroidNdkApi: targetAndroidNdkApi,
linkingEnabled: true,
);

if (!buildResult.success) {
Expand Down Expand Up @@ -217,6 +220,7 @@ Future<BuildDryRunResult> buildDryRun(
bool includeParentEnvironment = true,
List<String>? capturedLogs,
PackageLayout? packageLayout,
required bool linkingEnabled,
}) async =>
runWithLog(capturedLogs, () async {
final result = await NativeAssetsBuildRunner(
Expand All @@ -228,6 +232,7 @@ Future<BuildDryRunResult> buildDryRun(
workingDirectory: packageUri,
includeParentEnvironment: includeParentEnvironment,
packageLayout: packageLayout,
linkingEnabled: linkingEnabled,
);
return result;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void main() async {
packageUri,
logger,
dartExecutable,
linkingEnabled: true,
);
expect(buildResult.assets.length, 0);

Expand Down Expand Up @@ -70,6 +71,7 @@ void main() async {
packageUri,
logger,
dartExecutable,
linkingEnabled: true,
);
expect(buildResult.success, true);
expect(_getNames(buildResult.assets), orderedEquals(builtHelperAssets));
Expand Down
11 changes: 11 additions & 0 deletions pkgs/native_assets_builder/test/build_runner/link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void main() async {
packageUri,
logger,
dartExecutable,
linkingEnabled: true,
);
expect(buildResult.assets.length, 0);

Expand All @@ -40,6 +41,14 @@ void main() async {
buildResult: buildResult,
);
expect(linkResult.assets.length, 2);

final buildNoLinkResult = await build(
packageUri,
logger,
dartExecutable,
linkingEnabled: false,
);
expect(buildNoLinkResult.assets.length, 4);
});
},
);
Expand Down Expand Up @@ -70,6 +79,7 @@ void main() async {
packageUri,
logger,
dartExecutable,
linkingEnabled: true,
);
expect(buildResult.success, true);
expect(_getNames(buildResult.assets), orderedEquals(builtHelperAssets));
Expand Down Expand Up @@ -106,6 +116,7 @@ void main() async {
packageUri,
logger,
dartExecutable,
linkingEnabled: true,
);
expect(buildResult.assets.length, 0);
expect(buildResult.assetsForLinking.length, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void main() async {
packageUri,
logger,
dartExecutable,
linkingEnabled: true,
);

Iterable<String> buildFiles() => Directory.fromUri(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import 'package:native_toolchain_c/native_toolchain_c.dart';

void main(List<String> arguments) async {
await build(arguments, (config, output) async {
if (!config.linkingEnabled) {
throw Exception('Link hook must be run!');
}
final logger = Logger('')
..level = Level.ALL
..onRecord.listen((record) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ void main(List<String> args) async => build(args, (config, output) async {
package: packageName,
),
),
linkInPackage: 'complex_link',
linkInPackage: config.linkingEnabled ? 'complex_link' : null,
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ void main(List<String> args) async => build(args, (config, output) async {
package: packageName,
),
),
linkInPackage: 'complex_link',
linkInPackage: config.linkingEnabled ? 'complex_link' : null,
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void main(List<String> arguments) async {
..onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');
});
final linkInPackage = config.linkingEnabled ? packageName : null;
await CBuilder.library(
name: 'add',
assetName: 'dylib_add',
Expand All @@ -27,7 +28,7 @@ void main(List<String> arguments) async {
config: config,
output: output,
logger: logger,
linkInPackage: packageName,
linkInPackage: linkInPackage,
);

await CBuilder.library(
Expand All @@ -42,7 +43,7 @@ void main(List<String> arguments) async {
config: config,
output: output,
logger: logger,
linkInPackage: packageName,
linkInPackage: linkInPackage,
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ void main(List<String> arguments) async {
file: config.packageRoot.resolve('assets/data.json'),
package: config.packageName,
),
linkInPackage: 'fail_on_os_sdk_version_linker',
linkInPackage:
config.linkingEnabled ? 'fail_on_os_sdk_version_linker' : null,
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ void main(List<String> args) async => build(args, (config, output) async {
package: packageName,
),
),
linkInPackage: 'simple_link',
linkInPackage: config.linkingEnabled ? 'simple_link' : null,
);
});
4 changes: 2 additions & 2 deletions pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 0.6.2-wip
## 0.7.0-wip

- Nothing yet.
- `BuildConfig` constructors now have a required `linkingEnabled` parameter.

## 0.6.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ void main(List<String> args) async {
file: config.packageRoot.resolve('assets/used_asset.json'),
)
];
output.addAssets(allAssets, linkInPackage: packageName);
output.addAssets(
allAssets,
linkInPackage: config.linkingEnabled ? packageName : null,
);
});
}
Loading