Skip to content

Commit

Permalink
Add support for cargo build of LMP
Browse files Browse the repository at this point in the history
This change adds an option to skip generating Bluetooth packets using
bluetooth_packetgen if these were already built in advance.
It also adds crate definition for liblmp, similar to bt_packets.

Bug: 238726856
Tag: #floss
Test: cargo build
Ignore-AOSP-First: Cherry-picked from AOSP
Merged-In: Ib099b56c0eb39c37cb459132ddf2a813cb63ebf6
Change-Id: Ib099b56c0eb39c37cb459132ddf2a813cb63ebf6
  • Loading branch information
Ivan Podogov authored and 8lank committed Sep 16, 2022
1 parent 16a76ad commit cd9fe99
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 2 deletions.
9 changes: 9 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
'docs', # Build Rust docs
'main', # Build the main C++ codebase
'prepare', # Prepare the output directory (gn gen + rust setup)
'rootcanal', # Build Rust targets for RootCanal
'rust', # Build only the rust components + copy artifacts to output dir
'test', # Run the unit tests
'tools', # Build the host tools (i.e. packetgen)
Expand Down Expand Up @@ -428,6 +429,11 @@ def _target_rust(self):
"""
self._rust_build()

def _target_rootcanal(self):
""" Build rust artifacts for RootCanal in an already prepared environment.
"""
self.run_command('rust', ['cargo', 'build'], cwd=os.path.join(self.platform_dir, 'bt/tools/rootcanal'), env=self.env)

def _target_main(self):
""" Build the main GN artifacts in an already prepared environment.
"""
Expand All @@ -442,6 +448,7 @@ def _target_test(self):
rust_test_cmd = rust_test_cmd + [self.args.test_name]

self.run_command('test', rust_test_cmd, cwd=os.path.join(self.platform_dir, 'bt'), env=self.env)
self.run_command('test', rust_test_cmd, cwd=os.path.join(self.platform_dir, 'bt/tools/rootcanal'), env=self.env)

# Host tests second based on host test list
for t in HOST_TESTS:
Expand Down Expand Up @@ -537,6 +544,8 @@ def build(self):
self._target_prepare()
elif self.target == 'tools':
self._target_tools()
elif self.target == 'rootcanal':
self._target_rootcanal()
elif self.target == 'rust':
self._target_rust()
elif self.target == 'docs':
Expand Down
21 changes: 19 additions & 2 deletions system/gd/rust/packets/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@ use std::path::{Path, PathBuf};
use std::process::Command;

fn main() {
generate_packets();
let packets_prebuilt = match env::var("HCI_PACKETS_PREBUILT") {
Ok(dir) => PathBuf::from(dir),
Err(_) => PathBuf::from("hci_packets.rs"),
};
if Path::new(packets_prebuilt.as_os_str()).exists() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let outputted = out_dir.join("../../hci/hci_packets.rs");
std::fs::copy(
packets_prebuilt.as_os_str().to_str().unwrap(),
out_dir.join(outputted.file_name().unwrap()).as_os_str().to_str().unwrap(),
)
.unwrap();
} else {
generate_packets();
}
}

fn generate_packets() {
Expand All @@ -40,7 +54,10 @@ fn generate_packets() {
};

if !Path::new(packetgen.as_os_str()).exists() {
panic!("Unable to locate bluetooth packet generator:{:?}", packetgen.as_os_str().to_str().unwrap());
panic!(
"Unable to locate bluetooth packet generator:{:?}",
packetgen.as_os_str().to_str().unwrap()
);
}

for i in 0..input_files.len() {
Expand Down
20 changes: 20 additions & 0 deletions tools/rootcanal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright 2022 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.

[workspace]

members = [
"lmp",
]
36 changes: 36 additions & 0 deletions tools/rootcanal/lmp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# 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 = "lmp"
version = "0.1.0"
edition = "2018"
build="build.rs"

[dependencies]
bytes = "1.0.1"
num-bigint = "0.4.3"
num-derive = "0.3.3"
num-integer = "0.1.45"
num-traits = "0.2.14"
paste = "1.0.4"
pin-utils = "0.1.0"
rand = "0.8.3"
thiserror = "1.0.23"
bt_packets = { path = "../../../system/gd/rust/packets/" }

[lib]
path="src/lib.rs"
crate-type = ["staticlib"]
68 changes: 68 additions & 0 deletions tools/rootcanal/lmp/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Copyright 2022 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.

use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;

fn main() {
let packets_prebuilt = match env::var("LMP_PACKETS_PREBUILT") {
Ok(dir) => PathBuf::from(dir),
Err(_) => PathBuf::from("lmp_packets.rs"),
};
if Path::new(packets_prebuilt.as_os_str()).exists() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let outputted = out_dir.join("lmp_packets.rs");
std::fs::copy(
packets_prebuilt.as_os_str().to_str().unwrap(),
out_dir.join(outputted.file_name().unwrap()).as_os_str().to_str().unwrap(),
)
.unwrap();
} else {
generate_packets();
}
}

fn generate_packets() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

// Find the packetgen tool. Expecting it at CARGO_HOME/bin
let packetgen = match env::var("CARGO_HOME") {
Ok(dir) => PathBuf::from(dir).join("bin").join("bluetooth_packetgen"),
Err(_) => PathBuf::from("bluetooth_packetgen"),
};

if !Path::new(packetgen.as_os_str()).exists() {
panic!(
"Unable to locate bluetooth packet generator:{:?}",
packetgen.as_os_str().to_str().unwrap()
);
}

let output = Command::new(packetgen.as_os_str().to_str().unwrap())
.arg("--out=".to_owned() + out_dir.as_os_str().to_str().unwrap())
.arg("--include=.")
.arg("--rust")
.arg("lmp_packets.pdl")
.output()
.unwrap();

println!(
"Status: {}, stdout: {}, stderr: {}",
output.status,
String::from_utf8_lossy(output.stdout.as_slice()),
String::from_utf8_lossy(output.stderr.as_slice())
);
}

0 comments on commit cd9fe99

Please sign in to comment.