build_ios_framework_module_test.dart 12.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
xster's avatar
xster committed
2 3 4 5 6 7
// 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:flutter_devicelab/framework/framework.dart';
8
import 'package:flutter_devicelab/framework/ios.dart';
9
import 'package:flutter_devicelab/framework/task_result.dart';
xster's avatar
xster committed
10 11 12
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;

13
/// Tests that iOS .xcframeworks can be built.
xster's avatar
xster committed
14 15 16 17 18 19 20 21
Future<void> main() async {
  await task(() async {

    section('Create module project');

    final Directory tempDir = Directory.systemTemp.createTempSync('flutter_module_test.');
    try {
      await inDirectory(tempDir, () async {
22
        section('Test module template');
23

24 25
        final Directory moduleProjectDir =
            Directory(path.join(tempDir.path, 'hello_module'));
26
        await flutter(
27
          'create',
28
          options: <String>[
29 30 31 32
            '--org',
            'io.flutter.devicelab',
            '--template',
            'module',
33
            'hello_module',
34 35 36
          ],
        );

37
        await _testBuildIosFramework(moduleProjectDir, isModule: true);
xster's avatar
xster committed
38

39
        section('Test app template');
40

41 42
        final Directory projectDir =
            Directory(path.join(tempDir.path, 'hello_project'));
xster's avatar
xster committed
43
        await flutter(
44 45
          'create',
          options: <String>['--org', 'io.flutter.devicelab', 'hello_project'],
46 47
        );

48 49
        await _testBuildIosFramework(projectDir);
      });
50

51 52 53 54 55 56
      return TaskResult.success(null);
    } on TaskResult catch (taskResult) {
      return taskResult;
    } catch (e) {
      return TaskResult.failure(e.toString());
    } finally {
57
      rmTree(tempDir);
58 59 60
    }
  });
}
61

62 63 64 65 66 67 68
Future<void> _testBuildIosFramework(Directory projectDir, { bool isModule = false}) async {
  section('Add plugins');

  final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml'));
  String content = pubspec.readAsStringSync();
  content = content.replaceFirst(
    '\ndependencies:\n',
69
    '\ndependencies:\n  package_info: 2.0.2\n  connectivity: 3.0.6\n',
70 71 72 73 74 75 76 77
  );
  pubspec.writeAsStringSync(content, flush: true);
  await inDirectory(projectDir, () async {
    await flutter(
      'packages',
      options: <String>['get'],
    );
  });
78

79 80
  // First, build the module in Debug to copy the debug version of Flutter.xcframework.
  // This proves "flutter build ios-framework" re-copies the relevant Flutter.xcframework,
81
  // otherwise building plugins with bitcode will fail linking because the debug version
82
  // of Flutter.xcframework does not contain bitcode.
83 84 85 86 87 88 89 90 91 92
  await inDirectory(projectDir, () async {
    await flutter(
      'build',
      options: <String>[
        'ios',
        '--debug',
        '--no-codesign',
      ],
    );
  });
93

94 95
  // This builds all build modes' frameworks by default
  section('Build frameworks');
96

97
  const String outputDirectoryName = 'flutter-frameworks';
98

99 100 101 102 103
  await inDirectory(projectDir, () async {
    await flutter(
      'build',
      options: <String>[
        'ios-framework',
104
        '--verbose',
105 106 107
        '--output=$outputDirectoryName',
        '--obfuscate',
        '--split-debug-info=symbols',
108 109 110
      ],
    );
  });
111

112 113
  final String outputPath = path.join(projectDir.path, outputDirectoryName);

114 115 116 117 118 119 120 121 122 123
  // TODO(jmagman): Remove ios-arm64_armv7 checks when armv7 engine artifacts are removed.
  final String arm64FlutterFramework = path.join(
    outputPath,
    'Debug',
    'Flutter.xcframework',
    'ios-arm64',
    'Flutter.framework',
  );

  final String armv7FlutterFramework = path.join(
124 125 126
    outputPath,
    'Debug',
    'Flutter.xcframework',
127
    'ios-arm64_armv7',
128
    'Flutter.framework',
129 130 131 132 133 134 135
  );

  final bool arm64FlutterBinaryExists = exists(File(path.join(arm64FlutterFramework, 'Flutter')));
  final bool armv7FlutterBinaryExists = exists(File(path.join(armv7FlutterFramework, 'Flutter')));
  if (!arm64FlutterBinaryExists && !armv7FlutterBinaryExists) {
    throw TaskResult.failure('Expected debug Flutter engine artifact binary to exist');
  }
136

137 138 139 140
  final String debugAppFrameworkPath = path.join(
    outputPath,
    'Debug',
    'App.xcframework',
141
    'ios-arm64',
142 143 144 145 146
    'App.framework',
    'App',
  );
  checkFileExists(debugAppFrameworkPath);

147 148 149 150
  checkFileExists(path.join(
    outputPath,
    'Debug',
    'App.xcframework',
151
    'ios-arm64',
152 153 154 155
    'App.framework',
    'Info.plist',
  ));

156 157
  section('Check debug build has Dart snapshot as asset');

158 159 160 161
  checkFileExists(path.join(
    outputPath,
    'Debug',
    'App.xcframework',
162
    'ios-arm64_x86_64-simulator',
163
    'App.framework',
164 165
    'flutter_assets',
    'vm_snapshot_data',
166 167
  ));

168 169 170 171 172 173 174 175
  section('Check obfuscation symbols');

  checkFileExists(path.join(
    projectDir.path,
    'symbols',
    'app.ios-arm64.symbols',
  ));

176 177
  section('Check debug build has no Dart AOT');

178
  final String aotSymbols = await _dylibSymbols(debugAppFrameworkPath);
179 180 181 182 183 184

  if (aotSymbols.contains('architecture') ||
      aotSymbols.contains('_kDartVmSnapshot')) {
    throw TaskResult.failure('Debug App.framework contains AOT');
  }

185 186 187 188 189 190
  section('Check profile, release builds has Dart AOT dylib');

  for (final String mode in <String>['Profile', 'Release']) {
    final String appFrameworkPath = path.join(
      outputPath,
      mode,
191
      'App.xcframework',
192
      'ios-arm64',
193 194 195 196 197 198
      'App.framework',
      'App',
    );

    await _checkBitcode(appFrameworkPath, mode);

199
    final String aotSymbols = await _dylibSymbols(appFrameworkPath);
200 201 202 203

    if (!aotSymbols.contains('_kDartVmSnapshot')) {
      throw TaskResult.failure('$mode App.framework missing Dart AOT');
    }
204

205 206 207 208
    checkFileNotExists(path.join(
      outputPath,
      mode,
      'App.xcframework',
209
      'ios-arm64',
210
      'App.framework',
211 212
      'flutter_assets',
      'vm_snapshot_data',
213 214
    ));

215
    checkFileExists(path.join(
216 217 218
      outputPath,
      mode,
      'App.xcframework',
219
      'ios-arm64_x86_64-simulator',
220 221 222
      'App.framework',
      'App',
    ));
223 224 225 226 227

    checkFileExists(path.join(
      outputPath,
      mode,
      'App.xcframework',
228
      'ios-arm64_x86_64-simulator',
229 230 231
      'App.framework',
      'Info.plist',
    ));
232 233 234 235 236
  }

  section("Check all modes' engine dylib");

  for (final String mode in <String>['Debug', 'Profile', 'Release']) {
237 238 239 240 241 242 243 244 245 246 247
    // TODO(jmagman): Remove ios-arm64_armv7 checks when armv7 engine artifacts are removed.
    final String arm64EngineBinary = path.join(
      outputPath,
      mode,
      'Flutter.xcframework',
      'ios-arm64',
      'Flutter.framework',
      'Flutter',
    );

    final String arm64Armv7EngineBinary = path.join(
248 249
      outputPath,
      mode,
250
      'Flutter.xcframework',
251
      'ios-arm64_armv7',
252 253 254 255
      'Flutter.framework',
      'Flutter',
    );

256 257 258 259 260 261 262
    if (exists(File(arm64EngineBinary))) {
      await _checkBitcode(arm64EngineBinary, mode);
    } else if (exists(File(arm64Armv7EngineBinary))) {
      await _checkBitcode(arm64Armv7EngineBinary, mode);
    } else {
      throw TaskResult.failure('Expected Flutter $mode engine artifact binary to exist');
    }
263

264
    checkFileExists(path.join(
265 266 267
      outputPath,
      mode,
      'Flutter.xcframework',
268
      'ios-arm64_x86_64-simulator',
269
      'Flutter.framework',
270 271
      'Flutter',
    ));
272

273
    checkFileExists(path.join(
274 275 276
      outputPath,
      mode,
      'Flutter.xcframework',
277
      'ios-arm64_x86_64-simulator',
278
      'Flutter.framework',
279 280 281
      'Headers',
      'Flutter.h',
    ));
282 283 284 285 286 287 288 289
  }

  section('Check all modes have plugins');

  for (final String mode in <String>['Debug', 'Profile', 'Release']) {
    final String pluginFrameworkPath = path.join(
      outputPath,
      mode,
290
      'connectivity.xcframework',
291
      'ios-arm64',
292 293
      'connectivity.framework',
      'connectivity',
294 295
    );
    await _checkBitcode(pluginFrameworkPath, mode);
296 297 298 299 300 301 302 303
    if (!await _linksOnFlutter(pluginFrameworkPath)) {
      throw TaskResult.failure('$pluginFrameworkPath does not link on Flutter');
    }

    final String transitiveDependencyFrameworkPath = path.join(
      outputPath,
      mode,
      'Reachability.xcframework',
304
      'ios-arm64_armv7',
305 306 307 308 309 310
      'Reachability.framework',
      'Reachability',
    );
    if (await _linksOnFlutter(transitiveDependencyFrameworkPath)) {
      throw TaskResult.failure('Transitive dependency $transitiveDependencyFrameworkPath unexpectedly links on Flutter');
    }
311 312 313 314

    checkFileExists(path.join(
      outputPath,
      mode,
315
      'connectivity.xcframework',
316
      'ios-arm64',
317
      'connectivity.framework',
318
      'Headers',
319
      'FLTConnectivityPlugin.h',
320 321
    ));

322 323 324 325
    if (mode != 'Debug') {
      checkDirectoryExists(path.join(
        outputPath,
        mode,
326
        'connectivity.xcframework',
327
        'ios-arm64',
328
        'dSYMs',
329
        'connectivity.framework.dSYM',
330 331 332
      ));
    }

333 334 335
    final String simulatorFrameworkPath = path.join(
      outputPath,
      mode,
336
      'connectivity.xcframework',
337
      'ios-arm64_x86_64-simulator',
338 339
      'connectivity.framework',
      'connectivity',
340 341 342 343 344
    );

    final String simulatorFrameworkHeaderPath = path.join(
      outputPath,
      mode,
345
      'connectivity.xcframework',
346
      'ios-arm64_x86_64-simulator',
347
      'connectivity.framework',
348
      'Headers',
349
      'FLTConnectivityPlugin.h',
350 351
    );

352 353
    checkFileExists(simulatorFrameworkPath);
    checkFileExists(simulatorFrameworkHeaderPath);
354
  }
355

356 357 358
  checkDirectoryExists(path.join(
    outputPath,
    'Release',
359
    'connectivity.xcframework',
360
    'ios-arm64',
361 362 363
    'BCSymbolMaps',
  ));

364
  section('Check all modes have generated plugin registrant');
365

366 367 368 369 370 371 372
  for (final String mode in <String>['Debug', 'Profile', 'Release']) {
    if (!isModule) {
      continue;
    }
    final String registrantFrameworkPath = path.join(
      outputPath,
      mode,
373
      'FlutterPluginRegistrant.xcframework',
374
      'ios-arm64',
375
      'FlutterPluginRegistrant.framework',
376
      'FlutterPluginRegistrant',
377 378 379 380 381 382 383
    );
    await _checkBitcode(registrantFrameworkPath, mode);

    checkFileExists(path.join(
      outputPath,
      mode,
      'FlutterPluginRegistrant.xcframework',
384
      'ios-arm64',
385 386 387 388 389 390 391 392
      'FlutterPluginRegistrant.framework',
      'Headers',
      'GeneratedPluginRegistrant.h',
    ));
    final String simulatorHeaderPath = path.join(
      outputPath,
      mode,
      'FlutterPluginRegistrant.xcframework',
393
      'ios-arm64_x86_64-simulator',
394 395 396 397
      'FlutterPluginRegistrant.framework',
      'Headers',
      'GeneratedPluginRegistrant.h',
    );
398
    checkFileExists(simulatorHeaderPath);
399 400 401 402 403 404 405 406 407 408 409 410 411 412
  }

  // This builds all build modes' frameworks by default
  section('Build podspec');

  const String cocoapodsOutputDirectoryName = 'flutter-frameworks-cocoapods';

  await inDirectory(projectDir, () async {
    await flutter(
      'build',
      options: <String>[
        'ios-framework',
        '--cocoapods',
        '--force', // Allow podspec creation on master.
413
        '--output=$cocoapodsOutputDirectoryName',
414 415 416
      ],
    );
  });
417

418 419 420 421 422 423 424 425 426 427 428
  final String cocoapodsOutputPath = path.join(projectDir.path, cocoapodsOutputDirectoryName);
  for (final String mode in <String>['Debug', 'Profile', 'Release']) {
    checkFileExists(path.join(
      cocoapodsOutputPath,
      mode,
      'Flutter.podspec',
    ));

    checkDirectoryExists(path.join(
      cocoapodsOutputPath,
      mode,
429
      'App.xcframework',
430 431 432
    ));

    if (Directory(path.join(
433 434
          cocoapodsOutputPath,
          mode,
435
          'FlutterPluginRegistrant.xcframework',
436 437 438
        )).existsSync() !=
        isModule) {
      throw TaskResult.failure(
439
          'Unexpected FlutterPluginRegistrant.xcframework.');
440
    }
441

442 443 444
    checkDirectoryExists(path.join(
      cocoapodsOutputPath,
      mode,
445
      'package_info.xcframework',
446
    ));
447 448 449 450 451 452 453 454 455 456 457 458

    checkDirectoryExists(path.join(
      cocoapodsOutputPath,
      mode,
      'connectivity.xcframework',
    ));

    checkDirectoryExists(path.join(
      cocoapodsOutputPath,
      mode,
      'Reachability.xcframework',
    ));
459
  }
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
  if (File(path.join(
        outputPath,
        'GeneratedPluginRegistrant.h',
      )).existsSync() ==
      isModule) {
    throw TaskResult.failure('Unexpected GeneratedPluginRegistrant.h.');
  }

  if (File(path.join(
        outputPath,
        'GeneratedPluginRegistrant.m',
      )).existsSync() ==
      isModule) {
    throw TaskResult.failure('Unexpected GeneratedPluginRegistrant.m.');
  }
xster's avatar
xster committed
476
}
477

478 479 480 481 482 483 484 485
Future<void> _checkBitcode(String frameworkPath, String mode) async {
  checkFileExists(frameworkPath);

  // Bitcode only needed in Release mode for archiving.
  if (mode == 'Release' && !await containsBitcode(frameworkPath)) {
    throw TaskResult.failure('$frameworkPath does not contain bitcode');
  }
}
486 487 488 489 490 491 492 493 494

Future<String> _dylibSymbols(String pathToDylib) {
  return eval('nm', <String>[
    '-g',
    pathToDylib,
    '-arch',
    'arm64',
  ]);
}
495 496 497 498 499 500 501 502 503 504

Future<bool> _linksOnFlutter(String pathToBinary) async {
  final String loadCommands = await eval('otool', <String>[
    '-l',
    '-arch',
    'arm64',
    pathToBinary,
  ]);
  return loadCommands.contains('Flutter.framework');
}