1#!/bin/bash 2# Quick semi-auto file to build Windows SDK tools. 3# 4# Limitations: 5# - Expects the emulator has been built first, will pick it up from prebuilt. 6# - Run in Cygwin 7# - Needs Cygwin package zip 8# - Expects to have one of the existing SDK (Darwin or Linux) to build the Windows one 9 10set -e # Fail this script as soon as a command fails -- fail early, fail fast 11 12# Set to 1 to force removal of old unzipped SDK. Only disable for debugging, as it 13# will make some rm/mv commands to fail. 14FORCE="1" 15 16PROG_NAME="$0" 17SDK_ZIP="$1" 18DIST_DIR="$2" 19TEMP_DIR="$3" 20[ -z "$TEMP_DIR" ] && TEMP_DIR=${TMP:-/tmp} 21 22function die() { 23 echo "Error:" $* 24 echo "Aborting" 25 exit 1 26} 27 28function usage() { 29 local NAME 30 NAME=`basename ${PROG_NAME}` 31 echo "Usage: ${NAME} linux_or_mac_sdk.zip output_dir [temp_dir]" 32 echo "If temp_dir is not given, \$TMP is used. If that's missing, /tmp is used." 33 status 34 exit 2 35} 36 37function status() { 38 echo "Current values:" 39 echo "- Input SDK: ${SDK_ZIP:-missing}" 40 echo "- Output dir: ${DIST_DIR:-missing}" 41 echo "- Temp dir: ${TEMP_DIR:-missing}" 42} 43 44function check() { 45 [ -f "$SDK_ZIP" ] || usage 46 [ -d "$DIST_DIR" ] || usage 47 48 # Use the BUILD_ID as SDK_NUMBER if defined, otherwise try to get it from the 49 # provided zip filename. 50 if [ -f config/build_id.make ]; then 51 BUILD_ID=`cat config/build_id.make | sed -n '/BUILD_ID=/s/^[^=]\+=\(.*\)$/\1/p'` 52 [ -n "$BUILD_ID" ] && SDK_NUMBER="$BUILD_ID" 53 fi 54 if [ -z "$SDK_NUMBER" ]; then 55 # Look for a pattern like "anything_sdknumber.extension" 56 # The pattern is now "any-thing_sdknumber_anything-else.extension" 57 # 58 # The bottom line is that the SDK number is whatever is enclosed by 59 # the LAST couple of underscores. You can have underscores *before* the 60 # SDK number if you want, but not after, e.g these are valid: 61 # android_sdk_4242_platform.zip or blah_42_.zip 62 # 63 # SDK_NUMBER will be empty if nothing matched. 64 filename=`basename "$SDK_ZIP"` 65 SDK_NUMBER=`echo $filename | sed -n 's/^.*_\([^_./]\+\)_[^_.]*\..*$/\1/p'` 66 fi 67 68 [ -n "$SDK_NUMBER" ] || die "Failed to extract the SDK number from $SDK_ZIP. Check its format." 69 70 [ $OSTYPE == "cygwin" ] || die "This expects to run under Cygwin" 71 [ -e `which zip` ] || die "Please install 'zip' package in Cygwin" 72 [ -f "build/envsetup.sh" ] || die "Please run this from the 'android' directory" 73 74 echo "Using SDK ${SDK_NUMBER}" 75} 76 77function build() { 78 79 echo 80 echo "Building..." 81 [ -n "$MAKE_OPT" ] && echo "Make options: $MAKE_OPT" 82 . build/envsetup.sh 83 make -j 4 emulator || die "Build failed" 84 # Disable parallel build: it generates "permission denied" issues when 85 # multiple "ar.exe" are running in parallel. 86 make aapt adb aidl \ 87 prebuilt \ 88 dexdump dmtracedump \ 89 fastboot \ 90 hprof-conv \ 91 mksdcard \ 92 sqlite3 \ 93 zipalign \ 94 || die "Build failed" 95} 96 97function package() { 98 echo 99 echo "Packaging..." 100 DEST_NAME="android-sdk_${SDK_NUMBER}_windows" 101 DEST_NAME_ZIP="${DEST_NAME}.zip" 102 103 TEMP_SDK_DIR="$TEMP_DIR/$DEST_NAME" 104 105 # Unzip current linux/mac SDK and rename using the windows name 106 if [[ -n "$FORCE" || ! -d "$TEMP_SDK_DIR" ]]; then 107 [ -e "$TEMP_SDK_DIR" ] && rm -rfv "$TEMP_SDK_DIR" # cleanup dest first if exists 108 UNZIPPED=`basename "$SDK_ZIP"` 109 UNZIPPED="$TEMP_DIR/${UNZIPPED/.zip/}" 110 [ -e "$UNZIPPED" ] && rm -rfv "$UNZIPPED" # cleanup unzip dir (if exists) 111 unzip "$SDK_ZIP" -d "$TEMP_DIR" 112 mv -v "$UNZIPPED" "$TEMP_SDK_DIR" 113 fi 114 115 # Assert that the package contains only one platform 116 PLATFORMS="$TEMP_SDK_DIR/platforms" 117 THE_PLATFORM=`echo $PLATFORMS/*` 118 PLATFORM_TOOLS=$THE_PLATFORM/tools 119 echo "Platform found: " $THE_PLATFORM 120 [[ -d "$THE_PLATFORM" ]] || die \ 121 "Error: One platform was expected in $SDK_ZIP. " \ 122 "Instead found " $THE_PLATFORM 123 [[ -d "$PLATFORM_TOOLS" ]] || die "Missing folder $PLATFORM_TOOLS." 124 125 # Package USB Driver 126 if type package_usb_driver 2>&1 | grep -q function ; then 127 package_usb_driver $TEMP_SDK_DIR 128 fi 129 130 # Remove obsolete stuff from tools & platform 131 TOOLS="$TEMP_SDK_DIR/tools" 132 LIB="$TEMP_SDK_DIR/tools/lib" 133 rm -v "$TOOLS"/{adb,android,apkbuilder,ddms,dmtracedump,draw9patch,emulator} 134 rm -v "$TOOLS"/{hierarchyviewer,hprof-conv,mksdcard,sqlite3,traceview,zipalign} 135 rm -v "$LIB"/*/swt.jar 136 rm -v "$PLATFORM_TOOLS"/{aapt,aidl,dx,dexdump} 137 138 # Copy all the new stuff in tools 139 # Note: some tools are first copied here and then moved in platforms/<name>/tools/ 140 cp -v out/host/windows-x86/bin/*.{exe,dll} "$TOOLS"/ 141 mkdir -pv "$LIB"/x86 142 cp -v prebuilt/windows/swt/swt.jar "$LIB"/x86/ 143 mkdir -pv "$LIB"/x86_64 144 cp -v prebuilt/windows-x86_64/swt/swt.jar "$LIB"/x86_64/ 145 146 # If you want the emulator NOTICE in the tools dir, uncomment the following line: 147 # cp -v external/qemu/NOTICE "$TOOLS"/emulator_NOTICE.txt 148 149 # We currently need libz from MinGW for aapt 150 cp -v /cygdrive/c/cygwin/bin/mgwz.dll "$TOOLS"/ 151 152 # Update a bunch of bat files 153 cp -v development/tools/apkbuilder/etc/apkbuilder.bat "$TOOLS"/ 154 cp -v development/tools/ddms/app/etc/ddms.bat "$TOOLS"/ 155 cp -v development/tools/traceview/etc/traceview.bat "$TOOLS"/ 156 cp -v development/tools/hierarchyviewer/etc/hierarchyviewer.bat "$TOOLS"/ 157 cp -v development/tools/draw9patch/etc/draw9patch.bat "$TOOLS"/ 158 cp -v development/tools/sdkmanager/app/etc/android.bat "$TOOLS"/ 159 160 # Put the JetCreator tools, content and docs (not available in the linux SDK) 161 JET="$TOOLS/Jet" 162 JETCREATOR="$JET/JetCreator" 163 JETDEMOCONTENT="$JET/demo_content" 164 JETLOGICTEMPLATES="$JET/logic_templates" 165 JETDOC="$TEMP_SDK_DIR/docs/JetCreator" 166 167 # need to rm these folders since a Mac SDK will have them and it might create a conflict 168 rm -rfv "$JET" 169 rm -rfv "$JETDOC" 170 171 # now create fresh folders for JetCreator 172 mkdir -v "$JET" 173 mkdir -v "$JETDOC" 174 175 cp -rv external/sonivox/jet_tools/JetCreator "$JETCREATOR"/ 176 cp -rv external/sonivox/jet_tools/JetCreator_content "$JETDEMOCONTENT"/ 177 cp -rv external/sonivox/jet_tools/logic_templates "$JETLOGICTEMPLATES"/ 178 chmod -vR u+w "$JETCREATOR" # fixes an issue where Cygwin might copy the above as u+rx only 179 cp -v prebuilt/windows/jetcreator/EASDLL.dll "$JETCREATOR"/ 180 181 cp -v external/sonivox/docs/JET_Authoring_Guidelines.html "$JETDOC"/ 182 cp -rv external/sonivox/docs/JET_Authoring_Guidelines_files "$JETDOC"/ 183 cp -v external/sonivox/docs/JET_Creator_User_Manual.html "$JETDOC"/ 184 cp -rv external/sonivox/docs/JET_Creator_User_Manual_files "$JETDOC"/ 185 186 # Copy or move platform specific tools to the default platform. 187 cp -v dalvik/dx/etc/dx.bat "$PLATFORM_TOOLS"/ 188 mv -v "$TOOLS"/{aapt.exe,aidl.exe,dexdump.exe} "$PLATFORM_TOOLS"/ 189 # Note: mgwz.dll must be both in SDK/tools for zipalign and in SDK/platform/XYZ/tools/ for aapt 190 cp -v "$TOOLS"/mgwz.dll "$PLATFORM_TOOLS"/ 191 192 # Fix EOL chars to make window users happy - fix all files at the top level only 193 # as well as all batch files including those in platforms/<name>/tools/ 194 find "$TEMP_SDK_DIR" -maxdepth 1 -type f -writable -print0 | xargs -0 unix2dos -D 195 find "$TEMP_SDK_DIR" -maxdepth 3 -name "*.bat" -type f -writable -print0 | xargs -0 unix2dos -D 196 197 # Done.. Zip it. Clean the temp folder ONLY if the zip worked (to ease debugging) 198 pushd "$TEMP_DIR" > /dev/null 199 [ -e "$DEST_NAME_ZIP" ] && rm -rfv "$DEST_NAME_ZIP" 200 zip -9r "$DEST_NAME_ZIP" "$DEST_NAME" && rm -rfv "$DEST_NAME" 201 popd > /dev/null 202 203 # Now move the final zip from the temp dest to the final dist dir 204 mv -v "$TEMP_DIR/$DEST_NAME_ZIP" "$DIST_DIR/$DEST_NAME_ZIP" 205 206 # We want fastboot and adb (and its DLLs) next to the new SDK 207 for i in fastboot.exe adb.exe AdbWinApi.dll AdbWinUsbApi.dll; do 208 cp -vf out/host/windows-x86/bin/$i "$DIST_DIR"/$i 209 done 210 211 echo "Done" 212 echo 213 echo "Resulting SDK is in $DIST_DIR/$DEST_NAME_ZIP" 214} 215 216check 217status 218build 219package 220 221echo "Done" 222