Fix rust build in build.py

This copies the rust build result to the gn output directory to be
accessible when linking from C++. Also adds an option to use crates.io
for rust crates.

Bug: 176846220
Tag: #floss
Test: atest --host bluetooth_test_gd
Change-Id: I9f7732878a2aced6f80f363c25aff3243d34f0d5
diff --git a/build.py b/build.py
index 27332cf..131cbb2 100755
--- a/build.py
+++ b/build.py
@@ -318,9 +318,11 @@
         replace-with = "systembt"
         local-registry = "/nonexistent"
         """
-        contents = template.format(self.platform_dir)
-        with open(os.path.join(self.env['CARGO_HOME'], 'config'), 'w') as f:
-            f.write(contents)
+
+        if self.args.vendored_rust:
+            contents = template.format(self.platform_dir)
+            with open(os.path.join(self.env['CARGO_HOME'], 'config'), 'w') as f:
+                f.write(contents)
 
     def _rust_build(self):
         """ Run `cargo build` from platform2/bt directory.
@@ -349,6 +351,10 @@
         """ Build rust artifacts in an already prepared environment.
         """
         self._rust_build()
+        rust_dir = os.path.join(self._gn_default_output(), 'rust')
+        if os.path.exists(rust_dir):
+            shutil.rmtree(rust_dir)
+        shutil.copytree(os.path.join(self.output_dir, 'debug'), rust_dir)
 
     def _target_main(self):
         """ Build the main GN artifacts in an already prepared environment.
@@ -407,6 +413,7 @@
     parser.add_argument('--libdir', help='Libdir - default = usr/lib64', default='usr/lib64')
     parser.add_argument('--use-board', help='Use a built x86 board for dependencies. Provide path.')
     parser.add_argument('--jobs', help='Number of jobs to run', default=0, type=int)
+    parser.add_argument('--vendored-rust', help='Use vendored rust crates', default=False, action="store_true")
     parser.add_argument('--verbose', help='Verbose logs for build.')
 
     args = parser.parse_args()