blob: e80fd94f623f2ac223e0603b73f10e7442fa2abb [file] [log] [blame]
#!/bin/bash
#
# This script generates Android.bp files for this and all subdirs of this
#
set -e
readonly my_name=`basename $0`
readonly seccomp_archs=("x86_64" "aarch64")
# under get_arch_dir() in cuttlefish_vmm, where is seccomp?
readonly subdir="etc/seccomp"
function remove_trailing_slash {
if [[ $1 == "/" ]]; then
echo $i
else
echo ${1%/}
fi
}
# 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 "}"
}
# Reads lines from the input stream and outputs them with a slashes
# at the beginning of each line.
function comment_out() {
# Internal Field Separator (IFS) cleared to preserve leading space.
# -r predisables backslashe interpretation.
while IFS= read -r line; do
echo "// ${line}"
done
}
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
cat <<EOF
// NOTE: Using cc_prebuilt_binary because cc_prebuilt_library will add
// unwanted .so file extensions when installing shared libraries
EOF
for arch_dir in $(get_arch_dir aarch64) $(get_arch_dir x86_64); do
for i in $(echo $arch_dir/bin/{crosvm,lib{minijail.so,gfxstream_backend.so,*.so{.0,.1,.2,.7}}} | xargs -n1 | sort); do
name="${i//\//_}"
name="${name//-/_}"
name="${name/_bin_/_}"
path="$(dirname $(dirname "$i"))"
stem="$(basename "$i")"
if [[ "crosvm" != "${stem}" ]]; then
name="${name}_for_crosvm"
fi
# Command used to process gen_module output.
comment_or_noop='cat'
if [[ "x86_64_linux_gnu_crosvm" == "${name}" ]]; then
echo '// Note: This is commented out to avoid a conflict with the binary built'
echo '// from external/crosvm. This should be uncommented out when backporting to'
echo '// older branches with just use the prebuilt and which do not build from'
echo '// source.'
comment_or_noop="comment_out"
fi
gen_module "cc_prebuilt_binary" ' ' \
--name="\"${name}\"" \
--srcs="[\"$i\"]" \
--stem="\"$stem"\" \
--relative_install_path="\"${path}\"" \
--defaults="[\"cuttlefish_host\"]" \
--check_elf_files="false" | "${comment_or_noop}"
done
done
}
# main
# Set the current directory to the script location.
cd "$(dirname "$0")"
gen_main_android_bp > $(get_output_file .)
for arch in ${seccomp_archs[@]}; do
arch_dir=$(get_arch_dir ${arch})
outfile="$(get_output_file ${arch_dir})"
gen_android_bp4seccomp $arch > $outfile
done