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