toolchain.dart 4.88 KB
Newer Older
1 2 3 4 5
// 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.

import 'dart:async';
6
import 'dart:io';
7 8 9 10

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

import 'artifacts.dart';
11
import 'base/context.dart';
12
import 'base/process.dart';
Devon Carew's avatar
Devon Carew committed
13
import 'build_configuration.dart';
14 15
import 'cache.dart';
import 'globals.dart';
16
import 'package_map.dart';
17

18 19
class SnapshotCompiler {
  SnapshotCompiler(this._path);
20

21
  final String _path;
22

23
  Future<int> createSnapshot({
24
    String mainPath,
25 26 27
    String snapshotPath,
    String depfilePath,
    String buildOutputPath
28
  }) {
Devon Carew's avatar
Devon Carew committed
29 30 31
    assert(mainPath != null);
    assert(snapshotPath != null);

32
    final List<String> args = <String>[
33
      _path,
34
      mainPath,
35
      '--packages=${PackageMap.instance.packagesPath}',
36
      '--snapshot=$snapshotPath'
37
    ];
38
    if (depfilePath != null)
39
      args.add('--depfile=$depfilePath');
40
    if (buildOutputPath != null)
41 42
      args.add('--build-output=$buildOutputPath');
    return runCommandAndStreamOutput(args);
43 44 45
  }
}

46 47
// TODO(devoncarew): This should instead take a host platform and target platform.

Devon Carew's avatar
Devon Carew committed
48
String _getCompilerPath(BuildConfiguration config) {
49 50
  if (config.type != BuildType.prebuilt) {
    String compilerPath = path.join(config.buildDir, 'clang_x64', 'sky_snapshot');
51 52 53
    if (FileSystemEntity.isFileSync(compilerPath))
      return compilerPath;
    compilerPath = path.join(config.buildDir, 'sky_snapshot');
54 55 56 57
    if (FileSystemEntity.isFileSync(compilerPath))
      return compilerPath;
    return null;
  }
58 59
  Artifact artifact = ArtifactStore.getArtifact(
    type: ArtifactType.snapshot, hostPlatform: config.hostPlatform);
Devon Carew's avatar
Devon Carew committed
60
  return ArtifactStore.getPath(artifact);
61 62
}

63 64 65
class Toolchain {
  Toolchain({ this.compiler });

66
  final SnapshotCompiler compiler;
67

Devon Carew's avatar
Devon Carew committed
68
  static Toolchain forConfigs(List<BuildConfiguration> configs) {
69
    for (BuildConfiguration config in configs) {
Devon Carew's avatar
Devon Carew committed
70
      String compilerPath = _getCompilerPath(config);
71
      if (compilerPath != null)
72
        return new Toolchain(compiler: new SnapshotCompiler(compilerPath));
73 74
    }
    return null;
75 76
  }
}
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

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

  /// The engine mode to use (only relevent when [engineSrcPath] is set).
99
  bool engineRelease;
100

101
  bool get isLocalEngine => engineSrcPath != null;
102 103 104

  /// Return the directory that contains engine artifacts for the given targets.
  /// This directory might contain artifacts like `libsky_shell.so`.
105 106
  Directory getEngineArtifactsDirectory(TargetPlatform platform, BuildMode mode) {
    Directory dir = _getEngineArtifactsDirectory(platform, mode);
107 108 109 110 111
    if (dir != null)
      printTrace('Using engine artifacts dir: ${dir.path}');
    return dir;
  }

112
  Directory _getEngineArtifactsDirectory(TargetPlatform platform, BuildMode mode) {
113 114
    if (engineSrcPath != null) {
      List<String> buildDir = <String>[];
115 116 117 118

      switch (platform) {
        case TargetPlatform.android_arm:
        case TargetPlatform.android_x64:
119
        case TargetPlatform.android_x86:
120
          buildDir.add('android');
121 122 123 124
          break;

        // TODO(devoncarew): We will need an ios vs ios_x86 target (for ios vs. ios_sim).
        case TargetPlatform.ios:
125
          buildDir.add('ios');
126 127 128 129 130
          break;

        // These targets don't have engine artifacts.
        case TargetPlatform.darwin_x64:
        case TargetPlatform.linux_x64:
131 132
          buildDir.add('host');
          break;
133 134
      }

135 136 137 138
      buildDir.add(getModeName(mode));

      if (!engineRelease)
        buildDir.add('unopt');
139

140 141 142
      // Add a suffix for the target architecture.
      switch (platform) {
        case TargetPlatform.android_x64:
143
          buildDir.add('x64');
144 145
          break;
        case TargetPlatform.android_x86:
146
          buildDir.add('x86');
147 148 149 150 151
          break;
        default:
          break;
      }

152
      return new Directory(path.join(engineSrcPath, 'out', buildDir.join('_')));
153
    } else {
154
      String suffix = mode != BuildMode.debug ? '-${getModeName(mode)}' : '';
155

156
      // Create something like `android-arm` or `android-arm-release`.
Devon Carew's avatar
Devon Carew committed
157
      String dirName = getNameForTargetPlatform(platform) + suffix;
158 159 160 161 162
      Directory engineDir = _cache.getArtifactDirectory('engine');
      return new Directory(path.join(engineDir.path, dirName));
    }
  }
}