Move compile_platform to entry_points

Change-Id: Ic24710de408d5033b39c0dec471c2effc56f8135
Reviewed-on: https://dart-review.googlesource.com/63104
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Peter von der Ahé <ahe@google.com>
diff --git a/pkg/front_end/tool/_fasta/compile_platform.dart b/pkg/front_end/tool/_fasta/compile_platform.dart
index d824723..c9791fb 100644
--- a/pkg/front_end/tool/_fasta/compile_platform.dart
+++ b/pkg/front_end/tool/_fasta/compile_platform.dart
@@ -2,162 +2,6 @@
 // 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.
 
-library fasta.compile_platform;
+import 'entry_points.dart' show compilePlatformEntryPoint;
 
-import 'dart:async' show Future;
-
-import 'dart:io' show File, Platform, exitCode;
-
-import 'package:kernel/target/targets.dart' show Target, TargetFlags, getTarget;
-
-import 'package:vm/bytecode/gen_bytecode.dart'
-    show generateBytecode, isKernelBytecodeEnabledForPlatform;
-
-import 'package:front_end/src/fasta/compiler_context.dart' show CompilerContext;
-
-import 'package:front_end/src/fasta/deprecated_problems.dart'
-    show deprecated_InputError;
-
-import 'package:front_end/src/fasta/get_dependencies.dart' show getDependencies;
-
-import 'package:front_end/src/fasta/kernel/utils.dart'
-    show writeComponentToFile;
-
-import 'package:front_end/src/fasta/severity.dart' show Severity;
-
-import 'package:front_end/src/fasta/util/relativize.dart' show relativizeUri;
-
-import 'package:front_end/src/kernel_generator_impl.dart'
-    show generateKernelInternal;
-
-import 'additional_targets.dart' show installAdditionalTargets;
-
-import 'command_line.dart' show withGlobalOptions;
-
-const int iterations = const int.fromEnvironment("iterations", defaultValue: 1);
-
-Future main(List<String> arguments) async {
-  installAdditionalTargets();
-  for (int i = 0; i < iterations; i++) {
-    if (i > 0) {
-      print("\n");
-    }
-    try {
-      await compilePlatform(arguments);
-    } on deprecated_InputError catch (e) {
-      exitCode = 1;
-      await CompilerContext.runWithDefaultOptions((c) => new Future<void>.sync(
-          () => c.report(deprecated_InputError.toMessage(e), Severity.error)));
-      return null;
-    }
-  }
-}
-
-Future compilePlatform(List<String> arguments) async {
-  await withGlobalOptions("compile_platform", arguments, false,
-      (CompilerContext c, List<String> restArguments) {
-    Uri hostPlatform = Uri.base.resolveUri(new Uri.file(restArguments[2]));
-    Uri outlineOutput = Uri.base.resolveUri(new Uri.file(restArguments[4]));
-    return compilePlatformInternal(
-        c, c.options.output, outlineOutput, hostPlatform);
-  });
-}
-
-Future compilePlatformInternal(CompilerContext c, Uri fullOutput,
-    Uri outlineOutput, Uri hostPlatform) async {
-  if (c.options.verbose) {
-    print("Generating outline of ${c.options.sdkRoot} into $outlineOutput");
-    print("Compiling ${c.options.sdkRoot} to $fullOutput");
-  }
-
-  var result =
-      await generateKernelInternal(buildSummary: true, buildComponent: true);
-  if (result == null) {
-    exitCode = 1;
-    // Note: an error should have been reported by now.
-    print('The platform .dill files were not created.');
-    return;
-  }
-  new File.fromUri(outlineOutput).writeAsBytesSync(result.summary);
-  c.options.ticker.logMs("Wrote outline to ${outlineOutput.toFilePath()}");
-
-  if (isKernelBytecodeEnabledForPlatform) {
-    generateBytecode(result.component, strongMode: c.options.strongMode);
-  }
-
-  await writeComponentToFile(result.component, fullOutput,
-      filter: (lib) => !lib.isExternal);
-
-  c.options.ticker.logMs("Wrote component to ${fullOutput.toFilePath()}");
-
-  List<Uri> deps = result.deps.toList();
-  for (Uri dependency in await computeHostDependencies(hostPlatform)) {
-    // Add the dependencies of the compiler's own sources.
-    if (dependency != outlineOutput) {
-      // We're computing the dependencies for [outlineOutput], so we shouldn't
-      // include it in the deps file.
-      deps.add(dependency);
-    }
-  }
-  await writeDepsFile(
-      fullOutput, new File(new File.fromUri(fullOutput).path + ".d").uri, deps);
-}
-
-Future<List<Uri>> computeHostDependencies(Uri hostPlatform) async {
-  // Returns a list of source files that make up the Fasta compiler (the files
-  // the Dart VM reads to run Fasta). Until Fasta is self-hosting (in strong
-  // mode), this is only an approximation, albeit accurate.  Once Fasta is
-  // self-hosting, this isn't an approximation. Regardless, strong mode
-  // shouldn't affect which files are read.
-  Target hostTarget = getTarget("vm", new TargetFlags(strongMode: true));
-  return getDependencies(Platform.script,
-      platform: hostPlatform, target: hostTarget);
-}
-
-Future writeDepsFile(
-    Uri output, Uri depsFile, List<Uri> allDependencies) async {
-  if (allDependencies.isEmpty) return;
-  String toRelativeFilePath(Uri uri) {
-    // Ninja expects to find file names relative to the current working
-    // directory. We've tried making them relative to the deps file, but that
-    // doesn't work for downstream projects. Making them absolute also
-    // doesn't work.
-    //
-    // We can test if it works by running ninja twice, for example:
-    //
-    //     ninja -C xcodebuild/ReleaseX64 -d explain compile_platform
-    //     ninja -C xcodebuild/ReleaseX64 -d explain compile_platform
-    //
-    // The second time, ninja should say:
-    //
-    //     ninja: Entering directory `xcodebuild/ReleaseX64'
-    //     ninja: no work to do.
-    //
-    // It's broken if it says something like this:
-    //
-    //     ninja explain: expected depfile 'vm_platform.dill.d' to mention \
-    //     'vm_platform.dill', got '/.../xcodebuild/ReleaseX64/vm_platform.dill'
-    return Uri.parse(relativizeUri(uri, base: Uri.base)).toFilePath();
-  }
-
-  StringBuffer sb = new StringBuffer();
-  sb.write(toRelativeFilePath(output));
-  sb.write(":");
-  List<String> paths = new List<String>(allDependencies.length);
-  for (int i = 0; i < allDependencies.length; i++) {
-    paths[i] = toRelativeFilePath(allDependencies[i]);
-  }
-  // Sort the relative paths to ease analyzing future changes to this code.
-  paths.sort();
-  String previous;
-  for (String path in paths) {
-    // Check for and omit duplicates.
-    if (path != previous) {
-      previous = path;
-      sb.write(" \\\n  ");
-      sb.write(path);
-    }
-  }
-  sb.writeln();
-  await new File.fromUri(depsFile).writeAsString("$sb");
-}
+main(List<String> arguments) => compilePlatformEntryPoint(arguments);
diff --git a/pkg/front_end/tool/_fasta/entry_points.dart b/pkg/front_end/tool/_fasta/entry_points.dart
index 2c27df0..a79de85 100644
--- a/pkg/front_end/tool/_fasta/entry_points.dart
+++ b/pkg/front_end/tool/_fasta/entry_points.dart
@@ -8,11 +8,16 @@
 
 import 'dart:convert' show jsonDecode, jsonEncode, LineSplitter, utf8;
 
-import 'dart:io' show File, exitCode, stderr, stdin, stdout;
+import 'dart:io' show File, Platform, exitCode, stderr, stdin, stdout;
 
 import 'package:kernel/kernel.dart'
     show CanonicalName, Library, Component, Source, loadComponentFromBytes;
 
+import 'package:kernel/target/targets.dart' show Target, TargetFlags, getTarget;
+
+import 'package:vm/bytecode/gen_bytecode.dart'
+    show generateBytecode, isKernelBytecodeEnabledForPlatform;
+
 import 'package:front_end/src/api_prototype/compiler_options.dart'
     show CompilerOptions;
 
@@ -26,6 +31,8 @@
 
 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget;
 
+import 'package:front_end/src/fasta/get_dependencies.dart' show getDependencies;
+
 import 'package:front_end/src/fasta/incremental_compiler.dart'
     show IncrementalCompiler;
 
@@ -41,6 +48,11 @@
 
 import 'package:front_end/src/fasta/uri_translator.dart' show UriTranslator;
 
+import 'package:front_end/src/fasta/util/relativize.dart' show relativizeUri;
+
+import 'package:front_end/src/kernel_generator_impl.dart'
+    show generateKernelInternal;
+
 import 'additional_targets.dart' show installAdditionalTargets;
 
 import 'command_line.dart' show withGlobalOptions;
@@ -83,9 +95,25 @@
   }
 }
 
+compilePlatformEntryPoint(List<String> arguments) async {
+  installAdditionalTargets();
+  for (int i = 0; i < iterations; i++) {
+    if (i > 0) {
+      print("\n");
+    }
+    try {
+      await compilePlatform(arguments);
+    } on deprecated_InputError catch (e) {
+      exitCode = 1;
+      await CompilerContext.runWithDefaultOptions((c) => new Future<void>.sync(
+          () => c.report(deprecated_InputError.toMessage(e), Severity.error)));
+      return null;
+    }
+  }
+}
+
 batchEntryPoint(List<String> arguments) {
   installAdditionalTargets();
-
   return new BatchCompiler(
           stdin.transform(utf8.decoder).transform(new LineSplitter()))
       .run();
@@ -294,3 +322,112 @@
   var platformComponent = loadComponentFromBytes(bytes);
   dillTarget.loader.appendLibraries(platformComponent, byteCount: bytes.length);
 }
+
+Future compilePlatform(List<String> arguments) async {
+  await withGlobalOptions("compile_platform", arguments, false,
+      (CompilerContext c, List<String> restArguments) {
+    Uri hostPlatform = Uri.base.resolveUri(new Uri.file(restArguments[2]));
+    Uri outlineOutput = Uri.base.resolveUri(new Uri.file(restArguments[4]));
+    return compilePlatformInternal(
+        c, c.options.output, outlineOutput, hostPlatform);
+  });
+}
+
+Future compilePlatformInternal(CompilerContext c, Uri fullOutput,
+    Uri outlineOutput, Uri hostPlatform) async {
+  if (c.options.verbose) {
+    print("Generating outline of ${c.options.sdkRoot} into $outlineOutput");
+    print("Compiling ${c.options.sdkRoot} to $fullOutput");
+  }
+
+  var result =
+      await generateKernelInternal(buildSummary: true, buildComponent: true);
+  if (result == null) {
+    exitCode = 1;
+    // Note: an error should have been reported by now.
+    print('The platform .dill files were not created.');
+    return;
+  }
+  new File.fromUri(outlineOutput).writeAsBytesSync(result.summary);
+  c.options.ticker.logMs("Wrote outline to ${outlineOutput.toFilePath()}");
+
+  if (isKernelBytecodeEnabledForPlatform) {
+    generateBytecode(result.component, strongMode: c.options.strongMode);
+  }
+
+  await writeComponentToFile(result.component, fullOutput,
+      filter: (lib) => !lib.isExternal);
+
+  c.options.ticker.logMs("Wrote component to ${fullOutput.toFilePath()}");
+
+  List<Uri> deps = result.deps.toList();
+  for (Uri dependency in await computeHostDependencies(hostPlatform)) {
+    // Add the dependencies of the compiler's own sources.
+    if (dependency != outlineOutput) {
+      // We're computing the dependencies for [outlineOutput], so we shouldn't
+      // include it in the deps file.
+      deps.add(dependency);
+    }
+  }
+  await writeDepsFile(
+      fullOutput, new File(new File.fromUri(fullOutput).path + ".d").uri, deps);
+}
+
+Future<List<Uri>> computeHostDependencies(Uri hostPlatform) async {
+  // Returns a list of source files that make up the Fasta compiler (the files
+  // the Dart VM reads to run Fasta). Until Fasta is self-hosting (in strong
+  // mode), this is only an approximation, albeit accurate.  Once Fasta is
+  // self-hosting, this isn't an approximation. Regardless, strong mode
+  // shouldn't affect which files are read.
+  Target hostTarget = getTarget("vm", new TargetFlags(strongMode: true));
+  return getDependencies(Platform.script,
+      platform: hostPlatform, target: hostTarget);
+}
+
+Future writeDepsFile(
+    Uri output, Uri depsFile, List<Uri> allDependencies) async {
+  if (allDependencies.isEmpty) return;
+  String toRelativeFilePath(Uri uri) {
+    // Ninja expects to find file names relative to the current working
+    // directory. We've tried making them relative to the deps file, but that
+    // doesn't work for downstream projects. Making them absolute also
+    // doesn't work.
+    //
+    // We can test if it works by running ninja twice, for example:
+    //
+    //     ninja -C xcodebuild/ReleaseX64 -d explain compile_platform
+    //     ninja -C xcodebuild/ReleaseX64 -d explain compile_platform
+    //
+    // The second time, ninja should say:
+    //
+    //     ninja: Entering directory `xcodebuild/ReleaseX64'
+    //     ninja: no work to do.
+    //
+    // It's broken if it says something like this:
+    //
+    //     ninja explain: expected depfile 'vm_platform.dill.d' to mention \
+    //     'vm_platform.dill', got '/.../xcodebuild/ReleaseX64/vm_platform.dill'
+    return Uri.parse(relativizeUri(uri, base: Uri.base)).toFilePath();
+  }
+
+  StringBuffer sb = new StringBuffer();
+  sb.write(toRelativeFilePath(output));
+  sb.write(":");
+  List<String> paths = new List<String>(allDependencies.length);
+  for (int i = 0; i < allDependencies.length; i++) {
+    paths[i] = toRelativeFilePath(allDependencies[i]);
+  }
+  // Sort the relative paths to ease analyzing future changes to this code.
+  paths.sort();
+  String previous;
+  for (String path in paths) {
+    // Check for and omit duplicates.
+    if (path != previous) {
+      previous = path;
+      sb.write(" \\\n  ");
+      sb.write(path);
+    }
+  }
+  sb.writeln();
+  await new File.fromUri(depsFile).writeAsString("$sb");
+}