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=
21in=
22out=
23
24function usage() {
25  echo "usage: $0 [--bootstrap|--all] --in in --out out" && 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    --in)
39      in=$2
40      shift
41      shift
42      ;;
43    --out)
44      out=$2
45      shift
46      shift
47      ;;
48    *)
49      usage
50  esac
51done
52
53if [ -z $in ] || [ -z $out ]; 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_IN=$(abs $in)
86ROOT_OUT=$(abs $out)
87
88# to use relative paths
89cd $(dirname $0)
90
91rm -iRf $ROOT_OUT
92mkdir -p $ROOT_OUT
93mkdir -p $ROOT_OUT/apex
94cp -R $ROOT_IN/* $ROOT_OUT
95
96if test -f $ROOT_OUT/system/etc/linker.config.json; then
97  conv_linker_config proto -s $ROOT_OUT/system/etc/linker.config.json -o $ROOT_OUT/system/etc/linker.config.pb
98fi
99
100apexInfo=$ROOT_OUT/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_OUT/$partition/apex ]; then
106    for src in $ROOT_OUT/$partition/apex/*/; do
107      name=$(basename $src)
108      dst=$ROOT_OUT/apex/$name
109      preinstalled_path=/$(realpath --relative-to=$ROOT_OUT $src)
110      module_path=/$(realpath --relative-to=$ROOT_OUT $dst)
111      if [ $(get_level $name) -le $activate_level ]; then
112        cp -r $src $dst
113        conv_apex_manifest proto $dst/apex_manifest.json -o $dst/apex_manifest.pb
114        if test -f $dst/etc/linker.config.json; then
115          conv_linker_config proto -s $dst/etc/linker.config.json -o $dst/etc/linker.config.pb
116        fi
117        mkdir $dst/lib
118        echo " <apex-info moduleName=\"$name\" modulePath=\"$module_path\" preinstalledModulePath=\"$preinstalled_path\" isFactory=\"true\" isActive=\"true\" />" >> $apexInfo
119      else
120        echo " <apex-info moduleName=\"$name\" preinstalledModulePath=\"$preinstalled_path\" isFactory=\"true\" isActive=\"false\" />" >> $apexInfo
121      fi
122    done
123  fi
124done
125
126echo "</apex-info-list>" >> $apexInfo
127