Android.bp相关知识
1.简介
早期的Android系统都是采⽤Android.mk的配置来编译源码,从Android 7.0开始引⼊Android.bp。很明显Android.bp的出现就是为了替换掉Android.mk。
再来说⼀说跟着Android版本相应的发展演变过程:
- Android 7.0引⼊ninja和kati
- Android 8.0使⽤Android.bp来替换Android.mk,引⼊Soong
- Android 9.0强制使⽤Android.bp
转换关系图如下:
通过Kati将Android.mk转换成ninja格式的⽂件,通过Blueprint+ Soong将Android.bp转换成ninja格式的⽂件,通过androidmk将Android.mk转换成Android.bp,但针对没有分⽀、循环等流程控制的Android.mk才有效。这⾥涉及到Ninja, kati, Soong, bp概念,接下来分别简单介绍⼀下。
1-1.Ninja
ninja是⼀个编译框架,会根据相应的ninja格式的配置⽂件进⾏编译,但是ninja⽂件⼀般不会⼿动修改,⽽是通过将Android.bp⽂件转换成ninja格⽂件来编译。
1-2.Android.bp
Android.bp的出现就是为了替换Android.mk⽂件。bp跟mk⽂件不同,它是纯粹的配置,没有分⽀、循环等流程控制,不能做算数逻辑运算。如果需要控制逻辑,那么只能通过Go语⾔编写。
1-3.Soong
Soong类似于之前的Makefile编译系统的核⼼,负责提供Android.bp语义解析,并将之转换成Ninja⽂件。Soong还会编译⽣成⼀个androidmk命令,⽤于将Android.mk⽂件转换为Android.bp⽂件,不过这个转换功能仅限于没有分⽀、循环等流程控制的Android.mk才有效。
1-4.Blueprint
Blueprint是⽣成、解析Android.bp的⼯具,是Soong的⼀部分。Soong负责Android编译⽽设计的⼯具,⽽Blueprint只是解析⽂件格式,Soong解析内容的具体含义。Blueprint和Soong都是由Golang写的项⽬,从Android 7.0,prebuilts/go/⽬录下新增Golang所需的运⾏环境,在编译时使⽤。
1-5. Kati
kati是专为Android开发的⼀个基于Golang和C++的⼯具,主要功能是把Android中的Android.mk⽂件转换成Ninja⽂件。代码路径是build/kati/,编译后的产物是ckati。
2.Android.bp语法
Android.bp是⼀种纯粹的配置⽂件,设计简单,没有条件判断或控制流语句,采⽤在Go语⾔编写控制逻辑。Android.bp⽂件记录着模块信息,每⼀个模块以模块类型开始,后⾯跟着⼀组模块的属性,以名值对(name:value)表示,每个模块都必须有⼀个 name属性。
cc_library_shared {
name: "libbluetooth_jni",
srcs: [
//编译成动态库,类似于Android.mk中的BUILD_SHARED_LIBRARY
//编译出的模块的名称,类似于Android.mk中的LOCAL_MODULE
//源⽂件,类似于Android.mk中的LOCAL_SRC_FILES
"com_android_bluetooth_btservice_AdapterService.cpp",
"com_android_bluetooth_hfp.cpp",
"com_android_bluetooth_hfpclient.cpp",
"com_android_bluetooth_a2dp.cpp",
"com_android_bluetooth_a2dp_sink.cpp",
"com_android_bluetooth_avrcp.cpp",
"com_android_bluetooth_avrcp_controller.cpp",
"com_android_bluetooth_hid.cpp",
"com_android_bluetooth_hidd.cpp",
"com_android_bluetooth_hdp.cpp",
"com_android_bluetooth_pan.cpp",
"com_android_bluetooth_gatt.cpp",
"com_android_bluetooth_sdp.cpp",
],
include_dirs: [
//⽤户指定的头⽂件查找路径,类似于Android.mk中的LOCAL_C_INCLUDES
"libnativehelper/include/nativehelper",
"system/bt/types",
],
shared_libs: [
LOCAL_SHARED_LIBRARIES
"libandroid_runtime",
"libchrome",
"libnativehelper",
"libcutils",
"libutils",
"liblog",
"libhardware",
],
//编译所依赖的动态库,类似于Android.mk中的static_libs: [
//编译所依赖的静态库,类似于Android.mk中的
LOCAL_STATIC_LIBRARIES
"libbluetooth-types",
],
cflags: [
///编译flag,类似于Android.mk中的LOCAL_CFLAGS
"-Wall",
"-Wextra",
"-Wno-unused-parameter",
],
}
2-1.模块
从前⾯的列⼦可以看出定义⼀个模块从模块的类型开始,模块有不同的类型,如前⾯例⼦中的cc_library_shared,当然类型还有很多种,譬如cc_binary android_app cc_library_static等等。模块包含⼀些属性格式为“property-name:property-value”,其中name属性必须指定,其属性值必须是全局唯⼀的。其中默认模块可⽤于在多个模块中重复相同的属性
cc_defaults {//
//默认模块名称
name: "default_module",
shared_libs: ["libz"],
stl: "none",
}
cc_binary {
name: "test1",
defaults: ["default_module"],
//引⽤默认模块名称
srcs: ["src/test/test.c"],
}
srcs 属性以字符串列表的形式指定⽤于编译模块的源⽂件。您可以使⽤模块引⽤语法 “:” 来引⽤⽣成源⽂件的其他
模块的输出,如 genrule 或 filegroup:
~/ssd/qcom_64/msm8953-9$ cd frameworks/base/core/java/
~/ssd/qcom_64/msm8953-9/frameworks/base/core/java$ vi Android.bp
filegroup {
name: "IKeyAttestationApplicationIdProvider.aidl",
srcs: ["android/security/keymaster/IKeyAttestationApplicationIdProvider.aidl"],
}
filegroup {
name: "IDropBoxManagerService.aidl",
srcs: ["com/android/internal/os/IDropBoxManagerService.aidl"],
~/ssd/qcom_64/msm8953-9/frameworks/base/core$ cd ..
~/ssd/qcom_64/msm8953-9/frameworks/base$ vi libs/services/Android.bp
cc_library_shared {name: "libservices",
srcs: [
":IDropBoxManagerService.aidl",
"src/os/DropBoxManager.cpp",
"src/os/StatsDimensionsValue.cpp",
"src/os/StatsLogEventWrapper.cpp",
],
shared_libs: [
"libbinder",
"liblog",
"libcutils",
"libutils",
],
2-2.类型
具体⽀持以下⼏种类型:
- Bool(true or false)
- Integers(int)
- Strings("string")
- Listsof strings (["string1", "string2"])
- Maps({key1: "value1", key2: ["value2"]})
2-3.注释
我们知道Android.mk中可以进⾏注释,当然Android.bp⾥⾯也可以,Android.mk中使⽤"#"然后添加注释,Android.bp使⽤单⾏注释//和多⾏注释/* */两种⽅式。
2-4.变量
变量范围限定为声明它们的⽂件的其余部分,可以使⽤ “=” 号赋值, 但是不能使⽤ “:=” 赋值。变量是不可变的,但有⼀个例外,它们可以附上+= 赋值,但仅在变量被引⽤之前。下⾯我们看⼀下正确使⽤变量的列⼦:
gzip_srcs = ["src/minigzip.c"],
cc_binary {
name: "gzip",
srcs: gzip_srcs,
shared_libs: ["libz"],
stl: "none",
}
2-5.操作符
String类型、字符串列表类型和Map类型⽀持操作符“+”。
2-6.⽀持模块类型
Android.bp可以⽀持android_app、cc_binary、cc_binary_host等多种类型,具体定义在Android源码的
android-11/build/soong/androidmk/androidmk/android.go 可以查看,具体如下:
var moduleTypes = map[string]string{
"BUILD_SHARED_LIBRARY": "cc_library_shared",
"BUILD_STATIC_LIBRARY": "cc_library_static",
"BUILD_HOST_SHARED_LIBRARY": "cc_library_host_shared",
"BUILD_HOST_STATIC_LIBRARY": "cc_library_host_static",
"BUILD_HEADER_LIBRARY": "cc_library_headers",
"BUILD_EXECUTABLE": "cc_binary",
"BUILD_HOST_EXECUTABLE": "cc_binary_host",
"BUILD_NATIVE_TEST": "cc_test",
"BUILD_HOST_NATIVE_TEST": "cc_test_host",
"BUILD_NATIVE_BENCHMARK": "cc_benchmark",
"BUILD_HOST_NATIVE_BENCHMARK": "cc_benchmark_host",
"BUILD_JAVA_LIBRARY":
"java_library_installable", // will be rewritten
to java_library by bpfix
"BUILD_STATIC_JAVA_LIBRARY": "java_library",
"BUILD_HOST_JAVA_LIBRARY": "java_library_host",
"BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
"BUILD_PACKAGE":
"BUILD_CTS_EXECUTABLE":
"android_app",
"cc_binary",
// will be further
massaged by bpfix depending on the output path
"BUILD_CTS_SUPPORT_PACKAGE":
"cts_support_package", // will be rewritten to
"cts_package", // will be rewritten to
android_test by bpfix
"BUILD_CTS_PACKAGE":
android_test by bpfix
"BUILD_CTS_TARGET_JAVA_LIBRARY": "cts_target_java_library", // will be rewritten to
java_library by bpfix
"BUILD_CTS_HOST_JAVA_LIBRARY":
"cts_host_java_library",
java_library_host by bpfix
}
2-7.⽀持预编译类型
Android.bp可以⽀持多种预编译类型,具体定义在Android源码的 android-11/build/soong/androidmk/androidmk/android.go
可以查看,如下图所示:
var prebuiltTypes = map[string]string{
"SHARED_LIBRARIES": "cc_prebuilt_library_shared",
"STATIC_LIBRARIES": "cc_prebuilt_library_static",
"EXECUTABLES": "cc_prebuilt_binary",
"JAVA_LIBRARIES": "java_import",
"APPS": "android_app_import",
"ETC": "prebuilt_etc",
}
2-8.条件式编译
system/core/libusbhost$ cat Android.bp
cc_library {
name: "libusbhost",
vendor_available: true,
vndk: {
enabled: true,
},
host_supported: true,
srcs: ["usbhost.c"],
cflags: ["-Werror"],
export_include_dirs: ["include"],
target: {
android: {
//编译Android上运⾏的程序
cflags: [
"-g",
"-DUSE_LIBLOG",
],
shared_libs: ["liblog"],
},
darwin: {
//编译darwin上运⾏的程序
enabled: false,
},
},
}