blob: 20d85c62760a02ea183f9aed9caf21133753c019 [file] [log] [blame]
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +01001#!/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-eabi-
10CONFIG=goldfish
11
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +010012# 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 ))
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +010035
36ARCH=arm
37
38OPTION_HELP=no
39OPTION_ARMV7=no
40OPTION_OUT=
41OPTION_CROSS=
42OPTION_ARCH=
43OPTION_CONFIG=
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +010044OPTION_JOBS=
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +010045
46for opt do
47 optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)')
48 case $opt in
49 --help|-h|-\?) OPTION_HELP=yes
50 ;;
51 --armv7)
52 OPTION_ARMV7=yes
53 ;;
54 --out=*)
55 OPTION_OUT=$optarg
56 ;;
57 --cross=*)
58 OPTION_CROSS=$optarg
59 ;;
60 --arch=*)
61 OPTION_ARCH=$optarg
62 ;;
63 --config=*)
64 OPTION_CONFIG=$optarg
65 ;;
66 -j*)
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +010067 OPTION_JOBS=$optarg
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +010068 ;;
69 *)
70 echo "unknown option '$opt', use --help"
71 exit 1
72 esac
73done
74
75if [ $OPTION_HELP = "yes" ] ; then
76 echo "Rebuild the prebuilt kernel binary for Android's emulator."
77 echo ""
78 echo "options (defaults are within brackets):"
79 echo ""
80 echo " --help print this message"
81 echo " --arch=<arch> change target architecture [$ARCH]"
82 echo " --armv7 build ARMv7 binaries (see note below)"
83 echo " --out=<directory> output directory [$OUTPUT]"
84 echo " --cross=<prefix> cross-toolchain prefix [$CROSSPREFIX]"
85 echo " --config=<name> kernel config name [$CONFIG]"
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +010086 echo " -j<number> launch <number> parallel build jobs [$JOBS]"
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +010087 echo ""
88 echo "NOTE: --armv7 is equivalent to --config=goldfish_armv7. It is"
89 echo " ignored if --config=<name> is used."
90 echo ""
91 exit 0
92fi
93
94if [ -n "$OPTION_ARCH" ]; then
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +010095 ARCH=$OPTION_ARCH
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +010096fi
97
98if [ -n "$OPTION_CONFIG" ]; then
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +010099 CONFIG=$OPTION_CONFIG
100else
101 if [ "$OPTION_ARMV7" = "yes" ]; then
102 CONFIG=goldfish_armv7
103 fi
104 echo "Auto-config: --config=$CONFIG"
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100105fi
106
107# Check that we are in the kernel directory
108if [ ! -d arch/$ARCH/mach-$MACHINE ] ; then
109 echo "Cannot find arch/$ARCH/mach-$MACHINE. Please cd to the kernel source directory."
110 echo "Aborting."
111 #exit 1
112fi
113
114# Check output directory.
115if [ -n "$OPTION_OUT" ] ; then
116 if [ ! -d "$OPTION_OUT" ] ; then
117 echo "Output directory '$OPTION_OUT' does not exist ! Aborting."
118 exit 1
119 fi
120 OUTPUT=$OPTION_OUT
121else
122 mkdir -p $OUTPUT
123fi
124
125if [ -n "$OPTION_CROSS" ] ; then
126 CROSSPREFIX="$OPTION_CROSS"
127else
128 case $ARCH in
129 arm)
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +0100130 CROSSTOOLCHAIN=arm-eabi-4.4.3
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100131 CROSSPREFIX=arm-eabi-
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100132 ;;
133 x86)
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +0100134 CROSSTOOLCHAIN=i686-android-linux-4.4.3
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100135 CROSSPREFIX=i686-android-linux-
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100136 ;;
137 *)
138 echo "ERROR: Unsupported architecture!"
139 exit 1
140 ;;
141 esac
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +0100142 echo "Auto-config: --cross=$CROSSPREFIX"
143fi
144
Andrew Hsieh6f0425a2011-11-03 17:20:32 +0800145ZIMAGE=zImage
146
147case $ARCH in
148 x86)
149 ZIMAGE=bzImage
150 ;;
151esac
152
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +0100153# If the cross-compiler is not in the path, try to find it automatically
154CROSS_COMPILER="${CROSSPREFIX}gcc"
155CROSS_COMPILER_VERSION=$($CROSS_COMPILER --version 2>/dev/null)
156if [ $? != 0 ] ; then
157 BUILD_TOP=$ANDROID_BUILD_TOP
158 if [ -z "$BUILD_TOP" ]; then
159 # Assume this script is under external/qemu/distrib/ in the
160 # Android source tree.
161 BUILD_TOP=$(dirname $0)/../../..
162 if [ ! -d "$BUILD_TOP/prebuilt" ]; then
163 BUILD_TOP=
164 else
165 BUILD_TOP=$(cd $BUILD_TOP && pwd)
166 fi
167 fi
168 CROSSPREFIX=$BUILD_TOP/prebuilt/$HOST_TAG/toolchain/$CROSSTOOLCHAIN/bin/$CROSSPREFIX
169 if [ "$BUILD_TOP" -a -f ${CROSSPREFIX}gcc ]; then
170 echo "Auto-config: --cross=$CROSSPREFIX"
171 else
172 echo "It looks like $CROSS_COMPILER is not in your path ! Aborting."
173 exit 1
174 fi
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100175fi
176
177export CROSS_COMPILE="$CROSSPREFIX" ARCH SUBARCH=$ARCH
178
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +0100179if [ "$OPTION_JOBS" ]; then
180 JOBS=$OPTION_JOBS
181else
182 echo "Auto-config: -j$JOBS"
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100183fi
184
David 'Digit' Turnerbc9cbbe2011-12-14 21:18:50 +0100185
186# Special magic redirection with our magic toolbox script
187# This is needed to add extra compiler flags for x86
188#
189# We could use that for ARM, but don't need to at the moment.
190#
191if [ "$ARCH" = "x86" ]; then
192 export REAL_CROSS_COMPILE="$CROSS_COMPILE"
193 export ARCH
194 CROSS_COMPILE=$(dirname "$0")/kernel-toolchain/android-kernel-toolchain-
195fi
196
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +0100197# Do the build
198#
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100199rm -f include/asm &&
200make ${CONFIG}_defconfig && # configure the kernel
201make -j$JOBS # build it
202
203if [ $? != 0 ] ; then
204 echo "Could not build the kernel. Aborting !"
205 exit 1
206fi
207
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +0100208# Note: The exact names of the output files are important for the Android build,
209# do not change the definitions lightly.
210case $CONFIG in
211 vbox*)
212 OUTPUT_KERNEL=kernel-vbox
213 OUTPUT_VMLINUX=vmlinux-vbox
214 ;;
215 goldfish)
216 OUTPUT_KERNEL=kernel-qemu
217 OUTPUT_VMLINUX=vmlinux-qemu
218 ;;
219 goldfish_armv7)
220 OUTPUT_KERNEL=kernel-qemu-armv7
221 OUTPUT_VMLINUX=vmlinux-qemu-armv7
222 ;;
223 *)
224 OUTPUT_KERNEL=kernel-$CONFIG
225 OUTPUT_VMLINUX=vmlinux-$CONFIG
226esac
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100227
228cp -f arch/$ARCH/boot/$ZIMAGE $OUTPUT/$OUTPUT_KERNEL
229cp -f vmlinux $OUTPUT/$OUTPUT_VMLINUX
230
David 'Digit' Turnerfe6c89d2011-03-09 18:41:26 +0100231echo "Kernel $CONFIG prebuilt images ($OUTPUT_KERNEL and $OUTPUT_VMLINUX) copied to $OUTPUT successfully !"
David 'Digit' Turner5c25d3d2011-03-05 00:20:16 +0100232exit 0