build-kernel.sh revision 40c434bda53accf3e837866d64a5323ac571edc4
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=
47
48for opt do
49    optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)')
50    case $opt in
51    --help|-h|-\?) OPTION_HELP=yes
52        ;;
53    --armv5)
54        OPTION_ARMV7=no
55        ;;
56    --armv7)
57        OPTION_ARMV7=yes
58        ;;
59    --out=*)
60        OPTION_OUT=$optarg
61        ;;
62    --cross=*)
63        OPTION_CROSS=$optarg
64        ;;
65    --arch=*)
66        OPTION_ARCH=$optarg
67        ;;
68    --config=*)
69        OPTION_CONFIG=$optarg
70        ;;
71    --savedefconfig)
72        OPTION_SAVEDEFCONFIG=yes
73        ;;
74    --verbose)
75        OPTION_VERBOSE=true
76        ;;
77    -j*)
78        OPTION_JOBS=$optarg
79        ;;
80    *)
81        echo "unknown option '$opt', use --help"
82        exit 1
83    esac
84done
85
86if [ $OPTION_HELP = "yes" ] ; then
87    echo "Rebuild the prebuilt kernel binary for Android's emulator."
88    echo ""
89    echo "options (defaults are within brackets):"
90    echo ""
91    echo "  --help                   print this message"
92    echo "  --arch=<arch>            change target architecture [$ARCH]"
93    echo "  --armv5                  build ARMv5 binaries"
94    echo "  --armv7                  build ARMv7 binaries (default. see note below)"
95    echo "  --out=<directory>        output directory [$OUTPUT]"
96    echo "  --cross=<prefix>         cross-toolchain prefix [$CROSSPREFIX]"
97    echo "  --config=<name>          kernel config name [$CONFIG]"
98    echo "  --savedefconfig          run savedefconfig"
99    echo "  --verbose                show build commands"
100    echo "  -j<number>               launch <number> parallel build jobs [$JOBS]"
101    echo ""
102    echo "NOTE: --armv7 is equivalent to --config=goldfish_armv7. It is"
103    echo "      ignored if --config=<name> is used."
104    echo ""
105    exit 0
106fi
107
108if [ -n "$OPTION_ARCH" ]; then
109    ARCH=$OPTION_ARCH
110fi
111
112if [ -n "$OPTION_CONFIG" ]; then
113    CONFIG=$OPTION_CONFIG
114else
115    if [ "$OPTION_ARMV7" = "yes" ]; then
116        CONFIG=goldfish_armv7
117    fi
118    echo "Auto-config: --config=$CONFIG"
119fi
120
121# Check that we are in the kernel directory
122if [ ! -d arch/$ARCH/mach-$MACHINE ] ; then
123    echo "Cannot find arch/$ARCH/mach-$MACHINE. Please cd to the kernel source directory."
124    echo "Aborting."
125    #exit 1
126fi
127
128# Check output directory.
129if [ -n "$OPTION_OUT" ] ; then
130    if [ ! -d "$OPTION_OUT" ] ; then
131        echo "Output directory '$OPTION_OUT' does not exist ! Aborting."
132        exit 1
133    fi
134    OUTPUT=$OPTION_OUT
135else
136    mkdir -p $OUTPUT
137fi
138
139if [ -n "$OPTION_CROSS" ] ; then
140    CROSSPREFIX="$OPTION_CROSS"
141else
142    case $ARCH in
143        arm)
144            CROSSTOOLCHAIN=arm-linux-androideabi-4.6
145            CROSSPREFIX=arm-linux-androideabi-
146            ;;
147        x86)
148            CROSSTOOLCHAIN=i686-linux-android-4.6
149            CROSSPREFIX=i686-linux-android-
150            ;;
151        mips)
152            CROSSTOOLCHAIN=mipsel-linux-android-4.6
153            CROSSPREFIX=mipsel-linux-android-
154            ;;
155        *)
156            echo "ERROR: Unsupported architecture!"
157            exit 1
158            ;;
159    esac
160    echo "Auto-config: --cross=$CROSSPREFIX"
161fi
162
163ZIMAGE=zImage
164
165case $ARCH in
166    x86)
167        ZIMAGE=bzImage
168        ;;
169    mips)
170        ZIMAGE=
171        ;;
172esac
173
174# If the cross-compiler is not in the path, try to find it automatically
175CROSS_COMPILER="${CROSSPREFIX}gcc"
176CROSS_COMPILER_VERSION=$($CROSS_COMPILER --version 2>/dev/null)
177if [ $? != 0 ] ; then
178    BUILD_TOP=$ANDROID_BUILD_TOP
179    if [ -z "$BUILD_TOP" ]; then
180        # Assume this script is under external/qemu/distrib/ in the
181        # Android source tree.
182        BUILD_TOP=$(dirname $0)/../../..
183        if [ ! -d "$BUILD_TOP/prebuilts" ]; then
184            BUILD_TOP=
185        else
186            BUILD_TOP=$(cd $BUILD_TOP && pwd)
187        fi
188    fi
189    CROSSPREFIX=$BUILD_TOP/prebuilts/gcc/$HOST_TAG/$ARCH/$CROSSTOOLCHAIN/bin/$CROSSPREFIX
190    if [ "$BUILD_TOP" -a -f ${CROSSPREFIX}gcc ]; then
191        echo "Auto-config: --cross=$CROSSPREFIX"
192    else
193        echo "It looks like $CROSS_COMPILER is not in your path ! Aborting."
194        exit 1
195    fi
196fi
197
198export CROSS_COMPILE="$CROSSPREFIX" ARCH SUBARCH=$ARCH
199
200if [ "$OPTION_JOBS" ]; then
201    JOBS=$OPTION_JOBS
202else
203    echo "Auto-config: -j$JOBS"
204fi
205
206
207# Special magic redirection with our magic toolbox script
208# This is needed to add extra compiler flags to compiler.
209# See kernel-toolchain/android-kernel-toolchain-* for details
210#
211export REAL_CROSS_COMPILE="$CROSS_COMPILE"
212CROSS_COMPILE=$(dirname "$0")/kernel-toolchain/android-kernel-toolchain-
213
214MAKE_FLAGS=
215if [ "$OPTION_VERBOSE" ]; then
216  MAKE_FLAGS="$MAKE_FLAGS V=1"
217fi
218
219# Do the build
220#
221rm -f include/asm &&
222make ${CONFIG}_defconfig &&    # configure the kernel
223make -j$JOBS $MAKE_FLAGS       # build it
224
225if [ $? != 0 ] ; then
226    echo "Could not build the kernel. Aborting !"
227    exit 1
228fi
229
230if [ "$OPTION_SAVEDEFCONFIG" = "yes" ]; then
231    make savedefconfig
232    mv -f defconfig arch/$ARCH/configs/${CONFIG}_defconfig
233fi
234
235# Note: The exact names of the output files are important for the Android build,
236#       do not change the definitions lightly.
237case $CONFIG in
238    vbox*)
239        OUTPUT_KERNEL=kernel-vbox
240        OUTPUT_VMLINUX=vmlinux-vbox
241        ;;
242    goldfish)
243        OUTPUT_KERNEL=kernel-qemu
244        OUTPUT_VMLINUX=vmlinux-qemu
245        ;;
246    goldfish_armv7)
247        OUTPUT_KERNEL=kernel-qemu-armv7
248        OUTPUT_VMLINUX=vmlinux-qemu-armv7
249        ;;
250    *)
251        OUTPUT_KERNEL=kernel-$CONFIG
252        OUTPUT_VMLINUX=vmlinux-$CONFIG
253esac
254
255cp -f vmlinux $OUTPUT/$OUTPUT_VMLINUX
256if [ ! -z $ZIMAGE ]; then
257    cp -f arch/$ARCH/boot/$ZIMAGE $OUTPUT/$OUTPUT_KERNEL
258else
259    cp -f vmlinux $OUTPUT/$OUTPUT_KERNEL
260fi
261echo "Kernel $CONFIG prebuilt images ($OUTPUT_KERNEL and $OUTPUT_VMLINUX) copied to $OUTPUT successfully !"
262
263exit 0
264