Skip to content

Commit dd4f341

Browse files
alexmarkovCommit Queue
authored andcommitted
[dart2bytecode] Add API to invoke dart2bytecode programmatically
TEST=ci Bug: b/394206952 Change-Id: Icd606c83bfed335e45498fceed6add784c465826 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/407820 Reviewed-by: Jia Hao Goh <[email protected]> Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent c19f913 commit dd4f341

File tree

2 files changed

+78
-23
lines changed

2 files changed

+78
-23
lines changed

pkg/dart2bytecode/lib/dart2bytecode.dart

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ ${_argParser.usage}
9797
''';
9898

9999
Future<void> main(List<String> arguments) async {
100-
io.exitCode = await runCompiler(_argParser.parse(arguments));
100+
io.exitCode = await runCompilerWithCommandLineArguments(arguments);
101101
}
102102

103-
/// Run bytecode compiler tool with given [options]
104-
/// and return exit code.
105-
Future<int> runCompiler(ArgResults options) async {
103+
/// Run bytecode compiler tool with given [arguments]
104+
/// and return exit code (0 on success, non-zero on failure).
105+
Future<int> runCompilerWithCommandLineArguments(List<String> arguments) async {
106+
final ArgResults options = _argParser.parse(arguments);
106107
final String? platformKernel = options['platform'];
107108

108109
if (options['help']) {
@@ -131,6 +132,58 @@ Future<int> runCompiler(ArgResults options) async {
131132
return badUsageExitCode;
132133
}
133134

135+
final String? importDill = options['import-dill'];
136+
final String? validateDynamicInterface = options['validate'];
137+
final String messageVerbosity = options['verbosity'];
138+
final String cfeInvocationModes = options['invocation-modes'];
139+
final bool trackWidgetCreation = options['track-widget-creation'];
140+
final List<String>? bytecodeGeneratorOptions = options['bytecode-options'];
141+
142+
return await runCompilerWithOptions(
143+
input: input,
144+
platformKernel: platformKernel,
145+
outputFileName: outputFileName,
146+
targetName: targetName,
147+
packages: packages,
148+
importDill: importDill,
149+
validateDynamicInterface: validateDynamicInterface,
150+
enableAsserts: enableAsserts,
151+
experimentalFlags: experimentalFlags,
152+
environmentDefines: environmentDefines,
153+
fileSystemScheme: fileSystemScheme,
154+
fileSystemRoots: fileSystemRoots,
155+
messageVerbosity: messageVerbosity,
156+
cfeInvocationModes: cfeInvocationModes,
157+
trackWidgetCreation: trackWidgetCreation,
158+
bytecodeGeneratorOptions: bytecodeGeneratorOptions,
159+
depfile: depfile,
160+
depfileTarget: depfileTarget,
161+
);
162+
}
163+
164+
/// Run bytecode compiler tool with given options
165+
/// and return exit code (0 on success, non-zero on failure).
166+
Future<int> runCompilerWithOptions({
167+
required String input,
168+
required String platformKernel,
169+
required String outputFileName,
170+
required String targetName,
171+
String? packages,
172+
String? importDill,
173+
String? validateDynamicInterface,
174+
bool enableAsserts = false,
175+
List<String>? experimentalFlags,
176+
Map<String, String> environmentDefines = const {},
177+
String? fileSystemScheme,
178+
List<String>? fileSystemRoots,
179+
String messageVerbosity = Verbosity.defaultValue,
180+
void Function(String) printMessage = print,
181+
String cfeInvocationModes = '',
182+
bool trackWidgetCreation = false,
183+
List<String>? bytecodeGeneratorOptions,
184+
String? depfile,
185+
String? depfileTarget,
186+
}) async {
134187
final fileSystem =
135188
createFrontEndFileSystem(fileSystemScheme, fileSystemRoots);
136189

@@ -139,17 +192,17 @@ Future<int> runCompiler(ArgResults options) async {
139192
final platformKernelUri = Uri.base.resolveUri(new Uri.file(platformKernel));
140193

141194
final List<Uri> additionalDills = <Uri>[];
142-
final String? importDill = options['import-dill'];
143195
if (importDill != null) {
144196
additionalDills.add(Uri.base.resolveUri(new Uri.file(importDill)));
145197
}
146198

147-
final String? validate = options['validate'];
148199
final Uri? dynamicInterfaceSpecificationUri =
149-
(validate != null) ? resolveInputUri(validate) : null;
200+
(validateDynamicInterface != null)
201+
? resolveInputUri(validateDynamicInterface)
202+
: null;
150203

151-
final verbosity = Verbosity.parseArgument(options['verbosity']);
152-
final errorPrinter = ErrorPrinter(verbosity);
204+
final verbosity = Verbosity.parseArgument(messageVerbosity);
205+
final errorPrinter = ErrorPrinter(verbosity, println: printMessage);
153206
final errorDetector = ErrorDetector(previousErrorHandler: errorPrinter.call);
154207

155208
Uri mainUri = resolveInputUri(input);
@@ -165,20 +218,18 @@ Future<int> runCompiler(ArgResults options) async {
165218
..dynamicInterfaceSpecificationUri = dynamicInterfaceSpecificationUri
166219
..explicitExperimentalFlags = parseExperimentalFlags(
167220
parseExperimentalArguments(experimentalFlags),
168-
onError: print)
221+
onError: printMessage)
169222
..onDiagnostic = (DiagnosticMessage m) {
170223
errorDetector(m);
171224
}
172225
..embedSourceText = false
173-
..invocationModes =
174-
InvocationMode.parseArguments(options['invocation-modes'])
226+
..invocationModes = InvocationMode.parseArguments(cfeInvocationModes)
175227
..verbosity = verbosity;
176228

177229
compilerOptions.target = createFrontEndTarget(targetName,
178-
trackWidgetCreation: options['track-widget-creation'],
179-
supportMirrors: false);
230+
trackWidgetCreation: trackWidgetCreation, supportMirrors: false);
180231
if (compilerOptions.target == null) {
181-
print('Failed to create front-end target $targetName.');
232+
printMessage('Failed to create front-end target $targetName.');
182233
return badUsageExitCode;
183234
}
184235

@@ -187,7 +238,7 @@ Future<int> runCompiler(ArgResults options) async {
187238
options: compilerOptions,
188239
requireMain: false,
189240
includePlatform: false,
190-
environmentDefines: environmentDefines,
241+
environmentDefines: Map.of(environmentDefines),
191242
enableAsserts: enableAsserts));
192243

193244
errorPrinter.printCompilationMessages();
@@ -199,7 +250,7 @@ Future<int> runCompiler(ArgResults options) async {
199250

200251
final BytecodeOptions bytecodeOptions =
201252
BytecodeOptions(enableAsserts: enableAsserts)
202-
..parseCommandLineFlags(options['bytecode-options']);
253+
..parseCommandLineFlags(bytecodeGeneratorOptions);
203254

204255
if (bytecodeOptions.showBytecodeSizeStatistics) {
205256
BytecodeSizeStatistics.reset();

pkg/vm/lib/kernel_front_end.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ class KernelCompilationResults {
433433
final Iterable<Uri>? compiledSources;
434434
final Uri? usedPackageConfig;
435435

436-
KernelCompilationResults(this.component, this.loadedLibraries,
436+
KernelCompilationResults(
437+
this.component,
438+
this.loadedLibraries,
437439
this.classHierarchy,
438440
this.coreTypes,
439441
this.compiledSources,
@@ -540,9 +542,9 @@ Future<KernelCompilationResults> compileToKernel(
540542
compilerResult = await CompilerContext.runWithOptions(processedOptions,
541543
(CompilerContext context) async {
542544
return args.requireMain
543-
? await kernelForProgram(args.source!, options,
544-
additionalSources: args.additionalSources)
545-
: await kernelForModule(
545+
? await kernelForProgram(args.source!, options,
546+
additionalSources: args.additionalSources)
547+
: await kernelForModule(
546548
[args.source!, ...args.additionalSources], options);
547549
});
548550
usedPackageConfig = await processedOptions.resolvePackagesFileUri();
@@ -724,8 +726,10 @@ class ErrorPrinter {
724726
final DiagnosticMessageHandler? previousErrorHandler;
725727
final Map<Uri?, List<DiagnosticMessage>> compilationMessages =
726728
<Uri?, List<DiagnosticMessage>>{};
729+
final void Function(String) println;
727730

728-
ErrorPrinter(this.verbosity, {this.previousErrorHandler});
731+
ErrorPrinter(this.verbosity,
732+
{this.previousErrorHandler, this.println = print});
729733

730734
void call(DiagnosticMessage message) {
731735
final sourceUri = getMessageUri(message);
@@ -750,7 +754,7 @@ class ErrorPrinter {
750754
for (final Uri? sourceUri in sortedUris) {
751755
for (final DiagnosticMessage message in compilationMessages[sourceUri]!) {
752756
if (Verbosity.shouldPrint(verbosity, message)) {
753-
printDiagnosticMessage(message, print);
757+
printDiagnosticMessage(message, println);
754758
}
755759
}
756760
}

0 commit comments

Comments
 (0)