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