[GN] Hack around Dart 2 application_snapshot deps issue

For Dart 2 snapshots, application_snapshots() hardcodes a deps on
//utils/kernel-service:copy_kernel_service_snapshot

related #33167

Change-Id: I9a598e83a3e8ebb9a27bfe83132f3a957f2fea0e
Reviewed-on: https://dart-review.googlesource.com/56161
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Zach Anderson <zra@google.com>
diff --git a/utils/application_snapshot.gni b/utils/application_snapshot.gni
index 096b944..3630bee 100644
--- a/utils/application_snapshot.gni
+++ b/utils/application_snapshot.gni
@@ -15,6 +15,37 @@
   }
 }
 
+# Creates an app-jit snapshot for a Dart program based on a training run.
+#
+# Parameters:
+#  main_dart (required):
+#    The entrypoint to the Dart application.
+#
+#  training_args (required):
+#    Arguments to pass to the Dart application for the training run.
+#
+#  dart_version (optional):
+#    Must be 1 or 2. Defaults to 1.
+#
+#  vm_args (optional):
+#    Additional arguments to the Dart VM. Do not put --preview-dart-2 here.
+#    Instead set dart_version = 2.
+#
+#  name (optional):
+#    The name of the snapshot if different from the target name. The output
+#    will be in $root_gen_dir/$name.dart.snapshot.
+#
+#  extra_deps (optional):
+#    Any additional build dependencies.
+#
+#  extra_inputs (optional):
+#    Any extra build inputs.
+#
+#  dot_packages (optional):
+#    The .packages file for the app. Defaults to the $_dart_root/.packages.
+#
+#  output (optional):
+#    Overrides the full output path.
 template("application_snapshot") {
   assert(defined(invoker.main_dart), "Must specify 'main_dart'")
   assert(defined(invoker.training_args), "Must specify 'training_args'")
@@ -45,8 +76,25 @@
   if (defined(invoker.output)) {
     output = invoker.output
   }
+  dart_version = 1
+  if (defined(invoker.dart_version)) {
+    dart_version = invoker.dart_version
+  }
+  assert(dart_version == 1 || dart_version == 2,
+      "For $target_name, dart_version must be 1 or 2")
   dart_action(target_name) {
     deps = extra_deps
+
+    if (dart_version == 2) {
+      # HACK: When creating app-jit snapshots for Dart 2 apps, the standalone
+      # Dart VM binary requires the app-jit snapshot for the kernel service to
+      # be adjacent to it. This deps ensures that it is there, but a different
+      # approach will be needed when the kernel service itself switches to
+      # Dart 2 to avoid creating a circular dependence.
+      deps += [ "$_dart_root/utils/kernel-service:copy_kernel_service_snapshot"]
+      snapshot_vm_args += [ "--preview-dart-2" ]
+    }
+
     depfile = "$output.d"
 
     script = main_dart
diff --git a/utils/dartanalyzer/BUILD.gn b/utils/dartanalyzer/BUILD.gn
index 874ee7b..ff06c7b 100644
--- a/utils/dartanalyzer/BUILD.gn
+++ b/utils/dartanalyzer/BUILD.gn
@@ -21,9 +21,7 @@
                              "list lines")
 
 application_snapshot("generate_dartanalyzer_snapshot") {
-  vm_args = [
-    "--preview-dart-2",
-  ]
+  dart_version = 2
   main_dart = "../../pkg/analyzer_cli/bin/analyzer.dart"
   training_args = [
     "--dart-sdk=" + rebase_path("../../sdk"),
diff --git a/utils/dartdevc/BUILD.gn b/utils/dartdevc/BUILD.gn
index 0c5c4e2..6165c85 100644
--- a/utils/dartdevc/BUILD.gn
+++ b/utils/dartdevc/BUILD.gn
@@ -11,9 +11,7 @@
 sdk_dill = "$target_gen_dir/kernel/ddc_sdk.dill"
 
 application_snapshot("dartdevc") {
-  vm_args = [
-    "--preview-dart-2",
-  ]
+  dart_version = 2
 
   main_dart = "../../pkg/dev_compiler/bin/dartdevc.dart"
 
diff --git a/utils/pub/BUILD.gn b/utils/pub/BUILD.gn
index f34a69a..0adf27a 100644
--- a/utils/pub/BUILD.gn
+++ b/utils/pub/BUILD.gn
@@ -10,9 +10,7 @@
 }
 
 application_snapshot("pub2") {
+  dart_version = 2
   main_dart = "../../third_party/pkg/pub/bin/pub.dart"
   training_args = [ "--help" ]
-  vm_args = [
-    "--preview-dart-2",
-  ]
 }