Skip to content

Commit

Permalink
[SDK] Adds dart2native arguments for gen_snapshot & AOT runtime
Browse files Browse the repository at this point in the history
Example:
  # sdk (x64)
  $ ./tools/build.py -a x64 -m product create_sdk
  $ DART2NATIVE_X64=out/ProductX64/dart-sdk/bin/dart2native
  ninja -C out/ProductX64 create_sdk
  [...]
  The build took XXX seconds

  # gen_snapshot (simarm)
  $ ./tools/build.py -a simarm -m product copy_gen_snapshot
  ninja -C out/ProductSIMARM copy_gen_snapshot
  [...]
  The build took XXX seconds
  $ GEN_SNAPSHOT_SIMARM=out/ProductSIMARM/dart-sdk/bin/utils/gen_snapshot
  $ file $GEN_SNAPSHOT_SIMARM
  [...]/gen_snapshot: ELF 32-bit LSB executable, Intel 80386, [...]

  # dartaotruntime (arm)
  $ ./tools/build.py -a arm -m product copy_dartaotruntime
  ninja -C out/ProductXARM copy_dartaotruntime
  [...]
  The build took XXX seconds
  $ DARTAOTRUNTIME_XARM=out/ProductXARM/dart-sdk/bin/dartaotruntime
  $ file $DARTAOTRUNTIME_XARM
  [...]/dartaotruntime: ELF 32-bit LSB shared object, ARM, [...]

  # test
  $ cat cpu.dart
  import 'dart:io';

  void main() {
    final lines = File('/proc/cpuinfo').readAsLinesSync();
    final model = lines.firstWhere((line) => line.startsWith('model name'));
    print(model.split(':').last.trim());
  }

  # x64
  $ $DART2NATIVE_X64 cpu.dart -o cpu_x64
  Generated: [...]/cpu_x64
  $ file cpu_x64
  cpu_x64: ELF 64-bit LSB shared object, x86-64, [...]
  $ ./cpu_x64
  Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz

  # arm
  $ $DART2NATIVE_X64 cpu.dart --gen-snapshot $GEN_SNAPSHOT_SIMARM --aot-runtime $DARTAOTRUNTIME_XARM -o cpu_arm
  Generated: [...]/cpu_arm
  $ file cpu_arm
  cpu_arm: ELF 32-bit LSB shared object, ARM, [...]
  # -> rpi
  pi@raspberrypi:~ $ ./cpu_arm
  ARMv7 Processor rev 3 (v7l)
  • Loading branch information
jpnurmi committed Sep 6, 2020
1 parent f8295fa commit 9625310
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 10 additions & 0 deletions pkg/dart2native/bin/dart2native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ 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.
''')
..addOption('gen-snapshot', valueHelp: 'path', hide: true, help: '''
Override the gen_snapshot tool. <path> can be relative or absolute.
E.g.: dart2native main.dart -g /tmp/dart-sdk-simarm/bin/utils/gen_snapshot
''')
..addOption('aot-runtime', valueHelp: 'path', hide: true, help: '''
Override the AOT runtime. <path> can be relative or absolute.
E.g.: dart2native main.dart -r /tmp/dart-sdk-arm/bin/dartaotruntime
''')
..addOption('enable-experiment',
defaultsTo: '', valueHelp: 'feature', hide: true, help: '''
Expand Down Expand Up @@ -106,6 +114,8 @@ Comma separated list of experimental features.
enableExperiment: parsedArgs['enable-experiment'],
enableAsserts: parsedArgs['enable-asserts'],
verbose: parsedArgs['verbose'],
overrideGenSnapshot: parsedArgs['gen-snapshot'],
overrideDartaotruntime: parsedArgs['aot-runtime'],
extraOptions: parsedArgs['extra-gen-snapshot-options']);
} catch (e) {
stderr.writeln('Failed to generate native files:');
Expand Down
14 changes: 11 additions & 3 deletions pkg/dart2native/lib/generate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Future<void> generateNative({
String enableExperiment = '',
bool enableAsserts = false,
bool verbose = false,
String overrideGenSnapshot,
String overrideDartaotruntime,
List<String> extraOptions = const [],
}) async {
final Directory tempDir = Directory.systemTemp.createTempSync();
Expand Down Expand Up @@ -69,8 +71,13 @@ Future<void> generateNative({
final String snapshotFile = (outputKind == Kind.aot
? outputPath
: path.join(tempDir.path, 'snapshot.aot'));
final snapshotResult = await generateAotSnapshot(genSnapshot, kernelFile,
snapshotFile, debugPath, enableAsserts, extraOptions);
final snapshotResult = await generateAotSnapshot(
overrideGenSnapshot ?? genSnapshot,
kernelFile,
snapshotFile,
debugPath,
enableAsserts,
extraOptions);
if (snapshotResult.exitCode != 0) {
stderr.writeln(snapshotResult.stdout);
stderr.writeln(snapshotResult.stderr);
Expand All @@ -82,7 +89,8 @@ Future<void> generateNative({
if (verbose) {
print('Generating executable.');
}
await writeAppendedExecutable(dartaotruntime, snapshotFile, outputPath);
await writeAppendedExecutable(
overrideDartaotruntime ?? dartaotruntime, snapshotFile, outputPath);

if (Platform.isLinux || Platform.isMacOS) {
if (verbose) {
Expand Down

0 comments on commit 9625310

Please sign in to comment.