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