Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cd9fe99e authored by Ivan Podogov's avatar Ivan Podogov Committed by Thomas Girardier
Browse files

Add support for cargo build of LMP

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
parent 16a76ad8
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ VALID_TARGETS = [
    '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)
@@ -428,6 +429,11 @@ class HostBuild():
        """
        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.
        """
@@ -442,6 +448,7 @@ class HostBuild():
            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:
@@ -537,6 +544,8 @@ class HostBuild():
            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':
+19 −2
Original line number Diff line number Diff line
@@ -18,8 +18,22 @@ use std::path::{Path, PathBuf};
use std::process::Command;

fn main() {
    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() {
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
@@ -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() {
+20 −0
Original line number Diff line number Diff line
#
#  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 −0
Original line number Diff line number Diff line
#
#  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 −0
Original line number Diff line number Diff line
//
//  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())
    );
}