build_ios_framework_module_test.dart 11.5 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 33
            '--org',
            'io.flutter.devicelab',
            '--template',
            'module',
            '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 69 70 71 72 73 74 75 76 77
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',
    '\ndependencies:\n  device_info: 0.4.1\n  package_info: 0.4.0+9\n',
  );
  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 108
        '--output=$outputDirectoryName'
      ],
    );
  });
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  final String outputPath = path.join(projectDir.path, outputDirectoryName);

  // Xcode changed the name of this generated directory in Xcode 12.
  const String xcode11ArmDirectoryName = 'ios-armv7_arm64';
  const String xcode12ArmDirectoryName = 'ios-arm64_armv7';

  final String xcode11AppFrameworkDirectory = path.join(
    outputPath,
    'Debug',
    'App.xcframework',
    xcode11ArmDirectoryName,
    'App.framework',
    'App',
  );
  final String xcode12AppFrameworkDirectory = path.join(
    outputPath,
    'Debug',
    'App.xcframework',
    xcode12ArmDirectoryName,
    'App.framework',
    'App',
  );

133 134
  // Based on the locally installed version of Xcode.
  String localXcodeArmDirectoryName;
135
  if (exists(File(xcode11AppFrameworkDirectory))) {
136
    localXcodeArmDirectoryName = xcode11ArmDirectoryName;
137
  } else if (exists(File(xcode12AppFrameworkDirectory))) {
138
    localXcodeArmDirectoryName = xcode12ArmDirectoryName;
139 140 141 142
  } else {
    throw const FileSystemException('Expected App.framework binary to exist.');
  }

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  final String xcode11FlutterFrameworkDirectory = path.join(
    outputPath,
    'Debug',
    'Flutter.xcframework',
    xcode11ArmDirectoryName,
    'Flutter.framework',
    'Flutter',
  );
  final String xcode12FlutterFrameworkDirectory = path.join(
    outputPath,
    'Debug',
    'Flutter.xcframework',
    xcode12ArmDirectoryName,
    'Flutter.framework',
    'Flutter',
  );

  // Based on the version of Xcode installed on the engine builder.
  String builderXcodeArmDirectoryName;
  if (exists(File(xcode11FlutterFrameworkDirectory))) {
    builderXcodeArmDirectoryName = xcode11ArmDirectoryName;
  } else if (exists(File(xcode12FlutterFrameworkDirectory))) {
    builderXcodeArmDirectoryName = xcode12ArmDirectoryName;
  } else {
    throw const FileSystemException('Expected Flutter.framework binary to exist.');
  }

170 171 172 173 174 175 176 177 178 179 180 181
  final String debugAppFrameworkPath = path.join(
    outputPath,
    'Debug',
    'App.xcframework',
    localXcodeArmDirectoryName,
    'App.framework',
    'App',
  );
  checkFileExists(debugAppFrameworkPath);

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

182 183 184 185 186 187
  checkFileExists(path.join(
    outputPath,
    'Debug',
    'App.xcframework',
    'ios-x86_64-simulator',
    'App.framework',
188 189
    'flutter_assets',
    'vm_snapshot_data',
190 191
  ));

192 193
  section('Check debug build has no Dart AOT');

194
  final String aotSymbols = await _dylibSymbols(debugAppFrameworkPath);
195 196 197 198 199 200

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

201 202 203 204 205 206
  section('Check profile, release builds has Dart AOT dylib');

  for (final String mode in <String>['Profile', 'Release']) {
    final String appFrameworkPath = path.join(
      outputPath,
      mode,
207 208
      'App.xcframework',
      localXcodeArmDirectoryName,
209 210 211 212 213 214
      'App.framework',
      'App',
    );

    await _checkBitcode(appFrameworkPath, mode);

215
    final String aotSymbols = await _dylibSymbols(appFrameworkPath);
216 217 218 219

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

221 222 223 224
    checkFileNotExists(path.join(
      outputPath,
      mode,
      'App.xcframework',
225
      localXcodeArmDirectoryName,
226
      'App.framework',
227 228
      'flutter_assets',
      'vm_snapshot_data',
229 230
    ));

231
    checkFileExists(path.join(
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
      outputPath,
      mode,
      'App.xcframework',
      'ios-x86_64-simulator',
      'App.framework',
      'App',
    ));
  }

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

  for (final String mode in <String>['Debug', 'Profile', 'Release']) {
    final String engineFrameworkPath = path.join(
      outputPath,
      mode,
247 248
      'Flutter.xcframework',
      builderXcodeArmDirectoryName,
249 250 251 252 253 254 255 256 257 258
      'Flutter.framework',
      'Flutter',
    );

    await _checkBitcode(engineFrameworkPath, mode);

    checkFileExists(path.join(
      outputPath,
      mode,
      'Flutter.xcframework',
259
      'ios-x86_64-simulator',
260 261 262
      'Flutter.framework',
      'Flutter',
    ));
263

264
    checkFileExists(path.join(
265 266 267 268 269
      outputPath,
      mode,
      'Flutter.xcframework',
      'ios-x86_64-simulator',
      'Flutter.framework',
270 271
      'Headers',
      'Flutter.h',
272
    ));
273 274 275 276 277 278 279 280 281
  }

  section('Check all modes have plugins');

  for (final String mode in <String>['Debug', 'Profile', 'Release']) {
    final String pluginFrameworkPath = path.join(
      outputPath,
      mode,
      'device_info.xcframework',
282
      localXcodeArmDirectoryName,
283 284
      'device_info.framework',
      'device_info',
285 286
    );
    await _checkBitcode(pluginFrameworkPath, mode);
287 288 289 290 291

    checkFileExists(path.join(
      outputPath,
      mode,
      'device_info.xcframework',
292
      localXcodeArmDirectoryName,
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
      'device_info.framework',
      'Headers',
      'DeviceInfoPlugin.h',
    ));

    final String simulatorFrameworkPath = path.join(
      outputPath,
      mode,
      'device_info.xcframework',
      'ios-x86_64-simulator',
      'device_info.framework',
      'device_info',
    );

    final String simulatorFrameworkHeaderPath = path.join(
      outputPath,
      mode,
      'device_info.xcframework',
      'ios-x86_64-simulator',
      'device_info.framework',
      'Headers',
      'DeviceInfoPlugin.h',
    );

317 318
    checkFileExists(simulatorFrameworkPath);
    checkFileExists(simulatorFrameworkHeaderPath);
319
  }
320

321
  section('Check all modes have generated plugin registrant');
322

323 324 325 326 327 328 329
  for (final String mode in <String>['Debug', 'Profile', 'Release']) {
    if (!isModule) {
      continue;
    }
    final String registrantFrameworkPath = path.join(
      outputPath,
      mode,
330 331
      'FlutterPluginRegistrant.xcframework',
      localXcodeArmDirectoryName,
332
      'FlutterPluginRegistrant.framework',
333
      'FlutterPluginRegistrant',
334 335 336 337 338 339 340
    );
    await _checkBitcode(registrantFrameworkPath, mode);

    checkFileExists(path.join(
      outputPath,
      mode,
      'FlutterPluginRegistrant.xcframework',
341
      localXcodeArmDirectoryName,
342 343 344 345 346 347 348 349 350 351 352 353 354
      'FlutterPluginRegistrant.framework',
      'Headers',
      'GeneratedPluginRegistrant.h',
    ));
    final String simulatorHeaderPath = path.join(
      outputPath,
      mode,
      'FlutterPluginRegistrant.xcframework',
      'ios-x86_64-simulator',
      'FlutterPluginRegistrant.framework',
      'Headers',
      'GeneratedPluginRegistrant.h',
    );
355
    checkFileExists(simulatorHeaderPath);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  }

  // 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.
        '--output=$cocoapodsOutputDirectoryName'
      ],
    );
  });
374

375 376 377 378 379 380 381 382 383 384 385
  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,
386
      'App.xcframework',
387 388 389
    ));

    if (Directory(path.join(
390 391
          cocoapodsOutputPath,
          mode,
392
          'FlutterPluginRegistrant.xcframework',
393 394 395
        )).existsSync() !=
        isModule) {
      throw TaskResult.failure(
396
          'Unexpected FlutterPluginRegistrant.xcframework.');
397
    }
398

399 400 401
    checkDirectoryExists(path.join(
      cocoapodsOutputPath,
      mode,
402
      'device_info.xcframework',
403
    ));
404

405 406 407
    checkDirectoryExists(path.join(
      cocoapodsOutputPath,
      mode,
408
      'package_info.xcframework',
409 410
    ));
  }
411

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
  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
427
}
428

429 430 431 432 433 434 435 436
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');
  }
}
437 438 439 440 441 442 443 444 445

Future<String> _dylibSymbols(String pathToDylib) {
  return eval('nm', <String>[
    '-g',
    pathToDylib,
    '-arch',
    'arm64',
  ]);
}