|
3 | 3 | // for details. All rights reserved. Use of this source code is governed by a
|
4 | 4 | // BSD-style license that can be found in the LICENSE file.
|
5 | 5 |
|
| 6 | +import 'dart:io'; |
| 7 | + |
6 | 8 | import 'package:args/args.dart';
|
7 |
| -import 'dart2aot.dart'; |
| 9 | +import 'package:dart2native/dart2native.dart'; |
| 10 | +import 'package:path/path.dart' as path; |
8 | 11 |
|
9 |
| -typedef void Command(ArgResults args, List<String> ds); |
| 12 | +final String executableSuffix = Platform.isWindows ? '.exe' : ''; |
| 13 | +final String snapshotDir = path.dirname(Platform.script.toFilePath()); |
| 14 | +final String binDir = path.canonicalize(path.join(snapshotDir, '..')); |
| 15 | +final String sdkDir = path.canonicalize(path.join(binDir, '..')); |
| 16 | +final String dart = path.join(binDir, 'dart${executableSuffix}'); |
| 17 | +final String genKernel = path.join(snapshotDir, 'gen_kernel.dart.snapshot'); |
| 18 | +final String dartaotruntime = |
| 19 | + path.join(binDir, 'dartaotruntime${executableSuffix}'); |
| 20 | +final String genSnapshot = |
| 21 | + path.join(binDir, 'utils', 'gen_snapshot${executableSuffix}'); |
| 22 | +final String platformDill = |
| 23 | + path.join(sdkDir, 'lib', '_internal', 'vm_platform_strong.dill'); |
10 | 24 |
|
11 |
| -void main(List<String> args) { |
12 |
| - Map<String, Command> commands = <String, Command>{}; |
13 |
| - commands['aot'] = callAOT; |
| 25 | +Future<void> generateNative(Kind kind, String sourceFile, String outputFile, |
| 26 | + String packages, List<String> defines, bool enableAsserts) async { |
| 27 | + final Directory tempDir = Directory.systemTemp.createTempSync(); |
| 28 | + try { |
| 29 | + final String kernelFile = path.join(tempDir.path, 'kernel.dill'); |
| 30 | + final String snapshotFile = (kind == Kind.aot |
| 31 | + ? outputFile |
| 32 | + : path.join(tempDir.path, 'snapshot.aot')); |
14 | 33 |
|
15 |
| - // Read -D args that the ArgParser can't handle. |
16 |
| - List<String> ds = []; |
17 |
| - args = filterDArgs(args, ds); |
| 34 | + print('Generating AOT kernel dill.'); |
| 35 | + final kernelResult = await generateAotKernel(dart, genKernel, platformDill, |
| 36 | + sourceFile, kernelFile, packages, defines); |
| 37 | + if (kernelResult.exitCode != 0) { |
| 38 | + stderr.writeln(kernelResult.stdout); |
| 39 | + stderr.writeln(kernelResult.stderr); |
| 40 | + await stderr.flush(); |
| 41 | + throw 'Generating AOT kernel dill failed!'; |
| 42 | + } |
18 | 43 |
|
19 |
| - ArgParser parser = ArgParser(); |
20 |
| - parser.addFlag('help'); |
21 |
| - ArgParser aotParser = parser.addCommand('aot'); |
22 |
| - setupAOTArgs(aotParser); |
| 44 | + print('Generating AOT snapshot.'); |
| 45 | + final snapshotResult = await generateAotSnapshot( |
| 46 | + genSnapshot, kernelFile, snapshotFile, enableAsserts); |
| 47 | + if (snapshotResult.exitCode != 0) { |
| 48 | + stderr.writeln(snapshotResult.stdout); |
| 49 | + stderr.writeln(snapshotResult.stderr); |
| 50 | + await stderr.flush(); |
| 51 | + throw 'Generating AOT snapshot failed!'; |
| 52 | + } |
23 | 53 |
|
24 |
| - ArgResults result = null; |
25 |
| - try { |
26 |
| - result = parser.parse(args); |
27 |
| - } catch (ArgParserException) { |
28 |
| - // We handle this case as result == null below. |
29 |
| - } |
| 54 | + if (kind == Kind.exe) { |
| 55 | + print('Generating executable.'); |
| 56 | + await writeAppendedExecutable(dartaotruntime, snapshotFile, outputFile); |
30 | 57 |
|
31 |
| - if (result == null || result.command == null || result['help']) { |
32 |
| - print('dart2native <command> <args>\n'); |
33 |
| - print(' command: '); |
34 |
| - print(' aot - Compile script into one ahead of time dart snapshot'); |
35 |
| - return; |
36 |
| - } |
| 58 | + if (Platform.isLinux || Platform.isMacOS) { |
| 59 | + print('Marking binary executable.'); |
| 60 | + await markExecutable(outputFile); |
| 61 | + } |
| 62 | + } |
37 | 63 |
|
38 |
| - if (commands.containsKey(result.command.name)) { |
39 |
| - commands[result.command.name](result.command, ds); |
40 |
| - return; |
| 64 | + print('Generated: ${outputFile}'); |
| 65 | + } finally { |
| 66 | + tempDir.deleteSync(recursive: true); |
41 | 67 | }
|
42 | 68 | }
|
43 | 69 |
|
44 |
| -void callAOT(ArgResults args, List<String> ds) { |
45 |
| - List<String> rest = args.rest; |
46 |
| - if (rest.length != 2) { |
47 |
| - print( |
48 |
| - 'Usage: dart2native aot [options] <dart-source-file> <dart-aot-file>\n'); |
49 |
| - print( |
50 |
| - 'Dart AOT (ahead-of-time) compile Dart source code into native machine code.'); |
51 |
| - return; |
| 70 | +void printUsage(final ArgParser parser) { |
| 71 | + print('Usage: dart2native <dart-script-file> [<options>]'); |
| 72 | + print(''); |
| 73 | + print('Generates an executable or an AOT snapshot from <dart-script-file>.'); |
| 74 | + print(''); |
| 75 | + print(parser.usage); |
| 76 | +} |
| 77 | + |
| 78 | +Future<void> main(List<String> args) async { |
| 79 | + final ArgParser parser = ArgParser() |
| 80 | + ..addMultiOption('define', |
| 81 | + abbr: 'D', |
| 82 | + valueHelp: 'key=value', |
| 83 | + help: 'The values for the environment constants.') |
| 84 | + ..addFlag('enable-asserts', |
| 85 | + negatable: false, help: 'Enable assert statements.') |
| 86 | + ..addFlag('help', |
| 87 | + abbr: 'h', negatable: false, help: 'Displays the help message.') |
| 88 | + ..addOption('output', |
| 89 | + abbr: 'o', valueHelp: 'path', help: 'Path to output file.') |
| 90 | + ..addOption('output-kind', |
| 91 | + abbr: 'k', |
| 92 | + allowed: ['exe', 'aot'], |
| 93 | + defaultsTo: 'exe', |
| 94 | + valueHelp: 'exe|aot', |
| 95 | + help: |
| 96 | + 'Whether to generate a stand-alone executable or an AOT snapshot.') |
| 97 | + ..addOption('packages', |
| 98 | + valueHelp: 'path', help: 'Where to find a package spec file.'); |
| 99 | + |
| 100 | + final ArgResults parsedArgs = parser.parse(args); |
| 101 | + |
| 102 | + if (parsedArgs['help']) { |
| 103 | + printUsage(parser); |
| 104 | + exit(0); |
52 | 105 | }
|
53 | 106 |
|
54 |
| - aot(rest[0], rest[1], args['build-elf'], args['enable-asserts'], args['tfa'], |
55 |
| - args['no-tfa'], args['packages'], ds); |
56 |
| -} |
| 107 | + if (parsedArgs.rest.length != 1) { |
| 108 | + printUsage(parser); |
| 109 | + exit(1); |
| 110 | + } |
57 | 111 |
|
58 |
| -List<String> filterDArgs(List<String> args, List<String> ds) { |
59 |
| - List<String> result = <String>[]; |
| 112 | + final Kind kind = { |
| 113 | + 'aot': Kind.aot, |
| 114 | + 'exe': Kind.exe, |
| 115 | + }[parsedArgs['output-kind']]; |
60 | 116 |
|
61 |
| - args.forEach((String arg) { |
62 |
| - if (!arg.startsWith('-D')) { |
63 |
| - result.add(arg); |
64 |
| - } else { |
65 |
| - ds.add(arg); |
66 |
| - } |
67 |
| - }); |
| 117 | + final sourcePath = path.canonicalize(path.normalize(parsedArgs.rest[0])); |
| 118 | + final outputPath = |
| 119 | + path.canonicalize(path.normalize(parsedArgs['output'] != null |
| 120 | + ? parsedArgs['output'] |
| 121 | + : { |
| 122 | + Kind.aot: '${sourcePath}.aot', |
| 123 | + Kind.exe: '${sourcePath}.exe', |
| 124 | + }[kind])); |
68 | 125 |
|
69 |
| - return result; |
| 126 | + if (!FileSystemEntity.isFileSync(sourcePath)) { |
| 127 | + stderr.writeln( |
| 128 | + '"${sourcePath}" is not a file. See \'--help\' for more information.'); |
| 129 | + await stderr.flush(); |
| 130 | + exit(1); |
| 131 | + } |
| 132 | + |
| 133 | + try { |
| 134 | + await generateNative(kind, sourcePath, outputPath, parsedArgs['packages'], |
| 135 | + parsedArgs['define'], parsedArgs['enable-asserts']); |
| 136 | + } catch (e) { |
| 137 | + stderr.writeln('Failed to generate native files:'); |
| 138 | + stderr.writeln(e); |
| 139 | + await stderr.flush(); |
| 140 | + exit(1); |
| 141 | + } |
70 | 142 | }
|
0 commit comments