Skip to content

Commit bb8df07

Browse files
sstricklCommit Queue
authored andcommitted
[dart2native] Use Sdk for paths to host artifacts.
This allows `dart compile` to be used from the build root without a fully built SDK. Abstract out executable and dill retrieval in Sdk getters similar to how snapshot retrieval was abstracted out. Remove the hostDartAotRuntime parameter to generateKernelHelper, since all callers always passed the same variable from generate.dart, and instead retrieve it using Sdk.dartAotRuntime. Remove all the top-level variables that contained paths in dart2native.dart and generate.dart and replace their uses with the appropriate Sdk getter. TEST=ci Cq-Include-Trybots: luci.dart.try:pkg-linux-release-arm64-try,pkg-linux-release-try,pkg-mac-release-arm64-try,pkg-mac-release-try,pkg-win-release-arm64-try,pkg-win-release-try,dart-sdk-linux-riscv64-try,dart-sdk-linux-arm64-try,dart-sdk-linux-try,dart-sdk-mac-try,dart-sdk-win-arm64-try,dart-sdk-win-try,dart-sdk-mac-arm64-try Change-Id: I92d8110faff135263a9fdf1c395759a52dab914b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/442181 Commit-Queue: Tess Strickland <[email protected]> Reviewed-by: Daco Harkes <[email protected]>
1 parent c7bf131 commit bb8df07

File tree

6 files changed

+83
-96
lines changed

6 files changed

+83
-96
lines changed

pkg/dart2native/lib/dart2native.dart

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,10 @@ import 'dart:typed_data';
88
import 'package:code_assets/code_assets.dart' show OS;
99
import 'package:collection/collection.dart';
1010
import 'package:kernel/binary/tag.dart' show Tag;
11-
import 'package:path/path.dart' as path;
1211

1312
import 'dart2native_macho.dart' show writeAppendedMachOExecutable;
1413
import 'dart2native_pe.dart' show writeAppendedPortableExecutable;
15-
16-
final binDir = File(Platform.resolvedExecutable).parent;
17-
final executableSuffix = Platform.isWindows ? '.exe' : '';
18-
final genKernel = path.join(
19-
binDir.path,
20-
'snapshots',
21-
'gen_kernel_aot.dart.snapshot',
22-
);
23-
final genSnapshotHost = path.join(
24-
binDir.path,
25-
'utils',
26-
'gen_snapshot$executableSuffix',
27-
);
28-
final platformDill = path.join(
29-
binDir.parent.path,
30-
'lib',
31-
'_internal',
32-
'vm_platform.dill',
33-
);
34-
final productPlatformDill = path.join(
35-
binDir.parent.path,
36-
'lib',
37-
'_internal',
38-
'vm_platform_product.dill',
39-
);
14+
import 'sdk.dart';
4015

4116
// Maximum page size across all supported architectures (arm64 macOS has 16K
4217
// pages, some arm64 Linux distributions have 64K pages).
@@ -104,12 +79,11 @@ Future<ProcessResult> markExecutable(String outputFile) {
10479
return Process.run('chmod', ['+x', outputFile]);
10580
}
10681

107-
/// Generates kernel by running the provided [genKernel] path.
82+
/// Generates kernel using the host machine's kernel generator.
10883
///
10984
/// Also takes a path to the [recordedUsagesFile] JSON file, where the method
11085
/// calls to static functions annotated with `@RecordUse` will be collected.
11186
Future<ProcessResult> generateKernelHelper({
112-
required String hostDartAotRuntime,
11387
String? sourceFile,
11488
required String kernelFile,
11589
String? packages,
@@ -128,8 +102,8 @@ Future<ProcessResult> generateKernelHelper({
128102
bool product = true,
129103
}) {
130104
final args = [
131-
genKernel,
132-
'--platform=${product ? productPlatformDill : platformDill}',
105+
sdk.genKernelSnapshot,
106+
'--platform=${product ? sdk.vmPlatformProductDill : sdk.vmPlatformDill}',
133107
if (product) '-Ddart.vm.product=true',
134108
if (enableExperiment.isNotEmpty) '--enable-experiment=$enableExperiment',
135109
if (targetOS != null) '--target-os=$targetOS',
@@ -148,7 +122,7 @@ Future<ProcessResult> generateKernelHelper({
148122
...extraGenKernelOptions,
149123
if (sourceFile != null) sourceFile,
150124
];
151-
return Process.run(hostDartAotRuntime, args);
125+
return Process.run(sdk.dartAotRuntime, args);
152126
}
153127

154128
Future<ProcessResult> generateAotSnapshotHelper(

pkg/dart2native/lib/generate.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ import 'package:path/path.dart' as path;
1010
import 'dart2native.dart';
1111
import 'src/generate_utils.dart';
1212

13-
export 'dart2native.dart' show genKernel, genSnapshotHost;
14-
15-
final hostDartAotRuntime = path.join(
16-
binDir.path,
17-
'dartaotruntime$executableSuffix',
18-
);
19-
2013
/// The kinds of native executables supported by [KernelGenerator].
2114
enum Kind {
2215
aot,
@@ -215,7 +208,6 @@ class _Generator {
215208
}
216209

217210
final kernelResult = await generateKernelHelper(
218-
hostDartAotRuntime: hostDartAotRuntime,
219211
sourceFile: _sourcePath,
220212
kernelFile: _programKernelFile,
221213
packages: _packages,
@@ -316,7 +308,6 @@ class _Generator {
316308
final nativeAssetsDillFile =
317309
path.join(_tempDir.path, 'native_assets.dill');
318310
final kernelResult = await generateKernelHelper(
319-
hostDartAotRuntime: hostDartAotRuntime,
320311
kernelFile: nativeAssetsDillFile,
321312
packages: _packages,
322313
defines: _defines,
@@ -405,7 +396,6 @@ Future<void> generateKernel({
405396
packages = _normalize(packages);
406397

407398
final kernelResult = await generateKernelHelper(
408-
hostDartAotRuntime: hostDartAotRuntime,
409399
sourceFile: sourcePath,
410400
kernelFile: outputPath,
411401
packages: packages,

pkg/dart2native/lib/sdk.dart

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,51 +30,28 @@ class Sdk {
3030
// Assume that we want to use the same Dart executable that we used to spawn
3131
// DartDev. We should be able to run programs with out/ReleaseX64/dart even
3232
// if the SDK isn't completely built.
33-
String get dart {
34-
var basename = path.basename(Platform.executable);
35-
// It's possible that Platform.executable won't include the .exe extension
36-
// on Windows (e.g., launching `dart` from cmd.exe where `dart` is on the
37-
// PATH). Append .exe in this case so the `checkArtifactExists` check won't
38-
// fail.
39-
if (Platform.isWindows && !basename.endsWith('.exe')) {
40-
basename += '.exe';
41-
}
42-
return path.absolute(
43-
runFromBuildRoot
44-
? sdkPath
45-
: path.absolute(
46-
sdkPath,
47-
'bin',
48-
),
49-
basename,
50-
);
51-
}
33+
String get dart => _executablePathFor(
34+
path.basename(Platform.executable),
35+
);
5236

53-
String get dartvm {
54-
final basename = Platform.isWindows ? 'dartvm.exe' : 'dartvm';
55-
return path.absolute(
56-
runFromBuildRoot
57-
? sdkPath
58-
: path.absolute(
59-
sdkPath,
60-
'bin',
61-
),
62-
basename,
63-
);
64-
}
37+
String get dartvm => _executablePathFor(
38+
'dartvm',
39+
);
6540

66-
String get dartAotRuntime => runFromBuildRoot
67-
? path.absolute(
68-
sdkPath,
69-
Platform.isWindows
70-
? 'dartaotruntime_product.exe'
71-
: 'dartaotruntime_product',
72-
)
73-
: path.absolute(
74-
sdkPath,
75-
'bin',
76-
Platform.isWindows ? 'dartaotruntime.exe' : 'dartaotruntime',
77-
);
41+
String get dartAotRuntime => _executablePathFor(
42+
'dartaotruntime',
43+
forceProductInBuildRoot: true,
44+
);
45+
46+
String get genSnapshot => _executablePathFor(
47+
'gen_snapshot',
48+
forceProductInBuildRoot: true,
49+
sdkRelativePath: 'utils',
50+
);
51+
52+
String get genKernelSnapshot => _snapshotPathFor(
53+
'gen_kernel_aot.dart.snapshot',
54+
);
7855

7956
String get analysisServerAotSnapshot => _snapshotPathFor(
8057
'analysis_server_aot.dart.snapshot',
@@ -150,10 +127,52 @@ class Sdk {
150127
// non-SDK build targets.
151128
String get librariesJson => path.absolute(sdkPath, 'lib', 'libraries.json');
152129

153-
// This file is only generated when building the SDK and isn't generated for
154-
// non-SDK build targets.
155-
String get wasmPlatformDill =>
156-
path.absolute(sdkPath, 'lib', '_internal', 'dart2wasm_platform.dill');
130+
String get vmPlatformDill => _dillPathFor(
131+
'vm_platform.dill',
132+
);
133+
134+
String get vmPlatformProductDill => _dillPathFor(
135+
'vm_platform_product.dill',
136+
);
137+
138+
String get wasmPlatformDill => _dillPathFor(
139+
'dart2wasm_platform.dill',
140+
);
141+
142+
String _dillPathFor(String dillName) => path.absolute(
143+
runFromBuildRoot
144+
? sdkPath
145+
: path.join(
146+
sdkPath,
147+
'lib',
148+
'_internal',
149+
),
150+
dillName);
151+
152+
String _executablePathFor(String executableName,
153+
{bool forceProductInBuildRoot = false, String? sdkRelativePath}) {
154+
if (Platform.isWindows && executableName.endsWith('.exe')) {
155+
// Don't modify the executable name on Windows if it already includes
156+
// the extension.
157+
assert(!forceProductInBuildRoot);
158+
} else {
159+
if (runFromBuildRoot && forceProductInBuildRoot) {
160+
executableName = '${executableName}_product';
161+
}
162+
if (Platform.isWindows) {
163+
executableName = '$executableName.exe';
164+
}
165+
}
166+
return path.absolute(
167+
runFromBuildRoot
168+
? sdkPath
169+
: path.absolute(
170+
sdkPath,
171+
'bin',
172+
sdkRelativePath,
173+
),
174+
executableName);
175+
}
157176

158177
String _snapshotPathFor(String snapshotName) => path.absolute(
159178
runFromBuildRoot

pkg/dartdev/lib/src/commands/build.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ then that is used instead.''',
115115

116116
@override
117117
Future<int> run() async {
118-
if (!checkArtifactExists(genKernel) ||
119-
!checkArtifactExists(genSnapshotHost) ||
118+
if (!checkArtifactExists(sdk.genKernelSnapshot) ||
119+
!checkArtifactExists(sdk.genSnapshot) ||
120+
!checkArtifactExists(sdk.dartAotRuntime) ||
120121
!checkArtifactExists(sdk.dart)) {
121122
return 255;
122123
}
@@ -199,8 +200,8 @@ See documentation on https://dart.dev/interop/c-interop#native-assets.
199200
recordedUsagesPath = path.join(tempDir.path, 'recorded_usages.json');
200201
}
201202
final generator = KernelGenerator(
202-
genSnapshot: genSnapshotHost,
203-
targetDartAotRuntime: hostDartAotRuntime,
203+
genSnapshot: sdk.genSnapshot,
204+
targetDartAotRuntime: sdk.dartAotRuntime,
204205
kind: Kind.exe,
205206
sourceFile: sourceUri.toFilePath(),
206207
outputFile: outputExeUri.toFilePath(),

pkg/dartdev/lib/src/commands/compile.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,10 @@ Remove debugging information from the output and save it separately to the speci
553553
"'dart compile $commandName' is not supported on x86 architectures.\n");
554554
return 64;
555555
}
556-
if (!checkArtifactExists(genKernel)) {
556+
// Kernel is always generated using the host's dartaotruntime and
557+
// gen_kernel_aot.dart.snapshot, even during cross compilation.
558+
if (!checkArtifactExists(sdk.genKernelSnapshot) ||
559+
!checkArtifactExists(sdk.dartAotRuntime)) {
557560
return 255;
558561
}
559562
final args = argResults!;
@@ -574,8 +577,8 @@ Remove debugging information from the output and save it separately to the speci
574577
return compileErrorExitCode;
575578
}
576579

577-
var genSnapshotBinary = genSnapshotHost;
578-
var dartAotRuntimeBinary = hostDartAotRuntime;
580+
var genSnapshotBinary = sdk.genSnapshot;
581+
var dartAotRuntimeBinary = sdk.dartAotRuntime;
579582

580583
final target = crossCompilationTarget(args);
581584

runtime/tests/vm/dart/run_appended_aot_snapshot_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'dart:async';
66
import 'dart:io';
77

8-
import 'package:dart2native/dart2native.dart' hide platformDill, genSnapshot;
8+
import 'package:dart2native/dart2native.dart';
99
import 'package:path/path.dart' as path;
1010
import 'package:expect/expect.dart';
1111
import 'package:code_assets/code_assets.dart' show OS;

0 commit comments

Comments
 (0)