blob: e771fc50a52ea72da09e57a71d170614021647df [file] [log] [blame]
#!/usr/bin/env dart
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// Command line tool to merge the SDK libraries and our patch files.
/// This is currently designed as an offline tool, but we could automate it.
import 'dart:io';
import 'dart:isolate' show RawReceivePort;
import 'dart:async';
import 'dart:math' as math;
import 'package:analyzer/analyzer.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:path/path.dart' as path;
import 'package:front_end/src/fasta/fasta.dart'
show compilePlatform, writeDepsFile;
/// Set of input files that were read by this script to generate patched SDK.
/// We will dump it out into the depfile for ninja to use.
///
/// For more information see GN and Ninja references:
/// https://chromium.googlesource.com/chromium/src/+/56807c6cb383140af0c03da8f6731d77785d7160/tools/gn/docs/reference.md#depfile_string_File-name-for-input-dependencies-for-actions
/// https://ninja-build.org/manual.html#_depfile
///
final deps = new Set<Uri>();
/// Create [File] object from the given path and register it as a dependency.
File getInputFile(String path, {canBeMissing: false}) {
final file = new File(path);
if (!file.existsSync()) {
if (!canBeMissing) throw "patch_sdk.dart expects all inputs to exist";
return null;
}
deps.add(Uri.base.resolveUri(file.uri));
return file;
}
/// Read the given file synchronously as a string and register this path as
/// a dependency.
String readInputFile(String path, {canBeMissing: false}) =>
getInputFile(path, canBeMissing: canBeMissing)?.readAsStringSync();
Future main(List<String> argv) async {
var port = new RawReceivePort();
try {
await _main(argv);
} finally {
port.close();
}
}
Future _main(List<String> argv) async {
var base = path.fromUri(Platform.script);
var dartDir = path.dirname(path.dirname(path.absolute(base)));
var sdkDir = argv[0];
var packagesFile = argv[1];
var platformOut = argv[2];
Uri platform = Uri.base.resolveUri(new Uri.file('${platformOut}.tmp'));
Uri platformFinalLocation = Uri.base.resolveUri(new Uri.file(platformOut));
Uri packages = Uri.base.resolveUri(new Uri.file(packagesFile));
await compilePlatform(
Uri.base.resolveUri(new Uri.directory(sdkDir)), platform,
packages: packages, verbose: false);
final platformDeps = new File.fromUri(Uri.base.resolveUri(new Uri.file('${platformOut}.tmp.d'))).readAsStringSync();
await writeDepsFile(Platform.script,
Uri.base.resolveUri(new Uri.file("$platformOut.d")),
platformFinalLocation,
packages: packages,
platform: platform,
extraDependencies: new Set.from(platformDeps.split(': ')[1].split(' ').where((s) => s.isNotEmpty).map((path) => new Uri.file(path))),
verbose: false);
await new File.fromUri(platform).rename(platformFinalLocation.toFilePath());
}