OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino 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.md file. | |
4 | |
5 library fletchc.fletch_compiler; | |
6 | |
7 import 'dart:async' show | |
8 Future; | |
9 | |
10 import 'dart:convert' show | |
11 UTF8; | |
12 | |
13 import 'dart:io' show | |
14 File, | |
15 Link, | |
16 Platform; | |
17 | |
18 import 'package:compiler/compiler_new.dart' show | |
19 CompilerInput, | |
20 CompilerOutput, | |
21 CompilerDiagnostics; | |
22 | |
23 import 'package:compiler/src/source_file_provider.dart' show | |
24 CompilerSourceFileProvider, | |
25 FormattingDiagnosticHandler, | |
26 SourceFileProvider; | |
27 | |
28 import 'package:compiler/src/filenames.dart' show | |
29 appendSlash; | |
30 | |
31 import 'src/fletch_native_descriptor.dart' show | |
32 FletchNativeDescriptor; | |
33 | |
34 import 'src/fletch_backend.dart' show | |
35 FletchBackend; | |
36 | |
37 import 'package:compiler/src/apiimpl.dart' as apiimpl; | |
38 | |
39 import 'src/fletch_compiler_implementation.dart' show | |
40 FletchCompilerImplementation, | |
41 OutputProvider; | |
42 | |
43 import 'fletch_system.dart'; | |
44 | |
45 import 'incremental/fletchc_incremental.dart' show | |
46 IncrementalCompiler, | |
47 IncrementalMode; | |
48 | |
49 import 'src/guess_configuration.dart' show | |
50 executable, | |
51 guessFletchVm; | |
52 | |
53 const String _LIBRARY_ROOT = | |
54 const String.fromEnvironment("fletchc-library-root"); | |
55 | |
56 const String fletchDeviceType = | |
57 const String.fromEnvironment("fletch.device-type"); | |
58 const String _NATIVES_JSON = | |
59 const String.fromEnvironment("fletch-natives-json"); | |
60 | |
61 const String StringOrUri = "String or Uri"; | |
62 | |
63 class FletchCompiler { | |
64 final FletchCompilerImplementation _compiler; | |
65 | |
66 final Uri script; | |
67 | |
68 final bool verbose; | |
69 | |
70 final String platform; | |
71 | |
72 final Uri nativesJson; | |
73 | |
74 FletchCompiler._( | |
75 this._compiler, | |
76 this.script, | |
77 this.verbose, | |
78 this.platform, | |
79 this.nativesJson); | |
80 | |
81 Backdoor get backdoor => new Backdoor(this); | |
82 | |
83 factory FletchCompiler( | |
84 {CompilerInput provider, | |
85 CompilerOutput outputProvider, | |
86 CompilerDiagnostics handler, | |
87 @StringOrUri libraryRoot, | |
88 @StringOrUri packageConfig, | |
89 @StringOrUri script, | |
90 @StringOrUri fletchVm, | |
91 @StringOrUri currentDirectory, | |
92 @StringOrUri nativesJson, | |
93 List<String> options, | |
94 Map<String, dynamic> environment, | |
95 String platform, | |
96 IncrementalCompiler incrementalCompiler}) { | |
97 | |
98 Uri base = _computeValidatedUri( | |
99 currentDirectory, name: 'currentDirectory', ensureTrailingSlash: true); | |
100 if (base == null) { | |
101 base = Uri.base; | |
102 } | |
103 | |
104 if (options == null) { | |
105 options = <String>[]; | |
106 } else { | |
107 options = new List<String>.from(options); | |
108 } | |
109 | |
110 options.add("--platform-config=$platform"); | |
111 | |
112 final bool isVerbose = apiimpl.CompilerImpl.hasOption(options, '--verbose'); | |
113 | |
114 if (provider == null) { | |
115 provider = new CompilerSourceFileProvider() | |
116 ..cwd = base; | |
117 } | |
118 | |
119 if (handler == null) { | |
120 SourceFileProvider sourceFileProvider = null; | |
121 if (provider is SourceFileProvider) { | |
122 sourceFileProvider = provider; | |
123 } | |
124 handler = new FormattingDiagnosticHandler(sourceFileProvider) | |
125 ..throwOnError = false | |
126 ..verbose = isVerbose; | |
127 } | |
128 | |
129 if (outputProvider == null) { | |
130 outputProvider = new OutputProvider(); | |
131 } | |
132 | |
133 if (libraryRoot == null && _LIBRARY_ROOT != null) { | |
134 libraryRoot = executable.resolve(appendSlash(_LIBRARY_ROOT)); | |
135 } | |
136 libraryRoot = _computeValidatedUri( | |
137 libraryRoot, name: 'libraryRoot', ensureTrailingSlash: true, | |
138 base: base); | |
139 if (libraryRoot == null) { | |
140 libraryRoot = _guessLibraryRoot(platform); | |
141 if (libraryRoot == null) { | |
142 throw new StateError(""" | |
143 Unable to guess the location of the Dart SDK (libraryRoot). | |
144 Try adding command-line option '-Ddart-sdk=<location of the Dart sdk>'."""); | |
145 } | |
146 } else if (!_looksLikeLibraryRoot(libraryRoot, platform)) { | |
147 throw new ArgumentError( | |
148 "[libraryRoot]: Dart SDK library not found in '$libraryRoot'."); | |
149 } | |
150 | |
151 script = _computeValidatedUri(script, name: 'script', base: base); | |
152 | |
153 packageConfig = _computeValidatedUri( | |
154 packageConfig, name: 'packageConfig', base: base); | |
155 if (packageConfig == null) { | |
156 if (script != null) { | |
157 packageConfig = script.resolve('.packages'); | |
158 } else { | |
159 packageConfig = base.resolve('.packages'); | |
160 } | |
161 } | |
162 | |
163 fletchVm = guessFletchVm( | |
164 _computeValidatedUri(fletchVm, name: 'fletchVm', base: base)); | |
165 | |
166 if (environment == null) { | |
167 environment = <String, dynamic>{}; | |
168 } | |
169 | |
170 if (nativesJson == null && _NATIVES_JSON != null) { | |
171 nativesJson = base.resolve(_NATIVES_JSON); | |
172 } | |
173 nativesJson = _computeValidatedUri( | |
174 nativesJson, name: 'nativesJson', base: base); | |
175 | |
176 if (nativesJson == null) { | |
177 nativesJson = _guessNativesJson(); | |
178 if (nativesJson == null) { | |
179 throw new StateError( | |
180 """ | |
181 Unable to guess the location of the 'natives.json' file (nativesJson). | |
182 Try adding command-line option '-Dfletch-natives-json=<path to natives.json>.""" | |
183 ); | |
184 } | |
185 } else if (!_looksLikeNativesJson(nativesJson)) { | |
186 throw new ArgumentError( | |
187 "[nativesJson]: natives.json not found in '$nativesJson'."); | |
188 } | |
189 | |
190 FletchCompilerImplementation compiler = new FletchCompilerImplementation( | |
191 provider, | |
192 outputProvider, | |
193 handler, | |
194 libraryRoot, | |
195 packageConfig, | |
196 nativesJson, | |
197 options, | |
198 environment, | |
199 fletchVm, | |
200 incrementalCompiler); | |
201 | |
202 compiler.log("Using library root: $libraryRoot"); | |
203 compiler.log("Using package config: $packageConfig"); | |
204 | |
205 var helper = new FletchCompiler._( | |
206 compiler, script, isVerbose, platform, nativesJson); | |
207 compiler.helper = helper; | |
208 return helper; | |
209 } | |
210 | |
211 Future<FletchDelta> run([@StringOrUri script]) async { | |
212 // TODO(ahe): Need a base argument. | |
213 script = _computeValidatedUri(script, name: 'script'); | |
214 if (script == null) { | |
215 script = this.script; | |
216 } | |
217 if (script == null) { | |
218 throw new StateError("No [script] provided."); | |
219 } | |
220 await _inititalizeContext(); | |
221 FletchBackend backend = _compiler.backend; | |
222 return _compiler.run(script).then((_) => backend.computeDelta()); | |
223 } | |
224 | |
225 Future _inititalizeContext() async { | |
226 var data = await _compiler.callUserProvider(nativesJson); | |
227 if (data is! String) { | |
228 if (data.last == 0) { | |
229 data = data.sublist(0, data.length - 1); | |
230 } | |
231 data = UTF8.decode(data); | |
232 } | |
233 Map<String, FletchNativeDescriptor> natives = | |
234 <String, FletchNativeDescriptor>{}; | |
235 Map<String, String> names = <String, String>{}; | |
236 FletchNativeDescriptor.decode(data, natives, names); | |
237 _compiler.context.nativeDescriptors = natives; | |
238 _compiler.context.setNames(names); | |
239 } | |
240 | |
241 Uri get fletchVm => _compiler.fletchVm; | |
242 | |
243 /// Create a new instance of [IncrementalCompiler]. | |
244 IncrementalCompiler newIncrementalCompiler( | |
245 IncrementalMode support, | |
246 {List<String> options: const <String>[]}) { | |
247 return new IncrementalCompiler( | |
248 libraryRoot: _compiler.libraryRoot, | |
249 packageConfig: _compiler.packageConfig, | |
250 fletchVm: _compiler.fletchVm, | |
251 nativesJson: _compiler.nativesJson, | |
252 inputProvider: _compiler.provider, | |
253 diagnosticHandler: _compiler.handler, | |
254 options: options, | |
255 outputProvider: _compiler.userOutputProvider, | |
256 environment: _compiler.environment, | |
257 support: support, | |
258 platform: platform); | |
259 } | |
260 } | |
261 | |
262 // Backdoor around Dart privacy. For now, certain components (in particular | |
263 // incremental compilation) need access to implementation details that shouldn't | |
264 // be part of the API of this file. | |
265 // TODO(ahe): Delete this class. | |
266 class Backdoor { | |
267 final FletchCompiler _compiler; | |
268 | |
269 Backdoor(this._compiler); | |
270 | |
271 Future<FletchCompilerImplementation> get compilerImplementation async { | |
272 await _compiler._inititalizeContext(); | |
273 return _compiler._compiler; | |
274 } | |
275 } | |
276 | |
277 /// Resolves any symbolic links in [uri] if its scheme is "file". Otherwise | |
278 /// return the given [uri]. | |
279 Uri _resolveSymbolicLinks(Uri uri) { | |
280 if (uri.scheme != 'file') return uri; | |
281 File apparentLocation = new File.fromUri(uri); | |
282 String realLocation = apparentLocation.resolveSymbolicLinksSync(); | |
283 if (uri.path.endsWith("/")) { | |
284 realLocation = appendSlash(realLocation); | |
285 } | |
286 return new Uri.file(realLocation); | |
287 } | |
288 | |
289 bool _containsFile(Uri uri, String expectedFile) { | |
290 if (uri.scheme != 'file') return true; | |
291 return new File.fromUri(uri.resolve(expectedFile)).existsSync(); | |
292 } | |
293 | |
294 bool _looksLikeLibraryRoot(Uri uri, String platform) { | |
295 return _containsFile(uri, platform); | |
296 } | |
297 | |
298 Uri _computeValidatedUri( | |
299 @StringOrUri stringOrUri, | |
300 {String name, | |
301 bool ensureTrailingSlash: false, | |
302 Uri base}) { | |
303 if (base == null) { | |
304 base = Uri.base; | |
305 } | |
306 assert(name != null); | |
307 if (stringOrUri == null) { | |
308 return null; | |
309 } else if (stringOrUri is String) { | |
310 if (ensureTrailingSlash) { | |
311 stringOrUri = appendSlash(stringOrUri); | |
312 } | |
313 return base.resolve(stringOrUri); | |
314 } else if (stringOrUri is Uri) { | |
315 return base.resolveUri(stringOrUri); | |
316 } else { | |
317 throw new ArgumentError("[$name] should be a String or a Uri."); | |
318 } | |
319 } | |
320 | |
321 Uri _guessLibraryRoot(String platform) { | |
322 // When running from fletch, [executable] is | |
323 // ".../fletch-repo/fletch/out/$CONFIGURATION/dart", which means that the | |
324 // fletch root is the lib directory in the 2th parent directory (due to | |
325 // how URI resolution works, the filename ("dart") is removed before | |
326 // resolving, for example, | |
327 // ".../fletch-repo/fletch/out/$CONFIGURATION/../../" becomes | |
328 // ".../fletch-repo/fletch/"). | |
329 Uri guess = executable.resolve('../../lib/'); | |
330 if (_looksLikeLibraryRoot(guess, platform)) return guess; | |
331 return null; | |
332 } | |
333 | |
334 bool _looksLikeNativesJson(Uri uri) { | |
335 return new File.fromUri(uri).existsSync(); | |
336 } | |
337 | |
338 Uri _guessNativesJson() { | |
339 Uri uri = executable.resolve('natives.json'); | |
340 return _looksLikeNativesJson(uri) ? uri : null; | |
341 } | |
OLD | NEW |