blob: ca5d91eaeaa99faacc521d29270c6ed5d4f77cad [file] [log] [blame]
#!/usr/bin/env bash
# Copyright 2022 The Android Open Source Project
#
# 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.
# Update the Rust protobuf files on git_main-netsim-dev branch
# Prerequisites:
# - protobuf-compiler
# Linux: sudo apt-get install protobuf-compiler
# Mac: brew install protobuf
set -e # Exit immediately if a command exits with a non-zero status.
echo "Starting Rust protobuf file update process..."
# --- Configuration ---
# Absolute path to tools/netsim using this scripts directory
REPO_NETSIM=$(dirname $(readlink -f "$0"))/..
CARGO_MANIFEST="$REPO_NETSIM/rust/proto/Cargo.toml"
PROTO_SRC_DIR="$REPO_NETSIM/rust/proto/src"
OS=$(uname | tr '[:upper:]' '[:lower:]')
# --- Step 1. Generate gRPC protobuf files ---
echo "[Step 1] Generating gRPC protobuf files..."
# NOTE: These files are generated by cargo build because protoc-grpcio doesn't support protobuf v3 yet.
# They will be reverted and regenerated properly in Step 2 using the build script.
# https://github.com/mtp401/protoc-grpcio/issues/41
# Install compilers since the crates are not in AOSP
# TODO: Add required crate mappings to work in git_main-netsim-dev
export CARGO_HOME=""
cargo install protobuf-codegen --version 3.2.0
cargo install grpcio-compiler --version 0.13.0
PROTOC_PLUGIN_PATH=$(which grpc_rust_plugin)
if [ -z "$PROTOC_PLUGIN_PATH" ]; then
echo "Error: grpc_rust_plugin not found in PATH after cargo install."
exit 1
fi
PROTOC_CMD="protoc --rust_out=$PROTO_SRC_DIR --grpc_out=$PROTO_SRC_DIR \
--plugin=protoc-gen-grpc=$PROTOC_PLUGIN_PATH \
-I./proto -I../../external/protobuf/src \
-I../../packages/modules/Bluetooth/tools/rootcanal/proto"
$PROTOC_CMD ./proto/netsim/frontend.proto
$PROTOC_CMD ./proto/netsim/packet_streamer.proto
# Revert the generated proto files so they can be properly re-generated by build.rs in Step 2.
git checkout "$PROTO_SRC_DIR/packet_streamer.rs"
git checkout "$PROTO_SRC_DIR/frontend.rs"
if [ -f "$PROTO_SRC_DIR/mod.rs" ]; then
rm "$PROTO_SRC_DIR/mod.rs"
fi
# --- Step 2. Generate protobuf files using proto/build.rs ---
echo "[Step 2] Generating protobuf files using Cargo build script..."
# Temporarily uncomment build script lines in Cargo.toml
if [[ "$OS" == "linux" ]]; then
# GNU sed
sed -i 's/^##//g' "$CARGO_MANIFEST"
elif [[ "$OS" == "darwin" ]]; then
# BSD sed requires '' for in-place edit without backup
sed -i '' 's/^##//g' "$CARGO_MANIFEST"
else
echo "Warning: Unrecognized OS '$OS'."
fi
# Build dependencies if .cargo directory doesn't exist
if [ ! -d "$REPO_NETSIM/objs/rust/.cargo" ]; then
python3 "$REPO_NETSIM/scripts/build_tools.py"
fi
# TODO: Use Rust dependency crates available on git_main-netsim-dev branch after Rust toolchain is upgraded.
# export CARGO_HOME=$REPO_NETSIM/objs/rust/.cargo
# For grpcio-sys
#export GRPCIO_SYS_GRPC_INCLUDE_PATH="$REPO_NETSIM/../../external/grpc/include"
# Workaround: Use stable Rust temporarily and crates from crates.io due to potential `rustix` crate compile error with older toolchains.
rustup default stable
cd "$REPO_NETSIM"
cargo build --manifest-path "$CARGO_MANIFEST"
# Restore the default toolchain
rustup default 1.73.0
# Restore original Cargo.toml
git checkout "$CARGO_MANIFEST"
# --- Step 3. Post-processing Rust Files ---
echo "[Step 3] Post-processing generated Rust files..."
# Remove #![allow(box_pointers)] attribute from generated files. This has been removed with latest toolchain.
# TODO: Remove this step after Rust toolchain upgrade.
echo "Removing #![allow(box_pointers)] attribute..."
PATTERN_TO_REMOVE='^#\!\[allow(box_pointers)\]$'
if [[ "$OS" == "linux" ]]; then
find "$PROTO_SRC_DIR" -name '*.rs' -exec sed -i "/${PATTERN_TO_REMOVE}/d" {} \;
elif [[ "$OS" == "darwin" ]]; then
find "$PROTO_SRC_DIR" -name '*.rs' -exec sed -i '' "/${PATTERN_TO_REMOVE}/d" {} \;
else
echo "Warning: Unsupported OS '$OS' for automatic removal of '#![allow(box_pointers)]'. Please check files manually."
fi
# Format generated code using prebuilt rustfmt
# Find the most recent rustfmt available in prebuilts for the detected OS
RUSTFMT=$(ls -d ../../prebuilts/rust/$OS-x86/*/bin/rustfmt | tail -1)
if [ -z "$RUSTFMT" ] || [ ! -x "$RUSTFMT" ]; then
echo "Error: Could not find prebuilt rustfmt executable. Skipping formatting."
else
find "$PROTO_SRC_DIR" -name '*.rs' -exec "$RUSTFMT" --files-with-diff {} \;
fi
if [ -f "rust/Cargo.lock" ]; then
rm rust/Cargo.lock
echo "Removed rust/Cargo.lock"
fi