aboutsummaryrefslogtreecommitdiff
path: root/gen_android_bp.sh
blob: 4e751d970d7b678cff474d9efe2e2df92e16a9f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/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