1#!/bin/sh 2# 3# Copyright (C) 2013 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# cleanup-headers.sh 18# 19# This tool is used when we want to generate NDK arch headers. 20# It will populate $NDK_DIR/../development/ndk/platforms/android-*/arch-$NEW_ARCH by default. 21# 22 23PROGDIR=`dirname $0` 24PROGDIR=`cd $PROGDIR && pwd` 25. "$PROGDIR/prebuilt-common.sh" 26NDK_DIR=$ANDROID_NDK_ROOT 27 28DEV_DIR="$NDK_DIR/../development" 29register_var_option "--development-dir=<path>" DEV_DIR "Specify where the development dir is" 30 31PROGRAM_PARAMETERS="<old-arch-name> <new-arch-name>" 32PROGRAM_DESCRIPTION=\ 33"This tool is used when we want to generate NDK arch headers. 34It will populate $NDK_DIR/../development/ndk/platforms/android-*/arch-$NEW_ARCH by default." 35 36extract_parameters "$@" 37 38set_parameters() { 39 OLD_ARCH=$1 40 NEW_ARCH=$2 41 DEV_NDK_PLATFORM_DIR=$DEV_DIR/ndk/platforms 42} 43 44set_parameters $PARAMETERS 45 46if [ -z "$OLD_ARCH" ] || [ -z "$NEW_ARCH" ]; then 47 dump "[Error] Must specify old and new arch names" 48 exit 1 49fi 50 51if [ -n "`find $DEV_NDK_PLATFORM_DIR -type d | grep arch-$NEW_ARCH`" ]; then 52 log "[Warning] Already exist new arch headers. Remove it first." 53 for API in $API_LEVELS; do 54 rm -rf $DEV_NDK_PLATFORM_DIR/android-$API/arch-$NEW_ARCH 55 done 56fi 57 58replace() { 59 local HEADERS_DIR=$1 60 local OLD=$2 61 local NEW=$3 62 FILES="`find $HEADERS_DIR | xargs grep $OLD | awk -F ':' '{print $1}' | sort -u | xargs`" 63 for FILE in $FILES; do 64 TMP_FILE=`mktemp` 65 cp -a $FILE $TMP_FILE 66 cat $TMP_FILE | sed -e "s/$OLD/$NEW/g" > $FILE 67 rm -f $TMP_FILE 68 done 69} 70 71remove() { 72 local HEADERS_DIR=$1 73 local PATTERN=$2 74 local NAME=$3 75 FILES="`find $HEADERS_DIR | xargs grep $NAME | awk -F ':' '{print $1}' | sort -u | xargs`" 76 for FILE in $FILES; do 77 TMP_FILE=`mktemp` 78 cp -a $FILE $TMP_FILE 79 cat $TMP_FILE | sed -e "s/.*${PATTERN}.*${NAME}.*//g" > $FILE 80 rm -f $TMP_FILE 81 done 82} 83 84OLD_PATTERN=ARM 85NEW_PATTERN=MACHINE 86TMP_PATTERN=NDKTMPMACHINE 87KEEP_NO_RENAME_STUB= 88replace_stub_to_temp() { 89 local COMMAND=$1 90 if [ "$COMMAND" = "add" ]; then 91 local NAME=$2 92 KEEP_NO_RENAME_STUB="$KEEP_NO_RENAME_STUB $NAME" 93 elif [ "$COMMAND" = "do" ] || [ "$COMMAND" = "revert" ]; then 94 local DIR=$2 95 for stub in $KEEP_NO_RENAME_STUB; do 96 local new_stub=`echo $stub | sed -e "s/$OLD_PATTERN/$TMP_PATTERN/g"` 97 if [ "$COMMAND" = "do" ]; then 98 replace $DIR $stub $new_stub 99 else 100 replace $DIR $new_stub $stub 101 fi 102 done 103 fi 104} 105 106for API in $API_LEVELS; do 107 log2 "API: $API" 108 OLD_PLATFORM_DIR=$DEV_NDK_PLATFORM_DIR/android-$API/arch-$OLD_ARCH 109 NEW_PLATFORM_DIR=$DEV_NDK_PLATFORM_DIR/android-$API/arch-$NEW_ARCH 110 if [ -d "$OLD_PLATFORM_DIR/symbols" ]; then 111 run copy_directory $OLD_PLATFORM_DIR/symbols $NEW_PLATFORM_DIR/symbols 112 fi 113 if [ -d "$OLD_PLATFORM_DIR/include" ]; then 114 run copy_directory $OLD_PLATFORM_DIR/include $NEW_PLATFORM_DIR/include 115 fi 116 117 PATCH_PLATFORM_DIR=$DEV_NDK_PLATFORM_DIR/android-$API/header-patches 118 if [ -d "$PATCH_PLATFORM_DIR" ]; then 119 run rm -rf $NEW_PLATFORM_DIR/include/asm/arch 120 for asm_header_stem in domain dyntick fpstate glue hardware ide mtd-xip pgtable-hwdef proc-fns procinfo suspend topology vga; do 121 run rm -f $NEW_PLATFORM_DIR/include/asm/${asm_header_stem}.h 122 done 123 for machine_header_stem in cpu-features; do 124 run rm -f $NEW_PLATFORM_DIR/include/machine/${machine_header_stem}.h 125 done 126 127 # Some keyword should not be replaced nor droped 128 replace_stub_to_temp add ARMv6 129 replace_stub_to_temp add PARMRK 130 replace_stub_to_temp add __ARM_EABI__ 131 replace_stub_to_temp add __ARMEB__ 132 replace_stub_to_temp add __LINUX_ARM_ARCH__ 133 replace_stub_to_temp do $NEW_PLATFORM_DIR/include 134 135 remove $NEW_PLATFORM_DIR/include "#include" glue.h 136 remove $NEW_PLATFORM_DIR/include "#include" arch 137 remove $NEW_PLATFORM_DIR/include "#include" mach 138 remove $NEW_PLATFORM_DIR/include "#include" domain.h 139 remove $NEW_PLATFORM_DIR/include "#include" pgtable-hwdef.h 140 remove $NEW_PLATFORM_DIR/include "#include" proc-fns.h 141 142 remove $NEW_PLATFORM_DIR/include "#define" EM_ARM 143 remove $NEW_PLATFORM_DIR/include "#define" MODULE_ARCH_VERMAGIC 144 remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_BASE 145 remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_breakpoint 146 remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_cacheflush 147 remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_usr26 148 remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_usr32 149 remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_set_tls 150 151 replace $NEW_PLATFORM_DIR $OLD_PATTERN $NEW_PATTERN 152 replace_stub_to_temp revert $NEW_PLATFORM_DIR/include 153 154 FILES="`cd $PATCH_PLATFORM_DIR && find include -type f | xargs`" 155 for FILE in $FILES; do 156 run mkdir -p $NEW_PLATFORM_DIR/`dirname $FILE` 157 run cp -a $PATCH_PLATFORM_DIR/$FILE $NEW_PLATFORM_DIR/$FILE 158 done 159 160 PATCH_FILE=$PATCH_PLATFORM_DIR/headers.patch 161 if [ -f "$PATCH_FILE" ]; then 162 cd $NEW_PLATFORM_DIR && patch -p1 -s < $PATCH_FILE 163 fi 164 fi 165done 166