xref: /aosp_15_r20/tools/netsim/scripts/cargo_env.sh (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1#!/bin/bash
2# Copyright 2024 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# This script sets up the necessary environment variables for Cargo builds.
17# It determines the OUT_PATH, sets up CARGO_HOME and library paths,
18# and defines paths to prebuilt packet files.
19
20# Usage: scripts/cargo_env.sh [OUT_PATH]
21#   OUT_PATH: Optional. The output directory for build artifacts.
22#             Defaults to "tools/netsim/objs" if not specified.
23
24# Set up necessary env vars for Cargo
25function setup_cargo_env {
26  # Get the directory of the script
27  local REPO=$(realpath "$(dirname "${BASH_SOURCE[0]}")/../../..")
28
29  # Determine the OUT_PATH
30  local OUT_PATH="${1:-$REPO/tools/netsim/objs}"
31
32  # Get OS name (lowercase)
33  local OS=$(uname | tr '[:upper:]' '[:lower:]')
34
35  # Set environment variables
36  export CARGO_HOME=$OUT_PATH/rust/.cargo
37  export OBJS_PATH=$OUT_PATH
38  export GRPCIO_SYS_GRPC_INCLUDE_PATH=$REPO/external/grpc/include
39
40  # Paths to pdl generated packets files
41  local ROOTCANAL_PDL_PATH=$OUT_PATH/rootcanal/pdl_gen
42  export LINK_LAYER_PACKETS_PREBUILT=$ROOTCANAL_PDL_PATH/link_layer_packets.rs
43  local PDL_PATH=$OUT_PATH/pdl/pdl_gen
44  export MAC80211_HWSIM_PACKETS_PREBUILT=$PDL_PATH/mac80211_hwsim_packets.rs
45  export IEEE80211_PACKETS_PREBUILT=$PDL_PATH/ieee80211_packets.rs
46  export LLC_PACKETS_PREBUILT=$PDL_PATH/llc_packets.rs
47  export NETLINK_PACKETS_PREBUILT=$PDL_PATH/netlink_packets.rs
48
49  # Set library path based on OS
50  if [[ "$OS" == "darwin" ]]; then
51    export DYLD_FALLBACK_LIBRARY_PATH=$OUT_PATH/lib64
52  else
53    export LD_LIBRARY_PATH=$OUT_PATH/lib64
54  fi
55}
56
57setup_cargo_env "$1"
58