[sdk][docsgen] Add reference docs as sdk_documentation to core_testing

Change-Id: Ia88867efb657b64d08e498e0a2fe26360b2d6473
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/684563
Reviewed-by: Dave Schuyler <dschuyler@google.com>
Reviewed-by: Nick Van der Auwermeulen <nickvander@google.com>
Reviewed-by: Oliver Newman <olivernewman@google.com>
Commit-Queue: Theodore Siu <theosiu@google.com>
diff --git a/build/sdk/sdk_atom.gni b/build/sdk/sdk_atom.gni
index 580c974..1f52aac 100644
--- a/build/sdk/sdk_atom.gni
+++ b/build/sdk/sdk_atom.gni
@@ -237,7 +237,6 @@
           "--output",
           rebase_path(current_api_file, root_build_dir),
         ]
-
         deps = gn_deps
 
         foreach(file, invoker.api_contents) {
diff --git a/build/sdk/sdk_documentation.gni b/build/sdk/sdk_documentation.gni
index c940adb..be0b94d 100644
--- a/build/sdk/sdk_documentation.gni
+++ b/build/sdk/sdk_documentation.gni
@@ -35,6 +35,8 @@
                            [
                              "category",
                              "files",
+                             "non_sdk_deps",
+                             "testonly",
                            ])
 
     id = "sdk://docs/${invoker.name}"
diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn
index 6cd3fd6..dd6e9a4 100644
--- a/sdk/BUILD.gn
+++ b/sdk/BUILD.gn
@@ -365,6 +365,7 @@
     "//sdk/fidl:fidl_testing",
     "//sdk/lib/sys/component/cpp/testing:cpp_library_sdk",
     "//sdk/lib/sys/cpp/testing:cpp_sdk",
+    "//tools/docsgen:ref_docs_sdk",
     "//zircon/system/ulib/async-testing:async-testing_sdk",
   ]
 }
diff --git a/sdk/manifests/core_testing.manifest b/sdk/manifests/core_testing.manifest
index 2021c07..83262b8 100644
--- a/sdk/manifests/core_testing.manifest
+++ b/sdk/manifests/core_testing.manifest
@@ -1,4 +1,5 @@
 sdk://docs/metadata_schemas
+sdk://docs/ref_docs
 sdk://fidl/fuchsia.component
 sdk://fidl/fuchsia.component.config
 sdk://fidl/fuchsia.component.decl
diff --git a/tools/docsgen/BUILD.gn b/tools/docsgen/BUILD.gn
index 3507ffd1..b551fcd3 100644
--- a/tools/docsgen/BUILD.gn
+++ b/tools/docsgen/BUILD.gn
@@ -7,6 +7,7 @@
 import("//build/dart/dart_packages.gni")
 import("//build/python/python_binary.gni")
 import("//build/python/python_host_test.gni")
+import("//build/sdk/sdk_documentation.gni")
 
 group("docsgen") {
   testonly = true
@@ -269,6 +270,56 @@
   deps = [ "//tools/cmc($host_toolchain)" ]
 }
 
+# Set up targets and files for sdk_documentation and build_api_module.
+ref_docs = [
+  {
+    target = ":dart_sdk_docs($host_toolchain)"
+    file = "dartdoc_out.zip"
+  },
+  {
+    target = ":invoke_clidoc($host_toolchain)"
+    file = "clidoc_out.tar.gz"
+  },
+  {
+    target = ":invoke_cmldoc($host_toolchain)"
+    file = "cmldoc_out/index.md"
+  },
+  {
+    target = ":invoke_fidldoc($host_toolchain)"
+    file = "fidldoc.zip"
+  },
+  {
+    target = ":invoke_helpdoc($host_toolchain)"
+    file = "helpdoc_out.tar.gz"
+  },
+  {
+    target = "//bundles:create_all_drivers_doc"
+    file = "all_drivers_doc.yaml"
+  },
+]
+
+ref_doc_files = []
+ref_doc_targets = []
+foreach(ref_doc, ref_docs) {
+  out_dir = get_label_info(ref_doc.target, "target_out_dir")
+  ref_doc_file_prefix = ref_doc.file
+
+  # Unlike other ref docs, drivers docs uses gen_dir instead of output_dir
+  if (ref_doc_file_prefix == "all_drivers_doc.yaml") {
+    gen_dir = get_label_info(ref_doc.target, "target_gen_dir")
+    ref_doc_file_path = "$gen_dir/$ref_doc_file_prefix"
+  } else {
+    ref_doc_file_path = "$out_dir/$ref_doc_file_prefix"
+  }
+  ref_doc_files += [
+    {
+      source = ref_doc_file_path
+      dest = "docs/$ref_doc_file_prefix"
+    },
+  ]
+  ref_doc_targets += [ ref_doc.target ]
+}
+
 # This exposes relevant reference doc paths for infra to upload.
 #
 # Type: list(scope)
@@ -309,12 +360,14 @@
 build_api_module("generated_docs") {
   testonly = true
   data_keys = [ "generated_docset" ]
-  deps = [
-    ":dart_sdk_docs($host_toolchain)",
-    ":invoke_clidoc($host_toolchain)",
-    ":invoke_cmldoc($host_toolchain)",
-    ":invoke_fidldoc($host_toolchain)",
-    ":invoke_helpdoc($host_toolchain)",
-    "//bundles:create_all_drivers_doc",
-  ]
+  deps = ref_doc_targets
+}
+
+sdk_documentation("ref_docs_sdk") {
+  name = "ref_docs"
+  testonly = true
+  category = "partner"
+
+  files = ref_doc_files
+  non_sdk_deps = ref_doc_targets
 }