[Android Software/Hacking General [Developers Only]] trevd:
Hi Folks

I wasn't sure where this should belong but as it is a bit of an Hack this forum is probably the most appropriate :D

Introduction

This short tutorial will show you how to patch the Android Build System to allow you to cross-compile Android AOSP host tools ( adb, fastboot etc ) for OSX using a linux based machine. This is something Google said was impossible or at the very least unsupported.

Assumptions

You have a linux based machine and working copy of the AOSP source tree.

You can/have successfully compile(d) a full Android Release from this tree.

A basic idea of how the Android Build System works is beneficial.

Getting Started

I've set-up a git repository which contains a binary copy of the OSX SDK 10.6 and the apple-darwin10-gcc cross compiler. So first things first. open a terminal and set the root of the AOSP sources tree to the current directory.

STAGE 1: Copy the OSX SDK

Step 1.

Clone the repo with the SDK and toolchain

git clone https://github.com/trevd/android_platform_build2.git build2

Step 2.

Create /Developer directory at your filesystem root, this is a known location for the SDKs

sudo mkdir /Developer
sudo chown $USER.$USER /Developer

Step 3.

Copy and unpack the SDK package

cp build2/osxsdks10.6.tar.gz /Developer
cd /Developer
tar -zxvf osxsdks10.6.tar.gz
rm osxsdks10.6.tar.gz
cd - # back to aosp root

STAGE 2 : Swapping the Toolchain

This is where the fun begins :laugh:

The Android Build system has the majority of the infrastructure in place already to build for OSX, the only problem is that you need OSX to build for OSX. However we can remedy that with a couple of dirty hacks :laugh:.

The prebuilts/gcc/darwin-x86 directory contains a toolchain compatible with osx ( mach-o binaries ). We are going to swap this for a linux compatible ( elf ) executables.

Step 4:

Copy and unpack the elf compatible darwin cross toolchain

cp build2/i686-apple-darwin-4.2.1.tar.gz prebuilts/gcc/linux-x86/host
cd prebuilts/gcc/linux-x86/host
tar -zxvf i686-apple-darwin-4.2.1.tar.gz
cd - # back to aosp root

Step 5:

Remove the mach-o binaries and symlink the elf binaries in it's place

cd prebuilts/gcc
rm -rf darwin-x86
ln -s linux-x86 darwin-x86
cd - # back to aosp root

Step 6:

We also need to replace the mach-o version of the ccache executable which live in the prebuilt/misc directory

cd prebuilts/misc
rm -rf darwin-x86
ln -s linux-x86 darwin-x86
cd - # back to aosp root

STAGE 3: Patching the build system .mk files

We need to patch a couple of files in the build directory namely the build/core/combo/HOST_darwin-x86.mk the main crux of this is swapping the ar tool for libtool so static libraries can be created without error.

patch -p1 < build2/build.patch

If the patch has been applied successfully you should see the following

patching file system/core/adb/Android.mk
patching file build/core/combo/HOST_darwin-x86.mk
patching file build/core/combo/select.mk
patching file build/core/envsetup.mk
patching file build/envsetup.sh

You are now ready to cross compile!! :good: ..... well not quite, but nearly.... here's why!

The Android Build System will attempt to build both the Target and Host files for most modules so I'd advise using a lunch option which already has a full target built for it or alternatively you can build the generic sdk using the following commands at the AOSP source tree root.

. build/envsetup.sh
lunch sdk-eng
make sdk

This will stop target dependency errors occurring when you build individual modules.

NOW we're ready to cross compile.

STAGE 4: Building Modules

At present module build is very much a piecemeal process. To build adb for example we need to build the dependencies first. This is not too onerous as most host modules have very few dependencies.

Building adb

adb has dependencies on the following libraries

external/zlib
external/openssl
system/core/liblog
system/core/libcutils
system/core/libzipfile

I've found the easiest way to compile the dependencies is to navigate to each directory in turn an use to "mm" build system command to compile the individual module. the commands I run to compile adb are as follows.

From AOSP Source Root

cd external/zlib
USE_DARWIN=true mm -j8
cd ../openssl
USE_DARWIN=true mm -j8
croot # go back to the AOSP root
cd system/core/liblog
USE_DARWIN=true mm -j8
cd ../libcutils
USE_DARWIN=true mm -j8
cd ../libzipfile/
USE_DARWIN=true mm -j8
cd ../adb
USE_DARWIN=true mm -j8

All being well you should now have and adb binary located at out/host/darwin-x86/bin/adb. running the file command on this binary should produce the following output

adb: Mach-O executable i386

Conclusion

Although this method is a little rough and ready, it should produce the desired results if you need to cross compile for OSX. The eventual goal would be to compile a full OSX Android SDK on linux in a similar manner to the way the windows-sdk is currently compiled. This requires more investigation as compiling the windows sdk on linux employs a little bit of trickery on the part of the build system.

Final Notes and FAQs:

Why can't I just type make <module> from the root?

Doing this triggers building of additional modules such as LLVM and clang which are to deployed out/host/darwin-x86/bin the build system then attempts to use binary later on. These are obviously built for the Mach-o architecture and as such are incompatible with the linux. This results in a build error which can and would be resolved by the above mentioned trickery ( see conclusion )