toolchain.dart 3.19 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:io';
6 7 8

import 'package:path/path.dart' as path;

9
import 'base/context.dart';
10
import 'build_info.dart';
11 12
import 'cache.dart';
import 'globals.dart';
13

14 15
enum HostTool {
  SkySnapshot,
16
  SkyShell,
17
}
18

19 20 21 22 23
const Map<HostTool, String> _kHostToolFileName = const <HostTool, String>{
  HostTool.SkySnapshot: 'sky_snapshot',
  HostTool.SkyShell: 'sky_shell',
};

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/// A ToolConfiguration can return the tools directory for the current host platform
/// and the engine artifact directory for a given target platform. It is configurable
/// via command-line arguments in order to support local engine builds.
class ToolConfiguration {
  /// [overrideCache] is configurable for testing.
  ToolConfiguration({ Cache overrideCache }) {
    _cache = overrideCache ?? cache;
  }

  Cache _cache;

  static ToolConfiguration get instance {
    if (context[ToolConfiguration] == null)
      context[ToolConfiguration] = new ToolConfiguration();
    return context[ToolConfiguration];
  }

  /// Override using the artifacts from the cache directory (--engine-src-path).
  String engineSrcPath;

44 45 46
  /// Path to a local engine build acting as a source for artifacts (--local-engine).
  String engineBuildPath;

47
  bool get isLocalEngine => engineSrcPath != null;
48 49 50

  /// Return the directory that contains engine artifacts for the given targets.
  /// This directory might contain artifacts like `libsky_shell.so`.
51 52
  Directory getEngineArtifactsDirectory(TargetPlatform platform, BuildMode mode) {
    Directory dir = _getEngineArtifactsDirectory(platform, mode);
53 54 55 56 57
    if (dir != null)
      printTrace('Using engine artifacts dir: ${dir.path}');
    return dir;
  }

58
  Directory _getEngineArtifactsDirectory(TargetPlatform platform, BuildMode mode) {
59 60
    if (engineBuildPath != null) {
      return new Directory(engineBuildPath);
61
    } else {
62
      String suffix = mode != BuildMode.debug ? '-${getModeName(mode)}' : '';
63

64
      // Create something like `android-arm` or `android-arm-release`.
Devon Carew's avatar
Devon Carew committed
65
      String dirName = getNameForTargetPlatform(platform) + suffix;
66 67 68 69
      Directory engineDir = _cache.getArtifactDirectory('engine');
      return new Directory(path.join(engineDir.path, dirName));
    }
  }
70 71

  String getHostToolPath(HostTool tool) {
72
    if (engineBuildPath == null) {
73 74
      return path.join(_cache.getArtifactDirectory('engine').path,
                       getNameForHostPlatform(getCurrentHostPlatform()),
75 76 77 78 79 80 81 82 83 84 85 86 87 88
                       _kHostToolFileName[tool]);
    }

    if (tool == HostTool.SkySnapshot) {
      String clangPath = path.join(engineBuildPath, 'clang_x64', 'sky_snapshot');
      if (FileSystemEntity.isFileSync(clangPath))
        return clangPath;
      return path.join(engineBuildPath, 'sky_snapshot');
    } else if (tool == HostTool.SkyShell) {
      if (getCurrentHostPlatform() == HostPlatform.linux_x64) {
        return path.join(engineBuildPath, 'sky_shell');
      } else if (getCurrentHostPlatform() == HostPlatform.darwin_x64) {
        return path.join(engineBuildPath, 'SkyShell.app', 'Contents', 'MacOS', 'SkyShell');
      }
89
    }
90 91

    throw 'Unexpected host tool: $tool';
92
  }
93
}