h1. Build a Native Developement Kit (NDK) with Replicant 4.2 h2. Principle of operation The [[ReplicantSourceCode|Android 4.2 source code]] ships a @ndk@ directory with NDK build scripts. These tools download various upstream imports from https://android.googlesource.com/toolchain/XXX/ with optional commit date (no branches or tags), apply patches, and build them. Note: as of 2015-07, the upcoming Android version moved to a different build system, and will populate $topdir/toolchain/ through @repo@ instead of invoking git in a temporary directory (i.e. ditched @download-toolchain-sources.sh@). h2. Ubuntu 10.04 build environment with LXC Replicant 4.2 is based on CyanogenMod 10.1 which is based on AOSP 4.2.2 (2013-02). The build environment used by Google should be Ubuntu LTS 12.04. We recreate it through LXC. If you know how to use Triskel 6 instead, please contribute the instructions.
lxc-create -n replicant -t download -- -d ubuntu -r precise -a amd64
# Note: or Triskel 6, if you know how to configure LXC/debootstrap for it
lxc-start -n replicant -d
lxc-attach -n replicant

# clean-up non-free sources
sed -i -e 's/ restricted//' -e 's/ multiverse//' /etc/apt/sources.list
apt-get update

# prepare build user
apt-get install openssh-server
useradd replicant --shell /bin/bash --create-home
mkdir -p -m 700 ~replicant/.ssh
cat <> ~replicant/.ssh/authorized_keys
your public key
EOF
chown -R replicant: ~replicant
h2. Build dependencies
# Base Android build dependencies (from https://source.android.com/source/initializing.html, section "Installing required packages (Ubuntu 12.04)")
# Dropping mingw to skip windows builds for now.
# Installing g++-multilib first otherwise apt-get complains.
apt-get install build-essential g++-multilib
apt-get install git gnupg flex bison gperf \
  zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev \
  libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 \
  libgl1-mesa-dev \
  python-markdown libxml2-utils xsltproc zlib1g-dev:i386

# Additional NDK build dependencies (completed from https://android.googlesource.com/platform/ndk/+/master/README.md)
apt-get install curl texinfo bison flex libtool pbzip2 groff

# Make 'bash' the default shell otherwise 'build-gabi++.sh' will fail
dpkg-reconfigure dash
h2. Preparing the sources Based on [[ReplicantSourceCode]] with additional configuration.
mkdir ~/tools/
cd ~/tools/
wget http://commondatastorage.googleapis.com/git-repo-downloads/repo
chmod a+x repo
cd ~

cat <> ~/.bashrc
export USE_CCACHE=1
EOF

# Avoid prompts
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git config --global color.ui true

mkdir replicant-4.2/
cd replicant-4.2/
../tools/repo init -u git://git.replicant.us/manifest.git -b replicant-4.2
../tools/repo sync -j4 
# > 1h, 27GB (inc. 17GB .repo)
h2. Determining the checkout date Since there's no NDK release tag for the AOSP @toolchain/@ checkouts, we need to find the right date. @cd ndk/ && git log@ says the last commit is 2012-10-05, so it's around NDK r8b (2012-07) and NDK r8c (2012-11). Checking @build/tools/toolchain-patches/@ in the NDKs shows r8c has matching patches. Also NDKs from google contain several @SOURCES@ files listing Git repos and commit IDs. With r8c's release date (@ls --full-time RELEASE.TXT@), @build/tools/download-toolchain-sources.sh@ checkouts the same Git commit IDs, and the patches apply. Only, r8c references a LLVM Git repo, while our scripts download a tarball. So our scripts match a NDK version between r8b and r8c, and can almost exactly reproduce r8c. Let's call it r8b2. h2. Building the SDK Based on @ndk/docs/DEVELOPMENT.html@, with much improvement :)
export NDK=~/replicant-4.2/ndk
cd $NDK

# Allow checking build errors
export NDK_LOGFILE=$HOME/ndk.log

# Follow Python download redirection
sed -i -e 's/curl -S/curl -L -S/' build/tools/download-toolchain-sources.sh

# Get the sources from toolchain/ repos, with r8c release date:
bash $NDK/build/tools/download-toolchain-sources.sh --git-date='2012-10-22T07:59:40+0200' ~/ndk-dl
# ndk-dl: 2.5GB

# Define NDK_TMPDIR and and use --incremental so the build can be resumed on error
export NDK_TMPDIR=~/ndk-tmp
mkdir -p $NDK_TMPDIR/release-r8b2/

# Build the release
bash $NDK/build/tools/make-release.sh --toolchain-src-dir=$HOME/ndk-dl --release=r8b2 --incremental
# 30mn with 4 cores, 28GB (inc. 17GB .repo), 2.5GB ndk-dl, 1GB /tmp
The GNU/Linux NDK release is in @/tmp/ndk-replicant/release/android-ndk-r8b2-linux-x86.tar.bz2@ :)