| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:front_end/compilation_message.dart'; | 7 import 'package:front_end/compilation_message.dart'; |
| 8 import 'package:front_end/compiler_options.dart'; | 8 import 'package:front_end/compiler_options.dart'; |
| 9 import 'package:front_end/file_system.dart'; | 9 import 'package:front_end/file_system.dart'; |
| 10 import 'package:front_end/src/base/performace_logger.dart'; | 10 import 'package:front_end/src/base/performace_logger.dart'; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 import 'package:kernel/target/targets.dart'; | 21 import 'package:kernel/target/targets.dart'; |
| 22 import 'package:kernel/target/vm_fasta.dart'; | 22 import 'package:kernel/target/vm_fasta.dart'; |
| 23 import 'package:package_config/packages.dart' show Packages; | 23 import 'package:package_config/packages.dart' show Packages; |
| 24 import 'package:package_config/src/packages_impl.dart' | 24 import 'package:package_config/src/packages_impl.dart' |
| 25 show NonFilePackagesDirectoryPackages, MapPackages; | 25 show NonFilePackagesDirectoryPackages, MapPackages; |
| 26 import 'package:package_config/packages_file.dart' as package_config; | 26 import 'package:package_config/packages_file.dart' as package_config; |
| 27 import 'package:source_span/source_span.dart' show SourceSpan, SourceLocation; | 27 import 'package:source_span/source_span.dart' show SourceSpan, SourceLocation; |
| 28 import 'package:front_end/src/fasta/command_line_reporting.dart' | 28 import 'package:front_end/src/fasta/command_line_reporting.dart' |
| 29 as command_line_reporting; | 29 as command_line_reporting; |
| 30 | 30 |
| 31 import 'libraries_specification.dart'; | |
| 32 | |
| 33 /// All options needed for the front end implementation. | 31 /// All options needed for the front end implementation. |
| 34 /// | 32 /// |
| 35 /// This includes: all of [CompilerOptions] in a form useful to the | 33 /// This includes: all of [CompilerOptions] in a form useful to the |
| 36 /// implementation, default values for options that were not provided, | 34 /// implementation, default values for options that were not provided, |
| 37 /// and information derived from how the compiler was invoked (like the | 35 /// and information derived from how the compiler was invoked (like the |
| 38 /// entry-points given to the compiler and whether a modular or whole-program | 36 /// entry-points given to the compiler and whether a modular or whole-program |
| 39 /// API was used). | 37 /// API was used). |
| 40 /// | 38 /// |
| 41 /// The intent is that the front end should immediately wrap any incoming | 39 /// The intent is that the front end should immediately wrap any incoming |
| 42 /// [CompilerOptions] object in this class before doing further processing, and | 40 /// [CompilerOptions] object in this class before doing further processing, and |
| (...skipping 28 matching lines...) Expand all Loading... |
| 71 /// includes inferred types. | 69 /// includes inferred types. |
| 72 List<Program> _inputSummariesPrograms; | 70 List<Program> _inputSummariesPrograms; |
| 73 | 71 |
| 74 /// Other programs that are meant to be linked and compiled with the input | 72 /// Other programs that are meant to be linked and compiled with the input |
| 75 /// sources. | 73 /// sources. |
| 76 List<Program> _linkedDependencies; | 74 List<Program> _linkedDependencies; |
| 77 | 75 |
| 78 /// The location of the SDK, or `null` if the location hasn't been determined | 76 /// The location of the SDK, or `null` if the location hasn't been determined |
| 79 /// yet. | 77 /// yet. |
| 80 Uri _sdkRoot; | 78 Uri _sdkRoot; |
| 81 Uri get sdkRoot { | 79 Uri get sdkRoot => _sdkRoot ??= _normalizeSdkRoot(); |
| 82 _ensureSdkDefaults(); | |
| 83 return _sdkRoot; | |
| 84 } | |
| 85 | 80 |
| 86 Uri _sdkSummary; | 81 Uri _sdkSummary; |
| 87 Uri get sdkSummary { | 82 Uri get sdkSummary => _sdkSummary ??= _computeSdkSummaryUri(); |
| 88 _ensureSdkDefaults(); | |
| 89 return _sdkSummary; | |
| 90 } | |
| 91 | |
| 92 Uri _librariesSpecificationUri; | |
| 93 Uri get librariesSpecificationUri { | |
| 94 _ensureSdkDefaults(); | |
| 95 return _librariesSpecificationUri; | |
| 96 } | |
| 97 | 83 |
| 98 Ticker ticker; | 84 Ticker ticker; |
| 99 | 85 |
| 100 bool get verbose => _raw.verbose; | 86 bool get verbose => _raw.verbose; |
| 101 | 87 |
| 102 bool get verify => _raw.verify; | 88 bool get verify => _raw.verify; |
| 103 | 89 |
| 104 bool get debugDump => _raw.debugDump; | 90 bool get debugDump => _raw.debugDump; |
| 105 | 91 |
| 106 bool get setExitCodeOnProblem => _raw.setExitCodeOnProblem; | 92 bool get setExitCodeOnProblem => _raw.setExitCodeOnProblem; |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 Program loadProgram(List<int> bytes, CanonicalName nameRoot) { | 267 Program loadProgram(List<int> bytes, CanonicalName nameRoot) { |
| 282 return loadProgramFromBytes(bytes, new Program(nameRoot: nameRoot)); | 268 return loadProgramFromBytes(bytes, new Program(nameRoot: nameRoot)); |
| 283 } | 269 } |
| 284 | 270 |
| 285 /// Get the [UriTranslator] which resolves "package:" and "dart:" URIs. | 271 /// Get the [UriTranslator] which resolves "package:" and "dart:" URIs. |
| 286 /// | 272 /// |
| 287 /// This is an asynchronous method since file system operations may be | 273 /// This is an asynchronous method since file system operations may be |
| 288 /// required to locate/read the packages file as well as SDK metadata. | 274 /// required to locate/read the packages file as well as SDK metadata. |
| 289 Future<UriTranslatorImpl> getUriTranslator() async { | 275 Future<UriTranslatorImpl> getUriTranslator() async { |
| 290 if (_uriTranslator == null) { | 276 if (_uriTranslator == null) { |
| 291 ticker.logMs("Started building UriTranslator"); | 277 await _getPackages(); |
| 292 var libraries = await _computeLibrarySpecification(); | 278 // TODO(scheglov) Load SDK libraries from whatever format we decide. |
| 293 ticker.logMs("Read libraries file"); | 279 // TODO(scheglov) Remove the field "_raw.dartLibraries". |
| 294 var packages = await _getPackages(); | 280 var libraries = _raw.dartLibraries ?? await _parseDartLibraries(); |
| 281 _uriTranslator = new UriTranslatorImpl( |
| 282 libraries, const <String, List<Uri>>{}, _packages); |
| 295 ticker.logMs("Read packages file"); | 283 ticker.logMs("Read packages file"); |
| 296 _uriTranslator = new UriTranslatorImpl(libraries, packages); | |
| 297 } | 284 } |
| 298 return _uriTranslator; | 285 return _uriTranslator; |
| 299 } | 286 } |
| 300 | 287 |
| 301 Future<TargetLibrariesSpecification> _computeLibrarySpecification() async { | 288 Future<Map<String, Uri>> _parseDartLibraries() async { |
| 302 var name = target.name; | 289 Uri librariesJson = _raw.sdkRoot?.resolve("lib/libraries.json"); |
| 303 // TODO(sigmund): Eek! We should get to the point where there is no | 290 return await computeDartLibraries(fileSystem, librariesJson); |
| 304 // fasta-specific targets and the target names are meaningful. | |
| 305 if (name.endsWith('_fasta')) name = name.substring(0, name.length - 6); | |
| 306 | |
| 307 if (librariesSpecificationUri == null || | |
| 308 !await fileSystem.entityForUri(librariesSpecificationUri).exists()) { | |
| 309 if (compileSdk) { | |
| 310 reportWithoutLocation( | |
| 311 templateSdkSpecificationNotFound | |
| 312 .withArguments(librariesSpecificationUri), | |
| 313 Severity.error); | |
| 314 } | |
| 315 return new TargetLibrariesSpecification(name); | |
| 316 } | |
| 317 | |
| 318 var json = | |
| 319 await fileSystem.entityForUri(librariesSpecificationUri).readAsString(); | |
| 320 try { | |
| 321 var spec = | |
| 322 await LibrariesSpecification.parse(librariesSpecificationUri, json); | |
| 323 return spec.specificationFor(name); | |
| 324 } on LibrariesSpecificationException catch (e) { | |
| 325 reportWithoutLocation( | |
| 326 templateCannotReadSdkSpecification.withArguments('${e.error}'), | |
| 327 Severity.error); | |
| 328 return new TargetLibrariesSpecification(name); | |
| 329 } | |
| 330 } | 291 } |
| 331 | 292 |
| 332 /// Get the package map which maps package names to URIs. | 293 /// Get the package map which maps package names to URIs. |
| 333 /// | 294 /// |
| 334 /// This is an asynchronous getter since file system operations may be | 295 /// This is an asynchronous getter since file system operations may be |
| 335 /// required to locate/read the packages file. | 296 /// required to locate/read the packages file. |
| 336 Future<Packages> _getPackages() async { | 297 Future<Packages> _getPackages() async { |
| 337 if (_packages != null) return _packages; | 298 if (_packages != null) return _packages; |
| 338 if (_raw.packagesFileUri != null) { | 299 if (_raw.packagesFileUri != null) { |
| 339 return _packages = await createPackagesFromFile(_raw.packagesFileUri); | 300 return _packages = await createPackagesFromFile(_raw.packagesFileUri); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 candidate = await checkInDir(parentDir); | 391 candidate = await checkInDir(parentDir); |
| 431 if (candidate != null) break; | 392 if (candidate != null) break; |
| 432 dir = parentDir; | 393 dir = parentDir; |
| 433 parentDir = dir.resolve('..'); | 394 parentDir = dir.resolve('..'); |
| 434 } | 395 } |
| 435 | 396 |
| 436 if (candidate != null) return createPackagesFromFile(candidate); | 397 if (candidate != null) return createPackagesFromFile(candidate); |
| 437 return Packages.noPackages; | 398 return Packages.noPackages; |
| 438 } | 399 } |
| 439 | 400 |
| 440 bool _computedSdkDefaults = false; | 401 /// Get the location of the SDK. |
| 441 | 402 Uri _normalizeSdkRoot() { |
| 442 /// Ensure [_sdkRoot], [_sdkSummary] and [_librarySpecUri] are initialized. | 403 // If an SDK summary location was provided, the SDK itself should not be |
| 443 /// | 404 // needed. |
| 444 /// If they are not set explicitly, they are infered based on the default | 405 assert(_raw.sdkSummary == null); |
| 445 /// behavior described in [CompilerOptions]. | 406 if (_raw.sdkRoot == null) { |
| 446 void _ensureSdkDefaults() { | |
| 447 if (_computedSdkDefaults) return; | |
| 448 _computedSdkDefaults = true; | |
| 449 var root = _raw.sdkRoot; | |
| 450 if (root != null) { | |
| 451 // Normalize to always end in '/' | |
| 452 if (!root.path.endsWith('/')) { | |
| 453 root = root.replace(path: root.path + '/'); | |
| 454 } | |
| 455 _sdkRoot = root; | |
| 456 } else if (compileSdk) { | |
| 457 // TODO(paulberry): implement the algorithm for finding the SDK | 407 // TODO(paulberry): implement the algorithm for finding the SDK |
| 458 // automagically. | 408 // automagically. |
| 459 unimplemented('infer the default sdk location', -1, null); | 409 return unimplemented('infer the default sdk location', -1, null); |
| 460 } | 410 } |
| 411 var root = _raw.sdkRoot; |
| 412 if (!root.path.endsWith('/')) { |
| 413 root = root.replace(path: root.path + '/'); |
| 414 } |
| 415 return root; |
| 416 } |
| 461 | 417 |
| 462 if (_raw.sdkSummary != null) { | 418 /// Get or infer the location of the SDK summary. |
| 463 _sdkSummary = _raw.sdkSummary; | 419 Uri _computeSdkSummaryUri() { |
| 464 } else if (!compileSdk) { | 420 if (_raw.sdkSummary != null) return _raw.sdkSummary; |
| 465 // Infer based on the sdkRoot, but only when `compileSdk` is false, | |
| 466 // otherwise the default intent was to compile the sdk from sources and | |
| 467 // not to load an sdk summary file. | |
| 468 _sdkSummary = root?.resolve('outline.dill'); | |
| 469 } | |
| 470 | 421 |
| 471 if (_raw.librariesSpecificationUri != null) { | 422 // Infer based on the sdkRoot, but only when `compileSdk` is false, |
| 472 _librariesSpecificationUri = _raw.librariesSpecificationUri; | 423 // otherwise the default intent was to compile the sdk from sources and not |
| 473 } else if (compileSdk) { | 424 // to load an sdk summary file. |
| 474 _librariesSpecificationUri = sdkRoot.resolve('lib/libraries.json'); | 425 if (_raw.compileSdk) return null; |
| 475 } | 426 return sdkRoot.resolve('outline.dill'); |
| 476 } | 427 } |
| 477 | 428 |
| 478 /// Create a [FileSystem] specific to the current options. | 429 /// Create a [FileSystem] specific to the current options. |
| 479 /// | 430 /// |
| 480 /// If `_raw.multiRoots` is not empty, the file-system will implement the | 431 /// If `_raw.multiRoots` is not empty, the file-system will implement the |
| 481 /// semantics of multiple roots. If [chaseDependencies] is false, the | 432 /// semantics of multiple roots. If [chaseDependencies] is false, the |
| 482 /// resulting file system will be hermetic. | 433 /// resulting file system will be hermetic. |
| 483 FileSystem _createFileSystem() { | 434 FileSystem _createFileSystem() { |
| 484 var result = _raw.fileSystem; | 435 var result = _raw.fileSystem; |
| 485 // Note: hermetic checks are done before translating multi-root URIs, so | 436 // Note: hermetic checks are done before translating multi-root URIs, so |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 writeList('Multiroots', _raw.multiRoots); | 487 writeList('Multiroots', _raw.multiRoots); |
| 537 | 488 |
| 538 sb.writeln('Modular: ${_modularApi}'); | 489 sb.writeln('Modular: ${_modularApi}'); |
| 539 sb.writeln('Hermetic: ${!chaseDependencies}' | 490 sb.writeln('Hermetic: ${!chaseDependencies}' |
| 540 ' (provided: ${!_raw.chaseDependencies})'); | 491 ' (provided: ${!_raw.chaseDependencies})'); |
| 541 sb.writeln('Packages uri: ${_raw.packagesFileUri}'); | 492 sb.writeln('Packages uri: ${_raw.packagesFileUri}'); |
| 542 sb.writeln('Packages: ${_packages}'); | 493 sb.writeln('Packages: ${_packages}'); |
| 543 | 494 |
| 544 sb.writeln('Compile SDK: ${compileSdk}'); | 495 sb.writeln('Compile SDK: ${compileSdk}'); |
| 545 sb.writeln('SDK root: ${_sdkRoot} (provided: ${_raw.sdkRoot})'); | 496 sb.writeln('SDK root: ${_sdkRoot} (provided: ${_raw.sdkRoot})'); |
| 546 sb.writeln('SDK specification: ${_librariesSpecificationUri} ' | |
| 547 '(provided: ${_raw.librariesSpecificationUri})'); | |
| 548 sb.writeln('SDK summary: ${_sdkSummary} (provided: ${_raw.sdkSummary})'); | 497 sb.writeln('SDK summary: ${_sdkSummary} (provided: ${_raw.sdkSummary})'); |
| 549 | 498 |
| 550 sb.writeln('Strong: ${strongMode}'); | 499 sb.writeln('Strong: ${strongMode}'); |
| 551 sb.writeln('Target: ${_target?.name} (provided: ${_raw.target?.name})'); | 500 sb.writeln('Target: ${_target?.name} (provided: ${_raw.target?.name})'); |
| 552 | 501 |
| 553 sb.writeln('throwOnErrorsAreFatal: ${throwOnErrors}'); | 502 sb.writeln('throwOnErrorsAreFatal: ${throwOnErrors}'); |
| 554 sb.writeln('throwOnWarningsAreFatal: ${throwOnWarnings}'); | 503 sb.writeln('throwOnWarningsAreFatal: ${throwOnWarnings}'); |
| 555 sb.writeln('throwOnNits: ${throwOnNits}'); | 504 sb.writeln('throwOnNits: ${throwOnNits}'); |
| 556 sb.writeln('exit on problem: ${setExitCodeOnProblem}'); | 505 sb.writeln('exit on problem: ${setExitCodeOnProblem}'); |
| 557 sb.writeln('Embed sources: ${embedSourceText}'); | 506 sb.writeln('Embed sources: ${embedSourceText}'); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 String get dart2jsCode => _original.code.dart2jsCode; | 551 String get dart2jsCode => _original.code.dart2jsCode; |
| 603 | 552 |
| 604 SourceSpan get span => | 553 SourceSpan get span => |
| 605 new SourceLocation(_original.charOffset, sourceUrl: _original.uri) | 554 new SourceLocation(_original.charOffset, sourceUrl: _original.uri) |
| 606 .pointSpan(); | 555 .pointSpan(); |
| 607 | 556 |
| 608 _CompilationMessage(this._original, this.severity); | 557 _CompilationMessage(this._original, this.severity); |
| 609 | 558 |
| 610 String toString() => message; | 559 String toString() => message; |
| 611 } | 560 } |
| OLD | NEW |