David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 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 | # |
| 6 | MACHINE=goldfish |
| 7 | VARIANT=goldfish |
| 8 | OUTPUT=/tmp/kernel-qemu |
Andrew Hsieh | 9ada5ea | 2012-05-30 16:09:29 +0800 | [diff] [blame] | 9 | CROSSPREFIX=arm-linux-androideabi- |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 10 | CONFIG=goldfish |
| 11 | |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 12 | # Determine the host architecture, and which default prebuilt tag we need. |
| 13 | # For the toolchain auto-detection. |
| 14 | # |
| 15 | HOST_OS=`uname -s` |
| 16 | case "$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 |
| 31 | esac |
| 32 | |
| 33 | # Default number of parallel jobs during the build: cores * 2 |
| 34 | JOBS=$(( $BUILD_NUM_CPUS * 2 )) |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 35 | |
| 36 | ARCH=arm |
| 37 | |
| 38 | OPTION_HELP=no |
| 39 | OPTION_ARMV7=no |
| 40 | OPTION_OUT= |
| 41 | OPTION_CROSS= |
| 42 | OPTION_ARCH= |
| 43 | OPTION_CONFIG= |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 44 | OPTION_JOBS= |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 45 | |
| 46 | for 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' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 67 | OPTION_JOBS=$optarg |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 68 | ;; |
| 69 | *) |
| 70 | echo "unknown option '$opt', use --help" |
| 71 | exit 1 |
| 72 | esac |
| 73 | done |
| 74 | |
| 75 | if [ $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' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 86 | echo " -j<number> launch <number> parallel build jobs [$JOBS]" |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 87 | 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 |
| 92 | fi |
| 93 | |
| 94 | if [ -n "$OPTION_ARCH" ]; then |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 95 | ARCH=$OPTION_ARCH |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 96 | fi |
| 97 | |
| 98 | if [ -n "$OPTION_CONFIG" ]; then |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 99 | CONFIG=$OPTION_CONFIG |
| 100 | else |
| 101 | if [ "$OPTION_ARMV7" = "yes" ]; then |
| 102 | CONFIG=goldfish_armv7 |
| 103 | fi |
| 104 | echo "Auto-config: --config=$CONFIG" |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 105 | fi |
| 106 | |
| 107 | # Check that we are in the kernel directory |
| 108 | if [ ! -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 |
| 112 | fi |
| 113 | |
| 114 | # Check output directory. |
| 115 | if [ -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 |
| 121 | else |
| 122 | mkdir -p $OUTPUT |
| 123 | fi |
| 124 | |
| 125 | if [ -n "$OPTION_CROSS" ] ; then |
| 126 | CROSSPREFIX="$OPTION_CROSS" |
| 127 | else |
| 128 | case $ARCH in |
| 129 | arm) |
Andrew Hsieh | 9ada5ea | 2012-05-30 16:09:29 +0800 | [diff] [blame] | 130 | CROSSTOOLCHAIN=arm-linux-androideabi-4.6 |
| 131 | CROSSPREFIX=arm-linux-androideabi- |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 132 | ;; |
| 133 | x86) |
H.J. Lu | 0621eeb | 2012-04-20 17:32:56 -0700 | [diff] [blame] | 134 | CROSSTOOLCHAIN=i686-linux-android-4.6 |
| 135 | CROSSPREFIX=i686-linux-android- |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 136 | ;; |
Bhanu Chetlapalli | 88f828e | 2012-05-21 16:39:51 -0700 | [diff] [blame] | 137 | mips) |
Andrew Hsieh | 9ada5ea | 2012-05-30 16:09:29 +0800 | [diff] [blame] | 138 | CROSSTOOLCHAIN=mipsel-linux-android-4.6 |
Bhanu Chetlapalli | 88f828e | 2012-05-21 16:39:51 -0700 | [diff] [blame] | 139 | CROSSPREFIX=mipsel-linux-android- |
| 140 | ;; |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 141 | *) |
| 142 | echo "ERROR: Unsupported architecture!" |
| 143 | exit 1 |
| 144 | ;; |
| 145 | esac |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 146 | echo "Auto-config: --cross=$CROSSPREFIX" |
| 147 | fi |
| 148 | |
Andrew Hsieh | 6f0425a | 2011-11-03 17:20:32 +0800 | [diff] [blame] | 149 | ZIMAGE=zImage |
| 150 | |
| 151 | case $ARCH in |
| 152 | x86) |
| 153 | ZIMAGE=bzImage |
| 154 | ;; |
Bhanu Chetlapalli | 88f828e | 2012-05-21 16:39:51 -0700 | [diff] [blame] | 155 | mips) |
| 156 | ZIMAGE= |
| 157 | ;; |
Andrew Hsieh | 6f0425a | 2011-11-03 17:20:32 +0800 | [diff] [blame] | 158 | esac |
| 159 | |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 160 | # If the cross-compiler is not in the path, try to find it automatically |
| 161 | CROSS_COMPILER="${CROSSPREFIX}gcc" |
| 162 | CROSS_COMPILER_VERSION=$($CROSS_COMPILER --version 2>/dev/null) |
| 163 | if [ $? != 0 ] ; then |
| 164 | BUILD_TOP=$ANDROID_BUILD_TOP |
| 165 | if [ -z "$BUILD_TOP" ]; then |
| 166 | # Assume this script is under external/qemu/distrib/ in the |
| 167 | # Android source tree. |
| 168 | BUILD_TOP=$(dirname $0)/../../.. |
Andrew Hsieh | 9ada5ea | 2012-05-30 16:09:29 +0800 | [diff] [blame] | 169 | if [ ! -d "$BUILD_TOP/prebuilts" ]; then |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 170 | BUILD_TOP= |
| 171 | else |
| 172 | BUILD_TOP=$(cd $BUILD_TOP && pwd) |
| 173 | fi |
| 174 | fi |
Andrew Hsieh | 9ada5ea | 2012-05-30 16:09:29 +0800 | [diff] [blame] | 175 | CROSSPREFIX=$BUILD_TOP/prebuilts/gcc/$HOST_TAG/$ARCH/$CROSSTOOLCHAIN/bin/$CROSSPREFIX |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 176 | if [ "$BUILD_TOP" -a -f ${CROSSPREFIX}gcc ]; then |
| 177 | echo "Auto-config: --cross=$CROSSPREFIX" |
| 178 | else |
| 179 | echo "It looks like $CROSS_COMPILER is not in your path ! Aborting." |
| 180 | exit 1 |
| 181 | fi |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 182 | fi |
| 183 | |
| 184 | export CROSS_COMPILE="$CROSSPREFIX" ARCH SUBARCH=$ARCH |
| 185 | |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 186 | if [ "$OPTION_JOBS" ]; then |
| 187 | JOBS=$OPTION_JOBS |
| 188 | else |
| 189 | echo "Auto-config: -j$JOBS" |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 190 | fi |
| 191 | |
David 'Digit' Turner | bc9cbbe | 2011-12-14 21:18:50 +0100 | [diff] [blame] | 192 | |
| 193 | # Special magic redirection with our magic toolbox script |
Andrew Hsieh | 9ada5ea | 2012-05-30 16:09:29 +0800 | [diff] [blame] | 194 | # This is needed to add extra compiler flags to compiler. |
| 195 | # See kernel-toolchain/android-kernel-toolchain-* for details |
David 'Digit' Turner | bc9cbbe | 2011-12-14 21:18:50 +0100 | [diff] [blame] | 196 | # |
Andrew Hsieh | 9ada5ea | 2012-05-30 16:09:29 +0800 | [diff] [blame] | 197 | export REAL_CROSS_COMPILE="$CROSS_COMPILE" |
| 198 | CROSS_COMPILE=$(dirname "$0")/kernel-toolchain/android-kernel-toolchain- |
David 'Digit' Turner | bc9cbbe | 2011-12-14 21:18:50 +0100 | [diff] [blame] | 199 | |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 200 | # Do the build |
| 201 | # |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 202 | rm -f include/asm && |
| 203 | make ${CONFIG}_defconfig && # configure the kernel |
| 204 | make -j$JOBS # build it |
| 205 | |
| 206 | if [ $? != 0 ] ; then |
| 207 | echo "Could not build the kernel. Aborting !" |
| 208 | exit 1 |
| 209 | fi |
| 210 | |
David 'Digit' Turner | fe6c89d | 2011-03-09 18:41:26 +0100 | [diff] [blame] | 211 | # Note: The exact names of the output files are important for the Android build, |
| 212 | # do not change the definitions lightly. |
| 213 | case $CONFIG in |
| 214 | vbox*) |
| 215 | OUTPUT_KERNEL=kernel-vbox |
| 216 | OUTPUT_VMLINUX=vmlinux-vbox |
| 217 | ;; |
| 218 | goldfish) |
| 219 | OUTPUT_KERNEL=kernel-qemu |
| 220 | OUTPUT_VMLINUX=vmlinux-qemu |
| 221 | ;; |
| 222 | goldfish_armv7) |
| 223 | OUTPUT_KERNEL=kernel-qemu-armv7 |
| 224 | OUTPUT_VMLINUX=vmlinux-qemu-armv7 |
| 225 | ;; |
| 226 | *) |
| 227 | OUTPUT_KERNEL=kernel-$CONFIG |
| 228 | OUTPUT_VMLINUX=vmlinux-$CONFIG |
| 229 | esac |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 230 | |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 231 | cp -f vmlinux $OUTPUT/$OUTPUT_VMLINUX |
Bhanu Chetlapalli | 88f828e | 2012-05-21 16:39:51 -0700 | [diff] [blame] | 232 | if [ ! -z $ZIMAGE ]; then |
| 233 | cp -f arch/$ARCH/boot/$ZIMAGE $OUTPUT/$OUTPUT_KERNEL |
Bhanu Chetlapalli | 88f828e | 2012-05-21 16:39:51 -0700 | [diff] [blame] | 234 | else |
Bhanu Chetlapalli | 9bc2c5e | 2012-06-05 13:23:00 -0700 | [diff] [blame^] | 235 | cp -f vmlinux $OUTPUT/$OUTPUT_KERNEL |
Bhanu Chetlapalli | 88f828e | 2012-05-21 16:39:51 -0700 | [diff] [blame] | 236 | fi |
Bhanu Chetlapalli | 9bc2c5e | 2012-06-05 13:23:00 -0700 | [diff] [blame^] | 237 | echo "Kernel $CONFIG prebuilt images ($OUTPUT_KERNEL and $OUTPUT_VMLINUX) copied to $OUTPUT successfully !" |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 238 | |
David 'Digit' Turner | 5c25d3d | 2011-03-05 00:20:16 +0100 | [diff] [blame] | 239 | exit 0 |