findImpellerC function

Future<Uri> findImpellerC()

Locate the ImpellerC offline shader compiler in the engine artifacts cach directory.

Implementation

Future<Uri> findImpellerC() async {
  /////////////////////////////////////////////////////////////////////////////
  /// 1. If the `IMPELLERC` define is set at compile time, use it.
  ///

  const impellercDefine = String.fromEnvironment('IMPELLERC', defaultValue: '');
  if (impellercDefine != '') {
    logger.info('IMPELLERC compile-time define: `$impellercDefine`');
    if (!await File(impellercDefine).exists()) {
      throw Exception(
        'IMPELLERC compile-time define is set, but it doesn\'t point to a valid file!',
      );
    }
    return Uri.file(impellercDefine);
  }

  /////////////////////////////////////////////////////////////////////////////
  /// 2. If the `IMPELLERC` environment variable is set on the running
  ///    process, use it. flutter_tools populates this for build hook
  ///    subprocesses, including the `--local-engine` override case where
  ///    the locally built `impellerc` should be preferred over the SDK
  ///    cache. See https://github.com/flutter/flutter/pull/186300.
  ///

  final impellercEnvVar = Platform.environment['IMPELLERC'];
  if (impellercEnvVar != null && impellercEnvVar.isNotEmpty) {
    logger.info('IMPELLERC environment variable: `$impellercEnvVar`');
    if (!await File(impellercEnvVar).exists()) {
      throw Exception(
        'IMPELLERC environment variable is set, but it doesn\'t point to a valid file!',
      );
    }
    return Uri.file(impellercEnvVar);
  }

  /////////////////////////////////////////////////////////////////////////////
  /// 3. Search for the `impellerc` binary within the host-specific artifacts.
  ///

  Uri engineArtifactsDir = findEngineArtifactsDir();

  // No need to get fancy. Just search all the possible directories rather than
  // picking the correct one for the specific host type.
  Uri? found;
  List<Uri> tried = [];
  logger.info('Searching for impellerc in artifacts directories...');
  for (final variant in _impellercLocations) {
    logger.info('  Checking `$variant`...');
    final impellercPath = engineArtifactsDir.resolve(variant);
    if (await File(impellercPath.toFilePath()).exists()) {
      found = impellercPath;
      break;
    }
    tried.add(impellercPath);
  }
  if (found == null) {
    throw Exception(
      'Unable to find impellerc! Tried the following locations: $tried',
    );
  }

  return found;
}