Skip to content

Commit 63082be

Browse files
sstricklCommit Queue
authored andcommitted
[dartdev] Move sdk.dart from dartdev to dart2native.
Leave a stub in its place that re-exports package:dart2native/sdk.dart and an implementation of checkArtifactExists, which used to be part of the Sdk class, to limit the amount of changes needed. This move is made so that followup CLs can use the Sdk class from dart2native without causing a cyclic dependency. 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: I492084031efce1e6747f93f2067249425fe9b922 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/442220 Commit-Queue: Tess Strickland <[email protected]> Reviewed-by: Ben Konyi <[email protected]>
1 parent 644d0d4 commit 63082be

File tree

9 files changed

+289
-287
lines changed

9 files changed

+289
-287
lines changed

pkg/dart2native/lib/sdk.dart

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:path/path.dart' as path;
8+
9+
final Sdk sdk = Sdk._instance;
10+
11+
/// A utility class for finding and referencing paths within the Dart SDK.
12+
class Sdk {
13+
static final Sdk _instance = _createSingleton();
14+
15+
/// Path to SDK directory.
16+
final String sdkPath;
17+
18+
/// The SDK's semantic versioning version (x.y.z-a.b.channel).
19+
final String version;
20+
21+
/// The SDK's git revision, if known.
22+
final String? revision;
23+
24+
final bool runFromBuildRoot;
25+
26+
factory Sdk() => _instance;
27+
28+
Sdk._(this.sdkPath, this.version, this.revision, this.runFromBuildRoot);
29+
30+
// Assume that we want to use the same Dart executable that we used to spawn
31+
// DartDev. We should be able to run programs with out/ReleaseX64/dart even
32+
// 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+
}
52+
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+
}
65+
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+
);
78+
79+
String get analysisServerAotSnapshot => _snapshotPathFor(
80+
'analysis_server_aot.dart.snapshot',
81+
);
82+
83+
String get analysisServerSnapshot => _snapshotPathFor(
84+
'analysis_server.dart.snapshot',
85+
);
86+
87+
String get ddcAotSnapshot => runFromBuildRoot
88+
? _snapshotPathFor(
89+
'dartdevc_aot_product.dart.snapshot',
90+
)
91+
: _snapshotPathFor(
92+
'dartdevc_aot.dart.snapshot',
93+
);
94+
95+
String get dart2jsAotSnapshot => runFromBuildRoot
96+
? _snapshotPathFor(
97+
'dart2js_aot_product.dart.snapshot',
98+
)
99+
: _snapshotPathFor(
100+
'dart2js_aot.dart.snapshot',
101+
);
102+
103+
String get dart2wasmSnapshot => _snapshotPathFor(
104+
'dart2wasm_product.snapshot',
105+
);
106+
107+
String get dartMCPServerAotSnapshot => _snapshotPathFor(
108+
'dart_mcp_server_aot.dart.snapshot',
109+
);
110+
111+
String get ddsAotSnapshot => _snapshotPathFor(
112+
'dds_aot.dart.snapshot',
113+
);
114+
115+
String get frontendServerAotSnapshot => runFromBuildRoot
116+
? _snapshotPathFor(
117+
'frontend_server_aot_product.dart.snapshot',
118+
)
119+
: _snapshotPathFor(
120+
'frontend_server_aot.dart.snapshot',
121+
);
122+
123+
String get dtdAotSnapshot => _snapshotPathFor(
124+
'dart_tooling_daemon_aot.dart.snapshot',
125+
);
126+
127+
String get devToolsBinaries => path.absolute(
128+
runFromBuildRoot
129+
? sdkPath
130+
: path.absolute(
131+
sdkPath,
132+
'bin',
133+
'resources',
134+
),
135+
'devtools',
136+
);
137+
138+
String get wasmOpt => path.absolute(
139+
runFromBuildRoot
140+
? sdkPath
141+
: path.absolute(
142+
sdkPath,
143+
'bin',
144+
'utils',
145+
),
146+
Platform.isWindows ? 'wasm-opt.exe' : 'wasm-opt',
147+
);
148+
149+
// This file is only generated when building the SDK and isn't generated for
150+
// non-SDK build targets.
151+
String get librariesJson => path.absolute(sdkPath, 'lib', 'libraries.json');
152+
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');
157+
158+
String _snapshotPathFor(String snapshotName) => path.absolute(
159+
runFromBuildRoot
160+
? sdkPath
161+
: path.absolute(
162+
sdkPath,
163+
'bin',
164+
'snapshots',
165+
),
166+
snapshotName,
167+
);
168+
169+
static Sdk _createSingleton() {
170+
// Find SDK path.
171+
(String, bool)? trySDKPath(String executablePath) {
172+
// The common case, and how cli_util.dart computes the Dart SDK directory,
173+
// [path.dirname] called twice on Platform.executable. We confirm by
174+
// asserting that the directory `./bin/snapshots/` exists in this directory:
175+
var sdkPath = path.absolute(path.dirname(path.dirname(executablePath)));
176+
var snapshotsDir = path.join(sdkPath, 'bin', 'snapshots');
177+
var runFromBuildRoot = false;
178+
final type = FileSystemEntity.typeSync(snapshotsDir);
179+
if (type != FileSystemEntityType.directory &&
180+
type != FileSystemEntityType.link) {
181+
// This is the less common case where the user is in
182+
// the checked out Dart SDK, and is executing `dart` via:
183+
// ./out/ReleaseX64/dart ... or in google3.
184+
sdkPath = path.absolute(path.dirname(executablePath));
185+
snapshotsDir = sdkPath;
186+
runFromBuildRoot = true;
187+
}
188+
189+
// Try to locate the DartDev snapshot to determine if we're able to find
190+
// the SDK snapshots with this SDK path. This is meant to handle
191+
// non-standard SDK layouts that can involve symlinks (e.g., Brew
192+
// installations, google3 tests, etc).
193+
final snapshot = path.join(snapshotsDir, 'dartdev_aot.dart.snapshot');
194+
if (FileSystemEntity.typeSync(snapshot) ==
195+
FileSystemEntityType.notFound) {
196+
return null;
197+
}
198+
return (sdkPath, runFromBuildRoot);
199+
}
200+
201+
final (sdkPath, runFromBuildRoot) =
202+
trySDKPath(Platform.resolvedExecutable) ??
203+
trySDKPath(Platform.executable)!;
204+
205+
// Defer to [Runtime] for the version.
206+
final version = Runtime.runtime.version;
207+
208+
return Sdk._(sdkPath, version, getRevision(sdkPath), runFromBuildRoot);
209+
}
210+
211+
/// Reads the contents of `revision` file at SDK root.
212+
///
213+
/// Returns `null` if the file does not exist.
214+
static String? getRevision(String sdkPath) {
215+
String? revision;
216+
final revisionFile = File(path.join(sdkPath, 'revision'));
217+
if (revisionFile.existsSync()) {
218+
revision = revisionFile.readAsStringSync().trim();
219+
}
220+
return revision;
221+
}
222+
}
223+
224+
/// Information about the current runtime.
225+
class Runtime {
226+
static Runtime runtime = _createSingleton();
227+
228+
/// The SDK's semantic versioning version (x.y.z-a.b.channel).
229+
final String version;
230+
231+
/// The SDK's release channel (`main`, `dev`, `beta`, `stable`).
232+
///
233+
/// May be null if [Platform.version] does not have the expected format.
234+
final String? channel;
235+
236+
Runtime._(this.version, this.channel);
237+
238+
static Runtime _createSingleton() {
239+
final versionString = Platform.version;
240+
// Expected format: "version (channel) ..."
241+
var version = versionString;
242+
String? channel;
243+
final versionEnd = versionString.indexOf(' ');
244+
if (versionEnd > 0) {
245+
version = versionString.substring(0, versionEnd);
246+
var channelEnd = versionString.indexOf(' ', versionEnd + 1);
247+
if (channelEnd < 0) channelEnd = versionString.length;
248+
if (versionString.startsWith('(', versionEnd + 1) &&
249+
versionString.startsWith(')', channelEnd - 1)) {
250+
channel = versionString.substring(versionEnd + 2, channelEnd - 1);
251+
}
252+
}
253+
return Runtime._(version, channel);
254+
}
255+
}
256+
257+
const useAotSnapshotFlag = 'use-aot-snapshot';

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

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

116116
@override
117117
Future<int> run() async {
118-
if (!Sdk.checkArtifactExists(genKernel) ||
119-
!Sdk.checkArtifactExists(genSnapshotHost) ||
120-
!Sdk.checkArtifactExists(sdk.dart)) {
118+
if (!checkArtifactExists(genKernel) ||
119+
!checkArtifactExists(genSnapshotHost) ||
120+
!checkArtifactExists(sdk.dart)) {
121121
return 255;
122122
}
123123
// AOT compilation isn't supported on ia32. Currently, generating an

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ class CompileJSCommand extends CompileSubcommandCommand {
9494

9595
@override
9696
FutureOr<int> run() async {
97-
if (!Sdk.checkArtifactExists(sdk.librariesJson)) {
97+
if (!checkArtifactExists(sdk.librariesJson, warnIfBuildRoot: true)) {
9898
return genericErrorExitCode;
9999
}
100100
final args = argResults!;
101101
var snapshot = sdk.dart2jsAotSnapshot;
102-
if (!Sdk.checkArtifactExists(snapshot, logError: false)) {
102+
if (!checkArtifactExists(snapshot, logError: false)) {
103103
log.stderr('Error: JS compilation failed');
104104
log.stderr('Unable to find $snapshot');
105105
return compileErrorExitCode;
@@ -152,12 +152,12 @@ class CompileDDCCommand extends CompileSubcommandCommand {
152152

153153
@override
154154
FutureOr<int> run() async {
155-
if (!Sdk.checkArtifactExists(sdk.librariesJson)) {
155+
if (!checkArtifactExists(sdk.librariesJson, warnIfBuildRoot: true)) {
156156
return genericErrorExitCode;
157157
}
158158
final args = argResults!;
159159
var snapshot = sdk.ddcAotSnapshot;
160-
if (!Sdk.checkArtifactExists(snapshot, logError: false)) {
160+
if (!checkArtifactExists(snapshot, logError: false)) {
161161
log.stderr('Error: JS compilation failed');
162162
log.stderr('Unable to find $snapshot');
163163
return compileErrorExitCode;
@@ -553,7 +553,7 @@ 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 (!Sdk.checkArtifactExists(genKernel)) {
556+
if (!checkArtifactExists(genKernel)) {
557557
return 255;
558558
}
559559
final args = argResults!;
@@ -896,10 +896,10 @@ class CompileWasmCommand extends CompileSubcommandCommand {
896896
final args = argResults!;
897897
final verbose = this.verbose || args.flag('verbose');
898898

899-
if (!Sdk.checkArtifactExists(sdk.wasmPlatformDill) ||
900-
!Sdk.checkArtifactExists(sdk.dartAotRuntime) ||
901-
!Sdk.checkArtifactExists(sdk.dart2wasmSnapshot) ||
902-
!Sdk.checkArtifactExists(sdk.wasmOpt)) {
899+
if (!checkArtifactExists(sdk.wasmPlatformDill, warnIfBuildRoot: true) ||
900+
!checkArtifactExists(sdk.dartAotRuntime) ||
901+
!checkArtifactExists(sdk.dart2wasmSnapshot) ||
902+
!checkArtifactExists(sdk.wasmOpt)) {
903903
return 255;
904904
}
905905

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class DevelopmentServiceCommand extends DartdevCommand {
4141
: absolute(sdkDir, 'dds_aot.dart.snapshot');
4242
final args = argResults!.arguments;
4343

44-
if (!Sdk.checkArtifactExists(snapshot, logError: false)) {
44+
if (!checkArtifactExists(snapshot, logError: false)) {
4545
log.stderr('Error: launching development server failed : '
4646
'Unable to find snapshot for the development server');
4747
return 255;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ For more information about the server's capabilities and configuration, see:
5757
var script = sdk.analysisServerAotSnapshot;
5858
var useExec = false;
5959
if (argResults!.flag(useAotSnapshotFlag)) {
60-
if (!Sdk.checkArtifactExists(sdk.analysisServerAotSnapshot)) {
60+
if (!checkArtifactExists(sdk.analysisServerAotSnapshot)) {
6161
log.stderr('Error: launching language analysis server failed');
6262
log.stderr('${sdk.analysisServerAotSnapshot} not found');
6363
return _genericErrorExitCode;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ToolingDaemonCommand extends DartdevCommand {
3535
Future<int> run() async {
3636
var snapshot = sdk.dtdAotSnapshot;
3737
final args = argResults!.arguments;
38-
if (!Sdk.checkArtifactExists(sdk.dtdAotSnapshot, logError: false)) {
38+
if (!checkArtifactExists(sdk.dtdAotSnapshot, logError: false)) {
3939
log.stderr('Error: launching dart tooling daemon failed : '
4040
'Unable to find snapshot for the tooling daemon');
4141
return 255;

pkg/dartdev/lib/src/dds_runner.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class DDSRunner {
3434
var snapshotName = fullSdk
3535
? sdk.ddsAotSnapshot
3636
: absolute(sdkDir, 'dds_aot.dart.snapshot');
37-
final isAot = Sdk.checkArtifactExists(snapshotName) ? true : false;
37+
final isAot = checkArtifactExists(snapshotName) ? true : false;
3838
if (!isAot) {
3939
printError('Unable to find snapshot for the development server');
4040
return false;

0 commit comments

Comments
 (0)