| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library dart2js.cmdline; | 5 library dart2js.cmdline; |
| 6 | 6 |
| 7 import 'dart:async' show EventSink, Future; | 7 import 'dart:async' show EventSink, Future; |
| 8 import 'dart:convert' show UTF8, LineSplitter; | 8 import 'dart:convert' show UTF8, LineSplitter; |
| 9 import 'dart:io' show exit, File, FileMode, Platform, stdin, stderr; | 9 import 'dart:io' show exit, File, FileMode, Platform, stdin, stderr; |
| 10 | 10 |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 diagnosticHandler.info( | 464 diagnosticHandler.info( |
| 465 "Option '${Flags.analyzeAll}' implies '${Flags.analyzeOnly}'.", | 465 "Option '${Flags.analyzeAll}' implies '${Flags.analyzeOnly}'.", |
| 466 api.Diagnostic.INFO); | 466 api.Diagnostic.INFO); |
| 467 } | 467 } |
| 468 diagnosticHandler.info( | 468 diagnosticHandler.info( |
| 469 "Options $optionsImplyOutput indicate that output is expected, " | 469 "Options $optionsImplyOutput indicate that output is expected, " |
| 470 "but compilation is turned off by the option '${Flags.analyzeOnly}'.", | 470 "but compilation is turned off by the option '${Flags.analyzeOnly}'.", |
| 471 api.Diagnostic.INFO); | 471 api.Diagnostic.INFO); |
| 472 } | 472 } |
| 473 if (resolveOnly) { | 473 if (resolveOnly) { |
| 474 if (resolutionInputs.contains(resolutionOutput)) { |
| 475 helpAndFail("Resolution input '${resolutionOutput}' can't be used as " |
| 476 "resolution output. Use the '--out' option to specify another resolution " |
| 477 "output."); |
| 478 } |
| 474 analyzeOnly = analyzeAll = true; | 479 analyzeOnly = analyzeAll = true; |
| 475 } else if (analyzeAll) { | 480 } else if (analyzeAll) { |
| 476 analyzeOnly = true; | 481 analyzeOnly = true; |
| 477 } | 482 } |
| 478 if (!analyzeOnly) { | 483 if (!analyzeOnly) { |
| 479 if (allowNativeExtensions) { | 484 if (allowNativeExtensions) { |
| 480 helpAndFail("Option '${Flags.allowNativeExtensions}' is only supported " | 485 helpAndFail("Option '${Flags.allowNativeExtensions}' is only supported " |
| 481 "in combination with the '${Flags.analyzeOnly}' option."); | 486 "in combination with the '${Flags.analyzeOnly}' option."); |
| 482 } | 487 } |
| 483 } | 488 } |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 // TODO(johnniwinther): Add corresponding options to the test script and change | 843 // TODO(johnniwinther): Add corresponding options to the test script and change |
| 839 // these to use 'bool.fromEnvironment'. | 844 // these to use 'bool.fromEnvironment'. |
| 840 final bool USE_SERIALIZED_DART_CORE = | 845 final bool USE_SERIALIZED_DART_CORE = |
| 841 Platform.environment['USE_SERIALIZED_DART_CORE'] == 'true'; | 846 Platform.environment['USE_SERIALIZED_DART_CORE'] == 'true'; |
| 842 | 847 |
| 843 final bool SERIALIZED_COMPILATION = | 848 final bool SERIALIZED_COMPILATION = |
| 844 Platform.environment['SERIALIZED_COMPILATION'] == 'true'; | 849 Platform.environment['SERIALIZED_COMPILATION'] == 'true'; |
| 845 | 850 |
| 846 /// Mock URI used only in testing when [USE_SERIALIZED_DART_CORE] or | 851 /// Mock URI used only in testing when [USE_SERIALIZED_DART_CORE] or |
| 847 /// [SERIALIZED_COMPILATION] is enabled. | 852 /// [SERIALIZED_COMPILATION] is enabled. |
| 848 final Uri _SERIALIZED_URI = Uri.parse('file:fake.data'); | 853 final Uri _SERIALIZED_DART_CORE_URI = Uri.parse('file:core.data'); |
| 854 final Uri _SERIALIZED_TEST_URI = Uri.parse('file:test.data'); |
| 849 | 855 |
| 850 void _useSerializedDataForDartCore(CompileFunc oldCompileFunc) { | 856 void _useSerializedDataForDartCore(CompileFunc oldCompileFunc) { |
| 851 /// Run the [oldCompileFunc] with [serializedData] added as resolution input. | 857 /// Run the [oldCompileFunc] with [serializedData] added as resolution input. |
| 852 Future<api.CompilationResult> compileWithSerializedData( | 858 Future<api.CompilationResult> compileWithSerializedData( |
| 853 CompilerOptions compilerOptions, | 859 CompilerOptions compilerOptions, |
| 854 api.CompilerInput compilerInput, | 860 api.CompilerInput compilerInput, |
| 855 api.CompilerDiagnostics compilerDiagnostics, | 861 api.CompilerDiagnostics compilerDiagnostics, |
| 856 api.CompilerOutput compilerOutput, | 862 api.CompilerOutput compilerOutput, |
| 857 String serializedData) { | 863 List<_SerializedData> serializedData) { |
| 858 api.CompilerInput input = compilerInput; | 864 api.CompilerInput input = compilerInput; |
| 859 CompilerOptions options = compilerOptions; | 865 CompilerOptions options = compilerOptions; |
| 860 if (serializedData != null) { | 866 if (serializedData != null && serializedData.isNotEmpty) { |
| 861 input = new _CompilerInput(input, serializedData); | 867 Map<Uri, String> dataMap = <Uri, String>{}; |
| 862 List<Uri> resolutionInputs = compilerOptions.resolutionInputs; | 868 for (_SerializedData data in serializedData) { |
| 863 if (resolutionInputs == null) { | 869 dataMap[data.uri] = data.data; |
| 864 resolutionInputs = <Uri>[_SERIALIZED_URI]; | 870 } |
| 865 } else if (!resolutionInputs.contains(_SERIALIZED_URI)) { | 871 input = new _CompilerInput(input, dataMap); |
| 866 resolutionInputs = new List<Uri>.from(resolutionInputs) | 872 List<Uri> resolutionInputs = dataMap.keys.toList(); |
| 867 ..add(_SERIALIZED_URI); | 873 if (compilerOptions.resolutionInputs != null) { |
| 874 for (Uri uri in compilerOptions.resolutionInputs) { |
| 875 if (!dataMap.containsKey(uri)) { |
| 876 resolutionInputs.add(uri); |
| 877 } |
| 878 } |
| 868 } | 879 } |
| 869 options = options.copy(resolutionInputs: resolutionInputs); | 880 options = options.copy(resolutionInputs: resolutionInputs); |
| 870 } | 881 } |
| 871 return oldCompileFunc(options, input, compilerDiagnostics, compilerOutput); | 882 return oldCompileFunc(options, input, compilerDiagnostics, compilerOutput); |
| 872 } | 883 } |
| 873 | 884 |
| 874 /// Serialize [entryPoint] using [serializedData] if provided. | 885 /// Serialize [entryPoint] using [serializedData] if provided. |
| 875 Future<api.CompilationResult> serialize( | 886 Future<api.CompilationResult> serialize( |
| 876 Uri entryPoint, | 887 Uri entryPoint, |
| 888 Uri serializedUri, |
| 877 CompilerOptions compilerOptions, | 889 CompilerOptions compilerOptions, |
| 878 api.CompilerInput compilerInput, | 890 api.CompilerInput compilerInput, |
| 879 api.CompilerDiagnostics compilerDiagnostics, | 891 api.CompilerDiagnostics compilerDiagnostics, |
| 880 api.CompilerOutput compilerOutput, | 892 api.CompilerOutput compilerOutput, |
| 881 [String serializedData]) { | 893 [List<_SerializedData> serializedData]) { |
| 882 CompilerOptions options = new CompilerOptions.parse( | 894 CompilerOptions options = new CompilerOptions.parse( |
| 883 entryPoint: entryPoint, | 895 entryPoint: entryPoint, |
| 884 libraryRoot: compilerOptions.libraryRoot, | 896 libraryRoot: compilerOptions.libraryRoot, |
| 885 packageRoot: compilerOptions.packageRoot, | 897 packageRoot: compilerOptions.packageRoot, |
| 886 packageConfig: compilerOptions.packageConfig, | 898 packageConfig: compilerOptions.packageConfig, |
| 887 packagesDiscoveryProvider: compilerOptions.packagesDiscoveryProvider, | 899 packagesDiscoveryProvider: compilerOptions.packagesDiscoveryProvider, |
| 888 environment: compilerOptions.environment, | 900 environment: compilerOptions.environment, |
| 889 resolutionOutput: _SERIALIZED_URI, | 901 resolutionOutput: serializedUri, |
| 890 options: [Flags.resolveOnly]); | 902 options: [Flags.resolveOnly]); |
| 891 return compileWithSerializedData(options, compilerInput, | 903 return compileWithSerializedData(options, compilerInput, |
| 892 compilerDiagnostics, compilerOutput, serializedData); | 904 compilerDiagnostics, compilerOutput, serializedData); |
| 893 } | 905 } |
| 894 | 906 |
| 895 // Local cache for the serialized data for dart:core. | 907 // Local cache for the serialized data for dart:core. |
| 896 String serializedDartCore; | 908 _SerializedData serializedDartCore; |
| 897 | 909 |
| 898 /// Serialize the entry point using serialized data from dart:core and run | 910 /// Serialize the entry point using serialized data from dart:core and run |
| 899 /// [oldCompileFunc] using serialized data for whole program. | 911 /// [oldCompileFunc] using serialized data for whole program. |
| 900 Future<api.CompilationResult> compileFromSerializedData( | 912 Future<api.CompilationResult> compileFromSerializedData( |
| 901 CompilerOptions compilerOptions, | 913 CompilerOptions compilerOptions, |
| 902 api.CompilerInput compilerInput, | 914 api.CompilerInput compilerInput, |
| 903 api.CompilerDiagnostics compilerDiagnostics, | 915 api.CompilerDiagnostics compilerDiagnostics, |
| 904 api.CompilerOutput compilerOutput) async { | 916 api.CompilerOutput compilerOutput) async { |
| 905 _CompilerOutput output = new _CompilerOutput(); | 917 _CompilerOutput output = new _CompilerOutput(_SERIALIZED_TEST_URI); |
| 906 api.CompilationResult result = await serialize( | 918 api.CompilationResult result = await serialize( |
| 907 compilerOptions.entryPoint, | 919 compilerOptions.entryPoint, |
| 920 output.uri, |
| 908 compilerOptions, | 921 compilerOptions, |
| 909 compilerInput, | 922 compilerInput, |
| 910 compilerDiagnostics, | 923 compilerDiagnostics, |
| 911 output, | 924 output, |
| 912 serializedDartCore); | 925 [serializedDartCore]); |
| 913 if (!result.isSuccess) { | 926 if (!result.isSuccess) { |
| 914 return result; | 927 return result; |
| 915 } | 928 } |
| 916 return compileWithSerializedData(compilerOptions, compilerInput, | 929 return compileWithSerializedData( |
| 917 compilerDiagnostics, compilerOutput, output.serializedData); | 930 compilerOptions, |
| 931 compilerInput, |
| 932 compilerDiagnostics, |
| 933 compilerOutput, |
| 934 [serializedDartCore, output.serializedData]); |
| 918 } | 935 } |
| 919 | 936 |
| 920 /// Compiles the entry point using the serialized data from dart:core. | 937 /// Compiles the entry point using the serialized data from dart:core. |
| 921 Future<api.CompilationResult> compileWithSerializedDartCoreData( | 938 Future<api.CompilationResult> compileWithSerializedDartCoreData( |
| 922 CompilerOptions compilerOptions, | 939 CompilerOptions compilerOptions, |
| 923 api.CompilerInput compilerInput, | 940 api.CompilerInput compilerInput, |
| 924 api.CompilerDiagnostics compilerDiagnostics, | 941 api.CompilerDiagnostics compilerDiagnostics, |
| 925 api.CompilerOutput compilerOutput) async { | 942 api.CompilerOutput compilerOutput) async { |
| 926 return compileWithSerializedData(compilerOptions, compilerInput, | 943 return compileWithSerializedData(compilerOptions, compilerInput, |
| 927 compilerDiagnostics, compilerOutput, serializedDartCore); | 944 compilerDiagnostics, compilerOutput, [serializedDartCore]); |
| 928 } | 945 } |
| 929 | 946 |
| 930 /// Serialize dart:core data into [serializedDartCore] and setup the | 947 /// Serialize dart:core data into [serializedDartCore] and setup the |
| 931 /// [compileFunc] to run the compiler using this data. | 948 /// [compileFunc] to run the compiler using this data. |
| 932 Future<api.CompilationResult> generateSerializedDataForDartCore( | 949 Future<api.CompilationResult> generateSerializedDataForDartCore( |
| 933 CompilerOptions compilerOptions, | 950 CompilerOptions compilerOptions, |
| 934 api.CompilerInput compilerInput, | 951 api.CompilerInput compilerInput, |
| 935 api.CompilerDiagnostics compilerDiagnostics, | 952 api.CompilerDiagnostics compilerDiagnostics, |
| 936 api.CompilerOutput compilerOutput) async { | 953 api.CompilerOutput compilerOutput) async { |
| 937 _CompilerOutput output = new _CompilerOutput(); | 954 _CompilerOutput output = new _CompilerOutput(_SERIALIZED_DART_CORE_URI); |
| 938 await serialize(Uris.dart_core, compilerOptions, compilerInput, | 955 await serialize(Uris.dart_core, output.uri, compilerOptions, compilerInput, |
| 939 compilerDiagnostics, output); | 956 compilerDiagnostics, output); |
| 940 serializedDartCore = output.serializedData; | 957 serializedDartCore = output.serializedData; |
| 941 if (SERIALIZED_COMPILATION) { | 958 if (SERIALIZED_COMPILATION) { |
| 942 compileFunc = compileFromSerializedData; | 959 compileFunc = compileFromSerializedData; |
| 943 } else { | 960 } else { |
| 944 compileFunc = compileWithSerializedDartCoreData; | 961 compileFunc = compileWithSerializedDartCoreData; |
| 945 } | 962 } |
| 946 return compileFunc( | 963 return compileFunc( |
| 947 compilerOptions, compilerInput, compilerDiagnostics, compilerOutput); | 964 compilerOptions, compilerInput, compilerDiagnostics, compilerOutput); |
| 948 } | 965 } |
| 949 | 966 |
| 950 compileFunc = generateSerializedDataForDartCore; | 967 compileFunc = generateSerializedDataForDartCore; |
| 951 } | 968 } |
| 952 | 969 |
| 953 class _CompilerInput implements api.CompilerInput { | 970 class _CompilerInput implements api.CompilerInput { |
| 954 final api.CompilerInput _input; | 971 final api.CompilerInput _input; |
| 955 final String _data; | 972 final Map<Uri, String> _data; |
| 956 | 973 |
| 957 _CompilerInput(this._input, this._data); | 974 _CompilerInput(this._input, this._data); |
| 958 | 975 |
| 959 @override | 976 @override |
| 960 Future readFromUri(Uri uri) { | 977 Future readFromUri(Uri uri) { |
| 961 if (uri == _SERIALIZED_URI) { | 978 String data = _data[uri]; |
| 962 return new Future.value(_data); | 979 if (data != null) { |
| 980 return new Future.value(data); |
| 963 } | 981 } |
| 964 return _input.readFromUri(uri); | 982 return _input.readFromUri(uri); |
| 965 } | 983 } |
| 966 } | 984 } |
| 967 | 985 |
| 986 class _SerializedData { |
| 987 final Uri uri; |
| 988 final String data; |
| 989 |
| 990 _SerializedData(this.uri, this.data); |
| 991 } |
| 992 |
| 968 class _CompilerOutput extends NullCompilerOutput { | 993 class _CompilerOutput extends NullCompilerOutput { |
| 994 final Uri uri; |
| 969 _BufferedEventSink sink; | 995 _BufferedEventSink sink; |
| 970 | 996 |
| 997 _CompilerOutput(this.uri); |
| 998 |
| 971 @override | 999 @override |
| 972 EventSink<String> createEventSink(String name, String extension) { | 1000 EventSink<String> createEventSink(String name, String extension) { |
| 973 if (name == '' && extension == 'data') { | 1001 if (name == '' && extension == 'data') { |
| 974 return sink = new _BufferedEventSink(); | 1002 return sink = new _BufferedEventSink(); |
| 975 } | 1003 } |
| 976 return super.createEventSink(name, extension); | 1004 return super.createEventSink(name, extension); |
| 977 } | 1005 } |
| 978 | 1006 |
| 979 String get serializedData => sink.sb.toString(); | 1007 _SerializedData get serializedData { |
| 1008 return new _SerializedData(uri, sink.sb.toString()); |
| 1009 } |
| 980 } | 1010 } |
| 981 | 1011 |
| 982 class _BufferedEventSink implements EventSink<String> { | 1012 class _BufferedEventSink implements EventSink<String> { |
| 983 StringBuffer sb = new StringBuffer(); | 1013 StringBuffer sb = new StringBuffer(); |
| 984 | 1014 |
| 985 @override | 1015 @override |
| 986 void add(String event) { | 1016 void add(String event) { |
| 987 sb.write(event); | 1017 sb.write(event); |
| 988 } | 1018 } |
| 989 | 1019 |
| 990 @override | 1020 @override |
| 991 void close() { | 1021 void close() { |
| 992 // Do nothing. | 1022 // Do nothing. |
| 993 } | 1023 } |
| 994 | 1024 |
| 995 @override | 1025 @override |
| 996 void addError(errorEvent, [StackTrace stackTrace]) { | 1026 void addError(errorEvent, [StackTrace stackTrace]) { |
| 997 // Ignore | 1027 // Ignore |
| 998 } | 1028 } |
| 999 } | 1029 } |
| OLD | NEW |