[dart_component] Don't pass -Ddart.vm.product=true in profile mode

Passing this flag in an AOT build results in the vm service isolate
code being tree-shaken away. See the pragma on main around here:

https://github.com/dart-lang/sdk/blob/master/runtime/bin/vmservice/vmservice_io.dart#L240

Change-Id: Iacf374d759a5e07ee50be3a63a619f6aa95bef13
diff --git a/runtime/dart/dart_component.gni b/runtime/dart/dart_component.gni
index 376fa85..2f58fba 100644
--- a/runtime/dart/dart_component.gni
+++ b/runtime/dart/dart_component.gni
@@ -430,6 +430,10 @@
         component_name,
       ]
 
+      # The variable 'product' is already defined in this scope. This line makes
+      # explicit that that definition is passed to dart_kernel().
+      product = product
+
       # TODO(CP-140): Should be forwarded from the component.
       non_dart_deps = []
       if (defined(invoker.non_dart_deps)) {
@@ -780,6 +784,11 @@
       }
       sources_required = false
 
+      # The variable 'product' is already defined in this scope. This line makes
+      # explicit that that definition is passed to dart_kernel().
+      product = product
+      aot = true
+
       # TODO(CP-140): These variables should be from the component
       forward_variables_from(invoker,
                              [
@@ -796,10 +805,6 @@
       }
 
       main_dart = component.main_dart
-      args = [
-        "--aot",
-        "--tfa",
-      ]
     }
 
     # TODO(rmacnak): VM snapshot is ignored. Allow skipping its generation.
diff --git a/runtime/dart/dart_kernel.gni b/runtime/dart/dart_kernel.gni
index 9017f31..7f8d945 100644
--- a/runtime/dart/dart_kernel.gni
+++ b/runtime/dart/dart_kernel.gni
@@ -41,6 +41,12 @@
 #  manifest (optional)
 #    Path to an output manifest file containing the list of partial dill files.
 #
+#  product (optional)
+#    Whether this is a product build. Defaults to false.
+#
+#  aot (optional)
+#    Whether this is an AOT build. Defaults to false.
+#
 #  args (optional)
 #    A list of additional arguments to the compiler.dart program in this
 #    directory that generates the kernel files.
@@ -56,6 +62,20 @@
   assert(defined(invoker.platform_name), "dart_kernel() requires platform_name")
   assert(defined(invoker.main_dart), "dart_kernel() requires main_dart")
 
+  args = []
+  if (defined(invoker.args)) {
+    args += invoker.args
+  }
+
+  aot = false
+  if (defined(invoker.aot) && invoker.aot) {
+    aot = invoker.aot
+  }
+  product = false
+  if (defined(invoker.product) && invoker.product) {
+    product = invoker.product
+  }
+
   dart_library_target_name = "${target_name}_dart_library"
   dart_library(dart_library_target_name) {
     forward_variables_from(invoker,
@@ -122,7 +142,7 @@
     multi_root_scheme = "fuchsia-source"
 
     script = _compiler_path
-    args = invoker.args + [
+    args += [
              # TODO(https://github.com/dart-lang/sdk/issues/36639):
              # Remove when new constant eval supports dilp files.
              "--enable-experiment=no-constant-update-2018",
@@ -153,7 +173,28 @@
     if (is_debug) {
       args += [ "--embed-sources" ]
     } else {
-      args += [ "--no-embed-sources", "-Ddart.vm.product=true" ]
+      args += [ "--no-embed-sources" ]
+    }
+
+    if (aot) {
+      args += [
+        "--aot",
+        "--tfa",
+      ]
+    }
+
+    if (product) {
+      # Setting this flag in a non-product release build for AOT (a "profile"
+      # build) causes the vm service isolate code to be tree-shaken from an app.
+      # See the pragma on the entrypoint here:
+      #
+      # https://github.com/dart-lang/sdk/blob/master/runtime/bin/vmservice/vmservice_io.dart#L240
+      #
+      # To make the flags uniform across AOT and JIT, we also exclude this flag
+      # from a JIT profile build.
+      args += [
+        "-Ddart.vm.product=true",
+      ]
     }
 
     if (defined(invoker.gen_bytecode) && invoker.gen_bytecode) {
diff --git a/runtime/dart/flutter_test.gni b/runtime/dart/flutter_test.gni
index 280e7fb..1503c59 100644
--- a/runtime/dart/flutter_test.gni
+++ b/runtime/dart/flutter_test.gni
@@ -123,6 +123,9 @@
           main_dart = _bootstrap_filename
           args = []
 
+          product = false
+          aot = false
+
           # TODO(tvolkert): Change to flutter platform once libraries.json works
           #                 in Fuchsia
           # platform_deps = [
diff --git a/runtime/dart_runner/embedder/BUILD.gn b/runtime/dart_runner/embedder/BUILD.gn
index 9b52153..fd2b918 100644
--- a/runtime/dart_runner/embedder/BUILD.gn
+++ b/runtime/dart_runner/embedder/BUILD.gn
@@ -13,7 +13,24 @@
   disable_analysis = true
 
   main_dart = "shim.dart"
-  args = [ "--aot" ]
+
+  aot = true
+  product = false
+
+  visibility = [ ":*" ]
+}
+
+dart_kernel("shim_product") {
+  platform_name = "dart_runner"
+  platform_deps = [ "//topaz/runtime/dart_runner/kernel:kernel_platform_files" ]
+  platform_path = "$root_out_dir/dart_runner_patched_sdk"
+
+  disable_analysis = true
+
+  main_dart = "shim.dart"
+
+  aot = true
+  product = true
 
   visibility = [ ":*" ]
 }
@@ -28,9 +45,9 @@
     snapshot_assembly = "$target_gen_dir/aot${product_suffix}_vm_snapshot.S"
 
     # gen_snapshot only needs this to go through the motions of setting up an isolate.
-    shim_target = ":shim_kernel"
+    shim_target = ":shim${product_suffix}_kernel"
     shim_kernel =
-        get_label_info(shim_target, "target_gen_dir") + "/shim_kernel.dil"
+        get_label_info(shim_target, "target_gen_dir") + "/shim${product_suffix}_kernel.dil"
 
     inputs = [
       shim_kernel,
diff --git a/runtime/flutter_runner/kernel/BUILD.gn b/runtime/flutter_runner/kernel/BUILD.gn
index 8a00839..8282b3e 100644
--- a/runtime/flutter_runner/kernel/BUILD.gn
+++ b/runtime/flutter_runner/kernel/BUILD.gn
@@ -46,6 +46,23 @@
   manifest = "$target_gen_dir/framework_shim.dilpmanifest"
 }
 
+dart_kernel("framework_shim_product") {
+  platform_name = "flutter_runner"
+  platform_deps =
+      [ "//topaz/runtime/flutter_runner/kernel:kernel_platform_files" ]
+  platform_path = "$root_out_dir/flutter_runner_patched_sdk"
+  disable_analysis = true
+  gen_bytecode = true
+  product = true
+  args = []
+
+  main_dart = "framework_shim.dart"
+  deps = [
+    "//third_party/dart-pkg/git/flutter/packages/flutter",
+  ]
+  manifest = "$target_gen_dir/framework_shim_product.dilpmanifest"
+}
+
 template("core_snapshot") {
   assert(defined(invoker.product),
          "core_snapshot requires 'product' to be defined")
@@ -60,6 +77,11 @@
     suffix = "${suffix}_framework"
   }
 
+  product_suffix = ""
+  if (invoker.product) {
+    product_suffix = "_product"
+  }
+
   action(target_name) {
     deps = gen_snapshot_deps + [ ":kernel_platform_files" ]
 
@@ -121,9 +143,9 @@
 
     if (invoker.framework) {
       if (prebuilt_framework_path == "") {
-        deps += [ ":framework_shim_kernel" ]
-        inputs += [ "$target_gen_dir/framework_shim_kernel.dil" ]
-        args += [ rebase_path("$target_gen_dir/framework_shim_kernel.dil") ]
+        deps += [ ":framework_shim${product_suffix}_kernel" ]
+        inputs += [ "$target_gen_dir/framework_shim${product_suffix}_kernel.dil" ]
+        args += [ rebase_path("$target_gen_dir/framework_shim${product_suffix}_kernel.dil") ]
       } else {
         deps += [ ":extract_prebuilt_framework" ]
         foreach(package, framework_packages) {
@@ -133,6 +155,8 @@
               [ "$target_gen_dir/data/$prebuilt_framework_name/$package.dilp" ]
         }
       }
+    } else {
+      not_needed(["product_suffix"])
     }
   }
 }