android 10 较 android 9而言,使用了很多Android.bp文件替代Android.mk 文件 虽然有工具实现Android.mk转换为 Android.bp文件了
但如果mk文件编译中带有项目使用的宏实现动态编译如下:
vendor/sprd/platform/packages/apps/ValidationTools/Android.mk
ifneq ($(filter $(strip $(BOARD_HAVE_DTV)), MN88553 ), )
$(warning "revo ValidationTools add LOCAL_JNI_SHARED_LIBRARIES -- librevo_dtv -- !!")
LOCAL_JNI_SHARED_LIBRARIES += librevo_dtv
endif
vendor/sprd/platform/packages/apps/ValidationTools/jni/Android.mk
ifeq ($(strip $(BOARD_HAVE_DTV)),MN88553)
$(warning "librevo_dtv chip $(BOARD_HAVE_DTV)")
LOCAL_SRC_FILES := \
src/mn88553/jniutils.cpp \
src/mn88553/libtuner55x.c \
src/mn88553/libtuner55x_dvr.c \
src/mn88553/mtvhal_drvif.c \
src/mn88553/mtvhal_util.c \
src/mn88553/test_isdbt.c \
src/mn88553/tuner_ac.c \
src/mn88553/test.c \
src/mn88553/tuner_berper.c \
src/mn88553/tuner_cn.c \
src/mn88553/tuner_common.c \
src/mn88553/tuner_dev.c \
src/mn88553/tuner_dvr.c \
src/mn88553/tuner_event.c \
src/mn88553/tuner_init.c \
src/mn88553/tuner_rom.c \
src/mn88553/tuner_rssi.c \
src/mn88553/tuner_rwreg.c \
src/mn88553/tuner_sync.c \
src/mn88553/tuner_tmcc.c \
src/mn88553/tuner_tune.c \
src/mn88553/tunerHwlLnx.c
endif
include $(BUILD_SHARED_LIBRARY)
此处就是实现动态编译的一个地方,如果带有dtv 功能就编译librevo_dtv 模块,否则不编译使用转换工具发现带有宏控制的地方被加了注释
也就是根本没有实现动态编译模块。针对此问题有如下两种解决方法
1.添加go语言编译脚本,实现动态控制编译项也就是预加载的项可以不加,生成的.so文件仅是个空壳。
如下所示
Android.bp 文件添加如下代码
cc_library_shared {
name: "libjni_revo_dtv",
defaults: ["libjni_revo_dtv_module"],
sanitize: {
misc_undefined: ["integer"],
},
}
libjni_revo_dtv {
name: "libjni_revo_dtv_module",
}
/// add start
bootstrap_go_package {
// name and pkgPath need to according to your module
name: "soong-libjni_revo_dtv",
pkgPath: "android/soong/libjni_revo_dtv",
deps: [
"blueprint",
"blueprint-pathtools",
"soong",
"soong-android",
"soong-cc",
"soong-genrule",
],
srcs: [
// include new add .go file
"libjni_revo_dtv.go",
],
pluginFor: ["soong_build"],
}
同时新增一个.go 文件实现动态加载
libjni_revo_dtv.go
package libjni_revo_dtv
import (
"android/soong/android"
"android/soong/cc"
"fmt"
)
var isLoad bool
func init() {
// for DEBUG
fmt.Println("init start")
android.RegisterModuleType("libjni_revo_dtv", Dtv_module_Factory)
}
func Dtv_module_Factory() (android.Module) {
module := cc.DefaultsFactory()
android.AddLoadHook(module, dynamic_load)
return module
}
func dynamic_load(ctx android.LoadHookContext) {
isLoad = globalDefaults(ctx)
if (isLoad){
type props struct {
Cflags []string
Cppflags []string
Srcs []string
Include_dirs []string
Shared_libs []string
Local_include_dirs []string
Static_libs []string
Export_shared_lib_headers []string
}
p := &props{}
p.Srcs = getSrcs(ctx)
p.Local_include_dirs = getLocalIncludeDirs(ctx)
p.Cflags = getCflags(ctx)
p.Cppflags = getCppflags(ctx)
p.Shared_libs = getSharedLibs(ctx)
//p.Static_libs = getStaticLibs(ctx)
//p.Include_dirs = getIncludeDirs(ctx)
//p.Export_shared_lib_headers = getExportSharedLibHeaders(ctx)
ctx.AppendProperties(p)
}
}
func globalDefaults(ctx android.BaseContext) bool {
if ctx.AConfig().IsEnvTrue("BOARD_HAVE_DTV_MN88553") {
fmt.Println("BOARD_HAVE_DTV_MN88553 is true")
return true
}else {
fmt.Println("BOARD_HAVE_DTV_MN88553 is false")
return false
}
}
func getLocalIncludeDirs(ctx android.BaseContext) ([]string) {
var localDirs []string
localDirs = append(localDirs, "src/header")
return localDirs
}
func getSrcs(ctx android.BaseContext) ([]string) {
var srcs []string
srcs = append(srcs, "src/mn88553/jniutils.cpp")
srcs = append(srcs, "src/mn88553/libtuner55x.c")
srcs = append(srcs, "src/mn88553/libtuner55x_dvr.c")
srcs = append(srcs, "src/mn88553/mtvhal_drvif.c")
srcs = append(srcs, "src/mn88553/mtvhal_util.c")
srcs = append(srcs, "src/mn88553/test_isdbt.c")
srcs = append(srcs, "src/mn88553/tuner_ac.c")
srcs = append(srcs, "src/mn88553/test.c")
srcs = append(srcs, "src/mn88553/tuner_berper.c")
srcs = append(srcs, "src/mn88553/tuner_cn.c")
srcs = append(srcs, "src/mn88553/tuner_common.c")
srcs = append(srcs, "src/mn88553/tuner_dev.c")
srcs = append(srcs, "src/mn88553/tuner_dvr.c")
srcs = append(srcs, "src/mn88553/tuner_event.c")
srcs = append(srcs, "src/mn88553/tuner_init.c")
srcs = append(srcs, "src/mn88553/tuner_rom.c")
srcs = append(srcs, "src/mn88553/tuner_rssi.c")
srcs = append(srcs, "src/mn88553/tuner_rwreg.c")
srcs = append(srcs, "src/mn88553/tuner_sync.c")
srcs = append(srcs, "src/mn88553/tuner_tmcc.c")
srcs = append(srcs, "src/mn88553/tuner_tune.c")
srcs = append(srcs, "src/mn88553/tunerHwlLnx.c")
return srcs
}
func getCppflags(ctx android.BaseContext) ([]string) {
var cppflags []string
cppflags = append(cppflags, "-fexceptions")
return cppflags
}
func getCflags(ctx android.BaseContext) ([]string) {
var cflags []string
cflags = append(cflags, "-O3")
cflags = append(cflags, "-fvisibility=hidden")
cflags = append(cflags, "-mfloat-abi=softfp")
cflags = append(cflags, "-mfpu=neon")
cflags = append(cflags, "-pie")
cflags = append(cflags, "-fPIE")
cflags = append(cflags, "-DNDK")
return cflags
}
func getSharedLibs(ctx android.BaseContext) ([]string) {
var libs []string
libs = append(libs, "libcutils")
libs = append(libs, "libutils")
libs = append(libs, "liblog")
return libs
}
func getStaticLibs(ctx android.BaseContext) ([]string) {
var libs []string
//libs = append(libs, "[email protected]")
return libs
}
func getIncludeDirs(ctx android.BaseContext) ([]string) {
var dirs []string
//dirs = append(dirs,"miui/frameworks/base/v29/native/libmqsas")
return dirs
}
func getExportSharedLibHeaders(ctx android.BaseContext) ([]string) {
var libs []string
//libs = append(libs, "libsensor")
//libs = append(libs, "libsensorprivacy")
return libs
}
采用此方法弊端是就算宏没开启,.so库文件还是存在,只是比加宏版本小很多
下面介绍第二种方法:借助原来就带有go文件的脚本,在脚本中动态加载此编译项
由于是在 vendor/sprd/platform/packages/apps/ValidationTools/jni目录下加dtv功能,
所以借助该目录下原来就有的go脚本
Android.bp 文件关于 validation_defaults的代码
cc_library_shared {
name: "libjni_validationtools",
compile_multilib: "first",
srcs: [
"src/jniutils.cpp",
],
defaults: ["validation_defaults"],
shared_libs: [
"libcutils",
"libutils",
"liblog",
],
}
/// add start
bootstrap_go_package {
// name and pkgPath need to according to your module
name: "soong-validation_defaults",
pkgPath: "android/soong/validation_defaults",
deps: [
"blueprint",
"blueprint-pathtools",
"soong",
"soong-android",
"soong-cc",
"soong-genrule",
],
srcs: [
// include new add .go file
"validation_defaults.go",
],
pluginFor: ["soong_build"],
}
// jni_utils is a module
validation_defaults {
name: "validation_defaults_module",
}
/// add end
cc_defaults {
name: "validation_defaults",
defaults: ["validation_defaults_module"],
sanitize: {
misc_undefined: ["integer"],
},
}
validation_defaults.go文件源码
package validation_defaults
import (
"android/soong/android"
"android/soong/cc"
"fmt"
)
func init() {
// for DEBUG
fmt.Println("init start")
android.RegisterModuleType("validation_defaults", validation_defaultsFactory)
}
func validation_defaultsFactory() (android.Module) {
module := cc.DefaultsFactory()
android.AddLoadHook(module, validation_defaults)
return module
}
func validation_defaults(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
sdkVersion := ctx.AConfig().PlatformSdkVersionInt()
fmt.Println("sdkVersion:", sdkVersion)
//Add Dflag USE_AUDIO_WHALE_HAL
fmt.Println("USE_AUDIO_WHALE_HAL:",
ctx.AConfig().IsEnvTrue("USE_AUDIO_WHALE_HAL"))
if ctx.AConfig().IsEnvTrue("USE_AUDIO_WHALE_HAL") {
cppflags = append(cppflags,
"-DAUDIO_WHALE_LOOPBACK=1")
}
//Add Dflag TARGET_CAMERA_SENSOR_CCT
cameraFlagCCT := envDefault(ctx, "TARGET_CAMERA_SENSOR_CCT", "null")
fmt.Println("TARGET_CAMERA_SENSOR_CCT:",cameraFlagCCT)
if cameraFlagCCT == "ams_tcs3430"{
cppflags = append(cppflags,"-DTARGET_CAMERA_SENSOR_CCT_TCS3430")
}
//Add Dflag TARGETARCH
ssss := envDefault(ctx, "PRODUCT_PACKAGES", "null")
fmt.Println("PRODUCT_PACKAGES:",ssss)
//Add Dflag TARGET_CAMERA_SENSOR_TOF
cameraFlagTOF := envDefault(ctx, "TARGET_CAMERA_SENSOR_TOF", "null")
fmt.Println("TARGET_CAMERA_SENSOR_TOF:",cameraFlagTOF)
if cameraFlagTOF == "tof_vl53l0"{
cppflags = append(cppflags,"-DTARGET_CAMERA_SENSOR_TOF_SUPPORT")
}
fmt.Println("TARGET_BOARD_SENSOR_OV4C2:",
ctx.AConfig().IsEnvTrue("TARGET_BOARD_SENSOR_OV4C"))
//Add Dflag BOARD_FEATUREPHONE_CONFIG
fmt.Println("BOARD_FEATUREPHONE_CONFIG:",
ctx.AConfig().IsEnvTrue("BOARD_FEATUREPHONE_CONFIG"))
if ctx.AConfig().IsEnvTrue("BOARD_FEATUREPHONE_CONFIG") {
cppflags = append(cppflags,
"-DBOARD_FEATUREPHONE_CONFIG")
}
return cppflags
}
func envDefault(ctx android.BaseContext, key string, defaultValue string) string {
ret := ctx.AConfig().Getenv(key)
fmt.Println("envDefault key:",key)
fmt.Println("envDefault ret:",ret)
if ret == "" {
return defaultValue
}
return ret
}
需要在Android.bp文件下加入如下代码
cc_library_shared {
name: "libjni_revo_dtv",
defaults: ["validation_defaults_module"],
compile_multilib: "first",
srcs: [
"src/mn88553/jniutils.cpp",
"src/mn88553/libtuner55x.c",
"src/mn88553/libtuner55x_dvr.c",
"src/mn88553/mtvhal_drvif.c",
"src/mn88553/mtvhal_util.c",
"src/mn88553/test_isdbt.c",
"src/mn88553/tuner_ac.c",
"src/mn88553/test.c",
"src/mn88553/tuner_berper.c",
"src/mn88553/tuner_cn.c",
"src/mn88553/tuner_common.c",
"src/mn88553/tuner_dev.c",
"src/mn88553/tuner_dvr.c",
"src/mn88553/tuner_event.c",
"src/mn88553/tuner_init.c",
"src/mn88553/tuner_rom.c",
"src/mn88553/tuner_rssi.c",
"src/mn88553/tuner_rwreg.c",
"src/mn88553/tuner_sync.c",
"src/mn88553/tuner_tmcc.c",
"src/mn88553/tuner_tune.c",
"src/mn88553/tunerHwlLnx.c",
],
local_include_dirs: [
"src/header",
],
shared_libs: [
"libcutils",
"libutils",
"liblog",
],
cflags: [
"-O3",
"-fvisibility=hidden",
"-mfloat-abi=softfp",
"-mfpu=neon",
"-pie",
"-fPIE",
"-DNDK",
],
cppflags: [
"-fexceptions",
],
}
也就是正常情况下加宏的编译代码
此处注意 defaults: ["validation_defaults_module"], 这一行关键代码
接着在 validation_defaults.go文件中加入如下代码
var isLoadDTV bool
func init() {
// for DEBUG
fmt.Println("init start")
android.RegisterModuleType("validation_defaults", validation_defaultsFactory)
if (isLoadDTV){
android.RegisterModuleType("libjni_revo_dtv", Dtv_module_Factory)
}
}
func Dtv_module_Factory() (android.Module) {
module := cc.DefaultsFactory()
//android.AddLoadHook(module, dynamic_load)
return module
}
func globalDefaults(ctx android.BaseContext) ([]string) {
var cppflags []string
sdkVersion := ctx.AConfig().PlatformSdkVersionInt()
fmt.Println("sdkVersion:", sdkVersion)
//Add Dflag USE_AUDIO_WHALE_HAL
fmt.Println("USE_AUDIO_WHALE_HAL:",
ctx.AConfig().IsEnvTrue("USE_AUDIO_WHALE_HAL"))
if ctx.AConfig().IsEnvTrue("USE_AUDIO_WHALE_HAL") {
cppflags = append(cppflags,
"-DAUDIO_WHALE_LOOPBACK=1")
}
//Add Dflag TARGET_CAMERA_SENSOR_CCT
cameraFlagCCT := envDefault(ctx, "TARGET_CAMERA_SENSOR_CCT", "null")
fmt.Println("TARGET_CAMERA_SENSOR_CCT:",cameraFlagCCT)
if cameraFlagCCT == "ams_tcs3430"{
cppflags = append(cppflags,"-DTARGET_CAMERA_SENSOR_CCT_TCS3430")
}
//Add Dflag TARGETARCH
ssss := envDefault(ctx, "PRODUCT_PACKAGES", "null")
fmt.Println("PRODUCT_PACKAGES:",ssss)
//Add Dflag TARGET_CAMERA_SENSOR_TOF
cameraFlagTOF := envDefault(ctx, "TARGET_CAMERA_SENSOR_TOF", "null")
fmt.Println("TARGET_CAMERA_SENSOR_TOF:",cameraFlagTOF)
if cameraFlagTOF == "tof_vl53l0"{
cppflags = append(cppflags,"-DTARGET_CAMERA_SENSOR_TOF_SUPPORT")
}
fmt.Println("TARGET_BOARD_SENSOR_OV4C2:",
ctx.AConfig().IsEnvTrue("TARGET_BOARD_SENSOR_OV4C"))
//Add Dflag BOARD_FEATUREPHONE_CONFIG
fmt.Println("BOARD_FEATUREPHONE_CONFIG:",
ctx.AConfig().IsEnvTrue("BOARD_FEATUREPHONE_CONFIG"))
if ctx.AConfig().IsEnvTrue("BOARD_FEATUREPHONE_CONFIG") {
cppflags = append(cppflags,
"-DBOARD_FEATUREPHONE_CONFIG")
}
isLoadDTV = ctx.AConfig().IsEnvTrue("BOARD_HAVE_DTV_MN88553")
fmt.Println("BOARD_HAVE_DTV_MN88553:",isLoadDTV)
return cppflags
}
注意isLoadDTV 变量声明及赋值地方
var isLoadDTV bool
isLoadDTV = ctx.AConfig().IsEnvTrue("BOARD_HAVE_DTV_MN88553")
以及模块加载地方
android.RegisterModuleType("libjni_revo_dtv", Dtv_module_Factory)
func Dtv_module_Factory() (android.Module) {
module := cc.DefaultsFactory()
//android.AddLoadHook(module, dynamic_load)
return module
}
https://download.csdn.net/download/yong_xu/12015347