Compile Xperia Z ROM with Linaro including O3 strict-aliasing optimizations

First of all, many thanks to @trailblazerz11 for bringing full Linaro ROM to our fellow S4 Pro devices like Nexus 4 with resourceful build guide. This guide is based on his repository with my own fixes that needed to be done outside his repository. This works fine on CM10.1 and should also work on AOSP as well with manual fixes applied. As current AOSP being incompetent, I’ll leave that until it actually works without relying on CM10.1 patches.

UPDATE #1: CM10.1 has changed build configuration to use qcom_media-caf instead so I fixed instructions and change stuff for default CM10.1 environment.

UPDATE #2: I added Linaro cortex string handling routines optimizations in bionic for armv7 neon architecture so Snapdragon S4 Pro can be even smoother with this.

Features
1. Replace stock kernel and recovery with @DooMLoRD‘s DooMKernel tweaks applied kernel by @jeroenqui and TWRP recovery. (Optional)
2. Replace Google Toolchain with Linaro Toolchain 4.8 15.03
3. Apply build configuration with Linaro suggested optimizations “-O3 -fmodulo-sched -fmodulo-sched-allow-regmoves -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.1 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 Yuga 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. (Optional) Modify local manifests for jeroenqui’s kernel and TWRP by typing “gedit .repo/local_manifests/roomservice.xml” and hit enter. Replace contents inside with this ones and save then exit.

<?xml version=”1.0″ encoding=”UTF-8″?>
<manifest>
  <project name="CyanogenMod/android_device_sony_yuga" path="device/sony/yuga" remote="github" />
  <project name="CyanogenMod/android_device_sony_common" path="device/sony/common" remote="github" />
  <project name="CyanogenMod/android_device_sony_qcom-common" path="device/sony/qcom-common" remote="github" />
  <project name="CyanogenMod/android_device_sony_fusion3-common" path="device/sony/fusion3-common" remote="github" />
  <project name="CyanogenMod/android_packages_apps_FMRadio" path="packages/apps/FmRadio" remote="github" />
  <project name="CyanogenMod/android_hardware_sony_DASH" path="hardware/sony/DASH" remote="github" />

  <project name="TheMuppets/proprietary_vendor_sony" path="vendor/sony" remote="github" />
  <project name="jeroenqui/android_kernel_sony_apq8064" path="kernel/sony/apq8064" remote="github" />
  <remove-project name="CyanogenMod/android_bootable_recovery" />
  <project name="TeamWin/Team-Win-Recovery-Project.git" path="bootable/recovery" remote="github" revision="twrp2.5"/>
</manifest>

You can skip this step and stick with default CM10.1 stuff without adjusting linaro patch later yourself to fix build problems.

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

#!/bin/bash

#Windows X Linaro patch for Xperia Z

DIR=$(pwd)

#Replace Google's Toolchain with Linaro's
cd $DIR/prebuilts/gcc/linux-x86/arm
wget http://releases.linaro.org/13.05/components/android/toolchain/4.8/android-toolchain-eabi-4.8-2013.05-x86.tar.bz2
tar -vxf android-toolchain-eabi-4.8-2013.05-x86.tar.bz2
rm android-toolchain-eabi-4.8-2013.05-x86.tar.bz2
mv arm-eabi-4.6 arm-eabi-4.6-google
mv android-toolchain-eabi arm-eabi-4.6

#Linaro -O3 optimizations for ROM building
cd $DIR/build
git fetch https://github.com/WindowsX/android_build.git && git cherry-pick 57074af3e68782745b3e70d3352d47de95f51bba
git fetch https://github.com/WindowsX/android_build.git && git cherry-pick 3d332aebe9bc97d453dfbb6c95ddacd8fadd46ba

#Linaro fix with -O3 optimizations (Comment these out if you're using jeroenqui's DooMKernel tweaks applied)
cd $DIR/kernel/sony/apq8064
git fetch https://github.com/WindowsX/android_kernel_sony_apq8064.git && git cherry-pick dcf8daa27d4321c53ac910ebd7889fe386d4beb6
git fetch https://github.com/WindowsX/android_kernel_sony_apq8064.git && git cherry-pick 892a2f12e9a8d82fe82740b647f924fac8080af4

#Linaro cortex string handling routines optimizations
cd $DIR/bionic/
git fetch https://github.com/CarbonDev/android_bionic.git && git cherry-pick a1c824da0d34fbcc71fe8dd95ff618f4bba3d753
git fetch https://github.com/CarbonDev/android_bionic.git && git cherry-pick a046eb1144eb947f69ad65bcc1a97498d8075741
git fetch https://github.com/CarbonDev/android_bionic.git && git cherry-pick b66fa68efbf907b8f9e6fcafa31e877cc3e1c565

#TWRP Recovery Linaro fix (Uncomment these if you pick this instead of 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/external/clang
git fetch https://github.com/WindowsX/android_external_clang.git && git cherry-pick 81d67da93bd3afa9ce7f5064562ddb0c91ba0a0c

cd $DIR/dalvik/
git fetch https://github.com/WindowsX/android_dalvik.git && git cherry-pick 1c5822ece7f43f13e38ca7d58bc0d5b7f9c79f0f

cd $DIR/frameworks/compile/mclinker
git fetch https://github.com/WindowsX/android_frameworks_compile_mclinker.git && git cherry-pick 1b1004f75cd7493e28f86690ff647cb36b130d64

cd $DIR/external/mesa3d
git fetch https://github.com/WindowsX/android_external_mesa3d.git && git cherry-pick 08ea469ce99454bbf177b4257a74af392fe23ba6

cd $DIR/external/linux-tools-perf
git fetch https://github.com/WindowsX/android_external_linux-tools-perf.git && git cherry-pick 0eb6e3237292092d4fcc05ecd5bd16425110fa3e

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

#Trailblazerz Linaro patches

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

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

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

cd $DIR/frameworks/base/
git fetch https://github.com/trailblazerz/android_frameworks_base.git && git cherry-pick 3c5ec8d73f8c8dd2b2b84d999c8ad4911566c079

cd $DIR/external/skia/
git fetch https://github.com/trailblazerz/android_external_skia.git && git cherry-pick d496d1bb506b925513d8f5d6e4d8a6c0a67cf7d8

cd $DIR/frameworks/av/
git fetch https://github.com/trailblazerz/android_frameworks_av.git && git cherry-pick 634477166074d503cc8df571315b3b5c4bf4a08d

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

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/frameworks/rs/
git fetch https://github.com/trailblazerz/android_frameworks_rs.git && git cherry-pick f276ba5f5435d6406ef07030e4b1cf613699c62e

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

cd $DIR/external/openssh/
git fetch https://github.com/trailblazerz/android_external_openssh.git && git cherry-pick d6a26a88f1f4a39d107c6b6e0296545b9852a50d

cd $DIR/frameworks/native/
git fetch https://github.com/trailblazerz/android_frameworks_native.git && git cherry-pick d75d8d1570bebf8f5de01754fa24e51ba3695999

cd $DIR/hardware/qcom/display-caf/
git fetch https://github.com/trailblazerz/android_hardware_qcom_display-caf.git && git cherry-pick 54b089f6a8d4313edca634c51ebdbe178f8b90e3
git fetch https://github.com/trailblazerz/android_hardware_qcom_display-caf.git && git cherry-pick d0a861554a0f6d13d3ac6a2aa6b81533f9c3b62d
git fetch https://github.com/trailblazerz/android_hardware_qcom_display-caf.git && git cherry-pick a9df638dda54472bb7f841b051f2d299316f6b51

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

This will replace Google Toolchain 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.

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.
CarbonDev – for Linaro cortex string handling routines optimizations commit that works with current source

And don’t forget to share this if you find this tutorial helpful :)