Home | History | Annotate | Download | only in tools
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2010 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 
     18 # Script used to patch a source directory from a series of patches
     19 # located under a directory hierarchy
     20 
     21 . `dirname $0`/prebuilt-common.sh
     22 
     23 PROGRAM_PARAMETERS="<src-dir> <patches-dir>"
     24 PROGRAM_DESCRIPTION=\
     25 "Patch a target source directory with a series of patches taken
     26 from another directory hierarchy. The idea is that anything that
     27 is found under <patches-dir>/subdir/foo.patch will be applied with
     28 'patch -p1' in <src-dir>/subdir.
     29 
     30 Patches are applied in the order they are found by 'find'."
     31 
     32 parse_parameters ()
     33 {
     34     SRC_DIR=$1
     35     if [ -z "$SRC_DIR" ] ; then
     36         echo "ERROR: Missing source directory. See --help for usage."
     37         exit 1
     38     fi
     39 
     40     if [ ! -d "$SRC_DIR" ] ; then
     41         echo "ERROR: Invalid target source directory: $SRC_DIR"
     42         exit 1
     43     fi
     44 
     45     PATCHES_DIR=$2
     46     if [ -z "$PATCHES_DIR" ] ; then
     47         echo "ERROR: Missing patches directory. See --help for usage."
     48         exit 1
     49     fi
     50 
     51     if [ ! -d "$PATCHES_DIR" ] ; then
     52         echo "ERROR: Invalid patches directory: $PATCHES_DIR"
     53         exit 1
     54     fi
     55 }
     56 
     57 extract_parameters $@
     58 parse_parameters $PARAMETERS
     59 
     60 PATCHES=`(cd $PATCHES_DIR && find . -name "*.patch") 2> /dev/null`
     61 if [ -z "$PATCHES" ] ; then
     62     log "No patches files in $PATCHES_DIR"
     63     exit 0
     64 fi
     65 PATCHES=`echo $PATCHES | sed -e s%^\./%%g`
     66 for PATCH in $PATCHES; do
     67     PATCHDIR=`dirname $PATCH`
     68     PATCHNAME=`basename $PATCH`
     69     log "Applying $PATCHNAME into $SRC_DIR/$PATCHDIR"
     70     cd $SRC_DIR/$PATCHDIR && patch -p1 < $ANDROID_NDK_ROOT/$PATCHES_DIR/$PATCH
     71     if [ $? != 0 ] ; then
     72         dump "ERROR: Patch failure !! Please check your patches directory!"
     73         exit 1
     74     fi
     75 done
     76 
     77 dump "Done!"
     78