[Fuchsia] Dynamically generate CIPD recipes from GN rules.

Produces CIPD YAML files and directories that will supercede the YAML
files checked in under //fuchsia/cipd.

The deprecated, checked-in CIPD files are left intact so as to allow
Infra to transition the official builders to the new rules. They will
be deleted once the transition is complete.

Bug: 974363
Change-Id: I87977a1c1259f19fcebe82b32064cac9c4214f89
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1661299
Reviewed-by: Fabrice de Gans-Riberi <fdegans@chromium.org>
Commit-Queue: Kevin Marshall <kmarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#669744}
diff --git a/fuchsia/BUILD.gn b/fuchsia/BUILD.gn
index 6ba6259..f665f80 100644
--- a/fuchsia/BUILD.gn
+++ b/fuchsia/BUILD.gn
@@ -5,8 +5,6 @@
 assert(is_fuchsia)
 
 import("//build/config/fuchsia/fidl_library.gni")
-import("//build/util/process_version.gni")
-import("//fuchsia/cipd/test_archive.gni")
 
 fidl_library("cast_fidl") {
   library_name = "cast"
@@ -24,7 +22,17 @@
   ]
 }
 
+# DEPRECATED. Use //fuchsia/cipd instead.
+# TODO(crbug.com/974363): Remove this.
 if (is_official_build) {
+  group("archive_sources") {
+    testonly = true
+    deps = [
+      ":release_archives",
+      ":symbol_tarballs",
+    ]
+  }
+
   # Location where Fuchsia release archives and supporting files are placed.
   _release_artifact_root = "$root_out_dir/fuchsia_artifacts"
 
@@ -39,49 +47,11 @@
     "$root_gen_dir/fuchsia/runners/web_runner/web_runner.symbols.tar.bz2",
   ]
   _symbol_manifest = "$target_gen_dir/debug_symbols.json"
-  write_file(_symbol_manifest, _symbol_tarballs, "json")
-
-  # gn binary location.
-  if (host_os == "mac") {
-    _gn_path = "//buildtools/mac/gn"
-  } else if (host_os == "linux") {
-    _gn_path = "//buildtools/linux64/gn"
+  _symbol_manifest_contents = []
+  foreach(_symbol_file, _symbol_tarballs) {
+    _symbol_manifest_contents += [ get_path_info(_symbol_file, "file") ]
   }
-
-  # Produces a LICENSE file for Fuchsia packages.
-  _license_path = "$_release_artifact_root/LICENSE"
-  action("license") {
-    script = "//tools/licenses.py"
-    inputs = [
-      "$_gn_path",
-    ]
-    outputs = [
-      _license_path,
-    ]
-    args = [
-      "license_file",
-      rebase_path(_license_path, root_build_dir),
-      "--gn-target",
-      "//fuchsia/runners:web_runner_pkg",
-      "--gn-out-dir",
-      ".",
-    ]
-  }
-
-  # Extracts the numeric Chrome build ID and writes it to a file in the output
-  # directory.
-  #
-  # To check out the repository on the commit where the build ID was generated,
-  # simply call `git checkout <build-id>`, and Git will check out the commit
-  # associated with the <build-id> tag.
-  process_version("build_id") {
-    template_file = "cipd/build_id.template"
-    sources = [
-      "//chrome/VERSION",
-    ]
-    output = "$_release_artifact_root/build_id.txt"
-    process_only = true
-  }
+  write_file(_symbol_manifest, _symbol_manifest_contents, "json")
 
   # Puts copies of files at the top level of the CIPD archive's structure.
   copy("release_archives") {
@@ -105,57 +75,21 @@
   copy("symbol_tarballs") {
     sources = [
                 _symbol_manifest,
-                "$_release_artifact_root/build_id.txt",
-                _license_path,
+                "${root_gen_dir}/fuchsia/cipd/build_id.txt",
+                "${root_gen_dir}/fuchsia/cipd/LICENSE",
               ] + _symbol_tarballs
     outputs = [
       "$_symbol_artifact_root/{{source_file_part}}",
     ]
     deps = [
-      ":build_id",
-      ":license",
+      "//fuchsia/cipd:build_id",
+      "//fuchsia/cipd:license",
       "//fuchsia/engine:symbol_archive",
       "//fuchsia/http:symbol_archive",
       "//fuchsia/runners:cast_runner_symbol_archive",
       "//fuchsia/runners:web_runner_symbol_archive",
     ]
   }
-
-  cipd_archive("test_archive") {
-    cipd_manifest_name = "tests.yaml"
-    cipd_path = "chromium/fuchsia/tests-\${targetarch}"
-    cipd_description = "Prebuilt Chromium tests for Fuchsia."
-    include_manifest = true
-
-    packages = [
-      "//base:base_unittests",
-      "//fuchsia/runners:cast_runner_integration_tests",
-      "//fuchsia/runners:web_runner_integration_tests",
-      "//ipc:ipc_tests",
-      "//media:media_unittests",
-      "//mojo:mojo_unittests",
-      "//skia:skia_unittests",
-      "//third_party/blink/common:blink_common_unittests",
-    ]
-
-    deps = [
-      ":build_id",
-      ":license",
-    ]
-  }
-
-  # Specifies the build steps that must be performed before the creation of
-  # a CIPD archive.
-  group("archive_sources") {
-    testonly = true
-    deps = [
-      ":build_id",
-      ":license",
-      ":release_archives",
-      ":symbol_tarballs",
-      ":test_archive",
-    ]
-  }
 }  # is_official_build
 
 # Used by the top-level "gn_all" target to discover Fuchsia build targets.
@@ -173,4 +107,8 @@
     "runners:web_runner",
     "//chromecast/bindings:bindings_manager_fuchsia",
   ]
+
+  if (is_official_build) {
+    deps += [ "cipd" ]
+  }
 }
diff --git a/fuchsia/cipd/BUILD.gn b/fuchsia/cipd/BUILD.gn
new file mode 100644
index 0000000..d8fed68
--- /dev/null
+++ b/fuchsia/cipd/BUILD.gn
@@ -0,0 +1,213 @@
+# Copyright 2019 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.
+
+# Build targets for constructing CIPD release archives.
+
+assert(is_fuchsia)
+
+import("//build/util/process_version.gni")
+
+# gn binary location.
+if (host_os == "mac") {
+  _gn_path = "//buildtools/mac/gn"
+} else if (host_os == "linux") {
+  _gn_path = "//buildtools/linux64/gn"
+}
+
+# Produces a consolidated license file.
+action("license") {
+  _license_path = "${target_gen_dir}/LICENSE"
+  script = "//tools/licenses.py"
+  inputs = [
+    "$_gn_path",
+  ]
+  outputs = [
+    _license_path,
+  ]
+  args = [
+    "license_file",
+    rebase_path(_license_path, root_build_dir),
+    "--gn-target",
+    "//fuchsia/runners:web_runner_pkg",
+    "--gn-out-dir",
+    ".",
+  ]
+}
+
+# Extracts the numeric Chrome build ID and writes it to a file in the output
+# directory.
+#
+# To check out the repository on the commit where the build ID was generated,
+# simply call `git checkout <build-id>`, and Git will check out the commit
+# associated with the <build-id> tag.
+process_version("build_id") {
+  template_file = "build_id.template"
+  sources = [
+    "//chrome/VERSION",
+  ]
+  output = "${target_gen_dir}/build_id.txt"
+  process_only = true
+}
+
+# Prepares a CIPD archive and generates a manifest file.
+#
+# Parameters:
+#   cipd_manifest_name: The filename to use for the generated CIPD YAML file.
+#   cipd_path: The path where the package will be located inside the CIPD
+#              repository.
+#   cipd_description: Sets the "description" field in CIPD metadata.
+#   deps: A list of targets to build prior to copying files.
+#   sources: A list of files to copy into the staging root.
+template("cipd_archive") {
+  forward_variables_from(invoker,
+                         [
+                           "cipd_manifest_name",
+                           "cipd_path",
+                           "cipd_description",
+                           "deps",
+                           "sources",
+                         ])
+  archive_staging_dir = "${target_gen_dir}/${target_name}"
+
+  yaml_contents = [
+    "package: ${cipd_path}",
+    "description: ${cipd_description}",
+    "root: \${outdir}/" + rebase_path(archive_staging_dir, root_build_dir),
+    "data:",
+    "  - file: LICENSE",
+  ]
+
+  if (!defined(deps)) {
+    deps = []
+  }
+  deps += [
+    ":build_id",
+    ":license",
+  ]
+
+  foreach(source, sources) {
+    yaml_contents += [ "  - file: " + get_path_info(source, "file") ]
+  }
+
+  sources += get_target_outputs(":license")
+  write_file("${archive_staging_dir}/${cipd_manifest_name}", yaml_contents)
+
+  copy(target_name) {
+    testonly = true
+    outputs = [
+      "${archive_staging_dir}/{{source_file_part}}",
+    ]
+  }
+}
+
+cipd_archive("webrunner") {
+  cipd_manifest_name = "webrunner.yaml"
+  cipd_path = "chromium/fuchsia/webrunner-\${targetarch}"
+  cipd_description = "Prebuilt Chrome and Web Runner binaries for Fuchsia."
+
+  deps = [
+    "//fuchsia/engine:web_engine",
+    "//fuchsia/runners:web_runner_pkg",
+  ]
+
+  sources = [
+    "${root_gen_dir}/fuchsia/engine/chromium/chromium.far",
+    "${root_gen_dir}/fuchsia/runners/web_runner/web_runner.far",
+  ]
+}
+
+cipd_archive("castrunner") {
+  cipd_manifest_name = "castrunner.yaml"
+  cipd_path = "chromium/fuchsia/castrunner-\${targetarch}"
+  cipd_description = "Prebuilt Cast application Runner binaries for Fuchsia."
+
+  deps = [
+    "//fuchsia/runners:cast_runner_pkg",
+  ]
+
+  sources = [
+    "${root_gen_dir}/fuchsia/runners/cast_runner/cast_runner.far",
+  ]
+}
+
+cipd_archive("tests") {
+  _manifest_path = "${target_gen_dir}/test_manifest.json"
+  cipd_manifest_name = "tests.yaml"
+  cipd_path = "chromium/fuchsia/tests-\${targetarch}"
+  cipd_description = "Prebuilt Chromium tests for Fuchsia."
+
+  deps = [
+    "//base:base_unittests_pkg",
+    "//fuchsia/runners:cast_runner_integration_tests_pkg",
+    "//fuchsia/runners:web_runner_integration_tests_pkg",
+    "//ipc:ipc_tests_pkg",
+    "//media:media_unittests_pkg",
+    "//mojo:mojo_unittests_pkg",
+    "//skia:skia_unittests_pkg",
+    "//third_party/blink/common:blink_common_unittests_pkg",
+  ]
+
+  far_sources = [
+    "${root_gen_dir}/base/base_unittests/base_unittests.far",
+    "${root_gen_dir}/fuchsia/runners/cast_runner_integration_tests/cast_runner_integration_tests.far",
+    "${root_gen_dir}/fuchsia/runners/web_runner_integration_tests/web_runner_integration_tests.far",
+    "${root_gen_dir}/ipc/ipc_tests/ipc_tests.far",
+    "${root_gen_dir}/media/media_unittests/media_unittests.far",
+    "${root_gen_dir}/mojo/mojo_unittests/mojo_unittests.far",
+    "${root_gen_dir}/skia/skia_unittests/skia_unittests.far",
+    "${root_gen_dir}/third_party/blink/common/blink_common_unittests/blink_common_unittests.far",
+  ]
+
+  # Build a JSON manifest of the tests and include it in the archive.
+  _manifest_contents = []
+  foreach(source, far_sources) {
+    package_name = get_path_info(source, "name")
+
+    _manifest_contents += [
+      {
+        package = package_name
+        component_name = package_name
+      },
+    ]
+  }
+  write_file(_manifest_path, _manifest_contents, "json")
+
+  sources = far_sources + [ _manifest_path ]
+}
+
+cipd_archive("debug_symbols") {
+  cipd_manifest_name = "debug_symbols.yaml"
+  cipd_path = "chromium/fuchsia/debug-symbols-\${targetarch}"
+  cipd_description = "Debugging symbols for prebuilt binaries from Chromium."
+
+  _symbol_tarballs = [
+    "${root_gen_dir}/fuchsia/engine/chromium/chromium.symbols.tar.bz2",
+    "${root_gen_dir}/fuchsia/runners/cast_runner/cast_runner.symbols.tar.bz2",
+    "${root_gen_dir}/fuchsia/runners/web_runner/web_runner.symbols.tar.bz2",
+  ]
+  _symbol_manifest = "$target_gen_dir/debug_symbols.json"
+  write_file(_symbol_manifest, _symbol_tarballs, "json")
+  _symbol_manifest_contents = []
+  foreach(_symbol_file, _symbol_tarballs) {
+    _symbol_manifest_contents += [ get_path_info(_symbol_file, "file") ]
+  }
+  write_file(_symbol_manifest, _symbol_manifest_contents, "json")
+
+  deps = [
+    "//fuchsia/engine:symbol_archive",
+    "//fuchsia/runners:cast_runner_symbol_archive",
+    "//fuchsia/runners:web_runner_symbol_archive",
+  ]
+  sources = [ _symbol_manifest ] + _symbol_tarballs
+}
+
+group("cipd") {
+  testonly = true
+  deps = [
+    ":castrunner",
+    ":debug_symbols",
+    ":tests",
+    ":webrunner",
+  ]
+}
diff --git a/fuchsia/cipd/test_archive.gni b/fuchsia/cipd/test_archive.gni
deleted file mode 100644
index 94c35a2e..0000000
--- a/fuchsia/cipd/test_archive.gni
+++ /dev/null
@@ -1,90 +0,0 @@
-# Copyright 2019 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.
-
-assert(is_fuchsia)
-
-# Prepares a staging directory for creating a CIPD archive of Fuchsia
-# packages. Generates a manifest JSON file used by consumers to discover
-# the set of available FARs.
-#
-# Parameters:
-#   cipd_manifest_name: The filename to use for the generated CIPD YAML file.
-#   cipd_path: The path where the package will be located inside the CIPD
-#              repository.
-#   cipd_description: Sets the "description" field in CIPD metadata.
-#   packages: A list of test targets whose FAR files will be included in the
-#             archive.
-#             Dependency links to these targets are implicitly created.
-#   include_manifest: If set, includes a JSON-encoded listing of FAR files in
-#                     the archive.
-template("cipd_archive") {
-  forward_variables_from(invoker,
-                         [
-                           "additional_files",
-                           "cipd_description",
-                           "cipd_manifest_name",
-                           "cipd_path",
-                           "deps",
-                           "include_manifest",
-                           "packages",
-                         ])
-  archive_staging_dir = "${target_gen_dir}/${target_name}"
-
-  manifest_contents = []
-  yaml_contents = [
-    "package: ${cipd_path}",
-    "description: ${cipd_description}",
-    "root: \${outdir}/" + rebase_path(archive_staging_dir, root_build_dir),
-    "data:",
-    "  - file: LICENSE",
-  ]
-
-  if (!defined(deps)) {
-    deps = []
-  }
-  deps += [
-    "//fuchsia:build_id",
-    "//fuchsia:license",
-  ]
-
-  copy_sources = [
-    "${root_out_dir}/fuchsia_artifacts/build_id.txt",
-    "${root_out_dir}/fuchsia_artifacts/LICENSE",
-  ]
-
-  package_deps = []
-  foreach(package, packages) {
-    package_name = get_label_info(package, "name")
-    package_deps += [ "${package}_pkg" ]
-
-    copy_sources += [ root_build_dir + "/gen" + get_label_info(package, "dir") +
-                      "/" + package_name + "/" + package_name + ".far" ]
-
-    manifest_contents += [
-      {
-        package = package_name
-        component_name = package_name
-      },
-    ]
-
-    yaml_contents += [ "  - file: ${package_name}.far" ]
-  }
-
-  if (defined(include_manifest) && include_manifest) {
-    yaml_contents += [ "  - file: test_manifest.json" ]
-    write_file("${archive_staging_dir}/test_manifest.json",
-               manifest_contents,
-               "json")
-  }
-  write_file("${archive_staging_dir}/${cipd_manifest_name}", yaml_contents)
-
-  copy(target_name) {
-    testonly = true
-    sources = copy_sources
-    deps += package_deps
-    outputs = [
-      "${archive_staging_dir}/{{source_file_part}}",
-    ]
-  }
-}