Cross-compiling bash for Android ARM

Most Android mobiles are running on the ARM architecture. Therefore you have to use a special compiler for such binaries. The Android SDK built in adb shell has no auto completion, which is really a nightmare in my opinion. Therefore I was looking for a way to compile bash for Android. Altough a lot of tutorials tell you to download the CodeSourcery cross-compiling toolchain, they are not really necessary (at least if you do a static compile like I do here).

I wrote a script that compiles bash-4.0. Should work out-of-the-box in Ubuntu 11.04.

#!/bin/bash
#BASH source code from http://ftp.gnu.org/gnu/bash/ 
#Example for compiling bash on Ubuntu 11.04
#Warnings during the compilation process seem to be alright, errors would be bad
BASH_VERSION="bash-4.1"

echo "[INFO] Checking if packages installed"
dpkg --status autoconf | grep -q not-installed
if [ $? -eq 0 ]; then
    echo "[INFO] Apt-get installing autoconf, please provide sudo password"
    sudo apt-get install autoconf
else
    echo "[INFO] autoconf already installed, good"
fi
dpkg --status gcc-arm-linux-gnueabi | grep -q not-installed
if [ $? -eq 0 ]; then
    echo "[INFO] Apt-get installing gcc-arm-linux-gnueabi, please provide sudo password"
    sudo apt-get install gcc-arm-linux-gnueabi
else
    echo "[INFO] gcc-arm-linux-gnueabi already installed, good"
fi
echo "[INFO] Starting bash source code download"
wget http://ftp.gnu.org/gnu/bash/$BASH_VERSION.tar.gz
tar xvfz $BASH_VERSION.tar.gz
cd $BASH_VERSION
CC=`which arm-linux-gnueabi-gcc`
./configure --host=arm-linux-gnueabi --enable-static-link --without-bash-malloc
make clean
make
file bash | grep -q ARM
if [ ! $? -eq 0 ]; then
    echo "[ERROR] Looks like bash was incorrectly compiled with another compler than arm-linux-gnueabi-gcc"
    echo "[ERROR] The resulting bash binary will not run on ARM, therefore aborting!"
    exit
fi
arm-linux-gnueabi-strip -o bash-stripped -s bash
cp ./bash-stripped ../bash
cd ..
file bash
echo "[INFO] Your bash binary is finished (file 'bash' in current directory), happy autocompleting on ARM!"

By changing the variable BASH_VERSION to bash-4.1 you should be able to compile an even newer version. Bash-4.2 did not work for me.

7 thoughts on “Cross-compiling bash for Android ARM

  1. this code didn’t work well for me, it say that some packages are installed but the terminal shows that they’re not

  2. Very nice, I need to try that if I have some time! I recommend zsh (because of the MUCH better auto-completion) with oh-my-zsh (for plugins and themes 😀 ). Have to take a look at it if I can get it working.

  3. I had to install autoconf, gcc-arm-linux-gnueabi you could probably rewrite the script to install the dependencies at run time which would make it easier for alot of people.

  4. Hi, I have change this line in your script for it to run on my Ubuntu :
    dpkg –status autoconf | grep -q not-installed
    to :
    dpkg –status autoconf 2>&1 | grep -q not.installed
    I did the same for the second test (testing the existence of the “gcc-arm-linux-gnueabi” package)

    I like your script, it is VERY usefull, where can we find or download the latest version of this script ?

  5. Thanks. Actually I did not put this script on github yet. So this is the latest version. Feel free to put it on github, just put a link to this blog post in the README please.

Leave a Reply

Your email address will not be published. Required fields are marked *

*