verify_exported.dart 5.0 KB
Newer Older
1 2 3 4
import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as p;
5
import 'package:collection/collection.dart' show MapEquality;
6 7 8 9 10 11 12 13 14 15 16

// This script verifies that the release binaries only export the expected
// symbols.
//
// Android binaries (libflutter.so) should only export one symbol "JNI_OnLoad"
// of type "T".
//
// iOS binaries (Flutter.framework/Flutter) should only export Objective-C
// Symbols from the Flutter namespace. These are either of type
// "(__DATA,__common)" or "(__DATA,__objc_data)".

17 18 19 20 21
/// Takes the path to the out directory as the first argument, and the path to
/// the buildtools directory as the second argument.
///
/// If the second argument is not specified, it is assumed that it is the parent
/// of the out directory (for backwards compatibility).
22
void main(List<String> arguments) {
23
  assert(arguments.length == 2 || arguments.length == 1);
24
  final String outPath = arguments.first;
25 26 27 28
  final String buildToolsPath = arguments.length == 1
      ? p.join(p.dirname(outPath), 'buildtools')
      : arguments[1];

29 30 31 32 33 34
  String platform;
  if (Platform.isLinux) {
    platform = 'linux-x64';
  } else if (Platform.isMacOS) {
    platform = 'mac-x64';
  } else {
D
Dan Field 已提交
35
    throw UnimplementedError('Script only support running on Linux or MacOS.');
36
  }
37
  final String nmPath = p.join(buildToolsPath, platform, 'clang', 'bin', 'llvm-nm');
38 39
  assert(new Directory(outPath).existsSync());

D
Dan Field 已提交
40
  final Iterable<String> releaseBuilds = Directory(outPath).listSync()
41
      .where((FileSystemEntity entity) => entity is Directory)
42
      .map<String>((FileSystemEntity dir) => p.basename(dir.path))
43 44 45 46 47 48 49 50
      .where((String s) => s.contains('_release'));

  final Iterable<String> iosReleaseBuilds = releaseBuilds
      .where((String s) => s.startsWith('ios_'));
  final Iterable<String> androidReleaseBuilds = releaseBuilds
      .where((String s) => s.startsWith('android_'));

  int failures = 0;
51 52
  failures += _checkIos(outPath, nmPath, iosReleaseBuilds);
  failures += _checkAndroid(outPath, nmPath, androidReleaseBuilds);
53 54
  print('Failing checks: $failures');
  exit(failures);
55 56
}

57
int _checkIos(String outPath, String nmPath, Iterable<String> builds) {
58 59 60
  int failures = 0;
  for (String build in builds) {
    final String libFlutter = p.join(outPath, build, 'Flutter.framework', 'Flutter');
61 62 63 64
    if (!new File(libFlutter).existsSync()) {
      print('SKIPPING: $libFlutter does not exist.');
      continue;
    }
65 66 67 68 69 70 71
    final ProcessResult nmResult = Process.runSync(nmPath, <String>['-gUm', libFlutter]);
    if (nmResult.exitCode != 0) {
      print('ERROR: failed to execute "nm -gUm $libFlutter":\n${nmResult.stderr}');
      failures++;
      continue;
    }
    final Iterable<NmEntry> unexpectedEntries = NmEntry.parse(nmResult.stdout).where((NmEntry entry) {
72
      return !(((entry.type == '(__DATA,__common)' || entry.type == '(__DATA,__const)') && entry.name.startsWith('_Flutter'))
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
          || (entry.type == '(__DATA,__objc_data)'
              && (entry.name.startsWith('_OBJC_METACLASS_\$_Flutter') || entry.name.startsWith('_OBJC_CLASS_\$_Flutter'))));
    });
    if (unexpectedEntries.isNotEmpty) {
      print('ERROR: $libFlutter exports unexpected symbols:');
      print(unexpectedEntries.fold<String>('', (String previous, NmEntry entry) {
        return '${previous == '' ? '' : '$previous\n'}     ${entry.type} ${entry.name}';
      }));
      failures++;
    } else {
      print('OK: $libFlutter');
    }
  }
  return failures;
}

89
int _checkAndroid(String outPath, String nmPath, Iterable<String> builds) {
90 91 92
  int failures = 0;
  for (String build in builds) {
    final String libFlutter = p.join(outPath, build, 'libflutter.so');
93 94 95 96
    if (!new File(libFlutter).existsSync()) {
      print('SKIPPING: $libFlutter does not exist.');
      continue;
    }
97 98 99 100 101 102 103
    final ProcessResult nmResult = Process.runSync(nmPath, <String>['-gU', libFlutter]);
    if (nmResult.exitCode != 0) {
      print('ERROR: failed to execute "nm -gU $libFlutter":\n${nmResult.stderr}');
      failures++;
      continue;
    }
    final Iterable<NmEntry> entries = NmEntry.parse(nmResult.stdout);
D
Dan Field 已提交
104
    final Map<String, String> entryMap = Map<String, String>.fromIterable(
105
        entries,
D
Dan Field 已提交
106 107 108
        key: (dynamic entry) => entry.name,
        value: (dynamic entry) => entry.type);
    final Map<String, String> expectedSymbols = <String, String>{
109 110 111 112
      'JNI_OnLoad': 'T',
      '_binary_icudtl_dat_size': 'A',
      '_binary_icudtl_dat_start': 'D',
    };
D
Dan Field 已提交
113
    if (!const MapEquality<String, String>().equals(entryMap, expectedSymbols)) {
114 115 116
      print('ERROR: $libFlutter exports the wrong symbols');
      print(' Expected $expectedSymbols');
      print(' Library has $entryMap.');
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      failures++;
    } else {
      print('OK: $libFlutter');
    }
  }
  return failures;
}

class NmEntry {
  NmEntry._(this.address, this.type, this.name);

  final String address;
  final String type;
  final String name;

  static Iterable<NmEntry> parse(String stdout) {
    return LineSplitter.split(stdout).map((String line) {
      final List<String> parts = line.split(' ');
D
Dan Field 已提交
135
      return NmEntry._(parts[0], parts[1], parts.last);
136 137 138
    });
  }
}