materializeAppleBinary function

Future<AppleBinaryVerification> materializeAppleBinary({
  1. required File source,
  2. required File destination,
  3. required String architecture,
  4. required String diagnosticContext,
  5. AppleCommandRunner runner = _runAppleCommand,
})

Implementation

Future<AppleBinaryVerification> materializeAppleBinary({
  required File source,
  required File destination,
  required String architecture,
  required String diagnosticContext,
  AppleCommandRunner runner = _runAppleCommand,
}) async {
  if (destination.existsSync()) destination.deleteSync();

  final sourceArchitectures = await inspectAppleBinaryArchitectures(
    source,
    runner: runner,
  );
  if (!sourceArchitectures.contains(architecture)) {
    throw AppleBinaryException(
      '$diagnosticContext: requested architecture $architecture is not '
      'available in ${source.path}; detected architectures: '
      '${sourceArchitectures.toList()..sort()}',
    );
  }

  if (sourceArchitectures.length > 1) {
    final result = await runner('xcrun', [
      'lipo',
      source.path,
      '-thin',
      architecture,
      '-output',
      destination.path,
    ]);
    if (result.exitCode != 0) {
      throw AppleBinaryException(
        '$diagnosticContext: lipo failed while selecting $architecture from '
        '${source.path}: ${result.stderr}',
      );
    }
  } else {
    source.copySync(destination.path);
  }

  final outputArchitectures = await inspectAppleBinaryArchitectures(
    destination,
    runner: runner,
  );
  if (outputArchitectures.length != 1 ||
      !outputArchitectures.contains(architecture)) {
    throw AppleBinaryException(
      '$diagnosticContext: output ${destination.path} has architectures '
      '${outputArchitectures.toList()..sort()}, expected only $architecture',
    );
  }

  final verification = await runner('xcrun', [
    'lipo',
    destination.path,
    '-verify_arch',
    architecture,
  ]);
  if (verification.exitCode != 0) {
    throw AppleBinaryException(
      '$diagnosticContext: lipo verification failed for $architecture at '
      '${destination.path}: ${verification.stderr}',
    );
  }
  return AppleBinaryVerification(
    sourceArchitectures: sourceArchitectures,
    outputArchitectures: outputArchitectures,
  );
}