artifacts.dart 53.9 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
  platformKernelDill,
  platformLibrariesJson,
  flutterPatchedSdkPath,
32 33 34 35 36

  /// The root directory of the dart SDK.
  engineDartSdkPath,
  /// The dart binary used to execute any of the required snapshots.
  engineDartBinary,
37 38
  /// The dart binary for running aot snapshots
  engineDartAotRuntime,
39
  /// The snapshot of frontend_server compiler.
40
  frontendServerSnapshotForEngineDartSdk,
41 42
  /// The dart snapshot of the dart2js compiler.
  dart2jsSnapshot,
43 44
  /// The dart snapshot of the dart2wasm compiler.
  dart2wasmSnapshot,
45

46 47 48 49 50 51
  /// 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,
52
  /// The root of the cpp client code for Windows desktop.
53
  windowsCppClientWrapper,
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68
  /// The root of the sky_engine package.
  skyEnginePath,

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

  /// Tools related to subsetting or icon font files.
  fontSubset,
  constFinder,
}

/// A subset of [Artifact]s that are platform and build mode independent
enum HostArtifact {
69
  /// The root of the web implementation of the dart SDK.
70
  flutterWebSdk,
71
  /// The libraries JSON file for web release builds.
72
  flutterWebLibrariesJson,
73 74 75 76

  /// Folder that contains platform dill files for the web sdk.
  webPlatformKernelFolder,

77
  /// The summary dill for the dartdevc target.
78 79 80 81 82
  webPlatformDDCKernelDill,
  /// The summary dill with null safety enabled for the dartdevc target.g
  webPlatformDDCSoundKernelDill,
  /// The summary dill for the dartdevc target.
  webPlatformDart2JSKernelDill,
83
  /// The summary dill with null safety enabled for the dartdevc target.
84
  webPlatformDart2JSSoundKernelDill,
85

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

100 101 102 103
  iosDeploy,
  idevicesyslog,
  idevicescreenshot,
  iproxy,
104

105
  /// The root of the sky_engine package.
106
  skyEnginePath,
107 108 109 110 111

  // The Impeller shader compiler.
  impellerc,
  // Impeller's tessellation library.
  libtessellator,
112 113
}

114 115 116 117 118 119 120 121
// 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);
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
// 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.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;
  }
}

145 146
String? _artifactToFileName(Artifact artifact, Platform hostPlatform, [ BuildMode? mode ]) {
  final String exe = hostPlatform.isWindows ? '.exe' : '';
147 148
  switch (artifact) {
    case Artifact.genSnapshot:
149
      return 'gen_snapshot';
150
    case Artifact.flutterTester:
151
      return 'flutter_tester$exe';
152 153
    case Artifact.flutterFramework:
      return 'Flutter.framework';
154 155
    case Artifact.flutterXcframework:
      return 'Flutter.xcframework';
156 157
    case Artifact.flutterMacOSFramework:
      return 'FlutterMacOS.framework';
158 159 160 161
    case Artifact.vmSnapshotData:
      return 'vm_isolate_snapshot.bin';
    case Artifact.isolateSnapshotData:
      return 'isolate_snapshot.bin';
162 163
    case Artifact.icuData:
      return 'icudtl.dat';
164
    case Artifact.platformKernelDill:
165
      return 'platform_strong.dill';
166 167 168 169 170
    case Artifact.platformLibrariesJson:
      return 'libraries.json';
    case Artifact.flutterPatchedSdkPath:
      assert(false, 'No filename for sdk path, should not be invoked');
      return null;
171 172 173 174
    case Artifact.engineDartSdkPath:
      return 'dart-sdk';
    case Artifact.engineDartBinary:
      return 'dart$exe';
175 176
    case Artifact.engineDartAotRuntime:
      return 'dartaotruntime$exe';
177 178
    case Artifact.dart2jsSnapshot:
      return 'dart2js.dart.snapshot';
179 180
    case Artifact.dart2wasmSnapshot:
      return 'dart2wasm_product.snapshot';
181 182
    case Artifact.frontendServerSnapshotForEngineDartSdk:
      return 'frontend_server.dart.snapshot';
183 184
    case Artifact.linuxDesktopPath:
      return '';
185 186
    case Artifact.linuxHeaders:
      return 'flutter_linux';
187 188
    case Artifact.windowsCppClientWrapper:
      return 'cpp_client_wrapper';
189 190
    case Artifact.windowsDesktopPath:
      return '';
191 192
    case Artifact.skyEnginePath:
      return 'sky_engine';
193 194
    case Artifact.fuchsiaKernelCompiler:
      return 'kernel_compiler.snapshot';
195
    case Artifact.fuchsiaFlutterRunner:
196
      final String jitOrAot = mode!.isJit ? '_jit' : '_aot';
197 198
      final String productOrNo = mode.isRelease ? '_product' : '';
      return 'flutter$jitOrAot${productOrNo}_runner-0.far';
199 200 201 202
    case Artifact.fontSubset:
      return 'font-subset$exe';
    case Artifact.constFinder:
      return 'const_finder.dart.snapshot';
203 204 205
  }
}

206 207 208 209 210 211 212 213
String _hostArtifactToFileName(HostArtifact artifact, Platform platform) {
  final String exe = platform.isWindows ? '.exe' : '';
  String dll = '.so';
  if (platform.isWindows) {
    dll = '.dll';
  } else if (platform.isMacOS) {
    dll = '.dylib';
  }
214 215 216 217 218 219 220 221 222 223 224 225 226
  switch (artifact) {
    case HostArtifact.flutterWebSdk:
      return '';
    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';
227 228 229 230 231 232 233 234 235 236
    case HostArtifact.webPlatformKernelFolder:
      return 'kernel';
    case HostArtifact.webPlatformDDCKernelDill:
      return 'ddc_outline.dill';
    case HostArtifact.webPlatformDDCSoundKernelDill:
      return 'ddc_outline_sound.dill';
    case HostArtifact.webPlatformDart2JSKernelDill:
      return 'dart2js_platform_unsound.dill';
    case HostArtifact.webPlatformDart2JSSoundKernelDill:
      return 'dart2js_platform.dill';
237
    case HostArtifact.flutterWebLibrariesJson:
238
      return 'libraries.json';
239 240 241 242 243 244
    case HostArtifact.webPrecompiledSdk:
    case HostArtifact.webPrecompiledCanvaskitSdk:
    case HostArtifact.webPrecompiledCanvaskitAndHtmlSdk:
    case HostArtifact.webPrecompiledSoundSdk:
    case HostArtifact.webPrecompiledCanvaskitSoundSdk:
    case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdk:
245
      return 'dart_sdk.js';
246 247 248 249 250 251
    case HostArtifact.webPrecompiledSdkSourcemaps:
    case HostArtifact.webPrecompiledCanvaskitSdkSourcemaps:
    case HostArtifact.webPrecompiledCanvaskitAndHtmlSdkSourcemaps:
    case HostArtifact.webPrecompiledSoundSdkSourcemaps:
    case HostArtifact.webPrecompiledCanvaskitSoundSdkSourcemaps:
    case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdkSourcemaps:
252
      return 'dart_sdk.js.map';
253 254 255 256
    case HostArtifact.impellerc:
      return 'impellerc$exe';
    case HostArtifact.libtessellator:
      return 'libtessellator$dll';
257 258 259
  }
}

260
class EngineBuildPaths {
261
  const EngineBuildPaths({
262 263
    required this.targetEngine,
    required this.hostEngine,
264 265 266 267 268 269 270 271 272 273 274 275 276 277
    required this.webSdk,
  });

  final String? targetEngine;
  final String? hostEngine;
  final String? webSdk;
}

/// Information about a local engine build
class LocalEngineInfo {
  const LocalEngineInfo({
    required this.engineOutPath,
    required this.localEngineName,
  });
278

279 280
  final String engineOutPath;
  final String localEngineName;
281 282
}

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

299 300
  static Artifacts getLocalEngine(EngineBuildPaths engineBuildPaths) {
    Artifacts artifacts = CachedArtifacts(
301 302
      fileSystem: globals.fs,
      platform: globals.platform,
303 304
      cache: globals.cache,
      operatingSystemUtils: globals.os
305
    );
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    if (engineBuildPaths.hostEngine != null && engineBuildPaths.targetEngine != null) {
      artifacts = CachedLocalEngineArtifacts(
        engineBuildPaths.hostEngine!,
        engineOutPath: engineBuildPaths.targetEngine!,
        cache: globals.cache,
        fileSystem: globals.fs,
        processManager: globals.processManager,
        platform: globals.platform,
        operatingSystemUtils: globals.os,
        parent: artifacts,
      );
    }
    if (engineBuildPaths.webSdk != null) {
      artifacts = CachedLocalWebSdkArtifacts(
        parent: artifacts,
        webSdkPath: engineBuildPaths.webSdk!,
        fileSystem: globals.fs,
        platform: globals.platform,
        operatingSystemUtils: globals.os
      );
    }
    return artifacts;
328 329
  }

330 331 332
  /// Returns the requested [artifact] for the [platform], [mode], and [environmentType] combination.
  String getArtifactPath(
    Artifact artifact, {
333 334 335
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
336
  });
337

338 339 340 341 342 343
  /// Retrieve a host specific artifact that does not depend on the
  /// current build mode or environment.
  FileSystemEntity getHostArtifact(
    HostArtifact artifact,
  );

344 345
  // Returns which set of engine artifacts is currently used for the [platform]
  // and [mode] combination.
346
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]);
347 348 349

  /// Whether these artifacts correspond to a non-versioned local engine.
  bool get isLocalEngine;
350 351 352 353

  /// If these artifacts are bound to a local engine build, returns info about
  /// the location and name of the local engine, otherwise returns null.
  LocalEngineInfo? get localEngineInfo;
354 355 356
}

/// Manages the engine artifacts downloaded to the local cache.
357
class CachedArtifacts implements Artifacts {
358
  CachedArtifacts({
359 360 361 362
    required FileSystem fileSystem,
    required Platform platform,
    required Cache cache,
    required OperatingSystemUtils operatingSystemUtils,
363 364
  }) : _fileSystem = fileSystem,
       _platform = platform,
365 366
       _cache = cache,
       _operatingSystemUtils = operatingSystemUtils;
367 368 369 370

  final FileSystem _fileSystem;
  final Platform _platform;
  final Cache _cache;
371
  final OperatingSystemUtils _operatingSystemUtils;
372

373 374 375
  @override
  LocalEngineInfo? get localEngineInfo => null;

376 377 378 379 380 381 382 383 384
  @override
  FileSystemEntity getHostArtifact(
    HostArtifact artifact,
  ) {
    switch (artifact) {
      case HostArtifact.flutterWebSdk:
        final String path = _getFlutterWebSdkPath();
        return _fileSystem.directory(path);
      case HostArtifact.flutterWebLibrariesJson:
385
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform));
386
        return _fileSystem.file(path);
387 388
      case HostArtifact.webPlatformKernelFolder:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel');
389
        return _fileSystem.file(path);
390 391 392 393
      case HostArtifact.webPlatformDDCKernelDill:
      case HostArtifact.webPlatformDDCSoundKernelDill:
      case HostArtifact.webPlatformDart2JSKernelDill:
      case HostArtifact.webPlatformDart2JSSoundKernelDill:
394
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', _hostArtifactToFileName(artifact, _platform));
395 396 397
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSdk:
      case HostArtifact.webPrecompiledSdkSourcemaps:
398
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd', _hostArtifactToFileName(artifact, _platform));
399 400 401
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSdk:
      case HostArtifact.webPrecompiledCanvaskitSdkSourcemaps:
402
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit', _hostArtifactToFileName(artifact, _platform));
403 404 405
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdk:
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdkSourcemaps:
406
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-html', _hostArtifactToFileName(artifact, _platform));
407 408 409
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSoundSdk:
      case HostArtifact.webPrecompiledSoundSdkSourcemaps:
410
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-sound', _hostArtifactToFileName(artifact, _platform));
411 412 413
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSoundSdk:
      case HostArtifact.webPrecompiledCanvaskitSoundSdkSourcemaps:
414
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-sound', _hostArtifactToFileName(artifact, _platform));
415 416 417
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdk:
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdkSourcemaps:
418
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-html-sound', _hostArtifactToFileName(artifact, _platform));
419 420 421
        return _fileSystem.file(path);
      case HostArtifact.idevicesyslog:
      case HostArtifact.idevicescreenshot:
422
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform);
423 424 425
        return _cache.getArtifactDirectory('libimobiledevice').childFile(artifactFileName);
      case HostArtifact.skyEnginePath:
        final Directory dartPackageDirectory = _cache.getCacheDir('pkg');
426
        final String path = _fileSystem.path.join(dartPackageDirectory.path,  _hostArtifactToFileName(artifact, _platform));
427 428
        return _fileSystem.directory(path);
      case HostArtifact.iosDeploy:
429
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform);
430 431
        return _cache.getArtifactDirectory('ios-deploy').childFile(artifactFileName);
      case HostArtifact.iproxy:
432
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform);
433
        return _cache.getArtifactDirectory('usbmuxd').childFile(artifactFileName);
434 435 436 437 438
      case HostArtifact.impellerc:
      case HostArtifact.libtessellator:
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform);
        final String engineDir = _getEngineArtifactsPath(_currentHostPlatform(_platform, _operatingSystemUtils))!;
        return _fileSystem.file(_fileSystem.path.join(engineDir, artifactFileName));
439 440 441
    }
  }

442
  @override
443 444
  String getArtifactPath(
    Artifact artifact, {
445 446 447
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
448
  }) {
449
    platform = _mapTargetPlatform(platform);
450
    switch (platform) {
451
      case TargetPlatform.android:
452
      case TargetPlatform.android_arm:
453
      case TargetPlatform.android_arm64:
454 455
      case TargetPlatform.android_x64:
      case TargetPlatform.android_x86:
456
        assert(platform != TargetPlatform.android);
457
        return _getAndroidArtifactPath(artifact, platform!, mode!);
458
      case TargetPlatform.ios:
459
        return _getIosArtifactPath(artifact, platform!, mode, environmentType);
460
      case TargetPlatform.darwin:
461
      case TargetPlatform.linux_x64:
462
      case TargetPlatform.linux_arm64:
463
      case TargetPlatform.windows_x64:
464
        return _getDesktopArtifactPath(artifact, platform, mode);
465 466
      case TargetPlatform.fuchsia_arm64:
      case TargetPlatform.fuchsia_x64:
467
        return _getFuchsiaArtifactPath(artifact, platform!, mode!);
468
      case TargetPlatform.tester:
469
      case TargetPlatform.web_javascript:
470
      case null:
471
        return _getHostArtifactPath(artifact, platform ?? _currentHostPlatform(_platform, _operatingSystemUtils), mode);
472 473 474 475
    }
  }

  @override
476 477
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]) {
    return _fileSystem.path.basename(_getEngineArtifactsPath(platform, mode)!);
478 479
  }

480
  String _getDesktopArtifactPath(Artifact artifact, TargetPlatform? platform, BuildMode? mode) {
481 482 483
    // 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) {
484
      final String engineDir = _getEngineArtifactsPath(platform, mode)!;
485
      return _fileSystem.path.join(engineDir, _artifactToFileName(artifact, _platform));
486
    }
487
    return _getHostArtifactPath(artifact, platform ?? _currentHostPlatform(_platform, _operatingSystemUtils), mode);
488 489
  }

490
  String _getAndroidArtifactPath(Artifact artifact, TargetPlatform platform, BuildMode mode) {
491
    final String engineDir = _getEngineArtifactsPath(platform, mode)!;
492 493 494
    switch (artifact) {
      case Artifact.genSnapshot:
        assert(mode != BuildMode.debug, 'Artifact $artifact only available in non-debug mode.');
495
        final String hostPlatform = getNameForHostPlatform(getCurrentHostPlatform());
496 497 498
        return _fileSystem.path.join(engineDir, hostPlatform, _artifactToFileName(artifact, _platform));
      case Artifact.engineDartSdkPath:
      case Artifact.engineDartBinary:
499
      case Artifact.engineDartAotRuntime:
500
      case Artifact.dart2jsSnapshot:
501
      case Artifact.dart2wasmSnapshot:
502
      case Artifact.frontendServerSnapshotForEngineDartSdk:
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
      case Artifact.constFinder:
      case Artifact.flutterFramework:
      case Artifact.flutterMacOSFramework:
      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.vmSnapshotData:
      case Artifact.windowsCppClientWrapper:
      case Artifact.windowsDesktopPath:
522
        return _getHostArtifactPath(artifact, platform, mode);
523 524 525
    }
  }

526
  String _getIosArtifactPath(Artifact artifact, TargetPlatform platform, BuildMode? mode, EnvironmentType? environmentType) {
527 528
    switch (artifact) {
      case Artifact.genSnapshot:
529
      case Artifact.flutterXcframework:
530
        final String artifactFileName = _artifactToFileName(artifact, _platform)!;
531
        final String engineDir = _getEngineArtifactsPath(platform, mode)!;
532
        return _fileSystem.path.join(engineDir, artifactFileName);
533
      case Artifact.flutterFramework:
534
        final String engineDir = _getEngineArtifactsPath(platform, mode)!;
535 536 537
        return _getIosEngineArtifactPath(engineDir, environmentType, _fileSystem, _platform);
      case Artifact.engineDartSdkPath:
      case Artifact.engineDartBinary:
538
      case Artifact.engineDartAotRuntime:
539
      case Artifact.dart2jsSnapshot:
540
      case Artifact.dart2wasmSnapshot:
541
      case Artifact.frontendServerSnapshotForEngineDartSdk:
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
      case Artifact.constFinder:
      case Artifact.flutterMacOSFramework:
      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.vmSnapshotData:
      case Artifact.windowsCppClientWrapper:
      case Artifact.windowsDesktopPath:
559
        return _getHostArtifactPath(artifact, platform, mode);
560 561 562
    }
  }

563
  String _getFuchsiaArtifactPath(Artifact artifact, TargetPlatform platform, BuildMode mode) {
564 565
    final String root = _fileSystem.path.join(
      _cache.getArtifactDirectory('flutter_runner').path,
566 567
      'flutter',
      fuchsiaArchForTargetPlatform(platform),
568
      mode.isRelease ? 'release' : mode.toString(),
569
    );
570
    final String runtime = mode.isJit ? 'jit' : 'aot';
571
    switch (artifact) {
572 573
      case Artifact.genSnapshot:
        final String genSnapshot = mode.isRelease ? 'gen_snapshot_product' : 'gen_snapshot';
574
        return _fileSystem.path.join(root, runtime, 'dart_binaries', genSnapshot);
575 576
      case Artifact.flutterPatchedSdkPath:
        const String artifactFileName = 'flutter_runner_patched_sdk';
577
        return _fileSystem.path.join(root, runtime, artifactFileName);
578
      case Artifact.platformKernelDill:
579
        final String artifactFileName = _artifactToFileName(artifact, _platform, mode)!;
580
        return _fileSystem.path.join(root, runtime, 'flutter_runner_patched_sdk', artifactFileName);
581
      case Artifact.fuchsiaKernelCompiler:
582
        final String artifactFileName = _artifactToFileName(artifact, _platform, mode)!;
583
        return _fileSystem.path.join(root, runtime, 'dart_binaries', artifactFileName);
584
      case Artifact.fuchsiaFlutterRunner:
585
        final String artifactFileName = _artifactToFileName(artifact, _platform, mode)!;
586
        return _fileSystem.path.join(root, runtime, artifactFileName);
587 588 589 590 591 592
      case Artifact.constFinder:
      case Artifact.flutterFramework:
      case Artifact.flutterMacOSFramework:
      case Artifact.flutterTester:
      case Artifact.flutterXcframework:
      case Artifact.fontSubset:
593 594
      case Artifact.engineDartSdkPath:
      case Artifact.engineDartBinary:
595
      case Artifact.engineDartAotRuntime:
596
      case Artifact.dart2jsSnapshot:
597
      case Artifact.dart2wasmSnapshot:
598 599 600 601 602 603 604 605 606 607
      case Artifact.frontendServerSnapshotForEngineDartSdk:
      case Artifact.icuData:
      case Artifact.isolateSnapshotData:
      case Artifact.linuxDesktopPath:
      case Artifact.linuxHeaders:
      case Artifact.platformLibrariesJson:
      case Artifact.skyEnginePath:
      case Artifact.vmSnapshotData:
      case Artifact.windowsCppClientWrapper:
      case Artifact.windowsDesktopPath:
608
        return _getHostArtifactPath(artifact, platform, mode);
609 610 611
    }
  }

612
  String _getFlutterPatchedSdkPath(BuildMode? mode) {
613 614
    final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
    return _fileSystem.path.join(engineArtifactsPath, 'common',
615
        mode == BuildMode.release ? 'flutter_patched_sdk_product' : 'flutter_patched_sdk');
616 617
  }

618
  String _getFlutterWebSdkPath() {
619
    return _cache.getWebSdkDirectory().path;
620 621
  }

622
  String _getHostArtifactPath(Artifact artifact, TargetPlatform platform, BuildMode? mode) {
623
    assert(platform != null);
624
    switch (artifact) {
625 626 627 628
      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);
629
      case Artifact.dart2jsSnapshot:
630
      case Artifact.dart2wasmSnapshot:
631 632 633
      case Artifact.frontendServerSnapshotForEngineDartSdk:
        return _fileSystem.path.join(
          _dartSdkPath(_cache), 'bin', 'snapshots',
634
          _artifactToFileName(artifact, _platform),
635
        );
636
      case Artifact.flutterTester:
637 638
      case Artifact.vmSnapshotData:
      case Artifact.isolateSnapshotData:
639
      case Artifact.icuData:
640
        final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
641
        final String platformDirName = _enginePlatformDirectoryName(platform);
642
        return _fileSystem.path.join(engineArtifactsPath, platformDirName, _artifactToFileName(artifact, _platform, mode));
643
      case Artifact.platformKernelDill:
644
        return _fileSystem.path.join(_getFlutterPatchedSdkPath(mode), _artifactToFileName(artifact, _platform));
645
      case Artifact.platformLibrariesJson:
646
        return _fileSystem.path.join(_getFlutterPatchedSdkPath(mode), 'lib', _artifactToFileName(artifact, _platform));
647
      case Artifact.flutterPatchedSdkPath:
648
        return _getFlutterPatchedSdkPath(mode);
649 650 651
      case Artifact.engineDartSdkPath:
        return _dartSdkPath(_cache);
      case Artifact.engineDartBinary:
652
      case Artifact.engineDartAotRuntime:
653
        return _fileSystem.path.join(_dartSdkPath(_cache), 'bin', _artifactToFileName(artifact, _platform));
654
      case Artifact.flutterMacOSFramework:
655 656
      case Artifact.linuxDesktopPath:
      case Artifact.windowsDesktopPath:
657
      case Artifact.linuxHeaders:
658
        // TODO(zanderso): remove once debug desktop artifacts are uploaded
659 660
        // under a separate directory from the host artifacts.
        // https://github.com/flutter/flutter/issues/38935
661
        String platformDirName = _enginePlatformDirectoryName(platform);
662
        if (mode == BuildMode.profile || mode == BuildMode.release) {
663
          platformDirName = '$platformDirName-${getNameForBuildMode(mode!)}';
664
        }
665
        final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
666
        return _fileSystem.path.join(engineArtifactsPath, platformDirName, _artifactToFileName(artifact, _platform, mode));
667 668
      case Artifact.windowsCppClientWrapper:
        final String engineArtifactsPath = _cache.getArtifactDirectory('engine').path;
669
        return _fileSystem.path.join(engineArtifactsPath, 'windows-x64', _artifactToFileName(artifact, _platform, mode));
670
      case Artifact.skyEnginePath:
671
        final Directory dartPackageDirectory = _cache.getCacheDir('pkg');
672
        return _fileSystem.path.join(dartPackageDirectory.path,  _artifactToFileName(artifact, _platform));
673 674
      case Artifact.fontSubset:
      case Artifact.constFinder:
675
        return _cache.getArtifactDirectory('engine')
676
                     .childDirectory(_enginePlatformDirectoryName(platform))
677
                     .childFile(_artifactToFileName(artifact, _platform, mode)!)
678
                     .path;
679 680 681 682
      case Artifact.flutterFramework:
      case Artifact.flutterXcframework:
      case Artifact.fuchsiaFlutterRunner:
      case Artifact.fuchsiaKernelCompiler:
683
        throw StateError('Artifact $artifact not available for platform $platform.');
684 685 686
    }
  }

687
  String? _getEngineArtifactsPath(TargetPlatform platform, [ BuildMode? mode ]) {
688
    final String engineDir = _cache.getArtifactDirectory('engine').path;
689
    final String platformName = _enginePlatformDirectoryName(platform);
690
    switch (platform) {
691
      case TargetPlatform.linux_x64:
692
      case TargetPlatform.linux_arm64:
693
      case TargetPlatform.darwin:
694
      case TargetPlatform.windows_x64:
695
        // TODO(zanderso): remove once debug desktop artifacts are uploaded
696 697 698
        // under a separate directory from the host artifacts.
        // https://github.com/flutter/flutter/issues/38935
        if (mode == BuildMode.debug || mode == null) {
699
          return _fileSystem.path.join(engineDir, platformName);
700 701
        }
        final String suffix = mode != BuildMode.debug ? '-${snakeCase(getModeName(mode), '-')}' : '';
702
        return _fileSystem.path.join(engineDir, platformName + suffix);
703 704
      case TargetPlatform.fuchsia_arm64:
      case TargetPlatform.fuchsia_x64:
705
      case TargetPlatform.tester:
706
      case TargetPlatform.web_javascript:
707
        assert(mode == null, 'Platform $platform does not support different build modes.');
708
        return _fileSystem.path.join(engineDir, platformName);
709 710
      case TargetPlatform.ios:
      case TargetPlatform.android_arm:
711
      case TargetPlatform.android_arm64:
712 713 714
      case TargetPlatform.android_x64:
      case TargetPlatform.android_x86:
        assert(mode != null, 'Need to specify a build mode for platform $platform.');
715
        final String suffix = mode != BuildMode.debug ? '-${snakeCase(getModeName(mode!), '-')}' : '';
716
        return _fileSystem.path.join(engineDir, platformName + suffix);
717 718 719
      case TargetPlatform.android:
        assert(false, 'cannot use TargetPlatform.android to look up artifacts');
        return null;
720 721
    }
  }
722 723 724

  @override
  bool get isLocalEngine => false;
725 726
}

727
TargetPlatform _currentHostPlatform(Platform platform, OperatingSystemUtils operatingSystemUtils) {
728
  if (platform.isMacOS) {
729
    return TargetPlatform.darwin;
730 731
  }
  if (platform.isLinux) {
732 733
    return operatingSystemUtils.hostPlatform == HostPlatform.linux_x64 ?
             TargetPlatform.linux_x64 : TargetPlatform.linux_arm64;
734 735
  }
  if (platform.isWindows) {
736
    return TargetPlatform.windows_x64;
737 738 739 740
  }
  throw UnimplementedError('Host OS not supported.');
}

741
String _getIosEngineArtifactPath(String engineDirectory,
742
    EnvironmentType? environmentType, FileSystem fileSystem, Platform hostPlatform) {
743 744
  final Directory xcframeworkDirectory = fileSystem
      .directory(engineDirectory)
745
      .childDirectory(_artifactToFileName(Artifact.flutterXcframework, hostPlatform)!);
746 747

  if (!xcframeworkDirectory.existsSync()) {
748
    throwToolExit('No xcframework found at ${xcframeworkDirectory.path}. Try running "flutter precache --ios".');
749
  }
750
  Directory? flutterFrameworkSource;
751 752 753 754 755
  for (final Directory platformDirectory
      in xcframeworkDirectory.listSync().whereType<Directory>()) {
    if (!platformDirectory.basename.startsWith('ios-')) {
      continue;
    }
756
    // ios-x86_64-simulator, ios-arm64_x86_64-simulator, or ios-arm64.
757
    final bool simulatorDirectory = platformDirectory.basename.endsWith('-simulator');
758 759 760 761 762 763 764 765 766 767
    if ((environmentType == EnvironmentType.simulator && simulatorDirectory) ||
        (environmentType == EnvironmentType.physical && !simulatorDirectory)) {
      flutterFrameworkSource = platformDirectory;
    }
  }
  if (flutterFrameworkSource == null) {
    throwToolExit('No iOS frameworks found in ${xcframeworkDirectory.path}');
  }

  return flutterFrameworkSource
768
      .childDirectory(_artifactToFileName(Artifact.flutterFramework, hostPlatform)!)
769 770 771
      .path;
}

772
/// Manages the artifacts of a locally built engine.
773
class CachedLocalEngineArtifacts implements Artifacts {
774
  CachedLocalEngineArtifacts(
775
    this._hostEngineOutPath, {
776
    required String engineOutPath,
777 778 779 780 781
    required FileSystem fileSystem,
    required Cache cache,
    required ProcessManager processManager,
    required Platform platform,
    required OperatingSystemUtils operatingSystemUtils,
782
    Artifacts? parent,
783
  }) : _fileSystem = fileSystem,
784 785 786 787 788
       localEngineInfo =
         LocalEngineInfo(
           engineOutPath: engineOutPath,
           localEngineName: fileSystem.path.basename(engineOutPath)
         ),
789 790
       _cache = cache,
       _processManager = processManager,
791
       _platform = platform,
792
       _operatingSystemUtils = operatingSystemUtils,
793 794 795 796 797 798 799
       _backupCache = parent ??
         CachedArtifacts(
           fileSystem: fileSystem,
           platform: platform,
           cache: cache,
           operatingSystemUtils: operatingSystemUtils
         );
800 801

  @override
802
  final LocalEngineInfo localEngineInfo;
803

804
  final String _hostEngineOutPath;
805 806 807 808
  final FileSystem _fileSystem;
  final Cache _cache;
  final ProcessManager _processManager;
  final Platform _platform;
809
  final OperatingSystemUtils _operatingSystemUtils;
810
  final Artifacts _backupCache;
811 812

  @override
813
  FileSystemEntity getHostArtifact(HostArtifact artifact) {
814 815 816 817 818
    switch (artifact) {
      case HostArtifact.flutterWebSdk:
        final String path = _getFlutterWebSdkPath();
        return _fileSystem.directory(path);
      case HostArtifact.flutterWebLibrariesJson:
819
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform));
820
        return _fileSystem.file(path);
821 822
      case HostArtifact.webPlatformKernelFolder:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel');
823
        return _fileSystem.file(path);
824 825 826 827
      case HostArtifact.webPlatformDDCKernelDill:
      case HostArtifact.webPlatformDDCSoundKernelDill:
      case HostArtifact.webPlatformDart2JSKernelDill:
      case HostArtifact.webPlatformDart2JSSoundKernelDill:
828
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', _hostArtifactToFileName(artifact, _platform));
829 830 831
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSdk:
      case HostArtifact.webPrecompiledSdkSourcemaps:
832
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd', _hostArtifactToFileName(artifact, _platform));
833 834 835
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSdk:
      case HostArtifact.webPrecompiledCanvaskitSdkSourcemaps:
836
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit', _hostArtifactToFileName(artifact, _platform));
837 838 839
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdk:
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdkSourcemaps:
840
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-html', _hostArtifactToFileName(artifact, _platform));
841 842 843
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSoundSdk:
      case HostArtifact.webPrecompiledSoundSdkSourcemaps:
844
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-sound', _hostArtifactToFileName(artifact, _platform));
845 846 847
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSoundSdk:
      case HostArtifact.webPrecompiledCanvaskitSoundSdkSourcemaps:
848
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-sound', _hostArtifactToFileName(artifact, _platform));
849 850 851
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdk:
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdkSourcemaps:
852
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-html-sound', _hostArtifactToFileName(artifact, _platform));
853 854 855
        return _fileSystem.file(path);
      case HostArtifact.idevicesyslog:
      case HostArtifact.idevicescreenshot:
856
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform);
857 858 859
        return _cache.getArtifactDirectory('libimobiledevice').childFile(artifactFileName);
      case HostArtifact.skyEnginePath:
        final Directory dartPackageDirectory = _cache.getCacheDir('pkg');
860
        final String path = _fileSystem.path.join(dartPackageDirectory.path,  _hostArtifactToFileName(artifact, _platform));
861 862
        return _fileSystem.directory(path);
      case HostArtifact.iosDeploy:
863
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform);
864 865
        return _cache.getArtifactDirectory('ios-deploy').childFile(artifactFileName);
      case HostArtifact.iproxy:
866
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform);
867
        return _cache.getArtifactDirectory('usbmuxd').childFile(artifactFileName);
868 869 870
      case HostArtifact.impellerc:
      case HostArtifact.libtessellator:
        final String artifactFileName = _hostArtifactToFileName(artifact, _platform);
871 872 873 874 875
        final File file = _fileSystem.file(_fileSystem.path.join(_hostEngineOutPath, artifactFileName));
        if (!file.existsSync()) {
          return _backupCache.getHostArtifact(artifact);
        }
        return file;
876 877 878
    }
  }

879
  @override
880 881
  String getArtifactPath(
    Artifact artifact, {
882 883 884
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
885
  }) {
886
    platform ??= _currentHostPlatform(_platform, _operatingSystemUtils);
887
    platform = _mapTargetPlatform(platform);
888
    final bool isDirectoryArtifact = artifact == Artifact.flutterPatchedSdkPath;
889
    final String? artifactFileName = isDirectoryArtifact ? null : _artifactToFileName(artifact, _platform, mode);
890 891
    switch (artifact) {
      case Artifact.genSnapshot:
892
        return _genSnapshotPath();
893
      case Artifact.flutterTester:
894
        return _flutterTesterPath(platform!);
895 896
      case Artifact.isolateSnapshotData:
      case Artifact.vmSnapshotData:
897
        return _fileSystem.path.join(localEngineInfo.engineOutPath, 'gen', 'flutter', 'lib', 'snapshot', artifactFileName);
898
      case Artifact.icuData:
899 900
      case Artifact.flutterXcframework:
      case Artifact.flutterMacOSFramework:
901
        return _fileSystem.path.join(localEngineInfo.engineOutPath, artifactFileName);
902
      case Artifact.platformKernelDill:
903
        if (platform == TargetPlatform.fuchsia_x64 || platform == TargetPlatform.fuchsia_arm64) {
904
          return _fileSystem.path.join(localEngineInfo.engineOutPath, 'flutter_runner_patched_sdk', artifactFileName);
905
        }
906
        return _fileSystem.path.join(_getFlutterPatchedSdkPath(mode), artifactFileName);
907
      case Artifact.platformLibrariesJson:
908
        return _fileSystem.path.join(_getFlutterPatchedSdkPath(mode), 'lib', artifactFileName);
909
      case Artifact.flutterFramework:
910
        return _getIosEngineArtifactPath(
911
            localEngineInfo.engineOutPath, environmentType, _fileSystem, _platform);
912
      case Artifact.flutterPatchedSdkPath:
913 914 915 916
        // 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.
917
        if (platform == TargetPlatform.fuchsia_x64 || platform == TargetPlatform.fuchsia_arm64) {
918
          return _fileSystem.path.join(localEngineInfo.engineOutPath, 'flutter_runner_patched_sdk');
919
        }
920
        return _getFlutterPatchedSdkPath(BuildMode.debug);
921
      case Artifact.skyEnginePath:
922
        return _fileSystem.path.join(_hostEngineOutPath, 'gen', 'dart-pkg', artifactFileName);
923
      case Artifact.fuchsiaKernelCompiler:
924
        final String hostPlatform = getNameForHostPlatform(getCurrentHostPlatform());
925
        final String modeName = mode!.isRelease ? 'release' : mode.toString();
926
        final String dartBinaries = 'dart_binaries-$modeName-$hostPlatform';
927
        return _fileSystem.path.join(localEngineInfo.engineOutPath, 'host_bundle', dartBinaries, 'kernel_compiler.dart.snapshot');
928
      case Artifact.fuchsiaFlutterRunner:
929
        final String jitOrAot = mode!.isJit ? '_jit' : '_aot';
930
        final String productOrNo = mode.isRelease ? '_product' : '';
931
        return _fileSystem.path.join(localEngineInfo.engineOutPath, 'flutter$jitOrAot${productOrNo}_runner-0.far');
932 933 934 935
      case Artifact.fontSubset:
        return _fileSystem.path.join(_hostEngineOutPath, artifactFileName);
      case Artifact.constFinder:
        return _fileSystem.path.join(_hostEngineOutPath, 'gen', artifactFileName);
936 937 938 939 940
      case Artifact.linuxDesktopPath:
      case Artifact.linuxHeaders:
      case Artifact.windowsDesktopPath:
      case Artifact.windowsCppClientWrapper:
        return _fileSystem.path.join(_hostEngineOutPath, artifactFileName);
941 942 943
      case Artifact.engineDartSdkPath:
        return _getDartSdkPath();
      case Artifact.engineDartBinary:
944
      case Artifact.engineDartAotRuntime:
945 946
        return _fileSystem.path.join(_getDartSdkPath(), 'bin', artifactFileName);
      case Artifact.dart2jsSnapshot:
947
      case Artifact.dart2wasmSnapshot:
948
      case Artifact.frontendServerSnapshotForEngineDartSdk:
949
        return _fileSystem.path.join(_getDartSdkPath(), 'bin', 'snapshots', artifactFileName);
950 951 952 953
    }
  }

  @override
954
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]) {
955
    return _fileSystem.path.basename(localEngineInfo.engineOutPath);
956 957
  }

958
  String _getFlutterPatchedSdkPath(BuildMode? buildMode) {
959
    return _fileSystem.path.join(localEngineInfo.engineOutPath,
960
        buildMode == BuildMode.release ? 'flutter_patched_sdk_product' : 'flutter_patched_sdk');
961 962
  }

963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
  String _getDartSdkPath() {
    final String builtPath = _fileSystem.path.join(_hostEngineOutPath, 'dart-sdk');
    if (_fileSystem.isDirectorySync(_fileSystem.path.join(builtPath, 'bin'))) {
      return builtPath;
    }

    // If we couldn't find a built dart sdk, let's look for a prebuilt one.
    final String prebuiltPath = _fileSystem.path.join(_getFlutterPrebuiltsPath(), _getPrebuiltTarget(), 'dart-sdk');
    if (_fileSystem.isDirectorySync(prebuiltPath)) {
      return prebuiltPath;
    }

    throw ToolExit('Unable to find a built dart sdk at: "$builtPath" or a prebuilt dart sdk at: "$prebuiltPath"');
  }

  String _getFlutterPrebuiltsPath() {
    final String engineSrcPath = _fileSystem.path.dirname(_fileSystem.path.dirname(_hostEngineOutPath));
    return _fileSystem.path.join(engineSrcPath, 'flutter', 'prebuilts');
  }

  String _getPrebuiltTarget() {
    final TargetPlatform hostPlatform = _currentHostPlatform(_platform, _operatingSystemUtils);
    switch (hostPlatform) {
      case TargetPlatform.darwin:
        return 'macos-x64';
      case TargetPlatform.linux_arm64:
        return 'linux-arm64';
      case TargetPlatform.linux_x64:
        return 'linux-x64';
      case TargetPlatform.windows_x64:
        return 'windows-x64';
      case TargetPlatform.ios:
      case TargetPlatform.android:
      case TargetPlatform.android_arm:
      case TargetPlatform.android_arm64:
      case TargetPlatform.android_x64:
      case TargetPlatform.android_x86:
      case TargetPlatform.fuchsia_arm64:
      case TargetPlatform.fuchsia_x64:
      case TargetPlatform.web_javascript:
      case TargetPlatform.tester:
        throwToolExit('Unsupported host platform: $hostPlatform');
    }
  }

1008
  String _getFlutterWebSdkPath() {
1009
    return _fileSystem.path.join(localEngineInfo.engineOutPath, 'flutter_web_sdk');
1010 1011
  }

1012
  String _genSnapshotPath() {
1013
    const List<String> clangDirs = <String>['.', 'clang_x64', 'clang_x86', 'clang_i386', 'clang_arm64'];
1014
    final String genSnapshotName = _artifactToFileName(Artifact.genSnapshot, _platform)!;
1015
    for (final String clangDir in clangDirs) {
1016
      final String genSnapshotPath = _fileSystem.path.join(localEngineInfo.engineOutPath, clangDir, genSnapshotName);
1017
      if (_processManager.canRun(genSnapshotPath)) {
1018
        return genSnapshotPath;
1019
      }
1020
    }
1021
    throw Exception('Unable to find $genSnapshotName');
1022 1023
  }

1024
  String _flutterTesterPath(TargetPlatform platform) {
1025
    if (_platform.isLinux) {
1026
      return _fileSystem.path.join(localEngineInfo.engineOutPath, _artifactToFileName(Artifact.flutterTester, _platform));
1027
    } else if (_platform.isMacOS) {
1028
      return _fileSystem.path.join(localEngineInfo.engineOutPath, 'flutter_tester');
1029
    } else if (_platform.isWindows) {
1030
      return _fileSystem.path.join(localEngineInfo.engineOutPath, 'flutter_tester.exe');
1031
    }
1032
    throw Exception('Unsupported platform $platform.');
1033
  }
1034 1035 1036

  @override
  bool get isLocalEngine => true;
1037
}
1038

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
class CachedLocalWebSdkArtifacts implements Artifacts {
  CachedLocalWebSdkArtifacts({
    required Artifacts parent,
    required String webSdkPath,
    required FileSystem fileSystem,
    required Platform platform,
    required OperatingSystemUtils operatingSystemUtils
  }) : _parent = parent,
       _webSdkPath = webSdkPath,
       _fileSystem = fileSystem,
       _platform = platform,
       _operatingSystemUtils = operatingSystemUtils;


  final Artifacts _parent;
  final String _webSdkPath;
  final FileSystem _fileSystem;
  final Platform _platform;
  final OperatingSystemUtils _operatingSystemUtils;

  @override
  String getArtifactPath(Artifact artifact, {TargetPlatform? platform, BuildMode? mode, EnvironmentType? environmentType}) {
    if (platform == TargetPlatform.web_javascript) {
      switch (artifact) {
        case Artifact.engineDartSdkPath:
          return _getDartSdkPath();
        case Artifact.engineDartBinary:
1066
        case Artifact.engineDartAotRuntime:
1067 1068 1069 1070
          return _fileSystem.path.join(
            _getDartSdkPath(), 'bin',
            _artifactToFileName(artifact, _platform, mode));
        case Artifact.dart2jsSnapshot:
1071
        case Artifact.dart2wasmSnapshot:
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
        case Artifact.frontendServerSnapshotForEngineDartSdk:
          return _fileSystem.path.join(
            _getDartSdkPath(), 'bin', 'snapshots',
            _artifactToFileName(artifact, _platform, mode),
          );
        case Artifact.genSnapshot:
        case Artifact.flutterTester:
        case Artifact.flutterFramework:
        case Artifact.flutterXcframework:
        case Artifact.flutterMacOSFramework:
        case Artifact.vmSnapshotData:
        case Artifact.isolateSnapshotData:
        case Artifact.icuData:
        case Artifact.platformKernelDill:
        case Artifact.platformLibrariesJson:
        case Artifact.flutterPatchedSdkPath:
        case Artifact.linuxDesktopPath:
        case Artifact.linuxHeaders:
        case Artifact.windowsDesktopPath:
        case Artifact.windowsCppClientWrapper:
        case Artifact.skyEnginePath:
        case Artifact.fuchsiaKernelCompiler:
        case Artifact.fuchsiaFlutterRunner:
        case Artifact.fontSubset:
        case Artifact.constFinder:
          break;
      }
    }
    return _parent.getArtifactPath(artifact, platform: platform, mode: mode, environmentType: environmentType);
  }

  @override
  String getEngineType(TargetPlatform platform, [BuildMode? mode]) => _parent.getEngineType(platform, mode);

  @override
  FileSystemEntity getHostArtifact(HostArtifact artifact) {
    switch (artifact) {
      case HostArtifact.flutterWebSdk:
        final String path = _getFlutterWebSdkPath();
        return _fileSystem.directory(path);
      case HostArtifact.flutterWebLibrariesJson:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), _hostArtifactToFileName(artifact, _platform));
        return _fileSystem.file(path);
      case HostArtifact.webPlatformKernelFolder:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel');
        return _fileSystem.file(path);
      case HostArtifact.webPlatformDDCKernelDill:
      case HostArtifact.webPlatformDDCSoundKernelDill:
      case HostArtifact.webPlatformDart2JSKernelDill:
      case HostArtifact.webPlatformDart2JSSoundKernelDill:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', _hostArtifactToFileName(artifact, _platform));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSdk:
      case HostArtifact.webPrecompiledSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd', _hostArtifactToFileName(artifact, _platform));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSdk:
      case HostArtifact.webPrecompiledCanvaskitSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit', _hostArtifactToFileName(artifact, _platform));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdk:
      case HostArtifact.webPrecompiledCanvaskitAndHtmlSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-html', _hostArtifactToFileName(artifact, _platform));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledSoundSdk:
      case HostArtifact.webPrecompiledSoundSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-sound', _hostArtifactToFileName(artifact, _platform));
        return _fileSystem.file(path);
      case HostArtifact.webPrecompiledCanvaskitSoundSdk:
      case HostArtifact.webPrecompiledCanvaskitSoundSdkSourcemaps:
        final String path = _fileSystem.path.join(_getFlutterWebSdkPath(), 'kernel', 'amd-canvaskit-sound', _hostArtifactToFileName(artifact, _platform));
        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));
        return _fileSystem.file(path);
      case HostArtifact.iosDeploy:
      case HostArtifact.idevicesyslog:
      case HostArtifact.idevicescreenshot:
      case HostArtifact.iproxy:
      case HostArtifact.skyEnginePath:
      case HostArtifact.impellerc:
      case HostArtifact.libtessellator:
        return _parent.getHostArtifact(artifact);
    }
  }

  String _getDartSdkPath() {
    // If we couldn't find a built dart sdk, let's look for a prebuilt one.
    final String prebuiltPath = _fileSystem.path.join(_getFlutterPrebuiltsPath(), _getPrebuiltTarget(), 'dart-sdk');
    if (_fileSystem.isDirectorySync(prebuiltPath)) {
      return prebuiltPath;
    }

    throw ToolExit('Unable to find a prebuilt dart sdk at: "$prebuiltPath"');
  }

  String _getFlutterPrebuiltsPath() {
    final String engineSrcPath = _fileSystem.path.dirname(_fileSystem.path.dirname(_webSdkPath));
    return _fileSystem.path.join(engineSrcPath, 'flutter', 'prebuilts');
  }

  String _getPrebuiltTarget() {
    final TargetPlatform hostPlatform = _currentHostPlatform(_platform, _operatingSystemUtils);
    switch (hostPlatform) {
      case TargetPlatform.darwin:
        return 'macos-x64';
      case TargetPlatform.linux_arm64:
        return 'linux-arm64';
      case TargetPlatform.linux_x64:
        return 'linux-x64';
      case TargetPlatform.windows_x64:
        return 'windows-x64';
      case TargetPlatform.ios:
      case TargetPlatform.android:
      case TargetPlatform.android_arm:
      case TargetPlatform.android_arm64:
      case TargetPlatform.android_x64:
      case TargetPlatform.android_x86:
      case TargetPlatform.fuchsia_arm64:
      case TargetPlatform.fuchsia_x64:
      case TargetPlatform.web_javascript:
      case TargetPlatform.tester:
        throwToolExit('Unsupported host platform: $hostPlatform');
    }
  }

  String _getFlutterWebSdkPath() {
    return _fileSystem.path.join(_webSdkPath, 'flutter_web_sdk');
  }

  @override
  bool get isLocalEngine => _parent.isLocalEngine;

  @override
  LocalEngineInfo? get localEngineInfo => _parent.localEngineInfo;
}

1210 1211 1212 1213 1214 1215 1216 1217
/// 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({
1218
    required this.parent,
1219
    this.frontendServer,
1220
    this.engineDartBinary,
1221 1222
    this.platformKernelDill,
    this.flutterPatchedSdk,
1223 1224 1225
  }) : assert(parent != null);

  final Artifacts parent;
1226 1227 1228 1229
  final File? frontendServer;
  final File? engineDartBinary;
  final File? platformKernelDill;
  final File? flutterPatchedSdk;
1230

1231 1232 1233
  @override
  LocalEngineInfo? get localEngineInfo => parent.localEngineInfo;

1234
  @override
1235 1236
  String getArtifactPath(
    Artifact artifact, {
1237 1238 1239
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
1240
  }) {
1241 1242 1243
    if (artifact == Artifact.engineDartBinary && engineDartBinary != null) {
      return engineDartBinary!.path;
    }
1244
    if (artifact == Artifact.frontendServerSnapshotForEngineDartSdk && frontendServer != null) {
1245
      return frontendServer!.path;
1246
    }
1247
    if (artifact == Artifact.platformKernelDill && platformKernelDill != null) {
1248
      return platformKernelDill!.path;
1249 1250
    }
    if (artifact == Artifact.flutterPatchedSdkPath && flutterPatchedSdk != null) {
1251
      return flutterPatchedSdk!.path;
1252
    }
1253 1254 1255 1256 1257 1258
    return parent.getArtifactPath(
      artifact,
      platform: platform,
      mode: mode,
      environmentType: environmentType,
    );
1259 1260 1261
  }

  @override
1262
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]) => parent.getEngineType(platform, mode);
1263 1264 1265

  @override
  bool get isLocalEngine => parent.isLocalEngine;
1266 1267 1268 1269 1270 1271 1272

  @override
  FileSystemEntity getHostArtifact(HostArtifact artifact) {
    return parent.getHostArtifact(
      artifact,
    );
  }
1273
}
1274 1275

/// Locate the Dart SDK.
1276 1277
String _dartSdkPath(Cache cache) {
  return cache.getRoot().childDirectory('dart-sdk').path;
1278
}
1279 1280

class _TestArtifacts implements Artifacts {
1281 1282 1283 1284
  _TestArtifacts(this.fileSystem);

  final FileSystem fileSystem;

1285 1286 1287
  @override
  LocalEngineInfo? get localEngineInfo => null;

1288
  @override
1289 1290
  String getArtifactPath(
    Artifact artifact, {
1291 1292 1293
    TargetPlatform? platform,
    BuildMode? mode,
    EnvironmentType? environmentType,
1294
  }) {
1295 1296 1297 1298 1299 1300 1301 1302
    final StringBuffer buffer = StringBuffer();
    buffer.write(artifact);
    if (platform != null) {
      buffer.write('.$platform');
    }
    if (mode != null) {
      buffer.write('.$mode');
    }
1303 1304 1305
    if (environmentType != null) {
      buffer.write('.$environmentType');
    }
1306 1307 1308 1309
    return buffer.toString();
  }

  @override
1310
  String getEngineType(TargetPlatform platform, [ BuildMode? mode ]) {
1311 1312 1313 1314 1315
    return 'test-engine';
  }

  @override
  bool get isLocalEngine => false;
1316 1317 1318 1319 1320

  @override
  FileSystemEntity getHostArtifact(HostArtifact artifact) {
    return fileSystem.file(artifact.toString());
  }
1321
}
1322

1323 1324 1325 1326 1327 1328 1329
class _TestLocalEngine extends _TestArtifacts {
  _TestLocalEngine(String engineOutPath, super.fileSystem) :
    localEngineInfo =
      LocalEngineInfo(
        engineOutPath: engineOutPath,
        localEngineName: fileSystem.path.basename(engineOutPath)
      );
1330 1331 1332 1333 1334

  @override
  bool get isLocalEngine => true;

  @override
1335
  final LocalEngineInfo localEngineInfo;
1336
}