[Android] frap129:
Introduction

Since the only guide I could find for switching toolchains in Android is one on linaro from a few years ago in the Galaxy nexus forum, I decided to make one and post it here. Custom toolchains can provide an overall better android experience to the end user by increasing speed, buttery smoothness, and even fixing bugs! Note: They can also produce bugs on some devices/ROMs.

Preface:

There are two mainstream custom toolchains, SaberMod and Linaro, which provide different improvements. In short, SaberMod is built for max speed, while Linaro is built for some subtle speed increases with much more smoothness.

Click for ARM info

For ARM devices, there are also 2 different toolchains used in Android; arm-linux-androideabi, which compiles the ROM, and arm-linux-eabi, which compiles the kernel. The recommended version for androideabi is 4.9, but higher should be used once the bugs are worked out/if it works for your device.

Click for AARCH64 (ARM64) info

For 64 bit ARM devices, there is one main toolchain,; aarch64-linux, which compiles both the ROM and the kernel. The recommended version is 4.9, because as of now 5.0 won't work with the kernel without heavy revision.

Switching ARM Toolchains

Step 1:

Run the following commands from your source directory to download the 4.9 toolchain for ROM and 4.9 toolchain for kernel

If using Linaro:

git clone -b linaro-4.9-arm-linux-androideabi https://github.com/ArchiDroid/Toolchain prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-linaro-4.8

git clone -b linaro-4.9-arm-linux-eabi https://github.com/ArchiDroid/Toolchain prebuilts/gcc/linux-x86/arm/arm-linux-eabi-linaro-4.9

If using SaberMod: Download from http://sabermod.com

If Using UBER

git clone https://bitbucket.org/UBERTC/arm-linux-androideabi-4.9.git prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-UB-4.8

git clone https://bitbucket.org/UBERTC/arm-eabi-4.9.git prebuilts/gcc/linux-x86/arm/arm-linux-eabi-UB-4.9

Step 2 :

Here's where things get a little tricky. Step 2 is different for different android sources. If you use a rom that has GCC Freedom, use the first method. If not, open the file in your source directory in gedit /build/core/combo/TARGET_linux-arm.mk and ctrl+f to search for "TARGET_GCC_VERSION_AND" without the quotes. If it's found, use the first method. If not, use method 2.

Method 1: To actually use the toolchains, you'll need to change a few things in /build/core/combo/TARGET_linux-arm.mk. An example of what you need to change is shown below:

ifeq ($(strip $(TARGET_GCC_VERSION_AND)),)
TARGET_GCC_VERSION_AND := SM-4.9
else
TARGET_GCC_VERSION_AND := $(TARGET_GCC_VERSION_AND)
endif
ifeq ($(strip $(TARGET_GCC_VERSION_ARM)),)
TARGET_GCC_VERSION_ARM := SM-4.9
else
TARGET_GCC_VERSION_ARM := $(TARGET_GCC_VERSION_ARM)
endif
# Specify Target Custom GCC Chains to use:
TARGET_GCC_VERSION_AND := SM-4.8
TARGET_GCC_VERSION_ARM := SM-4.9

Notice how the target versions are each listed in two places?(AND is for androideabi and ARM is eabi) Just change the versions at the bottom where it says to specify the custom toolchain to the suffix of the toolchain you chose, for example linaro-4.8 and

SM-4.9 . Save and your done!

Method 2: Open /build/envsetup.sh in gedit or another text editor and look for the following section of code:

export ANDROID_EABI_TOOLCHAIN=
local ARCH=$(get_build_var TARGET_ARCH)
case $ARCH in
x86) toolchaindir=x86/i686-linux-android-$targetgccversion/bin
;;
arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
;;
mips) toolchaindir=mips/mipsel-linux-android-$targetgccversion/bin
;;
*)
echo "Can't find toolchain for unknown architecture: $ARCH"
toolchaindir=xxxxxxxxx
;;
esac
if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
export ANDROID_EABI_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
fi

unset ARM_EABI_TOOLCHAIN ARM_EABI_TOOLCHAIN_PATH
case $ARCH in
arm)
toolchaindir=arm/arm-eabi-$targetgccversion/bin
if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
export ARM_EABI_TOOLCHAIN="$gccprebuiltdir/$toolchaindir"
ARM_EABI_TOOLCHAIN_PATH=":$gccprebuiltdir/$toolchaindir"
fi
;;

Look at the line "arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin" and change $targetgccversion to the suffix of the androideabi toolchain you downloaded in step 1, such as linaro-4.8. Now look at the lines

unset ARM_EABI_TOOLCHAIN ARM_EABI_TOOLCHAIN_PATH
case $ARCH in
arm)
toolchaindir=arm/arm-eabi-$targetgccversion/bin

and change $targetgccversion to the suffix of the eabi toolchain you downloaded in step 1, such as linaro-4.9. Save your file and you did it!

Switching AARCH64 Toolchains

Both ROM and Kernel use the same toolchain for AARCH64, so we need to clone it first.

Step 1:

Run the following commands from your source directory to download the 4.8 toolchain for ROM and 4.9 toolchain for kernel

If using Linaro:

git clone https://android.git.linaro.org/git-ro/platform/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9-linaro.git prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-linaro-4.9

If using SaberMod 4.9:

git clone https://github.com/SaberMod/aarch64-linux-android-4.9.git prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-SM-4.9

Step 2:

To actually use the toolchain, you'll need to change a few things in /build/core/combo/TARGET_linux-arm64.mk. An example of what you need to change is shown below:

ifeq ($(strip $(TARGET_GCC_VERSION_EXP)),)
TARGET_GCC_VERSION := 4.9
else
TARGET_GCC_VERSION := $(TARGET_GCC_VERSION_EXP)
endif

See where TARGET_GCC_VERSION is defined as 4.9? change 4.9 to your toolchain version, SM-4.9 or linaro-4.9, to switch toolchains. Save the file and you're golden!

Congrats, you just upgraded to better toolchains! Make sure you run "make clobber && rm -rf ~/.ccache" before your next build.