fetchPinnedImpellerC function

Future<Uri> fetchPinnedImpellerC(
  1. PinnedImpellerc pin
)

Downloads pin's impellerc and its shader_lib for this host, checks the archive against its pinned hash, and returns the compiler. A build that already ran reuses its copy.

Implementation

// TODO(pinned-impellerc): run this on Linux and Windows. It has run against
// the real archive on macOS arm64, and the other hosts' archives were only
// listed to confirm the same layout.
Future<Uri> fetchPinnedImpellerC(PinnedImpellerc pin) async {
  final host = hostArtifactsName();
  final expected = pin.archiveSha256[host];
  if (expected == null) {
    throw Exception(
      'No pinned impellerc hash for $host at engine ${pin.engineRevision}.',
    );
  }
  final directory = Directory(
    '${impellercCacheDirectory().path}/${pin.engineRevision}/$host',
  );
  final executable = File(
    '${directory.path}/impellerc${Platform.isWindows ? '.exe' : ''}',
  );
  final complete = File('${directory.path}/.complete');
  if (complete.existsSync() && executable.existsSync()) {
    return executable.uri;
  }

  final url = pin.archiveUrl(host);
  logger.info('Downloading impellerc from $url');
  stderr.writeln('flutter_gpu_shaders: downloading impellerc from $url');
  final client = HttpClient();
  final List<int> bytes;
  try {
    final request = await client.getUrl(url);
    final response = await request.close();
    if (response.statusCode != 200) {
      throw Exception('Fetching $url failed with HTTP ${response.statusCode}.');
    }
    final builder = BytesBuilder(copy: false);
    await for (final chunk in response) {
      builder.add(chunk);
    }
    bytes = builder.takeBytes();
  } finally {
    client.close();
  }
  final actual = sha256.convert(bytes).toString();
  if (actual != expected) {
    throw Exception(
      'The impellerc archive at $url does not match its pinned hash '
      '(expected $expected, got $actual). Nothing was installed.',
    );
  }

  // Unpacked beside the final place and renamed in, so an interrupted build
  // never leaves half a compiler behind.
  final staging = Directory('${directory.path}.partial');
  if (staging.existsSync()) staging.deleteSync(recursive: true);
  staging.createSync(recursive: true);
  var foundCompiler = false;
  for (final entry in ZipDecoder().decodeBytes(bytes)) {
    if (!entry.isFile) continue;
    final name = entry.name;
    final isCompiler = name == 'impellerc' || name == 'impellerc.exe';
    if (!isCompiler && !name.startsWith('shader_lib/')) continue;
    if (name.contains('..')) continue;
    final out = File('${staging.path}/$name');
    out.parent.createSync(recursive: true);
    out.writeAsBytesSync(entry.content as List<int>);
    foundCompiler = foundCompiler || isCompiler;
  }
  if (!foundCompiler) {
    staging.deleteSync(recursive: true);
    throw Exception('The archive at $url holds no impellerc.');
  }
  if (!Platform.isWindows) {
    Process.runSync('chmod', ['+x', '${staging.path}/impellerc']);
  }
  File('${staging.path}/.complete').writeAsStringSync('$actual\n');
  if (directory.existsSync()) directory.deleteSync(recursive: true);
  directory.parent.createSync(recursive: true);
  staging.renameSync(directory.path);
  return executable.uri;
}