From e19c23ef83b9930a3ca5fca012120c191164cb1a Mon Sep 17 00:00:00 2001
From: David Duarte <licorne@google.com>
Date: Thu, 28 Oct 2021 09:58:18 +0000
Subject: [PATCH] topshim/facade: Don't link with libbluetooth twice

libbluetooth was linked statically and dynamically causing a lot of
issue because of duplicated instances, to fix that we only link
with libbluetooth statically.

Linking statically with libluetooth is not straightforward because
of the dependency with bt_shim that depends on rust crates that
are used by bt_topshim_facade too, leading to linking twice with
theses libs and so the linker complaining of duplicate symbols.

To fix that we instead pass bt_shim as dependency of bt_topshim_facade
so the rust compiler will take care of only including the transitive
dependencies once

Test: m bt_topshim_facade
Test: out/host/linux-x86/bin/bt_topshim_facade

Change-Id: Ic24f383a4b2f9eda17f33dd344a90e416c482582
---
 system/btcore/Android.bp                  | 15 +++++++++++++--
 system/btif/Android.bp                    | 15 +++++++++++++--
 system/gd/rust/shim/Android.bp            | 23 ++++++++++++++++++++---
 system/gd/rust/topshim/facade/Android.bp  | 17 ++++++++++-------
 system/gd/rust/topshim/facade/src/main.rs | 12 ++++++++++++
 5 files changed, 68 insertions(+), 14 deletions(-)

diff --git a/system/btcore/Android.bp b/system/btcore/Android.bp
index d316943b3f8..5f2dd17c0b9 100644
--- a/system/btcore/Android.bp
+++ b/system/btcore/Android.bp
@@ -8,8 +8,8 @@ package {
     default_applicable_licenses: ["system_bt_license"],
 }
 
-cc_library_static {
-    name: "libbtcore",
+cc_defaults {
+    name: "libbtcore_defaults",
     defaults: ["fluoride_defaults"],
     local_include_dirs: ["include"],
     include_dirs: [
@@ -35,6 +35,17 @@ cc_library_static {
     },
 }
 
+cc_library_static {
+    name: "libbtcore",
+    defaults: ["libbtcore_defaults"],
+}
+
+cc_library_static {
+    name: "libbtcore-static",
+    defaults: ["libbtcore_defaults"],
+    cflags: ["-DSTATIC_LIBBLUETOOTH"],
+}
+
 cc_library_headers {
     name: "libbtcore_headers",
     defaults: ["libchrome_support_defaults"],
diff --git a/system/btif/Android.bp b/system/btif/Android.bp
index 640b82caab1..f2a156d4045 100644
--- a/system/btif/Android.bp
+++ b/system/btif/Android.bp
@@ -88,8 +88,8 @@ genrule {
 }
 
 // libbtif static library for target
-cc_library_static {
-    name: "libbtif",
+cc_defaults {
+    name: "libbtif_defaults",
     defaults: ["fluoride_defaults"],
     include_dirs: btifCommonIncludes,
     srcs: [
@@ -195,6 +195,17 @@ cc_library_static {
     host_supported: true,
 }
 
+cc_library_static {
+    name: "libbtif",
+    defaults: ["libbtif_defaults"],
+}
+
+cc_library_static {
+    name: "libbtif-static",
+    defaults: ["libbtif_defaults"],
+    cflags: ["-DSTATIC_LIBBLUETOOTH"],
+}
+
 // btif unit tests for target
 cc_test {
     name: "net_test_btif",
diff --git a/system/gd/rust/shim/Android.bp b/system/gd/rust/shim/Android.bp
index de909f6c157..3c35b628b43 100644
--- a/system/gd/rust/shim/Android.bp
+++ b/system/gd/rust/shim/Android.bp
@@ -26,8 +26,8 @@ cc_defaults {
     },
 }
 
-rust_ffi_static {
-    name: "libbt_shim_ffi",
+rust_defaults {
+    name: "libbt_shim_defaults",
     defaults: ["gd_rust_defaults"],
     crate_name: "bt_shim",
     srcs: ["src/lib.rs"],
@@ -51,8 +51,18 @@ rust_ffi_static {
     ],
 }
 
+rust_library_rlib {
+    name: "libbt_shim",
+    defaults: ["libbt_shim_defaults"],
+}
+
+rust_ffi_static {
+    name: "libbt_shim_ffi",
+    defaults: ["libbt_shim_defaults"],
+}
+
 cc_library_static {
-    name: "libbluetooth_rust_interop",
+    name: "libbt_shim_bridge",
     defaults: ["gd_ffi_defaults"],
     generated_headers: [
         "libbt_init_flags_bridge_header",
@@ -87,9 +97,16 @@ cc_library_static {
     shared_libs: [
         "libchrome",
     ],
+}
+
+cc_library_static {
+    name: "libbluetooth_rust_interop",
+    defaults: ["gd_ffi_defaults"],
     whole_static_libs: [
+        "libbt_shim_bridge",
         "libbt_shim_ffi",
     ],
+    host_supported: true,
 }
 
 cc_library_static {
diff --git a/system/gd/rust/topshim/facade/Android.bp b/system/gd/rust/topshim/facade/Android.bp
index bd94010e238..5b4446c8c7f 100644
--- a/system/gd/rust/topshim/facade/Android.bp
+++ b/system/gd/rust/topshim/facade/Android.bp
@@ -12,6 +12,7 @@ rust_binary_host {
     defaults: ["gd_rust_defaults"],
     crate_name: "bt_topshim_facade",
     srcs: ["src/main.rs"],
+    ld_flags: ["-fsanitize=undefined", "-fsanitize-minimal-runtime"],
     rustlibs: [
         "libbluetooth_rs",
         "libbt_common",
@@ -25,17 +26,18 @@ rust_binary_host {
         "libbt_facade_helpers",
         "libbt_topshim",
         "libbt_topshim_facade_protobuf",
+        "libbt_shim",
     ],
     static_libs: [
         "libbt_topshim_cxx",
         "libbt-bta",
         "libbt-common",
         "libbtdevice",
-        "libbtif",
+        "libbtif-static",
         "libbt-hci",
         "libbt-stack",
         "libbt-utils",
-        "libbtcore",
+        "libbtcore-static",
         "libosi",
         "libbt-protos-lite",
         "libbte",
@@ -46,18 +48,19 @@ rust_binary_host {
         "liblc3codec",
         "libudrv-uipc",
         "libbluetooth_gd", // Gabeldorsche
-        "libbluetooth_rust_interop",
         "libbluetooth-dumpsys",
+        "libbluetooth-types",
         "libflatbuffers-cpp",
+        "libbt_shim_bridge",
     ],
     shared_libs: [
         "libcrypto",
-        "libbluetooth",
         "libchrome",
+        "liblog",
+        "libcutils",
+        "libgrpc++",
+        "libgrpc_wrap"
     ],
-    sanitize: {
-        never: true,
-    },
     proc_macros: [
         "libpaste",
     ],
diff --git a/system/gd/rust/topshim/facade/src/main.rs b/system/gd/rust/topshim/facade/src/main.rs
index 0736933330e..44d27aa49b0 100644
--- a/system/gd/rust/topshim/facade/src/main.rs
+++ b/system/gd/rust/topshim/facade/src/main.rs
@@ -21,6 +21,18 @@ use tokio::runtime::Runtime;
 mod adapter_service;
 mod media_service;
 
+// This is needed for linking, libbt_shim_bridge needs symbols defined by
+// bt_shim, however bt_shim depends on rust crates (future, tokio) that
+// we use too, if we build and link them separately we ends with duplicate
+// symbols. To solve that we build bt_shim with bt_topshim_facade so the rust
+// compiler share the transitive dependencies.
+//
+// The `::*` is here to circuvent the single_component_path_imports from
+// 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 bt_shim::*;
+
 fn main() {
     let sigint = install_sigint();
     bt_common::init_logging();
-- 
GitLab