toolchain.dart 1.85 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/process.dart';
Devon Carew's avatar
Devon Carew committed
12
import 'build_configuration.dart';
13 14

class Compiler {
15
  Compiler(this._path);
16

17
  String _path;
18 19 20

  Future<int> compile({
    String mainPath,
21 22 23
    String snapshotPath,
    String depfilePath,
    String buildOutputPath
24
  }) {
25
    final List<String> args = [
26
      _path,
27 28 29
      mainPath,
      '--package-root=${ArtifactStore.packageRoot}',
      '--snapshot=$snapshotPath'
30 31 32 33 34 35 36 37
    ];
    if (depfilePath != null) {
      args.add('--depfile=$depfilePath');
    }
    if (buildOutputPath != null) {
      args.add('--build-output=$buildOutputPath');
    }
    return runCommandAndStreamOutput(args);
38 39 40
  }
}

41
Future<String> _getCompilerPath(BuildConfiguration config) async {
42 43
  if (config.type != BuildType.prebuilt) {
    String compilerPath = path.join(config.buildDir, 'clang_x64', 'sky_snapshot');
44 45 46
    if (FileSystemEntity.isFileSync(compilerPath))
      return compilerPath;
    compilerPath = path.join(config.buildDir, 'sky_snapshot');
47 48 49 50
    if (FileSystemEntity.isFileSync(compilerPath))
      return compilerPath;
    return null;
  }
51 52 53 54 55
  Artifact artifact = ArtifactStore.getArtifact(
    type: ArtifactType.snapshot, hostPlatform: config.hostPlatform);
  return await ArtifactStore.getPath(artifact);
}

56 57 58 59 60 61
class Toolchain {
  Toolchain({ this.compiler });

  final Compiler compiler;

  static Future<Toolchain> forConfigs(List<BuildConfiguration> configs) async {
62 63 64 65 66 67
    for (BuildConfiguration config in configs) {
      String compilerPath = await _getCompilerPath(config);
      if (compilerPath != null)
        return new Toolchain(compiler: new Compiler(compilerPath));
    }
    return null;
68 69
  }
}