xref: /aosp_15_r20/tools/external_updater/update_package.sh (revision 3c875a214f382db1236d28570d1304ce57138f32)
1#!/bin/bash
2#
3# Copyright (C) 2007 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
17# This script is used by external_updater to replace a package. Don't
18# invoke directly.
19
20set -e
21shopt -s globstar
22
23tmp_dir=$1
24external_dir=$2
25tmp_file=$3
26
27# root of Android source tree
28root_dir=`pwd`
29
30echo "Entering $tmp_dir..."
31cd $tmp_dir
32
33function CopyIfPresent() {
34  if [ -e $external_dir/$1 ]; then
35    cp -a --update=none $external_dir/$1 .
36  fi
37}
38
39echo "Copying preserved files..."
40CopyIfPresent "Android.mk"
41CopyIfPresent "CleanSpec.mk"
42CopyIfPresent "LICENSE"
43CopyIfPresent "NOTICE"
44cp -a -f --update=none $external_dir/MODULE_LICENSE_* .
45CopyIfPresent "METADATA"
46CopyIfPresent "TEST_MAPPING"
47CopyIfPresent ".git"
48CopyIfPresent ".gitignore"
49if compgen -G "$external_dir/cargo2android*"; then
50    cp -a -f --update=none $external_dir/cargo2android* .
51fi
52if compgen -G "$external_dir/cargo_embargo*"; then
53    cp -a -f --update=none $external_dir/cargo_embargo* .
54fi
55if compgen -G "$external_dir/**/*.bp"; then
56    pushd "$external_dir"
57    cp -a -f --update=none --parents **/*.bp "$tmp_dir"
58    popd
59fi
60CopyIfPresent "patches"
61CopyIfPresent "post_update.sh"
62CopyIfPresent "OWNERS"
63CopyIfPresent "README.android"
64CopyIfPresent "rules.mk"
65if compgen -G "$external_dir/cargo2rulesmk*"; then
66    cp -a -f --update=none $external_dir/cargo2rulesmk* .
67fi
68
69file_counter=0
70total_files=$(ls $tmp_dir/patches | grep -Ei '(diff|patch)$' | wc -l)
71for p in $tmp_dir/patches/*.{diff,patch}
72do
73  file_counter=$((file_counter+1))
74  [ -e "$p" ] || continue
75  # Do not patch the Android.bp file, as we assume it will
76  # patch itself.
77  if [ -f $tmp_dir/Cargo.toml ]
78  then
79      [ "$(basename $p)" != "Android.bp.diff" ] || continue
80      [ "$(basename $p)" != "Android.bp.patch" ] || continue
81      [ "$(basename $p)" != "rules.mk.diff" ] || continue
82      [ "$(basename $p)" != "rules.mk.patch" ] || continue
83  fi
84  echo "Applying patch [$file_counter/$total_files] $p..."
85  patch -p1 -d $tmp_dir --no-backup-if-mismatch < $p;
86done
87
88if [ -f $tmp_dir/Cargo.toml -a -f $tmp_dir/Android.bp ]
89then
90    # regenerate Android.bp after local patches, as they may
91    # have deleted files that it uses.
92  /bin/bash `dirname $0`/regen_bp.sh $root_dir $external_dir
93fi
94
95echo "Swapping old and new..."
96second_tmp_dir=`mktemp -d`
97mv $external_dir $second_tmp_dir
98mv $tmp_dir $external_dir
99mv $second_tmp_dir/* $tmp_dir
100rm -rf $second_tmp_dir
101if [ -n "$tmp_file" ]; then
102    # Write to the temporary file to show we have swapped.
103    echo "Swapping" > $tmp_file
104fi
105
106cd $external_dir
107git add .
108
109exit 0
110