From 93e8c2f5106ded7912e4e99e177b468a10072425 Mon Sep 17 00:00:00 2001
From: Rahul Arya <aryarahul@google.com>
Date: Tue, 11 Apr 2023 20:18:23 +0000
Subject: [PATCH] [Connection Manager] Move libbluetooth_rs into stack

Rather than depending on libbluetooth-core, this exchanges the order
so that libbluetooth-core depends on libbluetooth_core_rs.

But since they are static libraries, circular symbol dependencies are
allowed, but header-only dependencies can only flow one way. This lets
C++ access types defined only in Rust, while the reverse direction
still works.

Bug: 272572974
Test: mm
Change-Id: I64d063520104c611374c467a66a9b516f36f334f
---
 android/app/Android.bp                       |  1 +
 system/audio_hal_interface/fuzzer/Android.bp |  2 +-
 system/btif/Android.bp                       |  1 +
 system/gd/rust/topshim/facade/Android.bp     |  2 ++
 system/gd/rust/topshim/facade/src/main.rs    |  2 ++
 system/main/Android.bp                       |  3 ---
 system/rust/Android.bp                       | 12 ++++++++++--
 system/stack/Android.bp                      |  2 ++
 system/stack/test/fuzzers/Android.bp         |  1 +
 system/test/headless/Android.bp              |  1 +
 system/test/suite/Android.bp                 |  1 +
 11 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/android/app/Android.bp b/android/app/Android.bp
index 515bee826cf..6965514a36a 100644
--- a/android/app/Android.bp
+++ b/android/app/Android.bp
@@ -69,6 +69,7 @@ cc_library_shared {
     static_libs: [
         "libbluetooth-types",
         "libbluetooth",
+        "libbluetooth_core_rs",
         "libc++fs",
         "libbluetooth_core_rs_bridge",
     ],
diff --git a/system/audio_hal_interface/fuzzer/Android.bp b/system/audio_hal_interface/fuzzer/Android.bp
index 6d07b5e2031..1715d027ebf 100644
--- a/system/audio_hal_interface/fuzzer/Android.bp
+++ b/system/audio_hal_interface/fuzzer/Android.bp
@@ -53,6 +53,7 @@ cc_defaults {
         "libbluetooth-dumpsys",
         "libbluetooth-protos",
         "libbluetooth-types",
+        "libbluetooth_core_rs",
         "libbluetooth_gd",
         "libbluetooth_rust_interop",
         "libbt-audio-hal-interface",
@@ -65,7 +66,6 @@ cc_defaults {
         "libbt-stack",
         "libbt-stack-core",
         "libbt_shim_bridge",
-        "libbt_shim_ffi",
         "libbtcore",
         "libbtdevice",
         "libbte",
diff --git a/system/btif/Android.bp b/system/btif/Android.bp
index c670e9a9a23..8a51cb846be 100644
--- a/system/btif/Android.bp
+++ b/system/btif/Android.bp
@@ -268,6 +268,7 @@ cc_test {
         "android.system.suspend.control-V1-ndk",
         "libFraunhoferAAC",
         "libbluetooth-types",
+        "libbluetooth_core_rs",
         "libbt-audio-hal-interface",
         "libbt-bta",
         "libbt-bta-core",
diff --git a/system/gd/rust/topshim/facade/Android.bp b/system/gd/rust/topshim/facade/Android.bp
index 711300e9253..1d2c0f6f2a0 100644
--- a/system/gd/rust/topshim/facade/Android.bp
+++ b/system/gd/rust/topshim/facade/Android.bp
@@ -21,6 +21,7 @@ rust_defaults {
     ],
     rustlibs: [
         "libbluetooth_rs",
+        "libbluetooth_core_rs_for_facade",
         "libbt_common",
         "libbt_facade_helpers",
         "libbt_packets",
@@ -45,6 +46,7 @@ rust_defaults {
         "libaudio-a2dp-hw-utils",
         "libbluetooth-dumpsys",
         "libbluetooth-types",
+        "libbluetooth_core_rs",
         "libbluetooth_gd", // Gabeldorsche
         "libbt-audio-hal-interface",
         "libbt-bta",
diff --git a/system/gd/rust/topshim/facade/src/main.rs b/system/gd/rust/topshim/facade/src/main.rs
index 139a62faf62..e5b4ce30c73 100644
--- a/system/gd/rust/topshim/facade/src/main.rs
+++ b/system/gd/rust/topshim/facade/src/main.rs
@@ -31,6 +31,8 @@ mod utils;
 // clippy that is denied on the rust command line so we can't just allow it.
 // This is fine for now since bt_shim doesn't export anything
 #[allow(unused)]
+use bluetooth_core_rs_for_facade::*;
+#[allow(unused)]
 use bt_shim::*;
 
 fn main() {
diff --git a/system/main/Android.bp b/system/main/Android.bp
index ea91ce872d0..647b43bce53 100644
--- a/system/main/Android.bp
+++ b/system/main/Android.bp
@@ -88,9 +88,6 @@ cc_library {
         "packages/modules/Bluetooth/system/vnd/include",
         "system/security/keystore/include",
     ],
-    whole_static_libs: [
-        "libbluetooth_core_rs",
-    ],
     shared_libs: [
         "android.hardware.bluetooth@1.0",
         "android.hardware.bluetooth@1.1",
diff --git a/system/rust/Android.bp b/system/rust/Android.bp
index 9cb585642be..a9cb124ef2a 100644
--- a/system/rust/Android.bp
+++ b/system/rust/Android.bp
@@ -22,7 +22,6 @@ rust_defaults {
         "libbase",
     ],
     static_libs: [
-        "libbluetooth-core",
         "libbt_shim_bridge",
         "libbt_shim_ffi",
 
@@ -71,6 +70,16 @@ rust_defaults {
     apex_available: ["com.android.btservices"],
 }
 
+rust_library {
+    name: "libbluetooth_core_rs_for_facade",
+    crate_name: "bluetooth_core_rs_for_facade",
+    defaults: ["libbluetooth_core_rs_defaults"],
+    rustlibs: [
+        "libtokio",
+    ],
+    host_supported: true,
+}
+
 rust_ffi_static {
     name: "libbluetooth_core_rs",
     crate_name: "bluetooth_core",
@@ -115,7 +124,6 @@ cc_library_static {
     ],
     export_include_dirs: ["."],
     static_libs: [
-        "libbluetooth-core",
         "libbt_shim_bridge",
         "libbt_shim_ffi",
     ],
diff --git a/system/stack/Android.bp b/system/stack/Android.bp
index 1919ff9c4f4..0b166e1aa15 100644
--- a/system/stack/Android.bp
+++ b/system/stack/Android.bp
@@ -282,6 +282,7 @@ cc_library_static {
     ],
     static_libs: [
         "libbt-hci",
+        "libbluetooth_core_rs",
     ],
     host_supported: true,
     min_sdk_version: "Tiramisu",
@@ -342,6 +343,7 @@ cc_test {
         "libbt-sbc-encoder",
         "libbtif",
         "libbtif-core",
+        "libbluetooth_core_rs",
         "libFraunhoferAAC",
         "libbt-hci",
         "libbtdevice",
diff --git a/system/stack/test/fuzzers/Android.bp b/system/stack/test/fuzzers/Android.bp
index 399215b6086..65dcc620af1 100644
--- a/system/stack/test/fuzzers/Android.bp
+++ b/system/stack/test/fuzzers/Android.bp
@@ -31,6 +31,7 @@ cc_defaults {
         "libbt-sbc-decoder",
         "libbt-sbc-encoder",
         "libbtif",
+        "libbluetooth_core_rs",
         "libbtif-core",
         "libbt-hci",
         "libbtdevice",
diff --git a/system/test/headless/Android.bp b/system/test/headless/Android.bp
index beff3d36ac9..6d81ac16ce5 100644
--- a/system/test/headless/Android.bp
+++ b/system/test/headless/Android.bp
@@ -85,6 +85,7 @@ cc_binary {
         "libaudio-a2dp-hw-utils",
         "libbluetooth-dumpsys",
         "libbluetooth_gd",
+        "libbluetooth_core_rs",
         "libbluetooth_rust_interop",
         "libbt-audio-hal-interface",
         "libbt-bta",
diff --git a/system/test/suite/Android.bp b/system/test/suite/Android.bp
index 383f512f8e1..ee9dc6020ee 100644
--- a/system/test/suite/Android.bp
+++ b/system/test/suite/Android.bp
@@ -70,6 +70,7 @@ cc_defaults {
         "lib-bt-packets-base",
         "libFraunhoferAAC",
         "libaudio-a2dp-hw-utils",
+        "libbluetooth_core_rs",
         "libbluetooth-dumpsys",
         "libbluetooth-types",
         "libbt-audio-hal-interface",
-- 
GitLab