blob: ed18daf72a2b95120f86af2489dbc595897b04ef [file] [log] [blame]
#!/usr/bin/env dart
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:args/args.dart';
import 'package:dart2native/generate.dart';
import 'package:front_end/src/api_prototype/compiler_options.dart'
show Verbosity;
void printUsage(final ArgParser parser) {
Usage: dart2native <main-dart-file> [<options>]
Generates an executable or an AOT snapshot from <main-dart-file>.
Future<void> main(List<String> args) async {
// If we're outputting to a terminal, wrap usage text to that width.
int outputLineWidth;
try {
outputLineWidth = stdout.terminalColumns;
} catch (_) {/* Ignore. */}
final ArgParser parser = ArgParser(usageLineLength: outputLineWidth)
..addMultiOption('define', abbr: 'D', valueHelp: 'key=value', help: '''
Define an environment declaration. To specify multiple declarations, use multiple options or use commas to separate key-value pairs.
E.g.: dart2native -Da=1,b=2 main.dart''')
negatable: false, help: 'Enable assert statements.')
help: 'Pass additional options to gen_snapshot.',
hide: true,
valueHelp: 'opt1,opt2,...',
abbr: 'h', negatable: false, help: 'Display this help message.')
abbr: 'k',
allowed: ['aot', 'exe'],
allowedHelp: {
'aot': 'Generate an AOT snapshot.',
'exe': 'Generate a standalone executable.',
defaultsTo: 'exe',
valueHelp: 'aot|exe',
..addOption('output', abbr: 'o', valueHelp: 'path', help: '''
Set the output filename. <path> can be relative or absolute.
E.g.: dart2native main.dart -o ../bin/my_app.exe
..addOption('packages', abbr: 'p', valueHelp: 'path', help: '''
Get package locations from the specified file instead of .packages. <path> can be relative or absolute.
E.g.: dart2native --packages=/tmp/pkgs main.dart
..addOption('save-debugging-info', abbr: 'S', valueHelp: 'path', help: '''
Remove debugging information from the output and save it separately to the specified file. <path> can be relative or absolute.
defaultsTo: '', valueHelp: 'feature', hide: true, help: '''
Comma separated list of experimental features.
help: 'Respect the nullability of types at runtime.', defaultsTo: null)
abbr: 'v', negatable: false, help: 'Show verbose output.')
defaultsTo: Verbosity.defaultValue,
help: '''
Sets the verbosity level used for filtering messages during compilation.
allowed: Verbosity.allowedValues,
allowedHelp: Verbosity.allowedValuesHelp,
ArgResults parsedArgs;
try {
parsedArgs = parser.parse(args);
} on FormatException catch (e) {
stderr.writeln('Error: ${e.message}');
await stderr.flush();
if (parsedArgs['help']) {
if ( != 1) {
final String sourceFile =[0];
if (!FileSystemEntity.isFileSync(sourceFile)) {
'"${sourceFile}" is not a file. See \'--help\' for more information.');
await stderr.flush();
try {
await generateNative(
kind: parsedArgs['output-kind'],
sourceFile: sourceFile,
outputFile: parsedArgs['output'],
debugFile: parsedArgs['save-debugging-info'],
packages: parsedArgs['packages'],
defines: parsedArgs['define'],
enableExperiment: parsedArgs['enable-experiment'],
enableAsserts: parsedArgs['enable-asserts'],
soundNullSafety: parsedArgs['sound-null-safety'],
verbose: parsedArgs['verbose'],
verbosity: parsedArgs['verbosity'],
extraOptions: parsedArgs['extra-gen-snapshot-options']);
} catch (e) {
stderr.writeln('Failed to generate native files:');
await stderr.flush();