-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcompute_platform_binaries_location.dart
131 lines (124 loc) · 4.57 KB
/
compute_platform_binaries_location.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// 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 "dart:io" show File, Platform;
import 'package:kernel/ast.dart' show Source;
import 'package:kernel/target/targets.dart';
import 'base/compiler_context.dart' show CompilerContext;
import 'base/processed_options.dart' show ProcessedOptions;
/// Returns the name of the default platform dill file name for the [target]
/// with the given [nnbdMode].
///
/// If the target doesn't have a default platform dill file for the nnbd mode,
/// [onError] is called.
String? computePlatformDillName(Target target, void Function() onError) {
switch (target.name) {
case 'dartdevc':
// DDC is always compiled against the outline so we use it here by
// default.
return 'ddc_outline.dill';
//TODO(johnniwinther): Support using the full dill.
//return 'ddc_platform.dill';
case 'dart2js':
return 'dart2js_platform.dill';
case 'dart2js_server':
return 'dart2js_server_platform.dill';
case 'vm':
// TODO(johnniwinther): Stop generating 'vm_platform.dill' and rename
// 'vm_platform_strong.dill' to 'vm_platform.dill'.
return "vm_platform_strong.dill";
case 'none':
return "vm_platform_strong.dill";
case 'wasm':
return 'dart2wasm_outline.dill';
// Coverage-ignore(suite): Not run.
case 'wasm_js_compatibility':
return 'dart2wasm_js_compatibility_outline.dill';
default:
break;
}
// Coverage-ignore-block(suite): Not run.
onError();
return null;
}
/// Computes the location of platform binaries, that is, compiled `.dill` files
/// of the platform libraries that are used to avoid recompiling those
/// libraries.
Uri computePlatformBinariesLocation({bool forceBuildDir = false}) {
String? resolvedExecutable = Platform.environment['resolvedExecutable'];
// The directory of the Dart VM executable.
Uri vmDirectory = Uri.base
.resolveUri(
new Uri.file(resolvedExecutable ?? Platform.resolvedExecutable))
.resolve(".");
if (vmDirectory.path.endsWith("/bin/")) {
// Looks like the VM is in a `/bin/` directory, so this is running from a
// built SDK.
return vmDirectory.resolve(forceBuildDir ? "../../" : "../lib/_internal/");
} else {
// We assume this is running from a build directory (for example,
// `out/ReleaseX64` or `xcodebuild/ReleaseX64`).
return vmDirectory;
}
}
/// Translates an SDK URI ("org-dartlang-sdk:///...") to a file URI.
Uri translateSdk(CompilerContext context, Uri uri) {
if (uri.isScheme("org-dartlang-sdk")) {
String path = uri.path;
if (path.startsWith("/sdk/")) {
Uri? sdkRoot = context.cachedSdkRoot;
if (sdkRoot == null) {
ProcessedOptions options = context.options;
sdkRoot = options.sdkRoot;
if (sdkRoot == null) {
sdkRoot = options.librariesSpecificationUri
// Coverage-ignore(suite): Not run.
?.resolve("../");
if (sdkRoot != null) {
// Coverage-ignore-block(suite): Not run.
if (!isExistingFile(sdkRoot.resolve("lib/libraries.json"))) {
sdkRoot = null;
}
}
}
if (sdkRoot == null) {
sdkRoot = (options.sdkSummary ?? // Coverage-ignore(suite): Not run.
computePlatformBinariesLocation())
.resolve("../../");
if (!isExistingFile(sdkRoot.resolve("lib/libraries.json"))) {
if (isExistingFile(sdkRoot.resolve("sdk/lib/libraries.json"))) {
sdkRoot = sdkRoot.resolve("sdk/");
} else {
sdkRoot = null;
}
}
}
// Coverage-ignore(suite): Not run.
sdkRoot ??= Uri.parse("org-dartlang-sdk:///sdk/");
context.cachedSdkRoot = sdkRoot;
}
Uri candidate = sdkRoot.resolve(path.substring(5));
if (isExistingFile(candidate)) {
Map<Uri, Source> uriToSource = context.uriToSource;
Source source = uriToSource[uri]!;
if (source.source.isEmpty) {
// Coverage-ignore-block(suite): Not run.
uriToSource[uri] = new Source(
source.lineStarts,
new File.fromUri(candidate).readAsBytesSync(),
source.importUri,
source.fileUri);
}
}
return candidate;
}
}
return uri;
}
bool isExistingFile(Uri uri) {
if (uri.isScheme("file")) {
return new File.fromUri(uri).existsSync();
} else {
return false;
}
}