[cmx] include cmx tool in sdk

Modify rust build rules to support producing SDK atoms, and use the
added functionality to produce an SDK atom for the cmx tool.

Change-Id: I3e8e686e01f7dbc52fc21d3fc34e2bffd6f23554
diff --git a/cmx/cmx.gni b/cmx/cmx.gni
index 757204e..246ea53 100644
--- a/cmx/cmx.gni
+++ b/cmx/cmx.gni
@@ -63,7 +63,7 @@
                              "visibility",
                            ])
 
-    tool = "//build/tools/cmx:cmx_bin_test_rustc"
+    tool = "//build/tools/cmx"
     tool_output_name = "cmx"
 
     stamp_file = "$target_gen_dir/$target_name.verified"
@@ -130,7 +130,7 @@
                              "visibility",
                            ])
 
-    tool = "//build/tools/cmx:cmx_bin_test_rustc"
+    tool = "//build/tools/cmx"
     tool_output_name = "cmx"
 
     merged_output = "$target_out_dir/$target_name"
@@ -183,7 +183,7 @@
                              "visibility",
                            ])
 
-    tool = "//build/tools/cmx:cmx_bin_test_rustc"
+    tool = "//build/tools/cmx"
     tool_output_name = "cmx"
 
     formatted_output = "$target_out_dir/$target_name"
diff --git a/rust/rustc_artifact.gni b/rust/rustc_artifact.gni
index a890d6d..d63a4bc 100644
--- a/rust/rustc_artifact.gni
+++ b/rust/rustc_artifact.gni
@@ -6,6 +6,7 @@
 import("//build/fidl/toolchain.gni")
 import("//build/rust/config.gni")  # for rust_config
 import("//build/testing/test_spec.gni")
+import("//build/sdk/sdk_atom.gni")
 
 template("rustc_third_party_artifact") {
   # Dummy build target to match the one in rustc_artifact
@@ -258,6 +259,7 @@
   if (type == "bin" || type == "staticlib") {
     copy_target_name = "${target_name}_copy"
     group_deps += [ ":${copy_target_name}" ]
+    output_path = "${root_out_dir}/${prefix}${crate_name}${extension}"
     copy(copy_target_name) {
       forward_variables_from(invoker, [ "testonly" ])
       deps = [
@@ -267,9 +269,47 @@
         output_file,
       ]
       outputs = [
-        "${root_out_dir}/${prefix}${crate_name}${extension}",
+        output_path,
       ]
     }
+    # if appropriate, create an SDK atom for the binary/staticlib that we just
+    # copied
+    if (type == "bin" && defined(invoker.sdk_category) &&
+        invoker.sdk_category != "excluded" &&
+        !is_fuchsia && !(defined(invoker.test) && invoker.test)) {
+      output_name = target_name
+      file_base = "tools/$output_name"
+
+      sdk_atom("${target_name}_sdk") {
+        id = "sdk://tools/${output_name}"
+
+        category = invoker.sdk_category
+
+        meta = {
+          dest = "${file_base}-meta.json"
+          schema = "host_tool"
+          value = {
+            type = "host_tool"
+            name = output_name
+            root = "tools"
+            files = [ file_base ]
+          }
+        }
+
+        files = [
+          {
+            source = output_path
+            dest = file_base
+          },
+        ]
+
+        if (defined(invoker.sdk_deps)) {
+          deps = invoker.sdk_deps
+        }
+
+        non_sdk_deps = [ ":$copy_target_name" ]
+      }
+    }
   }
 
   cargo_toml_target_name = "${target_name}_cargo"
diff --git a/rust/rustc_binary.gni b/rust/rustc_binary.gni
index e41c20f..d3ad80d 100644
--- a/rust/rustc_binary.gni
+++ b/rust/rustc_binary.gni
@@ -31,6 +31,11 @@
 #     Builds unit tests associated with the binary. This will create a
 #     `<name>_bin_test` test file in the output directory.
 #
+#   sdk_category (optional)
+#     If this field is set, this rust binary will be included in SDK builds for
+#     the provided category. See //build/sdk/sdk_atom.gni for available
+#     categories.
+#
 #   source_root (optional)
 #     Location of the crate root (e.g. `src/main.rs` or `src/lib.rs`).
 #     This defaults to `./src/main.rs` for binaries and `./src/lib.rs` for libraries,
@@ -66,6 +71,7 @@
                              "source_root",
                              "testonly",
                              "with_lto",
+                             "sdk_category"
                            ])
     type = "bin"
   }
diff --git a/tools/cmx/BUILD.gn b/tools/cmx/BUILD.gn
index c6816f8..ec526d1 100644
--- a/tools/cmx/BUILD.gn
+++ b/tools/cmx/BUILD.gn
@@ -13,11 +13,13 @@
   ]
 }
 
-rustc_binary("cmx_bin_test_rustc") {
+rustc_binary("cmx") {
   name = "cmx"
   with_unit_tests = true
   edition = "2018"
 
+  sdk_category = "partner"
+
   deps = [
     "//third_party/rust-crates/rustc_deps:clap",
     "//third_party/rust-crates/rustc_deps:failure",