artifacts.dart 45.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:file/memory.dart';
6
import 'package:process/process.dart';
7

8
import 'base/common.dart';
9
import 'base/file_system.dart';
10
import 'base/os.dart';
11
import 'base/platform.dart';
12
import 'base/utils.dart';
13
import 'build_info.dart';
14
import 'cache.dart';
15
import 'globals.dart' as globals;
16 17

enum Artifact {
18
  /// The tool which compiles a dart kernel file into native code.
19
  genSnapshot,
20
  /// The flutter tester binary.
21
  flutterTester,
22
  flutterFramework,
23
  flutterXcframework,
24
  /// The framework directory of the macOS desktop.
25
  flutterMacOSFramework,
26
  vmSnapshotData,
27
  isolateSnapshotData,
28
  icuData,
29 30 31 32
  platformKernelDill,
  platformLibrariesJson,
  flutterPatchedSdkPath,
  frontendServerSnapshotForEngineDartSdk,
33 34 35 36 37 38
  /// The root of the Linux desktop sources.
  linuxDesktopPath,
  // The root of the cpp headers for Linux desktop.
  linuxHeaders,
  /// The root of the Windows desktop sources.
  windowsDesktopPath,
39
  /// The root of the cpp client code for Windows desktop.
40
  windowsCppClientWrapper,
41 42
  /// The root of the cpp client code for Windows UWP desktop.
  windowsUwpCppClientWrapper,
43 44 45 46 47 48 49 50 51 52 53 54 55 56
  /// The root of the Windows UWP desktop sources.
  windowsUwpDesktopPath,
  /// The root of the sky_engine package.
  skyEnginePath,
  /// The location of the macOS engine podspec file.
  flutterMacOSPodspec,

  // Fuchsia artifacts from the engine prebuilts.
  fuchsiaKernelCompiler,
  fuchsiaFlutterRunner,

  /// Tools related to subsetting or icon font files.
  fontSubset,
  constFinder,
57 58 59

  // Windows UWP app management tool.
  uwptool,
60 61 62 63
}

/// A subset of [Artifact]s that are platform and build mode independent
enum HostArtifact {
64
  /// The root directory of the dart SDK.
65
  engineDartSdkPath,
66
  /// The dart binary used to execute any of the required snapshots.
67
  engineDartBinary,
68
  /// The dart snapshot of the dart2js compiler.
69
  dart2jsSnapshot,
70
  /// The dart snapshot of the dartdev compiler.
71
  dartdevcSnapshot,
72
  /// The dart snapshot of the kernel worker compiler.
73
  kernelWorkerSnapshot,
74
  /// The root of the web implementation of the dart SDK.
75
  flutterWebSdk,
76
  /// The libraries JSON file for web release builds.
77
  flutterWebLibrariesJson,
78 79
  /// The summary dill for the dartdevc target.
  webPlatformKernelDill,
80 81
  /// The summary dill with null safety enabled for the dartdevc target.
  webPlatformSoundKernelDill,
82

83 84 85 86 87
  /// The precompiled SDKs and sourcemaps for web debug builds.
  webPrecompiledSdk,
  webPrecompiledSdkSourcemaps,
  webPrecompiledCanvaskitSdk,
  webPrecompiledCanvaskitSdkSourcemaps,
88 89
  webPrecompiledCanvaskitAndHtmlSdk,
  webPrecompiledCanvaskitAndHtmlSdkSourcemaps,
90 91 92 93
  webPrecompiledSoundSdk,
  webPrecompiledSoundSdkSourcemaps,
  webPrecompiledCanvaskitSoundSdk,
  webPrecompiledCanvaskitSoundSdkSourcemaps,
94 95
  webPrecompiledCanvaskitAndHtmlSoundSdk,
  webPrecompiledCanvaskitAndHtmlSoundSdkSourcemaps,
96

97 98 99 100
  iosDeploy,
  idevicesyslog,
  idevicescreenshot,
  iproxy,
101
  /// The root of the sky_engine package.
102
  skyEnginePath,
103 104
}

105 106 107 108 109 110 111 112
// TODO(knopp): Remove once darwin artifacts are universal and moved out of darwin-x64
String _enginePlatformDirectoryName(TargetPlatform platform) {
  if (platform == TargetPlatform.darwin) {
    return 'darwin-x64';
  }
  return getNameForTargetPlatform(platform);
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
// Remove android target platform type.
TargetPlatform? _mapTargetPlatform(TargetPlatform? targetPlatform) {
  switch (targetPlatform) {
    case TargetPlatform.android:
      return TargetPlatform.android_arm64;
    case TargetPlatform.ios:
    case TargetPlatform.darwin:
    case TargetPlatform.linux_x64:
    case TargetPlatform.linux_arm64:
    case TargetPlatform.windows_x64:
    case TargetPlatform.windows_uwp_x64:
    case TargetPlatform.fuchsia_arm64:
    case TargetPlatform.fuchsia_x64:
    case TargetPlatform.tester:
    case TargetPlatform.web_javascript:
    case TargetPlatform.android_arm:
    case TargetPlatform.android_arm64:
    case TargetPlatform.android_x64:
    case TargetPlatform.android_x86:
    case null:
      return targetPlatform;
  }
}

bool _isWindows(TargetPlatform? platform) {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  switch (platform) {
    case TargetPlatform.windows_x64:
    case TargetPlatform.windows_uwp_x64:
      return true;
    case TargetPlatform.android:
    case TargetPlatform.android_arm:
    case TargetPlatform.android_arm64:
    case TargetPlatform.android_x64:
    case TargetPlatform.android_x86:
    case TargetPlatform.darwin:
    case TargetPlatform.fuchsia_arm64:
    case TargetPlatform.fuchsia_x64:
    case TargetPlatform.ios:
    case TargetPlatform.linux_arm64:
    case TargetPlatform.linux_x64:
    case TargetPlatform.tester:
    case TargetPlatform.web_javascript:
155
    case null:
156 157 158 159
      return false;
  }
}

160
String? _artifactToFileName(Artifact artifact, [ TargetPlatform? platform, BuildMode? mode ]) {
161
  final String exe = _isWindows(platform) ? '.exe' : '';
162 163
  switch (artifact) {
    case Artifact.genSnapshot:
164
      return 'gen_snapshot';
165
    case Artifact.flutterTester:
166
      return 'flutter_tester$exe';
167 168
    case Artifact.flutterFramework:
      return 'Flutter.framework';
169 170
    case Artifact.flutterXcframework:
      return 'Flutter.xcframework';
171 172
    case Artifact.flutterMacOSFramework:
      return 'FlutterMacOS.framework';
173 174 175 176
    case Artifact.vmSnapshotData:
      return 'vm_isolate_snapshot.bin';
    case Artifact.isolateSnapshotData:
      return 'isolate_snapshot.bin';
177 178
    case Artifact.icuData:
      return 'icudtl.dat';
179
    case Artifact.platformKernelDill:
180
      return 'platform_strong.dill';
181 182 183 184 185 186 187
    case Artifact.platformLibrariesJson:
      return 'libraries.json';
    case Artifact.flutterPatchedSdkPath:
      assert(false, 'No filename for sdk path, should not be invoked');
      return null;
    case Artifact.frontendServerSnapshotForEngineDartSdk:
      return 'frontend_server.dart.snapshot';
188 189
    case Artifact.linuxDesktopPath:
      return '';
190 191
    case Artifact.linuxHeaders:
      return 'flutter_linux';
192
    case Artifact.windowsCppClientWrapper:
193
    case Artifact.windowsUwpCppClientWrapper:
194
      return 'cpp_client_wrapper';
195 196 197
    case Artifact.windowsUwpDesktopPath:
    case Artifact.windowsDesktopPath:
      return '';
198 199
    case Artifact.skyEnginePath:
      return 'sky_engine';
200 201
    case Artifact.flutterMacOSPodspec:
      return 'FlutterMacOS.podspec';
202 203
    case Artifact.fuchsiaKernelCompiler:
      return 'kernel_compiler.snapshot';
204
    case Artifact.fuchsiaFlutterRunner:
205
      final String jitOrAot = mode!.isJit ? '_jit' : '_aot';
206 207
      final String productOrNo = mode.isRelease ? '_product' : '';
      return 'flutter$jitOrAot${productOrNo}_runner-0.far';
208 209 210 211
    case Artifact.fontSubset:
      return 'font-subset$exe';
    case Artifact.constFinder:
      return 'const_finder.dart.snapshot';
212 213
    case Artifact.uwptool:
      return 'uwptool$exe';
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
  }
}

String _hostArtifactToFileName(HostArtifact artifact, bool windows) {
  final String exe = windows ? '.exe' : '';
  switch (artifact) {
    case HostArtifact.flutterWebSdk:
      return '';
    case HostArtifact.engineDartSdkPath:
      return 'dart-sdk';
    case HostArtifact.engineDartBinary:
      return 'dart$exe';
    case HostArtifact.dart2jsSnapshot:
      return 'dart2js.dart.snapshot';
    case HostArtifact.dartdevcSnapshot:
      return 'dartdevc.dart.snapshot';
    case HostArtifact.kernelWorkerSnapshot:
      return 'kernel_worker.dart.snapshot';
    case HostArtifact.iosDeploy:
      return 'ios-deploy';
    case HostArtifact.idevicesyslog:
      return 'idevicesyslog';
    case HostArtifact.idevicescreenshot:
      return 'idevicescreenshot';
    case HostArtifact.iproxy:
      return 'iproxy';
    case HostArtifact.skyEnginePath:
      return 'sky_engine';
    case HostArtifact.webPlatformKernelDill:
      return 'flutter_ddc_sdk.dill';
    case HostArtifact.webPlatformSoundKernelDill:
      return 'flutter_ddc_sdk_sound.dill';
    case HostArtifact.flutterWebLibrariesJson:
247
      return 'libraries.json';
248 249 250 251 252 253
    case HostArtifact.webPrecompiledSdk:
    case HostArtifact.webPrecompiledCanvaskitSdk:
    case HostArtifact.webPrecompiledCanvaskitAndHtmlSdk:
    case HostArtifact.webPrecompiledSoundSdk:
    case HostArtifact.webPrecompiledCanvaskitSoundSdk:
    case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdk:
254
      return 'dart_sdk.js';
255 256 257 258 259 260
    case HostArtifact.webPrecompiledSdkSourcemaps:
    case HostArtifact.webPrecompiledCanvaskitSdkSourcemaps:
    case HostArtifact.webPrecompiledCanvaskitAndHtmlSdkSourcemaps:
    case HostArtifact.webPrecompiledSoundSdkSourcemaps:
    case HostArtifact.webPrecompiledCanvaskitSoundSdkSourcemaps:
    case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdkSourcemaps:
261
      return 'dart_sdk.js.map';
262 263 264
  }
}

265
class EngineBuildPaths {
266
  const EngineBuildPaths({
267 268
    required this.targetEngine,
    required this.hostEngine,
269 270
  }) : assert(targetEngine != null),
       assert(hostEngine != null);
271 272 273 274 275

  final String targetEngine;
  final String hostEngine;
}

276 277
// Manages the engine artifacts of Flutter.
abstract class Artifacts {
278 279
  /// A test-specific implementation of artifacts that returns stable paths for
  /// all artifacts.
280
  ///
281 282
  /// If a [fileSystem] is not provided, creates a new [MemoryFileSystem] instance.
  ///
283
  /// Creates a [LocalEngineArtifacts] if `localEngine` is non-null
284
  factory Artifacts.test({String? localEngine, FileSystem? fileSystem}) {
285
    fileSystem ??= MemoryFileSystem.test();
286
    if (localEngine != null) {
287
      return _TestLocalEngine(localEngine, fileSystem);
288
    }
289
    return _TestArtifacts(fileSystem);
290
  }
291

292
  static LocalEngineArtifacts getLocalEngine(EngineBuildPaths engineBuildPaths) {
293 294 295 296 297 298 299
    return LocalEngineArtifacts(
      engineBuildPaths.targetEngine,
      engineBuildPaths.hostEngine,
      cache: globals.cache,
      fileSystem: globals.fs,
      processManager: globals.processManager,
      platform: globals.platform,
300
      operatingSystemUtils: globals.os,
301
    );
302 303
  }

304 305 306
  /// Returns the requested [artifact] for the [platform], [mode], and [environmentType] combination.
  String getArtifactPath(
    Artifact artifact, {
307 308 309
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
310
  });
311

312 313 314 315 316 317
  /// Retrieve a host specific artifact that does not depend on the
  /// current build mode or environment.
  FileSystemEntity getHostArtifact(
    HostArtifact artifact,
  );

318 319
  // Returns which set of engine artifacts is currently used for the [platform]
  // and [mode] combination.
320
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]);
321 322 323

  /// Whether these artifacts correspond to a non-versioned local engine.
  bool get isLocalEngine;
324 325 326
}

/// Manages the engine artifacts downloaded to the local cache.
327
class CachedArtifacts implements Artifacts {
328
  CachedArtifacts({
329 330 331 332
    required FileSystem fileSystem,
    required Platform platform,
    required Cache cache,
    required OperatingSystemUtils operatingSystemUtils,
333 334
  }) : _fileSystem = fileSystem,
       _platform = platform,
335 336
       _cache = cache,
       _operatingSystemUtils = operatingSystemUtils;
337 338 339 340

  final FileSystem _fileSystem;
  final Platform _platform;
  final Cache _cache;
341
  final OperatingSystemUtils _operatingSystemUtils;
342

343 344 345 346 347 348
  @override
  FileSystemEntity getHostArtifact(
    HostArtifact artifact,
  ) {
    switch (artifact) {
      case HostArtifact.engineDartSdkPath:
349
        final String path = _dartSdkPath(_cache);
350 351
        return _fileSystem.directory(path);
      case HostArtifact.engineDartBinary:
352
        final String path = _fileSystem.path.join(_dartSdkPath(_cache), 'bin', _hostArtifactToFileName(artifact, _platform.isWindows));
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
        return _fileSystem.file(path);
      case HostArtifact.flutterWebSdk:
        final String path = _getFlutterWebSdkPath();
        return _fileSystem.directory(path);
      case HostArtifact.flutterWebLibrariesJson:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPlatformKernelDill:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPlatformSoundKernelDill:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSdk:
      case HostArtifact.webPrecompiledSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSdk:
      case HostArtifact.webPrecompiledCanvaskitSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdk:
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-html', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSoundSdk:
      case HostArtifact.webPrecompiledSoundSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-sound', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSoundSdk:
      case HostArtifact.webPrecompiledCanvaskitSoundSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-sound', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdk:
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-html-sound', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.idevicesyslog:
      case HostArtifact.idevicescreenshot:
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform.isWindows);
        return _cache.getArtifactDirectory('libimobiledevice').childFile(artifactFileName);
      case HostArtifact.skyEnginePath:
        final Directory dartPackageDirectory = _cache.getCacheDir('pkg');
        final String path = _fileSystem.path.join(dartPackageDirectory.path,  _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.directory(path);
      case HostArtifact.dart2jsSnapshot:
      case HostArtifact.dartdevcSnapshot:
      case HostArtifact.kernelWorkerSnapshot:
401
        final String path = _fileSystem.path.join(_dartSdkPath(_cache), 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform.isWindows));
402 403 404 405 406 407 408 409 410 411
        return _fileSystem.file(path);
      case HostArtifact.iosDeploy:
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform.isWindows);
        return _cache.getArtifactDirectory('ios-deploy').childFile(artifactFileName);
      case HostArtifact.iproxy:
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform.isWindows);
        return _cache.getArtifactDirectory('usbmuxd').childFile(artifactFileName);
    }
  }

412
  @override
413 414
  String getArtifactPath(
    Artifact artifact, {
415 416 417
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
418
  }) {
419
    platform = _mapTargetPlatform(platform);
420
    switch (platform) {
421
      case TargetPlatform.android:
422
      case TargetPlatform.android_arm:
423
      case TargetPlatform.android_arm64:
424 425
      case TargetPlatform.android_x64:
      case TargetPlatform.android_x86:
426
        assert(platform != TargetPlatform.android);
427
        return _getAndroidArtifactPath(artifact, platform!, mode!);
428
      case TargetPlatform.ios:
429
        return _getIosArtifactPath(artifact, platform!, mode, environmentType);
430
      case TargetPlatform.darwin:
431
      case TargetPlatform.linux_x64:
432
      case TargetPlatform.linux_arm64:
433
      case TargetPlatform.windows_x64:
434
      case TargetPlatform.windows_uwp_x64:
435
        return _getDesktopArtifactPath(artifact, platform, mode);
436 437
      case TargetPlatform.fuchsia_arm64:
      case TargetPlatform.fuchsia_x64:
438
        return _getFuchsiaArtifactPath(artifact, platform!, mode!);
439
      case TargetPlatform.tester:
440
      case TargetPlatform.web_javascript:
441
      case null:
442
        return _getHostArtifactPath(artifact, platform ?? _currentHostPlatform(_platform, _operatingSystemUtils), mode);
443 444 445 446
    }
  }

  @override
447 448
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]) {
    return _fileSystem.path.basename(_getEngineArtifactsPath(platform, mode)!);
449 450
  }

451
  String _getDesktopArtifactPath(Artifact artifact, TargetPlatform? platform, BuildMode? mode) {
452 453 454
    // When platform is null, a generic host platform artifact is being requested
    // and not the gen_snapshot for darwin as a target platform.
    if (platform != null && artifact == Artifact.genSnapshot) {
455
      final String engineDir = _getEngineArtifactsPath(platform, mode)!;
456
      return _fileSystem.path.join(engineDir, _artifactToFileName(artifact));
457
    }
458
    return _getHostArtifactPath(artifact, platform ?? _currentHostPlatform(_platform, _operatingSystemUtils), mode);
459 460
  }

461
  String _getAndroidArtifactPath(Artifact artifact, TargetPlatform platform, BuildMode mode) {
462
    final String engineDir = _getEngineArtifactsPath(platform, mode)!;
463 464 465
    switch (artifact) {
      case Artifact.genSnapshot:
        assert(mode != BuildMode.debug, 'Artifact $artifact only available in non-debug mode.');
466
        final String hostPlatform = getNameForHostPlatform(getCurrentHostPlatform());
467
        return _fileSystem.path.join(engineDir, hostPlatform, _artifactToFileName(artifact));
468
      case Artifact.frontendServerSnapshotForEngineDartSdk:
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
      case Artifact.constFinder:
      case Artifact.flutterFramework:
      case Artifact.flutterMacOSFramework:
      case Artifact.flutterMacOSPodspec:
      case Artifact.flutterPatchedSdkPath:
      case Artifact.flutterTester:
      case Artifact.flutterXcframework:
      case Artifact.fontSubset:
      case Artifact.fuchsiaFlutterRunner:
      case Artifact.fuchsiaKernelCompiler:
      case Artifact.icuData:
      case Artifact.isolateSnapshotData:
      case Artifact.linuxDesktopPath:
      case Artifact.linuxHeaders:
      case Artifact.platformKernelDill:
      case Artifact.platformLibrariesJson:
      case Artifact.skyEnginePath:
      case Artifact.uwptool:
      case Artifact.vmSnapshotData:
      case Artifact.windowsCppClientWrapper:
      case Artifact.windowsDesktopPath:
      case Artifact.windowsUwpCppClientWrapper:
      case Artifact.windowsUwpDesktopPath:
492
        return _getHostArtifactPath(artifact, platform, mode);
493 494 495
    }
  }

496
  String _getIosArtifactPath(Artifact artifact, TargetPlatform platform, BuildMode? mode, EnvironmentType? environmentType) {
497 498
    switch (artifact) {
      case Artifact.genSnapshot:
499
      case Artifact.flutterXcframework:
500 501
        final String artifactFileName = _artifactToFileName(artifact)!;
        final String engineDir = _getEngineArtifactsPath(platform, mode)!;
502
        return _fileSystem.path.join(engineDir, artifactFileName);
503
      case Artifact.flutterFramework:
504
        final String engineDir = _getEngineArtifactsPath(platform, mode)!;
505
        return _getIosEngineArtifactPath(engineDir, environmentType, _fileSystem);
506
      case Artifact.frontendServerSnapshotForEngineDartSdk:
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
      case Artifact.constFinder:
      case Artifact.flutterMacOSFramework:
      case Artifact.flutterMacOSPodspec:
      case Artifact.flutterPatchedSdkPath:
      case Artifact.flutterTester:
      case Artifact.fontSubset:
      case Artifact.fuchsiaFlutterRunner:
      case Artifact.fuchsiaKernelCompiler:
      case Artifact.icuData:
      case Artifact.isolateSnapshotData:
      case Artifact.linuxDesktopPath:
      case Artifact.linuxHeaders:
      case Artifact.platformKernelDill:
      case Artifact.platformLibrariesJson:
      case Artifact.skyEnginePath:
      case Artifact.uwptool:
      case Artifact.vmSnapshotData:
      case Artifact.windowsCppClientWrapper:
      case Artifact.windowsDesktopPath:
      case Artifact.windowsUwpCppClientWrapper:
      case Artifact.windowsUwpDesktopPath:
528
        return _getHostArtifactPath(artifact, platform, mode);
529 530 531
    }
  }

532
  String _getFuchsiaArtifactPath(Artifact artifact, TargetPlatform platform, BuildMode mode) {
533 534
    final String root = _fileSystem.path.join(
      _cache.getArtifactDirectory('flutter_runner').path,
535 536
      'flutter',
      fuchsiaArchForTargetPlatform(platform),
537
      mode.isRelease ? 'release' : mode.toString(),
538
    );
539
    final String runtime = mode.isJit ? 'jit' : 'aot';
540
    switch (artifact) {
541 542
      case Artifact.genSnapshot:
        final String genSnapshot = mode.isRelease ? 'gen_snapshot_product' : 'gen_snapshot';
543
        return _fileSystem.path.join(root, runtime, 'dart_binaries', genSnapshot);
544 545
      case Artifact.flutterPatchedSdkPath:
        const String artifactFileName = 'flutter_runner_patched_sdk';
546
        return _fileSystem.path.join(root, runtime, artifactFileName);
547
      case Artifact.platformKernelDill:
548
        final String artifactFileName = _artifactToFileName(artifact, platform, mode)!;
549
        return _fileSystem.path.join(root, runtime, 'flutter_runner_patched_sdk', artifactFileName);
550
      case Artifact.fuchsiaKernelCompiler:
551
        final String artifactFileName = _artifactToFileName(artifact, platform, mode)!;
552
        return _fileSystem.path.join(root, runtime, 'dart_binaries', artifactFileName);
553
      case Artifact.fuchsiaFlutterRunner:
554
        final String artifactFileName = _artifactToFileName(artifact, platform, mode)!;
555
        return _fileSystem.path.join(root, runtime, artifactFileName);
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
      case Artifact.constFinder:
      case Artifact.flutterFramework:
      case Artifact.flutterMacOSFramework:
      case Artifact.flutterMacOSPodspec:
      case Artifact.flutterTester:
      case Artifact.flutterXcframework:
      case Artifact.fontSubset:
      case Artifact.frontendServerSnapshotForEngineDartSdk:
      case Artifact.icuData:
      case Artifact.isolateSnapshotData:
      case Artifact.linuxDesktopPath:
      case Artifact.linuxHeaders:
      case Artifact.platformLibrariesJson:
      case Artifact.skyEnginePath:
      case Artifact.uwptool:
      case Artifact.vmSnapshotData:
      case Artifact.windowsCppClientWrapper:
      case Artifact.windowsDesktopPath:
      case Artifact.windowsUwpCppClientWrapper:
      case Artifact.windowsUwpDesktopPath:
576
        return _getHostArtifactPath(artifact, platform, mode);
577 578 579
    }
  }

580
  String _getFlutterPatchedSdkPath(BuildMode? mode) {
581 582
    final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
    return _fileSystem.path.join(engineArtifactsPath, 'common',
583
        mode == BuildMode.release ? 'flutter_patched_sdk_product' : 'flutter_patched_sdk');
584 585
  }

586
  String _getFlutterWebSdkPath() {
587
    return _cache.getWebSdkDirectory().path;
588 589
  }

590
  String _getHostArtifactPath(Artifact artifact, TargetPlatform platform, BuildMode? mode) {
591
    assert(platform != null);
592
    switch (artifact) {
593 594 595 596
      case Artifact.genSnapshot:
        // For script snapshots any gen_snapshot binary will do. Returning gen_snapshot for
        // android_arm in profile mode because it is available on all supported host platforms.
        return _getAndroidArtifactPath(artifact, TargetPlatform.android_arm, BuildMode.profile);
597 598 599 600 601
      case Artifact.frontendServerSnapshotForEngineDartSdk:
        return _fileSystem.path.join(
          _dartSdkPath(_cache), 'bin', 'snapshots',
          _artifactToFileName(artifact),
        );
602
      case Artifact.flutterTester:
603 604
      case Artifact.vmSnapshotData:
      case Artifact.isolateSnapshotData:
605
      case Artifact.icuData:
606
        final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
607
        final String platformDirName = _enginePlatformDirectoryName(platform);
608
        return _fileSystem.path.join(engineArtifactsPath, platformDirName, _artifactToFileName(artifact, platform, mode));
609
      case Artifact.platformKernelDill:
610
        return _fileSystem.path.join(_getFlutterPatchedSdkPath(mode), _artifactToFileName(artifact));
611
      case Artifact.platformLibrariesJson:
612
        return _fileSystem.path.join(_getFlutterPatchedSdkPath(mode), 'lib', _artifactToFileName(artifact));
613
      case Artifact.flutterPatchedSdkPath:
614
        return _getFlutterPatchedSdkPath(mode);
615
      case Artifact.flutterMacOSFramework:
616 617
      case Artifact.linuxDesktopPath:
      case Artifact.windowsDesktopPath:
618
      case Artifact.flutterMacOSPodspec:
619
      case Artifact.linuxHeaders:
620
        // TODO(zanderso): remove once debug desktop artifacts are uploaded
621 622
        // under a separate directory from the host artifacts.
        // https://github.com/flutter/flutter/issues/38935
623
        String platformDirName = _enginePlatformDirectoryName(platform);
624
        if (mode == BuildMode.profile || mode == BuildMode.release) {
625
          platformDirName = '$platformDirName-${getNameForBuildMode(mode!)}';
626
        }
627 628
        final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
        return _fileSystem.path.join(engineArtifactsPath, platformDirName, _artifactToFileName(artifact, platform, mode));
629 630
      case Artifact.windowsUwpDesktopPath:
        final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
631
        return _fileSystem.path.join(engineArtifactsPath, 'windows-uwp-x64-${getNameForBuildMode(mode!)}', _artifactToFileName(artifact, platform, mode));
632 633 634
      case Artifact.windowsCppClientWrapper:
        final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
        return _fileSystem.path.join(engineArtifactsPath, 'windows-x64', _artifactToFileName(artifact, platform, mode));
635 636 637
      case Artifact.windowsUwpCppClientWrapper:
        final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
        return _fileSystem.path.join(engineArtifactsPath, 'windows-uwp-x64-debug', _artifactToFileName(artifact, platform, mode));
638
      case Artifact.skyEnginePath:
639 640
        final Directory dartPackageDirectory = _cache.getCacheDir('pkg');
        return _fileSystem.path.join(dartPackageDirectory.path,  _artifactToFileName(artifact));
641 642
      case Artifact.fontSubset:
      case Artifact.constFinder:
643
        return _cache.getArtifactDirectory('engine')
644
                     .childDirectory(_enginePlatformDirectoryName(platform))
645
                     .childFile(_artifactToFileName(artifact, platform, mode)!)
646
                     .path;
647 648 649
      case Artifact.uwptool:
        return _cache.getArtifactDirectory('engine')
                     .childDirectory('windows-uwp-x64-${getNameForBuildMode(mode ?? BuildMode.debug)}')
650
                     .childFile(_artifactToFileName(artifact, platform, mode)!)
651
                     .path;
652 653 654 655
      case Artifact.flutterFramework:
      case Artifact.flutterXcframework:
      case Artifact.fuchsiaFlutterRunner:
      case Artifact.fuchsiaKernelCompiler:
656
        throw StateError('Artifact $artifact not available for platform $platform.');
657 658 659
    }
  }

660
  String? _getEngineArtifactsPath(TargetPlatform platform, [ BuildMode? mode ]) {
661
    final String engineDir = _cache.getArtifactDirectory('engine').path;
662
    final String platformName = _enginePlatformDirectoryName(platform);
663
    switch (platform) {
664
      case TargetPlatform.linux_x64:
665
      case TargetPlatform.linux_arm64:
666
      case TargetPlatform.darwin:
667
      case TargetPlatform.windows_x64:
668
        // TODO(zanderso): remove once debug desktop artifacts are uploaded
669 670 671
        // under a separate directory from the host artifacts.
        // https://github.com/flutter/flutter/issues/38935
        if (mode == BuildMode.debug || mode == null) {
672
          return _fileSystem.path.join(engineDir, platformName);
673 674
        }
        final String suffix = mode != BuildMode.debug ? '-${snakeCase(getModeName(mode), '-')}' : '';
675
        return _fileSystem.path.join(engineDir, platformName + suffix);
676 677
      case TargetPlatform.fuchsia_arm64:
      case TargetPlatform.fuchsia_x64:
678
      case TargetPlatform.tester:
679
      case TargetPlatform.web_javascript:
680
        assert(mode == null, 'Platform $platform does not support different build modes.');
681
        return _fileSystem.path.join(engineDir, platformName);
682 683
      case TargetPlatform.ios:
      case TargetPlatform.android_arm:
684
      case TargetPlatform.android_arm64:
685 686
      case TargetPlatform.android_x64:
      case TargetPlatform.android_x86:
687
      case TargetPlatform.windows_uwp_x64:
688
        assert(mode != null, 'Need to specify a build mode for platform $platform.');
689
        final String suffix = mode != BuildMode.debug ? '-${snakeCase(getModeName(mode!), '-')}' : '';
690
        return _fileSystem.path.join(engineDir, platformName + suffix);
691 692 693
      case TargetPlatform.android:
        assert(false, 'cannot use TargetPlatform.android to look up artifacts');
        return null;
694 695
    }
  }
696 697 698

  @override
  bool get isLocalEngine => false;
699 700
}

701
TargetPlatform _currentHostPlatform(Platform platform, OperatingSystemUtils operatingSystemUtils) {
702
  if (platform.isMacOS) {
703
    return TargetPlatform.darwin;
704 705
  }
  if (platform.isLinux) {
706 707
    return operatingSystemUtils.hostPlatform == HostPlatform.linux_x64 ?
             TargetPlatform.linux_x64 : TargetPlatform.linux_arm64;
708 709
  }
  if (platform.isWindows) {
710
    return TargetPlatform.windows_x64;
711 712 713 714
  }
  throw UnimplementedError('Host OS not supported.');
}

715
String _getIosEngineArtifactPath(String engineDirectory,
716
    EnvironmentType? environmentType, FileSystem fileSystem) {
717 718
  final Directory xcframeworkDirectory = fileSystem
      .directory(engineDirectory)
719
      .childDirectory(_artifactToFileName(Artifact.flutterXcframework)!);
720 721

  if (!xcframeworkDirectory.existsSync()) {
722
    throwToolExit('No xcframework found at ${xcframeworkDirectory.path}. Try running "flutter precache --ios".');
723
  }
724
  Directory? flutterFrameworkSource;
725 726 727 728 729
  for (final Directory platformDirectory
      in xcframeworkDirectory.listSync().whereType<Directory>()) {
    if (!platformDirectory.basename.startsWith('ios-')) {
      continue;
    }
730 731
    // ios-x86_64-simulator, ios-arm64_x86_64-simulator, ios-armv7_arm64 (Xcode 11), or ios-arm64_armv7 (Xcode 12).
    final bool simulatorDirectory = platformDirectory.basename.endsWith('-simulator');
732 733 734 735 736 737 738 739 740 741
    if ((environmentType == EnvironmentType.simulator && simulatorDirectory) ||
        (environmentType == EnvironmentType.physical && !simulatorDirectory)) {
      flutterFrameworkSource = platformDirectory;
    }
  }
  if (flutterFrameworkSource == null) {
    throwToolExit('No iOS frameworks found in ${xcframeworkDirectory.path}');
  }

  return flutterFrameworkSource
742
      .childDirectory(_artifactToFileName(Artifact.flutterFramework)!)
743 744 745
      .path;
}

746 747
abstract class LocalEngineArtifacts implements Artifacts {
  factory LocalEngineArtifacts(String engineOutPath, String hostEngineOutPath, {
748 749 750 751 752
    required FileSystem fileSystem,
    required Cache cache,
    required ProcessManager processManager,
    required Platform platform,
    required OperatingSystemUtils operatingSystemUtils,
753 754 755 756 757
  }) = CachedLocalEngineArtifacts;

  String get engineOutPath;
}

758
/// Manages the artifacts of a locally built engine.
759 760
class CachedLocalEngineArtifacts implements LocalEngineArtifacts {
  CachedLocalEngineArtifacts(
761 762
    this.engineOutPath,
    this._hostEngineOutPath, {
763 764 765 766 767
    required FileSystem fileSystem,
    required Cache cache,
    required ProcessManager processManager,
    required Platform platform,
    required OperatingSystemUtils operatingSystemUtils,
768 769 770
  }) : _fileSystem = fileSystem,
       _cache = cache,
       _processManager = processManager,
771 772
       _platform = platform,
       _operatingSystemUtils = operatingSystemUtils;
773

774 775 776
  @override
  final String engineOutPath;

777
  final String _hostEngineOutPath;
778 779 780 781
  final FileSystem _fileSystem;
  final Cache _cache;
  final ProcessManager _processManager;
  final Platform _platform;
782
  final OperatingSystemUtils _operatingSystemUtils;
783

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

  @override
  FileSystemEntity getHostArtifact(
    HostArtifact artifact,
  ) {
    switch (artifact) {
      case HostArtifact.engineDartSdkPath:
        final String path = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk');
        return _fileSystem.directory(path);
      case HostArtifact.engineDartBinary:
        final String path = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk', 'bin', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.dart2jsSnapshot:
        final String path = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk', 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.dartdevcSnapshot:
800
        final String path = _fileSystem.path.join(_dartSdkPath(_cache), 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform.isWindows));
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
        return _fileSystem.file(path);
      case HostArtifact.kernelWorkerSnapshot:
        final String path = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk', 'bin', 'snapshots', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.flutterWebSdk:
        final String path = _getFlutterWebSdkPath();
        return _fileSystem.directory(path);
      case HostArtifact.flutterWebLibrariesJson:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPlatformKernelDill:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPlatformSoundKernelDill:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSdk:
      case HostArtifact.webPrecompiledSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSdk:
      case HostArtifact.webPrecompiledCanvaskitSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdk:
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-html', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSoundSdk:
      case HostArtifact.webPrecompiledSoundSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-sound', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSoundSdk:
      case HostArtifact.webPrecompiledCanvaskitSoundSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-sound', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdk:
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-html-sound', _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.file(path);
      case HostArtifact.idevicesyslog:
      case HostArtifact.idevicescreenshot:
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform.isWindows);
        return _cache.getArtifactDirectory('libimobiledevice').childFile(artifactFileName);
      case HostArtifact.skyEnginePath:
        final Directory dartPackageDirectory = _cache.getCacheDir('pkg');
        final String path = _fileSystem.path.join(dartPackageDirectory.path,  _hostArtifactToFileName(artifact, _platform.isWindows));
        return _fileSystem.directory(path);
      case HostArtifact.iosDeploy:
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform.isWindows);
        return _cache.getArtifactDirectory('ios-deploy').childFile(artifactFileName);
      case HostArtifact.iproxy:
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform.isWindows);
        return _cache.getArtifactDirectory('usbmuxd').childFile(artifactFileName);
    }
  }

858
  @override
859 860
  String getArtifactPath(
    Artifact artifact, {
861 862 863
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
864
  }) {
865
    platform ??= _currentHostPlatform(_platform, _operatingSystemUtils);
866
    platform = _mapTargetPlatform(platform);
867
    final bool isDirectoryArtifact = artifact == Artifact.flutterPatchedSdkPath;
868
    final String? artifactFileName = isDirectoryArtifact ? null : _artifactToFileName(artifact, platform, mode);
869 870
    switch (artifact) {
      case Artifact.genSnapshot:
871
        return _genSnapshotPath();
872
      case Artifact.flutterTester:
873
        return _flutterTesterPath(platform!);
874 875
      case Artifact.isolateSnapshotData:
      case Artifact.vmSnapshotData:
876
        return _fileSystem.path.join(engineOutPath, 'gen', 'flutter', 'lib', 'snapshot', artifactFileName);
877
      case Artifact.icuData:
878 879
      case Artifact.flutterXcframework:
      case Artifact.flutterMacOSFramework:
880
        return _fileSystem.path.join(engineOutPath, artifactFileName);
881
      case Artifact.platformKernelDill:
882
        if (platform == TargetPlatform.fuchsia_x64 || platform == TargetPlatform.fuchsia_arm64) {
883
          return _fileSystem.path.join(engineOutPath, 'flutter_runner_patched_sdk', artifactFileName);
884
        }
885
        return _fileSystem.path.join(_getFlutterPatchedSdkPath(mode), artifactFileName);
886
      case Artifact.platformLibrariesJson:
887
        return _fileSystem.path.join(_getFlutterPatchedSdkPath(mode), 'lib', artifactFileName);
888
      case Artifact.flutterFramework:
889 890
        return _getIosEngineArtifactPath(
            engineOutPath, environmentType, _fileSystem);
891
      case Artifact.flutterPatchedSdkPath:
892 893 894 895
        // When using local engine always use [BuildMode.debug] regardless of
        // what was specified in [mode] argument because local engine will
        // have only one flutter_patched_sdk in standard location, that
        // is happen to be what debug(non-release) mode is using.
896
        if (platform == TargetPlatform.fuchsia_x64 || platform == TargetPlatform.fuchsia_arm64) {
897
          return _fileSystem.path.join(engineOutPath, 'flutter_runner_patched_sdk');
898
        }
899
        return _getFlutterPatchedSdkPath(BuildMode.debug);
900
      case Artifact.skyEnginePath:
901
        return _fileSystem.path.join(_hostEngineOutPath, 'gen', 'dart-pkg', artifactFileName);
902
      case Artifact.flutterMacOSPodspec:
903
        return _fileSystem.path.join(_hostEngineOutPath, _artifactToFileName(artifact));
904
      case Artifact.fuchsiaKernelCompiler:
905
        final String hostPlatform = getNameForHostPlatform(getCurrentHostPlatform());
906
        final String modeName = mode!.isRelease ? 'release' : mode.toString();
907
        final String dartBinaries = 'dart_binaries-$modeName-$hostPlatform';
908
        return _fileSystem.path.join(engineOutPath, 'host_bundle', dartBinaries, 'kernel_compiler.dart.snapshot');
909
      case Artifact.fuchsiaFlutterRunner:
910
        final String jitOrAot = mode!.isJit ? '_jit' : '_aot';
911
        final String productOrNo = mode.isRelease ? '_product' : '';
912
        return _fileSystem.path.join(engineOutPath, 'flutter$jitOrAot${productOrNo}_runner-0.far');
913 914 915 916
      case Artifact.fontSubset:
        return _fileSystem.path.join(_hostEngineOutPath, artifactFileName);
      case Artifact.constFinder:
        return _fileSystem.path.join(_hostEngineOutPath, 'gen', artifactFileName);
917 918 919 920 921
      case Artifact.windowsUwpDesktopPath:
      case Artifact.linuxDesktopPath:
      case Artifact.linuxHeaders:
      case Artifact.windowsDesktopPath:
      case Artifact.windowsCppClientWrapper:
922
      case Artifact.windowsUwpCppClientWrapper:
923
        return _fileSystem.path.join(_hostEngineOutPath, artifactFileName);
924
      case Artifact.frontendServerSnapshotForEngineDartSdk:
925 926 927
        return _fileSystem.path.join(
          _hostEngineOutPath, 'dart-sdk', 'bin', 'snapshots', artifactFileName,
        );
928 929
      case Artifact.uwptool:
        return _fileSystem.path.join(_hostEngineOutPath, artifactFileName);
930 931 932 933
    }
  }

  @override
934
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]) {
935
    return _fileSystem.path.basename(engineOutPath);
936 937
  }

938
  String _getFlutterPatchedSdkPath(BuildMode? buildMode) {
939
    return _fileSystem.path.join(engineOutPath,
940
        buildMode == BuildMode.release ? 'flutter_patched_sdk_product' : 'flutter_patched_sdk');
941 942
  }

943
  String _getFlutterWebSdkPath() {
944
    return _fileSystem.path.join(engineOutPath, 'flutter_web_sdk');
945 946
  }

947
  String _genSnapshotPath() {
948
    const List<String> clangDirs = <String>['.', 'clang_x64', 'clang_x86', 'clang_i386', 'clang_arm64'];
949
    final String genSnapshotName = _artifactToFileName(Artifact.genSnapshot)!;
950
    for (final String clangDir in clangDirs) {
951 952
      final String genSnapshotPath = _fileSystem.path.join(engineOutPath, clangDir, genSnapshotName);
      if (_processManager.canRun(genSnapshotPath)) {
953
        return genSnapshotPath;
954
      }
955
    }
956
    throw Exception('Unable to find $genSnapshotName');
957 958
  }

959
  String _flutterTesterPath(TargetPlatform platform) {
960
    if (_platform.isLinux) {
961
      return _fileSystem.path.join(engineOutPath, _artifactToFileName(Artifact.flutterTester));
962
    } else if (_platform.isMacOS) {
963
      return _fileSystem.path.join(engineOutPath, 'flutter_tester');
964
    } else if (_platform.isWindows) {
965
      return _fileSystem.path.join(engineOutPath, 'flutter_tester.exe');
966
    }
967
    throw Exception('Unsupported platform $platform.');
968
  }
969 970 971

  @override
  bool get isLocalEngine => true;
972
}
973 974 975 976 977 978 979 980 981

/// An implementation of [Artifacts] that provides individual overrides.
///
/// If an artifact is not provided, the lookup delegates to the parent.
class OverrideArtifacts implements Artifacts {
  /// Creates a new [OverrideArtifacts].
  ///
  /// [parent] must be provided.
  OverrideArtifacts({
982
    required this.parent,
983
    this.frontendServer,
984
    this.engineDartBinary,
985 986
    this.platformKernelDill,
    this.flutterPatchedSdk,
987 988 989
  }) : assert(parent != null);

  final Artifacts parent;
990 991 992 993
  final File? frontendServer;
  final File? engineDartBinary;
  final File? platformKernelDill;
  final File? flutterPatchedSdk;
994 995

  @override
996 997
  String getArtifactPath(
    Artifact artifact, {
998 999 1000
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
1001
  }) {
1002
    if (artifact == Artifact.frontendServerSnapshotForEngineDartSdk && frontendServer != null) {
1003
      return frontendServer!.path;
1004
    }
1005
    if (artifact == Artifact.platformKernelDill && platformKernelDill != null) {
1006
      return platformKernelDill!.path;
1007 1008
    }
    if (artifact == Artifact.flutterPatchedSdkPath && flutterPatchedSdk != null) {
1009
      return flutterPatchedSdk!.path;
1010
    }
1011 1012 1013 1014 1015 1016
    return parent.getArtifactPath(
      artifact,
      platform: platform,
      mode: mode,
      environmentType: environmentType,
    );
1017 1018 1019
  }

  @override
1020
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]) => parent.getEngineType(platform, mode);
1021 1022 1023

  @override
  bool get isLocalEngine => parent.isLocalEngine;
1024 1025 1026 1027

  @override
  FileSystemEntity getHostArtifact(HostArtifact artifact) {
    if (artifact == HostArtifact.engineDartBinary && engineDartBinary != null) {
1028
      return engineDartBinary!;
1029 1030 1031 1032 1033
    }
    return parent.getHostArtifact(
      artifact,
    );
  }
1034
}
1035 1036

/// Locate the Dart SDK.
1037 1038
String _dartSdkPath(Cache cache) {
  return cache.getRoot().childDirectory('dart-sdk').path;
1039
}
1040 1041

class _TestArtifacts implements Artifacts {
1042 1043 1044 1045
  _TestArtifacts(this.fileSystem);

  final FileSystem fileSystem;

1046
  @override
1047 1048
  String getArtifactPath(
    Artifact artifact, {
1049 1050 1051
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
1052
  }) {
1053 1054 1055 1056 1057 1058 1059 1060
    final StringBuffer buffer = StringBuffer();
    buffer.write(artifact);
    if (platform != null) {
      buffer.write('.$platform');
    }
    if (mode != null) {
      buffer.write('.$mode');
    }
1061 1062 1063
    if (environmentType != null) {
      buffer.write('.$environmentType');
    }
1064 1065 1066 1067
    return buffer.toString();
  }

  @override
1068
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]) {
1069 1070 1071 1072 1073
    return 'test-engine';
  }

  @override
  bool get isLocalEngine => false;
1074 1075 1076 1077 1078

  @override
  FileSystemEntity getHostArtifact(HostArtifact artifact) {
    return fileSystem.file(artifact.toString());
  }
1079
}
1080 1081

class _TestLocalEngine extends _TestArtifacts implements LocalEngineArtifacts {
1082
  _TestLocalEngine(this.engineOutPath, FileSystem fileSystem) : super(fileSystem);
1083 1084 1085 1086 1087 1088 1089

  @override
  bool get isLocalEngine => true;

  @override
  final String engineOutPath;
}