Home | History | Annotate | Download | only in tools
      1 #!/bin/sh
      2 #
      3 # A small script used to update the content of libcxx/ to a newer
      4 # version of libc++.
      5 #
      6 
      7 PROGDIR=$(dirname "$0")
      8 PROGNAME=$(basename "$0")
      9 
     10 # Assume this script is under tools/
     11 NDK_LIBCXX_DIR=$(cd "$PROGDIR"/.. && pwd)
     12 
     13 # Sanitize environment.
     14 set -e
     15 export LANG=C
     16 export LC_ALL=C
     17 
     18 # Helper functions
     19 VERBOSE=1
     20 
     21 run () {
     22   if [ "$VERBOSE" -gt 1 ]; then
     23     echo "COMMAND: $@"
     24   fi
     25   case $VERBOSE in
     26     0|1)
     27         "$@" >/dev/null 2>&1
     28         ;;
     29     2)
     30         "$@" >/dev/null
     31         ;;
     32     *)
     33         "$@"
     34         ;;
     35   esac
     36 }
     37 
     38 log () {
     39   if [ "$VERBOSE" -gt 1 ]; then
     40     printf "%s\n" "$@"
     41   fi
     42 }
     43 
     44 get_config_field () {
     45   cat "$NDK_LIBCXX_DIR"/upstream.config | awk '$1 == "'$1':" { print $2; }'
     46 }
     47 
     48 # Process command line.
     49 
     50 DO_HELP=
     51 NO_CLEANUP=
     52 
     53 for opt; do
     54   case $opt in
     55     --verbose)
     56       VERBOSE=$(( $VERBOSE + 1 ))
     57       ;;
     58     --help)
     59       DO_HELP=true
     60       ;;
     61     --no-cleanup)
     62       NO_CLEANUP=true
     63       ;;
     64     -*)
     65       echo "ERROR: Unknown option '$opt'. See --help."
     66       exit 1
     67       ;;
     68   esac
     69 done
     70 
     71 if [ "$DO_HELP" ]; then
     72     echo "Usage: $PROGNAME [options]"
     73     echo ""
     74     echo "This script is used to update the LLVM libc++ sources to a"
     75     echo "more recent version."
     76     echo ""
     77     echo "Valid options:"
     78     echo "   --help        Print this message."
     79     echo "   --verbose     Increase verbosity."
     80     echo "   --no-cleanup  Don't remove build directory on exit."
     81     echo ""
     82     exit 0
     83 fi
     84 
     85 # Create build directory.
     86 BUILD_DIR=/tmp/ndk-$USER/llvm-libc++/build
     87 mkdir -p "$BUILD_DIR" && rm -rf "$BUILD_DIR"/*
     88 
     89 # Ensure it is cleared when this script exits.
     90 run_on_exit () {
     91   if [ -z "$NO_CLEANUP" ]; then
     92     # Remove temporary build directory.
     93     rm -rf "$BUILD_DIR"
     94   fi
     95 }
     96 trap "run_on_exit \$?" EXIT QUIT HUP TERM INT
     97 
     98 # Get upstream SVN and revision number.
     99 SVN_URL=$(get_config_field svn)
    100 if [ -z "$SVN_URL" ]; then
    101   echo "ERROR: Can't find SVN upstream in upstream.config!"
    102   exit 1
    103 fi
    104 
    105 REVISION=$(get_config_field revision)
    106 if [ -z "$REVISION" ]; then
    107   echo "ERROR: Can't find upstream revision in upstream.config!"
    108   exit 1
    109 fi
    110 
    111 run cd $BUILD_DIR &&
    112 echo "Checking out $SVN_URL@$REVISION"
    113 run svn co $SVN_URL@$REVISION libcxx > /dev/null
    114 run cd libcxx
    115 
    116 echo "Creating git repository and 'master' branch."
    117 run git init
    118 echo \
    119 ".gitignore
    120 .svn/
    121 " > .gitignore
    122 
    123 run git add .
    124 run git add -f .gitignore
    125 run git commit -m "upstream @$REVISION"
    126 
    127 echo "Create 'ndk' branch and apply patches.android/*"
    128 run git branch ndk master
    129 run git checkout ndk
    130 if [ -d "$NDK_LIBCXX_DIR/patches.android" ]; then
    131   (
    132     set +e;
    133     run git am "$NDK_LIBCXX_DIR"/patches.android/*
    134     if [ "$?" != 0 ]; then
    135       echo "A problem occured while applying the patches!!"
    136       exit 1
    137     fi
    138   )
    139 fi
    140 
    141 echo "Updating to newer upstream revision"
    142 run git checkout master
    143 run git tag revision-$REVISION HEAD
    144 
    145 run svn update
    146 NEW_REVISION=$(svn info | awk '$1 == "Revision:" { print $2; }')
    147 echo "Found new revision: $NEW_REVISION (was $REVISION)"
    148 
    149 ADDED_FILES=$(git ls-files -o --exclude-standard)
    150 MODIFIED_FILES=$(git ls-files -m)
    151 REMOVED_FILES=$(git ls-files -d)
    152 log "ADDED_FILES='$ADDED_FILES'"
    153 log "MODIFIED_FILES='$MODIFIED_FILES'"
    154 log "REMOVED_FILES='$REMOVED_FILES'"
    155 CHANGED=
    156 if [ -n "$ADDED_FILES" ]; then
    157   run git add $ADDED_FILES
    158   CHANGED=true
    159 fi
    160 if [ -n "$MODIFIED_FILES" ]; then
    161   run git add $MODIFIED_FILES
    162   CHANGED=true
    163 fi
    164 if [ -n "$REMOVED_FILES" ]; then
    165   run git rm -f $REMOVED_FILES
    166   CHANGED=true
    167 fi
    168 
    169 if [ -z "$CHANGED" ]; then
    170   echo "No changes detected. Exiting."
    171   exit 0
    172 fi
    173 
    174 ADDED_COUNT=$(echo "$ADDED_FILES" | wc -l)
    175 MODIFIED_COUNT=$(echo "$MODIFIED_FILES" | wc -l)
    176 REMOVED_COUNT=$(echo "$REMOVED_FILES" | wc -l)
    177 echo "Commiting changes ($ADDED_COUNT new, $MODIFIED_COUNT changed, $REMOVED_COUNT deleted)"
    178 run git commit -m "upstream @$NEW_REVISION"
    179 run git tag revision-$NEW_REVISION
    180 
    181 echo "Updating NDK branch."
    182 run git checkout ndk
    183 run git tag android-0 HEAD
    184 run git rebase revision-$NEW_REVISION
    185 
    186 echo "Re-creating new Android patches."
    187 run git format-patch -k -o "$BUILD_DIR"/patches.android revision-$NEW_REVISION
    188 run git format-patch -k -o "$BUILD_DIR"/patches.libcxx android-0
    189 
    190 echo "Updating local sources"
    191 run cd "$NDK_LIBCXX_DIR"/libcxx
    192 for PATCH in "$BUILD_DIR"/patches.libcxx/*.patch; do
    193   (
    194     set +e
    195     run patch -p1 < "$PATCH"
    196     if [ $? != 0 ]; then
    197       echo "ERROR: Can't apply $PATCH properly!"
    198       exit 1
    199     fi
    200   )
    201 done
    202 
    203 echo "Updating local patches"
    204 run cd "$NDK_LIBCXX_DIR"
    205 run git rm -f patches.android/*
    206 run cp "$BUILD_DIR"/patches.android/* patches.android/
    207 run git add patches.android/*
    208 
    209 echo "Updating upstream.config"
    210 sed -i -e "s|revision: $REVISION|revision: $NEW_REVISION|" "$NDK_LIBCXX_DIR"/upstream.config
    211 git add "$NDK_LIBCXX_DIR"/upstream.config
    212 
    213 echo "Done updating to $NEW_REVISION."
    214