OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 import 'dart:async'; | |
5 import 'dart:io'; | |
6 | |
7 import 'package:args/args.dart'; | |
8 import 'package:bazel_worker/bazel_worker.dart'; | |
9 import 'package:front_end/front_end.dart' hide FileSystemException; | |
10 import 'package:front_end/src/fasta/command_line_reporting.dart'; | |
11 import 'package:kernel/target/targets.dart'; | |
12 | |
13 main(List<String> args) async { | |
14 args = preprocessArgs(args); | |
15 | |
16 if (args.contains('--persistent_worker')) { | |
17 if (args.length != 1) { | |
18 throw new StateError( | |
19 "unexpected args, expected only --persistent-worker but got: $args"); | |
20 } | |
21 await new SummaryWorker().run(); | |
22 } else { | |
23 await computeSummary(args); | |
24 } | |
25 } | |
26 | |
27 /// A bazel worker loop that can compute summaries. | |
28 class SummaryWorker extends AsyncWorkerLoop { | |
29 Future<WorkResponse> performRequest(WorkRequest request) async { | |
30 var outputBuffer = new StringBuffer(); | |
31 var response = new WorkResponse()..exitCode = 0; | |
32 try { | |
33 await computeSummary(request.arguments, | |
34 isWorker: true, outputBuffer: outputBuffer); | |
35 } catch (_, s) { | |
36 outputBuffer.writeln(s); | |
37 response.exitCode = 15; | |
38 } | |
39 response.output = outputBuffer.toString(); | |
40 return response; | |
41 } | |
42 } | |
43 | |
44 /// If the last arg starts with `@`, this reads the file it points to and treats | |
45 /// each line as an additional arg. | |
46 /// | |
47 /// This is how individual work request args are differentiated from startup | |
48 /// args in bazel (inidividual work request args go in that file). | |
49 List<String> preprocessArgs(List<String> args) { | |
50 args = new List.from(args); | |
51 if (args.isEmpty) { | |
52 return args; | |
53 } | |
54 String lastArg = args.last; | |
55 if (lastArg.startsWith('@')) { | |
56 File argsFile = new File(lastArg.substring(1)); | |
57 try { | |
58 args.removeLast(); | |
59 args.addAll(argsFile.readAsLinesSync()); | |
60 } on FileSystemException catch (e) { | |
61 throw new Exception('Failed to read file specified by $lastArg : $e'); | |
62 } | |
63 } | |
64 return args; | |
65 } | |
66 | |
67 /// An [ArgParser] for generating kernel summaries. | |
68 final summaryArgsParser = new ArgParser() | |
69 ..addOption('dart-sdk-summary') | |
70 ..addOption('input-summary', allowMultiple: true) | |
71 ..addOption('multi-root', allowMultiple: true) | |
72 ..addOption('packages-file') | |
73 ..addOption('source', allowMultiple: true) | |
74 ..addOption('output'); | |
75 | |
76 /// Computes a kernel summary based on [args]. | |
77 /// | |
78 /// If [isWorker] is true then exit codes will not be set on failure. | |
79 /// | |
80 /// If [outputBuffer] is provided then messages will be written to that buffer | |
81 /// instead of printed to the console. | |
82 Future computeSummary(List<String> args, | |
83 {bool isWorker: false, StringBuffer outputBuffer}) async { | |
84 var parsedArgs = summaryArgsParser.parse(args); | |
85 var options = new CompilerOptions() | |
86 ..packagesFileUri = Uri.parse(parsedArgs['packages-file']) | |
87 ..inputSummaries = parsedArgs['input-summary'].map(Uri.parse).toList() | |
88 ..sdkSummary = Uri.parse(parsedArgs['dart-sdk-summary']) | |
89 ..multiRoots = parsedArgs['multi-root'].map(Uri.parse).toList() | |
90 ..target = new NoneTarget(new TargetFlags()); | |
91 | |
92 if (outputBuffer != null) { | |
93 options.onError = (CompilationMessage error) { | |
94 var severityString = severityName(error.severity, capitalized: true); | |
95 outputBuffer.writeln('$severityString: ${error.message}'); | |
96 if (error.severity != Severity.nit) { | |
97 throw error; | |
98 } | |
99 }; | |
100 } else { | |
101 options.throwOnWarnings = true; | |
ahe
2017/08/24 11:14:35
This is an internal option that makes Fasta crash
| |
102 } | |
103 | |
104 var sources = parsedArgs['source'].map(Uri.parse).toList(); | |
105 var program = await summaryFor(sources, options); | |
106 | |
107 var outputFile = new File(parsedArgs['output']); | |
108 outputFile.createSync(recursive: true); | |
109 outputFile.writeAsBytesSync(program); | |
110 } | |
OLD | NEW |