OSDN Git Service

Add rust binaries to link with C++ staticlib
authorAbhishek Pandit-Subedi <abhishekpandit@chromium.org>
Mon, 15 Mar 2021 20:54:19 +0000 (13:54 -0700)
committerAbhishek Pandit-Subedi <abhishekpandit@google.com>
Mon, 3 May 2021 22:29:53 +0000 (22:29 +0000)
Rather than using C++ binaries to link with Rust staticlib, we do it the
other way around. This allows us to write our dbus frontend with Rust
instead.

Bug: 179821440
Tag: #floss
Test: atest --host bluetooth_test_gd
Change-Id: I63037e0aaea152a4855e0a49971c1b2f9c312ac5

14 files changed:
BUILD.gn
Cargo.toml
gd/rust/facade/Cargo.toml
gd/rust/facade/helpers/Cargo.toml [new file with mode: 0644]
gd/rust/hal/Cargo.toml
gd/rust/hci/Cargo.toml
gd/rust/linux/adapter/Cargo.toml [new file with mode: 0644]
gd/rust/linux/adapter/build.rs [new file with mode: 0644]
gd/rust/linux/adapter/src/main.rs [new file with mode: 0644]
gd/rust/linux/mgmt/Cargo.toml [new file with mode: 0644]
gd/rust/linux/mgmt/src/main.rs [new file with mode: 0644]
gd/rust/shim/Cargo.toml [new file with mode: 0644]
main/BUILD.gn
service/BUILD.gn

index 8b563f8..56ed7d8 100644 (file)
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -32,7 +32,8 @@ group("all") {
 group("bluetooth") {
   deps = [
     "//bt/main:bluetooth",
-    "//bt/service:bluetoothtbd",
+    "//bt/main:bluetooth-static",
+    #"//bt/service:bluetoothtbd",
   ]
 }
 
index 2b60ccb..ac01a99 100644 (file)
 #  See the License for the specific language governing permissions and
 #  limitations under the License.
 
-[package]
-name = "bt_shim_ffi"
-version = "0.0.1"
-edition = "2018"
+[workspace]
 
-[dependencies]
-# BT dependencies
-bt_common = { path = "gd/rust/common" }
-bt_facade_helpers = { path = "gd/rust/facade" }
-bt_hal = { path = "gd/rust/hal" }
-bt_hci = { path = "gd/rust/hci" }
-bt_main = { path = "gd/rust/main" }
-bt_packets = { path = "gd/rust/packets" }
-
-# All external dependencies. Keep all versions at build/rust/Cargo.toml
-bindgen = "0.51"
-bytes = "1.0"
-cxx = { version = "0.5.9", features = ["c++17"] }
-env_logger = "0.8"
-futures = "0.3"
-grpcio = { version = "0.7", features = ["protobuf", "protobuf-codec", "openssl"] }
-grpcio-sys = { version = "*", features = ["openssl"] }
-lazy_static = "1.4"
-log = "0.4"
-nix = "0.19"
-num-derive = "0.3"
-num-traits = "0.2"
-paste = "1.0"
-proc-macro2 = "1.0.24"
-protobuf = "2.0"
-protoc-grpcio = "2.0"
-protoc-rust = "2.0"
-quote = "1.0.8"
-thiserror = "1.0"
-syn = { version = "1.0.58", features = ['default', 'full'] }
-tokio = { version = "1.0", features = ['bytes', 'fs', 'io-util', 'libc', 'macros', 'memchr', 'mio', 'net', 'num_cpus', 'rt', 'rt-multi-thread', 'sync', 'time', 'tokio-macros'] }
-tokio-stream = "0.1"
-walkdir = "2.2"
-
-
-[lib]
-path = "gd/rust/shim/src/lib.rs"
-crate-type = ["staticlib"]
+members = [
+  "gd/rust/shim",
+  "gd/rust/linux/mgmt",
+  "gd/rust/linux/adapter",
+]
index ee8f2ad..6758875 100644 (file)
@@ -14,7 +14,7 @@
 #  limitations under the License.
 
 [package]
-name = "bt_facade_helpers"
+name = "bluetooth_with_facades"
 version = "0.0.1"
 edition = "2018"
 
@@ -32,6 +32,17 @@ log = "*"
 protobuf = "*"
 tokio = "*"
 
+# Binary-only deps
+clap = "*"
+lazy_static = "*"
+nix = "*"
+bt_common = { path = "../common" }
+
+
+[[bin]]
+name = "bluetooth_with_facades"
+path = "src/main.rs"
+
 [lib]
-path = "helpers/lib.rs"
+path = "src/lib.rs"
 crate-type = ["rlib"]
diff --git a/gd/rust/facade/helpers/Cargo.toml b/gd/rust/facade/helpers/Cargo.toml
new file mode 100644 (file)
index 0000000..ceaa0f4
--- /dev/null
@@ -0,0 +1,38 @@
+#
+#  Copyright 2021 Google, Inc.
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at:
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+[package]
+name = "bt_facade_helpers"
+version = "0.0.1"
+edition = "2018"
+
+[dependencies]
+# GD bluetooth deps
+bt_facade_proto = { path = "../../facade_proto" }
+bt_packets = { path = "../../packets" }
+
+# External deps
+bytes = "*"
+cxx = "*"
+futures = "*"
+grpcio = "*"
+log = "*"
+protobuf = "*"
+tokio = "*"
+
+[lib]
+path = "lib.rs"
+crate-type = ["rlib"]
+
index 9963e7a..fe909e8 100644 (file)
@@ -21,7 +21,7 @@ edition = "2018"
 [dependencies]
 # BT dependencies
 bt_common = { path = "../common" }
-bt_facade_helpers = { path = "../facade" }
+bt_facade_helpers = { path = "../facade/helpers" }
 bt_facade_proto = { path = "../facade_proto" }
 bt_packets = { path = "../packets" }
 gddi = { path = "../gddi" }
index 14ccc92..48e08a9 100644 (file)
@@ -23,7 +23,7 @@ edition = "2018"
 bt_common = { path = "../common" }
 bt_hci_custom_types = { path = "custom_types" }
 bt_hal = { path = "../hal" }
-bt_facade_helpers = { path = "../facade" }
+bt_facade_helpers = { path = "../facade/helpers" }
 bt_facade_proto = { path = "../facade_proto" }
 bt_packets = { path = "../packets" }
 gddi = { path = "../gddi" }
diff --git a/gd/rust/linux/adapter/Cargo.toml b/gd/rust/linux/adapter/Cargo.toml
new file mode 100644 (file)
index 0000000..477b8b3
--- /dev/null
@@ -0,0 +1,31 @@
+#
+#  Copyright 2021 Google, Inc.
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at:
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+[package]
+name = "bluetooth"
+version = "0.0.1"
+edition = "2018"
+
+[dependencies]
+# BT dependencies
+bt_shim = { path = "../../shim" }
+
+[build-dependencies]
+pkg-config = "0.3"
+
+[[bin]]
+name = "bluetoothd"
+path = "src/main.rs"
+
diff --git a/gd/rust/linux/adapter/build.rs b/gd/rust/linux/adapter/build.rs
new file mode 100644 (file)
index 0000000..1c58ead
--- /dev/null
@@ -0,0 +1,25 @@
+use pkg_config::Config;
+
+fn main() {
+    // The main linking point with c++ code is the libbluetooth-static.a
+    // These includes all the symbols built via C++ but doesn't include other
+    // links (i.e. pkg-config)
+    println!("cargo:rustc-link-lib=static=bluetooth-static");
+
+    // A few dynamic links
+    println!("cargo:rustc-link-lib=dylib=flatbuffers");
+    println!("cargo:rustc-link-lib=dylib=protobuf");
+    println!("cargo:rustc-link-lib=dylib=resolv");
+
+    // Clang requires -lc++ instead of -lstdc++
+    println!("cargo:rustc-link-lib=c++");
+
+    // A few more dependencies from pkg-config. These aren't included as part of
+    // the libbluetooth-static.a
+    Config::new().probe("libchrome").unwrap();
+    Config::new().probe("libmodp_b64").unwrap();
+    Config::new().probe("tinyxml2").unwrap();
+
+    println!("cargo:rerun-if-changed=build.rs");
+    println!("cargo:rerun-if-changed=libbluetooth-static.a");
+}
diff --git a/gd/rust/linux/adapter/src/main.rs b/gd/rust/linux/adapter/src/main.rs
new file mode 100644 (file)
index 0000000..96f6a62
--- /dev/null
@@ -0,0 +1,5 @@
+extern crate bt_shim;
+
+fn main() {
+    println!("Bluetooth Adapter Daemon");
+}
diff --git a/gd/rust/linux/mgmt/Cargo.toml b/gd/rust/linux/mgmt/Cargo.toml
new file mode 100644 (file)
index 0000000..9ba7a80
--- /dev/null
@@ -0,0 +1,24 @@
+#
+#  Copyright 2021 Google, Inc.
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at:
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+[package]
+name = "btmgmt"
+version = "0.0.1"
+edition = "2018"
+
+[[bin]]
+name = "btmgmtd"
+path = "src/main.rs"
+
diff --git a/gd/rust/linux/mgmt/src/main.rs b/gd/rust/linux/mgmt/src/main.rs
new file mode 100644 (file)
index 0000000..aa198f8
--- /dev/null
@@ -0,0 +1,3 @@
+fn main() {
+    println!("Bluetooth Management Daemon");
+}
diff --git a/gd/rust/shim/Cargo.toml b/gd/rust/shim/Cargo.toml
new file mode 100644 (file)
index 0000000..b0e082b
--- /dev/null
@@ -0,0 +1,56 @@
+#
+#  Copyright 2021 Google, Inc.
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at:
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+[package]
+name = "bt_shim"
+version = "0.0.1"
+edition = "2018"
+
+[dependencies]
+# BT dependencies
+bt_common = { path = "../common" }
+bt_facade_helpers = { path = "../facade/helpers" }
+bt_hal = { path = "../hal" }
+bt_hci = { path = "../hci" }
+bt_main = { path = "../main" }
+bt_packets = { path = "../packets" }
+
+# All external dependencies. Keep all versions at build/rust/Cargo.toml
+bindgen = "0.51"
+bytes = "1.0"
+cxx = { version = "1.0.42", features = ["c++17"] }
+env_logger = "0.8"
+futures = "0.3"
+grpcio = { version = "0.7", features = ["protobuf", "protobuf-codec", "openssl"] }
+grpcio-sys = { version = "*", features = ["openssl"] }
+lazy_static = "1.4"
+log = "0.4"
+nix = "0.19"
+num-derive = "0.3"
+num-traits = "0.2"
+paste = "1.0"
+proc-macro2 = "1.0.24"
+protobuf = "2.0"
+protoc-grpcio = "2.0"
+protoc-rust = "2.0"
+quote = "1.0.8"
+thiserror = "1.0"
+syn = { version = "1.0.58", features = ['default', 'full'] }
+tokio = { version = "1.0", features = ['bytes', 'fs', 'io-util', 'libc', 'macros', 'memchr', 'mio', 'net', 'num_cpus', 'rt', 'rt-multi-thread', 'sync', 'time', 'tokio-macros'] }
+tokio-stream = "0.1"
+walkdir = "2.2"
+
+[lib]
+path = "src/lib.rs"
index b411211..9604f59 100644 (file)
@@ -18,6 +18,24 @@ config("libbluetooth_config") {
   include_dirs = [ "../include" ]
 }
 
+# Complete static library for linking with Rust
+static_library("bluetooth-static") {
+  complete_static_lib = true
+
+  sources = [
+    "//bt/service/hal/bluetooth_interface.cc",
+    "//bt/service/logging_helpers.cc",
+  ]
+
+  configs += [
+    "//bt:target_defaults"
+  ]
+
+  deps = [
+    ":bluetooth",
+  ]
+}
+
 # Configure libbluetooth as either dynamic or static library
 if (defined(use.bt_dynlib) && use.bt_dynlib) {
   lib_type = "shared_library"
@@ -26,11 +44,9 @@ if (defined(use.bt_dynlib) && use.bt_dynlib) {
 }
 
 target(lib_type, "bluetooth") {
-  # HAL layer
-  sources = [ "//bt/btif/src/bluetooth.cc" ]
 
-  # platform specific
-  sources += [
+  # Platform specific
+  sources = [
     "bte_conf.cc",
     "bte_init_cpp_logging.cc",
     "bte_logmsg.cc",
index 64d0844..83f92d3 100644 (file)
@@ -76,7 +76,7 @@ source_set("service_linux_src") {
   configs += [ ":service_config" ]
 }
 
-source_set("service") {
+static_library("service") {
   sources = [
     "common/bluetooth/a2dp_codec_config.cc",
     "common/bluetooth/adapter_state.cc",
@@ -125,6 +125,10 @@ executable("bluetoothtbd") {
     "ssl",
     "crypto",
   ]
+
+  lib_dirs = [
+    "${root_out_dir}/rust"
+  ]
 }
 
 if (use.test) {