OLD | NEW |
1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
2 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'batch_util.dart'; | 9 import 'batch_util.dart'; |
10 | 10 |
11 import 'package:args/args.dart'; | 11 import 'package:args/args.dart'; |
12 import 'package:kernel/analyzer/loader.dart'; | 12 import 'package:kernel/analyzer/loader.dart'; |
| 13 import 'package:kernel/application_root.dart'; |
13 import 'package:kernel/verifier.dart'; | 14 import 'package:kernel/verifier.dart'; |
14 import 'package:kernel/kernel.dart'; | 15 import 'package:kernel/kernel.dart'; |
15 import 'package:kernel/log.dart'; | 16 import 'package:kernel/log.dart'; |
16 import 'package:kernel/target/targets.dart'; | 17 import 'package:kernel/target/targets.dart'; |
17 import 'package:path/path.dart' as path; | 18 import 'package:path/path.dart' as path; |
18 | 19 |
19 // Returns the path to the current sdk based on `Platform.resolvedExecutable`. | 20 // Returns the path to the current sdk based on `Platform.resolvedExecutable`. |
20 String currentSdk() { | 21 String currentSdk() { |
21 // The dart executable should be inside dart-sdk/bin/dart. | 22 // The dart executable should be inside dart-sdk/bin/dart. |
22 return path.dirname(path.dirname(path.absolute(Platform.resolvedExecutable))); | 23 return path.dirname(path.dirname(path.absolute(Platform.resolvedExecutable))); |
23 } | 24 } |
24 | 25 |
25 ArgParser parser = new ArgParser(allowTrailingOptions: true) | 26 ArgParser parser = new ArgParser(allowTrailingOptions: true) |
26 ..addOption('format', | 27 ..addOption('format', |
27 abbr: 'f', | 28 abbr: 'f', |
28 allowed: ['text', 'bin'], | 29 allowed: ['text', 'bin'], |
29 help: 'Output format.\n' | 30 help: 'Output format.\n' |
30 '(defaults to "text" unless output file ends with ".dill")') | 31 '(defaults to "text" unless output file ends with ".dill")') |
31 ..addOption('out', | 32 ..addOption('out', |
32 abbr: 'o', | 33 abbr: 'o', |
33 help: 'Output file.\n' | 34 help: 'Output file.\n' |
34 '(defaults to "out.dill" if format is "bin", otherwise stdout)') | 35 '(defaults to "out.dill" if format is "bin", otherwise stdout)') |
35 ..addOption('sdk', defaultsTo: currentSdk(), help: 'Path to the Dart SDK.') | 36 ..addOption('sdk', defaultsTo: currentSdk(), help: 'Path to the Dart SDK.') |
36 ..addOption('packages', | 37 ..addOption('packages', |
37 abbr: 'p', help: 'Path to the .packages file or packages folder.') | 38 abbr: 'p', help: 'Path to the .packages file or packages folder.') |
38 ..addOption('package-root', help: 'Deprecated alias for --packages') | 39 ..addOption('package-root', help: 'Deprecated alias for --packages') |
| 40 ..addOption('app-root', |
| 41 help: 'Store library paths relative to the given directory.\n' |
| 42 'If none is given, absolute paths are used.\n' |
| 43 'Application libraries not inside the application root are stored ' |
| 44 'using absolute paths') |
39 ..addOption('target', | 45 ..addOption('target', |
40 abbr: 't', | 46 abbr: 't', |
41 help: 'Tailor the IR to the given target.', | 47 help: 'Tailor the IR to the given target.', |
42 allowed: targetNames, | 48 allowed: targetNames, |
43 defaultsTo: 'vm') | 49 defaultsTo: 'vm') |
44 ..addFlag('strong', | 50 ..addFlag('strong', |
45 help: 'Load .dart files in strong mode.\n' | 51 help: 'Load .dart files in strong mode.\n' |
46 'Does not affect loading of binary files. Strong mode support is very\
n' | 52 'Does not affect loading of binary files. Strong mode support is very\
n' |
47 'unstable and not well integrated yet.') | 53 'unstable and not well integrated yet.') |
48 ..addFlag('link', abbr: 'l', help: 'Link the whole program into one file.') | 54 ..addFlag('link', abbr: 'l', help: 'Link the whole program into one file.') |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 options = parser.parse(args); | 243 options = parser.parse(args); |
238 } on FormatException catch (e) { | 244 } on FormatException catch (e) { |
239 return fail(e.message); // Don't puke stack traces. | 245 return fail(e.message); // Don't puke stack traces. |
240 } | 246 } |
241 | 247 |
242 checkIsDirectoryOrNull(options['sdk'], 'Dart SDK'); | 248 checkIsDirectoryOrNull(options['sdk'], 'Dart SDK'); |
243 | 249 |
244 String packagePath = options['packages'] ?? options['package-root']; | 250 String packagePath = options['packages'] ?? options['package-root']; |
245 checkIsFileOrDirectoryOrNull(packagePath, 'Package root or .packages'); | 251 checkIsFileOrDirectoryOrNull(packagePath, 'Package root or .packages'); |
246 | 252 |
| 253 String applicationRootOption = options['app-root']; |
| 254 checkIsDirectoryOrNull(applicationRootOption, 'Application root'); |
| 255 if (applicationRootOption != null) { |
| 256 applicationRootOption = new File(applicationRootOption).absolute.path; |
| 257 } |
| 258 var applicationRoot = new ApplicationRoot(applicationRootOption); |
| 259 |
247 // Set up logging. | 260 // Set up logging. |
248 if (options['verbose']) { | 261 if (options['verbose']) { |
249 log.onRecord.listen((LogRecord rec) { | 262 log.onRecord.listen((LogRecord rec) { |
250 stderr.writeln(rec.message); | 263 stderr.writeln(rec.message); |
251 }); | 264 }); |
252 } | 265 } |
253 | 266 |
254 if (options.rest.length != 1) { | 267 if (options.rest.length != 1) { |
255 return fail('Exactly one FILE should be given.'); | 268 return fail('Exactly one FILE should be given.'); |
256 } | 269 } |
257 | 270 |
258 var file = options.rest.single; | 271 String file = options.rest.single; |
259 | |
260 checkIsFile(file, option: 'Input file'); | 272 checkIsFile(file, option: 'Input file'); |
| 273 file = new File(file).absolute.path; |
| 274 Uri fileUri = new Uri(scheme: 'file', path: file); |
261 | 275 |
262 String format = options['format'] ?? defaultFormat(); | 276 String format = options['format'] ?? defaultFormat(); |
263 String outputFile = options['out'] ?? defaultOutput(); | 277 String outputFile = options['out'] ?? defaultOutput(); |
264 | 278 |
265 List<String> urlMapping = options['url-mapping'] as List<String>; | 279 List<String> urlMapping = options['url-mapping'] as List<String>; |
266 var customUriMappings = parseCustomUriMappings(urlMapping); | 280 var customUriMappings = parseCustomUriMappings(urlMapping); |
267 var repository = new Repository(); | 281 var repository = new Repository(); |
268 | 282 |
269 Program program; | 283 Program program; |
270 | 284 |
(...skipping 19 matching lines...) Expand all Loading... |
290 if (file.endsWith('.dill')) { | 304 if (file.endsWith('.dill')) { |
291 program = loadProgramFromBinary(file, repository); | 305 program = loadProgramFromBinary(file, repository); |
292 getLoadedFiles = () => [file]; | 306 getLoadedFiles = () => [file]; |
293 } else { | 307 } else { |
294 DartOptions dartOptions = new DartOptions( | 308 DartOptions dartOptions = new DartOptions( |
295 strongMode: target.strongMode, | 309 strongMode: target.strongMode, |
296 strongModeSdk: target.strongModeSdk, | 310 strongModeSdk: target.strongModeSdk, |
297 sdk: options['sdk'], | 311 sdk: options['sdk'], |
298 packagePath: packagePath, | 312 packagePath: packagePath, |
299 customUriMappings: customUriMappings, | 313 customUriMappings: customUriMappings, |
300 declaredVariables: declaredVariables); | 314 declaredVariables: declaredVariables, |
| 315 applicationRoot: applicationRoot); |
301 String packageDiscoveryPath = batchModeState.isBatchMode ? null : file; | 316 String packageDiscoveryPath = batchModeState.isBatchMode ? null : file; |
302 DartLoader loader = await batchModeState.batch.getLoader( | 317 DartLoader loader = await batchModeState.batch.getLoader( |
303 repository, dartOptions, | 318 repository, dartOptions, |
304 packageDiscoveryPath: packageDiscoveryPath); | 319 packageDiscoveryPath: packageDiscoveryPath); |
305 if (options['link']) { | 320 if (options['link']) { |
306 program = loader.loadProgram(file, target: target); | 321 program = loader.loadProgram(fileUri, target: target); |
307 } else { | 322 } else { |
308 var library = loader.loadLibrary(file); | 323 var library = loader.loadLibrary(fileUri); |
309 assert(library == repository.getLibrary(file)); | 324 assert(library == |
| 325 repository.getLibraryReference(applicationRoot.relativeUri(fileUri))); |
310 program = new Program(repository.libraries); | 326 program = new Program(repository.libraries); |
311 } | 327 } |
312 errors = loader.errors; | 328 errors = loader.errors; |
313 if (errors.isNotEmpty) { | 329 if (errors.isNotEmpty) { |
314 const int errorLimit = 100; | 330 const int errorLimit = 100; |
315 stderr.writeln(errors.take(errorLimit).join('\n')); | 331 stderr.writeln(errors.take(errorLimit).join('\n')); |
316 if (errors.length > errorLimit) { | 332 if (errors.length > errorLimit) { |
317 stderr | 333 stderr |
318 .writeln('[error] ${errors.length - errorLimit} errors not shown'); | 334 .writeln('[error] ${errors.length - errorLimit} errors not shown'); |
319 } | 335 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 | 391 |
376 await ioFuture; | 392 await ioFuture; |
377 | 393 |
378 if (shouldReportMetrics) { | 394 if (shouldReportMetrics) { |
379 int flushTime = watch.elapsedMilliseconds - time; | 395 int flushTime = watch.elapsedMilliseconds - time; |
380 print('writer.flush_time = $flushTime ms'); | 396 print('writer.flush_time = $flushTime ms'); |
381 } | 397 } |
382 | 398 |
383 return errors.length > 0 ? CompilerOutcome.Fail : CompilerOutcome.Ok; | 399 return errors.length > 0 ? CompilerOutcome.Fail : CompilerOutcome.Ok; |
384 } | 400 } |
OLD | NEW |