Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Side by Side Diff: pkg/compiler/lib/src/dart2js.dart

Issue 1975153002: Support (de)serialization from command-line (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 Future; 7 import 'dart:async' show 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 } 98 }
99 } 99 }
100 100
101 FormattingDiagnosticHandler diagnosticHandler; 101 FormattingDiagnosticHandler diagnosticHandler;
102 102
103 Future<api.CompilationResult> compile(List<String> argv) { 103 Future<api.CompilationResult> compile(List<String> argv) {
104 stackTraceFilePrefix = '$currentDirectory'; 104 stackTraceFilePrefix = '$currentDirectory';
105 Uri libraryRoot = currentDirectory; 105 Uri libraryRoot = currentDirectory;
106 Uri out = currentDirectory.resolve('out.js'); 106 Uri out = currentDirectory.resolve('out.js');
107 Uri sourceMapOut = currentDirectory.resolve('out.js.map'); 107 Uri sourceMapOut = currentDirectory.resolve('out.js.map');
108 Uri serializationSource;
109 Uri serializationTarget;
108 Uri packageConfig = null; 110 Uri packageConfig = null;
109 Uri packageRoot = null; 111 Uri packageRoot = null;
110 List<String> options = new List<String>(); 112 List<String> options = new List<String>();
111 bool explicitOut = false; 113 bool explicitOut = false;
112 bool wantHelp = false; 114 bool wantHelp = false;
113 bool wantVersion = false; 115 bool wantVersion = false;
114 String outputLanguage = 'JavaScript'; 116 String outputLanguage = 'JavaScript';
115 bool stripArgumentSet = false; 117 bool stripArgumentSet = false;
116 bool analyzeOnly = false; 118 bool analyzeOnly = false;
117 bool analyzeAll = false; 119 bool analyzeAll = false;
118 bool dumpInfo = false; 120 bool dumpInfo = false;
119 bool allowNativeExtensions = false; 121 bool allowNativeExtensions = false;
120 bool trustTypeAnnotations = false; 122 bool trustTypeAnnotations = false;
121 bool trustJSInteropTypeAnnotations = false; 123 bool trustJSInteropTypeAnnotations = false;
122 bool checkedMode = false; 124 bool checkedMode = false;
123 // List of provided options that imply that output is expected. 125 // List of provided options that imply that output is expected.
124 List<String> optionsImplyCompilation = <String>[]; 126 List<String> optionsImplyCompilation = <String>[];
125 bool hasDisallowUnsafeEval = false; 127 bool hasDisallowUnsafeEval = false;
126 // TODO(johnniwinther): Measure time for reading files. 128 // TODO(johnniwinther): Measure time for reading files.
127 SourceFileProvider inputProvider = new CompilerSourceFileProvider(); 129 SourceFileProvider inputProvider = new CompilerSourceFileProvider();
128 diagnosticHandler = new FormattingDiagnosticHandler(inputProvider); 130 diagnosticHandler = new FormattingDiagnosticHandler(inputProvider);
129 Map<String, dynamic> environment = new Map<String, dynamic>(); 131 Map<String, dynamic> environment = new Map<String, dynamic>();
130 132
131 passThrough(String argument) => options.add(argument); 133 void passThrough(String argument) => options.add(argument);
132 134
133 if (BUILD_ID != null) { 135 if (BUILD_ID != null) {
134 passThrough("--build-id=$BUILD_ID"); 136 passThrough("--build-id=$BUILD_ID");
135 } 137 }
136 138
137 setLibraryRoot(String argument) { 139 void setLibraryRoot(String argument) {
138 libraryRoot = currentDirectory.resolve(extractPath(argument)); 140 libraryRoot = currentDirectory.resolve(extractPath(argument));
139 } 141 }
140 142
141 setPackageRoot(String argument) { 143 void setPackageRoot(String argument) {
142 packageRoot = currentDirectory.resolve(extractPath(argument)); 144 packageRoot = currentDirectory.resolve(extractPath(argument));
143 } 145 }
144 146
145 setPackageConfig(String argument) { 147 void setPackageConfig(String argument) {
146 packageConfig = 148 packageConfig =
147 currentDirectory.resolve(extractPath(argument, isDirectory: false)); 149 currentDirectory.resolve(extractPath(argument, isDirectory: false));
148 } 150 }
149 151
150 setOutput(Iterator<String> arguments) { 152 void setOutput(Iterator<String> arguments) {
151 optionsImplyCompilation.add(arguments.current); 153 optionsImplyCompilation.add(arguments.current);
152 String path; 154 String path;
153 if (arguments.current == '-o') { 155 if (arguments.current == '-o') {
154 if (!arguments.moveNext()) { 156 if (!arguments.moveNext()) {
155 helpAndFail('Error: Missing file after -o option.'); 157 helpAndFail('Error: Missing file after -o option.');
156 } 158 }
157 path = arguments.current; 159 path = arguments.current;
158 } else { 160 } else {
159 path = extractParameter(arguments.current); 161 path = extractParameter(arguments.current);
160 } 162 }
161 explicitOut = true; 163 explicitOut = true;
162 out = currentDirectory.resolve(nativeToUriPath(path)); 164 out = currentDirectory.resolve(nativeToUriPath(path));
163 sourceMapOut = Uri.parse('$out.map'); 165 sourceMapOut = Uri.parse('$out.map');
164 } 166 }
165 167
166 setOutputType(String argument) { 168 void setOutputType(String argument) {
167 optionsImplyCompilation.add(argument); 169 optionsImplyCompilation.add(argument);
168 if (argument == '--output-type=dart' || 170 if (argument == '--output-type=dart' ||
169 argument == '--output-type=dart-multi') { 171 argument == '--output-type=dart-multi') {
170 outputLanguage = OUTPUT_LANGUAGE_DART; 172 outputLanguage = OUTPUT_LANGUAGE_DART;
171 if (!explicitOut) { 173 if (!explicitOut) {
172 out = currentDirectory.resolve('out.dart'); 174 out = currentDirectory.resolve('out.dart');
173 sourceMapOut = currentDirectory.resolve('out.dart.map'); 175 sourceMapOut = currentDirectory.resolve('out.dart.map');
174 } 176 }
175 diagnosticHandler( 177 diagnosticHandler(
176 null, 178 null,
177 null, 179 null,
178 null, 180 null,
179 "--output-type=dart is deprecated. It will remain available " 181 "--output-type=dart is deprecated. It will remain available "
180 "in Dart 1.11, but will be removed in Dart 1.12.", 182 "in Dart 1.11, but will be removed in Dart 1.12.",
181 api.Diagnostic.WARNING); 183 api.Diagnostic.WARNING);
182 } 184 }
183 passThrough(argument); 185 passThrough(argument);
184 } 186 }
185 187
188 void setSerializationSource(String argument) {
189 serializationSource =
190 currentDirectory.resolve(extractPath(argument, isDirectory: false));
191 }
192
193 void setSerializationTarget(String argument) {
194 serializationTarget =
195 currentDirectory.resolve(extractPath(argument, isDirectory: false));
196 }
197
186 String getDepsOutput(Map<Uri, SourceFile> sourceFiles) { 198 String getDepsOutput(Map<Uri, SourceFile> sourceFiles) {
187 var filenames = sourceFiles.keys.map((uri) => '$uri').toList(); 199 var filenames = sourceFiles.keys.map((uri) => '$uri').toList();
188 filenames.sort(); 200 filenames.sort();
189 return filenames.join("\n"); 201 return filenames.join("\n");
190 } 202 }
191 203
192 setStrip(String argument) { 204 void setStrip(String argument) {
193 optionsImplyCompilation.add(argument); 205 optionsImplyCompilation.add(argument);
194 stripArgumentSet = true; 206 stripArgumentSet = true;
195 passThrough(argument); 207 passThrough(argument);
196 } 208 }
197 209
198 setAnalyzeOnly(String argument) { 210 void setAnalyzeOnly(String argument) {
199 analyzeOnly = true; 211 analyzeOnly = true;
200 passThrough(argument); 212 passThrough(argument);
201 } 213 }
202 214
203 setAnalyzeAll(String argument) { 215 void setAnalyzeAll(String argument) {
204 analyzeAll = true; 216 analyzeAll = true;
205 passThrough(argument); 217 passThrough(argument);
206 } 218 }
207 219
208 setAllowNativeExtensions(String argument) { 220 void setAllowNativeExtensions(String argument) {
209 allowNativeExtensions = true; 221 allowNativeExtensions = true;
210 passThrough(argument); 222 passThrough(argument);
211 } 223 }
212 224
213 setVerbose(_) { 225 void setVerbose(_) {
214 diagnosticHandler.verbose = true; 226 diagnosticHandler.verbose = true;
215 passThrough('--verbose'); 227 passThrough('--verbose');
216 } 228 }
217 229
218 implyCompilation(String argument) { 230 void implyCompilation(String argument) {
219 optionsImplyCompilation.add(argument); 231 optionsImplyCompilation.add(argument);
220 passThrough(argument); 232 passThrough(argument);
221 } 233 }
222 234
223 setDumpInfo(String argument) { 235 void setDumpInfo(String argument) {
224 implyCompilation(argument); 236 implyCompilation(argument);
225 dumpInfo = true; 237 dumpInfo = true;
226 } 238 }
227 239
228 setTrustTypeAnnotations(String argument) { 240 void setTrustTypeAnnotations(String argument) {
229 trustTypeAnnotations = true; 241 trustTypeAnnotations = true;
230 implyCompilation(argument); 242 implyCompilation(argument);
231 } 243 }
232 244
233 setTrustJSInteropTypeAnnotations(String argument) { 245 void setTrustJSInteropTypeAnnotations(String argument) {
234 trustJSInteropTypeAnnotations = true; 246 trustJSInteropTypeAnnotations = true;
235 implyCompilation(argument); 247 implyCompilation(argument);
236 } 248 }
237 249
238 setTrustPrimitives(String argument) { 250 void setTrustPrimitives(String argument) {
239 implyCompilation(argument); 251 implyCompilation(argument);
240 } 252 }
241 253
242 setCheckedMode(String argument) { 254 void setCheckedMode(String argument) {
243 checkedMode = true; 255 checkedMode = true;
244 passThrough(argument); 256 passThrough(argument);
245 } 257 }
246 258
247 addInEnvironment(String argument) { 259 void addInEnvironment(String argument) {
248 int eqIndex = argument.indexOf('='); 260 int eqIndex = argument.indexOf('=');
249 String name = argument.substring(2, eqIndex); 261 String name = argument.substring(2, eqIndex);
250 String value = argument.substring(eqIndex + 1); 262 String value = argument.substring(eqIndex + 1);
251 environment[name] = value; 263 environment[name] = value;
252 } 264 }
253 265
254 setCategories(String argument) { 266 void setCategories(String argument) {
255 List<String> categories = extractParameter(argument).split(','); 267 List<String> categories = extractParameter(argument).split(',');
256 if (categories.contains('all')) { 268 if (categories.contains('all')) {
257 categories = ["Client", "Server"]; 269 categories = ["Client", "Server"];
258 } else { 270 } else {
259 for (String category in categories) { 271 for (String category in categories) {
260 if (!["Client", "Server"].contains(category)) { 272 if (!["Client", "Server"].contains(category)) {
261 fail('Unsupported library category "$category", ' 273 fail('Unsupported library category "$category", '
262 'supported categories are: Client, Server, all'); 274 'supported categories are: Client, Server, all');
263 } 275 }
264 } 276 }
265 } 277 }
266 passThrough('--categories=${categories.join(",")}'); 278 passThrough('--categories=${categories.join(",")}');
267 } 279 }
268 280
269 void handleThrowOnError(String argument) { 281 void handleThrowOnError(String argument) {
270 diagnosticHandler.throwOnError = true; 282 diagnosticHandler.throwOnError = true;
271 String parameter = extractParameter(argument, isOptionalArgument: true); 283 String parameter = extractParameter(argument, isOptionalArgument: true);
272 if (parameter != null) { 284 if (parameter != null) {
273 diagnosticHandler.throwOnErrorCount = int.parse(parameter); 285 diagnosticHandler.throwOnErrorCount = int.parse(parameter);
274 } 286 }
275 } 287 }
276 288
277 handleShortOptions(String argument) { 289 void handleShortOptions(String argument) {
278 var shortOptions = argument.substring(1).split(""); 290 var shortOptions = argument.substring(1).split("");
279 for (var shortOption in shortOptions) { 291 for (var shortOption in shortOptions) {
280 switch (shortOption) { 292 switch (shortOption) {
281 case 'v': 293 case 'v':
282 setVerbose(null); 294 setVerbose(null);
283 break; 295 break;
284 case 'h': 296 case 'h':
285 case '?': 297 case '?':
286 wantHelp = true; 298 wantHelp = true;
287 break; 299 break;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 new OptionHandler( 348 new OptionHandler(
337 Flags.trustJSInteropTypeAnnotations, 349 Flags.trustJSInteropTypeAnnotations,
338 (_) => setTrustJSInteropTypeAnnotations( 350 (_) => setTrustJSInteropTypeAnnotations(
339 Flags.trustJSInteropTypeAnnotations)), 351 Flags.trustJSInteropTypeAnnotations)),
340 new OptionHandler(r'--help|/\?|/h', (_) => wantHelp = true), 352 new OptionHandler(r'--help|/\?|/h', (_) => wantHelp = true),
341 new OptionHandler('--packages=.+', setPackageConfig), 353 new OptionHandler('--packages=.+', setPackageConfig),
342 new OptionHandler('--package-root=.+|-p.+', setPackageRoot), 354 new OptionHandler('--package-root=.+|-p.+', setPackageRoot),
343 new OptionHandler(Flags.analyzeAll, setAnalyzeAll), 355 new OptionHandler(Flags.analyzeAll, setAnalyzeAll),
344 new OptionHandler(Flags.analyzeOnly, setAnalyzeOnly), 356 new OptionHandler(Flags.analyzeOnly, setAnalyzeOnly),
345 new OptionHandler(Flags.noSourceMaps, passThrough), 357 new OptionHandler(Flags.noSourceMaps, passThrough),
358 new OptionHandler(Option.serializationSource, setSerializationSource),
359 new OptionHandler(Option.serializationTarget, setSerializationTarget),
346 new OptionHandler(Flags.analyzeSignaturesOnly, setAnalyzeOnly), 360 new OptionHandler(Flags.analyzeSignaturesOnly, setAnalyzeOnly),
347 new OptionHandler(Flags.disableNativeLiveTypeAnalysis, passThrough), 361 new OptionHandler(Flags.disableNativeLiveTypeAnalysis, passThrough),
348 new OptionHandler('--categories=.*', setCategories), 362 new OptionHandler('--categories=.*', setCategories),
349 new OptionHandler(Flags.disableTypeInference, implyCompilation), 363 new OptionHandler(Flags.disableTypeInference, implyCompilation),
350 new OptionHandler(Flags.terse, passThrough), 364 new OptionHandler(Flags.terse, passThrough),
351 new OptionHandler('--deferred-map=.+', implyCompilation), 365 new OptionHandler('--deferred-map=.+', implyCompilation),
352 new OptionHandler(Flags.dumpInfo, setDumpInfo), 366 new OptionHandler(Flags.dumpInfo, setDumpInfo),
353 new OptionHandler( 367 new OptionHandler(
354 '--disallow-unsafe-eval', (_) => hasDisallowUnsafeEval = true), 368 '--disallow-unsafe-eval', (_) => hasDisallowUnsafeEval = true),
355 new OptionHandler(Option.showPackageWarnings, passThrough), 369 new OptionHandler(Option.showPackageWarnings, passThrough),
356 new OptionHandler(Flags.useContentSecurityPolicy, passThrough), 370 new OptionHandler(Flags.useContentSecurityPolicy, passThrough),
357 new OptionHandler(Flags.enableExperimentalMirrors, passThrough), 371 new OptionHandler(Flags.enableExperimentalMirrors, passThrough),
358 new OptionHandler(Flags.enableAssertMessage, passThrough), 372 new OptionHandler(Flags.enableAssertMessage, passThrough),
373
359 // TODO(floitsch): remove conditional directives flag. 374 // TODO(floitsch): remove conditional directives flag.
360 // We don't provide the info-message yet, since we haven't publicly 375 // We don't provide the info-message yet, since we haven't publicly
361 // launched the feature yet. 376 // launched the feature yet.
362 new OptionHandler(Flags.conditionalDirectives, (_) {}), 377 new OptionHandler(Flags.conditionalDirectives, (_) {}),
363 new OptionHandler('--enable-async', (_) { 378 new OptionHandler('--enable-async', (_) {
364 diagnosticHandler.info( 379 diagnosticHandler.info(
365 "Option '--enable-async' is no longer needed. " 380 "Option '--enable-async' is no longer needed. "
366 "Async-await is supported by default.", 381 "Async-await is supported by default.",
367 api.Diagnostic.HINT); 382 api.Diagnostic.HINT);
368 }), 383 }),
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 "but compilation is turned off by the option '${Flags.analyzeOnly}'.", 454 "but compilation is turned off by the option '${Flags.analyzeOnly}'.",
440 api.Diagnostic.INFO); 455 api.Diagnostic.INFO);
441 } 456 }
442 if (analyzeAll) analyzeOnly = true; 457 if (analyzeAll) analyzeOnly = true;
443 if (!analyzeOnly) { 458 if (!analyzeOnly) {
444 if (allowNativeExtensions) { 459 if (allowNativeExtensions) {
445 helpAndFail("Option '${Flags.allowNativeExtensions}' is only supported " 460 helpAndFail("Option '${Flags.allowNativeExtensions}' is only supported "
446 "in combination with the '${Flags.analyzeOnly}' option."); 461 "in combination with the '${Flags.analyzeOnly}' option.");
447 } 462 }
448 } 463 }
464 if (!analyzeAll) {
465 if (serializationTarget != null) {
466 helpAndFail("Option '${Option.serializationTarget}' is only supported "
Siggi Cherem (dart-lang) 2016/05/14 01:10:31 I'd be OK implying it (instead of requiring that w
Johnni Winther 2016/05/17 12:37:33 Done.
467 "in combination with the '${Flags.analyzeAll}' option.");
468 }
469 }
449 if (dumpInfo && outputLanguage == OUTPUT_LANGUAGE_DART) { 470 if (dumpInfo && outputLanguage == OUTPUT_LANGUAGE_DART) {
450 helpAndFail("Option '${Flags.dumpInfo}' is not supported in " 471 helpAndFail("Option '${Flags.dumpInfo}' is not supported in "
451 "combination with the '--output-type=dart' option."); 472 "combination with the '--output-type=dart' option.");
452 } 473 }
453 474
454 options.add('--out=$out'); 475 options.add('--out=$out');
455 options.add('--source-map=$sourceMapOut'); 476 options.add('--source-map=$sourceMapOut');
456 477
457 RandomAccessFileOutputProvider outputProvider = 478 RandomAccessFileOutputProvider outputProvider =
458 new RandomAccessFileOutputProvider(out, sourceMapOut, 479 new RandomAccessFileOutputProvider(out, sourceMapOut,
459 onInfo: diagnosticHandler.info, onFailure: fail); 480 onInfo: diagnosticHandler.info,
481 onFailure: fail,
482 serializationTarget: serializationTarget);
460 483
461 api.CompilationResult compilationDone(api.CompilationResult result) { 484 api.CompilationResult compilationDone(api.CompilationResult result) {
462 if (analyzeOnly) return result; 485 if (analyzeOnly) return result;
463 if (!result.isSuccess) { 486 if (!result.isSuccess) {
464 fail('Compilation failed.'); 487 fail('Compilation failed.');
465 } 488 }
466 writeString( 489 writeString(
467 Uri.parse('$out.deps'), getDepsOutput(inputProvider.sourceFiles)); 490 Uri.parse('$out.deps'), getDepsOutput(inputProvider.sourceFiles));
468 diagnosticHandler 491 diagnosticHandler
469 .info('Compiled ${inputProvider.dartCharactersRead} characters Dart ' 492 .info('Compiled ${inputProvider.dartCharactersRead} characters Dart '
(...skipping 15 matching lines...) Expand all
485 return result; 508 return result;
486 } 509 }
487 510
488 Uri script = currentDirectory.resolve(arguments[0]); 511 Uri script = currentDirectory.resolve(arguments[0]);
489 CompilerOptions compilerOptions = new CompilerOptions.parse( 512 CompilerOptions compilerOptions = new CompilerOptions.parse(
490 entryPoint: script, 513 entryPoint: script,
491 libraryRoot: libraryRoot, 514 libraryRoot: libraryRoot,
492 packageRoot: packageRoot, 515 packageRoot: packageRoot,
493 packageConfig: packageConfig, 516 packageConfig: packageConfig,
494 packagesDiscoveryProvider: findPackages, 517 packagesDiscoveryProvider: findPackages,
518 serializationSource: serializationSource,
519 serializationTarget: serializationTarget,
495 options: options, 520 options: options,
496 environment: environment); 521 environment: environment);
497 return compileFunc(compilerOptions, inputProvider, 522 return compileFunc(
498 diagnosticHandler, outputProvider) 523 compilerOptions, inputProvider, diagnosticHandler, outputProvider)
499 .then(compilationDone); 524 .then(compilationDone);
500 } 525 }
501 526
502 class AbortLeg { 527 class AbortLeg {
503 final message; 528 final message;
504 AbortLeg(this.message); 529 AbortLeg(this.message);
505 toString() => 'Aborted due to --throw-on-error: $message'; 530 toString() => 'Aborted due to --throw-on-error: $message';
506 } 531 }
507 532
508 void writeString(Uri uri, String text) { 533 void writeString(Uri uri, String text) {
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
784 } else if (exitCode == 253) { 809 } else if (exitCode == 253) {
785 print(">>> TEST CRASH"); 810 print(">>> TEST CRASH");
786 } else { 811 } else {
787 print(">>> TEST FAIL"); 812 print(">>> TEST FAIL");
788 } 813 }
789 stderr.writeln(">>> EOF STDERR"); 814 stderr.writeln(">>> EOF STDERR");
790 subscription.resume(); 815 subscription.resume();
791 }); 816 });
792 }); 817 });
793 } 818 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698