| #!/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" |
| |
| # Replaces the generated section with content read from stdin. |
| # |
| # Args: |
| # path to the file to edit in place |
| # tag uniquely identifying the generated section. |
| # The generated content is delimited by two lines ending with |
| # "Start of generated ${tag}" and "End of generated ${tag}" |
| function update_generated_section { |
| local -r file="$1" |
| local -r tag="$2" |
| local -r lead="^.*Start of generated ${tag}$" |
| local -r tail="^.*End of generated ${tag}$" |
| egrep --silent "${lead}" "${file}" |
| egrep --silent "${tail}" "${file}" |
| sed -i -e " |
| /$lead/,/$tail/{ |
| /$lead/{ |
| p; |
| a \ // Generated by device/google/cuttlefish_vmm/gen_android_bp.sh |
| r /dev/stdin |
| }; |
| /$tail/p; |
| d |
| }" "${file}""" |
| } |
| |
| 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,gfxstream_graphics_detector,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 |
| |
| for binary_path in qemu/x86_64-linux-gnu/bin/*.so* qemu/x86_64-linux-gnu/bin/qemu-system*; do |
| gen_module "cc_prebuilt_binary" ' ' \ |
| --name="\"x86_64_linux_gnu_$(basename "${binary_path}")_binary_for_qemu\"" \ |
| --srcs="[\"${binary_path}\"]" \ |
| --stem="\"$(basename "${binary_path}")"\" \ |
| --defaults="[\"cuttlefish_host\"]" \ |
| --check_elf_files="false" |
| done |
| resource_paths=( |
| qemu/efi-virtio.rom |
| qemu/keymaps/en-us |
| qemu/opensbi-riscv64-generic-fw_dynamic.bin |
| ) |
| |
| for resource_path in "${resource_paths[@]}" |
| do |
| local base_name="$(basename "${resource_path}")" |
| local arch='x86_64' |
| local arch_dir="$(get_arch_dir ${arch})" |
| local sub_dir="$(dirname "${resource_path}")" |
| gen_module "prebuilt_usr_share_host" ' ' \ |
| --name="\"${arch}_${base_name}_resource_for_qemu\"" \ |
| --src="\"qemu/x86_64-linux-gnu/usr/share/${resource_path}\"" \ |
| --filename="\"${base_name}\"" \ |
| --sub_dir="\"${sub_dir}\"" |
| done |
| } |
| |
| function generate_all_cuttlefish_host_package_android_bp() { |
| #TODO: Handle Crosvm prebuilts which are currently performed manually. |
| qemu_binaries="$(grep "_binary_for_qemu" Android.bp | sed 's/name: / /g')" |
| qemu_resources="$(grep "_resource_for_qemu" Android.bp | sed 's/name: / /g')" |
| |
| local android_bp_file="../cuttlefish/build/Android.bp" |
| echo "${qemu_binaries}" | update_generated_section "${android_bp_file}" "qemu_x86_64_linux_gnu_binary" |
| echo "${qemu_resources}" | update_generated_section "${android_bp_file}" "qemu_x86_64_linux_gnu_resource" |
| } |
| |
| |
| # main |
| |
| # Set the current directory to the script location. |
| cd "$(dirname "$0")" |
| |
| gen_main_android_bp > $(get_output_file .) |
| generate_all_cuttlefish_host_package_android_bp |
| |
| |
| for arch in ${seccomp_archs[@]}; do |
| arch_dir=$(get_arch_dir ${arch}) |
| outfile="$(get_output_file ${arch_dir})" |
| gen_android_bp4seccomp $arch > $outfile |
| done |