xref: /aosp_15_r20/build/soong/scripts/unpack-prebuilt-apex.sh (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker#!/bin/bash
2*333d2b36SAndroid Build Coastguard Worker
3*333d2b36SAndroid Build Coastguard Workerset -eu
4*333d2b36SAndroid Build Coastguard Worker
5*333d2b36SAndroid Build Coastguard Worker# Copyright 2020 Google Inc. All rights reserved.
6*333d2b36SAndroid Build Coastguard Worker#
7*333d2b36SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
8*333d2b36SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
9*333d2b36SAndroid Build Coastguard Worker# You may obtain a copy of the License at
10*333d2b36SAndroid Build Coastguard Worker#
11*333d2b36SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
12*333d2b36SAndroid Build Coastguard Worker#
13*333d2b36SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
14*333d2b36SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
15*333d2b36SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16*333d2b36SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
17*333d2b36SAndroid Build Coastguard Worker# limitations under the License.
18*333d2b36SAndroid Build Coastguard Worker
19*333d2b36SAndroid Build Coastguard Worker# Tool to unpack an apex file and verify that the required files were extracted.
20*333d2b36SAndroid Build Coastguard Workerif [ $# -lt 6 ]; then
21*333d2b36SAndroid Build Coastguard Worker  echo "usage: $0 <deapaxer_path> <debugfs_path> <fsck.erofs_path> <apex file> <output_dir> <required_files>+" >&2
22*333d2b36SAndroid Build Coastguard Worker  exit 1
23*333d2b36SAndroid Build Coastguard Workerfi
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard WorkerDEAPEXER_PATH=$1
26*333d2b36SAndroid Build Coastguard WorkerDEBUGFS_PATH=$2
27*333d2b36SAndroid Build Coastguard WorkerFSCK_EROFS_PATH=$3
28*333d2b36SAndroid Build Coastguard WorkerAPEX_FILE=$4
29*333d2b36SAndroid Build Coastguard WorkerOUTPUT_DIR=$5
30*333d2b36SAndroid Build Coastguard Workershift 5
31*333d2b36SAndroid Build Coastguard WorkerREQUIRED_PATHS=$@
32*333d2b36SAndroid Build Coastguard Worker
33*333d2b36SAndroid Build Coastguard Workerrm -fr $OUTPUT_DIR
34*333d2b36SAndroid Build Coastguard Workermkdir -p $OUTPUT_DIR
35*333d2b36SAndroid Build Coastguard Worker
36*333d2b36SAndroid Build Coastguard Worker# Unpack the apex file contents.
37*333d2b36SAndroid Build Coastguard Worker$DEAPEXER_PATH --debugfs_path $DEBUGFS_PATH \
38*333d2b36SAndroid Build Coastguard Worker               --fsckerofs_path $FSCK_EROFS_PATH \
39*333d2b36SAndroid Build Coastguard Worker               extract $APEX_FILE $OUTPUT_DIR
40*333d2b36SAndroid Build Coastguard Worker
41*333d2b36SAndroid Build Coastguard Worker# Verify that the files that the build expects to be in the .apex file actually
42*333d2b36SAndroid Build Coastguard Worker# exist, and make sure they have a fresh mtime to not confuse ninja.
43*333d2b36SAndroid Build Coastguard Workertypeset -i FAILED=0
44*333d2b36SAndroid Build Coastguard Workerfor r in $REQUIRED_PATHS; do
45*333d2b36SAndroid Build Coastguard Worker  if [ ! -f $r ]; then
46*333d2b36SAndroid Build Coastguard Worker    echo "Required file $r not present in apex $APEX_FILE" >&2
47*333d2b36SAndroid Build Coastguard Worker    FAILED=$FAILED+1
48*333d2b36SAndroid Build Coastguard Worker  else
49*333d2b36SAndroid Build Coastguard Worker    # TODO(http:/b/177646343) - deapexer extracts the files with a timestamp of 1 Jan 1970.
50*333d2b36SAndroid Build Coastguard Worker    # touch the file so that ninja knows it has changed.
51*333d2b36SAndroid Build Coastguard Worker    touch $r
52*333d2b36SAndroid Build Coastguard Worker  fi
53*333d2b36SAndroid Build Coastguard Workerdone
54*333d2b36SAndroid Build Coastguard Worker
55*333d2b36SAndroid Build Coastguard Workerif [ $FAILED -gt 0 ]; then
56*333d2b36SAndroid Build Coastguard Worker  echo "$FAILED required files were missing from $APEX_FILE" >&2
57*333d2b36SAndroid Build Coastguard Worker  echo "Available files are:" >&2
58*333d2b36SAndroid Build Coastguard Worker  find $OUTPUT_DIR -type f | sed "s|^|    |" >&2
59*333d2b36SAndroid Build Coastguard Worker  exit 1
60*333d2b36SAndroid Build Coastguard Workerfi
61