1#!/bin/sh
2#
3# A small script used to rebuild the Android goldfish kernel image
4# See docs/KERNEL.TXT for usage instructions.
5#
6MACHINE=goldfish
7VARIANT=goldfish
8OUTPUT=/tmp/kernel-qemu
9CROSSPREFIX=arm-linux-androideabi-
10CONFIG=goldfish
11
12# Determine the host architecture, and which default prebuilt tag we need.
13# For the toolchain auto-detection.
14#
15HOST_OS=`uname -s`
16case "$HOST_OS" in
17    Darwin)
18        HOST_OS=darwin
19        HOST_TAG=darwin-x86
20        BUILD_NUM_CPUS=$(sysctl -n hw.ncpu)
21        ;;
22    Linux)
23        # note that building  32-bit binaries on x86_64 is handled later
24        HOST_OS=linux
25        HOST_TAG=linux-x86
26        BUILD_NUM_CPUS=$(grep -c processor /proc/cpuinfo)
27        ;;
28    *)
29        echo "ERROR: Unsupported OS: $HOST_OS"
30        exit 1
31esac
32
33# Default number of parallel jobs during the build: cores * 2
34JOBS=$(( $BUILD_NUM_CPUS * 2 ))
35
36ARCH=arm
37
38OPTION_HELP=no
39OPTION_ARMV7=yes
40OPTION_OUT=
41OPTION_CROSS=
42OPTION_ARCH=
43OPTION_CONFIG=
44OPTION_SAVEDEFCONFIG=no
45OPTION_JOBS=
46OPTION_VERBOSE=
47CCACHE=
48
49case "$USE_CCACHE" in
50    "")
51        CCACHE=
52        ;;
53    *)
54        # use ccache bundled in AOSP source tree
55        CCACHE=${ANDROID_BUILD_TOP:-$(dirname $0)/../../..}/prebuilts/misc/$HOST_TAG/ccache/ccache
56        [ -x $CCACHE ] || CCACHE=
57        ;;
58esac
59
60for opt do
61    optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)')
62    case $opt in
63    --help|-h|-\?) OPTION_HELP=yes
64        ;;
65    --armv5)
66        OPTION_ARMV7=no
67        ;;
68    --armv7)
69        OPTION_ARMV7=yes
70        ;;
71    --out=*)
72        OPTION_OUT=$optarg
73        ;;
74    --cross=*)
75        OPTION_CROSS=$optarg
76        ;;
77    --arch=*)
78        OPTION_ARCH=$optarg
79        ;;
80    --config=*)
81        OPTION_CONFIG=$optarg
82        ;;
83    --savedefconfig)
84        OPTION_SAVEDEFCONFIG=yes
85        ;;
86    --ccache=*)
87        CCACHE=$optarg
88        ;;
89    --verbose)
90        OPTION_VERBOSE=true
91        ;;
92    -j*)
93        OPTION_JOBS=$optarg
94        ;;
95    *)
96        echo "unknown option '$opt', use --help"
97        exit 1
98    esac
99done
100
101if [ $OPTION_HELP = "yes" ] ; then
102    echo "Rebuild the prebuilt kernel binary for Android's emulator."
103    echo ""
104    echo "options (defaults are within brackets):"
105    echo ""
106    echo "  --help                   print this message"
107    echo "  --arch=<arch>            change target architecture [$ARCH]"
108    echo "  --armv5                  build ARMv5 binaries"
109    echo "  --armv7                  build ARMv7 binaries (default. see note below)"
110    echo "  --out=<directory>        output directory [$OUTPUT]"
111    echo "  --cross=<prefix>         cross-toolchain prefix [$CROSSPREFIX]"
112    echo "  --config=<name>          kernel config name [$CONFIG]"
113    echo "  --savedefconfig          run savedefconfig"
114    echo "  --ccache=<path>          use compiler cache [${CCACHE:-not set}]"
115    echo "  --verbose                show build commands"
116    echo "  -j<number>               launch <number> parallel build jobs [$JOBS]"
117    echo ""
118    echo "NOTE: --armv7 is equivalent to --config=goldfish_armv7. It is"
119    echo "      ignored if --config=<name> is used."
120    echo ""
121    exit 0
122fi
123
124if [ -n "$OPTION_ARCH" ]; then
125    ARCH=$OPTION_ARCH
126fi
127
128if [ -n "$OPTION_CONFIG" ]; then
129    CONFIG=$OPTION_CONFIG
130else
131    if [ "$ARCH" = "arm" -a "$OPTION_ARMV7" = "yes" ]; then
132        CONFIG=goldfish_armv7
133    fi
134    echo "Auto-config: --config=$CONFIG"
135fi
136
137# Check that we are in the kernel directory
138if [ ! -d arch/$ARCH/mach-$MACHINE ] ; then
139    echo "Cannot find arch/$ARCH/mach-$MACHINE. Please cd to the kernel source directory."
140    echo "Aborting."
141    #exit 1
142fi
143
144# Check output directory.
145if [ -n "$OPTION_OUT" ] ; then
146    if [ ! -d "$OPTION_OUT" ] ; then
147        echo "Output directory '$OPTION_OUT' does not exist ! Aborting."
148        exit 1
149    fi
150    OUTPUT=$OPTION_OUT
151else
152    mkdir -p $OUTPUT
153fi
154
155if [ -n "$OPTION_CROSS" ] ; then
156    CROSSPREFIX="$OPTION_CROSS"
157else
158    case $ARCH in
159        arm)
160            CROSSTOOLCHAIN=arm-linux-androideabi-4.6
161            CROSSPREFIX=arm-linux-androideabi-
162            ;;
163        x86)
164            CROSSTOOLCHAIN=i686-linux-android-4.6
165            CROSSPREFIX=i686-linux-android-
166            ;;
167        mips)
168            CROSSTOOLCHAIN=mipsel-linux-android-4.6
169            CROSSPREFIX=mipsel-linux-android-
170            ;;
171        *)
172            echo "ERROR: Unsupported architecture!"
173            exit 1
174            ;;
175    esac
176    echo "Auto-config: --cross=$CROSSPREFIX"
177fi
178
179ZIMAGE=zImage
180
181case $ARCH in
182    x86)
183        ZIMAGE=bzImage
184        ;;
185    mips)
186        ZIMAGE=
187        ;;
188esac
189
190# If the cross-compiler is not in the path, try to find it automatically
191CROSS_COMPILER="${CROSSPREFIX}gcc"
192CROSS_COMPILER_VERSION=$($CROSS_COMPILER --version 2>/dev/null)
193if [ $? != 0 ] ; then
194    BUILD_TOP=$ANDROID_BUILD_TOP
195    if [ -z "$BUILD_TOP" ]; then
196        # Assume this script is under external/qemu/distrib/ in the
197        # Android source tree.
198        BUILD_TOP=$(dirname $0)/../../..
199        if [ ! -d "$BUILD_TOP/prebuilts" ]; then
200            BUILD_TOP=
201        else
202            BUILD_TOP=$(cd $BUILD_TOP && pwd)
203        fi
204    fi
205    CROSSPREFIX=$BUILD_TOP/prebuilts/gcc/$HOST_TAG/$ARCH/$CROSSTOOLCHAIN/bin/$CROSSPREFIX
206    if [ "$BUILD_TOP" -a -f ${CROSSPREFIX}gcc ]; then
207        echo "Auto-config: --cross=$CROSSPREFIX"
208    else
209        echo "It looks like $CROSS_COMPILER is not in your path ! Aborting."
210        exit 1
211    fi
212fi
213
214if [ "$CCACHE" ] ; then
215    echo "Using ccache program: $CCACHE"
216    CROSSPREFIX="$CCACHE $CROSSPREFIX"
217fi
218
219export CROSS_COMPILE="$CROSSPREFIX" ARCH SUBARCH=$ARCH
220
221if [ "$OPTION_JOBS" ]; then
222    JOBS=$OPTION_JOBS
223else
224    echo "Auto-config: -j$JOBS"
225fi
226
227
228# Special magic redirection with our magic toolbox script
229# This is needed to add extra compiler flags to compiler.
230# See kernel-toolchain/android-kernel-toolchain-* for details
231#
232export REAL_CROSS_COMPILE="$CROSS_COMPILE"
233CROSS_COMPILE=$(dirname "$0")/kernel-toolchain/android-kernel-toolchain-
234
235MAKE_FLAGS=
236if [ "$OPTION_VERBOSE" ]; then
237  MAKE_FLAGS="$MAKE_FLAGS V=1"
238fi
239
240# Do the build
241#
242rm -f include/asm &&
243make ${CONFIG}_defconfig &&    # configure the kernel
244make -j$JOBS $MAKE_FLAGS       # build it
245
246if [ $? != 0 ] ; then
247    echo "Could not build the kernel. Aborting !"
248    exit 1
249fi
250
251if [ "$OPTION_SAVEDEFCONFIG" = "yes" ]; then
252    make savedefconfig
253    mv -f defconfig arch/$ARCH/configs/${CONFIG}_defconfig
254fi
255
256# Note: The exact names of the output files are important for the Android build,
257#       do not change the definitions lightly.
258case $CONFIG in
259    vbox*)
260        OUTPUT_KERNEL=kernel-vbox
261        OUTPUT_VMLINUX=vmlinux-vbox
262        ;;
263    goldfish)
264        OUTPUT_KERNEL=kernel-qemu
265        OUTPUT_VMLINUX=vmlinux-qemu
266        ;;
267    goldfish_armv7)
268        OUTPUT_KERNEL=kernel-qemu-armv7
269        OUTPUT_VMLINUX=vmlinux-qemu-armv7
270        ;;
271    *)
272        OUTPUT_KERNEL=kernel-$CONFIG
273        OUTPUT_VMLINUX=vmlinux-$CONFIG
274esac
275
276cp -f vmlinux $OUTPUT/$OUTPUT_VMLINUX
277if [ ! -z $ZIMAGE ]; then
278    cp -f arch/$ARCH/boot/$ZIMAGE $OUTPUT/$OUTPUT_KERNEL
279else
280    cp -f vmlinux $OUTPUT/$OUTPUT_KERNEL
281fi
282echo "Kernel $CONFIG prebuilt images ($OUTPUT_KERNEL and $OUTPUT_VMLINUX) copied to $OUTPUT successfully !"
283
284exit 0
285