Compile Xperia Z/Tablet Z CM10.2 ROM with full Linaro 4.8 including O3 strict-aliasing optimizations

It took me a while as things keep changing back and forth with CM10.2. Since CM10.2 has tons of bionic optimizations, I decided to leave cortex string optimizations for time being and added latest 4.8 toolchains for arm/linux and its own NDK. TWRP and jeroenqui’s kernel are included as usual.

Features
1. Replace stock kernel and recovery with @jeroenqui‘s MEOW-kernel and TWRP recovery. (Optional)
2. Replace Google toolchains for arm/linux with Linaro 4.8 ones from Mustaavalkosta’s github
3. Apply build configuration with Linaro suggested optimizations “-O3 -pipe -Wl,–hash-style=gnu -fno-tree-vectorize -fstrict-aliasing -Wstrict-aliasing=2 -Werror=strict-aliasing”
4. Fix codes breaking strict aliasing rules
5. Comes in form of automate shell script, the easiest Linaro build guide EVER

Requirements:
1. CM10.2 build environment – You can check on Build for yuga wiki for detailed instructions.
2. Oracle Java – Skip OpenJDK in wiki and install better ones from this link including adb and fastboot.
3. You need to install additional packages for compiling kernel. Type “sudo apt-get install lzop ccache” so that build kernel won’t break mid-way.
4. Make sure your default compiled ROM works without trouble as I got boot problems before and thought Linaro messed it.

Instructions:

1. Open terminal and go to your Android git repository.

2. Modify local manifests for jeroenqui’s kernel and TWRP by typing “gedit .repo/local_manifests/roomservice.xml” and hit enter. Added the following lines before ‘</manifest>’.

Code:
  <project name="Mustaavalkosta/android_prebuilts_gcc_linux-x86_arm_arm-eabi-4.8.git" path="prebuilts/gcc/linux-x86/arm/arm-eabi-4.8" remote="github" revision="master"/>
  <project name="Mustaavalkosta/android_prebuilts_gcc_linux-x86_arm_arm-linux-androideabi-4.8.git" path="prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.8" remote="github" revision="master"/>
#Add this if you want to use MEOW-kernel (You can use others if you want at your own risk)
  <remove-project name="CyanogenMod/android_kernel_sony_apq8064" />
  <project name="MEOW-kernel/android_kernel_sony_apq8064" path="kernel/sony/apq8064" remote="github" />
#Add this if you want to use TWRP
  <remove-project name="CyanogenMod/android_bootable_recovery" />
  <project name="TeamWin/Team-Win-Recovery-Project.git" path="bootable/recovery" remote="github" revision="twrp2.7"/>

3. Download Linaro NDK package in tar.gz format and place in Android git repository

3. Make linaro patch script by typing “gedit linaro_patch.sh” and hit enter. Put contents inside with this ones and save then exit.

Code:
#!/bin/bash

#Windows X's CM10.2 Linaro patch for Xperia Z

DIR=$(pwd)

#Linaro NDK files extraction (change 'android-ndk-r9-linaro-linux-x86_64.tar.bz2' and 'android-ndk-r9-linaro' to match what you place before)
cd $DIR
tar -vxf android-ndk-r9-linaro-linux-x86_64.tar.bz2
rm android-ndk-r9-linaro-linux-x86_64.tar.bz2
cp -f ndk/Android.mk android-ndk-r9-linaro/
cp -f ndk/CleanSpec.mk android-ndk-r9-linaro/
rm -r -f ndk
mv android-ndk-r9-linaro ndk

cd $DIR/build
git fetch https://github.com/WindowsX/android_build.git cm-10.2 && git cherry-pick ec9e3eea9cb1dc1929e45736f0608a888f97eb0f
git fetch https://github.com/WindowsX/android_build.git cm-10.2 && git cherry-pick 43ee5fdfaa404e474372072a8bd85ea613646861
git fetch https://github.com/Mustaavalkosta/android_build.git && git cherry-pick ffcaf82831783a420a5490e9894c016644e79743

cd $DIR/kernel/sony/apq8064
git fetch https://github.com/WindowsX/android_kernel_sony_apq8064.git cm-10.2 && git cherry-pick 71a48e15f0066e72b401d92afca7dd8b45efac1b
git fetch https://github.com/WindowsX/android_kernel_sony_apq8064.git cm-10.2 && git cherry-pick e748106f0ffcad5376e76b336dea287c493977dc
git fetch https://github.com/WindowsX/android_kernel_sony_apq8064.git cm-10.2 && git cherry-pick 7beb0a0de015a204745a46d03d7a1ca36775c5e8
git fetch https://github.com/WindowsX/android_kernel_sony_apq8064.git cm-10.2 && git cherry-pick bf46e6bc58141778af27bd49f0093a52df141aba
#Uncomment the following commit if you use original CM kernel
#git fetch https://github.com/WindowsX/android_kernel_sony_apq8064.git cm-10.2 && git cherry-pick 3c8432deaaf673b3dfd0673d8000a3d068001926

#TWRP Recovery Linaro fix (Comment these out if you stick with default CWM Recovery)
cd $DIR/bootable/recovery/
git fetch https://github.com/WindowsX/Team-Win-Recovery-Project.git && git cherry-pick fc866246fe05d743ac5917ee44dbdffe1f620714
git fetch https://github.com/WindowsX/Team-Win-Recovery-Project.git && git cherry-pick 4e0cc5ba0b883413834a81d32d9501a802022053
cd $DIR/device/sony/fusion3-common
git fetch https://github.com/jeroenqui/android_device_sony_fusion3-common.git && git cherry-pick fab74e8c2faa8e2afc405b44c7233d7e32ba96dd

cd $DIR/bionic
git fetch https://github.com/WindowsX/android_bionic.git cm-10.2 && git cherry-pick f607f6c8129eb41c602451b6bfe39be6d734d799

cd $DIR/device/generic/goldfish
git fetch https://github.com/WindowsX/android_device_generic_goldfish.git cm-10.2 && git cherry-pick f21fa86fcea62580a7d1ea3e32df168628517a82

cd $DIR/external/bash
git fetch https://github.com/SaberMod/android_external_bash.git && git cherry-pick d746365ea911c73ede17ed65933237875e29d9c3

cd $DIR/external/clang
git fetch https://github.com/WindowsX/android_external_clang.git && git cherry-pick 81d67da93bd3afa9ce7f5064562ddb0c91ba0a0c

cd $DIR/external/dnsmasq/
git fetch https://github.com/trailblazerz/android_external_dnsmasq.git && git cherry-pick 446fceadb803f8c28de809907d55dfab82e9827f

cd $DIR/external/e2fsprogs/
git fetch https://github.com/trailblazerz/android_external_e2fsprogs.git && git cherry-pick 0e540a3cf8c4dd22205ef39a6f9d6edfa0d1183a

cd $DIR/external/openssh/
git fetch https://github.com/WindowsX/android_external_openssh.git cm-10.2 && git cherry-pick bddb1dd7b9d594913061cdb6b2cd8bf77d047186

cd $DIR/external/openssl/
git fetch https://github.com/trailblazerz/android_external_openssl.git && git cherry-pick b43b3c2935e8f27b74c53be2f67fe49f3a515228

cd $DIR/external/ping/
git fetch https://github.com/WindowsX/android_external_ping.git cm-10.2 && git cherry-pick 46fb65c7a1c4b8b95b7b4b78fa10a1d4139f8339

cd $DIR/external/ping6/
git fetch https://github.com/WindowsX/android_external_ping6.git cm-10.2 && git cherry-pick 5f093083b78433b6056a532b39dc7e3d83330690

cd $DIR/external/skia
git fetch https://github.com/WindowsX/android_external_skia.git cm-10.2 && git cherry-pick 8000ce63239c673aacc905aeb48e357d07aca533

cd $DIR/external/stlport/
git fetch https://github.com/trailblazerz/android_external_stlport.git && git cherry-pick 05c206e22eddf9d09b3c497721a94aa23b922811

cd $DIR/frameworks/av/
git fetch https://github.com/WindowsX/android_frameworks_av.git cm-10.2 && git cherry-pick 8e10843ad4902943e50a7f4d23194319a78668ef
git fetch https://github.com/WindowsX/android_frameworks_av.git cm-10.2 && git cherry-pick 5735647d6c70660d20cf16ee5346159c345ad648
git fetch https://github.com/WindowsX/android_frameworks_av.git cm-10.2 && git cherry-pick d67b1fa60b2680e731a6683968856ef91362b1b3
git fetch https://github.com/WindowsX/android_frameworks_av.git cm-10.2 && git cherry-pick b5973bd61fa55ec7693103036fd618234e1ae4f7
git fetch https://github.com/WindowsX/android_frameworks_av.git cm-10.2 && git cherry-pick 2b7cd9fa973e829cc8a4842376280d01b3e9a487
git fetch https://github.com/WindowsX/android_frameworks_av.git cm-10.2 && git cherry-pick f1a15bce88881376c6f88546b9e698cdcd19732b

cd $DIR/frameworks/base/
git fetch https://github.com/WindowsX/android_frameworks_base.git cm-10.2 && git cherry-pick 937e6f24e503ca9fa203044a584b158edcc7840f

cd $DIR/frameworks/ex/
git fetch https://github.com/trailblazerz/android_frameworks_ex.git && git cherry-pick aa5da9c8956f09072bb9e783dee2a67798663bb6

cd $DIR/frameworks/native/
git fetch https://github.com/WindowsX/android_frameworks_native.git cm-10.2 && git cherry-pick 8b555aaaceee3ff422d239596c2f132329e80d7d

cd $DIR/frameworks/opt/net/voip
git fetch https://github.com/WindowsX/android_frameworks_opt_net_voip.git cm-10.2 && git cherry-pick 33f09279eaf8577753fc855611290acd29852b94

cd $DIR/frameworks/wilhelm/
git fetch https://github.com/trailblazerz/android_frameworks_wilhelm.git && git cherry-pick 07251f657bb21080ab96093dc5275b37589bbf37
git fetch https://github.com/trailblazerz/android_frameworks_wilhelm.git && git cherry-pick 48cc6405503668dc38b4713a98e08d1174b2340c

cd $DIR/hardware/qcom/media-caf
git fetch https://github.com/WindowsX/android_hardware_qcom_media-caf.git && git cherry-pick 4d64d4a65f4c04bce4ef3e373681337b967d4fb9

cd $DIR/external/chromium
git fetch https://github.com/WindowsX/android_external_chromium.git cm-10.2 && git cherry-pick 9631db924123e44ae0170aa4c52c122928a26b3b

cd $DIR/libcore
git fetch https://github.com/trailblazerz/android_libcore.git && git cherry-pick 40617f103bf38e61c43edee4c4ecbc2ea4aaedea

cd $DIR/external/webkit/
git fetch https://github.com/trailblazerz/android_external_webkit.git && git cherry-pick bb873522bc0ed7a4aaa3552527872ece198db104

cd $DIR/external/lsof/
git fetch https://github.com/trailblazerz/android_external_lsof.git && git cherry-pick 8a0665c786f53019acbcd28dfed8a5781bc3471e

cd $DIR/external/openvpn/
git fetch https://github.com/trailblazerz/android_external_openvpn.git && git cherry-pick b2cec9e8bd24ce3340fd23473b14258d063d580d

cd $DIR/system/qcom
git fetch https://github.com/WindowsX/android_system_qcom.git cm-10.2 && git cherry-pick ea84d6aab1b5011e6ae4f9ad938733c8af3d5c5e

This will replace Google NDK with Linaro ones and patch build configuration and codes that break strict aliasing rules.

4. Make Linaro patch executable by typing “chmod a+x linaro_patch.sh” and enter.

5. Re-sync again by typing “repo sync” and enter. It will take a while for downloading new toolchains.

6. Apply Linaro patches by typing “./linaro_patch.sh” and enter. All should goes well but keep your eyes on each patch and see if there’s anything failed to cherry-pick or not. Fix those failed to cherry-pick yourself and move on.

7. Reload build configuration by typing “source build/envsetup.sh” and enter.

8. Your ROM is ready be cooked with Linaro now. Build it with whatever method you prefer and enjoy.

Credits
trailblazerz11 – For his Linaro ROM for Nexus 4 and resourceful guide making Linaro Yuga implementations a lot easier.
DooMLorD – for his awesome DooMKernel tweaks. My Xperia Z won’t be so lovely as today without his tweaks, seriously.
jeroenqui – for his helpful effort to port DooMKernel tweaks into CM10.1.
TeamWin – for awesome recovery system.
Mustaavalkosta – For his 4.8 toolchains and commits

And don’t forget to press thanks if you find this tutorial helpful