blob: af5b2744cbef970eea7ed77db1f5499fcc3a765a [file] [log] [blame]
#!/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 <<EOF
// Autogenerated via ${my_name}
//
// Copyright (C) ${year} 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.
EOF
}
#
# first two args are module type (e.g. prebuilt_usr_share_host)
# and the whitespaces for indentation
#
# the rest must be in this form:
# --name=value
#
# then, it simply generates this line repeatedly:
# <indent>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 <<EOF
// NOTE: Using cc_prebuilt_binary because cc_prebuilt_library can't handle stem on pie
EOF
for i in */bin/*; do
if [[ ! -L "$i" ]]; then
name="${i//\//_}"
name="${name//-/_}"
name="${name/_bin_/_}"
path="$(dirname $(dirname "$i"))"
stem="$(basename "$i")"
if [[ "crosvm" != "${stem}" ]]; then
name="${name}_for_crosvm"
fi
gen_module "cc_prebuilt_binary" ' ' \
--name="\"${name}\"" \
--srcs="[\"$i\"]" \
--stem="\"$stem"\" \
--relative_install_path="\"${path}\"" \
--defaults="[\"cuttlefish_host_only\"]"
fi
done
}
# main
check_location
gen_main_android_bp > $(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