#!/bin/bash # # This script must be run in the location of the script! # # This script generates Android.bp files for this and all subdirs of this # DIR="${ANDROID_BUILD_TOP}/device/google/cuttlefish_vmm" function remove_trailing_slash { if [[ $1 == "/" ]]; then echo $i else echo ${1%/} fi } set -o errexit function check_location() { local my_loc="$(realpath ${DIR})" my_loc=$(remove_trailing_slash ${my_loc}) local my_pwd="$(realpath $PWD)" my_pwd="$(remove_trailing_slash ${my_pwd})" if [[ "${my_loc}" != "${my_pwd}" ]]; then echo ${my_loc} echo ${my_pwd} >&2 echo "the script location must be run where the script is located" exit 10 fi } my_name=`basename $0` seccomp_archs=("x86_64" "aarch64") # under get_arch_dir() in cuttlefish_vmm, where is seccomp? subdir="etc/seccomp" # define arch dir pattern: e.g. ${ARCH}-linux-gnu function get_arch_dir() { local suffix="-linux-gnu" local arch=$1 echo ${arch}${suffix} } # take arch, return the path of the output Android.bp file function get_output_file() { local blueprint_dir=$1 blueprint_dir="$(remove_trailing_slash ${blueprint_dir})" echo "${blueprint_dir}/Android.bp" } # utility function to enumerate policy files # # 1: seccomp dir to scan function scan_policy_name() { local seccomp_dir=$1 ( # pushd but no output to stdout/stderr # the output is taken and used by the caller pushd $seccomp_dir > /dev/null 2>&1 ls -1 popd > /dev/null 2>&1 ) } # starting from old Android.bp function gen_license() { local year=${1:-"2019"} cat <name: value, # # e.g. --name="\"foo\"" will generate # name: "foo", # function gen_module() { local mod_name=$1 # e.g. prebuilt_usr_share_host local indent="$2" shift 2 local long_options=() local long_opt="" local OPTIND=1 while getopts ":-:" op; do # a long opt like --srcs="some" is actually: # - + - + "srcs=some" # that's a short option '-' just like h, and # the OPTARGS of the option '-' is srcs=some if [[ "$op" != "-" ]]; then >&2 echo "gen_module does take long options with = only" exit 8 fi long_op="${OPTARG%%=*}" OPTARG="${OPTARG#$long_op}" OPTARG="${OPTARG#=}" long_options+=( "$long_op" ) declare local ${long_op}="${OPTARG}" done echo "$mod_name {" for field in "${long_options[@]}"; do eval local value=\$$field echo "${indent}${field}: ${value}," done echo "}" } function gen_android_bp4seccomp() { local arch="$1" local arch_dir="$(get_arch_dir ${arch})" local seccomp_dir="${arch_dir}/${subdir}" local where_in_etc_on_user_machine="cuttlefish/${arch_dir}/seccomp" gen_license 2019 for i in $(scan_policy_name $seccomp_dir); do # first two are: e.g. prebuilt_usr_share_host and whitespace for intentation local base_name="$(basename $i)" gen_module "prebuilt_usr_share_host" ' ' \ --name="\"${base_name}_at_${arch}\"" \ --src="\"${subdir}/${base_name}\"" \ --filename="\"${base_name}\"" \ --sub_dir="\"${where_in_etc_on_user_machine}\"" done } function gen_main_android_bp() { gen_license 2019 gen_module "cc_prebuilt_binary" ' ' \ --name="\"common_crosvm\"" \ --stem="\"crosvm"\" \ --srcs="[\"scripts/crosvm\"]" \ --defaults="[\"cuttlefish_host_only\"]" cat < $(get_output_file ${DIR}) for arch in ${seccomp_archs[@]}; do arch_dir=$(get_arch_dir ${arch}) outfile="$(get_output_file ${arch_dir})" gen_android_bp4seccomp $arch > $outfile done