1#!/bin/bash
2
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e
18
19bootstrap=
20all=
21root=
22block_apexes=
23
24function usage() {
25  echo "usage: $0 [--bootstrap|--all] [--block apexes(colol-separated)] --root root" && exit 1
26}
27
28while [[ $# -gt 0 ]]; do
29  case "$1" in
30    --bootstrap)
31      bootstrap=yes
32      shift
33      ;;
34    --all)
35      all=yes
36      shift
37      ;;
38    --block)
39      block_apexes=$2
40      shift
41      shift
42      ;;
43    --root)
44      root=$2
45      shift
46      shift
47      ;;
48    *)
49      usage
50  esac
51done
52
53if [ -z $root ]; then
54  usage
55fi
56
57if [ ! -z $bootstrap ] && [ ! -z $all ]; then
58  usage
59fi
60
61activate_level=0
62if [ ! -z $bootstrap ]; then
63  activate_level=1
64elif [ ! -z $all ]; then
65  activate_level=2
66fi
67
68function get_level() {
69  case $1 in
70    com.android.art|com.android.runtime|com.android.i18n|com.android.tzdata|com.android.vndk.vR)
71      echo 1 ;;
72    *)
73      echo 2 ;;
74  esac
75}
76
77function abs() {
78  if [[ $1 = /* ]]; then
79    echo $1
80  else
81    echo $(realpath $(pwd)/$1)
82  fi
83}
84
85ROOT=$(abs $root)
86
87# to use relative paths
88cd $(dirname $0)
89
90# clean /apex directory
91rm -iRf $ROOT/apex
92
93# prepare /apex directory
94# 1) activate APEXes
95# 2) generate /apex/apex-info-list.xml
96
97mkdir -p $ROOT/apex
98
99blockIndex=1
100apexInfo=$ROOT/apex/apex-info-list.xml
101echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>" > $apexInfo
102echo "<apex-info-list>" > $apexInfo
103
104for partition in system product system_ext vendor; do
105  if [ -d $ROOT/$partition/apex ]; then
106    for src in $ROOT/$partition/apex/*/; do
107      if test ! -d $src; then
108        continue
109      fi
110      name=$(basename $src)
111      dst=$ROOT/apex/$name
112      # b/234792422 Atest is running with PATH prebuilts/build-tools/path/linux-x86
113      # where the realpath does not support --relative-to option. Use shell
114      # parameter expansion to avoid invalid options.
115      module_path="${src/$ROOT/}"
116      # simulate block apexes are activated from /dev/block/vdaN
117      if [[ "$block_apexes" == *"$name"* ]]; then
118        module_path=/dev/block/vda$blockIndex
119        ((blockIndex=blockIndex+1))
120      fi
121      if [ $(get_level $name) -le $activate_level ]; then
122        # simulate "activation" by copying "apex dir" into /apex
123        cp -r $src $dst
124        echo " <apex-info moduleName=\"$name\" modulePath=\"$module_path\" preinstalledModulePath=\"$module_path\" isFactory=\"true\" isActive=\"true\" />" >> $apexInfo
125      fi
126    done
127  fi
128done
129
130echo "</apex-info-list>" >> $apexInfo
131