xref: /aosp_15_r20/tools/netsim/scripts/proto_update.sh (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1#!/usr/bin/env bash
2
3# Copyright 2022 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Update the Rust protobuf files on netsim-dev branch
18
19# Prerequisites:
20# - protobuf-compiler
21# Linux: sudo apt-get install protobuf-compiler
22# Mac:   brew install protobuf
23
24# Absolute path to tools/netsim using this scripts directory
25REPO_NETSIM=$(dirname $(readlink -f "$0"))/..
26CARGO_MANIFEST=$REPO_NETSIM/rust/proto/Cargo.toml
27
28# -- Step 1. Generate gRPC protobuf files
29# NOTE: the files can not be generated by proto/build.rs because protoc-grpcio doesn't support protobuf v3 yet.
30# https://github.com/mtp401/protoc-grpcio/issues/41
31
32# Install compilers since the crates are not in AOSP
33# TODO: Add required crate mappings to work in netsim-dev
34export CARGO_HOME=""
35# Specify versions to use the correct protobuf version.
36cargo install protobuf-codegen --version 3.2.0
37cargo install grpcio-compiler --version 0.13.0
38
39PROTOC_CMD="protoc --rust_out=./rust/proto/src --grpc_out=./rust/proto/src\
40 --plugin=protoc-gen-grpc=$(which grpc_rust_plugin)\
41 -I./proto -I../../external/protobuf/src\
42 -I../../packages/modules/Bluetooth/tools/rootcanal/proto"
43$PROTOC_CMD ./proto/netsim/frontend.proto
44$PROTOC_CMD ./proto/netsim/packet_streamer.proto
45
46# Revert the generate proto files to ensure they are re-generated by proto/build.rs.
47git checkout $REPO_NETSIM/rust/proto/src/packet_streamer.rs
48git checkout $REPO_NETSIM/rust/proto/src/frontend.rs
49rm $REPO_NETSIM/rust/proto/src/mod.rs
50
51# --- Step 2. Generate protobuf files using proto/build.rs
52# Uncomment lines starting with `##`
53OS=$(uname | tr '[:upper:]' '[:lower:]')
54if [[ "$OS" == "linux" ]]; then
55    sed -i 's/^##//g' $CARGO_MANIFEST
56fi
57if [[ "$OS" == "darwin" ]]; then
58    sed -i '' 's/^##//g' $CARGO_MANIFEST
59fi
60
61if [ ! -d "$REPO_NETSIM/objs/rust/.cargo" ]; then
62    python3 $REPO_NETSIM/scripts/build_tools.py
63fi
64
65# Use Rust dependency crates available on netsim-dev branch
66export CARGO_HOME=$REPO_NETSIM/objs/rust/.cargo
67
68# For grpcio-sys
69export GRPCIO_SYS_GRPC_INCLUDE_PATH="$REPO_NETSIM/../../external/grpc/include"
70
71cd $REPO_NETSIM
72cargo build --manifest-path $CARGO_MANIFEST
73
74# Restore original Cargo.toml
75git checkout $CARGO_MANIFEST
76
77# Find the most recent rustfmt installed
78RUSTFMT=$(ls -d ../../prebuilts/rust/$OS-x86/*/bin/rustfmt | tail -1)
79
80# Format Rust code
81# Need to format manually because it's not supported in build.rs
82find $REPO_NETSIM/rust/proto -name '*.rs' -exec $RUSTFMT --files-with-diff {} \;
83
84rm rust/Cargo.lock
85