[Android Software/Hacking General [Developers Only]] kingbri:
Many people have MacBooks and they MacBooks have a terminal. This terminal runs bash and other unix commands, so why can't we compile a kernel. That's what I thought at first... Then the errors started...

I will not be responsible if you bork, brick, or blow up your devices in the process​

Notes: This is using clang, gcc should also work using this procedure, just with a few alterations exactly like compiling on linux

This guide is intended to help people compile their kernel on OSX

Step 1: Getting the dependencies and setting up build environment

Installing package managers:

Xcode HAS to be installed for this to work! If you just want the command line tools, type "xcode-select --install". To set the OS to use the command line tools from the app, type "xcode-select --switch /Applications/Xcode.app/Contents/Developer"

Install homebrew here: http://brew.sh

Install macports here (Follow source instructions): http://https://guide.macports.org/chunked/installing.macports.html

Set up build environment:

The android developers website gives great instructions on how to do this, but they use macports, which I tend to stay away from. However, this guide will use macports until I can figure out the homebrew alternatives for these packages

MacPorts packages: gmake libsdl git gnupg bison (exclude the git packages if you already have them)

Reference site: https://source.android.com/setup/build/initializing

Case Sensitive disk image - Android anything won't build unless the filesystem is case sensitive (APFS isn't), so we need to create a sparseimage for it

Go into disk utility and hit file, new image, blank image or Command-N

Set (in this order) Image Format: sparse disk image, Format: Mac OS Extended (Journaled, case-sensitive), Size: 20GB, Name: Android

Create the new image

Edit your bash profile by entering "open -a TextEdit ~/.bash_profile" and make a mountAndroid function

mountandroid() { hdiutil attach ~/android.sparseimage -mountpoint /Volumes/android; }

Open a new terminal window and type mountandroid

Your new disk image is mounted at /Volumes/android

Setting the PATH:

Open your bash_profile

add this line:

export PATH="/usr/local/bin:$PATH"

export PATH="/opt/local/bin:$PATH"

Save and open a new window

Step 2 - Cloning the kernel: I don't know where you get your kernels from, but you should know the basics on how to clone a source, clone the source to /Volumes/android

Step 3 - Getting the Toolchain: For now, I use prebuilts via the NDK, However, Crosstool-ng is a viable option

Download the NDK here https://developer.android.com/ndk/downloads/

Make a standalone Toolchain inside your case sensitive image (For arm64 devices: use the guide for BOTH arm and arm64)

https://developer.android.com/ndk/guides/standalone_toolchain

Step 4 - Linking it all together:

Creating a script to use (inside kernel source folder)

make O=out ARCH=arm64 **Your_defconfig**

make -j4 O=out ARCH=arm64 CC='/Volumes/android/**Toolchain folder**/bin/clang' CLANG_TRIPLE=aarch64-linux-gnu CROSS_COMPILE_ARM32='/Volumes/android/**Toolchain arm folder**/bin/arm-linux-androideabi-' CROSS_COMPILE='/Volumes/android/**Toolchain arm folder**/bin/aarch64-linux-android-'

The arm64 devices have to use the CROSS_COMPILE_ARM32 line, if you don't have this, just omit the line

Then just type ./**Script Name**.sh (make sure to chmod +x or 755 it first) and you're good to go!

Step 5 - Errors... Those stupid errors!: There are a few known errors that can be easily fixed, but they're very annoying

elf.h error - Open the file /usr/local/include and make a new file, elf.h

Paste this in there:

#include "../opt/libelf/include/libelf/gelf.h"

#define R_386_NONE 0

#define R_386_32 1

#define R_386_PC32 2

#define R_ARM_NONE 0

#define R_ARM_PC24 1

#define R_ARM_ABS32 2

#define R_MIPS_NONE 0

#define R_MIPS_16 1

#define R_MIPS_32 2

#define R_MIPS_REL32 3

#define R_MIPS_26 4

#define R_MIPS_HI16 5

#define R_MIPS_LO16 6

#define R_IA64_IMM64 0x23 /* symbol + addend, mov imm64 */

#define R_PPC_ADDR32 1 /* 32bit absolute address */

#define R_PPC64_ADDR64 38 /* doubleword64 S + A */

#define R_SH_DIR32 1

#define R_SPARC_64 32 /* Direct 64 bit */

#define R_X86_64_64 1 /* Direct 64 bit */

#define R_390_32 4 /* Direct 32 bit. */

#define R_390_64 22 /* Direct 64 bit. */

#define R_MIPS_64 18

#define EF_ARM_EABIMASK 0XFF000000

#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK)

vdsomunge.c errors (all fixed with this simple file from torvalds himself)

Replace your arch/arm/vdso/vdsomunge.c file with this one

vdso_offset_sigtramp undeclared (shows signal.c in terminal)

change the file arch/arm64/vdso/gen_vdso_offsets.sh

Replace the last line to this - 's/^\([0-9a-fA-F]*\) . VDSO_\([a-zA-Z0-9_]*\)$/\#define vdso_offset_\2 0x\1/p'

This removes the "t" in the "2t0x" part

So, after this, your kernel should be built without any complications! I have some requests in the post below, so help me out and gimme a thanks!