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.
  ///

  Object? sdkFailure;
  try {
    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.
    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()) {
        return impellercPath;
      }
      tried.add(impellercPath);
    }
    sdkFailure = Exception(
      'Unable to find impellerc! Tried the following locations: $tried',
    );
  } on Exception catch (error) {
    sdkFailure = error;
  }

  /////////////////////////////////////////////////////////////////////////////
  /// 4. With no Flutter SDK to take it from, download the pinned build, but
  ///    only when the build asked for that by setting [pinnedImpellerc].
  ///

  final pin = pinnedImpellerc;
  if (pin == null) {
    throw Exception(
      '$sdkFailure\nA build with no Flutter SDK can download a pinned '
      'impellerc instead. That is off by default; the package whose hook '
      'compiles the shaders documents the `hooks: user_defines:` key that '
      'turns it on.',
    );
  }
  return fetchPinnedImpellerC(pin);
}