8. README.android


      
How to cross-compile for Android.  These notes were last updated on 
27 Sept 2011, for Valgrind SVN revision 12060/2209.

This is known to work at least for Android 2.3.4 running on a (rooted,
AOSP build) Nexus S.  It has also worked in the past on a
un-messed-with Motorola Xoom, although I haven't tested it recently.
Other configurations and toolchains might work, but haven't been
tested.  Feedback is welcome.

You need the android-ndk-r6 native development kit.  Install it
somewhere.  Doesn't matter where.  Then do this:


# Modify this (obviously).  Note, this "export" command is only done
# so as to reduce the amount of typing required.  None of the commands
# below read it as part of their operation.
#
export NDKROOT=/path/to/android-ndk-r6


# Modify this too.  Tell the build system which Android hardware you
# are building for.  It needs to know this so it can compile in
# support for the right Android-hw-specific ioctls.  (sigh.)  As with
# NDKROOT above, this is merely to avoid repeated typing; none of the
# commands read it.
#
# Currently the supported values are:  nexus_s pandaboard
# So choose one of the below:
#
export HWKIND=nexus_s         # Samsung Nexus S
export HWKIND=pandaboard      # Pandaboard running Linaro Android

# Then cd to the root of your Valgrind source tree.
#
cd /path/to/valgrind/source/tree


# After this point, you don't need to modify anything; just copy and
# paste the commands below.


# Set up toolchain paths.
#
export AR=$NDKROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-ar
export LD=$NDKROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-ld
export CC=$NDKROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc


# Do configuration stuff.  Don't mess with the --prefix in the
# configure command below, even if you think it's wrong.
# You may need to set the --with-tmpdir path to something
# different if /sdcard doesn't work on the device -- this is
# a known cause of difficulties.

./autogen.sh

CPPFLAGS="--sysroot=$NDKROOT/platforms/android-3/arch-arm -DANDROID_HARDWARE_$HWKIND" \
   CFLAGS="--sysroot=$NDKROOT/platforms/android-3/arch-arm" \
   ./configure --prefix=/data/local/Inst \
   --host=armv7-unknown-linux --target=armv7-unknown-linux \
   --with-tmpdir=/sdcard


# At the end of the configure run, a few lines of details
# are printed.  Make sure that you see these two lines:
#
#          Platform variant: android
#     Primary -DVGPV string: -DVGPV_arm_linux_android=1
#
# If you see anything else at this point, something is wrong, and
# either the build will fail, or will succeed but you'll get something
# which won't work.


# Build, and park the install tree in `pwd`/Inst
#
make -j2
make -j2 install DESTDIR=`pwd`/Inst


# To get the install tree onto the device:
# (I don't know why it's not "adb push Inst /data/local", but this
# formulation does appear to put the result in /data/local/Inst.)
#
adb push Inst /

# To run (on the device)
/data/local/Inst/bin/valgrind [the usual args etc]


# Once you're up and running, a handy modify-V-rebuild-reinstall
# command line (on the host, of course) is
#
mq -j2 && mq -j2 install DESTDIR=`pwd`/Inst && adb push Inst /
#
# where 'mq' is an alias for 'make --quiet'.


# One common cause of runs failing at startup is the inability of
# Valgrind to find a suitable temporary directory.  On the device,
# there doesn't seem to be any one location which we always have
# permission to write to.  The instructions above use /sdcard.  If
# that doesn't work for you, and you're Valgrinding one specific
# application which is already installed, you could try using its
# temporary directory, in /data/data, for example
# /data/data/org.mozilla.firefox_beta.
#
# Using /system/bin/logcat on the device is helpful for diagnosing
# these kinds of problems.