Home | History | Annotate | Download | only in distrib
      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
      9 CROSSPREFIX=arm-eabi-
     10 CONFIG=goldfish
     11 
     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 ))
     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=
     44 OPTION_JOBS=
     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*)
     67         OPTION_JOBS=$optarg
     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]"
     86     echo "  -j<number>               launch <number> parallel build jobs [$JOBS]"
     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
     95     ARCH=$OPTION_ARCH
     96 fi
     97 
     98 if [ -n "$OPTION_CONFIG" ]; then
     99     CONFIG=$OPTION_CONFIG
    100 else
    101     if [ "$OPTION_ARMV7" = "yes" ]; then
    102         CONFIG=goldfish_armv7
    103     fi
    104     echo "Auto-config: --config=$CONFIG"
    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)
    130             CROSSTOOLCHAIN=arm-eabi-4.4.3
    131             CROSSPREFIX=arm-eabi-
    132             ZIMAGE=zImage
    133             ;;
    134         x86)
    135             CROSSTOOLCHAIN=i686-android-linux-4.4.3
    136             CROSSPREFIX=i686-android-linux-
    137             ZIMAGE=bzImage
    138             ;;
    139         *)
    140             echo "ERROR: Unsupported architecture!"
    141             exit 1
    142             ;;
    143     esac
    144     echo "Auto-config: --cross=$CROSSPREFIX"
    145 fi
    146 
    147 # If the cross-compiler is not in the path, try to find it automatically
    148 CROSS_COMPILER="${CROSSPREFIX}gcc"
    149 CROSS_COMPILER_VERSION=$($CROSS_COMPILER --version 2>/dev/null)
    150 if [ $? != 0 ] ; then
    151     BUILD_TOP=$ANDROID_BUILD_TOP
    152     if [ -z "$BUILD_TOP" ]; then
    153         # Assume this script is under external/qemu/distrib/ in the
    154         # Android source tree.
    155         BUILD_TOP=$(dirname $0)/../../..
    156         if [ ! -d "$BUILD_TOP/prebuilt" ]; then
    157             BUILD_TOP=
    158         else
    159             BUILD_TOP=$(cd $BUILD_TOP && pwd)
    160         fi
    161     fi
    162     CROSSPREFIX=$BUILD_TOP/prebuilt/$HOST_TAG/toolchain/$CROSSTOOLCHAIN/bin/$CROSSPREFIX
    163     if [ "$BUILD_TOP" -a -f ${CROSSPREFIX}gcc ]; then
    164         echo "Auto-config: --cross=$CROSSPREFIX"
    165     else
    166         echo "It looks like $CROSS_COMPILER is not in your path ! Aborting."
    167         exit 1
    168     fi
    169 fi
    170 
    171 export CROSS_COMPILE="$CROSSPREFIX" ARCH SUBARCH=$ARCH
    172 
    173 if [ "$OPTION_JOBS" ]; then
    174     JOBS=$OPTION_JOBS
    175 else
    176     echo "Auto-config: -j$JOBS"
    177 fi
    178 
    179 # Do the build
    180 #
    181 rm -f include/asm &&
    182 make ${CONFIG}_defconfig &&    # configure the kernel
    183 make -j$JOBS                   # build it
    184 
    185 if [ $? != 0 ] ; then
    186     echo "Could not build the kernel. Aborting !"
    187     exit 1
    188 fi
    189 
    190 # Note: The exact names of the output files are important for the Android build,
    191 #       do not change the definitions lightly.
    192 case $CONFIG in
    193     vbox*)
    194         OUTPUT_KERNEL=kernel-vbox
    195         OUTPUT_VMLINUX=vmlinux-vbox
    196         ;;
    197     goldfish)
    198         OUTPUT_KERNEL=kernel-qemu
    199         OUTPUT_VMLINUX=vmlinux-qemu
    200         ;;
    201     goldfish_armv7)
    202         OUTPUT_KERNEL=kernel-qemu-armv7
    203         OUTPUT_VMLINUX=vmlinux-qemu-armv7
    204         ;;
    205     *)
    206         OUTPUT_KERNEL=kernel-$CONFIG
    207         OUTPUT_VMLINUX=vmlinux-$CONFIG
    208 esac
    209 
    210 cp -f arch/$ARCH/boot/$ZIMAGE $OUTPUT/$OUTPUT_KERNEL
    211 cp -f vmlinux $OUTPUT/$OUTPUT_VMLINUX
    212 
    213 echo "Kernel $CONFIG prebuilt images ($OUTPUT_KERNEL and $OUTPUT_VMLINUX) copied to $OUTPUT successfully !"
    214 exit 0
    215