This procedure works on macOS 10.12.2

Establishing build environment:
1. Creating a case-sensitive disk image

hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 100g ~/android.dmg

2. Mount the image

hdiutil attach ~/android.dmg.sparseimage -mountpoint /Volumes/android

3. Install JDK (for master branch of Android in AOSP, just install the latest version)
4. Install Xcode command line tools:

xcode-select --install

5. Install MacPorts
Make sure /opt/local/bin is before /usr/bin in path, otherwise

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

6. Get make, git, and GPG packages

POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg

7. Increase the cap of simultaneous file descriptors open

vim ~/.bash_profile
ulimit -S -n 1024

Download source code
1. Install Repo

mkdir ~/bin
export PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

2. Initialize Repo client

cd /Volumes/android
mkdir android_source
cd android_source
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
repo init -u https://android.googlesource.com/platform/manifest

3. Download Android source tree

repo sync

Prepare the build
1. Get proprietary binaries
2. Uncompress and copy the scripts to the root of Android source tree, then run them (e.g. for Pixel) to extract binaries into vendor foler (Note: when reading the Term, use Ctrl+F to page down, or press G to go to end)

./extract-google_devices-sailfish.sh
./extract-qcom-sailfish.sh

3. Clean up

make clobber

Note: if there is “Error: could not find jdk tools.jar”,

export ANDROID_JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

4. Set up environmnet

. build/envsetup.sh

5. Choose the target, e.g.

 lunch aosp_arm-eng

6. Build the code, for -jN, choose N between 1 and 2 times the number of hardware threads (e.g. for 4 core 8 threads, N is between 8 to 16), e.g.

 make -j12

7. Run on emulator (first create an AVD named “pixel”

 emulator -avd pixel

Reference:
https://source.android.com/source/requirements.html