How to build AOSP on M1

apple-m1.png

Before we start this job, let’s check how my laptop looks. Here are some factors of my M1 MacBook Pro 2020

  • OS version : macOS Monterey 12.1 Beta (21C5021h)
  • Chipset: Apple M1
  • Memory size: 16 GB

First of all, you will need to install the XCode command-line tools.

1
xcode-select --install

And, you should also install Rosetta for code-building.

1
/usr/sbin/softwareupdate --install-rosetta

When I tried to install Rosetta, I found some system errors, and I can’t solve that, but it seems that doesn’t matter. You can still build the AOSP project.

Now, we should prepare a case-sensitive disk image cause the AOSP should build on the Ubuntu system, but MacOS is not a case-sensitive file system ref.

1
hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 350g ~/aosp_source.dmg

After this, let’s create a ZSH environment. You might first time hear about ZSH. Here are some helpful tools for empowering your ZSH env, such as Oh My Zsh, Powerlevel10k, etc. I highly recommend that you try PowerLevel10k, a successor of the powerlevel9k, and it’s a perfect theme for ZSH env.

When you finish up installing/setting the ZSH, we need to create and edit the .zshenv with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# set the number of open files to be 2048
ulimit -S -n 2048

# Compiler cache
export USE_CCACHE=1

# Mount the Android Tree
function mountAOSP { hdiutil attach ~/aosp_source.dmg.sparseimage -mountpoint /Volumes/aosp_source; }

#Unmount the Android Tree
function umountAOSP() { hdiutil detach /Volumes/aosp_source; }

export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export PATH=~/.bin:$PATH

Let’s use this env setting and start downloading the AOSP code. We are going to use android-11.0.0_r48 for the building.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ source ~/.zshenv
$ mkdir ~/.bin
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/.bin/repo
$ chmod a+x ~/.bin/repo
$ mountAOSP
$ cd /Volumes/aosp_source
$ mkdir aospsrc
$ cd aospsrc

// setup your gitconfig
$ git config --global user.name "Your Name"
$ git config --global user.email "you@example.com"
$ repo init -u https://android.googlesource.com/platform/manifest -b android-11.0.0_r48
$ repo sync

Here are some commands to build AOSP:

1
2
3
$ source build/envsetup.sh 
$ lunch aosp_arm-eng
$ make -j24

Then, you can start enjoying the AOSP journey now.

[Reference]

  1. Code Search for AOSP
  2. Build AOSP
  3. Download AOSP
  4. AOSP codenames, tags, and build numbers

[Q&A]

  • Could not find a supported mac sdk: [“10.10” “10.11” “10.12” “10.13” “10.14” “10.15”]?

    If you see this error message, you can check your MacOSX SDK under

    1
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/

    To make sure you have correct SDKs for building. If you can’t see the needed SDKs under this folder, there are two ways you can solve this error:

    a. You can download corresponding SDKs from https://github.com/phracker/MacOSX-SDKs and put these SDKs into the folder.

    b. You can edit the build/soong/cc/config/x86_darwin_host.go and add your current pointed SDK into this file.


  • When seeing : system/core/base/cmsg.cpp:36:21: error: use of undeclared identifier PAGE_SIZE

    Add this line into the file size_t psize = getpagesize();

1
2
3
4
5
namespace base {
size_t psize = getpagesize();
ssize_t SendFileDescriptorVector(borrowed_fd sockfd, const void* data, size_t len,

...
And then, replace the two distances of the PAGE_SIZE with `psize`.
1
if (cmsg_space >= psize) {

  • When seeing Segmentation fault: 11

    Edit this file system/sepolicy/tests/Android.bp by deleting this line libc++_static and then re-compile.

    Check this official fix from google.


  • When seeing these errors
1
2
3
4
5
external/python/cpython2/Modules/getpath.c:414:50: error: incompatible pointer types passing 'unsigned long *' to parameter of type 'uint32_t *' (aka 'unsigned int *') [-Werror,-Wincompatible-pointer-types]

else if(0 == _NSGetExecutablePath(progpath, &nsexeclength) && progpath[0] == SEP)

^~~~~~~~~~~~~
or
1
2
3
4
5
external/python/cpython3/Modules/getpath.c:414:50: error: incompatible pointer types passing 'unsigned long *' to parameter of type 'uint32_t *' (aka 'unsigned int *') [-Werror,-Wincompatible-pointer-types]

else if(0 == _NSGetExecutablePath(progpath, &nsexeclength) && progpath[0] == SEP)

^~~~~~~~~~~~~
You can solve these errors by modifying them.
1
2
3
4
5
6
7
#ifdef __APPLE__
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
uint32_t nsexeclength = MAXPATHLEN;
#else
unsigned long nsexeclength = MAXPATHLEN;
#endif
#endif
to
1
2
3
#ifdef __APPLE__
uint32_t nsexeclength = MAXPATHLEN;
#endif
#Android #Android/Build

How to build AOSP on M1

apple-m1.png

Before we start this job, let’s check how my laptop looks. Here are some factors of my M1 MacBook Pro 2020

  • OS version : macOS Monterey 12.1 Beta (21C5021h)
  • Chipset: Apple M1
  • Memory size: 16 GB

First of all, you will need to install the XCode command-line tools.

1
xcode-select --install

And, you should also install Rosetta for code-building.

1
/usr/sbin/softwareupdate --install-rosetta

When I tried to install Rosetta, I found some system errors, and I can’t solve that, but it seems that doesn’t matter. You can still build the AOSP project.

Now, we should prepare a case-sensitive disk image cause the AOSP should build on the Ubuntu system, but MacOS is not a case-sensitive file system ref.

1
hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 350g ~/aosp_source.dmg

After this, let’s create a ZSH environment. You might first time hear about ZSH. Here are some helpful tools for empowering your ZSH env, such as Oh My Zsh, Powerlevel10k, etc. I highly recommend that you try PowerLevel10k, a successor of the powerlevel9k, and it’s a perfect theme for ZSH env.

When you finish up installing/setting the ZSH, we need to create and edit the .zshenv with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# set the number of open files to be 2048
ulimit -S -n 2048

# Compiler cache
export USE_CCACHE=1

# Mount the Android Tree
function mountAOSP { hdiutil attach ~/aosp_source.dmg.sparseimage -mountpoint /Volumes/aosp_source; }

#Unmount the Android Tree
function umountAOSP() { hdiutil detach /Volumes/aosp_source; }

export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export PATH=~/.bin:$PATH

Let’s use this env setting and start downloading the AOSP code. We are going to use android-11.0.0_r48 for the building.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ source ~/.zshenv
$ mkdir ~/.bin
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/.bin/repo
$ chmod a+x ~/.bin/repo
$ mountAOSP
$ cd /Volumes/aosp_source
$ mkdir aospsrc
$ cd aospsrc

// setup your gitconfig
$ git config --global user.name "Your Name"
$ git config --global user.email "you@example.com"
$ repo init -u https://android.googlesource.com/platform/manifest -b android-11.0.0_r48
$ repo sync

Here are some commands to build AOSP:

1
2
3
$ source build/envsetup.sh 
$ lunch aosp_arm-eng
$ make -j24

Then, you can start enjoying the AOSP journey now.

[Reference]

  1. Code Search for AOSP
  2. Build AOSP
  3. Download AOSP
  4. AOSP codenames, tags, and build numbers

[Q&A]

  • Could not find a supported mac sdk: [“10.10” “10.11” “10.12” “10.13” “10.14” “10.15”]?

    If you see this error message, you can check your MacOSX SDK under

    1
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/

    To make sure you have correct SDKs for building. If you can’t see the needed SDKs under this folder, there are two ways you can solve this error:

    a. You can download corresponding SDKs from https://github.com/phracker/MacOSX-SDKs and put these SDKs into the folder.

    b. You can edit the build/soong/cc/config/x86_darwin_host.go and add your current pointed SDK into this file.


  • When seeing : system/core/base/cmsg.cpp:36:21: error: use of undeclared identifier PAGE_SIZE

    Add this line into the file size_t psize = getpagesize();

1
2
3
4
5
namespace base {
size_t psize = getpagesize();
ssize_t SendFileDescriptorVector(borrowed_fd sockfd, const void* data, size_t len,

...
And then, replace the two distances of the PAGE_SIZE with `psize`.
1
if (cmsg_space >= psize) {

  • When seeing Segmentation fault: 11

    Edit this file system/sepolicy/tests/Android.bp by deleting this line libc++_static and then re-compile.

    Check this official fix from google.


  • When seeing these errors
1
2
3
4
5
external/python/cpython2/Modules/getpath.c:414:50: error: incompatible pointer types passing 'unsigned long *' to parameter of type 'uint32_t *' (aka 'unsigned int *') [-Werror,-Wincompatible-pointer-types]

else if(0 == _NSGetExecutablePath(progpath, &nsexeclength) && progpath[0] == SEP)

^~~~~~~~~~~~~
or
1
2
3
4
5
external/python/cpython3/Modules/getpath.c:414:50: error: incompatible pointer types passing 'unsigned long *' to parameter of type 'uint32_t *' (aka 'unsigned int *') [-Werror,-Wincompatible-pointer-types]

else if(0 == _NSGetExecutablePath(progpath, &nsexeclength) && progpath[0] == SEP)

^~~~~~~~~~~~~
You can solve these errors by modifying them.
1
2
3
4
5
6
7
#ifdef __APPLE__
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
uint32_t nsexeclength = MAXPATHLEN;
#else
unsigned long nsexeclength = MAXPATHLEN;
#endif
#endif
to
1
2
3
#ifdef __APPLE__
uint32_t nsexeclength = MAXPATHLEN;
#endif
#Android #Android/Build