前言
为了学习安卓源码的编译过程,以及给Pixel4手机烧录镜像,笔者利用手头的两台电脑进行了编译AOSP的尝试,
两台设备分别为:
1、黑苹果(Based on 19款联想小新pro13),i7 10710U 6核12线程+16GB,OS版本Big Sur 11.5.1,以下称为Intel平台
2、21款Macbook Pro 16,M1MAX 10核+32GB,OS版本Monterey 12.5.1,以下称为Apple Silicon平台
AOSP版本为Android11 r46
先说结论:两台设备均可以完成编译,但编译过程中会报出大量错误,排除这些错误需要花费很多的时间,而且其中错误需要修改源码来解决,这可能带来潜在的问题。
另外,谷歌官方文档已提示:
但是如果你坚持要尝试,请继续往下看:
一、准备磁盘
因为MacOS的磁盘默认为APFS不区分大小写格式,而AOSP的编译需要在区分大小写的文件系统中运行,所以需要开辟一块磁盘
操作步骤:
-
打开磁盘工具,选中Macintosh HD宗卷,点击“+”,名称自己定义,格式选择APFS(区分大小写),大小选项中设置至少200GB的空间
-
点击确定后,在/Volumes/下会自动挂载所开辟的磁盘
二、安装配置
1、安装python、git和Xcode
- python和git的安装没什么好说的,安装方法很多,请自行百度
但需要注意,MacOS从12.3版本开始系统不再预置python2,需要手动下载安装
安装Xcode直接在AppStore下载即可,安装过程很慢,需要耐心等待 - 配置git config
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
4、创建bin目录
- mkdir ~/bin
- PATH=~/bin:$PATH
5、下载repo
curl�https://storage.googleapis.com/git-repo-downloads/repo�>�~/bin/repo�//google地址,需要翻墙
curl -sSL 'https://gerrit-googlesource.proxy.ustclug.org/git-repo/+/master/repo?format=TEXT'|base64 -d > ~/bin/repo//若上⾯地址不能访问,可使⽤中科⼤地址
chmod a+x ~/bin/repo
由于repo运行过程中会尝试访问官方的git源更新自己,因此建议使用utsc镜像源进行更新
在 ~/.bash_profile新增
export REPO_URL='https://gerrit-googlesource.proxy.ustclug.org/git-repo'
6、执行source ~/.bash_profile
三、 源码下载
1、进入准备好的磁盘
2、创建源码存放的文件夹,进入文件夹
mkdir AOSP_11
cd AOSP_11
3、查看源代码标记
打开谷歌的源代码标记文档,从中选择想要同步的分支,笔者这里选择的是android-11.0.0_r46,因为它是最后一个适配Pixel4的Andoird11小版本
https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds
由于上面的链接需要翻墙,所以也提供如下的utsc链接供查找
http://mirrors.ustc.edu.cn/aosp/platform/manifest.git/refs/tags/
4、初始化
repo init -u git://mirrors.ustc.edu.cn/aosp/platform/manifest -b android-11.0.0_r46
若想下载其他版本,可用对应的标记来替换android-11.0.0_r46
5、同步源代码树
建议使用以下的命令
repo�sync�-c�--no-tags�--prune�-f�-j4�
其中-j4表示使用4个线程,但注意不要开过多的线程,否则服务器会拒绝你的访问
四、编译
1、初始化环境:
- 在AOSP_11目录下,执行envsetup.sh脚本初始化环境
source build/envsetup.sh
- 清理之前编译的输出文件:
make clobber
- 选择编译目标
lunch
- 执行上面的命令后,终端会输出如下提示信息:
You're building on Darwin
Lunch menu... pick a combo:
1. aosp_arm-eng
2. aosp_arm64-eng
3. aosp_blueline-userdebug
4. aosp_blueline_car-userdebug
5. aosp_bonito-userdebug
6. aosp_bonito_car-userdebug
7. aosp_bramble-userdebug
8. aosp_car_arm-userdebug
9. aosp_car_arm64-userdebug
10. aosp_car_x86-userdebug
11. aosp_car_x86_64-userdebug
12. aosp_cf_arm64_auto-userdebug
13. aosp_cf_arm64_phone-userdebug
14. aosp_cf_x86_64_phone-userdebug
15. aosp_cf_x86_auto-userdebug
16. aosp_cf_x86_phone-userdebug
17. aosp_cf_x86_tv-userdebug
18. aosp_coral-userdebug
19. aosp_coral_car-userdebug
20. aosp_crosshatch-userdebug
21. aosp_crosshatch_car-userdebug
22. aosp_flame-userdebug
23. aosp_flame_car-userdebug
24. aosp_redfin-userdebug
25. aosp_sargo-userdebug
26. aosp_sunfish-userdebug
27. aosp_trout_arm64-userdebug
28. aosp_trout_x86-userdebug
29. aosp_x86-eng
30. aosp_x86_64-eng
31. arm_krait-eng
32. arm_v7_v8-eng
33. armv8-eng
34. armv8_kryo385-eng
35. beagle_x15-userdebug
36. beagle_x15_auto-userdebug
37. car_x86_64-userdebug
38. db845c-userdebug
39. fuchsia_arm64-eng
40. fuchsia_x86_64-eng
41. hikey-userdebug
42. hikey64_only-userdebug
43. hikey960-userdebug
44. hikey960_tv-userdebug
45. hikey_tv-userdebug
46. pixel3_mainline-userdebug
47. poplar-eng
48. poplar-user
49. poplar-userdebug
50. qemu_trusty_arm64-userdebug
51. silvermont-eng
52. uml-userdebug
53. yukawa-userdebug
54. yukawa_sei510-userdebug
Which would you like? [aosp_arm-eng]
关于lunch选择,官方文档有如下说明
Pixel4代号flame,因此笔者选择22并回车,输出如下:
//Intel平台
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=11
TARGET_PRODUCT=aosp_flame
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv8-a
TARGET_2ND_CPU_VARIANT=generic
HOST_ARCH=x86_64
HOST_OS=darwin
HOST_OS_EXTRA=Darwin-20.6.0-x86_64-10.16
HOST_BUILD_TYPE=release
BUILD_ID=RQ3A.211001.001
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=hardware/google/av hardware/google/camera hardware/google/interfaces hardware/google/pixel device/google/coral hardware/qcom/sm8150/display hardware/qcom/sm8150/data/ipacfg-mgr hardware/qcom/sm8150/gps vendor/google/airbrush/floral vendor/google/biometrics/face vendor/google/camera vendor/google/darwinn vendor/qcom/sm8150 vendor/qcom/sm8150/proprietary/commonsys/telephony-apps/DataStatusNotification vendor/qcom/sm8150/proprietary/gps vendor/qcom/sm8150/codeaurora/location vendor/google/interfaces vendor/qcom/sensors vendor/google/tools/sensors vendor/qcom/sm7250/codeaurora/commonsys/telephony/ims/ims-ext-common vendor/qcom/sm7250/codeaurora/dataservices/rmnetctl vendor/qcom/sm7250/proprietary/commonsys/qcrilOemHook vendor/qcom/sm7250/proprietary/commonsys/telephony-apps/ims vendor/qcom/sm7250/proprietary/commonsys/telephony-apps/QtiTelephonyService vendor/qcom/sm7250/proprietary/commonsys/telephony-apps/xdivert vendor/qcom/sm7250/proprietary/qcril-data-hal vendor/qcom/sm7250/proprietary/qcril-hal vendor/qcom/sm7250/proprietary/data
//Apple Silicon平台
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=11
TARGET_PRODUCT=aosp_flame
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv8-a
TARGET_2ND_CPU_VARIANT=generic
HOST_ARCH=x86_64
HOST_OS=darwin
HOST_OS_EXTRA=Darwin-21.6.0-x86_64-12.5.1
HOST_BUILD_TYPE=release
BUILD_ID=RQ3A.211001.001
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=hardware/google/av hardware/google/camera hardware/google/interfaces hardware/google/pixel device/google/coral hardware/qcom/sm8150/display hardware/qcom/sm8150/data/ipacfg-mgr hardware/qcom/sm8150/gps vendor/google/airbrush/floral vendor/google/biometrics/face vendor/google/camera vendor/google/darwinn vendor/qcom/sm8150 vendor/qcom/sm8150/proprietary/commonsys/telephony-apps/DataStatusNotification vendor/qcom/sm8150/proprietary/gps vendor/qcom/sm8150/codeaurora/location vendor/google/interfaces vendor/qcom/sensors vendor/google/tools/sensors vendor/qcom/sm7250/codeaurora/commonsys/telephony/ims/ims-ext-common vendor/qcom/sm7250/codeaurora/dataservices/rmnetctl vendor/qcom/sm7250/proprietary/commonsys/qcrilOemHook vendor/qcom/sm7250/proprietary/commonsys/telephony-apps/ims vendor/qcom/sm7250/proprietary/commonsys/telephony-apps/QtiTelephonyService vendor/qcom/sm7250/proprietary/commonsys/telephony-apps/xdivert vendor/qcom/sm7250/proprietary/qcril-data-hal vendor/qcom/sm7250/proprietary/qcril-hal vendor/qcom/sm7250/proprietary/data
你会发现,Apple Silicon平台也被识别成了HOST_ARCH=x86_64,但是不用管,继续下一步
- 开始编译代码:
使用 make 编译代码,GNU Make 同样可以借助 -jN 参数处理并行任务,通常使用的任务数 N 介于编译时所用计算机上硬件线程数的 1-2 倍之间,笔者这里参照i7 10710的规格,线程数统一选择12
make -j12
- 执行完之后,开始编译并输出:
//Intel平台
13:51:59 ************************************************************
13:51:59 You are building on a machine with 16GB of RAM
13:51:59
13:51:59 The minimum required amount of free memory is around 16GB,
13:51:59 and even with that, some configurations may not work.
13:51:59
13:51:59 If you run into segfaults or other errors, try reducing your
13:51:59 -j value.
13:51:59 ************************************************************
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=11
TARGET_PRODUCT=aosp_flame
//Apple Silicon平台
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=11
TARGET_PRODUCT=aosp_flame
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv8-a
TARGET_2ND_CPU_VARIANT=generic
HOST_ARCH=x86_64
HOST_OS=darwin
HOST_OS_EXTRA=Darwin-21.6.0-x86_64-12.5.1
HOST_BUILD_TYPE=release
BUILD_ID=RQ3A.211001.001
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=hardware/google/av hardware/google/camera hardware/google/interfaces hardware/google/pixel device/google/coral hardware/qcom/sm8150/display hardware/qcom/sm8150/data/ipacfg-mgr hardware/qcom/sm8150/gps vendor/google/airbrush/floral vendor/google/biometrics/face vendor/google/camera vendor/google/darwinn vendor/qcom/sm8150 vendor/qcom/sm8150/proprietary/commonsys/telephony-apps/DataStatusNotification vendor/qcom/sm8150/proprietary/gps vendor/qcom/sm8150/codeaurora/location vendor/google/interfaces vendor/qcom/sensors vendor/google/tools/sensors vendor/qcom/sm7250/codeaurora/commonsys/telephony/ims/ims-ext-common vendor/qcom/sm7250/codeaurora/dataservices/rmnetctl vendor/qcom/sm7250/proprietary/commonsys/qcrilOemHook vendor/qcom/sm7250/proprietary/commonsys/telephony-apps/ims vendor/qcom/sm7250/proprietary/commonsys/telephony-apps/QtiTelephonyService vendor/qcom/sm7250/proprietary/commonsys/telephony-apps/xdivert vendor/qcom/sm7250/proprietary/qcril-data-hal vendor/qcom/sm7250/proprietary/qcril-hal vendor/qcom/sm7250/proprietary/data
============================================
接下来,耐心等待即可。。。(当然,事情不会如此简单,有大量的坑等着你)
五、编译遇到的坑
问题1、Could not find a supported mac sdk
错误信息:
--- FAIL: TestVndkUsingCoreVariant (0.04s)
cc_test.go:524: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:524: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:524: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
--- FAIL: TestVndkWhenVndkVersionIsNotSet (0.03s)
cc_test.go:530: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:530: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:530: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:530: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:530: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:530: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
--- FAIL: TestVndkWithHostSupported (0.04s)
cc_test.go:434: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:434: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:434: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:434: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:434: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
cc_test.go:434: "Could not find a supported mac sdk: [\"10.10\" \"10.11\" \"10.12\" \"10.13\" \"10.14\" \"10.15\"]"
FAIL
21:30:32 soong bootstrap failed with: exit status 1
ninja: build stopped: subcommand failed.
#### failed to build some targets (29 seconds) ####
1、问题原因:
找不到支持列表中指定的mac sdk
2、解决办法:
方法1: 直接修改/x86_darwin_host.go
- 首先按照下面的方法查看自己电脑上的 mac sdk:
cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
ls
- 如果提示找不到此目录,请在AppStore下载并安装Xcode
- 如果已经安装过Xcode,会打印出当前 XCode 提供的 SDK 版本号,其中MacOSX.sdk为指向MacOSX12.X.sdk的软链接,如下
//Intel平台
MacOSX.sdk MacOSX12.1.sdk
//Apple Silicon平台
MacOSX.sdk MacOSX12.3.sdk
- 在/build/soong/cc/config/x86_darwin_host.go 文件中找到 “darwinSupportedSdkVersions“ 添加 MacOSX.sdk 对应的版本号——以Intel平台为例
darwinSupportedSdkVersions = []string{
"10.10",
"10.11",
"10.12",
"10.13",
"10.14",
"10.15",
"12.1",
}
办法2、下载支持的sdk
-
从https://github.com/phracker/MacOSX-SDKs/releases下载当支持的sdk版本
解压并将sdk放入/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs目录下
(比较坑的是,网上有很多解决此问题的指导到这里就结束了,所以再次开始编译仍然会报相同的错!)-
修改系统支持的最小SDK版本
进入/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/目录
使用Xcode打开Info.plist文件,将MinimumSDKVersion改成你想支持的最低版本,笔者这里修改成了10.15
由于该文件和所在目录都有权限限制,因此无法直接修改
如果你比较懒,那就直接
sudo chmod 777 MacOSX.platform
sudo chmod 777 Info.plist
但是更建议先复制到当前用户有权限修改的目录下,然后修改,待完成修改后,复制回去:
cp /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Info.plist ~/Downloads
方法选择:
笔者更推荐第二种方法,因为如果当前sdk版本和支持的sdk版本相差过大(例如笔者这种情况)时,强行使用新sdk会带来更多的潜在麻烦,稳妥起见还是老老实实用官方推荐的sdk吧
问题2、too many open files
错误日志如下:
FAILED: out/soong/build.ninja
out/soong/.bootstrap/bin/soong_build -t -l out/.module_paths/Android.bp.list -b out/soong -n out -d out/soong/build.ninja.d -globFile out/soong/.bootstrap/build-globs.ninja -o out/soong/build.ninja Android.bp
error: prebuilts/sdk/Android.bp:25:1: module "metalava-sdk-android-jars": glob: fcntl: too many open files
error: system/timezone/distro/tools/Android.bp:16:1: module "create_time_zone_distro" variant "darwin_common": glob: open /Volumes/AOSP/WORKING_DIRECTORY/system/timezone/distro/tools/src/main/com/android/timezone: too many open files
error: cts/tools/vm-tests-tf/Android.bp:58:1: module "vmtests-dfh-dex-generated" variant "darwin_common": glob: fcntl: too many open files
1、问题原因:
打开的文件数量超过了系统限制
2、 解决办法:
当然是把这个值改大一点
ulimit -S -n 2048
问题3、host_ldlibs: Host library '-lrt' not available
1、错误日志:
FAILED: out/soong/build.ninja
out/soong/.bootstrap/bin/soong_build -t -l out/.module_paths/Android.bp.list -b out/soong -n out -d out/soong/build.ninja.d -globFile out/soong/.bootstrap/build-globs.ninja -o out/soong/build.ninja Android.bp
error: external/v8/Android.bp:181:16: module "v8_bytecode_builtins_list_generator" variant "darwin_x86_64": host_ldlibs: Host library `-lrt` not available
error: external/v8/Android.bp:209:16: module "v8_torque" variant "darwin_x86_64": host_ldlibs: Host library `-lrt` not available
21:48:40 soong bootstrap failed with: exit status 1
#### failed to build some targets (01:16 (mm:ss)) ####
2、问题原因:https://stackoverflow.com/questions/47703239/ld-library-not-found-for-lrt
在旧的 Linux 系统中有 -lrt 供 GNU libc 使用,但是在新的 glibc(after 2.17 from 2013) 之后就没有这个了。
3、解决办法:
在external/v8/Android.bp找到 -lrt ,把它们全部注释掉即可。
问题4、找不到头文件sys/prctl.h
1、错误日志:
FAILED: out/soong/.intermediates/external/v8/v8_libbase/darwin_x86_64_static/obj/external/v8/src/base/platform/platform-linux.o
prebuilts/clang/host/darwin-x86/clang-r383902b1/bin/clang++ -c -Wno-enum-compare -Wno-enum-compare-switch -Wno-null-pointer-arithmetic -Wno-null-dereference -Wno-pointer-compare -Wno-xor-used-as-pow -Wno-final-dtor-non-final-class -fPIC -funwind-tables -D__STDC_FORMAT_MACROS -D__STDC_CONSTANT_MACROS -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -mmacosx-version-min=10.10 -DMACOSX_DEPLOYMENT_TARGET=10.10 -m64 -integrated-as -fstack-protector-strong -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -no-canonical-prefixes -DNDEBUG -UDEBUG -fno-exceptions -Wno-multichar -O2 -g -fno-strict-aliasing -Werror=date-time -Werror=pragma-pack -Werror=pragma-pack-suspicious-include -D__compiler_offsetof=__builtin_offsetof -faddrsig -Werror=int-conversion -fexperimental-new-pass-manager -Wno-reserved-id-macro -Wno-unused-command-line-argument -fcolor-diagnostics -Wno-sign-compare -Wno-defaulted-function-deleted -Wno-inconsistent-missing-override -Wno-c99-designator -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -target x86_64-apple-darwin -B/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin -fPIC -Wsign-promo -Wimplicit-fallthrough -D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS -Wno-gnu-include-next -Iexternal/v8/src -Iexternal/v8 -Iexternal/libcxx/include -Iexternal/libcxxabi/include -Wall -Werror -Wno-endif-labels -Wno-implicit-fallthrough -Wno-import -Wno-format -Wno-unused-variable -Wno-unused-parameter -Wno-unused-private-field -Wno-sign-compare -Wno-missing-field-initializers -Wno-ignored-qualifiers -Wno-undefined-var-template -Wno-non-virtual-dtor -Wno-user-defined-warnings -Wno-unused-lambda-capture -Wno-missing-braces -fno-exceptions -fvisibility=hidden -fno-rtti -g0 -Os -DV8_TYPED_ARRAY_MAX_SIZE_IN_HEAP=64 -DV8_ATOMIC_OBJECT_FIELD_WRITES -DV8_ATOMIC_MARKING_STATE -DV8_ENABLE_LAZY_SOURCE_POSITIONS -DV8_ENABLE_REGEXP_INTERPRETER_THREADED_DISPATCH -DV8_SNAPSHOT_COMPRESSION -DV8_NO_ARGUMENTS_ADAPTOR -DDISABLE_UNTRUSTED_CODE_MITIGATIONS -UANDROID -DV8_TARGET_ARCH_X64 -std=gnu++14 -D_LIBCPP_DISABLE_AVAILABILITY -nostdinc++ -Isystem/core/include -Isystem/media/audio/include -Ihardware/libhardware/include -Ihardware/libhardware_legacy/include -Ihardware/ril/include -Iframeworks/native/include -Iframeworks/native/opengl/include -Iframeworks/av/include -Ilibnativehelper/include_jni -Werror=int-to-pointer-cast -Werror=pointer-to-int-cast -Werror=fortify-source -Werror=address-of-temporary -Werror=return-type -Wno-tautological-constant-compare -Wno-tautological-type-limit-compare -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-int-in-bool-context -Wno-sizeof-array-div -Wno-tautological-overlap-compare -Wno-deprecated-copy -Wno-range-loop-construct -Wno-misleading-indentation -Wno-zero-as-null-pointer-constant -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-string-compare -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-pessimizing-move -MD -MF out/soong/.intermediates/external/v8/v8_libbase/darwin_x86_64_static/obj/external/v8/src/base/platform/platform-linux.o.d -o out/soong/.intermediates/external/v8/v8_libbase/darwin_x86_64_static/obj/external/v8/src/base/platform/platform-linux.o external/v8/src/base/platform/platform-linux.cc
external/v8/src/base/platform/platform-linux.cc:13:10: fatal error: 'sys/prctl.h' file not found
#include
^~~~~~~~~~~~~
1 error generated.
16:47:22 ninja failed with: exit status 1
#### failed to build some targets (02:03:51 (hh:mm:ss)) ####
2、问题原因:
编译脚本错误的引用了platform-linux.cc(官方放弃治疗后这类问题会越来越多)
3、解决办法:
工程目录下全局搜索 “platform-linux”,并把 “platform-linux.cc” 改为 “platform-macos.cc”。涉及到的文件有:
external/v8/Android.libbase.bp
external/v8/BUILG.gn
external/v8/genmakefiles.py
问题5、'mach/mach_init.h' file not found
1、错误日志:
FAILED: out/soong/.intermediates/external/v8/v8_libbase/android_arm64_armv8-a_static_cfi_com.android.art.debug/obj/external/v8/src/base/platform/platform-macos.o
prebuilts/clang/host/darwin-x86/clang-r383902b1/bin/clang++ -c -Wno-enum-compare -Wno-enum-compare-switch -Wno-null-pointer-arithmetic -Wno-null-dereference -Wno-pointer-compare -Wno-xor-used-as-pow -Wno-final-dtor-non-final-class -D__ANDROID_APEX__ -D__ANDROID_SDK_VERSION__=10000 -Werror=implicit-function-declaration -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -no-canonical-prefixes -DNDEBUG -UDEBUG -fno-exceptions -Wno-multichar -O2 -g -fno-strict-aliasing -Werror=date-time -Werror=pragma-pack -Werror=pragma-pack-suspicious-include -D__compiler_offsetof=__builtin_offsetof -faddrsig -Werror=int-conversion -fexperimental-new-pass-manager -Wno-reserved-id-macro -Wno-unused-command-line-argument -fcolor-diagnostics -Wno-sign-compare -Wno-defaulted-function-deleted -Wno-inconsistent-missing-override -Wno-c99-designator -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ffunction-sections -fdata-sections -fno-short-enums -funwind-tables -fstack-protector-strong -Wa,--noexecstack -D_FORTIFY_SOURCE=2 -Wstrict-aliasing=2 -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Werror=format-security -nostdlibinc -march=armv8-a -target aarch64-linux-android10000 -Bprebuilts/gcc/darwin-x86/aarch64/aarch64-linux-android-4.9/aarch64-linux-android/bin -fPIC -Wsign-promo -Wimplicit-fallthrough -D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS -Wno-gnu-include-next -fvisibility-inlines-hidden -Iexternal/v8/src -Iexternal/v8 -D__LIBC_API__=10000 -D__LIBM_API__=10000 -D__LIBDL_API__=10000 -Iexternal/libcxx/include -Iexternal/libcxxabi/include -Ibionic/libc/include -Wall -Werror -Wno-endif-labels -Wno-implicit-fallthrough -Wno-import -Wno-format -Wno-unused-variable -Wno-unused-parameter -Wno-unused-private-field -Wno-sign-compare -Wno-missing-field-initializers -Wno-ignored-qualifiers -Wno-undefined-var-template -Wno-non-virtual-dtor -Wno-user-defined-warnings -Wno-unused-lambda-capture -Wno-missing-braces -fno-exceptions -fvisibility=hidden -fno-rtti -g0 -Os -DV8_TYPED_ARRAY_MAX_SIZE_IN_HEAP=64 -DV8_ATOMIC_OBJECT_FIELD_WRITES -DV8_ATOMIC_MARKING_STATE -DV8_ENABLE_LAZY_SOURCE_POSITIONS -DV8_ENABLE_REGEXP_INTERPRETER_THREADED_DISPATCH -DV8_SNAPSHOT_COMPRESSION -DV8_NO_ARGUMENTS_ADAPTOR -DDISABLE_UNTRUSTED_CODE_MITIGATIONS -DV8_TARGET_ARCH_ARM64 -flto -fsanitize-cfi-cross-dso -fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt -fsanitize=cfi -fsanitize-trap=all -ftrap-function=abort -std=gnu++14 -fno-rtti -Isystem/core/include -Isystem/media/audio/include -Ihardware/libhardware/include -Ihardware/libhardware_legacy/include -Ihardware/ril/include -Iframeworks/native/include -Iframeworks/native/opengl/include -Iframeworks/av/include -isystem bionic/libc/include -isystem bionic/libc/kernel/uapi -isystem bionic/libc/kernel/uapi/asm-arm64 -isystem bionic/libc/kernel/android/scsi -isystem bionic/libc/kernel/android/uapi -Ilibnativehelper/include_jni -Werror=int-to-pointer-cast -Werror=pointer-to-int-cast -Werror=fortify-source -Werror=address-of-temporary -Werror=return-type -Wno-tautological-constant-compare -Wno-tautological-type-limit-compare -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-int-in-bool-context -Wno-sizeof-array-div -Wno-tautological-overlap-compare -Wno-deprecated-copy -Wno-range-loop-construct -Wno-misleading-indentation -Wno-zero-as-null-pointer-constant -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-string-compare -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-pessimizing-move -MD -MF out/soong/.intermediates/external/v8/v8_libbase/android_arm64_armv8-a_static_cfi_com.android.art.debug/obj/external/v8/src/base/platform/platform-macos.o.d -o out/soong/.intermediates/external/v8/v8_libbase/android_arm64_armv8-a_static_cfi_com.android.art.debug/obj/external/v8/src/base/platform/platform-macos.o external/v8/src/base/platform/platform-macos.cc
external/v8/src/base/platform/platform-macos.cc:9:10: fatal error: 'mach/mach_init.h' file not found
#include
^~~~~~~~~~~~~~~~~~
1 error generated.
[ 50% 24333/48037] //bionic/libc:common_libc versioner preprocess include
warning: attempted to generate guard with empty availability: obsoleted = 21
warning: attempted to generate guard with empty availability: obsoleted = 23
[ 50% 24334/48037] //bionic/libc:libc.llndk versioner preprocess include
warning: attempted to generate guard with empty availability: obsoleted = 21
warning: attempted to generate guard with empty availability: obsoleted = 23
[ 50% 24335/48037] //bionic/libc:libc.llndk versioner preprocess include
warning: attempted to generate guard with empty availability: obsoleted = 21
warning: attempted to generate guard with empty availability: obsoleted = 23
10:33:54 ninja failed with: exit status 1
#### failed to build some targets (04:24 (mm:ss)) ####
2、问题原因:
在incldue目录下找不到指定的头文件
3、解决办法:
这是最难顶的一个问题,
Mac 系统的头文件是有由Xcode 提供的,笔者已经安装了Xcode,并配置了支持版本的SDK,在
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/mach目录下也能找到此头文件
网上有教程提供了此问题的解决办法:
把MacOSX10.15.sdk/usr/include/目录下的文件复制到 /Library/Developer/CommandLineTools/usr/include 目录下,然后
export C_INCLUDE_PATH=/Users/xxx/Develop/mac_include/include
或者直接
export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
笔者尝试了这两种方法之后,均会报错,此问题逐渐陷入无解的境地。
但是,网上一篇教程评论区的一条回复提醒了笔者:问题3、4、5均为/externel/v8这个仓内的文件所引起,这个仓最后的提交是:
commit dc02784c8323eab361da0fa9aaf796bfee98aee5 (HEAD, tag: android-11.0.0_r46, tag: android-11.0.0_r43, tag: android-11.0.0_r40, tag: android-11.0.0_r39, tag: android-11.0.0_r38, tag: m/android-11.0.0_r46, aosp/android11-qpr3-s1-release, aosp/android11-qpr3-release)
Merge: c35ae874 e11facc1
Author: android-build-team Robot
Date: Tue Apr 6 22:04:22 2021 +0000
Snap for 7262953 from e11facc1049b2a0c85e4f3e5ff214127851ddf2f to rvc-qpr3-release
Change-Id: Iae0a5ef411ead7201768d5c574a4676142bf7968
commit e11facc1049b2a0c85e4f3e5ff214127851ddf2f
Merge: 2eab410d 2dc37779
Author: Cindy Zhou
Date: Tue Apr 6 17:56:24 2021 +0000
Update v8 cfi blocklist am: 2dc3777900
Original change: https://googleplex-android-review.googlesource.com/c/platform/external/v8/+/13949411
Change-Id: I4b8313c4d29464d13c66add8c2932aeb3655b868
其中有几条提交值得注意:
commit 2eab410d1a22744d330b3a4565f26a52c1d1d8eb
Merge: de8ec0ef c509674b
Author: Rubin Xu
Date: Wed Mar 17 14:53:58 2021 +0000
[automerger skipped] [DO NOT MERGE] Fix OSX builds for V8 am: 5658a66011 -s ours am: 05251a6458 -s ours am: 70ec779ad7 am: 655032cb7c -s ours am: c509674b68
Original change: https://googleplex-android-review.googlesource.com/c/platform/external/v8/+/13799500
Change-Id: I8b94be689a70a2e0590244117e4f9c4a84ff26cf
commit c509674b68584088c88dd2aa04cc0d3616ef116c
Merge: bb466bed 655032cb
Author: Rubin Xu
Date: Wed Mar 17 14:14:27 2021 +0000
[automerger skipped] [DO NOT MERGE] Fix OSX builds for V8 am: 5658a66011 -s ours am: 05251a6458 -s ours am: 70ec779ad7 am: 655032cb7c -s ours
am skip reason: subject contains skip directive
Original change: https://googleplex-android-review.googlesource.com/c/platform/external/v8/+/13799500
Change-Id: Id90137931ba6224330aa4f9fb7c40ccde7d75fa2
commit 655032cb7ca217b7395649185d202c37f7681f39
Merge: 2df4129b 70ec779a
Author: Rubin Xu
Date: Wed Mar 17 13:32:27 2021 +0000
[automerger skipped] [DO NOT MERGE] Fix OSX builds for V8 am: 5658a66011 -s ours am: 05251a6458 -s ours am: 70ec779ad7
Original change: https://googleplex-android-review.googlesource.com/c/platform/external/v8/+/13799500
Change-Id: Iab9638c60ae6ba5364f304728c4e1602a1064744
可以看到,官方在2021年5月份时还在试图修复此仓在MacOS平台编译的问题,但是前文也提到,从6月份开始,官方就放弃维护了。因此笔者猜测,此仓在5月份之前的某些提交可能导致了MacOS平台的编译问题。
继续寻找,发现有很多的提交是为了执行操作Upgrade V8 to 8.8.278.14,例如
commit 9580a23bc5b8874a0979001d3595d027cbb68128
Author: Rubin Xu
Date: Tue Feb 16 09:38:50 2021 +0000
Upgrade V8 to 8.8.278.14
Bug: 162604069
Bug: 167389063
Test: gts-tradefed run gts-dev --module GtsGmscoreHostTestCases
--test com.google.android.gts.devicepolicy.DeviceOwnerTest#testProxyPacProxyTest
Test: m -j proxy_resolver_v8_unittest && adb sync && adb shell \
/data/nativetest/proxy_resolver_v8_unittest/proxy_resolver_v8_unittest
Merged-In: Ifb09923b9d7f6d8990fb062d7dc0294edf2c098e
Change-Id: Ifb09923b9d7f6d8990fb062d7dc0294edf2c098e
于是笔者将此仓直接回退到了2021年1月8日的版本:
cd external/v8
git reset --hard 55fc782bb8accc49ae3ed52e6856d55392e2a579
然后重新开始编译
至此,编译过程不再有源自v8仓的报错产生,两个平台均完成了编译:
其中Apple Silicon平台耗时2小时23分钟
Intel平台耗时3小时22分
可以看到Apple Silicon平台借助庞大的晶体管规模,编译速度相比Intel平台确实有一定提升,但是这个提升并没有想象中那么大。
参考链接
https://blog.csdn.net/dirksmaller/article/details/124977651
https://www.jianshu.com/p/37f5f826b642
https://blog.csdn.net/w__y__q/article/details/121570143
https://51wlb.top/aosp/#%E9%97%AE%E9%A2%98%E4%B8%80%EF%BC%9ACould_not_find_a_supported_mac_sdk
https://blog.csdn.net/qq_34231329/article/details/125522049
http://events.jianshu.io/p/7971444b5359
https://www.jianshu.com/p/5df66b910ce0
最后
笔者只是在macOS平台尝试了最简单的AOSP源码编译,如果要将镜像烧录进Pixel手机,还需要下载对应机型的驱动文件,整合进源码并编译后才能烧录
例如Pixel4对应的驱动
至于修改了externel/v8仓编译出的镜像,在烧录进手机后是否会存在问题,笔者已没有更多的精力去尝试。
回到开头的话,为什么笔者不建议现阶段在MacOS平台编译AOSP。因为MacOS平台编译AOSP存在的变量实在太多,芯片架构、OS大小版本、SDK版本、系统库路径、Xcode、AOSP版本等等,任何一项不匹配都会带来大量的问题。人的精力是有限的,为什么要编译AOSP,是否值得花这么多时间来解决编译环境的问题,这是我们要思考的。笔者还是建议把有限的时间投入到最核心的工作中。所以后续编译源码和烧录镜像至手机的工作,笔者会切换到Linux平台进行。
以上