背景介绍
本篇是一篇实操内容,是对【Android构建系统】如何在Camera Hal的Android.bp中选择性引用某个模块的优化与改进。本篇内容主要想通过一个具体例子介绍Soong构建系统较复杂的定制化方法和步骤,以便在今后的工作学习中更好的使用Soong构建系统。
【Android构建系统】如何在Camera Hal的Android.bp中选择性引用某个模块虽然达到了一定的选择效果,从实际使用角度来说
- 不好用不够灵活
- 定制化程度很受限
- 从项目维护角度来说,不够自动化
简短介绍到这里,下面介绍Android Soong构建系统如何实现更复杂的定制。
Soong构建系统(.bp + .go)定制化步骤
实现步骤:
- 创建一个.go文件,定义并注册自定义构建模块&处理函数
- Android.bp中引用自定义的模块(.go)到soong构建系统 (后边会补充些理解)
- Android.bp中使用自定义.go中定义的模块
- 导出编译控制开关或者变量 (这步是根据实际需要可有可无)
实例:根据构建条件添加预编译宏CAMERA_ENABLE_HW_PROCESS
1.创建my_custom.go,定义并注册模块
//hardware/google/camera/devices/EmulatedCamera/hwl/my_custom.go
package my_custom
import (
"android/soong/android"
"android/soong/cc"
)
func init() {
android.RegisterModuleType("my_custom_defaults", customDefaultsFactory)
}
func customDefaultFactory()(android.Module) {
module := cc.DefaultsFactory()
android.AddLoadHook(module, appendAospBuildParams)
return module
}
func appendAospBuildParams(ctx android.LoadHookContext) {
type props struct {
Cflags []string
}
p := &props{}
p.Cflags = globalDefaults(ctx)
ctx.AppendProperties(p)
}
func globalDefaults(ctx android.BaseContext)([]string) {
var cppflags []string
if ctx.AConfig().Getenv("ANDROIDBP_CUSTOM") == "YES" {
cppflags = append(cppflags, "-DCAMERA_ENABLE_HW_PROCESS")
}
return cppflags
}
2.Android.bp中导入自定义的模块
//hardware/google/camera/devices/EmulatedCamera/hwl/Android.bp文件头添加如下,
bootstrap_go_package { //这里将自定义的构建模块加到Soong构建系统
name: "soong-my_custom",
pkgPath: "android/soong/my_custom",
deps: [
"blueprint",
"blueprint-pathtools",
"soong",
"soong-android",
"soong-cc",
"soong-genrule",
],
srcs: [
"my_custom.go",
],
pluginFor: ["soong_build"],
}
3.Android.bp中引用自定义的模块
//hardware/google/camera/devices/EmulatedCamera/hwl/Android.bp
my_custom_defaults {
name: "custom_cflags_defaults",
}
cc_library_static {
name: "libgooglecamera_process",
owner: "google",
proprietary: true,
host_supported: false,
srcs: [
"a_wrapper.cpp",
"image_processor.cpp",
],
header_libs: [
"libgui_aidl_headers",
"arm_gralloc_headers",
],
static_libs: [
"vendor.hardware.camera.hwprocess",
],
shared_libs: [
"libui",
"libdmabufheap",
],
include_dirs: [
"system/media/private/camera/include",
"frameworks/native/libs/ui/include/",
"frameworks/native/include/",
"vendor/google/hardware/modules/gralloc/android/src",
"system/memdory/libdmabufheap/include",
"external/libyuv/include",
],
export_include_dirs: ["."],
cflags: [
"-Werror",
"-Wextra",
"-Wall",
],
target: {
android_arm64: {
enabled: true,
},
android_x86_64: {
enabled: false,
},
},
}
4.导出开关或者变量
my_custom.go中获取的ANDROIDBP_CUSTOM可以有多种方法导出,例如:
- 执行构建命令前,export ANDROIDBP_CUSTOM=TRUE
- 在打包脚本中(如device/google/gs201/device.mk,device/google/gs201/BoardConfig-common.mk) export ANDROIDBP_CUSTOM=TRUE
5.测试与结果
由于当前没有vendor.hardware.a-V1-ndk,只打开CAMERA_ENABLE_HW_PROCESS会在编译阶段出现未定义错误。